Remove dead code from disable-channel command
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
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 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #define _LGPL_SOURCE
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>
27 #include <assert.h>
28
29 #include <common/mi-lttng.h>
30
31 #include "../command.h"
32
33 static char *opt_channels;
34 static int opt_kernel;
35 static char *opt_session_name;
36 static int opt_userspace;
37
38 enum {
39 OPT_HELP = 1,
40 OPT_USERSPACE,
41 OPT_LIST_OPTIONS,
42 };
43
44 static struct lttng_handle *handle;
45 static struct mi_writer *writer;
46
47 static struct poptOption long_options[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
49 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
50 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
51 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
52 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
53 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
54 {0, 0, 0, 0, 0, 0, 0}
55 };
56
57 /*
58 * usage
59 */
60 static void usage(FILE *ofp)
61 {
62 fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] (-k | -u) [OPTIONS]\n");
63 fprintf(ofp, "\n");
64 fprintf(ofp, "Options:\n");
65 fprintf(ofp, " -h, --help Show this help\n");
66 fprintf(ofp, " --list-options Simple listing of options\n");
67 fprintf(ofp, " -s, --session NAME Apply to session name\n");
68 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
69 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
70 fprintf(ofp, "\n");
71 }
72
73 static 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,
103 mi_lttng_element_success, success);
104 if (ret) {
105 goto end;
106 }
107
108 /* Closing channel element */
109 ret = mi_lttng_writer_close_element(writer);
110
111 end:
112 return ret;
113 }
114
115 /*
116 * Disabling channel using the lttng API.
117 */
118 static int disable_channels(char *session_name)
119 {
120 int ret = CMD_SUCCESS, warn = 0, success;
121
122 /* Normal case for disable channed is enabled = 0 */
123 unsigned int enabled = 0;
124 char *channel_name;
125 struct lttng_domain dom;
126
127 memset(&dom, 0, sizeof(dom));
128
129 /* Create lttng domain */
130 if (opt_kernel) {
131 dom.type = LTTNG_DOMAIN_KERNEL;
132 } else if (opt_userspace) {
133 dom.type = LTTNG_DOMAIN_UST;
134 } else {
135 /* Checked by the caller. */
136 assert(0);
137 }
138
139 handle = lttng_create_handle(session_name, &dom);
140 if (handle == NULL) {
141 ret = -1;
142 goto error;
143 }
144
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
156 /* Strip channel list */
157 channel_name = strtok(opt_channels, ",");
158 while (channel_name != NULL) {
159 DBG("Disabling channel %s", channel_name);
160
161 ret = lttng_disable_channel(handle, channel_name);
162 if (ret < 0) {
163 ERR("Channel %s: %s (session %s)", channel_name,
164 lttng_strerror(ret), session_name);
165 warn = 1;
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.
171 * The client should look at the stderr stream
172 * for more informations.
173 */
174 enabled = 1;
175 success = 0;
176
177 } else {
178 MSG("%s channel %s disabled for session %s",
179 get_domain_str(dom.type), channel_name, session_name);
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 }
191 }
192
193 /* Next channel */
194 channel_name = strtok(NULL, ",");
195 }
196
197 ret = CMD_SUCCESS;
198
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
209 error:
210 /* Bypass the warning if a more important error happened */
211 if (!ret && warn) {
212 ret = CMD_WARNING;
213 }
214
215 lttng_destroy_handle(handle);
216
217 return ret;
218 }
219
220 /*
221 * cmd_disable_channels
222 *
223 * Disable channel to trace session
224 */
225 int cmd_disable_channels(int argc, const char **argv)
226 {
227 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
228 static poptContext pc;
229 char *session_name = NULL;
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:
237 usage(stdout);
238 goto end;
239 case OPT_USERSPACE:
240 opt_userspace = 1;
241 break;
242 case OPT_LIST_OPTIONS:
243 list_cmd_options(stdout, long_options);
244 goto end;
245 default:
246 usage(stderr);
247 ret = CMD_UNDEFINED;
248 goto end;
249 }
250 }
251
252 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
253 if (ret) {
254 ret = CMD_ERROR;
255 goto end;
256 }
257
258 opt_channels = (char*) poptGetArg(pc);
259 if (opt_channels == NULL) {
260 ERR("Missing channel name(s).\n");
261 usage(stderr);
262 ret = CMD_ERROR;
263 goto end;
264 }
265
266 if (!opt_session_name) {
267 session_name = get_session_name();
268 if (session_name == NULL) {
269 ret = CMD_ERROR;
270 goto end;
271 }
272 } else {
273 session_name = opt_session_name;
274 }
275
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 }
330
331 end:
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
338 if (!opt_session_name && session_name) {
339 free(session_name);
340 }
341
342 /* Overwrite ret if an error occurred in disable_channels */
343 ret = command_ret ? command_ret : ret;
344
345 poptFreeContext(pc);
346 return ret;
347 }
This page took 0.054387 seconds and 4 git commands to generate.