2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
32 #include <common/mi-lttng.h>
34 #include "../command.h"
37 #include <common/defaults.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/uri.h>
40 #include <common/utils.h>
41 #include <lttng/snapshot.h>
43 static char *opt_output_path
;
44 static char *opt_session_name
;
46 static char *opt_ctrl_url
;
47 static char *opt_data_url
;
48 static char *opt_shm_path
;
49 static int opt_no_consumer
;
50 static int opt_no_output
;
51 static int opt_snapshot
;
52 static unsigned int opt_live_timer
;
74 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 * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on
94 * why this declaration exists and used ONLY in for this command.
96 extern int _lttng_create_session_ext(const char *name
, const char *url
,
97 const char *datetime
);
100 * Retrieve the created session and mi output it based on provided argument
101 * This is currently a summary of what was pretty printed and is subject to
104 static int mi_created_session(const char *session_name
)
106 int ret
, i
, count
, found
;
107 struct lttng_session
*sessions
;
109 /* session_name should not be null */
110 assert(session_name
);
113 count
= lttng_list_sessions(&sessions
);
116 ERR("%s", lttng_strerror(ret
));
121 ERR("Error session creation failed: session %s not found", session_name
);
122 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
127 for (i
= 0; i
< count
; i
++) {
128 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
130 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
139 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
151 * For a session name, set the consumer URLs.
153 static int set_consumer_url(const char *session_name
, const char *ctrl_url
,
154 const char *data_url
)
157 struct lttng_handle
*handle
;
159 assert(session_name
);
162 * Set handle with the session_name, but no domain. This implies that
163 * the actions taken with this handle apply on the tracing session
164 * rather then the domain-specific session.
166 handle
= lttng_create_handle(session_name
, NULL
);
167 if (handle
== NULL
) {
172 ret
= lttng_set_consumer_url(handle
, ctrl_url
, data_url
);
178 lttng_destroy_handle(handle
);
182 static int add_snapshot_output(const char *session_name
, const char *ctrl_url
,
183 const char *data_url
)
186 struct lttng_snapshot_output
*output
= NULL
;
188 assert(session_name
);
190 output
= lttng_snapshot_output_create();
197 ret
= lttng_snapshot_output_set_ctrl_url(ctrl_url
, output
);
204 ret
= lttng_snapshot_output_set_data_url(data_url
, output
);
210 /* This call, if successful, populates the id of the output object. */
211 ret
= lttng_snapshot_add_output(session_name
, output
);
217 lttng_snapshot_output_destroy(output
);
223 * Validate the combinations of passed options
226 * CMD_SUCCESS on success
228 static int validate_command_options(void)
230 int ret
= CMD_SUCCESS
;
231 if (opt_snapshot
&& opt_live_timer
) {
232 ERR("Snapshot and live modes are mutually exclusive.");
237 if ((!opt_ctrl_url
&& opt_data_url
) || (opt_ctrl_url
&& !opt_data_url
)) {
238 ERR("You need both control and data URL.");
248 * Create a session via direct calls to liblttng-ctl.
250 * Return CMD_SUCCESS on success, negative value on internal lttng errors and positive
251 * value on command errors.
253 static int create_session_basic (const char *session_name
,
258 const char* ctrl_url
,
259 const char* data_url
,
260 const char* shm_path
,
261 const char* datetime
)
263 /* Create session based on session creation */
264 int ret
= CMD_SUCCESS
;
265 const char *pathname
;
269 if (opt_relayd_path
) {
270 pathname
= opt_relayd_path
;
272 pathname
= INSTALL_BIN_PATH
"/lttng-relayd";
275 switch (session_type
) {
277 ret
= _lttng_create_session_ext(session_name
, url
, datetime
);
279 case SESSION_SNAPSHOT
:
280 if (output_type
== OUTPUT_NONE
) {
281 ERR("--no-output on a snapshot session is invalid");
282 ret
= CMD_UNSUPPORTED
;
285 ret
= lttng_create_session_snapshot(session_name
, url
);
288 if (output_type
== OUTPUT_NONE
) {
289 ERR("--no-output on a live session is invalid");
290 ret
= CMD_UNSUPPORTED
;
294 if (output_type
== OUTPUT_LOCAL
) {
295 ERR("Local file output on a live session is invalid");
296 ret
= CMD_UNSUPPORTED
;
299 if (output_type
!= OUTPUT_NET
&& !check_relayd() &&
300 spawn_relayd(pathname
, 0) < 0) {
304 ret
= lttng_create_session_live(session_name
, url
, live_timer
);
307 ERR("Unknown session type");
313 /* Don't set ret so the sessiond error is propagated. */
315 case LTTNG_ERR_EXIST_SESS
:
316 WARN("Session %s already exists", session_name
);
324 /* Configure the session based on the output type */
325 switch (output_type
) {
329 if (!ctrl_url
|| !data_url
) {
333 if (session_type
== SESSION_SNAPSHOT
) {
334 ret
= add_snapshot_output(session_name
, ctrl_url
,
338 * Normal sessions and live sessions behave the same way
339 * regarding consumer url.
341 ret
= set_consumer_url(session_name
, ctrl_url
, data_url
);
344 /* Destroy created session on errors */
345 lttng_destroy_session(session_name
);
352 ERR("Unknown output type");
358 * Set the session shared memory path
361 ret
= lttng_set_session_shm_path(session_name
, shm_path
);
363 lttng_destroy_session(session_name
);
371 static int generate_output(const char *session_name
,
376 const char* ctrl_url
,
377 const char* data_url
,
378 const char* shm_path
)
380 int ret
= CMD_SUCCESS
;
383 * TODO move this to after session name
384 * for now we only emulate previous behaviour.
386 if (session_type
!= SESSION_SNAPSHOT
) {
388 MSG("Control URL %s set for session %s", ctrl_url
, session_name
);
392 MSG("Data URL %s set for session %s", data_url
, session_name
);
396 if (url
&& output_type
== OUTPUT_LOCAL
) {
397 /* Remove the file:// */
398 if (strlen(url
) > strlen("file://")){
399 url
= url
+ strlen("file://");
403 MSG("Session %s created.", session_name
);
404 if (url
&& session_type
!= SESSION_SNAPSHOT
) {
405 MSG("Traces will be written in %s", url
);
408 MSG("Live timer set to %u usec", live_timer
);
410 } else if (session_type
== SESSION_SNAPSHOT
) {
412 MSG("Default snapshot output set to: %s", url
);
414 MSG("Snapshot mode set. Every channel enabled for that session will "
415 "be set to mmap output, and default to overwrite mode.");
419 MSG("Session %s set to shm_path: %s.", session_name
,
425 ret
= mi_created_session(session_name
);
436 * Create a tracing session.
437 * If no name is specified, a default name is generated.
439 * Returns one of the CMD_* result constants.
441 static int create_session(void)
446 int base_session_type
= SESSION_UNKNOWN
;
447 int base_output_type
= OUTPUT_UNKNOWN
;
448 char *base_session_name
= NULL
;
449 char *base_url
= NULL
;
450 char *base_ctrl_url
= NULL
;
451 char *base_data_url
= NULL
;
452 char *base_shm_path
= NULL
;
453 int base_live_timer
= 0;
458 struct tm
*timeinfo
= NULL
;
460 /* Temporary variables */
461 char *traces_path
= NULL
;
462 char *temp_url
= NULL
;
463 char *session_name_date
= NULL
;
464 char *tmp_url
= NULL
;
465 char *tmp_home_path
= NULL
;
466 struct lttng_uri
*uris
= NULL
;
467 ssize_t uri_array_size
= 0;
469 /* Option validation */
470 if (validate_command_options() != CMD_SUCCESS
) {
475 /* Get date and time for automatic session name/path */
477 timeinfo
= localtime(&rawtime
);
478 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
480 /* Find the session type based on options */
481 if(base_session_type
== SESSION_UNKNOWN
) {
483 base_session_type
= SESSION_SNAPSHOT
;
484 } else if (opt_live_timer
) {
485 base_session_type
= SESSION_LIVE
;
487 base_session_type
= SESSION_NORMAL
;
492 * Session name handling
494 if (opt_session_name
) {
495 /* Override the session name */
496 if (strlen(opt_session_name
) > NAME_MAX
) {
497 ERR("Session name too long. Length must be lower or equal to %d",
499 ret
= LTTNG_ERR_SESSION_FAIL
;
500 free(session_name_date
);
504 * Check if the session name begins with "auto-" or is exactly "auto".
505 * Both are reserved for the default session name. See bug #449 to
506 * understand why we need to check both here.
508 if ((strncmp(opt_session_name
, DEFAULT_SESSION_NAME
"-",
509 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) ||
510 (strncmp(opt_session_name
, DEFAULT_SESSION_NAME
,
511 strlen(DEFAULT_SESSION_NAME
)) == 0 &&
512 strlen(opt_session_name
) == strlen(DEFAULT_SESSION_NAME
))) {
513 ERR("%s is a reserved keyword for default session(s)",
514 DEFAULT_SESSION_NAME
);
520 base_session_name
= strndup(opt_session_name
, NAME_MAX
);
521 if (!base_session_name
) {
522 PERROR("Strdup session name");
527 ret
= asprintf(&session_name_date
, "%s-%s", base_session_name
, datetime
);
529 PERROR("Asprintf session name");
532 DBG("Session name from command option set to %s", base_session_name
);
533 } else if (base_session_name
) {
534 ret
= asprintf(&session_name_date
, "%s-%s", base_session_name
, datetime
);
536 PERROR("Asprintf session name");
540 /* Generate a name */
541 /* TODO: use asprint */
542 ret
= asprintf(&base_session_name
, DEFAULT_SESSION_NAME
"-%s", datetime
);
544 PERROR("Asprintf generated session name");
547 session_name_date
= strdup(base_session_name
);
548 DBG("Auto session name set to %s", base_session_name
);
557 * If any of those options are present clear all output related data.
559 if (opt_output_path
|| opt_url
|| (opt_ctrl_url
&& opt_data_url
) || opt_no_output
) {
560 /* Overwrite output */
565 base_ctrl_url
= NULL
;
566 base_data_url
= NULL
;
569 if (opt_output_path
) {
571 traces_path
= utils_expand_path(opt_output_path
);
577 /* Create URL string from the local file system path */
578 ret
= asprintf(&temp_url
, "file://%s", traces_path
);
580 PERROR("asprintf url path");
586 } else if (opt_url
) { /* Handling URL (-U opt) */
587 base_url
= strdup(opt_url
);
588 } else if (opt_data_url
&& opt_ctrl_url
) {
590 * With both control and data, we'll be setting the consumer URL
591 * after session creation thus use no URL.
593 base_ctrl_url
= strdup(opt_ctrl_url
);
594 base_data_url
= strdup(opt_data_url
);
595 } else if (!(opt_no_output
|| base_output_type
== OUTPUT_NONE
||
596 base_url
|| base_ctrl_url
|| base_data_url
)) {
597 /* Generate default output depending on the session type */
598 switch (base_session_type
) {
601 case SESSION_SNAPSHOT
:
602 /* Default to a local path */
603 tmp_home_path
= utils_get_home_dir();
604 if (tmp_home_path
== NULL
) {
605 ERR("HOME path not found.\n \
606 Please specify an output path using -o, --output PATH");
611 ret
= asprintf(&tmp_url
,
612 "file://%s/" DEFAULT_TRACE_DIR_NAME
"/%s",
613 tmp_home_path
, session_name_date
);
616 PERROR("asprintf trace dir name");
624 /* Default to a net output */
625 ret
= asprintf(&tmp_url
, "net://127.0.0.1");
627 PERROR("asprintf default live URL");
634 ERR("Unknown session type");
641 * Shared memory path handling
644 ret
= asprintf(&base_shm_path
, "%s/%s", opt_shm_path
, session_name_date
);
646 PERROR("asprintf shm_path");
652 * Live timer handling
654 if (opt_live_timer
) {
655 base_live_timer
= opt_live_timer
;
658 /* Get output type from urls */
660 /* Get lttng uris from single url */
661 uri_array_size
= uri_parse_str_urls(base_url
, NULL
, &uris
);
662 if (uri_array_size
< 0) {
666 } else if (base_ctrl_url
&& base_data_url
) {
667 uri_array_size
= uri_parse_str_urls(base_ctrl_url
, base_data_url
, &uris
);
668 if (uri_array_size
< 0) {
677 switch (uri_array_size
) {
679 base_output_type
= OUTPUT_NONE
;
682 base_output_type
= OUTPUT_LOCAL
;
685 base_output_type
= OUTPUT_NET
;
692 ret
= create_session_basic (base_session_name
,
705 ret
= generate_output (base_session_name
,
717 /* Init lttng session config */
718 ret
= config_init(base_session_name
);
728 /* Session temp stuff */
729 free(session_name_date
);
734 ERR("%s", lttng_strerror(ret
));
736 free(base_session_name
);
747 * Spawn a session daemon by forking and execv.
749 static int spawn_sessiond(char *pathname
)
754 MSG("Spawning a session daemon");
758 * Spawn session daemon in daemon mode.
760 execlp(pathname
, "lttng-sessiond",
761 "--daemonize", NULL
);
762 /* execlp only returns if error happened */
763 if (errno
== ENOENT
) {
764 ERR("No session daemon found. Use --sessiond-path.");
768 kill(getppid(), SIGTERM
); /* wake parent */
770 } else if (pid
> 0) {
772 * In daemon mode (--daemonize), sessiond only exits when
773 * it's ready to accept commands.
777 pid_t wait_pid_ret
= waitpid(pid
, &status
, 0);
779 if (wait_pid_ret
< 0) {
780 if (errno
== EINTR
) {
788 if (WIFSIGNALED(status
)) {
789 ERR("Session daemon was killed by signal %d",
793 } else if (WIFEXITED(status
)) {
794 DBG("Session daemon terminated normally (exit status: %d)",
795 WEXITSTATUS(status
));
797 if (WEXITSTATUS(status
) != 0) {
798 ERR("Session daemon terminated with an error (exit status: %d)",
799 WEXITSTATUS(status
));
821 * Check if the session daemon is available using
822 * the liblttngctl API for the check. If not, try to
825 static int launch_sessiond(void)
828 char *pathname
= NULL
;
830 ret
= lttng_session_daemon_alive();
832 /* Sessiond is alive, not an error */
837 /* Try command line option path */
838 pathname
= opt_sessiond_path
;
840 /* Try LTTNG_SESSIOND_PATH env variable */
841 if (pathname
== NULL
) {
842 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
845 /* Try with configured path */
846 if (pathname
== NULL
) {
847 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
848 pathname
= CONFIG_SESSIOND_BIN
;
852 /* Try the default path */
853 if (pathname
== NULL
) {
854 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
857 DBG("Session daemon binary path: %s", pathname
);
859 /* Check existence and permissions */
860 ret
= access(pathname
, F_OK
| X_OK
);
862 ERR("No such file or access denied: %s", pathname
);
866 ret
= spawn_sessiond(pathname
);
869 ERR("Problem occurred while launching session daemon (%s)",
876 * The 'create <options>' first level command
878 * Returns one of the CMD_* result constants.
880 int cmd_create(int argc
, const char **argv
)
882 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
883 char *opt_arg
= NULL
;
884 static poptContext pc
;
886 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
887 poptReadDefaultConfig(pc
, 0);
889 while ((opt
= poptGetNextOpt(pc
)) != -1) {
894 case OPT_LIST_OPTIONS
:
895 list_cmd_options(stdout
, long_options
);
902 opt_arg
= poptGetOptArg(pc
);
904 /* Set up default values. */
905 opt_live_timer
= (uint32_t) DEFAULT_LTTNG_LIVE_TIMER
;
906 DBG("Session live timer interval set to default value %d",
911 v
= strtoul(opt_arg
, NULL
, 0);
912 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
913 ERR("Wrong value in --live parameter: %s", opt_arg
);
917 if (v
!= (uint32_t) v
) {
918 ERR("32-bit overflow in --live parameter: %s", opt_arg
);
923 ERR("Live timer interval must be greater than zero");
927 opt_live_timer
= (uint32_t) v
;
928 DBG("Session live timer interval set to %d", opt_live_timer
);
937 if (opt_no_consumer
) {
938 MSG("The option --no-consumer is obsolete. Use --no-output now.");
943 /* Spawn a session daemon if needed */
944 if (!opt_no_sessiond
) {
945 ret
= launch_sessiond();
952 /* MI initialization */
954 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
956 ret
= -LTTNG_ERR_NOMEM
;
960 /* Open command element */
961 ret
= mi_lttng_writer_command_open(writer
,
962 mi_lttng_element_command_create
);
968 /* Open output element */
969 ret
= mi_lttng_writer_open_element(writer
,
970 mi_lttng_element_command_output
);
976 opt_session_name
= (char*) poptGetArg(pc
);
978 command_ret
= create_session();
985 /* Close output element */
986 ret
= mi_lttng_writer_close_element(writer
);
993 ret
= mi_lttng_writer_write_element_bool(writer
,
994 mi_lttng_element_command_success
, success
);
1000 /* Command element close */
1001 ret
= mi_lttng_writer_command_close(writer
);
1010 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1011 /* Preserve original error code */
1012 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1015 /* Overwrite ret if an error occurred in create_session() */
1016 ret
= command_ret
? command_ret
: ret
;
1018 poptFreeContext(pc
);