Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / enable_channels.c
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
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 #include <inttypes.h>
17 #include <assert.h>
18 #include <ctype.h>
19
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/utils.h>
22 #include <common/mi-lttng.h>
23
24 #include "../command.h"
25 #include "../utils.h"
26
27
28 static struct lttng_channel chan_opts;
29 static int opt_kernel;
30 static char *opt_session_name;
31 static int opt_userspace;
32 static char *opt_output;
33 static int opt_buffer_uid;
34 static int opt_buffer_pid;
35 static int opt_buffer_global;
36 static struct {
37 bool set;
38 uint64_t interval;
39 } opt_monitor_timer;
40 static struct {
41 bool set;
42 int64_t value;
43 } opt_blocking_timeout;
44
45 static struct mi_writer *writer;
46
47 #ifdef LTTNG_EMBED_HELP
48 static const char help_msg[] =
49 #include <lttng-enable-channel.1.h>
50 ;
51 #endif
52
53 enum {
54 OPT_HELP = 1,
55 OPT_DISCARD,
56 OPT_OVERWRITE,
57 OPT_SUBBUF_SIZE,
58 OPT_NUM_SUBBUF,
59 OPT_SWITCH_TIMER,
60 OPT_MONITOR_TIMER,
61 OPT_READ_TIMER,
62 OPT_USERSPACE,
63 OPT_LIST_OPTIONS,
64 OPT_TRACEFILE_SIZE,
65 OPT_TRACEFILE_COUNT,
66 OPT_BLOCKING_TIMEOUT,
67 };
68
69 static struct lttng_handle *handle;
70
71 const char *output_mmap = "mmap";
72 const char *output_splice = "splice";
73
74 static struct poptOption long_options[] = {
75 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
76 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
77 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
78 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
79 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
80 {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0},
81 {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0},
82 {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0},
83 {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0},
84 {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0},
85 {"monitor-timer", 0, POPT_ARG_INT, 0, OPT_MONITOR_TIMER, 0, 0},
86 {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0},
87 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
88 {"output", 0, POPT_ARG_STRING, &opt_output, 0, 0, 0},
89 {"buffers-uid", 0, POPT_ARG_VAL, &opt_buffer_uid, 1, 0, 0},
90 {"buffers-pid", 0, POPT_ARG_VAL, &opt_buffer_pid, 1, 0, 0},
91 {"buffers-global", 0, POPT_ARG_VAL, &opt_buffer_global, 1, 0, 0},
92 {"tracefile-size", 'C', POPT_ARG_INT, 0, OPT_TRACEFILE_SIZE, 0, 0},
93 {"tracefile-count", 'W', POPT_ARG_INT, 0, OPT_TRACEFILE_COUNT, 0, 0},
94 {"blocking-timeout", 0, POPT_ARG_INT, 0, OPT_BLOCKING_TIMEOUT, 0, 0},
95 {0, 0, 0, 0, 0, 0, 0}
96 };
97
98 /*
99 * Set default attributes depending on those already defined from the command
100 * line.
101 */
102 static void set_default_attr(struct lttng_domain *dom)
103 {
104 struct lttng_channel_attr default_attr;
105
106 memset(&default_attr, 0, sizeof(default_attr));
107
108 /* Set attributes */
109 lttng_channel_set_default_attr(dom, &default_attr);
110
111 if (chan_opts.attr.overwrite == -1) {
112 chan_opts.attr.overwrite = default_attr.overwrite;
113 }
114 if (chan_opts.attr.subbuf_size == -1) {
115 chan_opts.attr.subbuf_size = default_attr.subbuf_size;
116 }
117 if (chan_opts.attr.num_subbuf == -1) {
118 chan_opts.attr.num_subbuf = default_attr.num_subbuf;
119 }
120 if (chan_opts.attr.switch_timer_interval == -1) {
121 chan_opts.attr.switch_timer_interval = default_attr.switch_timer_interval;
122 }
123 if (chan_opts.attr.read_timer_interval == -1) {
124 chan_opts.attr.read_timer_interval = default_attr.read_timer_interval;
125 }
126 if ((int) chan_opts.attr.output == -1) {
127 chan_opts.attr.output = default_attr.output;
128 }
129 if (chan_opts.attr.tracefile_count == -1) {
130 chan_opts.attr.tracefile_count = default_attr.tracefile_count;
131 }
132 if (chan_opts.attr.tracefile_size == -1) {
133 chan_opts.attr.tracefile_size = default_attr.tracefile_size;
134 }
135 }
136
137 /*
138 * Adding channel using the lttng API.
139 */
140 static int enable_channel(char *session_name, char *channel_list)
141 {
142 struct lttng_channel *channel = NULL;
143 int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0;
144 char *channel_name;
145 struct lttng_domain dom;
146
147 memset(&dom, 0, sizeof(dom));
148
149 /* Validate options. */
150 if (opt_kernel) {
151 if (opt_blocking_timeout.set) {
152 ERR("Retry timeout option not supported for kernel domain (-k)");
153 ret = CMD_ERROR;
154 goto error;
155 }
156 }
157
158 /* Create lttng domain */
159 if (opt_kernel) {
160 dom.type = LTTNG_DOMAIN_KERNEL;
161 dom.buf_type = LTTNG_BUFFER_GLOBAL;
162 if (opt_buffer_uid || opt_buffer_pid) {
163 ERR("Buffer type not supported for domain -k");
164 ret = CMD_ERROR;
165 goto error;
166 }
167 } else if (opt_userspace) {
168 dom.type = LTTNG_DOMAIN_UST;
169 if (opt_buffer_pid) {
170 dom.buf_type = LTTNG_BUFFER_PER_PID;
171 } else {
172 if (opt_buffer_global) {
173 ERR("Buffer type not supported for domain -u");
174 ret = CMD_ERROR;
175 goto error;
176 }
177 dom.buf_type = LTTNG_BUFFER_PER_UID;
178 }
179 } else {
180 /* Checked by the caller. */
181 assert(0);
182 }
183
184 set_default_attr(&dom);
185
186 if (chan_opts.attr.tracefile_size == 0 && chan_opts.attr.tracefile_count) {
187 ERR("Missing option --tracefile-size. "
188 "A file count without a size won't do anything.");
189 ret = CMD_ERROR;
190 goto error;
191 }
192
193 if ((chan_opts.attr.tracefile_size > 0) &&
194 (chan_opts.attr.tracefile_size < chan_opts.attr.subbuf_size)) {
195 WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")",
196 chan_opts.attr.tracefile_size, chan_opts.attr.subbuf_size);
197 chan_opts.attr.tracefile_size = chan_opts.attr.subbuf_size;
198 }
199
200 /* Setting channel output */
201 if (opt_output) {
202 if (!strncmp(output_mmap, opt_output, strlen(output_mmap))) {
203 chan_opts.attr.output = LTTNG_EVENT_MMAP;
204 } else if (!strncmp(output_splice, opt_output, strlen(output_splice))) {
205 chan_opts.attr.output = LTTNG_EVENT_SPLICE;
206 } else {
207 ERR("Unknown output type %s. Possible values are: %s, %s\n",
208 opt_output, output_mmap, output_splice);
209 ret = CMD_ERROR;
210 goto error;
211 }
212 }
213
214 handle = lttng_create_handle(session_name, &dom);
215 if (handle == NULL) {
216 ret = -1;
217 goto error;
218 }
219
220 /* Mi open channels element */
221 if (lttng_opt_mi) {
222 assert(writer);
223 ret = mi_lttng_channels_open(writer);
224 if (ret) {
225 ret = CMD_ERROR;
226 goto error;
227 }
228 }
229
230 /* Strip channel list (format: chan1,chan2,...) */
231 channel_name = strtok(channel_list, ",");
232 while (channel_name != NULL) {
233 void *extended_ptr;
234
235 /* Validate channel name's length */
236 if (strlen(channel_name) >= sizeof(chan_opts.name)) {
237 ERR("Channel name is too long (max. %zu characters)",
238 sizeof(chan_opts.name) - 1);
239 error = 1;
240 goto skip_enable;
241 }
242
243 /*
244 * A dynamically-allocated channel is used in order to allow
245 * the configuration of extended attributes (post-2.9).
246 */
247 channel = lttng_channel_create(&dom);
248 if (!channel) {
249 ERR("Unable to create channel object");
250 error = 1;
251 goto error;
252 }
253
254 /* Copy channel name */
255 strcpy(channel->name, channel_name);
256 channel->enabled = 1;
257 extended_ptr = channel->attr.extended.ptr;
258 memcpy(&channel->attr, &chan_opts.attr, sizeof(chan_opts.attr));
259 channel->attr.extended.ptr = extended_ptr;
260 if (opt_monitor_timer.set) {
261 ret = lttng_channel_set_monitor_timer_interval(channel,
262 opt_monitor_timer.interval);
263 if (ret) {
264 ERR("Failed to set the channel's monitor timer interval");
265 error = 1;
266 goto error;
267 }
268 }
269 if (opt_blocking_timeout.set) {
270 ret = lttng_channel_set_blocking_timeout(channel,
271 opt_blocking_timeout.value);
272 if (ret) {
273 ERR("Failed to set the channel's blocking timeout");
274 error = 1;
275 goto error;
276 }
277 }
278
279 DBG("Enabling channel %s", channel_name);
280
281 ret = lttng_enable_channel(handle, channel);
282 if (ret < 0) {
283 success = 0;
284 switch (-ret) {
285 case LTTNG_ERR_KERN_CHAN_EXIST:
286 case LTTNG_ERR_UST_CHAN_EXIST:
287 case LTTNG_ERR_CHAN_EXIST:
288 WARN("Channel %s: %s (session %s)", channel_name,
289 lttng_strerror(ret), session_name);
290 warn = 1;
291 break;
292 case LTTNG_ERR_INVALID_CHANNEL_NAME:
293 ERR("Invalid channel name: \"%s\". "
294 "Channel names may not start with '.', and "
295 "may not contain '/'.", channel_name);
296 error = 1;
297 break;
298 default:
299 ERR("Channel %s: %s (session %s)", channel_name,
300 lttng_strerror(ret), session_name);
301 error = 1;
302 break;
303 }
304 } else {
305 MSG("%s channel %s enabled for session %s",
306 get_domain_str(dom.type), channel_name, session_name);
307 success = 1;
308 }
309
310 skip_enable:
311 if (lttng_opt_mi) {
312 /* Mi print the channel element and leave it open */
313 ret = mi_lttng_channel(writer, channel, 1);
314 if (ret) {
315 ret = CMD_ERROR;
316 goto error;
317 }
318
319 /* Individual Success ? */
320 ret = mi_lttng_writer_write_element_bool(writer,
321 mi_lttng_element_command_success, success);
322 if (ret) {
323 ret = CMD_ERROR;
324 goto error;
325 }
326
327 /* Close channel element */
328 ret = mi_lttng_writer_close_element(writer);
329 if (ret) {
330 ret = CMD_ERROR;
331 goto error;
332 }
333 }
334
335 /* Next channel */
336 channel_name = strtok(NULL, ",");
337 lttng_channel_destroy(channel);
338 channel = NULL;
339 }
340
341 if (lttng_opt_mi) {
342 /* Close channels element */
343 ret = mi_lttng_writer_close_element(writer);
344 if (ret) {
345 ret = CMD_ERROR;
346 goto error;
347 }
348 }
349
350 ret = CMD_SUCCESS;
351
352 error:
353 if (channel) {
354 lttng_channel_destroy(channel);
355 }
356 /* If more important error happen bypass the warning */
357 if (!ret && warn) {
358 ret = CMD_WARNING;
359 }
360 /* If more important error happen bypass the warning */
361 if (!ret && error) {
362 ret = CMD_ERROR;
363 }
364
365 lttng_destroy_handle(handle);
366
367 return ret;
368 }
369
370 /*
371 * Default value for channel configuration.
372 */
373 static void init_channel_config(void)
374 {
375 /*
376 * Put -1 everywhere so we can identify those set by the command line and
377 * those needed to be set by the default values.
378 */
379 memset(&chan_opts.attr, -1, sizeof(chan_opts.attr));
380 chan_opts.attr.extended.ptr = NULL;
381 }
382
383 /*
384 * Add channel to trace session
385 */
386 int cmd_enable_channels(int argc, const char **argv)
387 {
388 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
389 static poptContext pc;
390 char *session_name = NULL;
391 char *channel_list = NULL;
392 char *opt_arg = NULL;
393 const char *arg_channel_list = NULL;
394 const char *leftover = NULL;
395
396 init_channel_config();
397
398 pc = poptGetContext(NULL, argc, argv, long_options, 0);
399 poptReadDefaultConfig(pc, 0);
400
401 while ((opt = poptGetNextOpt(pc)) != -1) {
402 switch (opt) {
403 case OPT_HELP:
404 SHOW_HELP();
405 goto end;
406 case OPT_DISCARD:
407 chan_opts.attr.overwrite = 0;
408 DBG("Channel set to discard");
409 break;
410 case OPT_OVERWRITE:
411 chan_opts.attr.overwrite = 1;
412 DBG("Channel set to overwrite");
413 break;
414 case OPT_SUBBUF_SIZE:
415 {
416 uint64_t rounded_size;
417 int order;
418
419 /* Parse the size */
420 opt_arg = poptGetOptArg(pc);
421 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.subbuf_size) < 0 || !chan_opts.attr.subbuf_size) {
422 ERR("Wrong value in --subbuf-size parameter: %s", opt_arg);
423 ret = CMD_ERROR;
424 goto end;
425 }
426
427 order = get_count_order_u64(chan_opts.attr.subbuf_size);
428 assert(order >= 0);
429 rounded_size = 1ULL << order;
430 if (rounded_size < chan_opts.attr.subbuf_size) {
431 ERR("The subbuf size (%" PRIu64 ") is rounded and overflows!",
432 chan_opts.attr.subbuf_size);
433 ret = CMD_ERROR;
434 goto end;
435 }
436
437 if (rounded_size != chan_opts.attr.subbuf_size) {
438 WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
439 chan_opts.attr.subbuf_size, rounded_size);
440 chan_opts.attr.subbuf_size = rounded_size;
441 }
442
443 /* Should now be power of 2 */
444 assert(!((chan_opts.attr.subbuf_size - 1) & chan_opts.attr.subbuf_size));
445
446 DBG("Channel subbuf size set to %" PRIu64, chan_opts.attr.subbuf_size);
447 break;
448 }
449 case OPT_NUM_SUBBUF:
450 {
451 uint64_t rounded_size;
452 int order;
453
454 errno = 0;
455 opt_arg = poptGetOptArg(pc);
456 chan_opts.attr.num_subbuf = strtoull(opt_arg, NULL, 0);
457 if (errno != 0 || !chan_opts.attr.num_subbuf || !isdigit(opt_arg[0])) {
458 ERR("Wrong value in --num-subbuf parameter: %s", opt_arg);
459 ret = CMD_ERROR;
460 goto end;
461 }
462
463 order = get_count_order_u64(chan_opts.attr.num_subbuf);
464 assert(order >= 0);
465 rounded_size = 1ULL << order;
466 if (rounded_size < chan_opts.attr.num_subbuf) {
467 ERR("The number of subbuffers (%" PRIu64 ") is rounded and overflows!",
468 chan_opts.attr.num_subbuf);
469 ret = CMD_ERROR;
470 goto end;
471 }
472
473 if (rounded_size != chan_opts.attr.num_subbuf) {
474 WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")",
475 chan_opts.attr.num_subbuf, rounded_size);
476 chan_opts.attr.num_subbuf = rounded_size;
477 }
478
479 /* Should now be power of 2 */
480 assert(!((chan_opts.attr.num_subbuf - 1) & chan_opts.attr.num_subbuf));
481
482 DBG("Channel subbuf num set to %" PRIu64, chan_opts.attr.num_subbuf);
483 break;
484 }
485 case OPT_SWITCH_TIMER:
486 {
487 uint64_t v;
488
489 errno = 0;
490 opt_arg = poptGetOptArg(pc);
491
492 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
493 ERR("Wrong value for --switch-timer parameter: %s", opt_arg);
494 ret = CMD_ERROR;
495 goto end;
496 }
497
498 if (v != (uint32_t) v) {
499 ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg);
500 ret = CMD_ERROR;
501 goto end;
502 }
503 chan_opts.attr.switch_timer_interval = (uint32_t) v;
504 DBG("Channel switch timer interval set to %d %s",
505 chan_opts.attr.switch_timer_interval,
506 USEC_UNIT);
507 break;
508 }
509 case OPT_READ_TIMER:
510 {
511 uint64_t v;
512
513 errno = 0;
514 opt_arg = poptGetOptArg(pc);
515
516 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
517 ERR("Wrong value for --read-timer parameter: %s", opt_arg);
518 ret = CMD_ERROR;
519 goto end;
520 }
521
522 if (v != (uint32_t) v) {
523 ERR("32-bit overflow in --read-timer parameter: %s", opt_arg);
524 ret = CMD_ERROR;
525 goto end;
526 }
527 chan_opts.attr.read_timer_interval = (uint32_t) v;
528 DBG("Channel read timer interval set to %d %s",
529 chan_opts.attr.read_timer_interval,
530 USEC_UNIT);
531 break;
532 }
533 case OPT_MONITOR_TIMER:
534 {
535 uint64_t v;
536
537 errno = 0;
538 opt_arg = poptGetOptArg(pc);
539
540 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
541 ERR("Wrong value for --monitor-timer parameter: %s", opt_arg);
542 ret = CMD_ERROR;
543 goto end;
544 }
545 opt_monitor_timer.interval = (uint64_t) v;
546 opt_monitor_timer.set = true;
547 DBG("Channel monitor timer interval set to %" PRIu64 " %s",
548 opt_monitor_timer.interval,
549 USEC_UNIT);
550 break;
551 }
552 case OPT_BLOCKING_TIMEOUT:
553 {
554 uint64_t v;
555 long long v_msec;
556
557 errno = 0;
558 opt_arg = poptGetOptArg(pc);
559
560 if (strcmp(opt_arg, "inf") == 0) {
561 opt_blocking_timeout.value = (int64_t) -1;
562 opt_blocking_timeout.set = true;
563 DBG("Channel blocking timeout set to infinity");
564 break;
565 }
566
567 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
568 ERR("Wrong value for --blocking-timeout parameter: %s", opt_arg);
569 ret = CMD_ERROR;
570 goto end;
571 }
572
573 /*
574 * While LTTng-UST and LTTng-tools will accept a
575 * blocking timeout expressed in µs, the current
576 * tracer implementation relies on poll() which
577 * takes an "int timeout" parameter expressed in
578 * msec.
579 *
580 * Since the error reporting from the tracer is
581 * not precise, we perform this check here to
582 * provide a helpful error message in case of
583 * overflow.
584 *
585 * The setter (liblttng-ctl) also performs an
586 * equivalent check.
587 */
588 v_msec = v / 1000;
589 if (v_msec != (int32_t) v_msec) {
590 ERR("32-bit milliseconds overflow in --blocking-timeout parameter: %s", opt_arg);
591 ret = CMD_ERROR;
592 goto end;
593 }
594
595 opt_blocking_timeout.value = (int64_t) v;
596 opt_blocking_timeout.set = true;
597 DBG("Channel blocking timeout set to %" PRId64 " %s%s",
598 opt_blocking_timeout.value,
599 USEC_UNIT,
600 opt_blocking_timeout.value == 0 ?
601 " (non-blocking)" : "");
602 break;
603 }
604 case OPT_USERSPACE:
605 opt_userspace = 1;
606 break;
607 case OPT_TRACEFILE_SIZE:
608 opt_arg = poptGetOptArg(pc);
609 if (utils_parse_size_suffix(opt_arg, &chan_opts.attr.tracefile_size) < 0) {
610 ERR("Wrong value in --tracefile-size parameter: %s", opt_arg);
611 ret = CMD_ERROR;
612 goto end;
613 }
614 DBG("Maximum tracefile size set to %" PRIu64,
615 chan_opts.attr.tracefile_size);
616 break;
617 case OPT_TRACEFILE_COUNT:
618 {
619 unsigned long v;
620
621 errno = 0;
622 opt_arg = poptGetOptArg(pc);
623 v = strtoul(opt_arg, NULL, 0);
624 if (errno != 0 || !isdigit(opt_arg[0])) {
625 ERR("Wrong value in --tracefile-count parameter: %s", opt_arg);
626 ret = CMD_ERROR;
627 goto end;
628 }
629 if (v != (uint32_t) v) {
630 ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg);
631 ret = CMD_ERROR;
632 goto end;
633 }
634 chan_opts.attr.tracefile_count = (uint32_t) v;
635 DBG("Maximum tracefile count set to %" PRIu64,
636 chan_opts.attr.tracefile_count);
637 break;
638 }
639 case OPT_LIST_OPTIONS:
640 list_cmd_options(stdout, long_options);
641 goto end;
642 default:
643 ret = CMD_UNDEFINED;
644 goto end;
645 }
646
647 if (opt_arg) {
648 free(opt_arg);
649 opt_arg = NULL;
650 }
651 }
652
653 ret = print_missing_or_multiple_domains(
654 opt_kernel + opt_userspace, false);
655 if (ret) {
656 ret = CMD_ERROR;
657 goto end;
658 }
659
660 if (chan_opts.attr.overwrite == 1 && opt_blocking_timeout.set &&
661 opt_blocking_timeout.value != 0) {
662 ERR("You cannot specify --overwrite and --blocking-timeout=N, "
663 "where N is different than 0");
664 ret = CMD_ERROR;
665 goto end;
666 }
667
668 /* Mi check */
669 if (lttng_opt_mi) {
670 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
671 if (!writer) {
672 ret = -LTTNG_ERR_NOMEM;
673 goto end;
674 }
675
676 /* Open command element */
677 ret = mi_lttng_writer_command_open(writer,
678 mi_lttng_element_command_enable_channels);
679 if (ret) {
680 ret = CMD_ERROR;
681 goto end;
682 }
683
684 /* Open output element */
685 ret = mi_lttng_writer_open_element(writer,
686 mi_lttng_element_command_output);
687 if (ret) {
688 ret = CMD_ERROR;
689 goto end;
690 }
691 }
692
693 arg_channel_list = poptGetArg(pc);
694 if (arg_channel_list == NULL) {
695 ERR("Missing channel name.");
696 ret = CMD_ERROR;
697 success = 0;
698 goto mi_closing;
699 }
700
701 channel_list = strdup(arg_channel_list);
702 if (channel_list == NULL) {
703 PERROR("Failed to copy channel name");
704 ret = CMD_ERROR;
705 success = 0;
706 goto mi_closing;
707 }
708
709 leftover = poptGetArg(pc);
710 if (leftover) {
711 ERR("Unknown argument: %s", leftover);
712 ret = CMD_ERROR;
713 success = 0;
714 goto mi_closing;
715 }
716
717 if (!opt_session_name) {
718 session_name = get_session_name();
719 if (session_name == NULL) {
720 command_ret = CMD_ERROR;
721 success = 0;
722 goto mi_closing;
723 }
724 } else {
725 session_name = opt_session_name;
726 }
727
728 command_ret = enable_channel(session_name, channel_list);
729 if (command_ret) {
730 success = 0;
731 }
732
733 mi_closing:
734 /* Mi closing */
735 if (lttng_opt_mi) {
736 /* Close output element */
737 ret = mi_lttng_writer_close_element(writer);
738 if (ret) {
739 goto end;
740 }
741
742 /* Success ? */
743 ret = mi_lttng_writer_write_element_bool(writer,
744 mi_lttng_element_command_success, success);
745 if (ret) {
746 goto end;
747 }
748
749 /* Command element close */
750 ret = mi_lttng_writer_command_close(writer);
751 if (ret) {
752 goto end;
753 }
754 }
755
756 end:
757 /* Mi clean-up */
758 if (writer && mi_lttng_writer_destroy(writer)) {
759 /* Preserve original error code */
760 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
761 }
762
763 if (!opt_session_name && session_name) {
764 free(session_name);
765 }
766
767 free(channel_list);
768
769 /* Overwrite ret if an error occurred when enable_channel */
770 ret = command_ret ? command_ret : ret;
771 poptFreeContext(pc);
772 free(opt_arg);
773 return ret;
774 }
This page took 0.072356 seconds and 4 git commands to generate.