Fix: nonsensical message printed by lttng track/untrack
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.c
CommitLineData
26cc6b4e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
26cc6b4e
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26cc6b4e
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
26cc6b4e
DG
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
50534d6f
JRJ
26#include <assert.h>
27
28#include <common/mi-lttng.h>
26cc6b4e 29
c399183f 30#include "../command.h"
26cc6b4e
DG
31
32static char *opt_channels;
e14f64a8 33static int opt_kernel;
5440dc42 34static char *opt_session_name;
26cc6b4e 35static int opt_userspace;
26cc6b4e 36
4fc83d94
PP
37#ifdef LTTNG_EMBED_HELP
38static const char help_msg[] =
39#include <lttng-disable-channel.1.h>
40;
41#endif
42
26cc6b4e
DG
43enum {
44 OPT_HELP = 1,
45 OPT_USERSPACE,
679b4943 46 OPT_LIST_OPTIONS,
26cc6b4e
DG
47};
48
cd80958d 49static struct lttng_handle *handle;
50534d6f 50static struct mi_writer *writer;
cd80958d 51
26cc6b4e
DG
52static struct poptOption long_options[] = {
53 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
54 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 55 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
26cc6b4e 56 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 57 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
679b4943 58 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
26cc6b4e
DG
59 {0, 0, 0, 0, 0, 0, 0}
60};
61
50534d6f
JRJ
62static int mi_partial_channel_print(char *channel_name, unsigned int enabled,
63 int success)
64{
65 int ret;
66
67 assert(writer);
68 assert(channel_name);
69
70 /* Open channel element */
71 ret = mi_lttng_writer_open_element(writer, config_element_channel);
72 if (ret) {
73 goto end;
74 }
75
76 /* Name */
77 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
78 channel_name);
79 if (ret) {
80 goto end;
81 }
82
83 /* Enabled ? */
84 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
85 enabled);
86 if (ret) {
87 goto end;
88 }
89
90 /* Success ? */
91 ret = mi_lttng_writer_write_element_bool(writer,
9618049b 92 mi_lttng_element_success, success);
50534d6f
JRJ
93 if (ret) {
94 goto end;
95 }
96
97 /* Closing channel element */
98 ret = mi_lttng_writer_close_element(writer);
99
100end:
101 return ret;
102}
103
26cc6b4e 104/*
cd80958d 105 * Disabling channel using the lttng API.
26cc6b4e 106 */
cd80958d 107static int disable_channels(char *session_name)
26cc6b4e 108{
50534d6f
JRJ
109 int ret = CMD_SUCCESS, warn = 0, success;
110
111 /* Normal case for disable channed is enabled = 0 */
112 unsigned int enabled = 0;
26cc6b4e 113 char *channel_name;
7d29a247 114 struct lttng_domain dom;
26cc6b4e 115
441c16a7
MD
116 memset(&dom, 0, sizeof(dom));
117
d78d6610 118 /* Create lttng domain */
7d29a247
DG
119 if (opt_kernel) {
120 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 121 } else if (opt_userspace) {
78f0bacd 122 dom.type = LTTNG_DOMAIN_UST;
78f0bacd 123 } else {
3ecec76a
PP
124 /* Checked by the caller. */
125 assert(0);
7d29a247
DG
126 }
127
cd80958d
DG
128 handle = lttng_create_handle(session_name, &dom);
129 if (handle == NULL) {
130 ret = -1;
131 goto error;
132 }
133
50534d6f
JRJ
134 /* Prepare MI */
135 if (lttng_opt_mi) {
136 /* open a channels element */
137 ret = mi_lttng_writer_open_element(writer, config_element_channels);
138 if (ret) {
139 ret = CMD_ERROR;
140 goto error;
141 }
142
143 }
144
26cc6b4e
DG
145 /* Strip channel list */
146 channel_name = strtok(opt_channels, ",");
147 while (channel_name != NULL) {
78f0bacd
DG
148 DBG("Disabling channel %s", channel_name);
149
150 ret = lttng_disable_channel(handle, channel_name);
151 if (ret < 0) {
ae856491
DG
152 ERR("Channel %s: %s (session %s)", channel_name,
153 lttng_strerror(ret), session_name);
154 warn = 1;
50534d6f
JRJ
155
156 /*
157 * Mi:
158 * We assume that if an error occurred the channel is still active.
159 * This might not be the case but is a good assumption.
9618049b
JRJ
160 * The client should look at the stderr stream
161 * for more informations.
50534d6f
JRJ
162 */
163 enabled = 1;
164 success = 0;
165
26cc6b4e 166 } else {
7885e399 167 MSG("%s channel %s disabled for session %s",
b9dfb167 168 get_domain_str(dom.type), channel_name, session_name);
50534d6f
JRJ
169 enabled = 0;
170 success = 1;
171 }
172
173 /* Print the channel */
174 if (lttng_opt_mi) {
175 ret = mi_partial_channel_print(channel_name, enabled, success);
176 if (ret) {
177 ret = CMD_ERROR;
178 goto error;
179 }
26cc6b4e
DG
180 }
181
182 /* Next channel */
183 channel_name = strtok(NULL, ",");
184 }
185
ae856491
DG
186 ret = CMD_SUCCESS;
187
50534d6f
JRJ
188 /* Close Mi */
189 if (lttng_opt_mi) {
190 /* Close channels element */
191 ret = mi_lttng_writer_close_element(writer);
192 if (ret) {
193 ret = CMD_ERROR;
194 goto error;
195 }
196 }
197
26cc6b4e 198error:
50534d6f
JRJ
199 /* Bypass the warning if a more important error happened */
200 if (!ret && warn) {
ae856491
DG
201 ret = CMD_WARNING;
202 }
203
cd80958d
DG
204 lttng_destroy_handle(handle);
205
26cc6b4e
DG
206 return ret;
207}
208
209/*
210 * cmd_disable_channels
211 *
212 * Disable channel to trace session
213 */
214int cmd_disable_channels(int argc, const char **argv)
215{
50534d6f 216 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
26cc6b4e 217 static poptContext pc;
cd80958d 218 char *session_name = NULL;
26cc6b4e
DG
219
220 pc = poptGetContext(NULL, argc, argv, long_options, 0);
221 poptReadDefaultConfig(pc, 0);
222
223 while ((opt = poptGetNextOpt(pc)) != -1) {
224 switch (opt) {
225 case OPT_HELP:
4ba92f18 226 SHOW_HELP();
26cc6b4e
DG
227 goto end;
228 case OPT_USERSPACE:
229 opt_userspace = 1;
230 break;
679b4943
SM
231 case OPT_LIST_OPTIONS:
232 list_cmd_options(stdout, long_options);
679b4943 233 goto end;
26cc6b4e 234 default:
26cc6b4e
DG
235 ret = CMD_UNDEFINED;
236 goto end;
237 }
238 }
239
3ecec76a
PP
240 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
241 if (ret) {
242 ret = CMD_ERROR;
243 goto end;
244 }
245
26cc6b4e
DG
246 opt_channels = (char*) poptGetArg(pc);
247 if (opt_channels == NULL) {
248 ERR("Missing channel name(s).\n");
ca1c3607 249 ret = CMD_ERROR;
26cc6b4e
DG
250 goto end;
251 }
252
cd80958d
DG
253 if (!opt_session_name) {
254 session_name = get_session_name();
255 if (session_name == NULL) {
ca1c3607 256 ret = CMD_ERROR;
cd80958d
DG
257 goto end;
258 }
259 } else {
260 session_name = opt_session_name;
261 }
262
50534d6f
JRJ
263 /* Mi check */
264 if (lttng_opt_mi) {
265 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
266 if (!writer) {
267 ret = -LTTNG_ERR_NOMEM;
268 goto end;
269 }
270
271 /* Open command element */
272 ret = mi_lttng_writer_command_open(writer,
273 mi_lttng_element_command_disable_channel);
274 if (ret) {
275 ret = CMD_ERROR;
276 goto end;
277 }
278
279 /* Open output element */
280 ret = mi_lttng_writer_open_element(writer,
281 mi_lttng_element_command_output);
282 if (ret) {
283 ret = CMD_ERROR;
284 goto end;
285 }
286 }
287
288 command_ret = disable_channels(session_name);
289 if (command_ret) {
290 success = 0;
291 }
292
293 /* Mi closing */
294 if (lttng_opt_mi) {
295 /* Close output element */
296 ret = mi_lttng_writer_close_element(writer);
297 if (ret) {
298 ret = CMD_ERROR;
299 goto end;
300 }
301
302 /* Success ? */
303 ret = mi_lttng_writer_write_element_bool(writer,
304 mi_lttng_element_success, success);
305 if (ret) {
306 ret = CMD_ERROR;
307 goto end;
308 }
309
310 /* Command element close */
311 ret = mi_lttng_writer_command_close(writer);
312 if (ret) {
313 ret = CMD_ERROR;
314 goto end;
315 }
316 }
26cc6b4e
DG
317
318end:
50534d6f
JRJ
319 /* Mi clean-up */
320 if (writer && mi_lttng_writer_destroy(writer)) {
321 /* Preserve original error code */
322 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
323 }
324
5853fd43
DG
325 if (!opt_session_name && session_name) {
326 free(session_name);
327 }
50534d6f
JRJ
328
329 /* Overwrite ret if an error occurred in disable_channels */
330 ret = command_ret ? command_ret : ret;
331
ca1c3607 332 poptFreeContext(pc);
26cc6b4e
DG
333 return ret;
334}
This page took 0.060926 seconds and 4 git commands to generate.