Commit | Line | Data |
---|---|---|
26cc6b4e | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
26cc6b4e | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
26cc6b4e | 5 | * |
26cc6b4e DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
28ab034a JG |
9 | #include "../command.hpp" |
10 | ||
11 | #include <common/mi-lttng.hpp> | |
12 | ||
13 | #include <lttng/domain-internal.hpp> | |
14 | ||
26cc6b4e DG |
15 | #include <popt.h> |
16 | #include <stdio.h> | |
17 | #include <stdlib.h> | |
18 | #include <string.h> | |
19 | #include <sys/stat.h> | |
20 | #include <sys/types.h> | |
21 | #include <unistd.h> | |
50534d6f | 22 | |
e14f64a8 | 23 | static int opt_kernel; |
5440dc42 | 24 | static char *opt_session_name; |
26cc6b4e | 25 | static int opt_userspace; |
26cc6b4e | 26 | |
4fc83d94 PP |
27 | #ifdef LTTNG_EMBED_HELP |
28 | static const char help_msg[] = | |
29 | #include <lttng-disable-channel.1.h> | |
28ab034a | 30 | ; |
4fc83d94 PP |
31 | #endif |
32 | ||
26cc6b4e DG |
33 | enum { |
34 | OPT_HELP = 1, | |
35 | OPT_USERSPACE, | |
679b4943 | 36 | OPT_LIST_OPTIONS, |
26cc6b4e DG |
37 | }; |
38 | ||
cd80958d | 39 | static struct lttng_handle *handle; |
50534d6f | 40 | static struct mi_writer *writer; |
cd80958d | 41 | |
26cc6b4e DG |
42 | static struct poptOption long_options[] = { |
43 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
cd9adb8b JG |
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 } | |
26cc6b4e DG |
50 | }; |
51 | ||
28ab034a | 52 | static int mi_partial_channel_print(char *channel_name, unsigned int enabled, int success) |
50534d6f JRJ |
53 | { |
54 | int ret; | |
55 | ||
a0377dfe FD |
56 | LTTNG_ASSERT(writer); |
57 | LTTNG_ASSERT(channel_name); | |
50534d6f JRJ |
58 | |
59 | /* Open channel element */ | |
60 | ret = mi_lttng_writer_open_element(writer, config_element_channel); | |
61 | if (ret) { | |
62 | goto end; | |
63 | } | |
64 | ||
65 | /* Name */ | |
28ab034a | 66 | ret = mi_lttng_writer_write_element_string(writer, config_element_name, channel_name); |
50534d6f JRJ |
67 | if (ret) { |
68 | goto end; | |
69 | } | |
70 | ||
71 | /* Enabled ? */ | |
28ab034a | 72 | ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled); |
50534d6f JRJ |
73 | if (ret) { |
74 | goto end; | |
75 | } | |
76 | ||
77 | /* Success ? */ | |
28ab034a | 78 | ret = mi_lttng_writer_write_element_bool(writer, mi_lttng_element_success, success); |
50534d6f JRJ |
79 | if (ret) { |
80 | goto end; | |
81 | } | |
82 | ||
83 | /* Closing channel element */ | |
84 | ret = mi_lttng_writer_close_element(writer); | |
85 | ||
86 | end: | |
87 | return ret; | |
88 | } | |
89 | ||
26cc6b4e | 90 | /* |
cd80958d | 91 | * Disabling channel using the lttng API. |
26cc6b4e | 92 | */ |
5b915816 | 93 | static int disable_channels(char *session_name, char *channel_list) |
26cc6b4e | 94 | { |
50534d6f JRJ |
95 | int ret = CMD_SUCCESS, warn = 0, success; |
96 | ||
97 | /* Normal case for disable channed is enabled = 0 */ | |
98 | unsigned int enabled = 0; | |
26cc6b4e | 99 | char *channel_name; |
7d29a247 | 100 | struct lttng_domain dom; |
26cc6b4e | 101 | |
441c16a7 MD |
102 | memset(&dom, 0, sizeof(dom)); |
103 | ||
d78d6610 | 104 | /* Create lttng domain */ |
7d29a247 DG |
105 | if (opt_kernel) { |
106 | dom.type = LTTNG_DOMAIN_KERNEL; | |
d78d6610 | 107 | } else if (opt_userspace) { |
78f0bacd | 108 | dom.type = LTTNG_DOMAIN_UST; |
78f0bacd | 109 | } else { |
3ecec76a | 110 | /* Checked by the caller. */ |
a0377dfe | 111 | abort(); |
7d29a247 DG |
112 | } |
113 | ||
cd80958d | 114 | handle = lttng_create_handle(session_name, &dom); |
cd9adb8b | 115 | if (handle == nullptr) { |
cd80958d DG |
116 | ret = -1; |
117 | goto error; | |
118 | } | |
119 | ||
50534d6f JRJ |
120 | /* Prepare MI */ |
121 | if (lttng_opt_mi) { | |
122 | /* open a channels element */ | |
123 | ret = mi_lttng_writer_open_element(writer, config_element_channels); | |
124 | if (ret) { | |
125 | ret = CMD_ERROR; | |
126 | goto error; | |
127 | } | |
50534d6f JRJ |
128 | } |
129 | ||
26cc6b4e | 130 | /* Strip channel list */ |
5b915816 | 131 | channel_name = strtok(channel_list, ","); |
cd9adb8b | 132 | while (channel_name != nullptr) { |
78f0bacd DG |
133 | DBG("Disabling channel %s", channel_name); |
134 | ||
135 | ret = lttng_disable_channel(handle, channel_name); | |
136 | if (ret < 0) { | |
28ab034a JG |
137 | ERR("Channel %s: %s (session %s)", |
138 | channel_name, | |
139 | lttng_strerror(ret), | |
140 | session_name); | |
ae856491 | 141 | warn = 1; |
50534d6f JRJ |
142 | |
143 | /* | |
144 | * Mi: | |
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. | |
9618049b JRJ |
147 | * The client should look at the stderr stream |
148 | * for more informations. | |
50534d6f JRJ |
149 | */ |
150 | enabled = 1; | |
151 | success = 0; | |
152 | ||
26cc6b4e | 153 | } else { |
7885e399 | 154 | MSG("%s channel %s disabled for session %s", |
28ab034a JG |
155 | lttng_domain_type_str(dom.type), |
156 | channel_name, | |
157 | session_name); | |
50534d6f JRJ |
158 | enabled = 0; |
159 | success = 1; | |
160 | } | |
161 | ||
162 | /* Print the channel */ | |
163 | if (lttng_opt_mi) { | |
164 | ret = mi_partial_channel_print(channel_name, enabled, success); | |
165 | if (ret) { | |
166 | ret = CMD_ERROR; | |
167 | goto error; | |
168 | } | |
26cc6b4e DG |
169 | } |
170 | ||
171 | /* Next channel */ | |
cd9adb8b | 172 | channel_name = strtok(nullptr, ","); |
26cc6b4e DG |
173 | } |
174 | ||
ae856491 DG |
175 | ret = CMD_SUCCESS; |
176 | ||
50534d6f JRJ |
177 | /* Close Mi */ |
178 | if (lttng_opt_mi) { | |
179 | /* Close channels element */ | |
180 | ret = mi_lttng_writer_close_element(writer); | |
181 | if (ret) { | |
182 | ret = CMD_ERROR; | |
183 | goto error; | |
184 | } | |
185 | } | |
186 | ||
26cc6b4e | 187 | error: |
50534d6f JRJ |
188 | /* Bypass the warning if a more important error happened */ |
189 | if (!ret && warn) { | |
ae856491 DG |
190 | ret = CMD_WARNING; |
191 | } | |
192 | ||
cd80958d DG |
193 | lttng_destroy_handle(handle); |
194 | ||
26cc6b4e DG |
195 | return ret; |
196 | } | |
197 | ||
198 | /* | |
199 | * cmd_disable_channels | |
200 | * | |
201 | * Disable channel to trace session | |
202 | */ | |
203 | int cmd_disable_channels(int argc, const char **argv) | |
204 | { | |
50534d6f | 205 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
26cc6b4e | 206 | static poptContext pc; |
cd9adb8b JG |
207 | char *session_name = nullptr; |
208 | char *channel_list = nullptr; | |
209 | const char *arg_channel_list = nullptr; | |
210 | const char *leftover = nullptr; | |
26cc6b4e | 211 | |
cd9adb8b | 212 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
26cc6b4e DG |
213 | poptReadDefaultConfig(pc, 0); |
214 | ||
215 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
216 | switch (opt) { | |
217 | case OPT_HELP: | |
4ba92f18 | 218 | SHOW_HELP(); |
26cc6b4e DG |
219 | goto end; |
220 | case OPT_USERSPACE: | |
221 | opt_userspace = 1; | |
222 | break; | |
679b4943 SM |
223 | case OPT_LIST_OPTIONS: |
224 | list_cmd_options(stdout, long_options); | |
679b4943 | 225 | goto end; |
26cc6b4e | 226 | default: |
26cc6b4e DG |
227 | ret = CMD_UNDEFINED; |
228 | goto end; | |
229 | } | |
230 | } | |
231 | ||
28ab034a | 232 | ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace, false); |
3ecec76a PP |
233 | if (ret) { |
234 | ret = CMD_ERROR; | |
235 | goto end; | |
236 | } | |
237 | ||
5b915816 | 238 | arg_channel_list = poptGetArg(pc); |
cd9adb8b | 239 | if (arg_channel_list == nullptr) { |
5b915816 MJ |
240 | ERR("Missing channel name(s)."); |
241 | ret = CMD_ERROR; | |
242 | goto end; | |
243 | } | |
244 | ||
245 | channel_list = strdup(arg_channel_list); | |
cd9adb8b | 246 | if (channel_list == nullptr) { |
5b915816 | 247 | PERROR("Failed to copy channel name"); |
ca1c3607 | 248 | ret = CMD_ERROR; |
26cc6b4e DG |
249 | goto end; |
250 | } | |
251 | ||
68c7f6e5 JD |
252 | leftover = poptGetArg(pc); |
253 | if (leftover) { | |
254 | ERR("Unknown argument: %s", leftover); | |
255 | ret = CMD_ERROR; | |
256 | goto end; | |
257 | } | |
258 | ||
cd80958d DG |
259 | if (!opt_session_name) { |
260 | session_name = get_session_name(); | |
cd9adb8b | 261 | if (session_name == nullptr) { |
ca1c3607 | 262 | ret = CMD_ERROR; |
cd80958d DG |
263 | goto end; |
264 | } | |
265 | } else { | |
266 | session_name = opt_session_name; | |
267 | } | |
268 | ||
50534d6f JRJ |
269 | /* Mi check */ |
270 | if (lttng_opt_mi) { | |
271 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
272 | if (!writer) { | |
273 | ret = -LTTNG_ERR_NOMEM; | |
274 | goto end; | |
275 | } | |
276 | ||
277 | /* Open command element */ | |
278 | ret = mi_lttng_writer_command_open(writer, | |
28ab034a | 279 | mi_lttng_element_command_disable_channel); |
50534d6f JRJ |
280 | if (ret) { |
281 | ret = CMD_ERROR; | |
282 | goto end; | |
283 | } | |
284 | ||
285 | /* Open output element */ | |
28ab034a | 286 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
50534d6f JRJ |
287 | if (ret) { |
288 | ret = CMD_ERROR; | |
289 | goto end; | |
290 | } | |
291 | } | |
292 | ||
5b915816 | 293 | command_ret = disable_channels(session_name, channel_list); |
50534d6f JRJ |
294 | if (command_ret) { |
295 | success = 0; | |
296 | } | |
297 | ||
298 | /* Mi closing */ | |
299 | if (lttng_opt_mi) { | |
300 | /* Close output element */ | |
301 | ret = mi_lttng_writer_close_element(writer); | |
302 | if (ret) { | |
303 | ret = CMD_ERROR; | |
304 | goto end; | |
305 | } | |
306 | ||
307 | /* Success ? */ | |
28ab034a | 308 | ret = mi_lttng_writer_write_element_bool(writer, mi_lttng_element_success, success); |
50534d6f JRJ |
309 | if (ret) { |
310 | ret = CMD_ERROR; | |
311 | goto end; | |
312 | } | |
313 | ||
314 | /* Command element close */ | |
315 | ret = mi_lttng_writer_command_close(writer); | |
316 | if (ret) { | |
317 | ret = CMD_ERROR; | |
318 | goto end; | |
319 | } | |
320 | } | |
26cc6b4e DG |
321 | |
322 | end: | |
50534d6f JRJ |
323 | /* Mi clean-up */ |
324 | if (writer && mi_lttng_writer_destroy(writer)) { | |
325 | /* Preserve original error code */ | |
326 | ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL; | |
327 | } | |
328 | ||
5853fd43 DG |
329 | if (!opt_session_name && session_name) { |
330 | free(session_name); | |
331 | } | |
50534d6f | 332 | |
5b915816 MJ |
333 | free(channel_list); |
334 | ||
50534d6f JRJ |
335 | /* Overwrite ret if an error occurred in disable_channels */ |
336 | ret = command_ret ? command_ret : ret; | |
337 | ||
ca1c3607 | 338 | poptFreeContext(pc); |
26cc6b4e DG |
339 | return ret; |
340 | } |