Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.c
CommitLineData
26cc6b4e 1/*
a4eb26f0 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
JRJ
16#include <assert.h>
17
18#include <common/mi-lttng.h>
26cc6b4e 19
c399183f 20#include "../command.h"
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
56 assert(writer);
57 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,
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 */
d569ed5f 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
PP
113 /* Checked by the caller. */
114 assert(0);
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 */
d569ed5f 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",
b9dfb167 157 get_domain_str(dom.type), channel_name, session_name);
50534d6f
JRJ
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 }
26cc6b4e
DG
169 }
170
171 /* Next channel */
172 channel_name = strtok(NULL, ",");
173 }
174
ae856491
DG
175 ret = CMD_SUCCESS;
176
50534d6f
JRJ
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
26cc6b4e 187error:
50534d6f
JRJ
188 /* Bypass the warning if a more important error happened */
189 if (!ret && warn) {
ae856491
DG
190 ret = CMD_WARNING;
191 }
192
cd80958d
DG
193 lttng_destroy_handle(handle);
194
26cc6b4e
DG
195 return ret;
196}
197
198/*
199 * cmd_disable_channels
200 *
201 * Disable channel to trace session
202 */
203int cmd_disable_channels(int argc, const char **argv)
204{
50534d6f 205 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
26cc6b4e 206 static poptContext pc;
cd80958d 207 char *session_name = NULL;
d569ed5f
MJ
208 char *channel_list = NULL;
209 const char *arg_channel_list = NULL;
68c7f6e5 210 const char *leftover = NULL;
26cc6b4e
DG
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:
4ba92f18 218 SHOW_HELP();
26cc6b4e
DG
219 goto end;
220 case OPT_USERSPACE:
221 opt_userspace = 1;
222 break;
679b4943
SM
223 case OPT_LIST_OPTIONS:
224 list_cmd_options(stdout, long_options);
679b4943 225 goto end;
26cc6b4e 226 default:
26cc6b4e
DG
227 ret = CMD_UNDEFINED;
228 goto end;
229 }
230 }
231
2948cce5
JG
232 ret = print_missing_or_multiple_domains(
233 opt_kernel + opt_userspace, false);
3ecec76a
PP
234 if (ret) {
235 ret = CMD_ERROR;
236 goto end;
237 }
238
d569ed5f
MJ
239 arg_channel_list = poptGetArg(pc);
240 if (arg_channel_list == NULL) {
241 ERR("Missing channel name(s).");
242 ret = CMD_ERROR;
243 goto end;
244 }
245
246 channel_list = strdup(arg_channel_list);
247 if (channel_list == NULL) {
248 PERROR("Failed to copy channel name");
ca1c3607 249 ret = CMD_ERROR;
26cc6b4e
DG
250 goto end;
251 }
252
68c7f6e5
JD
253 leftover = poptGetArg(pc);
254 if (leftover) {
255 ERR("Unknown argument: %s", leftover);
256 ret = CMD_ERROR;
257 goto end;
258 }
259
cd80958d
DG
260 if (!opt_session_name) {
261 session_name = get_session_name();
262 if (session_name == NULL) {
ca1c3607 263 ret = CMD_ERROR;
cd80958d
DG
264 goto end;
265 }
266 } else {
267 session_name = opt_session_name;
268 }
269
50534d6f
JRJ
270 /* Mi check */
271 if (lttng_opt_mi) {
272 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
273 if (!writer) {
274 ret = -LTTNG_ERR_NOMEM;
275 goto end;
276 }
277
278 /* Open command element */
279 ret = mi_lttng_writer_command_open(writer,
280 mi_lttng_element_command_disable_channel);
281 if (ret) {
282 ret = CMD_ERROR;
283 goto end;
284 }
285
286 /* Open output element */
287 ret = mi_lttng_writer_open_element(writer,
288 mi_lttng_element_command_output);
289 if (ret) {
290 ret = CMD_ERROR;
291 goto end;
292 }
293 }
294
d569ed5f 295 command_ret = disable_channels(session_name, channel_list);
50534d6f
JRJ
296 if (command_ret) {
297 success = 0;
298 }
299
300 /* Mi closing */
301 if (lttng_opt_mi) {
302 /* Close output element */
303 ret = mi_lttng_writer_close_element(writer);
304 if (ret) {
305 ret = CMD_ERROR;
306 goto end;
307 }
308
309 /* Success ? */
310 ret = mi_lttng_writer_write_element_bool(writer,
311 mi_lttng_element_success, success);
312 if (ret) {
313 ret = CMD_ERROR;
314 goto end;
315 }
316
317 /* Command element close */
318 ret = mi_lttng_writer_command_close(writer);
319 if (ret) {
320 ret = CMD_ERROR;
321 goto end;
322 }
323 }
26cc6b4e
DG
324
325end:
50534d6f
JRJ
326 /* Mi clean-up */
327 if (writer && mi_lttng_writer_destroy(writer)) {
328 /* Preserve original error code */
329 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
330 }
331
5853fd43
DG
332 if (!opt_session_name && session_name) {
333 free(session_name);
334 }
50534d6f 335
d569ed5f
MJ
336 free(channel_list);
337
50534d6f
JRJ
338 /* Overwrite ret if an error occurred in disable_channels */
339 ret = command_ret ? command_ret : ret;
340
ca1c3607 341 poptFreeContext(pc);
26cc6b4e
DG
342 return ret;
343}
This page took 0.072446 seconds and 4 git commands to generate.