The profile display loop in main() never terminates, so the
rte_eal_cleanup() and return after it are unreachable. Clang
reports this with -Wunreachable-code.

Add a signal handler setting force_quit, as done by l2fwd and
others, and test it in the loop so that cleanup runs on exit.

Change the main loop to use sleep() which is interruptible
rather than rte_delay_us_sleep() which retries on EINTR.

Signed-off-by: Stephen Hemminger <[email protected]>
---
 examples/service_cores/main.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/examples/service_cores/main.c b/examples/service_cores/main.c
index 9f52082254..2c7a3f7488 100644
--- a/examples/service_cores/main.c
+++ b/examples/service_cores/main.c
@@ -2,6 +2,8 @@
  * Copyright(c) 2017 Intel Corporation
  */
 
+#include <signal.h>
+#include <stdbool.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <string.h>
@@ -163,6 +165,15 @@ apply_profile(int profile_id)
        }
 }
 
+static volatile bool force_quit;
+
+static void
+signal_handler(int signum)
+{
+       if (signum == SIGINT || signum == SIGTERM)
+               force_quit = true;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -172,6 +183,9 @@ main(int argc, char **argv)
        if (ret < 0)
                rte_panic("Cannot init EAL\n");
 
+       signal(SIGINT, signal_handler);
+       signal(SIGTERM, signal_handler);
+
        uint32_t i;
        for (i = 0; i < NUM_SERVICES; i++) {
                services[i].callback_userdata = 0;
@@ -203,7 +217,7 @@ main(int argc, char **argv)
        }
 
        i = 0;
-       while (1) {
+       while (!force_quit) {
                const char clr[] = { 27, '[', '2', 'J', '\0' };
                const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
                printf("%s%s", clr, topLeft);
@@ -211,10 +225,13 @@ main(int argc, char **argv)
                apply_profile(i);
                printf("\n==> Profile: %s\n\n", profiles[i].name);
 
-               rte_delay_us_sleep(1 * US_PER_S);
+               /* sleep() returns the time left if interrupted by a signal */
+               if (sleep(1) != 0)
+                       break;
                rte_service_dump(stdout, UINT32_MAX);
 
-               rte_delay_us_sleep(5 * US_PER_S);
+               if (sleep(5) != 0)
+                       break;
                rte_service_dump(stdout, UINT32_MAX);
 
                i++;
-- 
2.53.0

Reply via email to