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>
33 #include <common/config/session-config.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>
44 static char *opt_output_path
;
45 static char *opt_session_name
;
47 static char *opt_ctrl_url
;
48 static char *opt_data_url
;
49 static char *opt_shm_path
;
50 static char *opt_template_path
;
51 static int opt_no_consumer
;
52 static int opt_no_output
;
53 static int opt_snapshot
;
54 static unsigned int opt_live_timer
;
55 static bool opt_default_name
;
78 static struct mi_writer
*writer
;
80 static struct poptOption long_options
[] = {
81 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
82 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
83 {"output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
84 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
85 {"set-url", 'U', POPT_ARG_STRING
, &opt_url
, 0, 0, 0},
86 {"ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, 0, 0},
87 {"data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, 0, 0},
88 {"no-output", 0, POPT_ARG_VAL
, &opt_no_output
, 1, 0, 0},
89 {"no-consumer", 0, POPT_ARG_VAL
, &opt_no_consumer
, 1, 0, 0},
90 {"snapshot", 0, POPT_ARG_VAL
, &opt_snapshot
, 1, 0, 0},
91 {"live", 0, POPT_ARG_INT
| POPT_ARGFLAG_OPTIONAL
, 0, OPT_LIVE_TIMER
, 0, 0},
92 {"shm-path", 0, POPT_ARG_STRING
, &opt_shm_path
, 0, 0, 0},
93 {"template-path", 0, POPT_ARG_STRING
, &opt_template_path
, 0, 0, 0},
94 {"default-name", 0, POPT_ARG_NONE
, NULL
, OPT_DEFAULT_NAME
, 0, 0},
99 * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on
100 * why this declaration exists and used ONLY in for this command.
102 extern int _lttng_create_session_ext(const char *name
, const char *url
,
103 const char *datetime
);
106 * Retrieve the created session and mi output it based on provided argument
107 * This is currently a summary of what was pretty printed and is subject to
110 static int mi_created_session(const char *session_name
)
112 int ret
, i
, count
, found
;
113 struct lttng_session
*sessions
;
115 /* session_name should not be null */
116 assert(session_name
);
119 count
= lttng_list_sessions(&sessions
);
122 ERR("%s", lttng_strerror(ret
));
127 ERR("Error session creation failed: session %s not found", session_name
);
128 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
133 for (i
= 0; i
< count
; i
++) {
134 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
136 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
145 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
157 * For a session name, set the consumer URLs.
159 static int set_consumer_url(const char *session_name
, const char *ctrl_url
,
160 const char *data_url
)
163 struct lttng_handle
*handle
;
165 assert(session_name
);
168 * Set handle with the session_name, but no domain. This implies that
169 * the actions taken with this handle apply on the tracing session
170 * rather then the domain-specific session.
172 handle
= lttng_create_handle(session_name
, NULL
);
173 if (handle
== NULL
) {
178 ret
= lttng_set_consumer_url(handle
, ctrl_url
, data_url
);
184 lttng_destroy_handle(handle
);
188 static int add_snapshot_output(const char *session_name
, const char *ctrl_url
,
189 const char *data_url
)
192 struct lttng_snapshot_output
*output
= NULL
;
194 assert(session_name
);
196 output
= lttng_snapshot_output_create();
203 ret
= lttng_snapshot_output_set_ctrl_url(ctrl_url
, output
);
210 ret
= lttng_snapshot_output_set_data_url(data_url
, output
);
216 /* This call, if successful, populates the id of the output object. */
217 ret
= lttng_snapshot_add_output(session_name
, output
);
223 lttng_snapshot_output_destroy(output
);
229 * Validate the combinations of passed options
232 * CMD_SUCCESS on success
234 static int validate_command_options(enum session_type type
)
236 int ret
= CMD_SUCCESS
;
238 if (opt_snapshot
&& opt_live_timer
) {
239 ERR("Snapshot and live modes are mutually exclusive.");
244 if ((!opt_ctrl_url
&& opt_data_url
) || (opt_ctrl_url
&& !opt_data_url
)) {
245 ERR("Control and data URLs must both be set.");
250 if (opt_template_path
) {
251 /* Restriction on flags exist when using template */
252 /* Session type flags are not permitted */
253 /* --live & --snapshot */
254 if ((opt_live_timer
&& type
!= SESSION_LIVE
) ||
255 (opt_snapshot
&& type
!= SESSION_SNAPSHOT
)) {
256 ERR("It is not possible to change the session type of a template");
262 if (opt_session_name
&& opt_default_name
) {
263 ERR("A session name must not be provided if the --default-name option is used");
272 * Create a session using direct calls to liblttng-ctl.
274 * Return CMD_SUCCESS on success, negative value on internal lttng errors and positive
275 * value on command errors.
277 static int create_session_basic (const char *session_name
,
278 enum session_type session_type
,
280 enum output_type output_type
,
282 const char* ctrl_url
,
283 const char* data_url
,
284 const char* shm_path
,
285 const char* datetime
)
287 /* Create session based on session creation */
288 int ret
= CMD_SUCCESS
;
289 const char *pathname
;
293 if (opt_relayd_path
) {
294 pathname
= opt_relayd_path
;
296 pathname
= INSTALL_BIN_PATH
"/lttng-relayd";
299 switch (session_type
) {
301 ret
= _lttng_create_session_ext(session_name
, url
, datetime
);
303 case SESSION_SNAPSHOT
:
304 if (output_type
== OUTPUT_NONE
) {
305 ERR("--no-output on a snapshot session is invalid");
306 ret
= CMD_UNSUPPORTED
;
309 ret
= lttng_create_session_snapshot(session_name
, url
);
312 if (output_type
== OUTPUT_NONE
) {
313 ERR("--no-output on a live session is invalid");
314 ret
= CMD_UNSUPPORTED
;
318 if (output_type
== OUTPUT_LOCAL
) {
319 ERR("Local file output on a live session is invalid");
320 ret
= CMD_UNSUPPORTED
;
323 if (output_type
!= OUTPUT_NET
&& !check_relayd() &&
324 spawn_relayd(pathname
, 0) < 0) {
328 ret
= lttng_create_session_live(session_name
, url
, live_timer
);
331 ERR("Unknown session type");
337 /* Don't set ret so the sessiond error is propagated. */
339 case LTTNG_ERR_EXIST_SESS
:
340 WARN("Session %s already exists", session_name
);
348 /* Configure the session based on the output type */
349 switch (output_type
) {
353 if (!ctrl_url
|| !data_url
) {
357 if (session_type
== SESSION_SNAPSHOT
) {
358 ret
= add_snapshot_output(session_name
, ctrl_url
,
362 * Normal sessions and live sessions behave the same way
363 * regarding consumer url.
365 ret
= set_consumer_url(session_name
, ctrl_url
, data_url
);
368 /* Destroy created session on errors */
369 lttng_destroy_session(session_name
);
376 ERR("Unknown output type");
382 * Set the session shared memory path
385 ret
= lttng_set_session_shm_path(session_name
, shm_path
);
387 lttng_destroy_session(session_name
);
395 static int generate_output(const char *session_name
,
396 enum session_type session_type
,
398 enum output_type output_type
,
400 const char* ctrl_url
,
401 const char* data_url
,
402 const char* shm_path
)
404 int ret
= CMD_SUCCESS
;
407 * TODO move this to after session name
408 * for now we only emulate previous behaviour.
410 if (session_type
!= SESSION_SNAPSHOT
) {
412 MSG("Control URL %s set for session %s", ctrl_url
, session_name
);
416 MSG("Data URL %s set for session %s", data_url
, session_name
);
420 if (url
&& output_type
== OUTPUT_LOCAL
) {
421 /* Remove the file:// */
422 if (strlen(url
) > strlen("file://")){
423 url
= url
+ strlen("file://");
427 MSG("Session %s created.", session_name
);
428 if (url
&& session_type
!= SESSION_SNAPSHOT
) {
429 MSG("Traces will be written in %s", url
);
432 MSG("Live timer set to %u usec", live_timer
);
434 } else if (session_type
== SESSION_SNAPSHOT
) {
436 MSG("Default snapshot output set to: %s", url
);
438 MSG("Snapshot mode set. Every channel enabled for that session will "
439 "be set to mmap output, and default to overwrite mode.");
443 MSG("Session %s set to shm_path: %s.", session_name
,
449 ret
= mi_created_session(session_name
);
459 static int parse_template (struct config_document
*template,
461 enum session_type
*session_type
,
463 enum output_type
*output_type
,
471 char *raw_value
= NULL
;
476 *session_name
= config_document_get_element_value(template,"/sessions/session/name");
478 /* Check the type of session we have in the template */
479 if(config_document_element_exist(template, "/sessions/session/attributes/snapshot_mode")) {
480 *session_type
= SESSION_SNAPSHOT
;
481 } else if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
482 *session_type
= SESSION_LIVE
;
483 raw_value
= config_document_get_element_value(template,"/sessions/session/attributes/live_timer_interval");
484 *live_timer
= config_parse_value(raw_value
);
488 *session_type
= SESSION_NORMAL
;
492 switch (*session_type
) {
495 if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")){
498 if (config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/path")){
499 raw_value
= config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/path");
505 if (strlen(raw_value
) > 0) {
506 *output_type
= OUTPUT_LOCAL
;
507 printed_bytes
= asprintf(url
, "file://%s", raw_value
);
508 if (printed_bytes
< 0) {
513 *output_type
= OUTPUT_NONE
;
519 } else if(config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/net_output")) {
520 *ctrl_url
= config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/control_uri");
521 *data_url
= config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/data_uri");
522 if (!*ctrl_url
|| ! *data_url
) {
526 *output_type
= OUTPUT_NET
;
528 /* There is no output definition */
531 case SESSION_SNAPSHOT
:
532 if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")){
535 if (config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path")){
536 raw_value
= config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path");
542 if (strlen(raw_value
) > 0) {
543 *output_type
= OUTPUT_LOCAL
;
544 printed_bytes
= asprintf(url
, "file://%s", raw_value
);
545 if (printed_bytes
< 0) {
550 *output_type
= OUTPUT_NONE
;
556 } else if(config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output")) {
557 *ctrl_url
= config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/control_uri");
558 *data_url
= config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/data_uri");
559 if (!*ctrl_url
|| ! *data_url
) {
563 *output_type
= OUTPUT_NET
;
565 /* There is no output definition */
568 case SESSION_UNKNOWN
:
574 /* shared memory path */
575 *shm_path
= config_document_get_element_value(template,"/sessions/session/shared_memory_path");
582 static int create_session_from_template(struct config_document
*template,
583 const char *session_name
,
584 enum session_type session_type
,
586 enum output_type output_type
,
588 const char *ctrl_url
,
589 const char *data_url
,
590 const char *shm_path
,
591 const char *datetime
)
593 int ret
= CMD_SUCCESS
;
595 struct config_element
*temp_element
= NULL
;
596 struct config_element
*temp_element_child
= NULL
;
597 char tmp_ctrl_uri
[PATH_MAX
];
598 char tmp_data_uri
[PATH_MAX
];
599 struct lttng_uri
*uris
= NULL
;
600 ssize_t uri_array_size
= 0;
601 char *tmp_string
= NULL
;
604 assert(session_name
);
606 memset(tmp_ctrl_uri
, 0, sizeof(tmp_ctrl_uri
));
607 memset(tmp_data_uri
, 0, sizeof(tmp_data_uri
));
610 if (config_document_element_exist(template, "/sessions/session/name")) {
611 /* Replace the node value */
612 config_document_replace_element_value(template, "/sessions/session/name", session_name
);
614 /* insert the node */
615 temp_element
= config_element_create("name", session_name
);
617 ERR("Could not create session name node configuration");
621 ret
= config_document_insert_element(template, "/sessions/session", temp_element
);
623 ERR("Could not insert session name node configuration");
627 config_element_free(temp_element
);
633 if (session_type
== SESSION_LIVE
) {
634 if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
635 printed_bytes
= asprintf(&tmp_string
, "%d", live_timer
);
636 if (printed_bytes
< 0) {
637 ERR("Asprintf failed for live timer");
641 ret
= config_document_replace_element_value(template, "/sessions/session/attributes/live_timer_interval", tmp_string
);
644 printf("error: %d\n", ret
);
645 ERR("Replacement of live_timer_interval failed");
653 ERR("Invalid live timer template. Missing live timer node");
661 * Generate the output node
664 /* Get output from urls */
666 /* Get lttng uris from single url */
667 uri_array_size
= uri_parse_str_urls(url
, NULL
, &uris
);
668 if (uri_array_size
< 0) {
672 } else if (ctrl_url
&& data_url
) {
673 uri_array_size
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
674 if (uri_array_size
< 0) {
683 /* Validate if the session output type still match the passed data */
684 if ( (uri_array_size
== 0 && output_type
!= OUTPUT_NONE
) ||
685 (uri_array_size
== 1 && output_type
!= OUTPUT_LOCAL
) ||
686 (uri_array_size
== 2 && output_type
!= OUTPUT_NET
)) {
687 ERR("Overwriting value for output do not match the base output type");
692 switch (output_type
) {
694 temp_element_child
= config_element_create("path", "");
695 if (!temp_element_child
) {
696 ERR("Could not create empty path node configuration");
702 temp_element_child
= config_element_create("path", uris
[0].dst
.path
);
703 if (!temp_element_child
) {
704 ERR("Could not create local path node configuration");
710 uri_to_str_url(&uris
[0], tmp_ctrl_uri
, sizeof(tmp_ctrl_uri
));
711 uri_to_str_url(&uris
[1], tmp_data_uri
, sizeof(tmp_data_uri
));
713 temp_element_child
= config_element_create("net_output", NULL
);
714 if (!temp_element_child
) {
715 ERR("Could not create net_output node configuration");
720 temp_element
= config_element_create("control_uri", tmp_ctrl_uri
);
721 if (!temp_element_child
) {
722 ERR("Could not create ctrl uri node configuration");
727 ret
= config_element_add_child(temp_element_child
, temp_element
);
729 ERR("Could not append control uri to the net_output node configuration");
734 config_element_free(temp_element
);
737 temp_element
= config_element_create("data_uri", tmp_data_uri
);
739 if (!temp_element_child
) {
740 ERR("Could not create data_uri configuration");
745 ret
= config_element_add_child(temp_element_child
, temp_element
);
747 ERR("Could not append data uri to the net_output node configuration");
751 config_element_free(temp_element
);
759 temp_element
= config_element_create("destination", NULL
);
761 ERR("Could not create destination node configuration");
766 ret
= config_element_add_child(temp_element
, temp_element_child
);
768 ERR("Could not append output data to the destination node configuration");
773 config_element_free(temp_element_child
);
774 temp_element_child
= NULL
;
776 /* Construct the output/consumer_output/ node*/
777 temp_element_child
= temp_element
;
778 temp_element
= config_element_create("consumer_output", NULL
);
780 ERR("Could not create consumer_output node configuration");
785 ret
= config_element_add_child(temp_element
, temp_element_child
);
787 ERR("Could not append output data to the consumer_output node configuration");
792 config_element_free(temp_element_child
);
793 temp_element_child
= config_element_create("enabled", "true");
794 if (!temp_element_child
) {
795 ERR("Could not create enbaled node configuration");
800 ret
= config_element_add_child(temp_element
, temp_element_child
);
802 ERR("Could not append node to the consumer_output node configuration");
807 config_element_free(temp_element_child
);
808 temp_element_child
= NULL
;
811 temp_element_child
= temp_element
;
812 temp_element
= config_element_create("output", NULL
);
814 ERR("Could not create output node configuration");
819 ret
= config_element_add_child(temp_element
, temp_element_child
);
821 ERR("Could not append output data to the output node configuration");
826 config_element_free(temp_element_child
);
827 temp_element_child
= NULL
;
831 * validate and replace the destination node for each session type
832 * TODO: export string as const and simply assign a base path for the
833 * destination node based on the session type
835 switch (session_type
) {
839 case SESSION_SNAPSHOT
:
840 /* construct the output/snapshots_outputs/ */
841 temp_element_child
= temp_element
;
842 temp_element
= config_element_create("snapshot_outputs", NULL
);
844 ERR("Could not create snapshot_outputs node configuration");
849 ret
= config_element_add_child(temp_element
, temp_element_child
);
851 ERR("Could not append output data to the snapshot_outputs node configuration");
856 config_element_free(temp_element_child
);
857 temp_element_child
= NULL
;
859 temp_element_child
= temp_element
;
860 temp_element
= config_element_create("output", NULL
);
862 ERR("Could not create output node configuration");
867 ret
= config_element_add_child(temp_element
, temp_element_child
);
869 ERR("Could not append output data to the output node configuration");
874 config_element_free(temp_element_child
);
875 temp_element_child
= NULL
;
878 ERR("Invalid session type");
883 if (!config_document_element_exist(template, "/sessions/session/output")) {
884 ret
= config_document_insert_element(template, "/sessions/session", temp_element
);
886 ret
= config_document_replace_element(template, "/sessions/session/output", temp_element
);
889 config_element_free(temp_element
);
893 ERR("%s", lttng_strerror(ret
));
900 if (shm_path
&& config_document_element_exist(template, "/sessions/session/shared_memory_path")) {
901 /* Replace the node value */
902 config_document_replace_element_value(template, "/sessions/session/shared_memory_path", shm_path
);
903 } else if (shm_path
) {
904 /* insert the node */
905 temp_element
= config_element_create("shared_memory_path", shm_path
);
907 ERR("Could not create shared_memory_path node configuration");
911 ret
= config_document_insert_element(template, "/sessions/session", temp_element
);
913 ERR("Could not insert shared_memory_path node configuration");
918 config_element_free(temp_element
);
922 ret
= config_load_configuration_sessions(template, session_name
, 0);
926 config_element_free(temp_element
);
927 config_element_free(temp_element_child
);
935 * Create a tracing session.
936 * If no name is specified, a default name is generated.
938 * Returns one of the CMD_* result constants.
940 static int create_session(void)
947 struct config_document
*template = NULL
;
950 enum session_type base_session_type
= SESSION_UNKNOWN
;
951 enum output_type base_output_type
= OUTPUT_UNKNOWN
;
952 char *base_session_name
= NULL
;
953 char *base_url
= NULL
;
954 char *base_ctrl_url
= NULL
;
955 char *base_data_url
= NULL
;
956 char *base_shm_path
= NULL
;
957 int base_live_timer
= 0;
962 struct tm
*timeinfo
= NULL
;
964 /* Temporary variables */
965 char *traces_path
= NULL
;
966 char *temp_url
= NULL
;
967 char *session_name_date
= NULL
;
968 char *tmp_url
= NULL
;
969 char *tmp_home_path
= NULL
;
970 struct lttng_uri
*uris
= NULL
;
971 ssize_t uri_array_size
= 0;
974 /* Get date and time for automatic session name/path */
976 timeinfo
= localtime(&rawtime
);
977 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
979 if (opt_template_path
) {
980 /* Restriction on flags exist when using template */
981 /* Session type flags are not permitted */
982 /* --live & --snapshot */
983 template = config_document_get(opt_template_path
);
985 ERR("Template could not be parsed");
989 /* Load info from template if any */
990 /* TODO: might want to use a struct in the end for the session... */
991 ret
= parse_template(template, &base_session_name
,
1004 /* Option validation */
1005 if (validate_command_options(base_session_type
) != CMD_SUCCESS
) {
1010 /* Find the session type based on options */
1011 if(base_session_type
== SESSION_UNKNOWN
) {
1013 base_session_type
= SESSION_SNAPSHOT
;
1014 } else if (opt_live_timer
) {
1015 base_session_type
= SESSION_LIVE
;
1017 base_session_type
= SESSION_NORMAL
;
1022 * Session name handling
1024 if (opt_session_name
) {
1025 /* Override the session name */
1026 if (strlen(opt_session_name
) > NAME_MAX
) {
1027 ERR("Session name too long. Length must be lower or equal to %d",
1029 ret
= LTTNG_ERR_SESSION_FAIL
;
1030 free(session_name_date
);
1034 * Check if the session name begins with "auto-" or is exactly "auto".
1035 * Both are reserved for the default session name. See bug #449 to
1036 * understand why we need to check both here.
1038 if ((strncmp(opt_session_name
, DEFAULT_SESSION_NAME
"-",
1039 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) ||
1040 (strncmp(opt_session_name
, DEFAULT_SESSION_NAME
,
1041 strlen(DEFAULT_SESSION_NAME
)) == 0 &&
1042 strlen(opt_session_name
) == strlen(DEFAULT_SESSION_NAME
))) {
1043 ERR("%s is a reserved keyword for default session(s)",
1044 DEFAULT_SESSION_NAME
);
1050 base_session_name
= strndup(opt_session_name
, NAME_MAX
);
1051 if (!base_session_name
) {
1052 PERROR("Strdup session name");
1057 printed_bytes
= asprintf(&session_name_date
, "%s-%s", base_session_name
, datetime
);
1058 if (printed_bytes
< 0) {
1059 PERROR("Asprintf session name");
1063 DBG("Session name from command option set to %s", base_session_name
);
1064 } else if (base_session_name
&& !opt_default_name
) {
1065 printed_bytes
= asprintf(&session_name_date
, "%s-%s", base_session_name
, datetime
);
1066 if (printed_bytes
< 0) {
1067 PERROR("Asprintf session name");
1072 /* Generate a name */
1073 printed_bytes
= asprintf(&base_session_name
, DEFAULT_SESSION_NAME
"-%s", datetime
);
1074 if (printed_bytes
< 0) {
1075 PERROR("Asprintf session name");
1079 session_name_date
= strdup(base_session_name
);
1080 DBG("Auto session name set to %s", base_session_name
);
1089 * If any of those options are present clear all output related data.
1091 if (opt_output_path
|| opt_url
|| (opt_ctrl_url
&& opt_data_url
) || opt_no_output
) {
1092 /* Overwrite output */
1094 free(base_ctrl_url
);
1095 free(base_data_url
);
1097 base_ctrl_url
= NULL
;
1098 base_data_url
= NULL
;
1101 if (opt_output_path
) {
1103 traces_path
= utils_expand_path(opt_output_path
);
1109 /* Create URL string from the local file system path */
1110 printed_bytes
= asprintf(&temp_url
, "file://%s", traces_path
);
1111 if (printed_bytes
< 0) {
1112 PERROR("asprintf url path");
1117 base_url
= temp_url
;
1118 } else if (opt_url
) { /* Handling URL (-U opt) */
1119 base_url
= strdup(opt_url
);
1120 } else if (opt_data_url
&& opt_ctrl_url
) {
1122 * With both control and data, we'll be setting the consumer URL
1123 * after session creation thus use no URL.
1125 base_ctrl_url
= strdup(opt_ctrl_url
);
1126 base_data_url
= strdup(opt_data_url
);
1127 } else if (!(opt_no_output
|| base_output_type
== OUTPUT_NONE
||
1128 base_url
|| base_ctrl_url
|| base_data_url
)) {
1129 /* Generate default output depending on the session type */
1130 switch (base_session_type
) {
1131 case SESSION_NORMAL
:
1133 case SESSION_SNAPSHOT
:
1134 /* Default to a local path */
1135 tmp_home_path
= utils_get_home_dir();
1136 if (tmp_home_path
== NULL
) {
1137 ERR("HOME path not found.\n \
1138 Please specify an output path using -o, --output PATH");
1143 printed_bytes
= asprintf(&tmp_url
,
1144 "file://%s/" DEFAULT_TRACE_DIR_NAME
"/%s",
1145 tmp_home_path
, session_name_date
);
1147 if (printed_bytes
< 0) {
1148 PERROR("asprintf trace dir name");
1153 base_url
= tmp_url
;
1156 /* Default to a net output */
1157 printed_bytes
= asprintf(&tmp_url
, "net://127.0.0.1");
1158 if (printed_bytes
< 0) {
1159 PERROR("asprintf default live URL");
1163 base_url
= tmp_url
;
1166 ERR("Unknown session type");
1173 * Shared memory path handling
1176 /* Overwrite shm_path so clear any previously defined one */
1177 free(base_shm_path
);
1178 printed_bytes
= asprintf(&base_shm_path
, "%s/%s", opt_shm_path
, session_name_date
);
1179 if (printed_bytes
< 0) {
1180 PERROR("asprintf shm_path");
1187 * Live timer handling
1189 if (opt_live_timer
) {
1190 base_live_timer
= opt_live_timer
;
1193 /* Get output type from urls */
1194 /* TODO: Find a better way of inferring output type */
1196 /* Get lttng uris from single url */
1197 uri_array_size
= uri_parse_str_urls(base_url
, NULL
, &uris
);
1198 if (uri_array_size
< 0) {
1202 } else if (base_ctrl_url
&& base_data_url
) {
1203 uri_array_size
= uri_parse_str_urls(base_ctrl_url
, base_data_url
, &uris
);
1204 if (uri_array_size
< 0) {
1213 switch (uri_array_size
) {
1215 base_output_type
= OUTPUT_NONE
;
1218 base_output_type
= OUTPUT_LOCAL
;
1221 base_output_type
= OUTPUT_NET
;
1229 ret
= create_session_from_template(template,
1240 ret
= create_session_basic(base_session_name
,
1254 ret
= generate_output(base_session_name
,
1266 /* Init lttng session config */
1267 ret
= config_init(base_session_name
);
1276 /* Session temp stuff */
1277 config_document_free(template);
1278 free(session_name_date
);
1283 ERR("%s", lttng_strerror(ret
));
1286 free(base_session_name
);
1288 free(base_ctrl_url
);
1289 free(base_data_url
);
1290 free(base_shm_path
);
1297 * Spawn a session daemon by forking and execv.
1299 static int spawn_sessiond(char *pathname
)
1304 MSG("Spawning a session daemon");
1308 * Spawn session daemon in daemon mode.
1310 execlp(pathname
, "lttng-sessiond",
1311 "--daemonize", NULL
);
1312 /* execlp only returns if error happened */
1313 if (errno
== ENOENT
) {
1314 ERR("No session daemon found. Use --sessiond-path.");
1318 kill(getppid(), SIGTERM
); /* wake parent */
1320 } else if (pid
> 0) {
1322 * In daemon mode (--daemonize), sessiond only exits when
1323 * it's ready to accept commands.
1327 pid_t wait_pid_ret
= waitpid(pid
, &status
, 0);
1329 if (wait_pid_ret
< 0) {
1330 if (errno
== EINTR
) {
1338 if (WIFSIGNALED(status
)) {
1339 ERR("Session daemon was killed by signal %d",
1343 } else if (WIFEXITED(status
)) {
1344 DBG("Session daemon terminated normally (exit status: %d)",
1345 WEXITSTATUS(status
));
1347 if (WEXITSTATUS(status
) != 0) {
1348 ERR("Session daemon terminated with an error (exit status: %d)",
1349 WEXITSTATUS(status
));
1371 * Check if the session daemon is available using
1372 * the liblttngctl API for the check. If not, try to
1375 static int launch_sessiond(void)
1378 char *pathname
= NULL
;
1380 ret
= lttng_session_daemon_alive();
1382 /* Sessiond is alive, not an error */
1387 /* Try command line option path */
1388 pathname
= opt_sessiond_path
;
1390 /* Try LTTNG_SESSIOND_PATH env variable */
1391 if (pathname
== NULL
) {
1392 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
1395 /* Try with configured path */
1396 if (pathname
== NULL
) {
1397 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
1398 pathname
= CONFIG_SESSIOND_BIN
;
1402 /* Try the default path */
1403 if (pathname
== NULL
) {
1404 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
1407 DBG("Session daemon binary path: %s", pathname
);
1409 /* Check existence and permissions */
1410 ret
= access(pathname
, F_OK
| X_OK
);
1412 ERR("No such file or access denied: %s", pathname
);
1416 ret
= spawn_sessiond(pathname
);
1419 ERR("Problem occurred while launching session daemon (%s)",
1426 * The 'create <options>' first level command
1428 * Returns one of the CMD_* result constants.
1430 int cmd_create(int argc
, const char **argv
)
1432 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1433 char *opt_arg
= NULL
;
1434 static poptContext pc
;
1436 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1437 poptReadDefaultConfig(pc
, 0);
1439 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1444 case OPT_LIST_OPTIONS
:
1445 list_cmd_options(stdout
, long_options
);
1447 case OPT_LIVE_TIMER
:
1452 opt_arg
= poptGetOptArg(pc
);
1454 /* Set up default values. */
1455 opt_live_timer
= (uint32_t) DEFAULT_LTTNG_LIVE_TIMER
;
1456 DBG("Session live timer interval set to default value %d",
1461 v
= strtoul(opt_arg
, NULL
, 0);
1462 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
1463 ERR("Wrong value in --live parameter: %s", opt_arg
);
1467 if (v
!= (uint32_t) v
) {
1468 ERR("32-bit overflow in --live parameter: %s", opt_arg
);
1473 ERR("Live timer interval must be greater than zero");
1477 opt_live_timer
= (uint32_t) v
;
1478 DBG("Session live timer interval set to %d", opt_live_timer
);
1481 case OPT_DEFAULT_NAME
:
1482 opt_default_name
= true;
1485 ret
= CMD_UNDEFINED
;
1490 if (opt_no_consumer
) {
1491 MSG("The option --no-consumer is obsolete. Use --no-output now.");
1496 /* Spawn a session daemon if needed */
1497 if (!opt_no_sessiond
) {
1498 ret
= launch_sessiond();
1505 /* MI initialization */
1507 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1509 ret
= -LTTNG_ERR_NOMEM
;
1513 /* Open command element */
1514 ret
= mi_lttng_writer_command_open(writer
,
1515 mi_lttng_element_command_create
);
1521 /* Open output element */
1522 ret
= mi_lttng_writer_open_element(writer
,
1523 mi_lttng_element_command_output
);
1529 opt_session_name
= (char*) poptGetArg(pc
);
1531 command_ret
= create_session();
1538 /* Close output element */
1539 ret
= mi_lttng_writer_close_element(writer
);
1546 ret
= mi_lttng_writer_write_element_bool(writer
,
1547 mi_lttng_element_command_success
, success
);
1553 /* Command element close */
1554 ret
= mi_lttng_writer_command_close(writer
);
1563 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1564 /* Preserve original error code */
1565 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1568 /* Overwrite ret if an error occurred in create_session() */
1569 ret
= command_ret
? command_ret
: ret
;
1571 poptFreeContext(pc
);