Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / set_session.c
index c0572e1275d46f273cb446bedd8010ab4a4463a2..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
 
 #include "../command.h"
 
-static char *opt_session_name;
+#ifdef LTTNG_EMBED_HELP
+static const char help_msg[] =
+#include <lttng-set-session.1.h>
+;
+#endif
 
 enum {
        OPT_HELP = 1,
@@ -45,23 +39,10 @@ static struct poptOption long_options[] = {
        {0, 0, 0, 0, 0, 0, 0}
 };
 
-/*
- * usage
- */
-static void usage(FILE *ofp)
-{
-       fprintf(ofp, "usage: lttng set-session NAME [OPTIONS]\n");
-       fprintf(ofp, "\n");
-       fprintf(ofp, "Options:\n");
-       fprintf(ofp, "  -h, --help               Show this help\n");
-       fprintf(ofp, "      --list-options       Simple listing of options\n");
-       fprintf(ofp, "\n");
-}
-
 /*
  * 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;
 
@@ -102,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;
@@ -124,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;
@@ -167,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);
@@ -174,22 +156,20 @@ int cmd_set_session(int argc, const char **argv)
        while ((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case OPT_HELP:
-                       usage(stdout);
+                       SHOW_HELP();
                        goto end;
                case OPT_LIST_OPTIONS:
                        list_cmd_options(stdout, long_options);
                        goto end;
                default:
-                       usage(stderr);
                        ret = CMD_UNDEFINED;
                        goto end;
                }
        }
 
-       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");
-               usage(stderr);
                ret = CMD_ERROR;
                goto end;
        }
@@ -219,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;
        }
@@ -256,7 +236,7 @@ end:
                ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
        }
 
-       /* Overwrite ret if an error occured during set_session() */
+       /* Overwrite ret if an error occurred during set_session() */
        ret = command_ret ? command_ret : ret;
 
        poptFreeContext(pc);
This page took 0.032944 seconds and 4 git commands to generate.