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
17 #include <sys/types.h>
18 #include <common/compat/time.h>
23 #include <common/mi-lttng.h>
25 #include "../command.h"
28 #include <common/defaults.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/uri.h>
31 #include <common/utils.h>
32 #include <common/path.h>
33 #include <lttng/lttng.h>
35 static char *opt_output_path
;
36 static char *opt_session_name
;
38 static char *opt_ctrl_url
;
39 static char *opt_data_url
;
40 static char *opt_shm_path
;
41 static int opt_no_consumer
;
42 static int opt_no_output
;
43 static int opt_snapshot
;
44 static uint32_t opt_live_timer
;
46 #ifdef LTTNG_EMBED_HELP
47 static const char help_msg
[] =
48 #include <lttng-create.1.h>
65 static struct mi_writer
*writer
;
66 static struct poptOption long_options
[] = {
67 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
68 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
69 {"output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
70 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
71 {"set-url", 'U', POPT_ARG_STRING
, &opt_url
, 0, 0, 0},
72 {"ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, 0, 0},
73 {"data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, 0, 0},
74 {"no-output", 0, POPT_ARG_VAL
, &opt_no_output
, 1, 0, 0},
75 {"no-consumer", 0, POPT_ARG_VAL
, &opt_no_consumer
, 1, 0, 0},
76 {"snapshot", 0, POPT_ARG_VAL
, &opt_snapshot
, 1, 0, 0},
77 {"live", 0, POPT_ARG_INT
| POPT_ARGFLAG_OPTIONAL
, 0, OPT_LIVE_TIMER
, 0, 0},
78 {"shm-path", 0, POPT_ARG_STRING
, &opt_shm_path
, 0, 0, 0},
83 * Retrieve the created session and mi output it based on provided argument
84 * This is currently a summary of what was pretty printed and is subject to
87 static int mi_created_session(const char *session_name
)
89 int ret
, i
, count
, found
;
90 struct lttng_session
*sessions
;
92 /* session_name should not be null */
96 count
= lttng_list_sessions(&sessions
);
99 ERR("%s", lttng_strerror(ret
));
104 ERR("Error session creation failed: session %s not found", session_name
);
105 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
110 for (i
= 0; i
< count
; i
++) {
111 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
113 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
122 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
134 struct lttng_session_descriptor
*create_session_descriptor(void)
137 enum output_type output_type
;
138 struct lttng_uri
*uris
= NULL
;
139 struct lttng_session_descriptor
*descriptor
= NULL
;
140 const char *uri_str1
= NULL
, *uri_str2
= NULL
;
141 char local_output_path
[LTTNG_PATH_MAX
] = {};
144 output_type
= OUTPUT_NONE
;
145 } else if (opt_output_path
) {
146 char *expanded_output_path
;
149 output_type
= OUTPUT_LOCAL
;
150 expanded_output_path
= utils_expand_path(opt_output_path
);
151 if (!expanded_output_path
) {
152 ERR("Failed to expand output path.");
155 ret
= lttng_strncpy(local_output_path
, expanded_output_path
,
156 sizeof(local_output_path
));
157 free(expanded_output_path
);
159 ERR("Output path exceeds the maximal supported length (%zu bytes)",
160 sizeof(local_output_path
));
163 } else if (opt_url
|| opt_ctrl_url
) {
166 uri_str1
= opt_ctrl_url
? opt_ctrl_url
: opt_url
;
167 uri_str2
= opt_data_url
;
169 uri_count
= uri_parse_str_urls(uri_str1
, uri_str2
, &uris
);
170 if (uri_count
!= 1 && uri_count
!= 2) {
171 ERR("Unrecognized URL format.");
177 output_type
= OUTPUT_LOCAL
;
178 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
179 ERR("Unrecognized URL format.");
182 ret
= lttng_strncpy(local_output_path
, uris
[0].dst
.path
,
183 sizeof(local_output_path
));
185 ERR("Output path exceeds the maximal supported length (%zu bytes)",
186 sizeof(local_output_path
));
190 output_type
= OUTPUT_NETWORK
;
193 /* Already checked. */
197 output_type
= OUTPUT_UNSPECIFIED
;
201 /* Snapshot session. */
202 switch (output_type
) {
203 case OUTPUT_UNSPECIFIED
:
205 descriptor
= lttng_session_descriptor_snapshot_local_create(
207 output_type
== OUTPUT_LOCAL
?
208 local_output_path
: NULL
);
211 descriptor
= lttng_session_descriptor_snapshot_create(
215 descriptor
= lttng_session_descriptor_snapshot_network_create(
216 opt_session_name
, uri_str1
, uri_str2
);
221 } else if (opt_live_timer
) {
223 if (output_type
!= OUTPUT_UNSPECIFIED
&&
224 output_type
!= OUTPUT_NETWORK
) {
225 ERR("Unsupported output type specified for live session.");
228 descriptor
= lttng_session_descriptor_live_network_create(
229 opt_session_name
, uri_str1
, uri_str2
,
232 /* Regular session. */
233 switch (output_type
) {
234 case OUTPUT_UNSPECIFIED
:
236 descriptor
= lttng_session_descriptor_local_create(
238 output_type
== OUTPUT_LOCAL
?
239 local_output_path
: NULL
);
242 descriptor
= lttng_session_descriptor_create(
246 descriptor
= lttng_session_descriptor_network_create(
247 opt_session_name
, uri_str1
, uri_str2
);
254 ERR("Failed to initialize session creation command.");
257 * Auto-launch the relay daemon when a live session
258 * is created using default URLs.
260 if (!opt_url
&& !opt_ctrl_url
&& !opt_data_url
&&
261 opt_live_timer
&& !check_relayd()) {
263 const char *pathname
= opt_relayd_path
? :
264 INSTALL_BIN_PATH
"/lttng-relayd";
266 ret
= spawn_relayd(pathname
, 0);
268 lttng_session_descriptor_destroy(descriptor
);
279 * Create a tracing session.
280 * If no name is specified, a default name is generated.
282 * Returns one of the CMD_* result constants.
284 static int create_session(void)
287 char shm_path
[LTTNG_PATH_MAX
] = {};
288 struct lttng_session_descriptor
*session_descriptor
= NULL
;
289 enum lttng_session_descriptor_status descriptor_status
;
290 enum lttng_error_code ret_code
;
291 struct lttng_session
*sessions
= NULL
;
292 const struct lttng_session
*created_session
= NULL
;
293 const char *created_session_name
;
295 /* Validate options. */
296 if (opt_session_name
) {
297 if (strlen(opt_session_name
) > NAME_MAX
) {
298 ERR("Session name too long. Length must be lower or equal to %d",
304 * Check if the session name begins with "auto-" or is exactly "auto".
305 * Both are reserved for the default session name. See bug #449 to
306 * understand why we need to check both here.
308 if ((strncmp(opt_session_name
, DEFAULT_SESSION_NAME
"-",
309 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) ||
310 (strncmp(opt_session_name
, DEFAULT_SESSION_NAME
,
311 strlen(DEFAULT_SESSION_NAME
)) == 0 &&
312 strlen(opt_session_name
) == strlen(DEFAULT_SESSION_NAME
))) {
313 ERR("%s is a reserved keyword for default session(s)",
314 DEFAULT_SESSION_NAME
);
320 if (opt_snapshot
&& opt_live_timer
) {
321 ERR("Snapshot and live modes are mutually exclusive.");
326 if ((!opt_ctrl_url
&& opt_data_url
) || (opt_ctrl_url
&& !opt_data_url
)) {
327 ERR("Both control and data URLs must be specified.");
332 session_descriptor
= create_session_descriptor();
333 if (!session_descriptor
) {
337 ret_code
= lttng_create_session_ext(session_descriptor
);
338 if (ret_code
!= LTTNG_OK
) {
339 ERR("%s", lttng_strerror(-ret_code
));
344 descriptor_status
= lttng_session_descriptor_get_session_name(
345 session_descriptor
, &created_session_name
);
346 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
347 ERR("Failed to obtain created session name");
352 ret
= lttng_list_sessions(&sessions
);
354 ERR("Failed to fetch properties of created session: %s",
355 lttng_strerror(ret
));
359 for (i
= 0; i
< ret
; i
++) {
360 if (!strcmp(created_session_name
, sessions
[i
].name
)) {
361 created_session
= &sessions
[i
];
365 if (!created_session
) {
366 ERR("Failed to fetch properties of created session");
372 char datetime_suffix
[17] = {};
375 * An auto-generated session name already includes the creation
378 if (opt_session_name
) {
379 uint64_t creation_time
;
381 time_t creation_time_t
;
384 ret_code
= lttng_session_get_creation_time(
387 if (ret_code
!= LTTNG_OK
) {
388 ERR("%s", lttng_strerror(-ret_code
));
392 creation_time_t
= (time_t) creation_time
;
393 timeinfo
= localtime(&creation_time_t
);
395 PERROR("Failed to interpret session creation time");
399 strftime_ret
= strftime(datetime_suffix
,
400 sizeof(datetime_suffix
),
401 "-%Y%m%d-%H%M%S", timeinfo
);
402 if (strftime_ret
== 0) {
403 ERR("Failed to format session creation time.");
409 ret
= snprintf(shm_path
, sizeof(shm_path
),
410 "%s/%s%s", opt_shm_path
, created_session_name
,
412 if (ret
< 0 || ret
>= sizeof(shm_path
)) {
413 ERR("Failed to format the shared memory path.");
417 ret
= lttng_set_session_shm_path(created_session_name
,
420 lttng_destroy_session(created_session_name
);
427 MSG("Snapshot session %s created.", created_session_name
);
428 } else if (opt_live_timer
) {
429 MSG("Live session %s created.", created_session_name
);
431 MSG("Session %s created.", created_session_name
);
434 if (*created_session
->path
&& !opt_snapshot
) {
435 MSG("Traces will be output to %s", created_session
->path
);
437 if (opt_live_timer
) {
438 MSG("Live timer interval set to %u %s", opt_live_timer
,
441 } else if (opt_snapshot
) {
442 struct lttng_snapshot_output_list
*list
;
443 struct lttng_snapshot_output
*iter
;
444 char snapshot_url
[LTTNG_PATH_MAX
] = {};
446 ret
= lttng_snapshot_list_output(created_session_name
, &list
);
448 ERR("Failed to list snapshot outputs.");
453 while ((iter
= lttng_snapshot_output_list_get_next(list
))) {
454 const char *url
= NULL
;
456 url
= lttng_snapshot_output_get_ctrl_url(
458 ret
= lttng_strncpy(snapshot_url
, url
,
459 sizeof(snapshot_url
));
461 snapshot_url
[0] = '\0';
462 ERR("Failed to retrieve snapshot output destination");
466 lttng_snapshot_output_list_destroy(list
);
469 MSG("Default snapshot output set to %s",
472 MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode.");
475 MSG("Shared memory path set to %s", shm_path
);
480 ret
= mi_created_session(created_session_name
);
487 /* Init lttng session config */
488 ret
= config_init(created_session_name
);
496 lttng_session_descriptor_destroy(session_descriptor
);
504 * Spawn a session daemon by forking and execv.
506 static int spawn_sessiond(const char *pathname
)
511 MSG("Spawning a session daemon");
515 * Spawn session daemon in daemon mode.
517 execlp(pathname
, "lttng-sessiond",
518 "--daemonize", NULL
);
519 /* execlp only returns if error happened */
520 if (errno
== ENOENT
) {
521 ERR("No session daemon found. Use --sessiond-path.");
525 kill(getppid(), SIGTERM
); /* wake parent */
527 } else if (pid
> 0) {
529 * In daemon mode (--daemonize), sessiond only exits when
530 * it's ready to accept commands.
534 pid_t wait_pid_ret
= waitpid(pid
, &status
, 0);
536 if (wait_pid_ret
< 0) {
537 if (errno
== EINTR
) {
545 if (WIFSIGNALED(status
)) {
546 ERR("Session daemon was killed by signal %d",
550 } else if (WIFEXITED(status
)) {
551 DBG("Session daemon terminated normally (exit status: %d)",
552 WEXITSTATUS(status
));
554 if (WEXITSTATUS(status
) != 0) {
555 ERR("Session daemon terminated with an error (exit status: %d)",
556 WEXITSTATUS(status
));
578 * Check if the session daemon is available using
579 * the liblttngctl API for the check. If not, try to
582 static int launch_sessiond(void)
585 const char *pathname
= NULL
;
587 ret
= lttng_session_daemon_alive();
589 /* Sessiond is alive, not an error */
594 /* Try command line option path */
595 pathname
= opt_sessiond_path
;
597 /* Try LTTNG_SESSIOND_PATH env variable */
598 if (pathname
== NULL
) {
599 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
602 /* Try with configured path */
603 if (pathname
== NULL
) {
604 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
605 pathname
= CONFIG_SESSIOND_BIN
;
609 /* Try the default path */
610 if (pathname
== NULL
) {
611 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
614 DBG("Session daemon binary path: %s", pathname
);
616 /* Check existence and permissions */
617 ret
= access(pathname
, F_OK
| X_OK
);
619 ERR("No such file or access denied: %s", pathname
);
623 ret
= spawn_sessiond(pathname
);
626 ERR("Problem occurred while launching session daemon (%s)",
633 int validate_url_option_combination(void)
638 used_count
+= !!opt_url
;
639 used_count
+= !!opt_output_path
;
640 used_count
+= (opt_data_url
|| opt_ctrl_url
);
641 if (used_count
> 1) {
642 ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once.");
650 * The 'create <options>' first level command
652 * Returns one of the CMD_* result constants.
654 int cmd_create(int argc
, const char **argv
)
656 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
657 char *opt_arg
= NULL
;
658 const char *leftover
= NULL
;
659 static poptContext pc
;
661 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
662 poptReadDefaultConfig(pc
, 0);
664 while ((opt
= poptGetNextOpt(pc
)) != -1) {
669 case OPT_LIST_OPTIONS
:
670 list_cmd_options(stdout
, long_options
);
677 opt_arg
= poptGetOptArg(pc
);
679 /* Set up default values. */
680 opt_live_timer
= (uint32_t) DEFAULT_LTTNG_LIVE_TIMER
;
681 DBG("Session live timer interval set to default value %d",
686 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
687 ERR("Wrong value for --live parameter: %s", opt_arg
);
692 if (v
!= (uint32_t) v
) {
693 ERR("32-bit overflow in --live parameter: %s", opt_arg
);
699 ERR("Live timer interval must be greater than zero");
704 opt_live_timer
= (uint32_t) v
;
705 DBG("Session live timer interval set to %d", opt_live_timer
);
714 if (opt_no_consumer
) {
715 MSG("The option --no-consumer is obsolete. Use --no-output now.");
720 ret
= validate_url_option_combination();
726 /* Spawn a session daemon if needed */
727 if (!opt_no_sessiond
) {
728 ret
= launch_sessiond();
735 /* MI initialization */
737 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
739 ret
= -LTTNG_ERR_NOMEM
;
743 /* Open command element */
744 ret
= mi_lttng_writer_command_open(writer
,
745 mi_lttng_element_command_create
);
751 /* Open output element */
752 ret
= mi_lttng_writer_open_element(writer
,
753 mi_lttng_element_command_output
);
759 opt_session_name
= (char*) poptGetArg(pc
);
761 leftover
= poptGetArg(pc
);
763 ERR("Unknown argument: %s", leftover
);
768 command_ret
= create_session();
774 /* Close output element */
775 ret
= mi_lttng_writer_close_element(writer
);
782 ret
= mi_lttng_writer_write_element_bool(writer
,
783 mi_lttng_element_command_success
, success
);
789 /* Command element close */
790 ret
= mi_lttng_writer_command_close(writer
);
799 if (writer
&& mi_lttng_writer_destroy(writer
)) {
800 /* Preserve original error code */
801 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
804 /* Overwrite ret if an error occurred in create_session() */
805 ret
= command_ret
? command_ret
: ret
;