Errors in command line options and in setting up the EAL arguments
were reported with rte_exit() or rte_panic(), which call EAL cleanup
or dump EAL state before EAL has been initialized.

Use a simple helper which prints the message and exits instead.

Fixes: cbb44143be74 ("app/dumpcap: add new packet capture application")
Cc: [email protected]

Signed-off-by: Stephen Hemminger <[email protected]>
---
 app/dumpcap/main.c | 57 +++++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 24 deletions(-)

diff --git a/app/dumpcap/main.c b/app/dumpcap/main.c
index 1f9f03c7a3..49979fbf37 100644
--- a/app/dumpcap/main.c
+++ b/app/dumpcap/main.c
@@ -12,6 +12,7 @@
 #include <inttypes.h>
 #include <limits.h>
 #include <signal.h>
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -163,6 +164,22 @@ static const char *version(void)
        return str;
 }
 
+/*
+ * Exit with error message, for errors before EAL is initialized
+ * where rte_exit() can not be used.
+ */
+static __rte_noreturn __rte_format_printf(1, 2)
+void early_exit(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       vfprintf(stderr, fmt, ap);
+       va_end(ap);
+
+       exit(EXIT_FAILURE);
+}
+
 /* Parse numeric argument from command line */
 static unsigned long get_uint(const char *arg, const char *name,
                             unsigned int limit)
@@ -172,13 +189,11 @@ static unsigned long get_uint(const char *arg, const char 
*name,
 
        u = strtoul(arg, &endp, 0);
        if (*arg == '\0' || *endp != '\0')
-               rte_exit(EXIT_FAILURE,
-                        "Specified %s \"%s\" is not a valid number\n",
-                        name, arg);
+               early_exit("Specified %s \"%s\" is not a valid number\n",
+                          name, arg);
        if (limit && u > limit)
-               rte_exit(EXIT_FAILURE,
-                        "Specified %s \"%s\" is too large (greater than %u)\n",
-                        name, arg, limit);
+               early_exit("Specified %s \"%s\" is too large (greater than 
%u)\n",
+                          name, arg, limit);
 
        return u;
 }
@@ -190,24 +205,21 @@ static void auto_stop(char *opt)
 
        value = strchr(opt, ':');
        if (value == NULL)
-               rte_exit(EXIT_FAILURE,
-                        "Missing colon in auto stop parameter\n");
+               early_exit("Missing colon in auto stop parameter\n");
 
        *value++ = '\0';
        if (strcmp(opt, "duration") == 0) {
                double interval = strtod(value, &endp);
 
                if (*value == '\0' || *endp != '\0' || interval <= 0)
-                       rte_exit(EXIT_FAILURE,
-                                "Invalid duration \"%s\"\n", value);
+                       early_exit("Invalid duration \"%s\"\n", value);
                stop.duration = interval;
        } else if (strcmp(opt, "filesize") == 0) {
                stop.size = get_uint(value, "filesize", 0) * 1024;
        } else if (strcmp(opt, "packets") == 0) {
                stop.packets = get_uint(value, "packets", 0);
        } else {
-               rte_exit(EXIT_FAILURE,
-                        "Unknown autostop parameter \"%s\"\n", opt);
+               early_exit("Unknown autostop parameter \"%s\"\n", opt);
        }
 }
 
@@ -217,11 +229,11 @@ static struct interface *add_interface(const char *name)
        struct interface *intf;
 
        if (strlen(name) >= RTE_ETH_NAME_MAX_LEN)
-               rte_exit(EXIT_FAILURE, "invalid name for interface: '%s'\n", 
name);
+               early_exit("invalid name for interface: '%s'\n", name);
 
        intf = malloc(sizeof(*intf));
        if (!intf)
-               rte_exit(EXIT_FAILURE, "no memory for interface\n");
+               early_exit("no memory for interface\n");
 
        memset(intf, 0, sizeof(*intf));
        rte_strscpy(intf->name, name, sizeof(intf->name));
@@ -381,13 +393,11 @@ static void parse_opts(int argc, char **argv)
                                tmp_dir = optarg;
                        } else if (!strcmp(longopt, "ifdescr")) {
                                if (last_intf == NULL)
-                                       rte_exit(EXIT_FAILURE,
-                                                "--ifdescr must be specified 
after a -i option\n");
+                                       early_exit("--ifdescr must be specified 
after a -i option\n");
                                last_intf->ifdescr = optarg;
                        } else if (!strcmp(longopt, "ifname")) {
                                if (last_intf == NULL)
-                                       rte_exit(EXIT_FAILURE,
-                                                "--ifname must be specified 
after a -i option\n");
+                                       early_exit("--ifname must be specified 
after a -i option\n");
                                last_intf->ifname = optarg;
                        } else {
                                usage();
@@ -399,8 +409,7 @@ static void parse_opts(int argc, char **argv)
                        auto_stop(optarg);
                        break;
                case 'b':
-                       rte_exit(EXIT_FAILURE,
-                                "multiple files not implemented\n");
+                       early_exit("multiple files not implemented\n");
                        break;
                case 'c':
                        stop.packets = get_uint(optarg, "packet_count", 0);
@@ -629,7 +638,7 @@ static void dpdk_init(void)
        /* DPDK API requires mutable versions of command line arguments. */
        eal_argv = calloc(eal_argc + 1, sizeof(char *));
        if (eal_argv == NULL)
-               rte_panic("No memory\n");
+               early_exit("No memory\n");
 
        eal_argv[0] = strdup(progname);
        for (i = 1; i < RTE_DIM(args); i++)
@@ -647,7 +656,7 @@ static void dpdk_init(void)
 
        for (i = 0; i < (unsigned int)eal_argc; i++) {
                if (eal_argv[i] == NULL)
-                       rte_panic("No memory\n");
+                       early_exit("No memory\n");
        }
 
        /*
@@ -656,10 +665,10 @@ static void dpdk_init(void)
         */
        if (lcore_arg == NULL &&
            rte_thread_get_affinity_by_id(rte_thread_self(), &cpuset) != 0)
-               rte_panic("rte_thread_getaffinity failed\n");
+               early_exit("rte_thread_getaffinity failed\n");
 
        if (rte_eal_init(eal_argc, eal_argv) < 0)
-               rte_exit(EXIT_FAILURE, "EAL init failed: is primary process 
running?\n");
+               early_exit("EAL init failed: is primary process running?\n");
 
        /*
         * Register pdump callback handler.
-- 
2.53.0

Reply via email to