2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
10 #include "../command.hpp"
11 #include "../utils.hpp"
13 #include <common/compat/time.hpp>
14 #include <common/defaults.hpp>
15 #include <common/mi-lttng.hpp>
16 #include <common/path.hpp>
17 #include <common/sessiond-comm/sessiond-comm.hpp>
18 #include <common/uri.hpp>
19 #include <common/utils.hpp>
21 #include <lttng/lttng.h>
30 #include <sys/types.h>
34 static char *opt_output_path
;
36 static char *opt_ctrl_url
;
37 static char *opt_data_url
;
38 static char *opt_shm_path
;
39 static int opt_no_consumer
;
40 static int opt_no_output
;
41 static int opt_snapshot
;
42 static uint32_t opt_live_timer
;
44 #ifdef LTTNG_EMBED_HELP
45 static const char help_msg
[] =
46 #include <lttng-create.1.h>
63 static struct mi_writer
*writer
;
64 static struct poptOption long_options
[] = {
65 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
66 { "help", 'h', POPT_ARG_NONE
, nullptr, OPT_HELP
, nullptr, nullptr },
67 { "output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, nullptr, nullptr },
68 { "list-options", 0, POPT_ARG_NONE
, nullptr, OPT_LIST_OPTIONS
, nullptr, nullptr },
69 { "set-url", 'U', POPT_ARG_STRING
, &opt_url
, 0, nullptr, nullptr },
70 { "ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, nullptr, nullptr },
71 { "data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, nullptr, nullptr },
72 { "no-output", 0, POPT_ARG_VAL
, &opt_no_output
, 1, nullptr, nullptr },
73 { "no-consumer", 0, POPT_ARG_VAL
, &opt_no_consumer
, 1, nullptr, nullptr },
74 { "snapshot", 0, POPT_ARG_VAL
, &opt_snapshot
, 1, nullptr, nullptr },
77 POPT_ARG_INT
| POPT_ARGFLAG_OPTIONAL
,
82 { "shm-path", 0, POPT_ARG_STRING
, &opt_shm_path
, 0, nullptr, nullptr },
83 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
87 * Retrieve the created session and mi output it based on provided argument
88 * This is currently a summary of what was pretty printed and is subject to
91 static int mi_created_session(const char *session_name
)
93 int ret
, i
, count
, found
;
94 struct lttng_session
*sessions
;
96 /* session_name should not be null */
97 LTTNG_ASSERT(session_name
);
100 count
= lttng_list_sessions(&sessions
);
103 ERR("%s", lttng_strerror(ret
));
108 ERR("Error session creation failed: session %s not found", session_name
);
109 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
114 for (i
= 0; i
< count
; i
++) {
115 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
117 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
126 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
137 static struct lttng_session_descriptor
*create_session_descriptor(const char *session_name
)
140 enum output_type output_type
;
141 struct lttng_uri
*uris
= nullptr;
142 struct lttng_session_descriptor
*descriptor
= nullptr;
143 const char *uri_str1
= nullptr, *uri_str2
= nullptr;
144 char local_output_path
[LTTNG_PATH_MAX
] = {};
147 output_type
= OUTPUT_NONE
;
148 } else if (opt_output_path
) {
149 char *expanded_output_path
;
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.");
159 local_output_path
, expanded_output_path
, sizeof(local_output_path
));
160 free(expanded_output_path
);
162 ERR("Output path exceeds the maximal supported length (%zu bytes)",
163 sizeof(local_output_path
));
166 } else if (opt_url
|| opt_ctrl_url
) {
169 uri_str1
= opt_ctrl_url
? opt_ctrl_url
: opt_url
;
170 uri_str2
= opt_data_url
;
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.");
180 output_type
= OUTPUT_LOCAL
;
181 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
182 ERR("Unrecognized URL format.");
186 local_output_path
, uris
[0].dst
.path
, sizeof(local_output_path
));
188 ERR("Output path exceeds the maximal supported length (%zu bytes)",
189 sizeof(local_output_path
));
193 output_type
= OUTPUT_NETWORK
;
196 /* Already checked. */
200 output_type
= OUTPUT_UNSPECIFIED
;
204 /* Snapshot session. */
205 switch (output_type
) {
206 case OUTPUT_UNSPECIFIED
:
208 descriptor
= lttng_session_descriptor_snapshot_local_create(
210 output_type
== OUTPUT_LOCAL
? local_output_path
: nullptr);
213 descriptor
= lttng_session_descriptor_snapshot_create(session_name
);
216 descriptor
= lttng_session_descriptor_snapshot_network_create(
217 session_name
, uri_str1
, uri_str2
);
222 } else if (opt_live_timer
) {
224 if (output_type
!= OUTPUT_UNSPECIFIED
&& output_type
!= OUTPUT_NETWORK
) {
225 ERR("Unsupported output type specified for live session.");
228 descriptor
= lttng_session_descriptor_live_network_create(
229 session_name
, uri_str1
, uri_str2
, opt_live_timer
);
231 /* Regular session. */
232 switch (output_type
) {
233 case OUTPUT_UNSPECIFIED
:
235 descriptor
= lttng_session_descriptor_local_create(
237 output_type
== OUTPUT_LOCAL
? local_output_path
: nullptr);
240 descriptor
= lttng_session_descriptor_create(session_name
);
243 descriptor
= lttng_session_descriptor_network_create(
244 session_name
, uri_str1
, uri_str2
);
251 ERR("Failed to initialize session creation command.");
254 * Auto-launch the relay daemon when a live session
255 * is created using default URLs.
257 if (!opt_url
&& !opt_ctrl_url
&& !opt_data_url
&& opt_live_timer
&&
260 const char *pathname
= opt_relayd_path
?: INSTALL_BIN_PATH
"/lttng-relayd";
262 ret
= spawn_relayd(pathname
, 0);
264 lttng_session_descriptor_destroy(descriptor
);
265 descriptor
= nullptr;
275 * Create a tracing session.
276 * If no name is specified, a default name is generated.
278 * Returns one of the CMD_* result constants.
280 static int create_session(const char *session_name
)
283 char shm_path
[LTTNG_PATH_MAX
] = {};
284 struct lttng_session_descriptor
*session_descriptor
= nullptr;
285 enum lttng_session_descriptor_status descriptor_status
;
286 enum lttng_error_code ret_code
;
287 struct lttng_session
*sessions
= nullptr;
288 const struct lttng_session
*created_session
= nullptr;
289 const char *created_session_name
;
291 /* Validate options. */
293 if (strlen(session_name
) > NAME_MAX
) {
294 ERR("Session name too long. Length must be lower or equal to %d", NAME_MAX
);
299 * Check if the session name begins with "auto-" or is exactly "auto".
300 * Both are reserved for the default session name. See bug #449 to
301 * understand why we need to check both here.
303 if ((strncmp(session_name
,
304 DEFAULT_SESSION_NAME
"-",
305 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) ||
306 (strncmp(session_name
, DEFAULT_SESSION_NAME
, strlen(DEFAULT_SESSION_NAME
)) ==
308 strlen(session_name
) == strlen(DEFAULT_SESSION_NAME
))) {
309 ERR("%s is a reserved keyword for default session(s)",
310 DEFAULT_SESSION_NAME
);
316 if (opt_snapshot
&& opt_live_timer
) {
317 ERR("Snapshot and live modes are mutually exclusive.");
322 if ((!opt_ctrl_url
&& opt_data_url
) || (opt_ctrl_url
&& !opt_data_url
)) {
323 ERR("Both control and data URLs must be specified.");
328 session_descriptor
= create_session_descriptor(session_name
);
329 if (!session_descriptor
) {
333 ret_code
= lttng_create_session_ext(session_descriptor
);
334 if (ret_code
!= LTTNG_OK
) {
335 ERR("%s", lttng_strerror(-ret_code
));
340 descriptor_status
= lttng_session_descriptor_get_session_name(session_descriptor
,
341 &created_session_name
);
342 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
343 ERR("Failed to obtain created session name");
348 ret
= lttng_list_sessions(&sessions
);
350 ERR("Failed to fetch properties of created session: %s", lttng_strerror(ret
));
354 for (i
= 0; i
< ret
; i
++) {
355 if (!strcmp(created_session_name
, sessions
[i
].name
)) {
356 created_session
= &sessions
[i
];
360 if (!created_session
) {
361 ERR("Failed to fetch properties of created session");
367 char datetime_suffix
[17] = {};
370 * An auto-generated session name already includes the creation
374 uint64_t creation_time
;
376 time_t creation_time_t
;
379 ret_code
= lttng_session_get_creation_time(created_session
, &creation_time
);
380 if (ret_code
!= LTTNG_OK
) {
381 ERR("%s", lttng_strerror(-ret_code
));
385 creation_time_t
= (time_t) creation_time
;
386 timeinfo
= localtime(&creation_time_t
);
388 PERROR("Failed to interpret session creation time");
392 strftime_ret
= strftime(datetime_suffix
,
393 sizeof(datetime_suffix
),
396 if (strftime_ret
== 0) {
397 ERR("Failed to format session creation time.");
403 ret
= snprintf(shm_path
,
407 created_session_name
,
409 if (ret
< 0 || ret
>= sizeof(shm_path
)) {
410 ERR("Failed to format the shared memory path.");
414 ret
= lttng_set_session_shm_path(created_session_name
, shm_path
);
416 lttng_destroy_session(created_session_name
);
423 MSG("Snapshot session %s created.", created_session_name
);
424 } else if (opt_live_timer
) {
425 MSG("Live session %s created.", created_session_name
);
427 MSG("Session %s created.", created_session_name
);
430 if (*created_session
->path
&& !opt_snapshot
) {
431 MSG("Traces will be output to %s", created_session
->path
);
433 if (opt_live_timer
) {
434 MSG("Live timer interval set to %u %s", opt_live_timer
, USEC_UNIT
);
436 } else if (opt_snapshot
) {
437 struct lttng_snapshot_output_list
*list
;
438 struct lttng_snapshot_output
*iter
;
439 char snapshot_url
[LTTNG_PATH_MAX
] = {};
441 ret
= lttng_snapshot_list_output(created_session_name
, &list
);
443 ERR("Failed to list snapshot outputs.");
448 while ((iter
= lttng_snapshot_output_list_get_next(list
))) {
449 const char *url
= nullptr;
451 url
= lttng_snapshot_output_get_ctrl_url(iter
);
452 ret
= lttng_strncpy(snapshot_url
, url
, sizeof(snapshot_url
));
454 snapshot_url
[0] = '\0';
455 ERR("Failed to retrieve snapshot output destination");
459 lttng_snapshot_output_list_destroy(list
);
462 MSG("Default snapshot output set to %s", snapshot_url
);
464 MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode.");
467 MSG("Shared memory path set to %s", shm_path
);
472 ret
= mi_created_session(created_session_name
);
479 /* Init lttng session config */
480 ret
= config_init(created_session_name
);
488 lttng_session_descriptor_destroy(session_descriptor
);
496 * Spawn a session daemon by forking and execv.
498 static int spawn_sessiond(const char *pathname
)
503 MSG("Spawning a session daemon");
507 * Spawn session daemon in daemon mode.
509 execlp(pathname
, "lttng-sessiond", "--daemonize", NULL
);
510 /* execlp only returns if error happened */
511 if (errno
== ENOENT
) {
512 ERR("No session daemon found. Use --sessiond-path.");
516 kill(getppid(), SIGTERM
); /* wake parent */
518 } else if (pid
> 0) {
520 * In daemon mode (--daemonize), sessiond only exits when
521 * it's ready to accept commands.
525 pid_t wait_pid_ret
= waitpid(pid
, &status
, 0);
527 if (wait_pid_ret
< 0) {
528 if (errno
== EINTR
) {
536 if (WIFSIGNALED(status
)) {
537 ERR("Session daemon was killed by signal %d", WTERMSIG(status
));
540 } else if (WIFEXITED(status
)) {
541 DBG("Session daemon terminated normally (exit status: %d)",
542 WEXITSTATUS(status
));
544 if (WEXITSTATUS(status
) != 0) {
545 ERR("Session daemon terminated with an error (exit status: %d)",
546 WEXITSTATUS(status
));
568 * Check if the session daemon is available using
569 * the liblttngctl API for the check. If not, try to
572 static int launch_sessiond()
575 const char *pathname
= nullptr;
577 ret
= lttng_session_daemon_alive();
579 /* Sessiond is alive, not an error */
584 /* Try command line option path */
585 pathname
= opt_sessiond_path
;
587 /* Try LTTNG_SESSIOND_PATH env variable */
588 if (pathname
== nullptr) {
589 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
592 /* Try with configured path */
593 if (pathname
== nullptr) {
594 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
595 pathname
= CONFIG_SESSIOND_BIN
;
599 /* Try the default path */
600 if (pathname
== nullptr) {
601 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
604 DBG("Session daemon binary path: %s", pathname
);
606 /* Check existence and permissions */
607 ret
= access(pathname
, F_OK
| X_OK
);
609 ERR("No such file or access denied: %s", pathname
);
613 ret
= spawn_sessiond(pathname
);
616 ERR("Problem occurred while launching session daemon (%s)", pathname
);
621 static int validate_url_option_combination()
626 used_count
+= !!opt_url
;
627 used_count
+= !!opt_output_path
;
628 used_count
+= (opt_data_url
|| opt_ctrl_url
);
629 if (used_count
> 1) {
630 ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once.");
638 * The 'create <options>' first level command
640 * Returns one of the CMD_* result constants.
642 int cmd_create(int argc
, const char **argv
)
644 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
645 char *opt_arg
= nullptr;
646 const char *arg_session_name
= nullptr;
647 const char *leftover
= nullptr;
648 static poptContext pc
;
650 pc
= poptGetContext(nullptr, argc
, argv
, long_options
, 0);
651 poptReadDefaultConfig(pc
, 0);
653 while ((opt
= poptGetNextOpt(pc
)) != -1) {
658 case OPT_LIST_OPTIONS
:
659 list_cmd_options(stdout
, long_options
);
671 opt_arg
= poptGetOptArg(pc
);
673 /* Set up default values. */
674 opt_live_timer
= (uint32_t) DEFAULT_LTTNG_LIVE_TIMER
;
675 DBG("Session live timer interval set to default value %d",
680 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
681 ERR("Wrong value for --live parameter: %s", opt_arg
);
686 if (v
!= (uint32_t) v
) {
687 ERR("32-bit overflow in --live parameter: %s", opt_arg
);
693 ERR("Live timer interval must be greater than zero");
698 opt_live_timer
= (uint32_t) v
;
699 DBG("Session live timer interval set to %d", opt_live_timer
);
708 if (opt_no_consumer
) {
709 MSG("The option --no-consumer is obsolete. Use --no-output now.");
714 ret
= validate_url_option_combination();
720 /* Spawn a session daemon if needed */
721 if (!opt_no_sessiond
) {
722 ret
= launch_sessiond();
729 /* MI initialization */
731 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
733 ret
= -LTTNG_ERR_NOMEM
;
737 /* Open command element */
738 ret
= mi_lttng_writer_command_open(writer
, mi_lttng_element_command_create
);
744 /* Open output element */
745 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_command_output
);
752 /* Get the optional session name argument. */
753 arg_session_name
= poptGetArg(pc
);
755 leftover
= poptGetArg(pc
);
757 ERR("Unknown argument: %s", leftover
);
762 command_ret
= create_session(arg_session_name
);
768 /* Close output element */
769 ret
= mi_lttng_writer_close_element(writer
);
776 ret
= mi_lttng_writer_write_element_bool(
777 writer
, mi_lttng_element_command_success
, success
);
783 /* Command element close */
784 ret
= mi_lttng_writer_command_close(writer
);
793 if (writer
&& mi_lttng_writer_destroy(writer
)) {
794 /* Preserve original error code */
795 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
798 /* Overwrite ret if an error occurred in create_session() */
799 ret
= command_ret
? command_ret
: ret
;