344be237d26c2d4581c26c50363dbbe7558293ff
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
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>
16 #include <assert.h>
17
18 #include <common/mi-lttng.h>
19
20 #include "../command.h"
21
22 static char *opt_channels;
23 static int opt_kernel;
24 static char *opt_session_name;
25 static int opt_userspace;
26
27 #ifdef LTTNG_EMBED_HELP
28 static const char help_msg[] =
29 #include <lttng-disable-channel.1.h>
30 ;
31 #endif
32
33 enum {
34 OPT_HELP = 1,
35 OPT_USERSPACE,
36 OPT_LIST_OPTIONS,
37 };
38
39 static struct lttng_handle *handle;
40 static struct mi_writer *writer;
41
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},
49 {0, 0, 0, 0, 0, 0, 0}
50 };
51
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,
82 mi_lttng_element_success, success);
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
94 /*
95 * Disabling channel using the lttng API.
96 */
97 static int disable_channels(char *session_name)
98 {
99 int ret = CMD_SUCCESS, warn = 0, success;
100
101 /* Normal case for disable channed is enabled = 0 */
102 unsigned int enabled = 0;
103 char *channel_name;
104 struct lttng_domain dom;
105
106 memset(&dom, 0, sizeof(dom));
107
108 /* Create lttng domain */
109 if (opt_kernel) {
110 dom.type = LTTNG_DOMAIN_KERNEL;
111 } else if (opt_userspace) {
112 dom.type = LTTNG_DOMAIN_UST;
113 } else {
114 /* Checked by the caller. */
115 assert(0);
116 }
117
118 handle = lttng_create_handle(session_name, &dom);
119 if (handle == NULL) {
120 ret = -1;
121 goto error;
122 }
123
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
135 /* Strip channel list */
136 channel_name = strtok(opt_channels, ",");
137 while (channel_name != NULL) {
138 DBG("Disabling channel %s", channel_name);
139
140 ret = lttng_disable_channel(handle, channel_name);
141 if (ret < 0) {
142 ERR("Channel %s: %s (session %s)", channel_name,
143 lttng_strerror(ret), session_name);
144 warn = 1;
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.
150 * The client should look at the stderr stream
151 * for more informations.
152 */
153 enabled = 1;
154 success = 0;
155
156 } else {
157 MSG("%s channel %s disabled for session %s",
158 get_domain_str(dom.type), channel_name, session_name);
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 }
170 }
171
172 /* Next channel */
173 channel_name = strtok(NULL, ",");
174 }
175
176 ret = CMD_SUCCESS;
177
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
188 error:
189 /* Bypass the warning if a more important error happened */
190 if (!ret && warn) {
191 ret = CMD_WARNING;
192 }
193
194 lttng_destroy_handle(handle);
195
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 {
206 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
207 static poptContext pc;
208 char *session_name = NULL;
209 const char *leftover = NULL;
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:
217 SHOW_HELP();
218 goto end;
219 case OPT_USERSPACE:
220 opt_userspace = 1;
221 break;
222 case OPT_LIST_OPTIONS:
223 list_cmd_options(stdout, long_options);
224 goto end;
225 default:
226 ret = CMD_UNDEFINED;
227 goto end;
228 }
229 }
230
231 ret = print_missing_or_multiple_domains(
232 opt_kernel + opt_userspace, false);
233 if (ret) {
234 ret = CMD_ERROR;
235 goto end;
236 }
237
238 opt_channels = (char*) poptGetArg(pc);
239 if (opt_channels == NULL) {
240 ERR("Missing channel name(s).\n");
241 ret = CMD_ERROR;
242 goto end;
243 }
244
245 leftover = poptGetArg(pc);
246 if (leftover) {
247 ERR("Unknown argument: %s", leftover);
248 ret = CMD_ERROR;
249 goto end;
250 }
251
252 if (!opt_session_name) {
253 session_name = get_session_name();
254 if (session_name == NULL) {
255 ret = CMD_ERROR;
256 goto end;
257 }
258 } else {
259 session_name = opt_session_name;
260 }
261
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 }
316
317 end:
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
324 if (!opt_session_name && session_name) {
325 free(session_name);
326 }
327
328 /* Overwrite ret if an error occurred in disable_channels */
329 ret = command_ret ? command_ret : ret;
330
331 poptFreeContext(pc);
332 return ret;
333 }
This page took 0.050759 seconds and 3 git commands to generate.