2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
18 #include <common/mi-lttng.h>
20 #include "../command.h"
22 static int opt_kernel
;
23 static char *opt_session_name
;
24 static int opt_userspace
;
26 #ifdef LTTNG_EMBED_HELP
27 static const char help_msg
[] =
28 #include <lttng-disable-channel.1.h>
38 static struct lttng_handle
*handle
;
39 static struct mi_writer
*writer
;
41 static struct poptOption long_options
[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
43 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
44 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
45 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
46 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
47 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
51 static int mi_partial_channel_print(char *channel_name
, unsigned int enabled
,
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
,
73 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
80 ret
= mi_lttng_writer_write_element_bool(writer
,
81 mi_lttng_element_success
, success
);
86 /* Closing channel element */
87 ret
= mi_lttng_writer_close_element(writer
);
94 * Disabling channel using the lttng API.
96 static int disable_channels(char *session_name
, char *channel_list
)
98 int ret
= CMD_SUCCESS
, warn
= 0, success
;
100 /* Normal case for disable channed is enabled = 0 */
101 unsigned int enabled
= 0;
103 struct lttng_domain dom
;
105 memset(&dom
, 0, sizeof(dom
));
107 /* Create lttng domain */
109 dom
.type
= LTTNG_DOMAIN_KERNEL
;
110 } else if (opt_userspace
) {
111 dom
.type
= LTTNG_DOMAIN_UST
;
113 /* Checked by the caller. */
117 handle
= lttng_create_handle(session_name
, &dom
);
118 if (handle
== NULL
) {
125 /* open a channels element */
126 ret
= mi_lttng_writer_open_element(writer
, config_element_channels
);
134 /* Strip channel list */
135 channel_name
= strtok(channel_list
, ",");
136 while (channel_name
!= NULL
) {
137 DBG("Disabling channel %s", channel_name
);
139 ret
= lttng_disable_channel(handle
, channel_name
);
141 ERR("Channel %s: %s (session %s)", channel_name
,
142 lttng_strerror(ret
), session_name
);
147 * We assume that if an error occurred the channel is still active.
148 * This might not be the case but is a good assumption.
149 * The client should look at the stderr stream
150 * for more informations.
156 MSG("%s channel %s disabled for session %s",
157 get_domain_str(dom
.type
), channel_name
, session_name
);
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(
233 opt_kernel
+ opt_userspace
, false);
239 arg_channel_list
= poptGetArg(pc
);
240 if (arg_channel_list
== NULL
) {
241 ERR("Missing channel name(s).");
246 channel_list
= strdup(arg_channel_list
);
247 if (channel_list
== NULL
) {
248 PERROR("Failed to copy channel name");
253 leftover
= poptGetArg(pc
);
255 ERR("Unknown argument: %s", leftover
);
260 if (!opt_session_name
) {
261 session_name
= get_session_name();
262 if (session_name
== NULL
) {
267 session_name
= opt_session_name
;
272 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
274 ret
= -LTTNG_ERR_NOMEM
;
278 /* Open command element */
279 ret
= mi_lttng_writer_command_open(writer
,
280 mi_lttng_element_command_disable_channel
);
286 /* Open output element */
287 ret
= mi_lttng_writer_open_element(writer
,
288 mi_lttng_element_command_output
);
295 command_ret
= disable_channels(session_name
, channel_list
);
302 /* Close output element */
303 ret
= mi_lttng_writer_close_element(writer
);
310 ret
= mi_lttng_writer_write_element_bool(writer
,
311 mi_lttng_element_success
, success
);
317 /* Command element close */
318 ret
= mi_lttng_writer_command_close(writer
);
327 if (writer
&& mi_lttng_writer_destroy(writer
)) {
328 /* Preserve original error code */
329 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
332 if (!opt_session_name
&& session_name
) {
338 /* Overwrite ret if an error occurred in disable_channels */
339 ret
= command_ret
? command_ret
: ret
;