Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / enable_channels.c
index cb9f44a635531bbc5a0db04b77baee748faf6fac..343df60e4a86e632c4ab60244c69e3e7c7806055 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
@@ -36,7 +26,6 @@
 
 
 static struct lttng_channel chan_opts;
-static char *opt_channels;
 static int opt_kernel;
 static char *opt_session_name;
 static int opt_userspace;
@@ -148,7 +137,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;
@@ -239,12 +228,12 @@ 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;
 
                /* Validate channel name's length */
-               if (strlen(channel_name) >= NAME_MAX) {
+               if (strlen(channel_name) >= sizeof(chan_opts.name)) {
                        ERR("Channel name is too long (max. %zu characters)",
                                        sizeof(chan_opts.name) - 1);
                        error = 1;
@@ -399,7 +388,10 @@ 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();
 
@@ -492,66 +484,74 @@ int cmd_enable_channels(int argc, const char **argv)
                }
                case OPT_SWITCH_TIMER:
                {
-                       unsigned long v;
+                       uint64_t v;
 
                        errno = 0;
                        opt_arg = poptGetOptArg(pc);
-                       v = strtoul(opt_arg, NULL, 0);
-                       if (errno != 0 || !isdigit(opt_arg[0])) {
-                               ERR("Wrong value in --switch-timer parameter: %s", opt_arg);
+
+                       if (utils_parse_time_suffix(opt_arg, &v) < 0) {
+                               ERR("Wrong value for --switch-timer parameter: %s", opt_arg);
                                ret = CMD_ERROR;
                                goto end;
                        }
+
                        if (v != (uint32_t) v) {
                                ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg);
                                ret = CMD_ERROR;
                                goto end;
                        }
                        chan_opts.attr.switch_timer_interval = (uint32_t) v;
-                       DBG("Channel switch timer interval set to %d", chan_opts.attr.switch_timer_interval);
+                       DBG("Channel switch timer interval set to %d %s",
+                                       chan_opts.attr.switch_timer_interval,
+                                       USEC_UNIT);
                        break;
                }
                case OPT_READ_TIMER:
                {
-                       unsigned long v;
+                       uint64_t v;
 
                        errno = 0;
                        opt_arg = poptGetOptArg(pc);
-                       v = strtoul(opt_arg, NULL, 0);
-                       if (errno != 0 || !isdigit(opt_arg[0])) {
-                               ERR("Wrong value in --read-timer parameter: %s", opt_arg);
+
+                       if (utils_parse_time_suffix(opt_arg, &v) < 0) {
+                               ERR("Wrong value for --read-timer parameter: %s", opt_arg);
                                ret = CMD_ERROR;
                                goto end;
                        }
+
                        if (v != (uint32_t) v) {
                                ERR("32-bit overflow in --read-timer parameter: %s", opt_arg);
                                ret = CMD_ERROR;
                                goto end;
                        }
                        chan_opts.attr.read_timer_interval = (uint32_t) v;
-                       DBG("Channel read timer interval set to %d", chan_opts.attr.read_timer_interval);
+                       DBG("Channel read timer interval set to %d %s",
+                                       chan_opts.attr.read_timer_interval,
+                                       USEC_UNIT);
                        break;
                }
                case OPT_MONITOR_TIMER:
                {
-                       unsigned long long v;
+                       uint64_t v;
 
                        errno = 0;
                        opt_arg = poptGetOptArg(pc);
-                       v = strtoull(opt_arg, NULL, 0);
-                       if (errno != 0 || !isdigit(opt_arg[0])) {
-                               ERR("Wrong value in --monitor-timer parameter: %s", opt_arg);
+
+                       if (utils_parse_time_suffix(opt_arg, &v) < 0) {
+                               ERR("Wrong value for --monitor-timer parameter: %s", opt_arg);
                                ret = CMD_ERROR;
                                goto end;
                        }
                        opt_monitor_timer.interval = (uint64_t) v;
                        opt_monitor_timer.set = true;
-                       DBG("Channel monitor timer interval set to %" PRIu64" (µs)", opt_monitor_timer.interval);
+                       DBG("Channel monitor timer interval set to %" PRIu64 " %s",
+                                       opt_monitor_timer.interval,
+                                       USEC_UNIT);
                        break;
                }
                case OPT_BLOCKING_TIMEOUT:
                {
-                       long long v;    /* in usec */
+                       uint64_t v;
                        long long v_msec;
 
                        errno = 0;
@@ -564,10 +564,8 @@ int cmd_enable_channels(int argc, const char **argv)
                                break;
                        }
 
-                       v = strtoll(opt_arg, NULL, 0);
-                       if (errno != 0 || (!isdigit(opt_arg[0]) && opt_arg[0] != '-')
-                                       || v < 0) {
-                               ERR("Wrong value in --blocking-timeout parameter: %s", opt_arg);
+                       if (utils_parse_time_suffix(opt_arg, &v) < 0) {
+                               ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg);
                                ret = CMD_ERROR;
                                goto end;
                        }
@@ -596,8 +594,9 @@ int cmd_enable_channels(int argc, const char **argv)
 
                        opt_blocking_timeout.value = (int64_t) v;
                        opt_blocking_timeout.set = true;
-                       DBG("Channel blocking timeout set to %" PRId64 " µs%s",
+                       DBG("Channel blocking timeout set to %" PRId64 " %s%s",
                                        opt_blocking_timeout.value,
+                                       USEC_UNIT,
                                        opt_blocking_timeout.value == 0 ?
                                                " (non-blocking)" : "");
                        break;
@@ -644,9 +643,15 @@ int cmd_enable_channels(int argc, const char **argv)
                        ret = CMD_UNDEFINED;
                        goto end;
                }
+
+               if (opt_arg) {
+                       free(opt_arg);
+                       opt_arg = NULL;
+               }
        }
 
-       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;
@@ -685,9 +690,25 @@ 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;
+       }
+
+       leftover = poptGetArg(pc);
+       if (leftover) {
+               ERR("Unknown argument: %s", leftover);
                ret = CMD_ERROR;
                success = 0;
                goto mi_closing;
@@ -704,7 +725,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;
        }
@@ -743,8 +764,11 @@ end:
                free(session_name);
        }
 
+       free(channel_list);
+
        /* Overwrite ret if an error occurred when enable_channel */
        ret = command_ret ? command_ret : ret;
        poptFreeContext(pc);
+       free(opt_arg);
        return ret;
 }
This page took 0.02851 seconds and 4 git commands to generate.