68a1020a892ef53cdb8379fc5266fcccc8b60b2a
[deliverable/lttng-tools.git] / src / bin / lttng / commands / create.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include <common/compat/time.hpp>
11 #include <ctype.h>
12 #include <functional>
13 #include <popt.h>
14 #include <signal.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 #include <unordered_map>
23
24 #include <common/mi-lttng.hpp>
25
26 #include "../command.hpp"
27 #include "../utils.hpp"
28
29 #include <common/defaults.hpp>
30 #include <common/sessiond-comm/sessiond-comm.hpp>
31 #include <common/uri.hpp>
32 #include <common/utils.hpp>
33 #include <common/path.hpp>
34 #include <lttng/lttng.h>
35
36 static char *opt_output_path;
37 static char *opt_session_name;
38 static char *opt_url;
39 static char *opt_ctrl_url;
40 static char *opt_data_url;
41 static char *opt_shm_path;
42 static char *opt_trace_format;
43 static int opt_no_consumer;
44 static int opt_no_output;
45 static int opt_snapshot;
46 static uint32_t opt_live_timer;
47
48 #ifdef LTTNG_EMBED_HELP
49 static const char help_msg[] =
50 #include <lttng-create.1.h>
51 ;
52 #endif
53
54 enum {
55 OPT_HELP = 1,
56 OPT_LIST_OPTIONS,
57 OPT_LIVE_TIMER,
58 };
59
60 enum output_type {
61 OUTPUT_NONE,
62 OUTPUT_LOCAL,
63 OUTPUT_NETWORK,
64 OUTPUT_UNSPECIFIED,
65 };
66
67 static struct mi_writer *writer;
68 static struct poptOption long_options[] = {
69 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
70 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
71 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
72 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
73 {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0},
74 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
75 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
76 {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0},
77 {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0},
78 {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0},
79 {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0},
80 {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0},
81 {"trace-format", 0, POPT_ARG_STRING, &opt_trace_format, 0, 0, 0},
82 {0, 0, 0, 0, 0, 0, 0}
83 };
84
85 /*
86 * Retrieve the created session and mi output it based on provided argument
87 * This is currently a summary of what was pretty printed and is subject to
88 * enhancements.
89 */
90 static int mi_created_session(const char *session_name)
91 {
92 int ret, i, count, found;
93 struct lttng_session *sessions;
94
95 /* session_name should not be null */
96 LTTNG_ASSERT(session_name);
97 LTTNG_ASSERT(writer);
98
99 count = lttng_list_sessions(&sessions);
100 if (count < 0) {
101 ret = count;
102 ERR("%s", lttng_strerror(ret));
103 goto error;
104 }
105
106 if (count == 0) {
107 ERR("Error session creation failed: session %s not found", session_name);
108 ret = -LTTNG_ERR_SESS_NOT_FOUND;
109 goto end;
110 }
111
112 found = 0;
113 for (i = 0; i < count; i++) {
114 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
115 found = 1;
116 ret = mi_lttng_session(writer, &sessions[i], 0);
117 if (ret) {
118 goto error;
119 }
120 break;
121 }
122 }
123
124 if (!found) {
125 ret = -LTTNG_ERR_SESS_NOT_FOUND;
126 } else {
127 ret = CMD_SUCCESS;
128 }
129
130 error:
131 free(sessions);
132 end:
133 return ret;
134 }
135
136 static
137 struct lttng_session_descriptor *create_session_descriptor(void)
138 {
139 ssize_t uri_count;
140 enum output_type output_type;
141 struct lttng_uri *uris = NULL;
142 struct lttng_session_descriptor *descriptor = NULL;
143 const char *uri_str1 = NULL, *uri_str2 = NULL;
144 char local_output_path[LTTNG_PATH_MAX] = {};
145
146 if (opt_no_output) {
147 output_type = OUTPUT_NONE;
148 } else if (opt_output_path) {
149 char *expanded_output_path;
150 int ret;
151
152 output_type = OUTPUT_LOCAL;
153 expanded_output_path = utils_expand_path(opt_output_path);
154 if (!expanded_output_path) {
155 ERR("Failed to expand output path.");
156 goto end;
157 }
158 ret = lttng_strncpy(local_output_path, expanded_output_path,
159 sizeof(local_output_path));
160 free(expanded_output_path);
161 if (ret) {
162 ERR("Output path exceeds the maximal supported length (%zu bytes)",
163 sizeof(local_output_path));
164 goto end;
165 }
166 } else if (opt_url || opt_ctrl_url) {
167 int ret;
168
169 uri_str1 = opt_ctrl_url ? opt_ctrl_url : opt_url;
170 uri_str2 = opt_data_url;
171
172 uri_count = uri_parse_str_urls(uri_str1, uri_str2, &uris);
173 if (uri_count != 1 && uri_count != 2) {
174 ERR("Unrecognized URL format.");
175 goto end;
176 }
177
178 switch (uri_count) {
179 case 1:
180 output_type = OUTPUT_LOCAL;
181 if (uris[0].dtype != LTTNG_DST_PATH) {
182 ERR("Unrecognized URL format.");
183 goto end;
184 }
185 ret = lttng_strncpy(local_output_path, uris[0].dst.path,
186 sizeof(local_output_path));
187 if (ret) {
188 ERR("Output path exceeds the maximal supported length (%zu bytes)",
189 sizeof(local_output_path));
190 }
191 break;
192 case 2:
193 output_type = OUTPUT_NETWORK;
194 break;
195 default:
196 /* Already checked. */
197 abort();
198 }
199 } else {
200 output_type = OUTPUT_UNSPECIFIED;
201 }
202
203 if (opt_snapshot) {
204 /* Snapshot session. */
205 switch (output_type) {
206 case OUTPUT_UNSPECIFIED:
207 case OUTPUT_LOCAL:
208 descriptor = lttng_session_descriptor_snapshot_local_create(
209 opt_session_name,
210 output_type == OUTPUT_LOCAL ?
211 local_output_path : NULL);
212 break;
213 case OUTPUT_NONE:
214 descriptor = lttng_session_descriptor_snapshot_create(
215 opt_session_name);
216 break;
217 case OUTPUT_NETWORK:
218 descriptor = lttng_session_descriptor_snapshot_network_create(
219 opt_session_name, uri_str1, uri_str2);
220 break;
221 default:
222 abort();
223 }
224 } else if (opt_live_timer) {
225 /* Live session. */
226 if (output_type != OUTPUT_UNSPECIFIED &&
227 output_type != OUTPUT_NETWORK) {
228 ERR("Unsupported output type specified for live session.");
229 goto end;
230 }
231 descriptor = lttng_session_descriptor_live_network_create(
232 opt_session_name, uri_str1, uri_str2,
233 opt_live_timer);
234 } else {
235 /* Regular session. */
236 switch (output_type) {
237 case OUTPUT_UNSPECIFIED:
238 case OUTPUT_LOCAL:
239 descriptor = lttng_session_descriptor_local_create(
240 opt_session_name,
241 output_type == OUTPUT_LOCAL ?
242 local_output_path : NULL);
243 break;
244 case OUTPUT_NONE:
245 descriptor = lttng_session_descriptor_create(
246 opt_session_name);
247 break;
248 case OUTPUT_NETWORK:
249 descriptor = lttng_session_descriptor_network_create(
250 opt_session_name, uri_str1, uri_str2);
251 break;
252 default:
253 abort();
254 }
255 }
256 if (!descriptor) {
257 ERR("Failed to initialize session creation command.");
258 } else {
259 /*
260 * Auto-launch the relay daemon when a live session
261 * is created using default URLs.
262 */
263 if (!opt_url && !opt_ctrl_url && !opt_data_url &&
264 opt_live_timer && !check_relayd()) {
265 int ret;
266 const char *pathname = opt_relayd_path ? :
267 INSTALL_BIN_PATH "/lttng-relayd";
268
269 ret = spawn_relayd(pathname, 0);
270 if (ret < 0) {
271 goto error;
272 }
273 }
274 }
275
276 if (opt_trace_format) {
277 std::unordered_map<std::string,
278 std::function<struct lttng_trace_format_descriptor *()>>
279 factory = {
280 {"ctf1", lttng_trace_format_ctf_1_descriptor_create},
281 {"ctf2", lttng_trace_format_ctf_2_descriptor_create},
282 };
283
284 lttng_session_descriptor_status status;
285 lttng_trace_format_descriptor *trace_format_descriptor = nullptr;
286 std::string s_trace_format(opt_trace_format);
287
288 auto it = factory.find(s_trace_format);
289
290 if (it == factory.end()) {
291 ERR("Unknown trace format");
292 goto error;
293 }
294
295 trace_format_descriptor = it->second();
296 if (trace_format_descriptor == nullptr) {
297 ERR("Failed to create trace format descriptor for trace format %s",
298 opt_trace_format);
299 goto error;
300 }
301
302 status = lttng_session_descriptor_set_trace_format_descriptor(
303 descriptor, trace_format_descriptor);
304 lttng_trace_format_descriptor_destroy(trace_format_descriptor);
305 if (status != LTTNG_SESSION_DESCRIPTOR_STATUS_OK) {
306 ERR("Failed to set trace format descriptor");
307 goto error;
308 }
309 }
310 end:
311 free(uris);
312 return descriptor;
313 error:
314 lttng_session_descriptor_destroy(descriptor);
315 descriptor = NULL;
316 goto end;
317 }
318
319 /*
320 * Create a tracing session.
321 * If no name is specified, a default name is generated.
322 *
323 * Returns one of the CMD_* result constants.
324 */
325 static int create_session(void)
326 {
327 int ret, i;
328 char shm_path[LTTNG_PATH_MAX] = {};
329 struct lttng_session_descriptor *session_descriptor = NULL;
330 enum lttng_session_descriptor_status descriptor_status;
331 enum lttng_error_code ret_code;
332 struct lttng_session *sessions = NULL;
333 const struct lttng_session *created_session = NULL;
334 const char *created_session_name;
335
336 /* Validate options. */
337 if (opt_session_name) {
338 if (strlen(opt_session_name) > NAME_MAX) {
339 ERR("Session name too long. Length must be lower or equal to %d",
340 NAME_MAX);
341 ret = CMD_ERROR;
342 goto error;
343 }
344 /*
345 * Check if the session name begins with "auto-" or is exactly "auto".
346 * Both are reserved for the default session name. See bug #449 to
347 * understand why we need to check both here.
348 */
349 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
350 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
351 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
352 strlen(DEFAULT_SESSION_NAME)) == 0 &&
353 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
354 ERR("%s is a reserved keyword for default session(s)",
355 DEFAULT_SESSION_NAME);
356 ret = CMD_ERROR;
357 goto error;
358 }
359 }
360
361 if (opt_snapshot && opt_live_timer) {
362 ERR("Snapshot and live modes are mutually exclusive.");
363 ret = CMD_ERROR;
364 goto error;
365 }
366
367 if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) {
368 ERR("Both control and data URLs must be specified.");
369 ret = CMD_ERROR;
370 goto error;
371 }
372
373 session_descriptor = create_session_descriptor();
374 if (!session_descriptor) {
375 ret = CMD_ERROR;
376 goto error;
377 }
378 ret_code = lttng_create_session_ext(session_descriptor);
379 if (ret_code != LTTNG_OK) {
380 ERR("%s", lttng_strerror(-ret_code));
381 ret = CMD_ERROR;
382 goto error;
383 }
384
385 descriptor_status = lttng_session_descriptor_get_session_name(
386 session_descriptor, &created_session_name);
387 if (descriptor_status != LTTNG_SESSION_DESCRIPTOR_STATUS_OK) {
388 ERR("Failed to obtain created session name");
389 ret = CMD_ERROR;
390 goto error;
391 }
392
393 ret = lttng_list_sessions(&sessions);
394 if (ret < 0) {
395 ERR("Failed to fetch properties of created session: %s",
396 lttng_strerror(ret));
397 ret = CMD_ERROR;
398 goto error;
399 }
400 for (i = 0; i < ret; i++) {
401 if (!strcmp(created_session_name, sessions[i].name)) {
402 created_session = &sessions[i];
403 break;
404 }
405 }
406 if (!created_session) {
407 ERR("Failed to fetch properties of created session");
408 ret = CMD_ERROR;
409 goto error;
410 }
411
412 if (opt_shm_path) {
413 char datetime_suffix[17] = {};
414
415 /*
416 * An auto-generated session name already includes the creation
417 * timestamp.
418 */
419 if (opt_session_name) {
420 uint64_t creation_time;
421 struct tm *timeinfo;
422 time_t creation_time_t;
423 size_t strftime_ret;
424
425 ret_code = lttng_session_get_creation_time(
426 created_session,
427 &creation_time);
428 if (ret_code != LTTNG_OK) {
429 ERR("%s", lttng_strerror(-ret_code));
430 ret = CMD_ERROR;
431 goto error;
432 }
433 creation_time_t = (time_t) creation_time;
434 timeinfo = localtime(&creation_time_t);
435 if (!timeinfo) {
436 PERROR("Failed to interpret session creation time");
437 ret = CMD_ERROR;
438 goto error;
439 }
440 strftime_ret = strftime(datetime_suffix,
441 sizeof(datetime_suffix),
442 "-%Y%m%d-%H%M%S", timeinfo);
443 if (strftime_ret == 0) {
444 ERR("Failed to format session creation time.");
445 ret = CMD_ERROR;
446 goto error;
447 }
448 }
449
450 ret = snprintf(shm_path, sizeof(shm_path),
451 "%s/%s%s", opt_shm_path, created_session_name,
452 datetime_suffix);
453 if (ret < 0 || ret >= sizeof(shm_path)) {
454 ERR("Failed to format the shared memory path.");
455 ret = CMD_ERROR;
456 goto error;
457 }
458 ret = lttng_set_session_shm_path(created_session_name,
459 shm_path);
460 if (ret < 0) {
461 lttng_destroy_session(created_session_name);
462 ret = CMD_ERROR;
463 goto error;
464 }
465 }
466
467 if (opt_snapshot) {
468 MSG("Snapshot session %s created.", created_session_name);
469 } else if (opt_live_timer) {
470 MSG("Live session %s created.", created_session_name);
471 } else {
472 MSG("Session %s created.", created_session_name);
473 }
474
475 if (*created_session->path && !opt_snapshot) {
476 MSG("Traces will be output to %s", created_session->path);
477
478 if (opt_live_timer) {
479 MSG("Live timer interval set to %u %s", opt_live_timer,
480 USEC_UNIT);
481 }
482 } else if (opt_snapshot) {
483 struct lttng_snapshot_output_list *list;
484 struct lttng_snapshot_output *iter;
485 char snapshot_url[LTTNG_PATH_MAX] = {};
486
487 ret = lttng_snapshot_list_output(created_session_name, &list);
488 if (ret < 0) {
489 ERR("Failed to list snapshot outputs.");
490 ret = CMD_ERROR;
491 goto error;
492 }
493
494 while ((iter = lttng_snapshot_output_list_get_next(list))) {
495 const char *url = NULL;
496
497 url = lttng_snapshot_output_get_ctrl_url(
498 iter);
499 ret = lttng_strncpy(snapshot_url, url,
500 sizeof(snapshot_url));
501 if (ret) {
502 snapshot_url[0] = '\0';
503 ERR("Failed to retrieve snapshot output destination");
504 }
505 break;
506 }
507 lttng_snapshot_output_list_destroy(list);
508
509 if (*snapshot_url) {
510 MSG("Default snapshot output set to %s",
511 snapshot_url);
512 }
513 MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode.");
514 }
515 if (opt_shm_path) {
516 MSG("Shared memory path set to %s", shm_path);
517 }
518
519 /* Mi output */
520 if (lttng_opt_mi) {
521 ret = mi_created_session(created_session_name);
522 if (ret) {
523 ret = CMD_ERROR;
524 goto error;
525 }
526 }
527
528 /* Init lttng session config */
529 ret = config_init(created_session_name);
530 if (ret < 0) {
531 ret = CMD_ERROR;
532 goto error;
533 }
534
535 ret = CMD_SUCCESS;
536 error:
537 lttng_session_descriptor_destroy(session_descriptor);
538 free(sessions);
539 return ret;
540 }
541
542 /*
543 * spawn_sessiond
544 *
545 * Spawn a session daemon by forking and execv.
546 */
547 static int spawn_sessiond(const char *pathname)
548 {
549 int ret = 0;
550 pid_t pid;
551
552 MSG("Spawning a session daemon");
553 pid = fork();
554 if (pid == 0) {
555 /*
556 * Spawn session daemon in daemon mode.
557 */
558 execlp(pathname, "lttng-sessiond",
559 "--daemonize", NULL);
560 /* execlp only returns if error happened */
561 if (errno == ENOENT) {
562 ERR("No session daemon found. Use --sessiond-path.");
563 } else {
564 PERROR("execlp");
565 }
566 kill(getppid(), SIGTERM); /* wake parent */
567 exit(EXIT_FAILURE);
568 } else if (pid > 0) {
569 /*
570 * In daemon mode (--daemonize), sessiond only exits when
571 * it's ready to accept commands.
572 */
573 for (;;) {
574 int status;
575 pid_t wait_pid_ret = waitpid(pid, &status, 0);
576
577 if (wait_pid_ret < 0) {
578 if (errno == EINTR) {
579 continue;
580 }
581 PERROR("waitpid");
582 ret = -errno;
583 goto end;
584 }
585
586 if (WIFSIGNALED(status)) {
587 ERR("Session daemon was killed by signal %d",
588 WTERMSIG(status));
589 ret = -1;
590 goto end;
591 } else if (WIFEXITED(status)) {
592 DBG("Session daemon terminated normally (exit status: %d)",
593 WEXITSTATUS(status));
594
595 if (WEXITSTATUS(status) != 0) {
596 ERR("Session daemon terminated with an error (exit status: %d)",
597 WEXITSTATUS(status));
598 ret = -1;
599 goto end;
600 }
601 break;
602 }
603 }
604
605 goto end;
606 } else {
607 PERROR("fork");
608 ret = -1;
609 goto end;
610 }
611
612 end:
613 return ret;
614 }
615
616 /*
617 * launch_sessiond
618 *
619 * Check if the session daemon is available using
620 * the liblttngctl API for the check. If not, try to
621 * spawn a daemon.
622 */
623 static int launch_sessiond(void)
624 {
625 int ret;
626 const char *pathname = NULL;
627
628 ret = lttng_session_daemon_alive();
629 if (ret) {
630 /* Sessiond is alive, not an error */
631 ret = 0;
632 goto end;
633 }
634
635 /* Try command line option path */
636 pathname = opt_sessiond_path;
637
638 /* Try LTTNG_SESSIOND_PATH env variable */
639 if (pathname == NULL) {
640 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
641 }
642
643 /* Try with configured path */
644 if (pathname == NULL) {
645 if (CONFIG_SESSIOND_BIN[0] != '\0') {
646 pathname = CONFIG_SESSIOND_BIN;
647 }
648 }
649
650 /* Try the default path */
651 if (pathname == NULL) {
652 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
653 }
654
655 DBG("Session daemon binary path: %s", pathname);
656
657 /* Check existence and permissions */
658 ret = access(pathname, F_OK | X_OK);
659 if (ret < 0) {
660 ERR("No such file or access denied: %s", pathname);
661 goto end;
662 }
663
664 ret = spawn_sessiond(pathname);
665 end:
666 if (ret) {
667 ERR("Problem occurred while launching session daemon (%s)",
668 pathname);
669 }
670 return ret;
671 }
672
673 static
674 int validate_url_option_combination(void)
675 {
676 int ret = 0;
677 int used_count = 0;
678
679 used_count += !!opt_url;
680 used_count += !!opt_output_path;
681 used_count += (opt_data_url || opt_ctrl_url);
682 if (used_count > 1) {
683 ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once.");
684 ret = -1;
685 }
686
687 return ret;
688 }
689
690 /*
691 * The 'create <options>' first level command
692 *
693 * Returns one of the CMD_* result constants.
694 */
695 int cmd_create(int argc, const char **argv)
696 {
697 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
698 char *opt_arg = NULL;
699 const char *leftover = NULL;
700 static poptContext pc;
701
702 pc = poptGetContext(NULL, argc, argv, long_options, 0);
703 poptReadDefaultConfig(pc, 0);
704
705 while ((opt = poptGetNextOpt(pc)) != -1) {
706 switch (opt) {
707 case OPT_HELP:
708 SHOW_HELP();
709 goto end;
710 case OPT_LIST_OPTIONS:
711 list_cmd_options(stdout, long_options);
712 goto end;
713 case OPT_LIVE_TIMER:
714 {
715 uint64_t v;
716
717 errno = 0;
718 if (opt_arg) {
719 free(opt_arg);
720 opt_arg = nullptr;
721 }
722
723 opt_arg = poptGetOptArg(pc);
724 if (!opt_arg) {
725 /* Set up default values. */
726 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
727 DBG("Session live timer interval set to default value %d",
728 opt_live_timer);
729 break;
730 }
731
732 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
733 ERR("Wrong value for --live parameter: %s", opt_arg);
734 ret = CMD_ERROR;
735 goto end;
736 }
737
738 if (v != (uint32_t) v) {
739 ERR("32-bit overflow in --live parameter: %s", opt_arg);
740 ret = CMD_ERROR;
741 goto end;
742 }
743
744 if (v == 0) {
745 ERR("Live timer interval must be greater than zero");
746 ret = CMD_ERROR;
747 goto end;
748 }
749
750 opt_live_timer = (uint32_t) v;
751 DBG("Session live timer interval set to %d", opt_live_timer);
752 break;
753 }
754 default:
755 ret = CMD_UNDEFINED;
756 goto end;
757 }
758 }
759
760 if (opt_no_consumer) {
761 MSG("The option --no-consumer is obsolete. Use --no-output now.");
762 ret = CMD_WARNING;
763 goto end;
764 }
765
766 ret = validate_url_option_combination();
767 if (ret) {
768 ret = CMD_ERROR;
769 goto end;
770 }
771
772 /* Spawn a session daemon if needed */
773 if (!opt_no_sessiond) {
774 ret = launch_sessiond();
775 if (ret) {
776 ret = CMD_ERROR;
777 goto end;
778 }
779 }
780
781 /* MI initialization */
782 if (lttng_opt_mi) {
783 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
784 if (!writer) {
785 ret = -LTTNG_ERR_NOMEM;
786 goto end;
787 }
788
789 /* Open command element */
790 ret = mi_lttng_writer_command_open(writer,
791 mi_lttng_element_command_create);
792 if (ret) {
793 ret = CMD_ERROR;
794 goto end;
795 }
796
797 /* Open output element */
798 ret = mi_lttng_writer_open_element(writer,
799 mi_lttng_element_command_output);
800 if (ret) {
801 ret = CMD_ERROR;
802 goto end;
803 }
804 }
805 opt_session_name = (char*) poptGetArg(pc);
806
807 leftover = poptGetArg(pc);
808 if (leftover) {
809 ERR("Unknown argument: %s", leftover);
810 ret = CMD_ERROR;
811 goto end;
812 }
813
814 command_ret = create_session();
815 if (command_ret) {
816 success = 0;
817 }
818
819 if (lttng_opt_mi) {
820 /* Close output element */
821 ret = mi_lttng_writer_close_element(writer);
822 if (ret) {
823 ret = CMD_ERROR;
824 goto end;
825 }
826
827 /* Success ? */
828 ret = mi_lttng_writer_write_element_bool(writer,
829 mi_lttng_element_command_success, success);
830 if (ret) {
831 ret = CMD_ERROR;
832 goto end;
833 }
834
835 /* Command element close */
836 ret = mi_lttng_writer_command_close(writer);
837 if (ret) {
838 ret = CMD_ERROR;
839 goto end;
840 }
841 }
842
843 end:
844 /* Mi clean-up */
845 if (writer && mi_lttng_writer_destroy(writer)) {
846 /* Preserve original error code */
847 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
848 }
849
850 /* Overwrite ret if an error occurred in create_session() */
851 ret = command_ret ? command_ret : ret;
852
853 free(opt_arg);
854 poptFreeContext(pc);
855 return ret;
856 }
This page took 0.050339 seconds and 4 git commands to generate.