Save/load: support session trace format
[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
311 end:
312 free(uris);
313 return descriptor;
314 error:
315 lttng_session_descriptor_destroy(descriptor);
316 descriptor = NULL;
317 goto end;
318 }
319
320 static int trace_format_check(struct lttng_session_descriptor& descriptor)
321 {
322 int ret = 0;
323 enum lttng_error_code ret_code;
324 const lttng_trace_format_descriptor *trace_format_descriptor = nullptr;
325 enum lttng_session_descriptor_status status;
326 std::string destination;
327
328 status = lttng_session_descriptor_get_trace_format_descriptor(
329 &descriptor, &trace_format_descriptor);
330 if (status != LTTNG_SESSION_DESCRIPTOR_STATUS_OK) {
331 ret_code = LTTNG_ERR_INVALID;
332 goto end;
333 }
334
335 /*
336 * Only check kernel domain since only the kernel tracer can lag behind
337 * in term of feature.
338 */
339
340 ret_code = lttng_domain_supports_trace_format(lttng_session_daemon_command_endpoint,
341 LTTNG_DOMAIN_KERNEL, trace_format_descriptor);
342 if (ret_code == LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_KERNEL_TRACER) {
343 WARN("%s", lttng_strerror(ret_code));
344 } else if (ret_code == LTTNG_ERR_KERN_NA || ret_code == LTTNG_ERR_NEED_ROOT_SESSIOND) {
345 /*
346 * Not an error, kernel tracers is simply not available at that
347 * time. Next operation will fail as necessary.
348 */
349 DBG("Kernel tracer not available for trace format support check");
350 } else if (ret_code != LTTNG_OK) {
351 ERR("Failed to validate trace format support: %s", lttng_strerror(ret_code));
352 ret = -1;
353 goto end;
354 }
355
356 if (opt_output_path) {
357 char *tmp = utils_expand_path(opt_output_path);
358 if (!tmp) {
359 ret = -1;
360 goto end;
361 }
362 destination = tmp;
363 free(tmp);
364 } else if (opt_url || opt_ctrl_url) {
365 destination = opt_ctrl_url ? opt_ctrl_url : opt_url;
366 } else if (opt_live_timer) {
367 destination = "tcp://127.0.0.1";
368 }
369
370 if (!destination.empty()) {
371 ret_code = lttng_destination_supports_trace_format(
372 lttng_session_daemon_command_endpoint, destination.c_str(),
373 trace_format_descriptor);
374 if (ret_code == LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_RELAY_DAEMON) {
375 WARN("%s", lttng_strerror(ret_code));
376 } else if (ret_code == LTTNG_ERR_RELAYD_CONNECT_FAIL) {
377 DBG("Could not validate trace format support: %s",
378 lttng_strerror(ret_code));
379 } else if (ret_code != LTTNG_OK) {
380 ERR("Failed to validate trace format support: %s",
381 lttng_strerror(ret_code));
382 ret = -1;
383 goto end;
384 }
385 }
386
387 end:
388 return ret;
389 }
390
391 /*
392 * Create a tracing session.
393 * If no name is specified, a default name is generated.
394 *
395 * Returns one of the CMD_* result constants.
396 */
397 static int create_session(void)
398 {
399 int ret, i;
400 char shm_path[LTTNG_PATH_MAX] = {};
401 struct lttng_session_descriptor *session_descriptor = NULL;
402 enum lttng_session_descriptor_status descriptor_status;
403 enum lttng_error_code ret_code;
404 struct lttng_session *sessions = NULL;
405 const struct lttng_session *created_session = NULL;
406 const char *created_session_name;
407
408 /* Validate options. */
409 if (opt_session_name) {
410 if (strlen(opt_session_name) > NAME_MAX) {
411 ERR("Session name too long. Length must be lower or equal to %d",
412 NAME_MAX);
413 ret = CMD_ERROR;
414 goto error;
415 }
416 /*
417 * Check if the session name begins with "auto-" or is exactly "auto".
418 * Both are reserved for the default session name. See bug #449 to
419 * understand why we need to check both here.
420 */
421 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
422 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
423 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
424 strlen(DEFAULT_SESSION_NAME)) == 0 &&
425 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
426 ERR("%s is a reserved keyword for default session(s)",
427 DEFAULT_SESSION_NAME);
428 ret = CMD_ERROR;
429 goto error;
430 }
431 }
432
433 if (opt_snapshot && opt_live_timer) {
434 ERR("Snapshot and live modes are mutually exclusive.");
435 ret = CMD_ERROR;
436 goto error;
437 }
438
439 if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) {
440 ERR("Both control and data URLs must be specified.");
441 ret = CMD_ERROR;
442 goto error;
443 }
444
445 session_descriptor = create_session_descriptor();
446 if (!session_descriptor) {
447 ret = CMD_ERROR;
448 goto error;
449 }
450
451 ret_code = lttng_create_session_ext(session_descriptor);
452 if (ret_code != LTTNG_OK) {
453 ERR("%s", lttng_strerror(-ret_code));
454 ret = CMD_ERROR;
455 goto error;
456 }
457
458 ret = trace_format_check(*session_descriptor);
459 if (ret < 0) {
460 ret = CMD_ERROR;
461 goto error;
462 }
463
464 descriptor_status = lttng_session_descriptor_get_session_name(
465 session_descriptor, &created_session_name);
466 if (descriptor_status != LTTNG_SESSION_DESCRIPTOR_STATUS_OK) {
467 ERR("Failed to obtain created session name");
468 ret = CMD_ERROR;
469 goto error;
470 }
471
472 ret = lttng_list_sessions(&sessions);
473 if (ret < 0) {
474 ERR("Failed to fetch properties of created session: %s",
475 lttng_strerror(ret));
476 ret = CMD_ERROR;
477 goto error;
478 }
479 for (i = 0; i < ret; i++) {
480 if (!strcmp(created_session_name, sessions[i].name)) {
481 created_session = &sessions[i];
482 break;
483 }
484 }
485 if (!created_session) {
486 ERR("Failed to fetch properties of created session");
487 ret = CMD_ERROR;
488 goto error;
489 }
490
491 if (opt_shm_path) {
492 char datetime_suffix[17] = {};
493
494 /*
495 * An auto-generated session name already includes the creation
496 * timestamp.
497 */
498 if (opt_session_name) {
499 uint64_t creation_time;
500 struct tm *timeinfo;
501 time_t creation_time_t;
502 size_t strftime_ret;
503
504 ret_code = lttng_session_get_creation_time(
505 created_session,
506 &creation_time);
507 if (ret_code != LTTNG_OK) {
508 ERR("%s", lttng_strerror(-ret_code));
509 ret = CMD_ERROR;
510 goto error;
511 }
512 creation_time_t = (time_t) creation_time;
513 timeinfo = localtime(&creation_time_t);
514 if (!timeinfo) {
515 PERROR("Failed to interpret session creation time");
516 ret = CMD_ERROR;
517 goto error;
518 }
519 strftime_ret = strftime(datetime_suffix,
520 sizeof(datetime_suffix),
521 "-%Y%m%d-%H%M%S", timeinfo);
522 if (strftime_ret == 0) {
523 ERR("Failed to format session creation time.");
524 ret = CMD_ERROR;
525 goto error;
526 }
527 }
528
529 ret = snprintf(shm_path, sizeof(shm_path),
530 "%s/%s%s", opt_shm_path, created_session_name,
531 datetime_suffix);
532 if (ret < 0 || ret >= sizeof(shm_path)) {
533 ERR("Failed to format the shared memory path.");
534 ret = CMD_ERROR;
535 goto error;
536 }
537 ret = lttng_set_session_shm_path(created_session_name,
538 shm_path);
539 if (ret < 0) {
540 lttng_destroy_session(created_session_name);
541 ret = CMD_ERROR;
542 goto error;
543 }
544 }
545
546 if (opt_snapshot) {
547 MSG("Snapshot session %s created.", created_session_name);
548 } else if (opt_live_timer) {
549 MSG("Live session %s created.", created_session_name);
550 } else {
551 MSG("Session %s created.", created_session_name);
552 }
553
554 if (*created_session->path && !opt_snapshot) {
555 MSG("Traces will be output to %s", created_session->path);
556
557 if (opt_live_timer) {
558 MSG("Live timer interval set to %u %s", opt_live_timer,
559 USEC_UNIT);
560 }
561 } else if (opt_snapshot) {
562 struct lttng_snapshot_output_list *list;
563 struct lttng_snapshot_output *iter;
564 char snapshot_url[LTTNG_PATH_MAX] = {};
565
566 ret = lttng_snapshot_list_output(created_session_name, &list);
567 if (ret < 0) {
568 ERR("Failed to list snapshot outputs.");
569 ret = CMD_ERROR;
570 goto error;
571 }
572
573 while ((iter = lttng_snapshot_output_list_get_next(list))) {
574 const char *url = NULL;
575
576 url = lttng_snapshot_output_get_ctrl_url(
577 iter);
578 ret = lttng_strncpy(snapshot_url, url,
579 sizeof(snapshot_url));
580 if (ret) {
581 snapshot_url[0] = '\0';
582 ERR("Failed to retrieve snapshot output destination");
583 }
584 break;
585 }
586 lttng_snapshot_output_list_destroy(list);
587
588 if (*snapshot_url) {
589 MSG("Default snapshot output set to %s",
590 snapshot_url);
591 }
592 MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode.");
593 }
594 if (opt_shm_path) {
595 MSG("Shared memory path set to %s", shm_path);
596 }
597
598 /* Mi output */
599 if (lttng_opt_mi) {
600 ret = mi_created_session(created_session_name);
601 if (ret) {
602 ret = CMD_ERROR;
603 goto error;
604 }
605 }
606
607 /* Init lttng session config */
608 ret = config_init(created_session_name);
609 if (ret < 0) {
610 ret = CMD_ERROR;
611 goto error;
612 }
613
614 ret = CMD_SUCCESS;
615 error:
616 lttng_session_descriptor_destroy(session_descriptor);
617 free(sessions);
618 return ret;
619 }
620
621 /*
622 * spawn_sessiond
623 *
624 * Spawn a session daemon by forking and execv.
625 */
626 static int spawn_sessiond(const char *pathname)
627 {
628 int ret = 0;
629 pid_t pid;
630
631 MSG("Spawning a session daemon");
632 pid = fork();
633 if (pid == 0) {
634 /*
635 * Spawn session daemon in daemon mode.
636 */
637 execlp(pathname, "lttng-sessiond",
638 "--daemonize", NULL);
639 /* execlp only returns if error happened */
640 if (errno == ENOENT) {
641 ERR("No session daemon found. Use --sessiond-path.");
642 } else {
643 PERROR("execlp");
644 }
645 kill(getppid(), SIGTERM); /* wake parent */
646 exit(EXIT_FAILURE);
647 } else if (pid > 0) {
648 /*
649 * In daemon mode (--daemonize), sessiond only exits when
650 * it's ready to accept commands.
651 */
652 for (;;) {
653 int status;
654 pid_t wait_pid_ret = waitpid(pid, &status, 0);
655
656 if (wait_pid_ret < 0) {
657 if (errno == EINTR) {
658 continue;
659 }
660 PERROR("waitpid");
661 ret = -errno;
662 goto end;
663 }
664
665 if (WIFSIGNALED(status)) {
666 ERR("Session daemon was killed by signal %d",
667 WTERMSIG(status));
668 ret = -1;
669 goto end;
670 } else if (WIFEXITED(status)) {
671 DBG("Session daemon terminated normally (exit status: %d)",
672 WEXITSTATUS(status));
673
674 if (WEXITSTATUS(status) != 0) {
675 ERR("Session daemon terminated with an error (exit status: %d)",
676 WEXITSTATUS(status));
677 ret = -1;
678 goto end;
679 }
680 break;
681 }
682 }
683
684 goto end;
685 } else {
686 PERROR("fork");
687 ret = -1;
688 goto end;
689 }
690
691 end:
692 return ret;
693 }
694
695 /*
696 * launch_sessiond
697 *
698 * Check if the session daemon is available using
699 * the liblttngctl API for the check. If not, try to
700 * spawn a daemon.
701 */
702 static int launch_sessiond(void)
703 {
704 int ret;
705 const char *pathname = NULL;
706
707 ret = lttng_session_daemon_alive();
708 if (ret) {
709 /* Sessiond is alive, not an error */
710 ret = 0;
711 goto end;
712 }
713
714 /* Try command line option path */
715 pathname = opt_sessiond_path;
716
717 /* Try LTTNG_SESSIOND_PATH env variable */
718 if (pathname == NULL) {
719 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
720 }
721
722 /* Try with configured path */
723 if (pathname == NULL) {
724 if (CONFIG_SESSIOND_BIN[0] != '\0') {
725 pathname = CONFIG_SESSIOND_BIN;
726 }
727 }
728
729 /* Try the default path */
730 if (pathname == NULL) {
731 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
732 }
733
734 DBG("Session daemon binary path: %s", pathname);
735
736 /* Check existence and permissions */
737 ret = access(pathname, F_OK | X_OK);
738 if (ret < 0) {
739 ERR("No such file or access denied: %s", pathname);
740 goto end;
741 }
742
743 ret = spawn_sessiond(pathname);
744 end:
745 if (ret) {
746 ERR("Problem occurred while launching session daemon (%s)",
747 pathname);
748 }
749 return ret;
750 }
751
752 static
753 int validate_url_option_combination(void)
754 {
755 int ret = 0;
756 int used_count = 0;
757
758 used_count += !!opt_url;
759 used_count += !!opt_output_path;
760 used_count += (opt_data_url || opt_ctrl_url);
761 if (used_count > 1) {
762 ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once.");
763 ret = -1;
764 }
765
766 return ret;
767 }
768
769 /*
770 * The 'create <options>' first level command
771 *
772 * Returns one of the CMD_* result constants.
773 */
774 int cmd_create(int argc, const char **argv)
775 {
776 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
777 char *opt_arg = NULL;
778 const char *leftover = NULL;
779 static poptContext pc;
780
781 pc = poptGetContext(NULL, argc, argv, long_options, 0);
782 poptReadDefaultConfig(pc, 0);
783
784 while ((opt = poptGetNextOpt(pc)) != -1) {
785 switch (opt) {
786 case OPT_HELP:
787 SHOW_HELP();
788 goto end;
789 case OPT_LIST_OPTIONS:
790 list_cmd_options(stdout, long_options);
791 goto end;
792 case OPT_LIVE_TIMER:
793 {
794 uint64_t v;
795
796 errno = 0;
797 if (opt_arg) {
798 free(opt_arg);
799 opt_arg = nullptr;
800 }
801
802 opt_arg = poptGetOptArg(pc);
803 if (!opt_arg) {
804 /* Set up default values. */
805 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
806 DBG("Session live timer interval set to default value %d",
807 opt_live_timer);
808 break;
809 }
810
811 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
812 ERR("Wrong value for --live parameter: %s", opt_arg);
813 ret = CMD_ERROR;
814 goto end;
815 }
816
817 if (v != (uint32_t) v) {
818 ERR("32-bit overflow in --live parameter: %s", opt_arg);
819 ret = CMD_ERROR;
820 goto end;
821 }
822
823 if (v == 0) {
824 ERR("Live timer interval must be greater than zero");
825 ret = CMD_ERROR;
826 goto end;
827 }
828
829 opt_live_timer = (uint32_t) v;
830 DBG("Session live timer interval set to %d", opt_live_timer);
831 break;
832 }
833 default:
834 ret = CMD_UNDEFINED;
835 goto end;
836 }
837 }
838
839 if (opt_no_consumer) {
840 MSG("The option --no-consumer is obsolete. Use --no-output now.");
841 ret = CMD_WARNING;
842 goto end;
843 }
844
845 ret = validate_url_option_combination();
846 if (ret) {
847 ret = CMD_ERROR;
848 goto end;
849 }
850
851 /* Spawn a session daemon if needed */
852 if (!opt_no_sessiond) {
853 ret = launch_sessiond();
854 if (ret) {
855 ret = CMD_ERROR;
856 goto end;
857 }
858 }
859
860 /* MI initialization */
861 if (lttng_opt_mi) {
862 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
863 if (!writer) {
864 ret = -LTTNG_ERR_NOMEM;
865 goto end;
866 }
867
868 /* Open command element */
869 ret = mi_lttng_writer_command_open(writer,
870 mi_lttng_element_command_create);
871 if (ret) {
872 ret = CMD_ERROR;
873 goto end;
874 }
875
876 /* Open output element */
877 ret = mi_lttng_writer_open_element(writer,
878 mi_lttng_element_command_output);
879 if (ret) {
880 ret = CMD_ERROR;
881 goto end;
882 }
883 }
884 opt_session_name = (char*) poptGetArg(pc);
885
886 leftover = poptGetArg(pc);
887 if (leftover) {
888 ERR("Unknown argument: %s", leftover);
889 ret = CMD_ERROR;
890 goto end;
891 }
892
893 command_ret = create_session();
894 if (command_ret) {
895 success = 0;
896 }
897
898 if (lttng_opt_mi) {
899 /* Close output element */
900 ret = mi_lttng_writer_close_element(writer);
901 if (ret) {
902 ret = CMD_ERROR;
903 goto end;
904 }
905
906 /* Success ? */
907 ret = mi_lttng_writer_write_element_bool(writer,
908 mi_lttng_element_command_success, success);
909 if (ret) {
910 ret = CMD_ERROR;
911 goto end;
912 }
913
914 /* Command element close */
915 ret = mi_lttng_writer_command_close(writer);
916 if (ret) {
917 ret = CMD_ERROR;
918 goto end;
919 }
920 }
921
922 end:
923 /* Mi clean-up */
924 if (writer && mi_lttng_writer_destroy(writer)) {
925 /* Preserve original error code */
926 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
927 }
928
929 /* Overwrite ret if an error occurred in create_session() */
930 ret = command_ret ? command_ret : ret;
931
932 free(opt_arg);
933 poptFreeContext(pc);
934 return ret;
935 }
This page took 0.078611 seconds and 6 git commands to generate.