From b7f4ea0f4c6ac71da5e4e6fe988df4d35734332a Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Mon, 31 Oct 2022 15:52:46 -0400 Subject: [PATCH] Fix: lttng: poptGetArg doesn't provide string ownership MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Jérémie Galarneau --- src/bin/lttng/commands/clear.c | 25 ++++++----- src/bin/lttng/commands/create.c | 40 +++++++++-------- src/bin/lttng/commands/destroy.c | 25 ++++++----- src/bin/lttng/commands/disable_channels.c | 24 +++++++--- src/bin/lttng/commands/disable_events.c | 24 +++++++--- src/bin/lttng/commands/enable_channels.c | 25 ++++++++--- src/bin/lttng/commands/enable_events.c | 26 ++++++++--- src/bin/lttng/commands/help.c | 13 +++--- src/bin/lttng/commands/list.c | 20 ++++----- src/bin/lttng/commands/rotate.c | 53 +++++++++++++---------- src/bin/lttng/commands/save.c | 22 +++++----- src/bin/lttng/commands/set_session.c | 25 +++++------ src/bin/lttng/commands/start.c | 26 ++++++----- src/bin/lttng/commands/stop.c | 26 ++++++----- src/bin/lttng/commands/view.c | 26 ++++++----- 15 files changed, 230 insertions(+), 170 deletions(-) diff --git a/src/bin/lttng/commands/clear.c b/src/bin/lttng/commands/clear.c index 61879d61d..82d938ff7 100644 --- a/src/bin/lttng/commands/clear.c +++ b/src/bin/lttng/commands/clear.c @@ -156,8 +156,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; @@ -213,19 +213,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); @@ -307,9 +310,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; diff --git a/src/bin/lttng/commands/create.c b/src/bin/lttng/commands/create.c index 4ac8b87ca..2223670ab 100644 --- a/src/bin/lttng/commands/create.c +++ b/src/bin/lttng/commands/create.c @@ -33,7 +33,6 @@ #include static char *opt_output_path; -static char *opt_session_name; static char *opt_url; static char *opt_ctrl_url; static char *opt_data_url; @@ -131,7 +130,7 @@ end: } static -struct lttng_session_descriptor *create_session_descriptor(void) +struct lttng_session_descriptor *create_session_descriptor(const char *session_name) { ssize_t uri_count; enum output_type output_type; @@ -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; } diff --git a/src/bin/lttng/commands/destroy.c b/src/bin/lttng/commands/destroy.c index e825d9c3e..d3e2ac59f 100644 --- a/src/bin/lttng/commands/destroy.c +++ b/src/bin/lttng/commands/destroy.c @@ -22,7 +22,6 @@ #include #include -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 */ diff --git a/src/bin/lttng/commands/disable_channels.c b/src/bin/lttng/commands/disable_channels.c index 400a4b569..aba7aac82 100644 --- a/src/bin/lttng/commands/disable_channels.c +++ b/src/bin/lttng/commands/disable_channels.c @@ -20,7 +20,6 @@ #include "../command.h" -static char *opt_channels; static int opt_kernel; static char *opt_session_name; static int opt_userspace; @@ -95,7 +94,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; @@ -134,7 +133,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); @@ -208,6 +207,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); @@ -237,9 +238,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; } @@ -286,7 +294,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; } @@ -327,6 +335,8 @@ end: free(session_name); } + free(channel_list); + /* Overwrite ret if an error occurred in disable_channels */ ret = command_ret ? command_ret : ret; diff --git a/src/bin/lttng/commands/disable_events.c b/src/bin/lttng/commands/disable_events.c index 386968604..b8b0ab95c 100644 --- a/src/bin/lttng/commands/disable_events.c +++ b/src/bin/lttng/commands/disable_events.c @@ -20,7 +20,6 @@ #include "../command.h" -static char *opt_event_list; static int opt_kernel; static char *opt_channel_name; static char *opt_session_name; @@ -152,7 +151,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; @@ -241,7 +240,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); @@ -319,6 +318,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; @@ -385,13 +386,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); @@ -434,7 +444,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; } @@ -468,6 +478,8 @@ end: free(session_name); } + free(event_list); + /* Mi clean-up */ if (writer && mi_lttng_writer_destroy(writer)) { /* Preserve original error code */ diff --git a/src/bin/lttng/commands/enable_channels.c b/src/bin/lttng/commands/enable_channels.c index 9bfce348b..dc27b8ba0 100644 --- a/src/bin/lttng/commands/enable_channels.c +++ b/src/bin/lttng/commands/enable_channels.c @@ -28,7 +28,6 @@ static struct lttng_channel chan_opts; -static char *opt_channels; static int opt_kernel; static char *opt_session_name; static int opt_userspace; @@ -140,7 +139,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; @@ -231,7 +230,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; @@ -392,7 +391,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(); @@ -692,9 +693,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; @@ -719,7 +728,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; } @@ -758,6 +767,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); diff --git a/src/bin/lttng/commands/enable_events.c b/src/bin/lttng/commands/enable_events.c index 30d28cb26..ebc46d87f 100644 --- a/src/bin/lttng/commands/enable_events.c +++ b/src/bin/lttng/commands/enable_events.c @@ -36,7 +36,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; @@ -431,7 +430,7 @@ static void warn_on_truncated_exclusion_names(const struct lttng_dynamic_pointer * 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; @@ -813,7 +812,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); @@ -1223,6 +1222,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; @@ -1321,13 +1322,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); @@ -1346,7 +1356,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; @@ -1388,6 +1398,8 @@ end: free(session_name); } + free(event_list); + /* Overwrite ret if an error occurred in enable_events */ ret = command_ret ? command_ret : ret; diff --git a/src/bin/lttng/commands/help.c b/src/bin/lttng/commands/help.c index 7ef21c2f5..bf5b603bd 100644 --- a/src/bin/lttng/commands/help.c +++ b/src/bin/lttng/commands/help.c @@ -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; } diff --git a/src/bin/lttng/commands/list.c b/src/bin/lttng/commands/list.c index 2b01e611e..79e4cf0e9 100644 --- a/src/bin/lttng/commands/list.c +++ b/src/bin/lttng/commands/list.c @@ -2282,7 +2282,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; @@ -2341,8 +2341,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) { @@ -2372,14 +2372,14 @@ int cmd_list(int argc, const char **argv) } if (opt_kernel || opt_userspace || opt_jul || opt_log4j || opt_python) { - the_handle = lttng_create_handle(session_name, &domain); + the_handle = lttng_create_handle(arg_session_name, &domain); if (the_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); @@ -2427,19 +2427,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; } @@ -2490,7 +2490,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)); @@ -2545,7 +2545,7 @@ int cmd_list(int argc, const char **argv) } the_handle = lttng_create_handle( - session_name, &domains[i]); + arg_session_name, &domains[i]); if (the_handle == NULL) { ret = CMD_FATAL; goto end; diff --git a/src/bin/lttng/commands/rotate.c b/src/bin/lttng/commands/rotate.c index ec8163157..7048be32f 100644 --- a/src/bin/lttng/commands/rotate.c +++ b/src/bin/lttng/commands/rotate.c @@ -23,7 +23,6 @@ #include "../command.h" #include -static char *opt_session_name; static int opt_no_wait; static struct mi_writer *writer; @@ -164,14 +163,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) { @@ -188,37 +188,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; } } @@ -229,35 +235,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; } diff --git a/src/bin/lttng/commands/save.c b/src/bin/lttng/commands/save.c index 7d09a42a2..a70c7ef6c 100644 --- a/src/bin/lttng/commands/save.c +++ b/src/bin/lttng/commands/save.c @@ -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; diff --git a/src/bin/lttng/commands/set_session.c b/src/bin/lttng/commands/set_session.c index 497f4cbac..75762a2d8 100644 --- a/src/bin/lttng/commands/set_session.c +++ b/src/bin/lttng/commands/set_session.c @@ -19,8 +19,6 @@ #include "../command.h" -static char *opt_session_name; - #ifdef LTTNG_EMBED_HELP static const char help_msg[] = #include @@ -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; } diff --git a/src/bin/lttng/commands/start.c b/src/bin/lttng/commands/start.c index 69bd9221f..6c9497d57 100644 --- a/src/bin/lttng/commands/start.c +++ b/src/bin/lttng/commands/start.c @@ -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; } diff --git a/src/bin/lttng/commands/stop.c b/src/bin/lttng/commands/stop.c index 21a691245..b1aa14b27 100644 --- a/src/bin/lttng/commands/stop.c +++ b/src/bin/lttng/commands/stop.c @@ -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; } diff --git a/src/bin/lttng/commands/view.c b/src/bin/lttng/commands/view.c index a2623fe49..9283c242f 100644 --- a/src/bin/lttng/commands/view.c +++ b/src/bin/lttng/commands/view.c @@ -17,7 +17,6 @@ #include #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); -- 2.34.1