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
;
76 static struct mi_writer
*writer
;
78 static struct poptOption long_options
[] = {
79 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
80 {"help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
81 {"output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, NULL
, NULL
},
82 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
83 {"set-url", 'U', POPT_ARG_STRING
, &opt_url
, 0, 0, 0},
84 {"ctrl-url", 'C', POPT_ARG_STRING
, &opt_ctrl_url
, 0, 0, 0},
85 {"data-url", 'D', POPT_ARG_STRING
, &opt_data_url
, 0, 0, 0},
86 {"no-output", 0, POPT_ARG_VAL
, &opt_no_output
, 1, 0, 0},
87 {"no-consumer", 0, POPT_ARG_VAL
, &opt_no_consumer
, 1, 0, 0},
88 {"snapshot", 0, POPT_ARG_VAL
, &opt_snapshot
, 1, 0, 0},
89 {"live", 0, POPT_ARG_INT
| POPT_ARGFLAG_OPTIONAL
, 0, OPT_LIVE_TIMER
, 0, 0},
90 {"shm-path", 0, POPT_ARG_STRING
, &opt_shm_path
, 0, 0, 0},
91 {"template-path", 0, POPT_ARG_STRING
, &opt_template_path
, 0, 0, 0},
96 * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on
97 * why this declaration exists and used ONLY in for this command.
99 extern int _lttng_create_session_ext(const char *name
, const char *url
,
100 const char *datetime
);
103 * Retrieve the created session and mi output it based on provided argument
104 * This is currently a summary of what was pretty printed and is subject to
107 static int mi_created_session(const char *session_name
)
109 int ret
, i
, count
, found
;
110 struct lttng_session
*sessions
;
112 /* session_name should not be null */
113 assert(session_name
);
116 count
= lttng_list_sessions(&sessions
);
119 ERR("%s", lttng_strerror(ret
));
124 ERR("Error session creation failed: session %s not found", session_name
);
125 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
130 for (i
= 0; i
< count
; i
++) {
131 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
133 ret
= mi_lttng_session(writer
, &sessions
[i
], 0);
142 ret
= -LTTNG_ERR_SESS_NOT_FOUND
;
154 * For a session name, set the consumer URLs.
156 static int set_consumer_url(const char *session_name
, const char *ctrl_url
,
157 const char *data_url
)
160 struct lttng_handle
*handle
;
162 assert(session_name
);
165 * Set handle with the session_name, but no domain. This implies that
166 * the actions taken with this handle apply on the tracing session
167 * rather then the domain-specific session.
169 handle
= lttng_create_handle(session_name
, NULL
);
170 if (handle
== NULL
) {
175 ret
= lttng_set_consumer_url(handle
, ctrl_url
, data_url
);
181 lttng_destroy_handle(handle
);
185 static int add_snapshot_output(const char *session_name
, const char *ctrl_url
,
186 const char *data_url
)
189 struct lttng_snapshot_output
*output
= NULL
;
191 assert(session_name
);
193 output
= lttng_snapshot_output_create();
200 ret
= lttng_snapshot_output_set_ctrl_url(ctrl_url
, output
);
207 ret
= lttng_snapshot_output_set_data_url(data_url
, output
);
213 /* This call, if successful, populates the id of the output object. */
214 ret
= lttng_snapshot_add_output(session_name
, output
);
220 lttng_snapshot_output_destroy(output
);
226 * Validate the combinations of passed options
229 * CMD_SUCCESS on success
231 static int validate_command_options(enum session_type type
)
233 int ret
= CMD_SUCCESS
;
235 if (opt_snapshot
&& opt_live_timer
) {
236 ERR("Snapshot and live modes are mutually exclusive.");
241 if ((!opt_ctrl_url
&& opt_data_url
) || (opt_ctrl_url
&& !opt_data_url
)) {
242 ERR("Control and data URLs must both be set.");
247 if (opt_template_path
) {
248 /* Restriction on flags exist when using template */
249 /* Session type flags are not permitted */
250 /* --live & --snapshot */
251 if ((opt_live_timer
&& type
!= SESSION_LIVE
) ||
252 (opt_snapshot
&& type
!= SESSION_SNAPSHOT
)) {
253 ERR("It is not possible to change the session type of a template");
264 * Create a session using direct calls to liblttng-ctl.
266 * Return CMD_SUCCESS on success, negative value on internal lttng errors and positive
267 * value on command errors.
269 static int create_session_basic (const char *session_name
,
270 enum session_type session_type
,
272 enum output_type output_type
,
274 const char* ctrl_url
,
275 const char* data_url
,
276 const char* shm_path
,
277 const char* datetime
)
279 /* Create session based on session creation */
280 int ret
= CMD_SUCCESS
;
281 const char *pathname
;
285 if (opt_relayd_path
) {
286 pathname
= opt_relayd_path
;
288 pathname
= INSTALL_BIN_PATH
"/lttng-relayd";
291 switch (session_type
) {
293 ret
= _lttng_create_session_ext(session_name
, url
, datetime
);
295 case SESSION_SNAPSHOT
:
296 if (output_type
== OUTPUT_NONE
) {
297 ERR("--no-output on a snapshot session is invalid");
298 ret
= CMD_UNSUPPORTED
;
301 ret
= lttng_create_session_snapshot(session_name
, url
);
304 if (output_type
== OUTPUT_NONE
) {
305 ERR("--no-output on a live session is invalid");
306 ret
= CMD_UNSUPPORTED
;
310 if (output_type
== OUTPUT_LOCAL
) {
311 ERR("Local file output on a live session is invalid");
312 ret
= CMD_UNSUPPORTED
;
315 if (output_type
!= OUTPUT_NET
&& !check_relayd() &&
316 spawn_relayd(pathname
, 0) < 0) {
320 ret
= lttng_create_session_live(session_name
, url
, live_timer
);
323 ERR("Unknown session type");
329 /* Don't set ret so the sessiond error is propagated. */
331 case LTTNG_ERR_EXIST_SESS
:
332 WARN("Session %s already exists", session_name
);
340 /* Configure the session based on the output type */
341 switch (output_type
) {
345 if (!ctrl_url
|| !data_url
) {
349 if (session_type
== SESSION_SNAPSHOT
) {
350 ret
= add_snapshot_output(session_name
, ctrl_url
,
354 * Normal sessions and live sessions behave the same way
355 * regarding consumer url.
357 ret
= set_consumer_url(session_name
, ctrl_url
, data_url
);
360 /* Destroy created session on errors */
361 lttng_destroy_session(session_name
);
368 ERR("Unknown output type");
374 * Set the session shared memory path
377 ret
= lttng_set_session_shm_path(session_name
, shm_path
);
379 lttng_destroy_session(session_name
);
387 static int generate_output(const char *session_name
,
388 enum session_type session_type
,
390 enum output_type output_type
,
392 const char* ctrl_url
,
393 const char* data_url
,
394 const char* shm_path
)
396 int ret
= CMD_SUCCESS
;
399 * TODO move this to after session name
400 * for now we only emulate previous behaviour.
402 if (session_type
!= SESSION_SNAPSHOT
) {
404 MSG("Control URL %s set for session %s", ctrl_url
, session_name
);
408 MSG("Data URL %s set for session %s", data_url
, session_name
);
412 if (url
&& output_type
== OUTPUT_LOCAL
) {
413 /* Remove the file:// */
414 if (strlen(url
) > strlen("file://")){
415 url
= url
+ strlen("file://");
419 MSG("Session %s created.", session_name
);
420 if (url
&& session_type
!= SESSION_SNAPSHOT
) {
421 MSG("Traces will be written in %s", url
);
424 MSG("Live timer set to %u usec", live_timer
);
426 } else if (session_type
== SESSION_SNAPSHOT
) {
428 MSG("Default snapshot output set to: %s", url
);
430 MSG("Snapshot mode set. Every channel enabled for that session will "
431 "be set to mmap output, and default to overwrite mode.");
435 MSG("Session %s set to shm_path: %s.", session_name
,
441 ret
= mi_created_session(session_name
);
451 static int parse_template (struct config_document
*template,
453 enum session_type
*session_type
,
455 enum output_type
*output_type
,
463 char *raw_value
= NULL
;
468 *session_name
= config_document_get_element_value(template,"/sessions/session/name");
470 /* Check the type of session we have in the template */
471 if(config_document_element_exist(template, "/sessions/session/attributes/snapshot_mode")) {
472 *session_type
= SESSION_SNAPSHOT
;
473 } else if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
474 *session_type
= SESSION_LIVE
;
475 raw_value
= config_document_get_element_value(template,"/sessions/session/attributes/live_timer_interval");
476 *live_timer
= config_parse_value(raw_value
);
480 *session_type
= SESSION_NORMAL
;
484 switch (*session_type
) {
487 if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")){
490 if (config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/path")){
491 raw_value
= config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/path");
497 if (strlen(raw_value
) > 0) {
498 *output_type
= OUTPUT_LOCAL
;
499 printed_bytes
= asprintf(url
, "file://%s", raw_value
);
500 if (printed_bytes
< 0) {
505 *output_type
= OUTPUT_NONE
;
511 } else if(config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/net_output")) {
512 *ctrl_url
= config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/control_uri");
513 *data_url
= config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/data_uri");
514 if (!*ctrl_url
|| ! *data_url
) {
518 *output_type
= OUTPUT_NET
;
520 /* There is no output definition */
523 case SESSION_SNAPSHOT
:
524 if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")){
527 if (config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path")){
528 raw_value
= config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path");
534 if (strlen(raw_value
) > 0) {
535 *output_type
= OUTPUT_LOCAL
;
536 printed_bytes
= asprintf(url
, "file://%s", raw_value
);
537 if (printed_bytes
< 0) {
542 *output_type
= OUTPUT_NONE
;
548 } else if(config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output")) {
549 *ctrl_url
= config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/control_uri");
550 *data_url
= config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/data_uri");
551 if (!*ctrl_url
|| ! *data_url
) {
555 *output_type
= OUTPUT_NET
;
557 /* There is no output definition */
560 case SESSION_UNKNOWN
:
566 /* shared memory path */
567 *shm_path
= config_document_get_element_value(template,"/sessions/session/shared_memory_path");
574 static int create_session_from_template(struct config_document
*template,
575 const char *session_name
,
576 enum session_type session_type
,
578 enum output_type output_type
,
580 const char *ctrl_url
,
581 const char *data_url
,
582 const char *shm_path
,
583 const char *datetime
)
585 int ret
= CMD_SUCCESS
;
587 struct config_element
*temp_element
= NULL
;
588 struct config_element
*temp_element_child
= NULL
;
589 char tmp_ctrl_uri
[PATH_MAX
];
590 char tmp_data_uri
[PATH_MAX
];
591 struct lttng_uri
*uris
= NULL
;
592 ssize_t uri_array_size
= 0;
593 char *tmp_string
= NULL
;
596 assert(session_name
);
598 memset(tmp_ctrl_uri
, 0, sizeof(tmp_ctrl_uri
));
599 memset(tmp_data_uri
, 0, sizeof(tmp_data_uri
));
602 if (config_document_element_exist(template, "/sessions/session/name")) {
603 /* Replace the node value */
604 config_document_replace_element_value(template, "/sessions/session/name", session_name
);
606 /* insert the node */
607 temp_element
= config_element_create("name", session_name
);
609 ERR("Could not create session name node configuration");
613 ret
= config_document_insert_element(template, "/sessions/session", temp_element
);
615 ERR("Could not insert session name node configuration");
619 config_element_free(temp_element
);
625 if (session_type
== SESSION_LIVE
) {
626 if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
627 printed_bytes
= asprintf(&tmp_string
, "%d", live_timer
);
628 if (printed_bytes
< 0) {
629 ERR("Asprintf failed for live timer");
633 ret
= config_document_replace_element_value(template, "/sessions/session/attributes/live_timer_interval", tmp_string
);
636 printf("error: %d\n", ret
);
637 ERR("Replacement of live_timer_interval failed");
645 ERR("Invalid live timer template. Missing live timer node");
653 * Generate the output node
656 /* Get output from urls */
658 /* Get lttng uris from single url */
659 uri_array_size
= uri_parse_str_urls(url
, NULL
, &uris
);
660 if (uri_array_size
< 0) {
664 } else if (ctrl_url
&& data_url
) {
665 uri_array_size
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
666 if (uri_array_size
< 0) {
675 /* Validate if the session output type still match the passed data */
676 if ( (uri_array_size
== 0 && output_type
!= OUTPUT_NONE
) ||
677 (uri_array_size
== 1 && output_type
!= OUTPUT_LOCAL
) ||
678 (uri_array_size
== 2 && output_type
!= OUTPUT_NET
)) {
679 ERR("Overwriting value for output do not match the base output type");
684 switch (output_type
) {
686 temp_element_child
= config_element_create("path", "");
687 if (!temp_element_child
) {
688 ERR("Could not create empty path node configuration");
694 temp_element_child
= config_element_create("path", uris
[0].dst
.path
);
695 if (!temp_element_child
) {
696 ERR("Could not create local path node configuration");
702 uri_to_str_url(&uris
[0], tmp_ctrl_uri
, sizeof(tmp_ctrl_uri
));
703 uri_to_str_url(&uris
[1], tmp_data_uri
, sizeof(tmp_data_uri
));
705 temp_element_child
= config_element_create("net_output", NULL
);
706 if (!temp_element_child
) {
707 ERR("Could not create net_output node configuration");
712 temp_element
= config_element_create("control_uri", tmp_ctrl_uri
);
713 if (!temp_element_child
) {
714 ERR("Could not create ctrl uri node configuration");
719 ret
= config_element_add_child(temp_element_child
, temp_element
);
721 ERR("Could not append control uri to the net_output node configuration");
726 config_element_free(temp_element
);
729 temp_element
= config_element_create("data_uri", tmp_data_uri
);
731 if (!temp_element_child
) {
732 ERR("Could not create data_uri configuration");
737 ret
= config_element_add_child(temp_element_child
, temp_element
);
739 ERR("Could not append data uri to the net_output node configuration");
743 config_element_free(temp_element
);
751 temp_element
= config_element_create("destination", NULL
);
753 ERR("Could not create destination node configuration");
758 ret
= config_element_add_child(temp_element
, temp_element_child
);
760 ERR("Could not append output data to the destination node configuration");
765 config_element_free(temp_element_child
);
766 temp_element_child
= NULL
;
768 /* Construct the output/consumer_output/ node*/
769 temp_element_child
= temp_element
;
770 temp_element
= config_element_create("consumer_output", NULL
);
772 ERR("Could not create consumer_output node configuration");
777 ret
= config_element_add_child(temp_element
, temp_element_child
);
779 ERR("Could not append output data to the consumer_output node configuration");
784 config_element_free(temp_element_child
);
785 temp_element_child
= config_element_create("enabled", "true");
786 if (!temp_element_child
) {
787 ERR("Could not create enbaled node configuration");
792 ret
= config_element_add_child(temp_element
, temp_element_child
);
794 ERR("Could not append node to the consumer_output node configuration");
799 config_element_free(temp_element_child
);
800 temp_element_child
= NULL
;
803 temp_element_child
= temp_element
;
804 temp_element
= config_element_create("output", NULL
);
806 ERR("Could not create output node configuration");
811 ret
= config_element_add_child(temp_element
, temp_element_child
);
813 ERR("Could not append output data to the output node configuration");
818 config_element_free(temp_element_child
);
819 temp_element_child
= NULL
;
823 * validate and replace the destination node for each session type
824 * TODO: export string as const and simply assign a base path for the
825 * destination node based on the session type
827 switch (session_type
) {
831 case SESSION_SNAPSHOT
:
832 /* construct the output/snapshots_outputs/ */
833 temp_element_child
= temp_element
;
834 temp_element
= config_element_create("snapshot_outputs", NULL
);
836 ERR("Could not create snapshot_outputs node configuration");
841 ret
= config_element_add_child(temp_element
, temp_element_child
);
843 ERR("Could not append output data to the snapshot_outputs node configuration");
848 config_element_free(temp_element_child
);
849 temp_element_child
= NULL
;
851 temp_element_child
= temp_element
;
852 temp_element
= config_element_create("output", NULL
);
854 ERR("Could not create output node configuration");
859 ret
= config_element_add_child(temp_element
, temp_element_child
);
861 ERR("Could not append output data to the output node configuration");
866 config_element_free(temp_element_child
);
867 temp_element_child
= NULL
;
870 ERR("Invalid session type");
875 if (!config_document_element_exist(template, "/sessions/session/output")) {
876 ret
= config_document_insert_element(template, "/sessions/session", temp_element
);
878 ret
= config_document_replace_element(template, "/sessions/session/output", temp_element
);
881 config_element_free(temp_element
);
885 ERR("%s", lttng_strerror(ret
));
892 if (shm_path
&& config_document_element_exist(template, "/sessions/session/shared_memory_path")) {
893 /* Replace the node value */
894 config_document_replace_element_value(template, "/sessions/session/shared_memory_path", shm_path
);
895 } else if (shm_path
) {
896 /* insert the node */
897 temp_element
= config_element_create("shared_memory_path", shm_path
);
899 ERR("Could not create shared_memory_path node configuration");
903 ret
= config_document_insert_element(template, "/sessions/session", temp_element
);
905 ERR("Could not insert shared_memory_path node configuration");
910 config_element_free(temp_element
);
914 ret
= config_load_configuration_sessions(template, session_name
, 0);
918 config_element_free(temp_element
);
919 config_element_free(temp_element_child
);
927 * Create a tracing session.
928 * If no name is specified, a default name is generated.
930 * Returns one of the CMD_* result constants.
932 static int create_session(void)
939 struct config_document
*template = NULL
;
942 enum session_type base_session_type
= SESSION_UNKNOWN
;
943 enum output_type base_output_type
= OUTPUT_UNKNOWN
;
944 char *base_session_name
= NULL
;
945 char *base_url
= NULL
;
946 char *base_ctrl_url
= NULL
;
947 char *base_data_url
= NULL
;
948 char *base_shm_path
= NULL
;
949 int base_live_timer
= 0;
954 struct tm
*timeinfo
= NULL
;
956 /* Temporary variables */
957 char *traces_path
= NULL
;
958 char *temp_url
= NULL
;
959 char *session_name_date
= NULL
;
960 char *tmp_url
= NULL
;
961 char *tmp_home_path
= NULL
;
962 struct lttng_uri
*uris
= NULL
;
963 ssize_t uri_array_size
= 0;
966 /* Get date and time for automatic session name/path */
968 timeinfo
= localtime(&rawtime
);
969 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
971 if (opt_template_path
) {
972 /* Restriction on flags exist when using template */
973 /* Session type flags are not permitted */
974 /* --live & --snapshot */
975 template = config_document_get(opt_template_path
);
977 ERR("Template could not be parsed");
981 /* Load info from template if any */
982 /* TODO: might want to use a struct in the end for the session... */
983 ret
= parse_template(template, &base_session_name
,
996 /* Option validation */
997 if (validate_command_options(base_session_type
) != CMD_SUCCESS
) {
1002 /* Find the session type based on options */
1003 if(base_session_type
== SESSION_UNKNOWN
) {
1005 base_session_type
= SESSION_SNAPSHOT
;
1006 } else if (opt_live_timer
) {
1007 base_session_type
= SESSION_LIVE
;
1009 base_session_type
= SESSION_NORMAL
;
1014 * Session name handling
1016 if (opt_session_name
) {
1017 /* Override the session name */
1018 if (strlen(opt_session_name
) > NAME_MAX
) {
1019 ERR("Session name too long. Length must be lower or equal to %d",
1021 ret
= LTTNG_ERR_SESSION_FAIL
;
1022 free(session_name_date
);
1026 * Check if the session name begins with "auto-" or is exactly "auto".
1027 * Both are reserved for the default session name. See bug #449 to
1028 * understand why we need to check both here.
1030 if ((strncmp(opt_session_name
, DEFAULT_SESSION_NAME
"-",
1031 strlen(DEFAULT_SESSION_NAME
) + 1) == 0) ||
1032 (strncmp(opt_session_name
, DEFAULT_SESSION_NAME
,
1033 strlen(DEFAULT_SESSION_NAME
)) == 0 &&
1034 strlen(opt_session_name
) == strlen(DEFAULT_SESSION_NAME
))) {
1035 ERR("%s is a reserved keyword for default session(s)",
1036 DEFAULT_SESSION_NAME
);
1042 base_session_name
= strndup(opt_session_name
, NAME_MAX
);
1043 if (!base_session_name
) {
1044 PERROR("Strdup session name");
1049 printed_bytes
= asprintf(&session_name_date
, "%s-%s", base_session_name
, datetime
);
1050 if (printed_bytes
< 0) {
1051 PERROR("Asprintf session name");
1055 DBG("Session name from command option set to %s", base_session_name
);
1056 } else if (base_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");
1064 /* Generate a name */
1065 printed_bytes
= asprintf(&base_session_name
, DEFAULT_SESSION_NAME
"-%s", datetime
);
1066 if (printed_bytes
< 0) {
1067 PERROR("Asprintf session name");
1071 session_name_date
= strdup(base_session_name
);
1072 DBG("Auto session name set to %s", base_session_name
);
1081 * If any of those options are present clear all output related data.
1083 if (opt_output_path
|| opt_url
|| (opt_ctrl_url
&& opt_data_url
) || opt_no_output
) {
1084 /* Overwrite output */
1086 free(base_ctrl_url
);
1087 free(base_data_url
);
1089 base_ctrl_url
= NULL
;
1090 base_data_url
= NULL
;
1093 if (opt_output_path
) {
1095 traces_path
= utils_expand_path(opt_output_path
);
1101 /* Create URL string from the local file system path */
1102 printed_bytes
= asprintf(&temp_url
, "file://%s", traces_path
);
1103 if (printed_bytes
< 0) {
1104 PERROR("asprintf url path");
1109 base_url
= temp_url
;
1110 } else if (opt_url
) { /* Handling URL (-U opt) */
1111 base_url
= strdup(opt_url
);
1112 } else if (opt_data_url
&& opt_ctrl_url
) {
1114 * With both control and data, we'll be setting the consumer URL
1115 * after session creation thus use no URL.
1117 base_ctrl_url
= strdup(opt_ctrl_url
);
1118 base_data_url
= strdup(opt_data_url
);
1119 } else if (!(opt_no_output
|| base_output_type
== OUTPUT_NONE
||
1120 base_url
|| base_ctrl_url
|| base_data_url
)) {
1121 /* Generate default output depending on the session type */
1122 switch (base_session_type
) {
1123 case SESSION_NORMAL
:
1125 case SESSION_SNAPSHOT
:
1126 /* Default to a local path */
1127 tmp_home_path
= utils_get_home_dir();
1128 if (tmp_home_path
== NULL
) {
1129 ERR("HOME path not found.\n \
1130 Please specify an output path using -o, --output PATH");
1135 printed_bytes
= asprintf(&tmp_url
,
1136 "file://%s/" DEFAULT_TRACE_DIR_NAME
"/%s",
1137 tmp_home_path
, session_name_date
);
1139 if (printed_bytes
< 0) {
1140 PERROR("asprintf trace dir name");
1145 base_url
= tmp_url
;
1148 /* Default to a net output */
1149 printed_bytes
= asprintf(&tmp_url
, "net://127.0.0.1");
1150 if (printed_bytes
< 0) {
1151 PERROR("asprintf default live URL");
1155 base_url
= tmp_url
;
1158 ERR("Unknown session type");
1165 * Shared memory path handling
1168 /* Overwrite shm_path so clear any previously defined one */
1169 free(base_shm_path
);
1170 printed_bytes
= asprintf(&base_shm_path
, "%s/%s", opt_shm_path
, session_name_date
);
1171 if (printed_bytes
< 0) {
1172 PERROR("asprintf shm_path");
1179 * Live timer handling
1181 if (opt_live_timer
) {
1182 base_live_timer
= opt_live_timer
;
1185 /* Get output type from urls */
1186 /* TODO: Find a better way of inferring output type */
1188 /* Get lttng uris from single url */
1189 uri_array_size
= uri_parse_str_urls(base_url
, NULL
, &uris
);
1190 if (uri_array_size
< 0) {
1194 } else if (base_ctrl_url
&& base_data_url
) {
1195 uri_array_size
= uri_parse_str_urls(base_ctrl_url
, base_data_url
, &uris
);
1196 if (uri_array_size
< 0) {
1205 switch (uri_array_size
) {
1207 base_output_type
= OUTPUT_NONE
;
1210 base_output_type
= OUTPUT_LOCAL
;
1213 base_output_type
= OUTPUT_NET
;
1221 ret
= create_session_from_template(template,
1232 ret
= create_session_basic(base_session_name
,
1246 ret
= generate_output(base_session_name
,
1258 /* Init lttng session config */
1259 ret
= config_init(base_session_name
);
1268 /* Session temp stuff */
1269 config_document_free(template);
1270 free(session_name_date
);
1275 ERR("%s", lttng_strerror(ret
));
1278 free(base_session_name
);
1280 free(base_ctrl_url
);
1281 free(base_data_url
);
1282 free(base_shm_path
);
1289 * Spawn a session daemon by forking and execv.
1291 static int spawn_sessiond(char *pathname
)
1296 MSG("Spawning a session daemon");
1300 * Spawn session daemon in daemon mode.
1302 execlp(pathname
, "lttng-sessiond",
1303 "--daemonize", NULL
);
1304 /* execlp only returns if error happened */
1305 if (errno
== ENOENT
) {
1306 ERR("No session daemon found. Use --sessiond-path.");
1310 kill(getppid(), SIGTERM
); /* wake parent */
1312 } else if (pid
> 0) {
1314 * In daemon mode (--daemonize), sessiond only exits when
1315 * it's ready to accept commands.
1319 pid_t wait_pid_ret
= waitpid(pid
, &status
, 0);
1321 if (wait_pid_ret
< 0) {
1322 if (errno
== EINTR
) {
1330 if (WIFSIGNALED(status
)) {
1331 ERR("Session daemon was killed by signal %d",
1335 } else if (WIFEXITED(status
)) {
1336 DBG("Session daemon terminated normally (exit status: %d)",
1337 WEXITSTATUS(status
));
1339 if (WEXITSTATUS(status
) != 0) {
1340 ERR("Session daemon terminated with an error (exit status: %d)",
1341 WEXITSTATUS(status
));
1363 * Check if the session daemon is available using
1364 * the liblttngctl API for the check. If not, try to
1367 static int launch_sessiond(void)
1370 char *pathname
= NULL
;
1372 ret
= lttng_session_daemon_alive();
1374 /* Sessiond is alive, not an error */
1379 /* Try command line option path */
1380 pathname
= opt_sessiond_path
;
1382 /* Try LTTNG_SESSIOND_PATH env variable */
1383 if (pathname
== NULL
) {
1384 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
1387 /* Try with configured path */
1388 if (pathname
== NULL
) {
1389 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
1390 pathname
= CONFIG_SESSIOND_BIN
;
1394 /* Try the default path */
1395 if (pathname
== NULL
) {
1396 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
1399 DBG("Session daemon binary path: %s", pathname
);
1401 /* Check existence and permissions */
1402 ret
= access(pathname
, F_OK
| X_OK
);
1404 ERR("No such file or access denied: %s", pathname
);
1408 ret
= spawn_sessiond(pathname
);
1411 ERR("Problem occurred while launching session daemon (%s)",
1418 * The 'create <options>' first level command
1420 * Returns one of the CMD_* result constants.
1422 int cmd_create(int argc
, const char **argv
)
1424 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1425 char *opt_arg
= NULL
;
1426 static poptContext pc
;
1428 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1429 poptReadDefaultConfig(pc
, 0);
1431 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1436 case OPT_LIST_OPTIONS
:
1437 list_cmd_options(stdout
, long_options
);
1439 case OPT_LIVE_TIMER
:
1444 opt_arg
= poptGetOptArg(pc
);
1446 /* Set up default values. */
1447 opt_live_timer
= (uint32_t) DEFAULT_LTTNG_LIVE_TIMER
;
1448 DBG("Session live timer interval set to default value %d",
1453 v
= strtoul(opt_arg
, NULL
, 0);
1454 if (errno
!= 0 || !isdigit(opt_arg
[0])) {
1455 ERR("Wrong value in --live parameter: %s", opt_arg
);
1459 if (v
!= (uint32_t) v
) {
1460 ERR("32-bit overflow in --live parameter: %s", opt_arg
);
1465 ERR("Live timer interval must be greater than zero");
1469 opt_live_timer
= (uint32_t) v
;
1470 DBG("Session live timer interval set to %d", opt_live_timer
);
1474 ret
= CMD_UNDEFINED
;
1479 if (opt_no_consumer
) {
1480 MSG("The option --no-consumer is obsolete. Use --no-output now.");
1485 /* Spawn a session daemon if needed */
1486 if (!opt_no_sessiond
) {
1487 ret
= launch_sessiond();
1494 /* MI initialization */
1496 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1498 ret
= -LTTNG_ERR_NOMEM
;
1502 /* Open command element */
1503 ret
= mi_lttng_writer_command_open(writer
,
1504 mi_lttng_element_command_create
);
1510 /* Open output element */
1511 ret
= mi_lttng_writer_open_element(writer
,
1512 mi_lttng_element_command_output
);
1518 opt_session_name
= (char*) poptGetArg(pc
);
1520 command_ret
= create_session();
1527 /* Close output element */
1528 ret
= mi_lttng_writer_close_element(writer
);
1535 ret
= mi_lttng_writer_write_element_bool(writer
,
1536 mi_lttng_element_command_success
, success
);
1542 /* Command element close */
1543 ret
= mi_lttng_writer_command_close(writer
);
1552 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1553 /* Preserve original error code */
1554 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
1557 /* Overwrite ret if an error occurred in create_session() */
1558 ret
= command_ret
? command_ret
: ret
;
1560 poptFreeContext(pc
);