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