Typo: occured -> occurred
[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 _LGPL_SOURCE
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>
26 #include <assert.h>
27
28 #include <common/mi-lttng.h>
29
30 #include "../command.h"
31
32 static char *opt_channels;
33 static int opt_kernel;
34 static char *opt_session_name;
35 static int opt_userspace;
36
37 enum {
38 OPT_HELP = 1,
39 OPT_USERSPACE,
40 OPT_LIST_OPTIONS,
41 };
42
43 static struct lttng_handle *handle;
44 static struct mi_writer *writer;
45
46 static struct poptOption long_options[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
49 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
50 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
51 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
52 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
53 {0, 0, 0, 0, 0, 0, 0}
54 };
55
56 static 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,
86 mi_lttng_element_success, success);
87 if (ret) {
88 goto end;
89 }
90
91 /* Closing channel element */
92 ret = mi_lttng_writer_close_element(writer);
93
94 end:
95 return ret;
96 }
97
98 /*
99 * Disabling channel using the lttng API.
100 */
101 static int disable_channels(char *session_name)
102 {
103 int ret = CMD_SUCCESS, warn = 0, success;
104
105 /* Normal case for disable channed is enabled = 0 */
106 unsigned int enabled = 0;
107 char *channel_name;
108 struct lttng_domain dom;
109
110 memset(&dom, 0, sizeof(dom));
111
112 /* Create lttng domain */
113 if (opt_kernel) {
114 dom.type = LTTNG_DOMAIN_KERNEL;
115 } else if (opt_userspace) {
116 dom.type = LTTNG_DOMAIN_UST;
117 } else {
118 /* Checked by the caller. */
119 assert(0);
120 }
121
122 handle = lttng_create_handle(session_name, &dom);
123 if (handle == NULL) {
124 ret = -1;
125 goto error;
126 }
127
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
139 /* Strip channel list */
140 channel_name = strtok(opt_channels, ",");
141 while (channel_name != NULL) {
142 DBG("Disabling channel %s", channel_name);
143
144 ret = lttng_disable_channel(handle, channel_name);
145 if (ret < 0) {
146 ERR("Channel %s: %s (session %s)", channel_name,
147 lttng_strerror(ret), session_name);
148 warn = 1;
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.
154 * The client should look at the stderr stream
155 * for more informations.
156 */
157 enabled = 1;
158 success = 0;
159
160 } else {
161 MSG("%s channel %s disabled for session %s",
162 get_domain_str(dom.type), channel_name, session_name);
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 }
174 }
175
176 /* Next channel */
177 channel_name = strtok(NULL, ",");
178 }
179
180 ret = CMD_SUCCESS;
181
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
192 error:
193 /* Bypass the warning if a more important error happened */
194 if (!ret && warn) {
195 ret = CMD_WARNING;
196 }
197
198 lttng_destroy_handle(handle);
199
200 return ret;
201 }
202
203 /*
204 * cmd_disable_channels
205 *
206 * Disable channel to trace session
207 */
208 int cmd_disable_channels(int argc, const char **argv)
209 {
210 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
211 static poptContext pc;
212 char *session_name = NULL;
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:
220 SHOW_HELP();
221 goto end;
222 case OPT_USERSPACE:
223 opt_userspace = 1;
224 break;
225 case OPT_LIST_OPTIONS:
226 list_cmd_options(stdout, long_options);
227 goto end;
228 default:
229 ret = CMD_UNDEFINED;
230 goto end;
231 }
232 }
233
234 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
235 if (ret) {
236 ret = CMD_ERROR;
237 goto end;
238 }
239
240 opt_channels = (char*) poptGetArg(pc);
241 if (opt_channels == NULL) {
242 ERR("Missing channel name(s).\n");
243 ret = CMD_ERROR;
244 goto end;
245 }
246
247 if (!opt_session_name) {
248 session_name = get_session_name();
249 if (session_name == NULL) {
250 ret = CMD_ERROR;
251 goto end;
252 }
253 } else {
254 session_name = opt_session_name;
255 }
256
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 }
311
312 end:
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
319 if (!opt_session_name && session_name) {
320 free(session_name);
321 }
322
323 /* Overwrite ret if an error occurred in disable_channels */
324 ret = command_ret ? command_ret : ret;
325
326 poptFreeContext(pc);
327 return ret;
328 }
This page took 0.034916 seconds and 4 git commands to generate.