Fix: lttng: poptGetArg doesn't provide string ownership
authorMichael Jeanson <mjeanson@efficios.com>
Mon, 31 Oct 2022 19:52:46 +0000 (15:52 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 5 Jan 2023 23:27:15 +0000 (18:27 -0500)
The string returned by poptGetArg() is 'const' because it is owned by
the popt librairy and is free'd by it when poptFreeContext() is called.
Copy those strings when we need to alter them to maintain proper
ownership.

The latest release of popt (v1.19) introduced a breaking
change (changing the ownership of left-over command line arguments) that
can cause double free()-s.

This is ultimately due to this upstream commit in popt 1.19:
https://github.com/rpm-software-management/popt/commit/7182e4618ad5a0186145fc2aa4a98c2229afdfa8

which is derived from a package patch:
https://src.fedoraproject.org/rpms/babeltrace/c/d48452beff87b145c038f070e7182358db04336c?branch=rawhide

Change-Id: Id2535d1534c0e47cc0747968d6dd60a587f0b810
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
15 files changed:
src/bin/lttng/commands/clear.c
src/bin/lttng/commands/create.c
src/bin/lttng/commands/destroy.c
src/bin/lttng/commands/disable_channels.c
src/bin/lttng/commands/disable_events.c
src/bin/lttng/commands/enable_channels.c
src/bin/lttng/commands/enable_events.c
src/bin/lttng/commands/help.c
src/bin/lttng/commands/list.c
src/bin/lttng/commands/rotate.c
src/bin/lttng/commands/save.c
src/bin/lttng/commands/set_session.c
src/bin/lttng/commands/start.c
src/bin/lttng/commands/stop.c
src/bin/lttng/commands/view.c

index b3974837fa987c660271d10890293164335b0f44..41d2a73843cfbe618b2793bbd9d350197040d55e 100644 (file)
@@ -157,8 +157,8 @@ int cmd_clear(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;
-       bool free_session_name = false;
        struct lttng_session *sessions = NULL;
        int count;
        int found;
@@ -214,19 +214,22 @@ int cmd_clear(int argc, const char **argv)
        }
 
        if (!opt_clear_all) {
-               session_name = (char *) poptGetArg(pc);
-               if (!session_name) {
+               arg_session_name = poptGetArg(pc);
+               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");
                        }
-                       free_session_name = true;
                }
-       } else {
-               session_name = NULL;
+
+               if (session_name == NULL) {
+                       command_ret = CMD_ERROR;
+                       success = 0;
+                       goto mi_closing;
+               }
        }
 
        leftover = poptGetArg(pc);
@@ -308,9 +311,7 @@ end:
        }
 
        free(sessions);
-       if (free_session_name) {
-               free(session_name);
-       }
+       free(session_name);
 
        /* Overwrite ret if an error occurred during clear_session/all */
        ret = command_ret ? command_ret : ret;
index 705605c92826e3b214eaf2e05c47f449d418445b..02087d852d706de9fa67fd4d49e477798de54f4b 100644 (file)
@@ -35,7 +35,6 @@
 #include <lttng/lttng.h>
 
 static char *opt_output_path;
-static char *opt_session_name;
 static char *opt_url;
 static char *opt_ctrl_url;
 static char *opt_data_url;
@@ -133,7 +132,7 @@ end:
 }
 
 static
-struct lttng_session_descriptor *create_session_descriptor(void)
+struct lttng_session_descriptor *create_session_descriptor(const char *session_name)
 {
        int ret;
        ssize_t uri_count;
@@ -203,17 +202,17 @@ struct lttng_session_descriptor *create_session_descriptor(void)
                case OUTPUT_UNSPECIFIED:
                case OUTPUT_LOCAL:
                        descriptor = lttng_session_descriptor_snapshot_local_create(
-                                       opt_session_name,
+                                       session_name,
                                        output_type == OUTPUT_LOCAL ?
                                                local_output_path : NULL);
                        break;
                case OUTPUT_NONE:
                        descriptor = lttng_session_descriptor_snapshot_create(
-                                       opt_session_name);
+                                       session_name);
                        break;
                case OUTPUT_NETWORK:
                        descriptor = lttng_session_descriptor_snapshot_network_create(
-                                       opt_session_name, uri_str1, uri_str2);
+                                       session_name, uri_str1, uri_str2);
                        break;
                default:
                        abort();
