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