Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / disable_events.c
index 238f8463fec1553c4f8410091a3ebbb057752423..b8b0ab95c17127c56bbd422b4ff1ab81ee32b897 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 <assert.h>
 
 #include <common/mi-lttng.h>
+#include <lttng/domain-internal.h>
 
 #include "../command.h"
 
-static char *opt_event_list;
 static int opt_kernel;
 static char *opt_channel_name;
 static char *opt_session_name;
@@ -40,6 +30,12 @@ static int opt_log4j;
 static int opt_python;
 static int opt_event_type;
 
+#ifdef LTTNG_EMBED_HELP
+static const char help_msg[] =
+#include <lttng-disable-event.1.h>
+;
+#endif
+
 enum {
        OPT_HELP = 1,
        OPT_TYPE_SYSCALL,
@@ -110,7 +106,7 @@ const char *print_event_type(const enum lttng_event_type ev_type)
  * enabled is 0 or 1
  * success is 0 or 1
  */
-static int mi_print_event(char *event_name, int enabled, int success)
+static int mi_print_event(const char *event_name, int enabled, int success)
 {
        int ret;
 
@@ -155,7 +151,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;
@@ -230,7 +226,7 @@ static int disable_events(char *session_name)
                        enabled = 0;
                        success = 1;
                        MSG("All %s events of type %s are disabled in channel %s",
-                                       get_domain_str(dom.type),
+                                       lttng_domain_type_str(dom.type),
                                        print_event_type(opt_event_type),
                                        print_channel_name(channel_name));
                }
@@ -244,7 +240,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);
 
@@ -269,7 +265,7 @@ static int disable_events(char *session_name)
                                enabled = 1;
                        } else {
                                MSG("%s %s of type %s disabled in channel %s for session %s",
-                                               get_domain_str(dom.type),
+                                               lttng_domain_type_str(dom.type),
                                                event_name,
                                                print_event_type(opt_event_type),
                                                print_channel_name(channel_name),
@@ -322,6 +318,9 @@ 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;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
@@ -371,7 +370,9 @@ int cmd_disable_events(int argc, const char **argv)
        }
 
        ret = print_missing_or_multiple_domains(
-               opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python);
+                       opt_kernel + opt_userspace + opt_jul + opt_log4j +
+                                       opt_python,
+                       true);
        if (ret) {
                ret = CMD_ERROR;
                goto end;
@@ -385,13 +386,29 @@ 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);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        if (!opt_session_name) {
                session_name = get_session_name();
                if (session_name == NULL) {
@@ -427,7 +444,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;
        }
@@ -461,6 +478,8 @@ end:
                free(session_name);
        }
 
+       free(event_list);
+
        /* Mi clean-up */
        if (writer && mi_lttng_writer_destroy(writer)) {
                /* Preserve original error code */
This page took 0.025701 seconds and 4 git commands to generate.