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
, nullptr, OPT_HELP
, nullptr, nullptr },
45 { "session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, nullptr, nullptr },
46 { "kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, nullptr, nullptr },
47 { "userspace", 'u', POPT_ARG_NONE
, nullptr, OPT_USERSPACE
, nullptr, nullptr },
48 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
49 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
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
;
96 /* Normal case for disable channed is enabled = false */
97 bool warn
= false, success
, enabled
= false;
99 struct lttng_domain dom
;
101 memset(&dom
, 0, sizeof(dom
));
103 /* Create lttng domain */
105 dom
.type
= LTTNG_DOMAIN_KERNEL
;
106 } else if (opt_userspace
) {
107 dom
.type
= LTTNG_DOMAIN_UST
;
109 /* Checked by the caller. */
113 handle
= lttng_create_handle(session_name
, &dom
);
114 if (handle
== nullptr) {
121 /* open a channels element */
122 ret
= mi_lttng_writer_open_element(writer
, config_element_channels
);
129 /* Strip channel list */
130 channel_name
= strtok(channel_list
, ",");
131 while (channel_name
!= nullptr) {
132 DBG("Disabling channel %s", channel_name
);
134 ret
= lttng_disable_channel(handle
, channel_name
);
136 ERR("Channel %s: %s (session %s)",
144 * We assume that if an error occurred the channel is still active.
145 * This might not be the case but is a good assumption.
146 * The client should look at the stderr stream
147 * for more informations.
153 MSG("%s channel %s disabled for session %s",
154 lttng_domain_type_str(dom
.type
),
161 /* Print the channel */
163 ret
= mi_partial_channel_print(channel_name
, enabled
, success
);
171 channel_name
= strtok(nullptr, ",");
178 /* Close channels element */
179 ret
= mi_lttng_writer_close_element(writer
);
187 /* Bypass the warning if a more important error happened */
192 lttng_destroy_handle(handle
);
198 * cmd_disable_channels
200 * Disable channel to trace session
202 int cmd_disable_channels(int argc
, const char **argv
)
204 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
205 static poptContext pc
;
206 char *session_name
= nullptr;
207 char *channel_list
= nullptr;
208 const char *arg_channel_list
= nullptr;
209 const char *leftover
= nullptr;
211 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
212 poptReadDefaultConfig(pc
, 0);
214 while ((opt
= poptGetNextOpt(pc
)) != -1) {
222 case OPT_LIST_OPTIONS
:
223 list_cmd_options(stdout
, long_options
);
231 ret
= print_missing_or_multiple_domains(opt_kernel
+ opt_userspace
, false);
237 arg_channel_list
= poptGetArg(pc
);
238 if (arg_channel_list
== nullptr) {
239 ERR("Missing channel name(s).");
244 channel_list
= strdup(arg_channel_list
);
245 if (channel_list
== nullptr) {
246 PERROR("Failed to copy channel name");
251 leftover
= poptGetArg(pc
);
253 ERR("Unknown argument: %s", leftover
);
258 if (!opt_session_name
) {
259 session_name
= get_session_name();
260 if (session_name
== nullptr) {
265 session_name
= opt_session_name
;
270 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
272 ret
= -LTTNG_ERR_NOMEM
;
276 /* Open command element */
277 ret
= mi_lttng_writer_command_open(writer
,
278 mi_lttng_element_command_disable_channel
);
284 /* Open output element */
285 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
292 command_ret
= disable_channels(session_name
, channel_list
);
299 /* Close output element */
300 ret
= mi_lttng_writer_close_element(writer
);
307 ret
= mi_lttng_writer_write_element_bool(writer
, mi_lttng_element_success
, success
);
313 /* Command element close */
314 ret
= mi_lttng_writer_command_close(writer
);
323 if (writer
&& mi_lttng_writer_destroy(writer
)) {
324 /* Preserve original error code */
325 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
328 if (!opt_session_name
&& session_name
) {
334 /* Overwrite ret if an error occurred in disable_channels */
335 ret
= command_ret
? command_ret
: ret
;