diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c index eeca7171a7..781b7ce0db 100644 --- a/app/test-pmd/cmdline.c +++ b/app/test-pmd/cmdline.c @@ -70,6 +70,8 @@ static void cmd_reconfig_device_queue(portid_t id, uint8_t dev, uint8_t queue); +static struct cmdline *testpmd_cl; + /* *** Help command with introduction. *** */ struct cmd_help_brief_result { cmdline_fixed_string_t help; @@ -18039,30 +18041,29 @@ cmdline_read_from_file(const char *filename) printf("Read CLI commands from %s\n", filename); } +void +prompt_exit(void) +{ + cmdline_quit(testpmd_cl); +} + /* prompt function, called from main on MAIN lcore */ void prompt(void) { - struct cmdline *cl; - /* initialize non-constant commands */ cmd_set_fwd_mode_init(); cmd_set_fwd_retry_mode_init(); - cl = cmdline_stdin_new(main_ctx, "testpmd> "); - if (cl == NULL) + testpmd_cl = cmdline_stdin_new(main_ctx, "testpmd> "); + if (testpmd_cl == NULL) { + fprintf(stderr, + "Failed to create stdin based cmdline context\n"); return; - - /* loop until signal or quit command */ - while (f_quit == 0 && cl_quit == 0) { - int status = cmdline_poll(cl); - - if (status < 0 || status == RDLINE_EXITED) - break; } - cmdline_quit(cl); - cmdline_stdin_exit(cl); + cmdline_interact(testpmd_cl); + cmdline_stdin_exit(testpmd_cl); } static void diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c index bade1feba4..fbfc090d68 100644 --- a/app/test-pmd/testpmd.c +++ b/app/test-pmd/testpmd.c @@ -4321,6 +4321,7 @@ static void signal_handler(int signum __rte_unused) { f_quit = 1; + prompt_exit(); } int @@ -4331,8 +4332,18 @@ main(int argc, char** argv) uint16_t count; int ret; +#ifdef RTE_EXEC_ENV_WINDOWS signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); +#else + /* Want read() not to be restarted on signal */ + struct sigaction action = { + .sa_handler = signal_handler, + }; + + sigaction(SIGINT, &action, NULL); + sigaction(SIGTERM, &action, NULL); +#endif testpmd_logtype = rte_log_register("testpmd"); if (testpmd_logtype < 0) diff --git a/lib/cmdline/cmdline.c b/lib/cmdline/cmdline.c index 5600f012c2..d0601d69f4 100644 --- a/lib/cmdline/cmdline.c +++ b/lib/cmdline/cmdline.c @@ -175,6 +175,7 @@ cmdline_quit(struct cmdline *cl) { if (!cl) return; + cmdline_cancel(cl); rdline_quit(&cl->rdl); } diff --git a/lib/cmdline/cmdline_os_unix.c b/lib/cmdline/cmdline_os_unix.c index 64a945a34f..9a4ec4e334 100644 --- a/lib/cmdline/cmdline_os_unix.c +++ b/lib/cmdline/cmdline_os_unix.c @@ -51,3 +51,9 @@ cmdline_vdprintf(int fd, const char *format, va_list op) { return vdprintf(fd, format, op); } + +/* This function is not needed on Linux, instead use sigaction() */ +void +cmdline_cancel(__rte_unused struct cmdline *cl) +{ +} diff --git a/lib/cmdline/cmdline_os_windows.c b/lib/cmdline/cmdline_os_windows.c index 73ed9ba290..80863bfc8a 100644 --- a/lib/cmdline/cmdline_os_windows.c +++ b/lib/cmdline/cmdline_os_windows.c @@ -203,3 +203,17 @@ cmdline_vdprintf(int fd, const char *format, va_list op) return ret; } + +void +cmdline_cancel(struct cmdline *cl) +{ + if (!cl) + return; + + /* force the outstanding read on console to exit */ + if (cl->oldterm.is_console_input) { + HANDLE handle = (HANDLE)_get_osfhandle(cl->s_in); + + CancelIoEx(handle, NULL); + } +} diff --git a/lib/cmdline/cmdline_private.h b/lib/cmdline/cmdline_private.h index a3271c7693..86a46cdea6 100644 --- a/lib/cmdline/cmdline_private.h +++ b/lib/cmdline/cmdline_private.h @@ -24,7 +24,7 @@ #define RDLINE_HISTORY_MAX_LINE 64 struct rdline { - enum rdline_status status; + volatile enum rdline_status status; /* rdline bufs */ struct cirbuf left; struct cirbuf right; @@ -90,6 +90,9 @@ int cmdline_poll_char(struct cmdline *cl); /* Read one character from input. */ ssize_t cmdline_read_char(struct cmdline *cl, char *c); +/* Force current cmdline read to unblock. */ +void cmdline_cancel(struct cmdline *cl); + /* vdprintf(3) */ __rte_format_printf(2, 0) int cmdline_vdprintf(int fd, const char *format, va_list op);