+/*
+ * Validate the combinations of passed options
+ *
+ * CMD_ERROR on error
+ * CMD_SUCCESS on success
+ */
+static int validate_command_options(void)
+{
+ int ret = CMD_SUCCESS;
+ if (opt_snapshot && opt_live_timer) {
+ ERR("Snapshot and live modes are mutually exclusive.");
+ ret = CMD_ERROR;
+ goto error;
+ }
+
+ if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) {
+ ERR("You need both control and data URL.");
+ ret = CMD_ERROR;
+ goto error;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Create a session via direct calls to liblttng-ctl.
+ *
+ * Return CMD_SUCCESS on success, negative value on internal lttng errors and positive
+ * value on command errors.
+ */
+static int create_session_basic (const char *session_name,
+ int session_type,
+ int live_timer,
+ int output_type,
+ const char* url,
+ const char* ctrl_url,
+ const char* data_url,
+ const char* shm_path,
+ const char* datetime)
+{
+ /* Create session based on session creation */
+ int ret = CMD_SUCCESS;
+ const char *pathname;
+
+ assert(datetime);
+
+ if (opt_relayd_path) {
+ pathname = opt_relayd_path;
+ } else {
+ pathname = INSTALL_BIN_PATH "/lttng-relayd";
+ }
+
+ switch (session_type) {
+ case SESSION_NORMAL:
+ ret = _lttng_create_session_ext(session_name, url, datetime);
+ break;
+ case SESSION_SNAPSHOT:
+ if (output_type == OUTPUT_NONE) {
+ ERR("--no-output on a snapshot session is invalid");
+ ret = CMD_UNSUPPORTED;
+ goto error;
+ }
+ ret = lttng_create_session_snapshot(session_name, url);
+ break;
+ case SESSION_LIVE:
+ if (output_type == OUTPUT_NONE) {
+ ERR("--no-output on a live session is invalid");
+ ret = CMD_UNSUPPORTED;
+ goto error;
+ }
+
+ if (output_type == OUTPUT_LOCAL) {
+ ERR("Local file output on a live session is invalid");
+ ret = CMD_UNSUPPORTED;
+ goto error;
+ }
+ if (output_type != OUTPUT_NET && !check_relayd() &&
+ spawn_relayd(pathname, 0) < 0) {
+ ret = CMD_FATAL;
+ goto error;
+ }
+ ret = lttng_create_session_live(session_name, url, live_timer);
+ break;
+ default:
+ ERR("Unknown session type");
+ ret = CMD_UNDEFINED;
+ goto error;
+ }
+
+ if (ret < 0) {
+ /* Don't set ret so lttng can interpret the sessiond error. */
+ switch (-ret) {
+ case LTTNG_ERR_EXIST_SESS:
+ WARN("Session %s already exists", session_name);
+ break;
+ default:
+ break;
+ }
+ goto error;
+ }
+
+ /* Configure the session based on the output type */
+ switch (output_type) {
+ case OUTPUT_LOCAL:
+ break;
+ case OUTPUT_NET:
+ if (session_type == SESSION_SNAPSHOT) {
+ ret = add_snapshot_output(session_name, ctrl_url,
+ data_url);
+ } else if (ctrl_url && data_url) {
+ /*
+ * Normal sessions and live sessions behave the same way
+ * regarding consumer url.
+ */
+ ret = set_consumer_url(session_name, ctrl_url, data_url);
+ }
+ if (ret < 0) {
+ /* Destroy created session on errors */
+ lttng_destroy_session(session_name);
+ goto error;
+ }
+ break;
+ case OUTPUT_NONE:
+ break;
+ default:
+ ERR("Unknown output type");
+ ret = CMD_UNDEFINED;
+ goto error;
+ }
+
+ /*
+ * Set the session shared memory path
+ */
+ if (shm_path) {
+ ret = lttng_set_session_shm_path(session_name, shm_path);
+ if (ret < 0) {
+ lttng_destroy_session(session_name);
+ goto error;
+ }
+ }
+error:
+ return ret;
+}
+
+static int generate_output(const char *session_name,
+ int session_type,
+ int live_timer,
+ int output_type,
+ const char* url,
+ const char* ctrl_url,
+ const char* data_url,
+ const char* shm_path)
+{
+ int ret = CMD_SUCCESS;
+
+ /*
+ * TODO move this to after session name
+ * for now we only emulate previous behaviour.
+ */
+ if (session_type != SESSION_SNAPSHOT) {
+ if (ctrl_url) {
+ MSG("Control URL %s set for session %s", ctrl_url, session_name);
+ }
+
+ if (data_url) {
+ MSG("Data URL %s set for session %s", data_url, session_name);
+ }
+ }
+
+ if (url && output_type == OUTPUT_LOCAL) {
+ /* Remove the file:// */
+ if (strlen(url) > strlen("file://")){
+ url = url + strlen("file://");
+ }
+ }
+
+ MSG("Session %s created.", session_name);
+ if (url && session_type != SESSION_SNAPSHOT) {
+ MSG("Traces will be written in %s", url);
+
+ if (live_timer) {
+ MSG("Live timer set to %u usec", live_timer);
+ }
+ } else if (session_type == SESSION_SNAPSHOT) {
+ if (url) {
+ MSG("Default snapshot output set to: %s", url);
+ }
+ MSG("Snapshot mode set. Every channel enabled for that session will "
+ "be set to mmap output, and default to overwrite mode.");
+ }
+
+ if (shm_path) {
+ MSG("Session %s set to shm_path: %s.", session_name,
+ shm_path);
+ }
+
+ /* Mi output */
+ if (lttng_opt_mi) {
+ ret = mi_created_session(session_name);
+ if (ret) {
+ ret = CMD_ERROR;
+ goto error;
+ }
+ }
+error:
+ return ret;
+}
+