Refactoring: use an opaque lttng_tracker_id type
[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;
68c7f6e5 219 const char *leftover = NULL;
26cc6b4e
DG
220
221 pc = poptGetContext(NULL, argc, argv, long_options, 0);
222 poptReadDefaultConfig(pc, 0);
223
224 while ((opt = poptGetNextOpt(pc)) != -1) {
225 switch (opt) {
226 case OPT_HELP:
4ba92f18 227 SHOW_HELP();
26cc6b4e
DG
228 goto end;
229 case OPT_USERSPACE:
230 opt_userspace = 1;
231 break;
679b4943
SM
232 case OPT_LIST_OPTIONS:
233 list_cmd_options(stdout, long_options);
679b4943 234 goto end;
26cc6b4e 235 default:
26cc6b4e
DG
236 ret = CMD_UNDEFINED;
237 goto end;
238 }
239 }
240
3ecec76a
PP
241 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
242 if (ret) {
243 ret = CMD_ERROR;
244 goto end;
245 }
246
26cc6b4e
DG
247 opt_channels = (char*) poptGetArg(pc);
248 if (opt_channels == NULL) {
249 ERR("Missing channel name(s).\n");
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
296 command_ret = disable_channels(session_name);
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
JRJ
336
337 /* Overwrite ret if an error occurred in disable_channels */
338 ret = command_ret ? command_ret : ret;
339
ca1c3607 340 poptFreeContext(pc);
26cc6b4e
DG
341 return ret;
342}
This page took 0.066062 seconds and 4 git commands to generate.