2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
28 #include <common/compat/time.h>
33 #include <common/mi-lttng.h>
35 #include "../command.h"
38 #include <common/defaults.h>
39 #include <common/sessiond-comm/sessiond-comm.h>
40 #include <common/uri.h>
41 #include <common/utils.h>
42 #include <lttng/snapshot.h>
43 #include <lttng/session-descriptor.h>
45 static char *opt_output_path
;
46 static char *opt_session_name
;
48 static char *opt_ctrl_url
;
49 static char *opt_data_url
;
50 static char *opt_shm_path
;
51 static int opt_no_consumer
;
52 static int opt_no_output
;
53 static int opt_snapshot
;
54 static uint32_t opt_live_timer
;
56 #ifdef LTTNG_EMBED_HELP
57 static const char help_msg
[] =
58 #include <lttng-create.1.h>
75 static struct mi_writer
*writer
;
76 static struct poptOption long_options
[] = {
77 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
78 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
79 {"output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
80 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
81 {"set-url", 'U', POPT_ARG_STRING
, &opt_url
, 0, 0, 0},
82 {"ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, 0, 0},
83 {"data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, 0, 0},
84 {"no-output", 0, POPT_ARG_VAL
, &opt_no_output
, 1, 0, 0},
85 {"no-consumer", 0, POPT_ARG_VAL
, &opt_no_consumer
, 1, 0, 0},
86 {"snapshot", 0, POPT_ARG_VAL
, &opt_snapshot
, 1, 0, 0},
87 {"live", 0, POPT_ARG_INT
| POPT_ARGFLAG_OPTIONAL
, 0, OPT_LIVE_TIMER
, 0, 0},
88 {"shm-path", 0, POPT_ARG_STRING
, &opt_shm_path
, 0, 0, 0},
93 * Retrieve the created session and mi output it based on provided argument
94 * This is currently a summary of what was pretty printed and is subject to
97 static int mi_created_session(const char *session_name
)
99 int ret
, i
, count
, found
;
100 struct lttng_session
*sessions
;
102 /* session_name should not be null */
103 assert(session_name
);
106 count
= lttng_list_sessions(&sessions
);
109 ERR("%s", lttng_strerror(ret
));
114 ERR("Error session creation failed: session %s not found", session_name
);
115 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
120 for (i
= 0; i
< count
; i
++) {
121 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
123 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
132 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
144 struct lttng_session_descriptor
*create_session_descriptor(void)
148 enum output_type output_type
;
149 struct lttng_uri
*uris
= NULL
;
150 struct lttng_session_descriptor
*descriptor
= NULL
;
151 const char *uri_str1
= NULL
, *uri_str2
= NULL
;
152 char local_output_path
[LTTNG_PATH_MAX
] = {};
155 output_type
= OUTPUT_NONE
;
156 } else if (opt_output_path
) {
157 char *expanded_output_path
;
159 output_type
= OUTPUT_LOCAL
;
160 expanded_output_path
= utils_expand_path(opt_output_path
);
161 if (!expanded_output_path
) {
162 ERR("Failed to expand output path.");
165 ret
= lttng_strncpy(local_output_path
, expanded_output_path
,
166 sizeof(local_output_path
));
167 free(expanded_output_path
);
169 ERR("Output path exceeds the maximal supported length (%zu bytes)",
170 sizeof(local_output_path
));
173 } else if (opt_url
|| opt_ctrl_url
) {
174 uri_str1
= opt_ctrl_url
? opt_ctrl_url
: opt_url
;
175 uri_str2
= opt_data_url
;
177 uri_count
= uri_parse_str_urls(uri_str1
, uri_str2
, &uris
);
178 if (uri_count
!= 1 && uri_count
!= 2) {
179 ERR("Unrecognized URL format.");
185 output_type
= OUTPUT_LOCAL
;
186 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
187 ERR("Unrecognized URL format.");
190 ret
= lttng_strncpy(local_output_path
, uris
[0].dst
.path
,
191 sizeof(local_output_path
));
193 ERR("Output path exceeds the maximal supported length (%zu bytes)",
194 sizeof(local_output_path
));
198 output_type
= OUTPUT_NETWORK
;
201 /* Already checked. */
205 output_type
= OUTPUT_UNSPECIFIED
;
209 /* Snapshot session. */
210 switch (output_type
) {
211 case OUTPUT_UNSPECIFIED
:
213 descriptor
= lttng_session_descriptor_snapshot_local_create(
215 output_type
== OUTPUT_LOCAL
?
216 local_output_path
: NULL
);
219 descriptor
= lttng_session_descriptor_snapshot_create(
223 descriptor
= lttng_session_descriptor_snapshot_network_create(
224 opt_session_name
, uri_str1
, uri_str2
);
229 } else if (opt_live_timer
) {
231 if (output_type
!= OUTPUT_UNSPECIFIED
&&
232 output_type
!= OUTPUT_NETWORK
) {
233 ERR("Unsupported output type specified for live session.");
236 descriptor
= lttng_session_descriptor_live_network_create(
237 opt_session_name
, uri_str1
, uri_str2
,
240 /* Regular session. */
241 switch (output_type
) {
242 case OUTPUT_UNSPECIFIED
:
244 descriptor
= lttng_session_descriptor_local_create(
246 output_type
== OUTPUT_LOCAL
?
247 local_output_path
: NULL
);
250 descriptor
= lttng_session_descriptor_create(
254 descriptor
= lttng_session_descriptor_network_create(
255 opt_session_name
, uri_str1
, uri_str2
);
262 ERR("Failed to initialize session creation command.");
270 * Create a tracing session.
271 * If no name is specified, a default name is generated.
273 * Returns one of the CMD_* result constants.
275 static int create_session(void)
278 char shm_path
[LTTNG_PATH_MAX
] = {};
279 struct lttng_session_descriptor
*session_descriptor
= NULL
;
280 enum lttng_session_descriptor_status descriptor_status
;
281 enum lttng_error_code ret_code
;
282 struct lttng_session
*sessions
= NULL
;
283 const struct lttng_session
*created_session
= NULL
;
284 const char *created_session_name
;
286 /* Validate options. */
287 if (opt_session_name
) {
288 if (strlen(opt_session_name
) > NAME_MAX
) {
289 ERR("Session name too long. Length must be lower or equal to %d",
295 * Check if the session name begins with "auto-" or is exactly "auto".
296 * Both are reserved for the default session name. See bug #449 to
297 * understand why we need to check both here.
299 if ((strncmp(opt_session_name
, DEFAULT_SESSION_NAME
"-",
300 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) ||
301 (strncmp(opt_session_name
, DEFAULT_SESSION_NAME
,
302 strlen(DEFAULT_SESSION_NAME
)) == 0 &&
303 strlen(opt_session_name
) == strlen(DEFAULT_SESSION_NAME
))) {
304 ERR("%s is a reserved keyword for default session(s)",
305 DEFAULT_SESSION_NAME
);
311 if (opt_snapshot
&& opt_live_timer
) {
312 ERR("Snapshot and live modes are mutually exclusive.");
317 if ((!opt_ctrl_url
&& opt_data_url
) || (opt_ctrl_url
&& !opt_data_url
)) {
318 ERR("Both control and data URLs must be specified.");
323 session_descriptor
= create_session_descriptor();
324 if (!session_descriptor
) {
328 ret_code
= lttng_create_session_ext(session_descriptor
);
329 if (ret_code
!= LTTNG_OK
) {
330 ERR("%s", lttng_strerror(-ret_code
));
335 descriptor_status
= lttng_session_descriptor_get_session_name(
336 session_descriptor
, &created_session_name
);
337 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
338 ERR("Failed to obtain created session name");
343 ret
= lttng_list_sessions(&sessions
);
345 ERR("Failed to fetch properties of created session: %s",
346 lttng_strerror(ret
));
350 for (i
= 0; i
< ret
; i
++) {
351 if (!strcmp(created_session_name
, sessions
[i
].name
)) {
352 created_session
= &sessions
[i
];
356 if (!created_session
) {
357 ERR("Failed to fetch properties of created session");
363 char datetime_suffix
[17] = {};
366 * An auto-generated session name already includes the creation
369 if (opt_session_name
) {
370 uint64_t creation_time
;
372 time_t creation_time_t
;
375 ret_code
= lttng_session_get_creation_time(
378 if (ret_code
!= LTTNG_OK
) {
379 ERR("%s", lttng_strerror(-ret_code
));
383 creation_time_t
= (time_t) creation_time
;
384 timeinfo
= localtime(&creation_time_t
);
386 PERROR("Failed to interpret session creation time");
390 strftime_ret
= strftime(datetime_suffix
,
391 sizeof(datetime_suffix
),
392 "-%Y%m%d-%H%M%S", timeinfo
);
393 if (strftime_ret
== 0) {
394 ERR("Failed to format session creation time.");
400 ret
= snprintf(shm_path
, sizeof(shm_path
),
401 "%s/%s%s", opt_shm_path
, created_session_name
,
403 if (ret
< 0 || ret
>= sizeof(shm_path
)) {
404 ERR("Failed to format the shared memory path.");
408 ret
= lttng_set_session_shm_path(created_session_name
,
411 lttng_destroy_session(created_session_name
);
418 MSG("Snapshot session %s created.", created_session_name
);
419 } else if (opt_live_timer
) {
420 MSG("Live session %s created.", created_session_name
);
422 MSG("Session %s created.", created_session_name
);
425 if (*created_session
->path
&& !opt_snapshot
) {
426 MSG("Traces will be output to %s", created_session
->path
);
428 if (opt_live_timer
) {
429 MSG("Live timer interval set to %u %s", opt_live_timer
,
432 } else if (opt_snapshot
) {
433 struct lttng_snapshot_output_list
*list
;
434 struct lttng_snapshot_output
*iter
;
435 char snapshot_url
[LTTNG_PATH_MAX
] = {};
437 ret
= lttng_snapshot_list_output(created_session_name
, &list
);
439 ERR("Failed to list snapshot outputs.");
444 while ((iter
= lttng_snapshot_output_list_get_next(list
))) {
445 const char *url
= NULL
;
447 url
= lttng_snapshot_output_get_ctrl_url(
449 ret
= lttng_strncpy(snapshot_url
, url
,
450 sizeof(snapshot_url
));
452 snapshot_url
[0] = '\0';
453 ERR("Failed to retrieve snapshot output destination");
457 lttng_snapshot_output_list_destroy(list
);
460 MSG("Default snapshot output set to %s",
463 MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode.");
466 MSG("Shared memory path set to %s", shm_path
);
471 ret
= mi_created_session(created_session_name
);
478 /* Init lttng session config */
479 ret
= config_init(created_session_name
);
487 lttng_session_descriptor_destroy(session_descriptor
);
495 * Spawn a session daemon by forking and execv.
497 static int spawn_sessiond(char *pathname
)
502 MSG("Spawning a session daemon");
506 * Spawn session daemon in daemon mode.
508 execlp(pathname
, "lttng-sessiond",
509 "--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",
541 } else if (WIFEXITED(status
)) {
542 DBG("Session daemon terminated normally (exit status: %d)",
543 WEXITSTATUS(status
));
545 if (WEXITSTATUS(status
) != 0) {
546 ERR("Session daemon terminated with an error (exit status: %d)",
547 WEXITSTATUS(status
));
569 * Check if the session daemon is available using
570 * the liblttngctl API for the check. If not, try to
573 static int launch_sessiond(void)
576 char *pathname
= NULL
;
578 ret
= lttng_session_daemon_alive();
580 /* Sessiond is alive, not an error */
585 /* Try command line option path */
586 pathname
= opt_sessiond_path
;
588 /* Try LTTNG_SESSIOND_PATH env variable */
589 if (pathname
== NULL
) {
590 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
593 /* Try with configured path */
594 if (pathname
== NULL
) {
595 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
596 pathname
= CONFIG_SESSIOND_BIN
;
600 /* Try the default path */
601 if (pathname
== NULL
) {
602 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
605 DBG("Session daemon binary path: %s", pathname
);
607 /* Check existence and permissions */
608 ret
= access(pathname
, F_OK
| X_OK
);
610 ERR("No such file or access denied: %s", pathname
);
614 ret
= spawn_sessiond(pathname
);
617 ERR("Problem occurred while launching session daemon (%s)",
623 int validate_url_option_combination(void)
628 used_count
+= !!opt_url
;
629 used_count
+= !!opt_output_path
;
630 used_count
+= (opt_data_url
|| opt_ctrl_url
);
631 if (used_count
> 1) {
632 ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once.");
640 * The 'create <options>' first level command
642 * Returns one of the CMD_* result constants.
644 int cmd_create(int argc
, const char **argv
)
646 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
647 char *opt_arg
= NULL
;
648 const char *leftover
= NULL
;
649 static poptContext pc
;
651 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
652 poptReadDefaultConfig(pc
, 0);
654 while ((opt
= poptGetNextOpt(pc
)) != -1) {
659 case OPT_LIST_OPTIONS
:
660 list_cmd_options(stdout
, long_options
);
667 opt_arg
= poptGetOptArg(pc
);
669 /* Set up default values. */
670 opt_live_timer
= (uint32_t) DEFAULT_LTTNG_LIVE_TIMER
;
671 DBG("Session live timer interval set to default value %d",
676 if (utils_parse_time_suffix(opt_arg
, &v
) < 0) {
677 ERR("Wrong value for --live parameter: %s", opt_arg
);
682 if (v
!= (uint32_t) v
) {
683 ERR("32-bit overflow in --live parameter: %s", opt_arg
);
689 ERR("Live timer interval must be greater than zero");
694 opt_live_timer
= (uint32_t) v
;
695 DBG("Session live timer interval set to %d", opt_live_timer
);
704 if (opt_no_consumer
) {
705 MSG("The option --no-consumer is obsolete. Use --no-output now.");
710 ret
= validate_url_option_combination();
716 /* Spawn a session daemon if needed */
717 if (!opt_no_sessiond
) {
718 ret
= launch_sessiond();
725 /* MI initialization */
727 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
729 ret
= -LTTNG_ERR_NOMEM
;
733 /* Open command element */
734 ret
= mi_lttng_writer_command_open(writer
,
735 mi_lttng_element_command_create
);
741 /* Open output element */
742 ret
= mi_lttng_writer_open_element(writer
,
743 mi_lttng_element_command_output
);
749 opt_session_name
= (char*) poptGetArg(pc
);
751 leftover
= poptGetArg(pc
);
753 ERR("Unknown argument: %s", leftover
);
758 command_ret
= create_session();
764 /* Close output element */
765 ret
= mi_lttng_writer_close_element(writer
);
772 ret
= mi_lttng_writer_write_element_bool(writer
,
773 mi_lttng_element_command_success
, success
);
779 /* Command element close */
780 ret
= mi_lttng_writer_command_close(writer
);
789 if (writer
&& mi_lttng_writer_destroy(writer
)) {
790 /* Preserve original error code */
791 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
794 /* Overwrite ret if an error occurred in create_session() */
795 ret
= command_ret
? command_ret
: ret
;