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