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