+ *relayd_sock = sock;
+
+ return LTTCOMM_OK;
+
+close_sock:
+ if (sock) {
+ (void) relayd_close(sock);
+ }
+free_sock:
+ if (sock) {
+ lttcomm_destroy_sock(sock);
+ }
+error:
+ return ret;
+}
+
+/*
+ * Connect to the relayd using URI and send the socket to the right consumer.
+ */
+static int send_socket_relayd_consumer(int domain, struct ltt_session *session,
+ struct lttng_uri *relayd_uri, struct consumer_output *consumer,
+ int consumer_fd)
+{
+ int ret;
+ struct lttcomm_sock *sock = NULL;
+
+ /* Set the network sequence index if not set. */
+ if (consumer->net_seq_index == -1) {
+ /*
+ * Increment net_seq_idx because we are about to transfer the
+ * new relayd socket to the consumer.
+ */
+ uatomic_inc(&relayd_net_seq_idx);
+ /* Assign unique key so the consumer can match streams */
+ consumer->net_seq_index = uatomic_read(&relayd_net_seq_idx);
+ }
+
+ /* Connect to relayd and make version check if uri is the control. */
+ ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock);
+ if (ret != LTTCOMM_OK) {
+ goto close_sock;
+ }
+
+ /* If the control socket is connected, network session is ready */
+ if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
+ session->net_handle = 1;
+ }
+
+ switch (domain) {
+ case LTTNG_DOMAIN_KERNEL:
+ /* Send relayd socket to consumer. */
+ ret = kernel_consumer_send_relayd_socket(consumer_fd, sock,
+ consumer, relayd_uri->stype);
+ if (ret < 0) {
+ ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
+ goto close_sock;
+ }
+ break;
+ case LTTNG_DOMAIN_UST:
+ /* Send relayd socket to consumer. */
+ ret = ust_consumer_send_relayd_socket(consumer_fd, sock,
+ consumer, relayd_uri->stype);
+ if (ret < 0) {
+ ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
+ goto close_sock;
+ }
+ break;
+ }
+
+ ret = LTTCOMM_OK;
+
+ /*
+ * Close socket which was dup on the consumer side. The session daemon does
+ * NOT keep track of the relayd socket(s) once transfer to the consumer.
+ */
+
+close_sock:
+ if (sock) {
+ (void) relayd_close(sock);
+ lttcomm_destroy_sock(sock);
+ }
+
+ return ret;
+}
+
+/*
+ * Send both relayd sockets to a specific consumer and domain. This is a
+ * helper function to facilitate sending the information to the consumer for a
+ * session.
+ */
+static int send_sockets_relayd_consumer(int domain,
+ struct ltt_session *session, struct consumer_output *consumer, int fd)
+{
+ int ret;
+
+ /* 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;
+ }
+
+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;
+
+ assert(session);
+
+ usess = session->ust_session;
+ ksess = session->kernel_session;
+
+ DBG2("Setting relayd for session %s", session->name);
+
+ if (usess && usess->consumer->sock == -1 &&
+ usess->consumer->type == CONSUMER_DST_NET &&
+ usess->consumer->enabled) {
+ /* Setup relayd for 64 bits consumer */
+ if (ust_consumerd64_fd >= 0) {
+ send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session,
+ usess->consumer, ust_consumerd64_fd);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ }
+
+ /* Setup relayd for 32 bits consumer */
+ if (ust_consumerd32_fd >= 0) {
+ send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session,
+ usess->consumer, ust_consumerd32_fd);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ }
+ } else if (ksess && ksess->consumer->sock == -1 &&
+ ksess->consumer->type == CONSUMER_DST_NET &&
+ ksess->consumer->enabled) {
+ send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL, session,
+ ksess->consumer, ksess->consumer_fd);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ }
+
+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;
+
+ 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;
+ }
+
+ /* Append correct directory to subdir */
+ strncat(consumer->subdir, dir_name, sizeof(consumer->subdir));
+ DBG3("Copy session consumer subdir %s", consumer->subdir);
+
+ /* Add default trace directory name */
+ if (consumer->type == CONSUMER_DST_LOCAL) {
+ strncat(consumer->dst.trace_path, dir_name,
+ sizeof(consumer->dst.trace_path));
+ }
+
+ 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(session->consumer);
+
+ switch (domain->type) {
+ case LTTNG_DOMAIN_UST:
+ break;