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>
19 #include <lttng/domain-internal.h>
21 #include "../command.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
},
52 static int mi_partial_channel_print(char *channel_name
, unsigned int enabled
,
60 /* Open channel element */
61 ret
= mi_lttng_writer_open_element(writer
, config_element_channel
);
67 ret
= mi_lttng_writer_write_element_string(writer
, config_element_name
,
74 ret
= mi_lttng_writer_write_element_bool(writer
, config_element_enabled
,
81 ret
= mi_lttng_writer_write_element_bool(writer
,
82 mi_lttng_element_success
, success
);
87 /* Closing channel element */
88 ret
= mi_lttng_writer_close_element(writer
);
95 * Disabling channel using the lttng API.
97 static int disable_channels(char *session_name
, char *channel_list
)
99 int ret
= CMD_SUCCESS
, warn
= 0, success
;
101 /* Normal case for disable channed is enabled = 0 */
102 unsigned int enabled
= 0;
104 struct lttng_domain dom
;
106 memset(&dom
, 0, sizeof(dom
));
108 /* Create lttng domain */
110 dom
.type
= LTTNG_DOMAIN_KERNEL
;
111 } else if (opt_userspace
) {
112 dom
.type
= LTTNG_DOMAIN_UST
;
114 /* Checked by the caller. */
118 handle
= lttng_create_handle(session_name
, &dom
);
119 if (handle
== NULL
) {
126 /* open a channels element */
127 ret
= mi_lttng_writer_open_element(writer
, config_element_channels
);
135 /* Strip channel list */
136 channel_name
= strtok(channel_list
, ",");
137 while (channel_name
!= NULL
) {
138 DBG("Disabling channel %s", channel_name
);
140 ret
= lttng_disable_channel(handle
, channel_name
);
142 ERR("Channel %s: %s (session %s)", channel_name
,
143 lttng_strerror(ret
), session_name
);
148 * We assume that if an error occurred the channel is still active.
149 * This might not be the case but is a good assumption.
150 * The client should look at the stderr stream
151 * for more informations.
157 MSG("%s channel %s disabled for session %s",
158 lttng_domain_type_str(dom
.type
),
159 channel_name
, session_name
);
164 /* Print the channel */
166 ret
= mi_partial_channel_print(channel_name
, enabled
, success
);
174 channel_name
= strtok(NULL
, ",");
181 /* Close channels element */
182 ret
= mi_lttng_writer_close_element(writer
);
190 /* Bypass the warning if a more important error happened */
195 lttng_destroy_handle(handle
);
201 * cmd_disable_channels
203 * Disable channel to trace session
205 int cmd_disable_channels(int argc
, const char **argv
)
207 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
208 static poptContext pc
;
209 char *session_name
= NULL
;
210 char *channel_list
= NULL
;
211 const char *arg_channel_list
= NULL
;
212 const char *leftover
= NULL
;
214 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
215 poptReadDefaultConfig(pc
, 0);
217 while ((opt
= poptGetNextOpt(pc
)) != -1) {
225 case OPT_LIST_OPTIONS
:
226 list_cmd_options(stdout
, long_options
);
234 ret
= print_missing_or_multiple_domains(
235 opt_kernel
+ opt_userspace
, false);
241 arg_channel_list
= poptGetArg(pc
);
242 if (arg_channel_list
== NULL
) {
243 ERR("Missing channel name(s).");
248 channel_list
= strdup(arg_channel_list
);
249 if (channel_list
== NULL
) {
250 PERROR("Failed to copy channel name");
255 leftover
= poptGetArg(pc
);
257 ERR("Unknown argument: %s", leftover
);
262 if (!opt_session_name
) {
263 session_name
= get_session_name();
264 if (session_name
== NULL
) {
269 session_name
= opt_session_name
;
274 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
276 ret
= -LTTNG_ERR_NOMEM
;
280 /* Open command element */
281 ret
= mi_lttng_writer_command_open(writer
,
282 mi_lttng_element_command_disable_channel
);
288 /* Open output element */
289 ret
= mi_lttng_writer_open_element(writer
,
290 mi_lttng_element_command_output
);
297 command_ret
= disable_channels(session_name
, channel_list
);
304 /* Close output element */
305 ret
= mi_lttng_writer_close_element(writer
);
312 ret
= mi_lttng_writer_write_element_bool(writer
,
313 mi_lttng_element_success
, success
);
319 /* Command element close */
320 ret
= mi_lttng_writer_command_close(writer
);
329 if (writer
&& mi_lttng_writer_destroy(writer
)) {
330 /* Preserve original error code */
331 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
334 if (!opt_session_name
&& session_name
) {
340 /* Overwrite ret if an error occurred in disable_channels */
341 ret
= command_ret
? command_ret
: ret
;