+ /* Create socket object from URI */
+ sock = lttcomm_alloc_sock_from_uri(uri);
+ if (sock == NULL) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ ret = lttcomm_create_sock(sock);
+ if (ret < 0) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ /* Connect to relayd so we can proceed with a session creation. */
+ ret = relayd_connect(sock);
+ if (ret < 0) {
+ ERR("Unable to reach lttng-relayd");
+ ret = LTTCOMM_RELAYD_SESSION_FAIL;
+ goto free_sock;
+ }
+
+ /* Create socket for control stream. */
+ if (uri->stype == LTTNG_STREAM_CONTROL) {
+ DBG3("Creating relayd stream socket from URI");
+
+ /* Check relayd version */
+ ret = relayd_version_check(sock, LTTNG_UST_COMM_MAJOR, 0);
+ if (ret < 0) {
+ ret = LTTCOMM_RELAYD_VERSION_FAIL;
+ goto close_sock;
+ }
+ } else if (uri->stype == LTTNG_STREAM_DATA) {
+ DBG3("Creating relayd data socket from URI");
+ } else {
+ /* Command is not valid */
+ ERR("Relayd invalid stream type: %d", uri->stype);
+ ret = LTTCOMM_INVALID;
+ goto close_sock;
+ }
+
+ *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;
+ }
+
+ /* Send relayd socket to consumer. */
+ ret = consumer_send_relayd_socket(consumer_fd, sock,
+ consumer, relayd_uri->stype);
+ if (ret < 0) {
+ ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
+ goto close_sock;
+ }
+
+ 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;
+
+ 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;