Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.c
index 678af746d5091181aed4febe926130525afa7c5b..2305d79076e602d2ddd716e7ad98bdea55de32eb 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_channels;
 static int opt_kernel;
 static char *opt_session_name;
 static int opt_userspace;
 
+#ifdef LTTNG_EMBED_HELP
+static const char help_msg[] =
+#include <lttng-disable-channel.1.h>
+;
+#endif
+
 enum {
        OPT_HELP = 1,
        OPT_USERSPACE,
@@ -98,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;
 
@@ -137,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);
 
@@ -210,6 +205,9 @@ 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);
        poptReadDefaultConfig(pc, 0);
@@ -231,15 +229,30 @@ int cmd_disable_channels(int argc, const char **argv)
                }
        }
 
-       ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
+       ret = print_missing_or_multiple_domains(
+                       opt_kernel + opt_userspace, false);
        if (ret) {
                ret = CMD_ERROR;
                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;
+       }
+
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
                ret = CMD_ERROR;
                goto end;
        }
@@ -279,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;
        }
@@ -320,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;
 
This page took 0.024727 seconds and 4 git commands to generate.