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