Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.c
CommitLineData
26cc6b4e 1/*
90c106c6 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>
1ceda6f1 19#include <lttng/domain-internal.h>
26cc6b4e 20
c399183f 21#include "../command.h"
26cc6b4e 22
e14f64a8 23static int opt_kernel;
5440dc42 24static char *opt_session_name;
26cc6b4e 25static int opt_userspace;
26cc6b4e 26
4fc83d94
PP
27#ifdef LTTNG_EMBED_HELP
28static const char help_msg[] =
29#include <lttng-disable-channel.1.h>
30;
31#endif
32
26cc6b4e
DG
33enum {
34 OPT_HELP = 1,
35 OPT_USERSPACE,
679b4943 36 OPT_LIST_OPTIONS,
26cc6b4e
DG
37};
38
cd80958d 39static struct lttng_handle *handle;
50534d6f 40static struct mi_writer *writer;
cd80958d 41
26cc6b4e
DG
42static 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
52static 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
90end:
91 return ret;
92}
93
26cc6b4e 94/*
cd80958d 95 * Disabling channel using the lttng API.
26cc6b4e 96 */
b7f4ea0f 97static int disable_channels(char *session_name, char *channel_list)
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 135 /* Strip channel list */
b7f4ea0f 136 channel_name = strtok(channel_list, ",");
26cc6b4e 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",
1ceda6f1
FD
158 lttng_domain_type_str(dom.type),
159 channel_name, session_name);
50534d6f
JRJ
160 enabled = 0;
161 success = 1;
162 }
163
164 /* Print the channel */
165 if (lttng_opt_mi) {
166 ret = mi_partial_channel_print(channel_name, enabled, success);
167 if (ret) {
168 ret = CMD_ERROR;
169 goto error;
170 }
26cc6b4e
DG
171 }
172
173 /* Next channel */
174 channel_name = strtok(NULL, ",");
175 }
176
ae856491
DG
177 ret = CMD_SUCCESS;
178
50534d6f
JRJ
179 /* Close Mi */
180 if (lttng_opt_mi) {
181 /* Close channels element */
182 ret = mi_lttng_writer_close_element(writer);
183 if (ret) {
184 ret = CMD_ERROR;
185 goto error;
186 }
187 }
188
26cc6b4e 189error:
50534d6f
JRJ
190 /* Bypass the warning if a more important error happened */
191 if (!ret && warn) {
ae856491
DG
192 ret = CMD_WARNING;
193 }
194
cd80958d
DG
195 lttng_destroy_handle(handle);
196
26cc6b4e
DG
197 return ret;
198}
199
200/*
201 * cmd_disable_channels
202 *
203 * Disable channel to trace session
204 */
205int cmd_disable_channels(int argc, const char **argv)
206{
50534d6f 207 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
26cc6b4e 208 static poptContext pc;
cd80958d 209 char *session_name = NULL;
b7f4ea0f
MJ
210 char *channel_list = NULL;
211 const char *arg_channel_list = NULL;
68c7f6e5 212 const char *leftover = NULL;
26cc6b4e
DG
213
214 pc = poptGetContext(NULL, argc, argv, long_options, 0);
215 poptReadDefaultConfig(pc, 0);
216
217 while ((opt = poptGetNextOpt(pc)) != -1) {
218 switch (opt) {
219 case OPT_HELP:
4ba92f18 220 SHOW_HELP();
26cc6b4e
DG
221 goto end;
222 case OPT_USERSPACE:
223 opt_userspace = 1;
224 break;
679b4943
SM
225 case OPT_LIST_OPTIONS:
226 list_cmd_options(stdout, long_options);
679b4943 227 goto end;
26cc6b4e 228 default:
26cc6b4e
DG
229 ret = CMD_UNDEFINED;
230 goto end;
231 }
232 }
233
3533d06b
JG
234 ret = print_missing_or_multiple_domains(
235 opt_kernel + opt_userspace, false);
3ecec76a
PP
236 if (ret) {
237 ret = CMD_ERROR;
238 goto end;
239 }
240
b7f4ea0f
MJ
241 arg_channel_list = poptGetArg(pc);
242 if (arg_channel_list == NULL) {
243 ERR("Missing channel name(s).");
244 ret = CMD_ERROR;
245 goto end;
246 }
247
248 channel_list = strdup(arg_channel_list);
249 if (channel_list == NULL) {
250 PERROR("Failed to copy channel name");
ca1c3607 251 ret = CMD_ERROR;
26cc6b4e
DG
252 goto end;
253 }
254
68c7f6e5
JD
255 leftover = poptGetArg(pc);
256 if (leftover) {
257 ERR("Unknown argument: %s", leftover);
258 ret = CMD_ERROR;
259 goto end;
260 }
261
cd80958d
DG
262 if (!opt_session_name) {
263 session_name = get_session_name();
264 if (session_name == NULL) {
ca1c3607 265 ret = CMD_ERROR;
cd80958d
DG
266 goto end;
267 }
268 } else {
269 session_name = opt_session_name;
270 }
271
50534d6f
JRJ
272 /* Mi check */
273 if (lttng_opt_mi) {
274 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
275 if (!writer) {
276 ret = -LTTNG_ERR_NOMEM;
277 goto end;
278 }
279
280 /* Open command element */
281 ret = mi_lttng_writer_command_open(writer,
282 mi_lttng_element_command_disable_channel);
283 if (ret) {
284 ret = CMD_ERROR;
285 goto end;
286 }
287
288 /* Open output element */
289 ret = mi_lttng_writer_open_element(writer,
290 mi_lttng_element_command_output);
291 if (ret) {
292 ret = CMD_ERROR;
293 goto end;
294 }
295 }
296
b7f4ea0f 297 command_ret = disable_channels(session_name, channel_list);
50534d6f
JRJ
298 if (command_ret) {
299 success = 0;
300 }
301
302 /* Mi closing */
303 if (lttng_opt_mi) {
304 /* Close output element */
305 ret = mi_lttng_writer_close_element(writer);
306 if (ret) {
307 ret = CMD_ERROR;
308 goto end;
309 }
310
311 /* Success ? */
312 ret = mi_lttng_writer_write_element_bool(writer,
313 mi_lttng_element_success, success);
314 if (ret) {
315 ret = CMD_ERROR;
316 goto end;
317 }
318
319 /* Command element close */
320 ret = mi_lttng_writer_command_close(writer);
321 if (ret) {
322 ret = CMD_ERROR;
323 goto end;
324 }
325 }
26cc6b4e
DG
326
327end:
50534d6f
JRJ
328 /* Mi clean-up */
329 if (writer && mi_lttng_writer_destroy(writer)) {
330 /* Preserve original error code */
331 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
332 }
333
5853fd43
DG
334 if (!opt_session_name && session_name) {
335 free(session_name);
336 }
50534d6f 337
b7f4ea0f
MJ
338 free(channel_list);
339
50534d6f
JRJ
340 /* Overwrite ret if an error occurred in disable_channels */
341 ret = command_ret ? command_ret : ret;
342
ca1c3607 343 poptFreeContext(pc);
26cc6b4e
DG
344 return ret;
345}
This page took 0.077528 seconds and 4 git commands to generate.