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