Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / set_session.c
index d143c7dd45ff2623899047b8b5e89ef0ed45dbcc..75762a2d8d6fb6727cabf9513ecce7527ad318fe 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
+ * Copyright (C) 2011 EfficiOS Inc.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #define _LGPL_SOURCE
@@ -29,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>
@@ -54,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;
 
@@ -95,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;
@@ -117,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;
@@ -160,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);
@@ -178,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;
@@ -210,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;
        }
This page took 0.02464 seconds and 4 git commands to generate.