2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "../command.hpp"
11 #include <common/mi-lttng.hpp>
13 #include <lttng/domain-internal.hpp>
20 #include <sys/types.h>
23 static int opt_kernel
;
24 static char *opt_session_name
;
25 static int opt_userspace
;
27 #ifdef LTTNG_EMBED_HELP
28 static const char help_msg
[] =
29 #include <lttng-disable-channel.1.h>
39 static struct lttng_handle
*handle
;
40 static struct mi_writer
*writer
;
42 static struct poptOption long_options
[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 { "help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0 },
45 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0 },
46 { "kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0 },
47 { "userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0 },
48 { "list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
49 { 0, 0, 0, 0, 0, 0, 0 }
52 static int mi_partial_channel_print(char *channel_name
, unsigned int enabled
, int success
)
57 LTTNG_ASSERT(channel_name
);
59 /* Open channel element */
60 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
66 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
, channel_name
);
72 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
, enabled
);
78 ret
= mi_lttng_writer_write_element_bool(writer
, mi_lttng_element_success
, success
);
83 /* Closing channel element */
84 ret
= mi_lttng_writer_close_element(writer
);
91 * Disabling channel using the lttng API.
93 static int disable_channels(char *session_name
, char *channel_list
)
95 int ret
= CMD_SUCCESS
, warn
= 0, success
;
97 /* Normal case for disable channed is enabled = 0 */
98 unsigned int enabled
= 0;
100 struct lttng_domain dom
;
102 memset(&dom
, 0, sizeof(dom
));
104 /* Create lttng domain */
106 dom
.type
= LTTNG_DOMAIN_KERNEL
;
107 } else if (opt_userspace
) {
108 dom
.type
= LTTNG_DOMAIN_UST
;
110 /* Checked by the caller. */
114 handle
= lttng_create_handle(session_name
, &dom
);
115 if (handle
== NULL
) {
122 /* open a channels element */
123 ret
= mi_lttng_writer_open_element(writer
, config_element_channels
);
130 /* Strip channel list */
131 channel_name
= strtok(channel_list
, ",");
132 while (channel_name
!= NULL
) {
133 DBG("Disabling channel %s", channel_name
);
135 ret
= lttng_disable_channel(handle
, channel_name
);
137 ERR("Channel %s: %s (session %s)",
145 * We assume that if an error occurred the channel is still active.
146 * This might not be the case but is a good assumption.
147 * The client should look at the stderr stream
148 * for more informations.
154 MSG("%s channel %s disabled for session %s",
155 lttng_domain_type_str(dom
.type
),
162 /* Print the channel */
164 ret
= mi_partial_channel_print(channel_name
, enabled
, success
);
172 channel_name
= strtok(NULL
, ",");
179 /* Close channels element */
180 ret
= mi_lttng_writer_close_element(writer
);
188 /* Bypass the warning if a more important error happened */
193 lttng_destroy_handle(handle
);
199 * cmd_disable_channels
201 * Disable channel to trace session
203 int cmd_disable_channels(int argc
, const char **argv
)
205 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
206 static poptContext pc
;
207 char *session_name
= NULL
;
208 char *channel_list
= NULL
;
209 const char *arg_channel_list
= NULL
;
210 const char *leftover
= NULL
;
212 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
213 poptReadDefaultConfig(pc
, 0);
215 while ((opt
= poptGetNextOpt(pc
)) != -1) {
223 case OPT_LIST_OPTIONS
:
224 list_cmd_options(stdout
, long_options
);
232 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
, false);
238 arg_channel_list
= poptGetArg(pc
);
239 if (arg_channel_list
== NULL
) {
240 ERR("Missing channel name(s).");
245 channel_list
= strdup(arg_channel_list
);
246 if (channel_list
== NULL
) {
247 PERROR("Failed to copy channel name");
252 leftover
= poptGetArg(pc
);
254 ERR("Unknown argument: %s", leftover
);
259 if (!opt_session_name
) {
260 session_name
= get_session_name();
261 if (session_name
== NULL
) {
266 session_name
= opt_session_name
;
271 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
273 ret
= -LTTNG_ERR_NOMEM
;
277 /* Open command element */
278 ret
= mi_lttng_writer_command_open(writer
,
279 mi_lttng_element_command_disable_channel
);
285 /* Open output element */
286 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
293 command_ret
= disable_channels(session_name
, channel_list
);
300 /* Close output element */
301 ret
= mi_lttng_writer_close_element(writer
);
308 ret
= mi_lttng_writer_write_element_bool(writer
, mi_lttng_element_success
, success
);
314 /* Command element close */
315 ret
= mi_lttng_writer_command_close(writer
);
324 if (writer
&& mi_lttng_writer_destroy(writer
)) {
325 /* Preserve original error code */
326 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
329 if (!opt_session_name
&& session_name
) {
335 /* Overwrite ret if an error occurred in disable_channels */
336 ret
= command_ret
? command_ret
: ret
;