Build fix: missing stdio.h include in signal-helper.hpp
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.cpp
CommitLineData
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
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 16
c9e313bc
SM
17#include <common/mi-lttng.hpp>
18#include <lttng/domain-internal.hpp>
26cc6b4e 19
c9e313bc 20#include "../command.hpp"
26cc6b4e 21
e14f64a8 22static int opt_kernel;
5440dc42 23static char *opt_session_name;
26cc6b4e 24static int opt_userspace;
26cc6b4e 25
4fc83d94
PP
26#ifdef LTTNG_EMBED_HELP
27static const char help_msg[] =
28#include <lttng-disable-channel.1.h>
29;
30#endif
31
26cc6b4e
DG
32enum {
33 OPT_HELP = 1,
34 OPT_USERSPACE,
679b4943 35 OPT_LIST_OPTIONS,
26cc6b4e
DG
36};
37
cd80958d 38static struct lttng_handle *handle;
50534d6f 39static struct mi_writer *writer;
cd80958d 40
26cc6b4e
DG
41static struct poptOption long_options[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
43 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 44 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
26cc6b4e 45 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 46 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
679b4943 47 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
26cc6b4e
DG
48 {0, 0, 0, 0, 0, 0, 0}
49};
50
50534d6f
JRJ
51static int mi_partial_channel_print(char *channel_name, unsigned int enabled,
52 int success)
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 */
66 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
67 channel_name);
68 if (ret) {
69 goto end;
70 }
71
72 /* Enabled ? */
73 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
74 enabled);
75 if (ret) {
76 goto end;
77 }
78
79 /* Success ? */
80 ret = mi_lttng_writer_write_element_bool(writer,
9618049b 81 mi_lttng_element_success, success);
50534d6f
JRJ
82 if (ret) {
83 goto end;
84 }
85
86 /* Closing channel element */
87 ret = mi_lttng_writer_close_element(writer);
88
89end:
90 return ret;
91}
92
26cc6b4e 93/*
cd80958d 94 * Disabling channel using the lttng API.
26cc6b4e 95 */
5b915816 96static int disable_channels(char *session_name, char *channel_list)
26cc6b4e 97{
50534d6f
JRJ
98 int ret = CMD_SUCCESS, warn = 0, success;
99
100 /* Normal case for disable channed is enabled = 0 */
101 unsigned int enabled = 0;
26cc6b4e 102 char *channel_name;
7d29a247 103 struct lttng_domain dom;
26cc6b4e 104
441c16a7
MD
105 memset(&dom, 0, sizeof(dom));
106
d78d6610 107 /* Create lttng domain */
7d29a247
DG
108 if (opt_kernel) {
109 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 110 } else if (opt_userspace) {
78f0bacd 111 dom.type = LTTNG_DOMAIN_UST;
78f0bacd 112 } else {
3ecec76a 113 /* Checked by the caller. */
a0377dfe 114 abort();
7d29a247
DG
115 }
116
cd80958d
DG
117 handle = lttng_create_handle(session_name, &dom);
118 if (handle == NULL) {
119 ret = -1;
120 goto error;
121 }
122
50534d6f
JRJ
123 /* Prepare MI */
124 if (lttng_opt_mi) {
125 /* open a channels element */
126 ret = mi_lttng_writer_open_element(writer, config_element_channels);
127 if (ret) {
128 ret = CMD_ERROR;
129 goto error;
130 }
131
132 }
133
26cc6b4e 134 /* Strip channel list */
5b915816 135 channel_name = strtok(channel_list, ",");
26cc6b4e 136 while (channel_name != NULL) {
78f0bacd
DG
137 DBG("Disabling channel %s", channel_name);
138
139 ret = lttng_disable_channel(handle, channel_name);
140 if (ret < 0) {
ae856491
DG
141 ERR("Channel %s: %s (session %s)", channel_name,
142 lttng_strerror(ret), session_name);
143 warn = 1;
50534d6f
JRJ
144
145 /*
146 * Mi:
147 * We assume that if an error occurred the channel is still active.
148 * This might not be the case but is a good assumption.
9618049b
JRJ
149 * The client should look at the stderr stream
150 * for more informations.
50534d6f
JRJ
151 */
152 enabled = 1;
153 success = 0;
154
26cc6b4e 155 } else {
7885e399 156 MSG("%s channel %s disabled for session %s",
1004b719
FD
157 lttng_domain_type_str(dom.type),
158 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 188error:
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 */
204int 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;
5b915816
MJ
209 char *channel_list = NULL;
210 const char *arg_channel_list = 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
5b915816
MJ
240 arg_channel_list = poptGetArg(pc);
241 if (arg_channel_list == NULL) {
242 ERR("Missing channel name(s).");
243 ret = CMD_ERROR;
244 goto end;
245 }
246
247 channel_list = strdup(arg_channel_list);
248 if (channel_list == NULL) {
249 PERROR("Failed to copy channel name");
ca1c3607 250 ret = CMD_ERROR;
26cc6b4e
DG
251 goto end;
252 }
253
68c7f6e5
JD
254 leftover = poptGetArg(pc);
255 if (leftover) {
256 ERR("Unknown argument: %s", leftover);
257 ret = CMD_ERROR;
258 goto end;
259 }
260
cd80958d
DG
261 if (!opt_session_name) {
262 session_name = get_session_name();
263 if (session_name == NULL) {
ca1c3607 264 ret = CMD_ERROR;
cd80958d
DG
265 goto end;
266 }
267 } else {
268 session_name = opt_session_name;
269 }
270
50534d6f
JRJ
271 /* Mi check */
272 if (lttng_opt_mi) {
273 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
274 if (!writer) {
275 ret = -LTTNG_ERR_NOMEM;
276 goto end;
277 }
278
279 /* Open command element */
280 ret = mi_lttng_writer_command_open(writer,
281 mi_lttng_element_command_disable_channel);
282 if (ret) {
283 ret = CMD_ERROR;
284 goto end;
285 }
286
287 /* Open output element */
288 ret = mi_lttng_writer_open_element(writer,
289 mi_lttng_element_command_output);
290 if (ret) {
291 ret = CMD_ERROR;
292 goto end;
293 }
294 }
295
5b915816 296 command_ret = disable_channels(session_name, channel_list);
50534d6f
JRJ
297 if (command_ret) {
298 success = 0;
299 }
300
301 /* Mi closing */
302 if (lttng_opt_mi) {
303 /* Close output element */
304 ret = mi_lttng_writer_close_element(writer);
305 if (ret) {
306 ret = CMD_ERROR;
307 goto end;
308 }
309
310 /* Success ? */
311 ret = mi_lttng_writer_write_element_bool(writer,
312 mi_lttng_element_success, success);
313 if (ret) {
314 ret = CMD_ERROR;
315 goto end;
316 }
317
318 /* Command element close */
319 ret = mi_lttng_writer_command_close(writer);
320 if (ret) {
321 ret = CMD_ERROR;
322 goto end;
323 }
324 }
26cc6b4e
DG
325
326end:
50534d6f
JRJ
327 /* Mi clean-up */
328 if (writer && mi_lttng_writer_destroy(writer)) {
329 /* Preserve original error code */
330 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
331 }
332
5853fd43
DG
333 if (!opt_session_name && session_name) {
334 free(session_name);
335 }
50534d6f 336
5b915816
MJ
337 free(channel_list);
338
50534d6f
JRJ
339 /* Overwrite ret if an error occurred in disable_channels */
340 ret = command_ret ? command_ret : ret;
341
ca1c3607 342 poptFreeContext(pc);
26cc6b4e
DG
343 return ret;
344}
This page took 0.081459 seconds and 4 git commands to generate.