Add a basic .clang-tidy file and fix typedef warnings
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "../command.hpp"
10
11 #include <common/mi-lttng.hpp>
12
13 #include <lttng/domain-internal.hpp>
14
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>
22
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, int success)
53 {
54 int ret;
55
56 LTTNG_ASSERT(writer);
57 LTTNG_ASSERT(channel_name);
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 */
66 ret = mi_lttng_writer_write_element_string(writer, config_element_name, channel_name);
67 if (ret) {
68 goto end;
69 }
70
71 /* Enabled ? */
72 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled);
73 if (ret) {
74 goto end;
75 }
76
77 /* Success ? */
78 ret = mi_lttng_writer_write_element_bool(writer, mi_lttng_element_success, success);
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
90 /*
91 * Disabling channel using the lttng API.
92 */
93 static int disable_channels(char *session_name, char *channel_list)
94 {
95 int ret = CMD_SUCCESS, warn = 0, success;
96
97 /* Normal case for disable channed is enabled = 0 */
98 unsigned int enabled = 0;
99 char *channel_name;
100 struct lttng_domain dom;
101
102 memset(&dom, 0, sizeof(dom));
103
104 /* Create lttng domain */
105 if (opt_kernel) {
106 dom.type = LTTNG_DOMAIN_KERNEL;
107 } else if (opt_userspace) {
108 dom.type = LTTNG_DOMAIN_UST;
109 } else {
110 /* Checked by the caller. */
111 abort();
112 }
113
114 handle = lttng_create_handle(session_name, &dom);
115 if (handle == NULL) {
116 ret = -1;
117 goto error;
118 }
119
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 }
128 }
129
130 /* Strip channel list */
131 channel_name = strtok(channel_list, ",");
132 while (channel_name != NULL) {
133 DBG("Disabling channel %s", channel_name);
134
135 ret = lttng_disable_channel(handle, channel_name);
136 if (ret < 0) {
137 ERR("Channel %s: %s (session %s)",
138 channel_name,
139 lttng_strerror(ret),
140 session_name);
141 warn = 1;
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.
147 * The client should look at the stderr stream
148 * for more informations.
149 */
150 enabled = 1;
151 success = 0;
152
153 } else {
154 MSG("%s channel %s disabled for session %s",
155 lttng_domain_type_str(dom.type),
156 channel_name,
157 session_name);
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 }
169 }
170
171 /* Next channel */
172 channel_name = strtok(NULL, ",");
173 }
174
175 ret = CMD_SUCCESS;
176
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
187 error:
188 /* Bypass the warning if a more important error happened */
189 if (!ret && warn) {
190 ret = CMD_WARNING;
191 }
192
193 lttng_destroy_handle(handle);
194
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 {
205 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
206 static poptContext pc;
207 char *session_name = NULL;
208 char *channel_list = NULL;
209 const char *arg_channel_list = NULL;
210 const char *leftover = NULL;
211
212 pc = poptGetContext(NULL, argc, argv, long_options, 0);
213 poptReadDefaultConfig(pc, 0);
214
215 while ((opt = poptGetNextOpt(pc)) != -1) {
216 switch (opt) {
217 case OPT_HELP:
218 SHOW_HELP();
219 goto end;
220 case OPT_USERSPACE:
221 opt_userspace = 1;
222 break;
223 case OPT_LIST_OPTIONS:
224 list_cmd_options(stdout, long_options);
225 goto end;
226 default:
227 ret = CMD_UNDEFINED;
228 goto end;
229 }
230 }
231
232 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace, false);
233 if (ret) {
234 ret = CMD_ERROR;
235 goto end;
236 }
237
238 arg_channel_list = poptGetArg(pc);
239 if (arg_channel_list == NULL) {
240 ERR("Missing channel name(s).");
241 ret = CMD_ERROR;
242 goto end;
243 }
244
245 channel_list = strdup(arg_channel_list);
246 if (channel_list == NULL) {
247 PERROR("Failed to copy channel name");
248 ret = CMD_ERROR;
249 goto end;
250 }
251
252 leftover = poptGetArg(pc);
253 if (leftover) {
254 ERR("Unknown argument: %s", leftover);
255 ret = CMD_ERROR;
256 goto end;
257 }
258
259 if (!opt_session_name) {
260 session_name = get_session_name();
261 if (session_name == NULL) {
262 ret = CMD_ERROR;
263 goto end;
264 }
265 } else {
266 session_name = opt_session_name;
267 }
268
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,
279 mi_lttng_element_command_disable_channel);
280 if (ret) {
281 ret = CMD_ERROR;
282 goto end;
283 }
284
285 /* Open output element */
286 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output);
287 if (ret) {
288 ret = CMD_ERROR;
289 goto end;
290 }
291 }
292
293 command_ret = disable_channels(session_name, channel_list);
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 ? */
308 ret = mi_lttng_writer_write_element_bool(writer, mi_lttng_element_success, success);
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 }
321
322 end:
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
329 if (!opt_session_name && session_name) {
330 free(session_name);
331 }
332
333 free(channel_list);
334
335 /* Overwrite ret if an error occurred in disable_channels */
336 ret = command_ret ? command_ret : ret;
337
338 poptFreeContext(pc);
339 return ret;
340 }
This page took 0.04295 seconds and 5 git commands to generate.