+ int ret;
+
+ assert(session);
+ assert(consumer);
+
+ /* Don't resend the sockets to the consumer. */
+ if (consumer->dst.net.relayd_socks_sent) {
+ ret = LTTCOMM_OK;
+ goto error;
+ }
+
+ /* Sending control relayd socket. */
+ ret = send_socket_relayd_consumer(domain, session,
+ &consumer->dst.net.control, consumer, fd);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ /* Sending data relayd socket. */
+ ret = send_socket_relayd_consumer(domain, session,
+ &consumer->dst.net.data, consumer, fd);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ /* Flag that all relayd sockets were sent to the consumer. */
+ consumer->dst.net.relayd_socks_sent = 1;
+
+error:
+ return ret;
+}
+
+/*
+ * Setup relayd connections for a tracing session. First creates the socket to
+ * the relayd and send them to the right domain consumer. Consumer type MUST be
+ * network.
+ */
+static int setup_relayd(struct ltt_session *session)
+{
+ int ret = LTTCOMM_OK;
+ struct ltt_ust_session *usess;
+ struct ltt_kernel_session *ksess;
+ struct consumer_socket *socket;
+ struct lttng_ht_iter iter;
+
+ assert(session);
+
+ usess = session->ust_session;
+ ksess = session->kernel_session;
+
+ DBG2("Setting relayd for session %s", session->name);
+
+ if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
+ && usess->consumer->enabled) {
+ /* For each consumer socket, send relayd sockets */
+ cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
+ socket, node.node) {
+ /* Code flow error */
+ assert(socket->fd >= 0);
+
+ pthread_mutex_lock(socket->lock);
+ send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session,
+ usess->consumer, socket->fd);
+ pthread_mutex_unlock(socket->lock);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ }
+ }
+
+ if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
+ && ksess->consumer->enabled) {
+ cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
+ socket, node.node) {
+ /* Code flow error */
+ assert(socket->fd >= 0);
+
+ pthread_mutex_lock(socket->lock);
+ send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL, session,
+ ksess->consumer, socket->fd);
+ pthread_mutex_unlock(socket->lock);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ }
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Set consumer subdirectory using the session name and a generated datetime if
+ * needed. This is appended to the current subdirectory.
+ */
+static int set_consumer_subdir(struct consumer_output *consumer,
+ const char *session_name)
+{
+ int ret = 0;
+ unsigned int have_default_name = 0;
+ char datetime[16], tmp_path[PATH_MAX];
+ time_t rawtime;
+ struct tm *timeinfo;
+
+ assert(consumer);
+ assert(session_name);
+
+ memset(tmp_path, 0, sizeof(tmp_path));
+
+ /* Flag if we have a default session. */
+ if (strncmp(session_name, DEFAULT_SESSION_NAME "-",
+ strlen(DEFAULT_SESSION_NAME) + 1) == 0) {
+ have_default_name = 1;
+ } else {
+ /* Get date and time for session path */
+ time(&rawtime);
+ timeinfo = localtime(&rawtime);
+ strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
+ }
+
+ if (have_default_name) {
+ ret = snprintf(tmp_path, sizeof(tmp_path),
+ "%s/%s", consumer->subdir, session_name);
+ } else {
+ ret = snprintf(tmp_path, sizeof(tmp_path),
+ "%s/%s-%s/", consumer->subdir, session_name, datetime);
+ }
+ if (ret < 0) {
+ PERROR("snprintf session name date");
+ goto error;
+ }
+
+ strncpy(consumer->subdir, tmp_path, sizeof(consumer->subdir));
+ DBG2("Consumer subdir set to %s", consumer->subdir);
+
+error:
+ return ret;
+}
+
+/*
+ * Copy consumer output from the tracing session to the domain session. The
+ * function also applies the right modification on a per domain basis for the
+ * trace files destination directory.
+ */
+static int copy_session_consumer(int domain, struct ltt_session *session)
+{
+ int ret;
+ const char *dir_name;
+ struct consumer_output *consumer;
+
+ assert(session);
+ assert(session->consumer);
+
+ switch (domain) {
+ case LTTNG_DOMAIN_KERNEL:
+ DBG3("Copying tracing session consumer output in kernel session");
+ session->kernel_session->consumer =
+ consumer_copy_output(session->consumer);
+ /* Ease our life a bit for the next part */
+ consumer = session->kernel_session->consumer;
+ dir_name = DEFAULT_KERNEL_TRACE_DIR;
+ break;
+ case LTTNG_DOMAIN_UST:
+ DBG3("Copying tracing session consumer output in UST session");
+ session->ust_session->consumer =
+ consumer_copy_output(session->consumer);
+ /* Ease our life a bit for the next part */
+ consumer = session->ust_session->consumer;
+ dir_name = DEFAULT_UST_TRACE_DIR;
+ break;
+ default:
+ ret = LTTCOMM_UNKNOWN_DOMAIN;
+ goto error;
+ }
+
+ ret = set_consumer_subdir(session->consumer, session->name);
+ if (ret < 0) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ /* Append correct directory to subdir */
+ strncat(consumer->subdir, dir_name, sizeof(consumer->subdir));
+ DBG3("Copy session consumer subdir %s", consumer->subdir);
+
+ ret = LTTCOMM_OK;
+
+error:
+ return ret;
+}
+
+/*
+ * Create an UST session and add it to the session ust list.
+ */
+static int create_ust_session(struct ltt_session *session,
+ struct lttng_domain *domain)
+{
+ int ret;
+ struct ltt_ust_session *lus = NULL;
+
+ assert(session);
+ assert(domain);
+ assert(session->consumer);
+
+ switch (domain->type) {
+ case LTTNG_DOMAIN_UST:
+ break;
+ default:
+ ERR("Unknown UST domain on create session %d", domain->type);
+ ret = LTTCOMM_UNKNOWN_DOMAIN;
+ goto error;
+ }
+
+ DBG("Creating UST session");
+
+ lus = trace_ust_create_session(session->path, session->id, domain);
+ if (lus == NULL) {
+ ret = LTTCOMM_UST_SESS_FAIL;
+ goto error;
+ }
+
+ lus->uid = session->uid;
+ lus->gid = session->gid;
+ session->ust_session = lus;
+
+ /* Copy session output to the newly created UST session */
+ ret = copy_session_consumer(domain->type, session);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ return LTTCOMM_OK;
+
+error:
+ free(lus);
+ session->ust_session = NULL;
+ return ret;
+}
+
+/*
+ * Create a kernel tracer session then create the default channel.
+ */
+static int create_kernel_session(struct ltt_session *session)
+{
+ int ret;
+
+ DBG("Creating kernel session");
+
+ ret = kernel_create_session(session, kernel_tracer_fd);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_SESS_FAIL;
+ goto error;
+ }
+
+ /* Code flow safety */
+ assert(session->kernel_session);
+
+ /* Copy session output to the newly created Kernel session */
+ ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ /* Create directory(ies) on local filesystem. */
+ if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
+ strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
+ ret = run_as_mkdir_recursive(
+ session->kernel_session->consumer->dst.trace_path,
+ S_IRWXU | S_IRWXG, session->uid, session->gid);
+ if (ret < 0) {
+ if (ret != -EEXIST) {
+ ERR("Trace directory creation error");
+ goto error;
+ }
+ }
+ }
+
+ session->kernel_session->uid = session->uid;
+ session->kernel_session->gid = session->gid;
+
+ return LTTCOMM_OK;
+
+error:
+ trace_kernel_destroy_session(session->kernel_session);
+ session->kernel_session = NULL;
+ return ret;
+}
+
+/*
+ * Check if the UID or GID match the session. Root user has access to all
+ * sessions.
+ */
+static int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
+{
+ if (uid != session->uid && gid != session->gid && uid != 0) {
+ return 0;
+ } else {
+ return 1;