From 5b915816f16e7cb3134dd7279abad79b31c2486c 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.cpp | 25 +++++----- src/bin/lttng/commands/create.cpp | 40 ++++++++-------- src/bin/lttng/commands/destroy.cpp | 25 +++++----- src/bin/lttng/commands/disable_channels.cpp | 24 +++++++--- src/bin/lttng/commands/disable_events.cpp | 24 +++++++--- src/bin/lttng/commands/enable_channels.cpp | 25 +++++++--- src/bin/lttng/commands/enable_events.cpp | 26 +++++++--- src/bin/lttng/commands/help.cpp | 13 +++-- src/bin/lttng/commands/list.cpp | 20 ++++---- src/bin/lttng/commands/rotate.cpp | 53 +++++++++++---------- src/bin/lttng/commands/save.cpp | 22 ++++----- src/bin/lttng/commands/set_session.cpp | 25 +++++----- src/bin/lttng/commands/start.cpp | 26 +++++----- src/bin/lttng/commands/stop.cpp | 26 +++++----- src/bin/lttng/commands/view.cpp | 26 +++++----- 15 files changed, 230 insertions(+), 170 deletions(-) diff --git a/src/bin/lttng/commands/clear.cpp b/src/bin/lttng/commands/clear.cpp index 7998920da..887c61f0e 100644 --- a/src/bin/lttng/commands/clear.cpp +++ b/src/bin/lttng/commands/clear.cpp @@ -154,8 +154,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; @@ -211,19 +211,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); @@ -305,9 +308,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.cpp b/src/bin/lttng/commands/create.cpp index 64cf2b37c..fde027fe5 100644 --- a/src/bin/lttng/commands/create.cpp +++ b/src/bin/lttng/commands/create.cpp @@ -32,7 +32,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; @@ -130,7 +129,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; @@ -202,17 +201,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(); @@ -225,7 +224,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. */ @@ -233,17 +232,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(); @@ -280,7 +279,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] = {}; @@ -292,8 +291,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; @@ -304,11 +303,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; @@ -328,7 +327,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; @@ -374,7 +373,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; @@ -654,6 +653,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; @@ -760,7 +760,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) { @@ -769,7 +771,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.cpp b/src/bin/lttng/commands/destroy.cpp index 3ab9b2248..9b5978bdd 100644 --- a/src/bin/lttng/commands/destroy.cpp +++ b/src/bin/lttng/commands/destroy.cpp @@ -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.cpp b/src/bin/lttng/commands/disable_channels.cpp index df9eb13fa..ff9bd91c8 100644 --- a/src/bin/lttng/commands/disable_channels.cpp +++ b/src/bin/lttng/commands/disable_channels.cpp @@ -19,7 +19,6 @@ #include "../command.hpp" -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); @@ -207,6 +206,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); @@ -236,9 +237,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; } @@ -285,7 +293,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; } @@ -326,6 +334,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.cpp b/src/bin/lttng/commands/disable_events.cpp index d2948ac04..bf8ed6e30 100644 --- a/src/bin/lttng/commands/disable_events.cpp +++ b/src/bin/lttng/commands/disable_events.cpp @@ -19,7 +19,6 @@ #include "../command.hpp" -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 */ diff --git a/src/bin/lttng/commands/enable_channels.cpp b/src/bin/lttng/commands/enable_channels.cpp index a7fffbff1..643171909 100644 --- a/src/bin/lttng/commands/enable_channels.cpp +++ b/src/bin/lttng/commands/enable_channels.cpp @@ -27,7 +27,6 @@ static struct lttng_channel chan_opts; -static char *opt_channels; static int opt_kernel; static char *opt_session_name; static int opt_userspace; @@ -139,7 +138,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; @@ -230,7 +229,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; @@ -391,7 +390,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(); @@ -691,9 +692,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; @@ -718,7 +727,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; } @@ -757,6 +766,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.cpp b/src/bin/lttng/commands/enable_events.cpp index 1d81143ae..2c20eea19 100644 --- a/src/bin/lttng/commands/enable_events.cpp +++ b/src/bin/lttng/commands/enable_events.cpp @@ -35,7 +35,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; @@ -430,7 +429,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; @@ -812,7 +811,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); @@ -1222,6 +1221,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; @@ -1320,13 +1321,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); @@ -1345,7 +1355,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; @@ -1387,6 +1397,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.cpp b/src/bin/lttng/commands/help.cpp index 74b649644..9822b47eb 100644 --- a/src/bin/lttng/commands/help.cpp +++ b/src/bin/lttng/commands/help.cpp @@ -46,7 +46,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; @@ -70,9 +70,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) { @@ -85,7 +84,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; } @@ -94,7 +93,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; } @@ -103,7 +102,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.cpp b/src/bin/lttng/commands/list.cpp index 8636cd101..5f52a57f6 100644 --- a/src/bin/lttng/commands/list.cpp +++ b/src/bin/lttng/commands/list.cpp @@ -2271,7 +2271,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; @@ -2330,8 +2330,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) { @@ -2361,14 +2361,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); @@ -2416,19 +2416,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; } @@ -2479,7 +2479,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)); @@ -2534,7 +2534,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.cpp b/src/bin/lttng/commands/rotate.cpp index 339e39598..27263da16 100644 --- a/src/bin/lttng/commands/rotate.cpp +++ b/src/bin/lttng/commands/rotate.cpp @@ -22,7 +22,6 @@ #include "../command.hpp" #include -static char *opt_session_name; static int opt_no_wait; static struct mi_writer *writer; @@ -163,14 +162,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) { @@ -187,37 +187,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; } } @@ -228,35 +234,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.cpp b/src/bin/lttng/commands/save.cpp index 9b939102c..91e80394e 100644 --- a/src/bin/lttng/commands/save.cpp +++ b/src/bin/lttng/commands/save.cpp @@ -116,7 +116,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; @@ -144,9 +144,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; @@ -166,7 +166,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; } @@ -212,12 +212,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 { @@ -229,7 +229,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.cpp b/src/bin/lttng/commands/set_session.cpp index 770921292..dfd247a22 100644 --- a/src/bin/lttng/commands/set_session.cpp +++ b/src/bin/lttng/commands/set_session.cpp @@ -18,8 +18,6 @@ #include "../command.hpp" -static char *opt_session_name; - #ifdef LTTNG_EMBED_HELP static const char help_msg[] = #include @@ -43,7 +41,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; @@ -84,14 +82,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; @@ -106,28 +104,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; @@ -149,6 +147,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); @@ -167,8 +166,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; @@ -199,7 +198,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.cpp b/src/bin/lttng/commands/start.cpp index 4d2b40a50..9a54d2227 100644 --- a/src/bin/lttng/commands/start.cpp +++ b/src/bin/lttng/commands/start.cpp @@ -20,7 +20,6 @@ #include "../command.hpp" -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.cpp b/src/bin/lttng/commands/stop.cpp index 6107dc158..ed235bc29 100644 --- a/src/bin/lttng/commands/stop.cpp +++ b/src/bin/lttng/commands/stop.cpp @@ -19,7 +19,6 @@ #include "../command.hpp" -static char *opt_session_name; static int opt_no_wait; static struct mi_writer *writer; @@ -81,19 +80,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); @@ -144,9 +147,7 @@ static int stop_tracing(void) } free_name: - if (opt_session_name == NULL) { - free(session_name); - } + free(session_name); error: return ret; @@ -161,6 +162,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); @@ -216,7 +218,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) { @@ -225,7 +227,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.cpp b/src/bin/lttng/commands/view.cpp index 1bf264871..cff6941f5 100644 --- a/src/bin/lttng/commands/view.cpp +++ b/src/bin/lttng/commands/view.cpp @@ -17,7 +17,6 @@ #include #include "../command.hpp" -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