@@ -226,7 +225,7 @@ struct lttng_session_descriptor *create_session_descriptor(void)
                        goto end;
                }
                descriptor = lttng_session_descriptor_live_network_create(
-                               opt_session_name, uri_str1, uri_str2,
+                               session_name, uri_str1, uri_str2,
                                opt_live_timer);
        } else {
                /* Regular session. */
@@ -234,17 +233,17 @@ struct lttng_session_descriptor *create_session_descriptor(void)
                case OUTPUT_UNSPECIFIED:
                case OUTPUT_LOCAL:
                        descriptor = lttng_session_descriptor_local_create(
-                                       opt_session_name,
+                                       session_name,
                                        output_type == OUTPUT_LOCAL ?
                                                local_output_path : NULL);
                        break;
                case OUTPUT_NONE:
                        descriptor = lttng_session_descriptor_create(
-                                       opt_session_name);
+                                       session_name);
                        break;
                case OUTPUT_NETWORK:
                        descriptor = lttng_session_descriptor_network_create(
-                                       opt_session_name, uri_str1, uri_str2);
+                                       session_name, uri_str1, uri_str2);
                        break;
                default:
                        abort();
@@ -281,7 +280,7 @@ end:
  *
  *  Returns one of the CMD_* result constants.
  */
-static int create_session(void)
+static int create_session(const char *session_name)
 {
        int ret, i;
        char shm_path[LTTNG_PATH_MAX] = {};
@@ -293,8 +292,8 @@ static int create_session(void)
        const char *created_session_name;
 
        /* Validate options. */
-       if (opt_session_name) {
-               if (strlen(opt_session_name) > NAME_MAX) {
+       if (session_name) {
+               if (strlen(session_name) > NAME_MAX) {
                        ERR("Session name too long. Length must be lower or equal to %d",
                                        NAME_MAX);
                        ret = CMD_ERROR;
@@ -305,11 +304,11 @@ static int create_session(void)
                 * Both are reserved for the default session name. See bug #449 to
                 * understand why we need to check both here.
                 */
-               if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
+               if ((strncmp(session_name, DEFAULT_SESSION_NAME "-",
                                        strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
-                               (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
+                               (strncmp(session_name, DEFAULT_SESSION_NAME,
                                        strlen(DEFAULT_SESSION_NAME)) == 0 &&
-                               strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
+                               strlen(session_name) == strlen(DEFAULT_SESSION_NAME))) {
                        ERR("%s is a reserved keyword for default session(s)",
                                        DEFAULT_SESSION_NAME);
                        ret = CMD_ERROR;
@@ -329,7 +328,7 @@ static int create_session(void)
                goto error;
        }
 
-       session_descriptor = create_session_descriptor();
+       session_descriptor = create_session_descriptor(session_name);
        if (!session_descriptor) {
                ret = CMD_ERROR;
                goto error;
@@ -375,7 +374,7 @@ static int create_session(void)
                 * An auto-generated session name already includes the creation
                 * timestamp.
                 */
-               if (opt_session_name) {
+               if (session_name) {
                        uint64_t creation_time;
                        struct tm *timeinfo;
                        time_t creation_time_t;
@@ -655,6 +654,7 @@ int cmd_create(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        char *opt_arg = NULL;
+       const char *arg_session_name = NULL;
        const char *leftover = NULL;
        static poptContext pc;
 
@@ -761,7 +761,9 @@ int cmd_create(int argc, const char **argv)
                        goto end;
                }
        }
-       opt_session_name = (char*) poptGetArg(pc);
+
+       /* Get the optional session name argument. */
+       arg_session_name = poptGetArg(pc);
 
        leftover = poptGetArg(pc);
        if (leftover) {
@@ -770,7 +772,7 @@ int cmd_create(int argc, const char **argv)
                goto end;
        }
 
-       command_ret = create_session();
+       command_ret = create_session(arg_session_name);
        if (command_ret) {
                success = 0;
        }
index e825d9c3e7dfa01ac5ca31399eb3b469ea17b07c..d3e2ac59fd37db36417c1a8b96f49ecd8fd08eb2 100644 (file)
@@ -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;
 
@@ -270,6 +269,7 @@ 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 = NULL;
@@ -342,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 */
@@ -419,10 +423,7 @@ 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 */
index 36f4308697ad1b149313bbc6fcd15cf0a9b1f3c9..2305d79076e602d2ddd716e7ad98bdea55de32eb 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "../command.h"
 
-static char *opt_channels;
 static int opt_kernel;
 static char *opt_session_name;
 static int opt_userspace;
@@ -94,7 +93,7 @@ end:
 /*
  * Disabling channel using the lttng API.
  */
-static int disable_channels(char *session_name)
+static int disable_channels(char *session_name, char *channel_list)
 {
        int ret = CMD_SUCCESS, warn = 0, success;
 
@@ -133,7 +132,7 @@ static int disable_channels(char *session_name)
        }
 
        /* Strip channel list */
-       channel_name = strtok(opt_channels, ",");
+       channel_name = strtok(channel_list, ",");
        while (channel_name != NULL) {
                DBG("Disabling channel %s", channel_name);
 
@@ -206,6 +205,8 @@ int cmd_disable_channels(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       char *channel_list = NULL;
+       const char *arg_channel_list = NULL;
        const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
@@ -235,9 +236,16 @@ int cmd_disable_channels(int argc, const char **argv)
                goto end;
        }
 
-       opt_channels = (char*) poptGetArg(pc);
-       if (opt_channels == NULL) {
-               ERR("Missing channel name(s).\n");
+       arg_channel_list = poptGetArg(pc);
+       if (arg_channel_list == NULL) {
+               ERR("Missing channel name(s).");
+               ret = CMD_ERROR;
+               goto end;
+       }
+
+       channel_list = strdup(arg_channel_list);
+       if (channel_list == NULL) {
+               PERROR("Failed to copy channel name");
                ret = CMD_ERROR;
                goto end;
        }
@@ -284,7 +292,7 @@ int cmd_disable_channels(int argc, const char **argv)
                }
        }
 
-       command_ret = disable_channels(session_name);
+       command_ret = disable_channels(session_name, channel_list);
        if (command_ret) {
                success = 0;
        }
@@ -325,6 +333,8 @@ end:
                free(session_name);
        }
 
+       free(channel_list);
+
        /* Overwrite ret if an error occurred in disable_channels */
        ret = command_ret ? command_ret : ret;
 
index d3a20df98be494d1a53404b51471468ab97fb176..338c5409504275223637086f6a259ff0e33b2d05 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "../command.h"
 
-static char *opt_event_list;
 static int opt_kernel;
 static char *opt_channel_name;
 static char *opt_session_name;
@@ -151,7 +150,7 @@ end:
  *
  *  Disabling event using the lttng API.
  */
-static int disable_events(char *session_name)
+static int disable_events(char *session_name, char *event_list)
 {
        int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS;
        int enabled = 1, success = 1;
@@ -240,7 +239,7 @@ static int disable_events(char *session_name)
                }
        } else {
                /* Strip event list */
-               event_name = strtok(opt_event_list, ",");
+               event_name = strtok(event_list, ",");
                while (event_name != NULL) {
                        DBG("Disabling event %s", event_name);
 
@@ -318,6 +317,8 @@ int cmd_disable_events(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       char *event_list = NULL;
+       const char *arg_event_list = NULL;
        const char *leftover = NULL;
        int event_type = -1;
 
@@ -384,13 +385,22 @@ int cmd_disable_events(int argc, const char **argv)
                goto end;
        }
 
-       opt_event_list = (char*) poptGetArg(pc);
-       if (opt_event_list == NULL && opt_disable_all == 0) {
+       arg_event_list = poptGetArg(pc);
+       if (arg_event_list == NULL && opt_disable_all == 0) {
                ERR("Missing event name(s).\n");
                ret = CMD_ERROR;
                goto end;
        }
 
+       if (opt_disable_all == 0) {
+               event_list = strdup(arg_event_list);
+               if (event_list == NULL) {
+                       PERROR("Failed to copy event name(s)");
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+       }
+
        leftover = poptGetArg(pc);
        if (leftover) {
                ERR("Unknown argument: %s", leftover);
@@ -433,7 +443,7 @@ int cmd_disable_events(int argc, const char **argv)
                }
        }
 
-       command_ret = disable_events(session_name);
+       command_ret = disable_events(session_name, event_list);
        if (command_ret) {
                success = 0;
        }
@@ -467,6 +477,8 @@ end:
                free(session_name);
        }
 
+       free(event_list);
+
        /* Mi clean-up */
        if (writer && mi_lttng_writer_destroy(writer)) {
                /* Preserve original error code */
index f28b6751993069c45753acdbc929605cf480cfb0..343df60e4a86e632c4ab60244c69e3e7c7806055 100644 (file)
@@ -26,7 +26,6 @@
 
 
 static struct lttng_channel chan_opts;
-static char *opt_channels;
 static int opt_kernel;
 static char *opt_session_name;
 static int opt_userspace;
@@ -138,7 +137,7 @@ static void set_default_attr(struct lttng_domain *dom)
 /*
  * Adding channel using the lttng API.
  */
-static int enable_channel(char *session_name)
+static int enable_channel(char *session_name, char *channel_list)
 {
        struct lttng_channel *channel = NULL;
        int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0;
@@ -229,7 +228,7 @@ static int enable_channel(char *session_name)
        }
 
        /* Strip channel list (format: chan1,chan2,...) */
-       channel_name = strtok(opt_channels, ",");
+       channel_name = strtok(channel_list, ",");
        while (channel_name != NULL) {
                void *extended_ptr;
 
@@ -389,7 +388,9 @@ int cmd_enable_channels(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       char *channel_list = NULL;
        char *opt_arg = NULL;
+       const char *arg_channel_list = NULL;
        const char *leftover = NULL;
 
        init_channel_config();
@@ -689,9 +690,17 @@ int cmd_enable_channels(int argc, const char **argv)
                }
        }
 
-       opt_channels = (char*) poptGetArg(pc);
-       if (opt_channels == NULL) {
-               ERR("Missing channel name.\n");
+       arg_channel_list = poptGetArg(pc);
+       if (arg_channel_list == NULL) {
+               ERR("Missing channel name.");
+               ret = CMD_ERROR;
+               success = 0;
+               goto mi_closing;
+       }
+
+       channel_list = strdup(arg_channel_list);
+       if (channel_list == NULL) {
+               PERROR("Failed to copy channel name");
                ret = CMD_ERROR;
                success = 0;
                goto mi_closing;
@@ -716,7 +725,7 @@ int cmd_enable_channels(int argc, const char **argv)
                session_name = opt_session_name;
        }
 
-       command_ret = enable_channel(session_name);
+       command_ret = enable_channel(session_name, channel_list);
        if (command_ret) {
                success = 0;
        }
@@ -755,6 +764,8 @@ end:
                free(session_name);
        }
 
+       free(channel_list);
+
        /* Overwrite ret if an error occurred when enable_channel */
        ret = command_ret ? command_ret : ret;
        poptFreeContext(pc);
index 8dc44a1fca1a6bb027950524bbf9f5eced3cae16..a833c23ad4a3fe7162867ca41803633276a541f8 100644 (file)
@@ -33,7 +33,6 @@
 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API    "255"
 #endif
 
-static char *opt_event_list;
 static int opt_event_type;
 static const char *opt_loglevel;
 static int opt_loglevel_type;
@@ -962,7 +961,7 @@ static void warn_on_truncated_exclusion_names(char * const *exclusion_list,
  * Enabling event using the lttng API.
  * Note: in case of error only the last error code will be return.
  */
-static int enable_events(char *session_name)
+static int enable_events(char *session_name, char *event_list)
 {
        int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
        int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
@@ -1326,7 +1325,7 @@ static int enable_events(char *session_name)
        }
 
        /* Strip event list */
-       event_name = strtok(opt_event_list, ",");
+       event_name = strtok(event_list, ",");
        while (event_name != NULL) {
                /* Copy name and type of the event */
                strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
@@ -1708,6 +1707,8 @@ int cmd_enable_events(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       char *event_list = NULL;
+       const char *arg_event_list = NULL;
        const char *leftover = NULL;
        int event_type = -1;
 
@@ -1806,13 +1807,22 @@ int cmd_enable_events(int argc, const char **argv)
                }
        }
 
-       opt_event_list = (char*) poptGetArg(pc);
-       if (opt_event_list == NULL && opt_enable_all == 0) {
-               ERR("Missing event name(s).\n");
+       arg_event_list = poptGetArg(pc);
+       if (arg_event_list == NULL && opt_enable_all == 0) {
+               ERR("Missing event name(s).");
                ret = CMD_ERROR;
                goto end;
        }
 
+       if (opt_enable_all == 0) {
+               event_list = strdup(arg_event_list);
+               if (event_list == NULL) {
+                       PERROR("Failed to copy event name(s)");
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+       }
+
        leftover = poptGetArg(pc);
        if (leftover) {
                ERR("Unknown argument: %s", leftover);
@@ -1831,7 +1841,7 @@ int cmd_enable_events(int argc, const char **argv)
                session_name = opt_session_name;
        }
 
-       command_ret = enable_events(session_name);
+       command_ret = enable_events(session_name, event_list);
        if (command_ret) {
                success = 0;
                goto mi_closing;
@@ -1873,6 +1883,8 @@ end:
                free(session_name);
        }
 
+       free(event_list);
+
        /* Overwrite ret if an error occurred in enable_events */
        ret = command_ret ? command_ret : ret;
 
index 7ef21c2f58008117d5d2a645a890e0819af067e4..bf5b603bd313ae99924c99f2911b96ab88555efa 100644 (file)
@@ -47,7 +47,7 @@ static struct poptOption long_options[] = {
 int cmd_help(int argc, const char **argv, const struct cmd_struct commands[])
 {
        int opt, ret = CMD_SUCCESS;
-       char *cmd_name;
+       const char *arg_cmd_name;
        static poptContext pc;
        const struct cmd_struct *cmd;
        int found = 0;
@@ -71,9 +71,8 @@ int cmd_help(int argc, const char **argv, const struct cmd_struct commands[])
        }
 
        /* Get command name */
-       cmd_name = (char *) poptGetArg(pc);
-
-       if (cmd_name == NULL) {
+       arg_cmd_name = poptGetArg(pc);
+       if (arg_cmd_name == NULL) {
                /* Fall back to lttng(1) */
                ret = utils_show_help(1, "lttng", lttng_help_msg);
                if (ret) {
@@ -86,7 +85,7 @@ int cmd_help(int argc, const char **argv, const struct cmd_struct commands[])
        }
 
        /* Help about help? */
-       if (strcmp(cmd_name, "help") == 0) {
+       if (strcmp(arg_cmd_name, "help") == 0) {
                SHOW_HELP();
                goto end;
        }
@@ -95,7 +94,7 @@ int cmd_help(int argc, const char **argv, const struct cmd_struct commands[])
        cmd = &commands[0];
 
        while (cmd->name != NULL) {
-               if (strcmp(cmd->name, cmd_name) == 0) {
+               if (strcmp(cmd->name, arg_cmd_name) == 0) {
                        found = 1;
                        break;
                }
@@ -104,7 +103,7 @@ int cmd_help(int argc, const char **argv, const struct cmd_struct commands[])
        }
 
        if (!found) {
-               ERR("Unknown command \"%s\"", cmd_name);
+               ERR("Unknown command \"%s\"", arg_cmd_name);
                ret = CMD_ERROR;
                goto end;
        }
index 654cb6f11028ba7bce77d286cc0b47b41d1cfbf5..58e2a6fb48d6a190b46fd2cec3de5a2f6c8a8cd7 100644 (file)
@@ -2268,7 +2268,7 @@ end:
 int cmd_list(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS;
-       const char *session_name, *leftover = NULL;
+       const char *arg_session_name, *leftover = NULL;
        static poptContext pc;
        struct lttng_domain domain;
        struct lttng_domain *domains = NULL;
@@ -2326,8 +2326,8 @@ int cmd_list(int argc, const char **argv)
        }
 
        /* Get session name (trailing argument) */
-       session_name = poptGetArg(pc);
-       DBG2("Session name: %s", session_name);
+       arg_session_name = poptGetArg(pc);
+       DBG2("Session name: %s", arg_session_name);
 
        leftover = poptGetArg(pc);
        if (leftover) {
@@ -2357,14 +2357,14 @@ int cmd_list(int argc, const char **argv)
        }
 
        if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) {
-               handle = lttng_create_handle(session_name, &domain);
+               handle = lttng_create_handle(arg_session_name, &domain);
                if (handle == NULL) {
                        ret = CMD_FATAL;
                        goto end;
                }
        }
 
-       if (session_name == NULL) {
+       if (arg_session_name == NULL) {
                if (!opt_kernel && !opt_userspace && !opt_jul && !opt_log4j
                                && !opt_python) {
                        ret = list_sessions(NULL);
@@ -2412,19 +2412,19 @@ int cmd_list(int argc, const char **argv)
                        }
                }
                /* MI: the ouptut of list_sessions is an unclosed session element */
-               ret = list_sessions(session_name);
+               ret = list_sessions(arg_session_name);
                if (ret) {
                        goto end;
                }
 
-               ret = list_rotate_settings(session_name);
+               ret = list_rotate_settings(arg_session_name);
                if (ret) {
                        goto end;
                }
 
                /* Domain listing */
                if (opt_domain) {
-                       ret = list_domains(session_name);
+                       ret = list_domains(arg_session_name);
                        goto end;
                }
 
@@ -2474,7 +2474,7 @@ int cmd_list(int argc, const char **argv)
                        int i, nb_domain;
 
                        /* We want all domain(s) */
-                       nb_domain = lttng_list_domains(session_name, &domains);
+                       nb_domain = lttng_list_domains(arg_session_name, &domains);
                        if (nb_domain < 0) {
                                ret = CMD_ERROR;
                                ERR("%s", lttng_strerror(nb_domain));
@@ -2527,7 +2527,7 @@ int cmd_list(int argc, const char **argv)
                                        lttng_destroy_handle(handle);
                                }
 
-                               handle = lttng_create_handle(session_name, &domains[i]);
+                               handle = lttng_create_handle(arg_session_name, &domains[i]);
                                if (handle == NULL) {
                                        ret = CMD_FATAL;
                                        goto end;
index 4873e5495f9bc15b6cef96ce60a22d939713ba65..0db09dd3172bb025a75b236c9d8f1a6c71a2eb07 100644 (file)
@@ -24,7 +24,6 @@
 #include <lttng/rotation.h>
 #include <lttng/location.h>
 
-static char *opt_session_name;
 static int opt_no_wait;
 static struct mi_writer *writer;
 
@@ -165,14 +164,15 @@ int cmd_rotate(int argc, const char **argv)
        enum cmd_error_code cmd_ret = CMD_SUCCESS;
        int popt_ret;
        static poptContext pc;
+       const char *arg_session_name = NULL;
        char *session_name = NULL;
-       bool free_session_name = false;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
        popt_ret = poptReadDefaultConfig(pc, 0);
        if (popt_ret) {
                ERR("poptReadDefaultConfig");
-               goto error;
+               cmd_ret = CMD_ERROR;
+               goto end;
        }
 
        while ((opt = poptGetNextOpt(pc)) != -1) {
@@ -189,37 +189,43 @@ int cmd_rotate(int argc, const char **argv)
                }
        }
 
-       opt_session_name = (char*) poptGetArg(pc);
-
-       if (!opt_session_name) {
+       arg_session_name = poptGetArg(pc);
+       if (arg_session_name == NULL) {
                session_name = get_session_name();
-               if (!session_name) {
-                       goto error;
-               }
-               free_session_name = true;
        } else {
-               session_name = opt_session_name;
+               session_name = strdup(arg_session_name);
+               if (session_name == NULL) {
+                       PERROR("Failed to copy session name");
+               }
+       }
+
+       if (session_name == NULL) {
+               cmd_ret = CMD_ERROR;
+               goto end;
        }
 
        /* Mi check */
        if (lttng_opt_mi) {
                writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
                if (!writer) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
 
                /* Open rotate command */
                ret = mi_lttng_writer_command_open(writer,
                                mi_lttng_element_command_rotate);
                if (ret) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
 
                /* Open output element */
                ret = mi_lttng_writer_open_element(writer,
                                mi_lttng_element_command_output);
                if (ret) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
        }
 
@@ -230,35 +236,34 @@ int cmd_rotate(int argc, const char **argv)
                /* Close output element */
                ret = mi_lttng_writer_close_element(writer);
                if (ret) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
                /* Success ? */
                ret = mi_lttng_writer_write_element_bool(writer,
                                mi_lttng_element_command_success,
                                cmd_ret == CMD_SUCCESS);
                if (ret) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
 
                /* Command element close */
                ret = mi_lttng_writer_command_close(writer);
                if (ret) {
-                       goto error;
+                       cmd_ret = CMD_ERROR;
+                       goto end;
                }
        }
 
        /* Mi clean-up */
        if (writer && mi_lttng_writer_destroy(writer)) {
-               goto error;
+               cmd_ret = CMD_ERROR;
+               goto end;
        }
 end:
+       free(session_name);
        poptFreeContext(pc);
-       if (free_session_name) {
-               free(session_name);
-       }
 
        return cmd_ret;
-error:
-       cmd_ret = CMD_ERROR;
-       goto end;
 }
index 59cd715682719d24976c81bba173e3c68eda4866..2801978759d23f146e0097b85796a7dc4d613c79 100644 (file)
@@ -117,7 +117,7 @@ int cmd_save(int argc, const char **argv)
 {
        int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
        int opt;
-       const char *session_name = NULL, *leftover = NULL;
+       const char *arg_session_name = NULL, *leftover = NULL;
        poptContext pc;
        struct lttng_save_session_attr *attr;
 
@@ -145,9 +145,9 @@ int cmd_save(int argc, const char **argv)
        }
 
        if (!opt_save_all) {
-               session_name = poptGetArg(pc);
-               if (session_name) {
-                       DBG2("Session name: %s", session_name);
+               arg_session_name = poptGetArg(pc);
+               if (arg_session_name) {
+                       DBG2("Session name: %s", arg_session_name);
                } else {
                        /* default to opt_save_all */
                        opt_save_all = true;
@@ -167,7 +167,7 @@ int cmd_save(int argc, const char **argv)
                goto end_destroy;
        }
 
-       if (lttng_save_session_attr_set_session_name(attr, session_name)) {
+       if (lttng_save_session_attr_set_session_name(attr, arg_session_name)) {
                ret = CMD_ERROR;
                goto end_destroy;
        }
@@ -213,12 +213,12 @@ int cmd_save(int argc, const char **argv)
                success = 0;
        } else {
                /* Inform the user of what just happened on success. */
-               if (session_name && opt_output_path) {
-                       MSG("Session %s saved successfully in %s.", session_name,
+               if (arg_session_name && opt_output_path) {
+                       MSG("Session %s saved successfully in %s.", arg_session_name,
                                        opt_output_path);
-               } else if (session_name && !opt_output_path) {
-                       MSG("Session %s saved successfully.", session_name);
-               } else if (!session_name && opt_output_path) {
+               } else if (arg_session_name && !opt_output_path) {
+                       MSG("Session %s saved successfully.", arg_session_name);
+               } else if (!arg_session_name && opt_output_path) {
                        MSG("All sessions have been saved successfully in %s.",
                                        opt_output_path);
                } else {
@@ -230,7 +230,7 @@ int cmd_save(int argc, const char **argv)
        /* Mi Printing and closing */
        if (lttng_opt_mi) {
                /* Mi print */
-               ret = mi_save_print(session_name);
+               ret = mi_save_print(arg_session_name);
                if (ret) {
                        ret = CMD_ERROR;
                        goto end_destroy;
index 497f4cbac8b9a9c01dc318314465dcc6c6eed76f..75762a2d8d6fb6727cabf9513ecce7527ad318fe 100644 (file)
@@ -19,8 +19,6 @@
 
 #include "../command.h"
 
-static char *opt_session_name;
-
 #ifdef LTTNG_EMBED_HELP
 static const char help_msg[] =
 #include <lttng-set-session.1.h>
@@ -44,7 +42,7 @@ static struct poptOption long_options[] = {
 /*
  * Print the necessary mi for a session and name.
  */
-static int mi_print(char *session_name)
+static int mi_print(const char *session_name)
 {
        int ret;
 
@@ -85,14 +83,14 @@ end:
 /*
  *  set_session
  */
-static int set_session(void)
+static int set_session(const char *session_name)
 {
        int ret = CMD_SUCCESS;
        int count, i;
        unsigned int session_found = 0;
        struct lttng_session *sessions;
 
-       if (opt_session_name && strlen(opt_session_name) > NAME_MAX) {
+       if (session_name && strlen(session_name) > NAME_MAX) {
                ERR("Session name too long. Length must be lower or equal to %d",
                        NAME_MAX);
                ret = CMD_ERROR;
@@ -107,28 +105,28 @@ static int set_session(void)
        }
 
        for (i = 0; i < count; i++) {
-               if (strncmp(sessions[i].name, opt_session_name, NAME_MAX) == 0) {
+               if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
                        session_found = 1;
                        break;
                }
        }
 
        if (!session_found) {
-               ERR("Session '%s' not found", opt_session_name);
+               ERR("Session '%s' not found", session_name);
                ret = CMD_ERROR;
                goto error;
        }
 
-       ret = config_init(opt_session_name);
+       ret = config_init(session_name);
        if (ret < 0) {
                ERR("Unable to set session name");
                ret = CMD_ERROR;
                goto error;
        }
 
-       MSG("Session set to %s", opt_session_name);
+       MSG("Session set to %s", session_name);
        if (lttng_opt_mi) {
-               ret = mi_print(opt_session_name);
+               ret = mi_print(session_name);
                if (ret) {
                        ret = CMD_ERROR;
                        goto error;
@@ -150,6 +148,7 @@ int cmd_set_session(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
+       const char *arg_session_name = NULL;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
@@ -168,8 +167,8 @@ int cmd_set_session(int argc, const char **argv)
                }
        }
 
-       opt_session_name = (char *) poptGetArg(pc);
-       if (opt_session_name == NULL) {
+       arg_session_name = poptGetArg(pc);
+       if (arg_session_name == NULL) {
                ERR("Missing session name");
                ret = CMD_ERROR;
                goto end;
@@ -200,7 +199,7 @@ int cmd_set_session(int argc, const char **argv)
                }
        }
 
-       command_ret = set_session();
+       command_ret = set_session(arg_session_name);
        if (command_ret) {
                success = 0;
        }
index 69bd9221fe475cb93668964efc29620688e315e2..6c9497d57bd12bab12a671ffc2229bc73f206cd3 100644 (file)
@@ -20,7 +20,6 @@
 #include "../command.h"
 
 
-static char *opt_session_name;
 static struct mi_writer *writer;
 
 #ifdef LTTNG_EMBED_HELP
@@ -76,19 +75,23 @@ end:
  *
  *  Start tracing for all trace of the session.
  */
-static int start_tracing(void)
+static int start_tracing(const char *arg_session_name)
 {
        int ret;
        char *session_name;
 
-       if (opt_session_name == NULL) {
+       if (arg_session_name == NULL) {
                session_name = get_session_name();
+       } else {
+               session_name = strdup(arg_session_name);
                if (session_name == NULL) {
-                       ret = CMD_ERROR;
-                       goto error;
+                       PERROR("Failed to copy session name");
                }
-       } else {
-               session_name = opt_session_name;
+       }
+
+       if (session_name == NULL) {
+               ret = CMD_ERROR;
+               goto error;
        }
 
        DBG("Starting tracing for session %s", session_name);
@@ -118,9 +121,7 @@ static int start_tracing(void)
        }
 
 free_name:
-       if (opt_session_name == NULL) {
-               free(session_name);
-       }
+       free(session_name);
 error:
        return ret;
 }
@@ -134,6 +135,7 @@ int cmd_start(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
+       const char *arg_session_name = NULL;
        const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
@@ -153,7 +155,7 @@ int cmd_start(int argc, const char **argv)
                }
        }
 
-       opt_session_name = (char*) poptGetArg(pc);
+       arg_session_name = poptGetArg(pc);
 
        leftover = poptGetArg(pc);
        if (leftover) {
@@ -198,7 +200,7 @@ int cmd_start(int argc, const char **argv)
                }
        }
 
-       command_ret = start_tracing();
+       command_ret = start_tracing(arg_session_name);
        if (command_ret) {
                success = 0;
        }
index 21a6912458ba59a3ab31474848c51a056d7010be..b1aa14b271f6ce2060954cacebbb8447d4c61a06 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "../command.h"
 
-static char *opt_session_name;
 static int opt_no_wait;
 static struct mi_writer *writer;
 
@@ -82,19 +81,23 @@ end:
 /*
  * Start tracing for all trace of the session.
  */
-static int stop_tracing(void)
+static int stop_tracing(const char *arg_session_name)
 {
        int ret;
        char *session_name;
 
-       if (opt_session_name == NULL) {
+       if (arg_session_name == NULL) {
                session_name = get_session_name();
+       } else {
+               session_name = strdup(arg_session_name);
                if (session_name == NULL) {
-                       ret = CMD_ERROR;
-                       goto error;
+                       PERROR("Failed to copy session name");
                }
-       } else {
-               session_name = opt_session_name;
+       }
+
+       if (session_name == NULL) {
+               ret = CMD_ERROR;
+               goto error;
        }
 
        ret = lttng_stop_tracing_no_wait(session_name);
@@ -145,9 +148,7 @@ static int stop_tracing(void)
        }
 
 free_name:
-       if (opt_session_name == NULL) {
-               free(session_name);
-       }
+       free(session_name);
 
 error:
        return ret;
@@ -162,6 +163,7 @@ int cmd_stop(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
+       const char *arg_session_name = NULL;
        const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
@@ -217,7 +219,7 @@ int cmd_stop(int argc, const char **argv)
                }
        }
 
-       opt_session_name = (char*) poptGetArg(pc);
+       arg_session_name = poptGetArg(pc);
 
        leftover = poptGetArg(pc);
        if (leftover) {
@@ -226,7 +228,7 @@ int cmd_stop(int argc, const char **argv)
                goto end;
        }
 
-       command_ret = stop_tracing();
+       command_ret = stop_tracing(arg_session_name);
        if (command_ret) {
                success = 0;
        }
index d7632b62bd722259c9b2c23f5c01bf2e0028dc9c..b25e8da28c5b5941448d2354dc01321386d32c9c 100644 (file)
@@ -17,7 +17,6 @@
 #include <common/spawn-viewer.h>
 #include "../command.h"
 
-static char *opt_session_name;
 static char *opt_viewer;
 static char *opt_trace_path;
 
@@ -73,7 +72,7 @@ error:
 /*
  * Exec viewer if found and use session name path.
  */
-static int view_trace(void)
+static int view_trace(const char *arg_session_name)
 {
        int ret;
        char *session_name, *trace_path = NULL;
@@ -99,14 +98,20 @@ static int view_trace(void)
        /* User define trace path override the session name */
        if (opt_trace_path) {
                session_name = NULL;
-       } else if(opt_session_name == NULL) {
-               session_name = get_session_name();
+       } else {
+               if (arg_session_name == NULL) {
+                       session_name = get_session_name();
+               } else {
+                       session_name = strdup(arg_session_name);
+                       if (session_name == NULL) {
+                               PERROR("Failed to copy session name");
+                       }
+               }
+
                if (session_name == NULL) {
                        ret = CMD_ERROR;
                        goto error;
                }
-       } else {
-               session_name = opt_session_name;
        }
 
        DBG("Viewing trace for session %s", session_name);
@@ -179,9 +184,7 @@ free_sessions:
        }
        free(sessions);
 free_error:
-       if (opt_session_name == NULL) {
-               free(session_name);
-       }
+       free(session_name);
 error:
        return ret;
 }
@@ -193,6 +196,7 @@ int cmd_view(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS;
        static poptContext pc;
+       const char *arg_session_name = NULL;
        const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
@@ -216,7 +220,7 @@ int cmd_view(int argc, const char **argv)
                }
        }
 
-       opt_session_name = (char*) poptGetArg(pc);
+       arg_session_name = poptGetArg(pc);
 
        leftover = poptGetArg(pc);
        if (leftover) {
@@ -225,7 +229,7 @@ int cmd_view(int argc, const char **argv)
                goto end;
        }
 
-       ret = view_trace();
+       ret = view_trace(arg_session_name);
 
 end:
        poptFreeContext(pc);
This page took 0.048561 seconds and 4 git commands to generate.