[RFC 2/2] testpmd: cleanup cleanly from signal
Stephen Hemminger
stephen at networkplumber.org
Fri Oct 14 19:23:28 CEST 2022
The original behavior of testpmd was to kill itself when
it received a SIGINT or SIGTERM. This makes it hard to use
testpmd in test automation where forwarding loop is started
and then stopped via SIGTERM.
Signed-off-by: Stephen Hemminger <stephen at networkplumber.org>
---
app/test-pmd/testpmd.c | 76 +++++++++++++++++++++++++-----------------
1 file changed, 46 insertions(+), 30 deletions(-)
diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index 815dd6dab4e3..8c19ded1655e 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -15,6 +15,7 @@
#include <sys/types.h>
#include <errno.h>
#include <stdbool.h>
+#include <poll.h>
#include <sys/queue.h>
#include <sys/stat.h>
@@ -4243,25 +4244,37 @@ print_stats(void)
static void
signal_handler(int signum)
{
- if (signum == SIGINT || signum == SIGTERM) {
- fprintf(stderr, "\nSignal %d received, preparing to exit...\n",
- signum);
-#ifdef RTE_LIB_PDUMP
- /* uninitialize packet capture framework */
- rte_pdump_uninit();
-#endif
-#ifdef RTE_LIB_LATENCYSTATS
- if (latencystats_enabled != 0)
- rte_latencystats_uninit();
-#endif
- force_quit();
- /* Set flag to indicate the force termination. */
- f_quit = 1;
- /* exit with the expected status */
-#ifndef RTE_EXEC_ENV_WINDOWS
- signal(signum, SIG_DFL);
- kill(getpid(), signum);
-#endif
+ fprintf(stderr, "\nSignal %d %s received, preparing to exit...\n",
+ signum, strsignal(signum));
+
+ /* Set flag to indicate the force termination. */
+ f_quit = 1;
+}
+
+static int
+wait_for_input(void)
+{
+ struct pollfd pfd = {
+ .fd = STDIN_FILENO,
+ .events = POLLIN,
+ };
+ char c;
+
+ printf("Press enter to exit\n");
+ for (;;) {
+ if (f_quit)
+ return 0;
+
+ if (poll(&pfd, 1, -1) < 0) {
+ if (errno == EINTR)
+ continue;
+ return -1;
+ }
+
+ if (read(STDIN_FILENO, &c, 1) < 0)
+ return -1;
+
+ return 0;
}
}
@@ -4441,11 +4454,6 @@ main(int argc, char** argv)
} else
#endif
{
- char c;
- int rc;
-
- f_quit = 0;
-
printf("No commandline core given, start packet forwarding\n");
start_packet_forwarding(tx_first);
if (stats_period != 0) {
@@ -4468,15 +4476,23 @@ main(int argc, char** argv)
prev_time = cur_time;
rte_delay_us_sleep(US_PER_S);
}
+ } else {
+ if (wait_for_input() < 0)
+ return 1;
}
-
- printf("Press enter to exit\n");
- rc = read(0, &c, 1);
- pmd_test_exit();
- if (rc < 0)
- return 1;
+ stop_packet_forwarding();
+ force_quit();
}
+#ifdef RTE_LIB_PDUMP
+ /* uninitialize packet capture framework */
+ rte_pdump_uninit();
+#endif
+#ifdef RTE_LIB_LATENCYSTATS
+ if (latencystats_enabled != 0)
+ rte_latencystats_uninit();
+#endif
+
ret = rte_eal_cleanup();
if (ret != 0)
rte_exit(EXIT_FAILURE,
--
2.35.1
More information about the dev
mailing list