Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / destroy.c
index eed1fe5c5878fc15679009c64b18e0788adf0cf9..d3e2ac59fd37db36417c1a8b96f49ecd8fd08eb2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
+ * Copyright (C) 2011 EfficiOS Inc.
  *
  * SPDX-License-Identifier: GPL-2.0-only
  *
@@ -22,7 +22,6 @@
 #include <common/sessiond-comm/sessiond-comm.h>
 #include <common/utils.h>
 
-static char *opt_session_name;
 static int opt_destroy_all;
 static int opt_no_wait;
 
@@ -87,10 +86,13 @@ static int destroy_session(struct lttng_session *session)
                         * availability.
                         */
                        if (ret) {
-                               _MSG("Destroying session %s", session->name);
-                               newline_needed = true;
-                               printed_destroy_msg = true;
-                               fflush(stdout);
+                               if (!printed_destroy_msg) {
+                                       _MSG("Destroying session %s",
+                                                       session->name);
+                                       newline_needed = true;
+                                       printed_destroy_msg = true;
+                                       fflush(stdout);
+                               }
 
                                usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
                                _MSG(".");
@@ -267,9 +269,10 @@ int cmd_destroy(int argc, const char **argv)
        int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       const char *arg_session_name = NULL;
        const char *leftover = NULL;
 
-       struct lttng_session *sessions;
+       struct lttng_session *sessions = NULL;
        int count;
        int found;
 
@@ -339,18 +342,22 @@ int cmd_destroy(int argc, const char **argv)
                        success = 0;
                }
        } else {
-               opt_session_name = (char *) poptGetArg(pc);
+               arg_session_name = poptGetArg(pc);
 
-               if (!opt_session_name) {
+               if (!arg_session_name) {
                        /* No session name specified, lookup default */
                        session_name = get_session_name();
+               } else {
+                       session_name = strdup(arg_session_name);
                        if (session_name == NULL) {
-                               command_ret = CMD_ERROR;
-                               success = 0;
-                               goto mi_closing;
+                               PERROR("Failed to copy session name");
                        }
-               } else {
-                       session_name = opt_session_name;
+               }
+
+               if (session_name == NULL) {
+                       command_ret = CMD_ERROR;
+                       success = 0;
+                       goto mi_closing;
                }
 
                /* Find the corresponding lttng_session struct */
@@ -416,9 +423,8 @@ end:
                ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
        }
 
-       if (opt_session_name == NULL) {
-               free(session_name);
-       }
+       free(session_name);
+       free(sessions);
 
        /* Overwrite ret if an error occurred during destroy_session/all */
        ret = command_ret ? command_ret : ret;
This page took 0.024446 seconds and 4 git commands to generate.