Add lttng-error.h containing every API err. code
[lttng-tools.git] / src / common / consumer.c
index ae59b6b602909a2752dc142d857dbe1fb503c703..008bf8e54904d71158400216393f0fc6c5786e63 100644 (file)
@@ -1,20 +1,20 @@
 /*
  * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
  *                      Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *               2012 - David Goulet <dgoulet@efficios.com>
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2 only,
+ * as published by the Free Software Foundation.
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #define _GNU_SOURCE
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <inttypes.h>
 
 #include <common/common.h>
 #include <common/kernel-ctl/kernel-ctl.h>
+#include <common/sessiond-comm/relayd.h>
 #include <common/sessiond-comm/sessiond-comm.h>
 #include <common/kernel-consumer/kernel-consumer.h>
+#include <common/relayd/relayd.h>
 #include <common/ust-consumer/ust-consumer.h>
 
 #include "consumer.h"
@@ -85,9 +88,18 @@ static void consumer_steal_stream_key(int key)
 {
        struct lttng_consumer_stream *stream;
 
+       rcu_read_lock();
        stream = consumer_find_stream(key);
-       if (stream)
+       if (stream) {
                stream->key = -1;
+               /*
+                * We don't want the lookup to match, but we still need
+                * to iterate on this stream when iterating over the hash table. Just
+                * change the node key.
+                */
+               stream->node.key = -1;
+       }
+       rcu_read_unlock();
 }
 
 static struct lttng_consumer_channel *consumer_find_channel(int key)
@@ -118,9 +130,94 @@ static void consumer_steal_channel_key(int key)
 {
        struct lttng_consumer_channel *channel;
 
+       rcu_read_lock();
        channel = consumer_find_channel(key);
-       if (channel)
+       if (channel) {
                channel->key = -1;
+               /*
+                * We don't want the lookup to match, but we still need
+                * to iterate on this channel when iterating over the hash table. Just
+                * change the node key.
+                */
+               channel->node.key = -1;
+       }
+       rcu_read_unlock();
+}
+
+static
+void consumer_free_stream(struct rcu_head *head)
+{
+       struct lttng_ht_node_ulong *node =
+               caa_container_of(head, struct lttng_ht_node_ulong, head);
+       struct lttng_consumer_stream *stream =
+               caa_container_of(node, struct lttng_consumer_stream, node);
+
+       free(stream);
+}
+
+/*
+ * RCU protected relayd socket pair free.
+ */
+static void consumer_rcu_free_relayd(struct rcu_head *head)
+{
+       struct lttng_ht_node_ulong *node =
+               caa_container_of(head, struct lttng_ht_node_ulong, head);
+       struct consumer_relayd_sock_pair *relayd =
+               caa_container_of(node, struct consumer_relayd_sock_pair, node);
+
+       free(relayd);
+}
+
+/*
+ * Destroy and free relayd socket pair object.
+ *
+ * This function MUST be called with the consumer_data lock acquired.
+ */
+void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd)
+{
+       int ret;
+       struct lttng_ht_iter iter;
+
+       if (relayd == NULL) {
+               return;
+       }
+
+       DBG("Consumer destroy and close relayd socket pair");
+
+       iter.iter.node = &relayd->node.node;
+       ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
+       if (ret != 0) {
+               /* We assume the relayd was already destroyed */
+               return;
+       }
+
+       /* Close all sockets */
+       pthread_mutex_lock(&relayd->ctrl_sock_mutex);
+       (void) relayd_close(&relayd->control_sock);
+       pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
+       (void) relayd_close(&relayd->data_sock);
+
+       /* RCU free() call */
+       call_rcu(&relayd->node.head, consumer_rcu_free_relayd);
+}
+
+/*
+ * Flag a relayd socket pair for destruction. Destroy it if the refcount
+ * reaches zero.
+ *
+ * RCU read side lock MUST be aquired before calling this function.
+ */
+void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
+{
+       assert(relayd);
+
+       /* Set destroy flag for this object */
+       uatomic_set(&relayd->destroy_flag, 1);
+
+       /* Destroy the relayd if refcount is 0 */
+       if (uatomic_read(&relayd->refcount) == 0) {
+               consumer_destroy_relayd(relayd);
+       }
 }
 
 /*
@@ -132,6 +229,9 @@ void consumer_del_stream(struct lttng_consumer_stream *stream)
        int ret;
        struct lttng_ht_iter iter;
        struct lttng_consumer_channel *free_chan = NULL;
+       struct consumer_relayd_sock_pair *relayd;
+
+       assert(stream);
 
        pthread_mutex_lock(&consumer_data.lock);
 
@@ -155,11 +255,7 @@ void consumer_del_stream(struct lttng_consumer_stream *stream)
        }
 
        rcu_read_lock();
-
-       /* Get stream node from hash table */
-       lttng_ht_lookup(consumer_data.stream_ht,
-                       (void *)((unsigned long) stream->key), &iter);
-       /* Remove stream node from hash table */
+       iter.iter.node = &stream->node.node;
        ret = lttng_ht_del(consumer_data.stream_ht, &iter);
        assert(!ret);
 
@@ -173,17 +269,60 @@ void consumer_del_stream(struct lttng_consumer_stream *stream)
                goto end;
        }
        if (stream->out_fd >= 0) {
-               close(stream->out_fd);
+               ret = close(stream->out_fd);
+               if (ret) {
+                       PERROR("close");
+               }
        }
        if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
-               close(stream->wait_fd);
+               ret = close(stream->wait_fd);
+               if (ret) {
+                       PERROR("close");
+               }
        }
        if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
-               close(stream->shm_fd);
+               ret = close(stream->shm_fd);
+               if (ret) {
+                       PERROR("close");
+               }
+       }
+
+       /* Check and cleanup relayd */
+       rcu_read_lock();
+       relayd = consumer_find_relayd(stream->net_seq_idx);
+       if (relayd != NULL) {
+               uatomic_dec(&relayd->refcount);
+               assert(uatomic_read(&relayd->refcount) >= 0);
+
+               /* Closing streams requires to lock the control socket. */
+               pthread_mutex_lock(&relayd->ctrl_sock_mutex);
+               ret = relayd_send_close_stream(&relayd->control_sock,
+                               stream->relayd_stream_id,
+                               stream->next_net_seq_num - 1);
+               pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
+               if (ret < 0) {
+                       DBG("Unable to close stream on the relayd. Continuing");
+                       /*
+                        * Continue here. There is nothing we can do for the relayd.
+                        * Chances are that the relayd has closed the socket so we just
+                        * continue cleaning up.
+                        */
+               }
+
+               /* Both conditions are met, we destroy the relayd. */
+               if (uatomic_read(&relayd->refcount) == 0 &&
+                               uatomic_read(&relayd->destroy_flag)) {
+                       consumer_destroy_relayd(relayd);
+               }
        }
-       if (!--stream->chan->refcount)
+       rcu_read_unlock();
+
+       if (!--stream->chan->refcount) {
                free_chan = stream->chan;
-       free(stream);
+       }
+
+
+       call_rcu(&stream->node.head, consumer_free_stream);
 end:
        consumer_data.need_update = 1;
        pthread_mutex_unlock(&consumer_data.lock);
@@ -192,16 +331,6 @@ end:
                consumer_del_channel(free_chan);
 }
 
-static void consumer_del_stream_rcu(struct rcu_head *head)
-{
-       struct lttng_ht_node_ulong *node =
-               caa_container_of(head, struct lttng_ht_node_ulong, head);
-       struct lttng_consumer_stream *stream =
-               caa_container_of(node, struct lttng_consumer_stream, node);
-
-       consumer_del_stream(stream);
-}
-
 struct lttng_consumer_stream *consumer_allocate_stream(
                int channel_key, int stream_key,
                int shm_fd, int wait_fd,
@@ -210,7 +339,9 @@ struct lttng_consumer_stream *consumer_allocate_stream(
                enum lttng_event_output output,
                const char *path_name,
                uid_t uid,
-               gid_t gid)
+               gid_t gid,
+               int net_index,
+               int metadata_flag)
 {
        struct lttng_consumer_stream *stream;
        int ret;
@@ -237,9 +368,12 @@ struct lttng_consumer_stream *consumer_allocate_stream(
        stream->output = output;
        stream->uid = uid;
        stream->gid = gid;
-       strncpy(stream->path_name, path_name, PATH_MAX - 1);
-       stream->path_name[PATH_MAX - 1] = '\0';
+       stream->net_seq_idx = net_index;
+       stream->metadata_flag = metadata_flag;
+       strncpy(stream->path_name, path_name, sizeof(stream->path_name));
+       stream->path_name[sizeof(stream->path_name) - 1] = '\0';
        lttng_ht_node_init_ulong(&stream->node, stream->key);
+       lttng_ht_node_init_ulong(&stream->waitfd_node, stream->wait_fd);
 
        switch (consumer_data.type) {
        case LTTNG_CONSUMER_KERNEL:
@@ -258,12 +392,13 @@ struct lttng_consumer_stream *consumer_allocate_stream(
                assert(0);
                goto end;
        }
-       DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
+       DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d, net_seq_idx %d)",
                        stream->path_name, stream->key,
                        stream->shm_fd,
                        stream->wait_fd,
                        (unsigned long long) stream->mmap_len,
-                       stream->out_fd);
+                       stream->out_fd,
+                       stream->net_seq_idx);
 end:
        return stream;
 }
@@ -274,13 +409,34 @@ end:
 int consumer_add_stream(struct lttng_consumer_stream *stream)
 {
        int ret = 0;
+       struct lttng_ht_node_ulong *node;
+       struct lttng_ht_iter iter;
+       struct consumer_relayd_sock_pair *relayd;
 
        pthread_mutex_lock(&consumer_data.lock);
        /* Steal stream identifier, for UST */
        consumer_steal_stream_key(stream->key);
+
        rcu_read_lock();
+       lttng_ht_lookup(consumer_data.stream_ht,
+                       (void *)((unsigned long) stream->key), &iter);
+       node = lttng_ht_iter_get_node_ulong(&iter);
+       if (node != NULL) {
+               rcu_read_unlock();
+               /* Stream already exist. Ignore the insertion */
+               goto end;
+       }
+
        lttng_ht_add_unique_ulong(consumer_data.stream_ht, &stream->node);
+
+       /* Check and cleanup relayd */
+       relayd = consumer_find_relayd(stream->net_seq_idx);
+       if (relayd != NULL) {
+               uatomic_inc(&relayd->refcount);
+       }
        rcu_read_unlock();
+
+       /* Update consumer data */
        consumer_data.stream_count++;
        consumer_data.need_update = 1;
 
@@ -300,9 +456,146 @@ int consumer_add_stream(struct lttng_consumer_stream *stream)
 
 end:
        pthread_mutex_unlock(&consumer_data.lock);
+
+       return ret;
+}
+
+/*
+ * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
+ * be acquired before calling this.
+ */
+
+int consumer_add_relayd(struct consumer_relayd_sock_pair *relayd)
+{
+       int ret = 0;
+       struct lttng_ht_node_ulong *node;
+       struct lttng_ht_iter iter;
+
+       if (relayd == NULL) {
+               ret = -1;
+               goto end;
+       }
+
+       lttng_ht_lookup(consumer_data.relayd_ht,
+                       (void *)((unsigned long) relayd->net_seq_idx), &iter);
+       node = lttng_ht_iter_get_node_ulong(&iter);
+       if (node != NULL) {
+               /* Relayd already exist. Ignore the insertion */
+               goto end;
+       }
+       lttng_ht_add_unique_ulong(consumer_data.relayd_ht, &relayd->node);
+
+end:
        return ret;
 }
 
+/*
+ * Allocate and return a consumer relayd socket.
+ */
+struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
+               int net_seq_idx)
+{
+       struct consumer_relayd_sock_pair *obj = NULL;
+
+       /* Negative net sequence index is a failure */
+       if (net_seq_idx < 0) {
+               goto error;
+       }
+
+       obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
+       if (obj == NULL) {
+               PERROR("zmalloc relayd sock");
+               goto error;
+       }
+
+       obj->net_seq_idx = net_seq_idx;
+       obj->refcount = 0;
+       obj->destroy_flag = 0;
+       lttng_ht_node_init_ulong(&obj->node, obj->net_seq_idx);
+       pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
+
+error:
+       return obj;
+}
+
+/*
+ * Find a relayd socket pair in the global consumer data.
+ *
+ * Return the object if found else NULL.
+ * RCU read-side lock must be held across this call and while using the
+ * returned object.
+ */
+struct consumer_relayd_sock_pair *consumer_find_relayd(int key)
+{
+       struct lttng_ht_iter iter;
+       struct lttng_ht_node_ulong *node;
+       struct consumer_relayd_sock_pair *relayd = NULL;
+
+       /* Negative keys are lookup failures */
+       if (key < 0) {
+               goto error;
+       }
+
+       lttng_ht_lookup(consumer_data.relayd_ht, (void *)((unsigned long) key),
+                       &iter);
+       node = lttng_ht_iter_get_node_ulong(&iter);
+       if (node != NULL) {
+               relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
+       }
+
+error:
+       return relayd;
+}
+
+/*
+ * Handle stream for relayd transmission if the stream applies for network
+ * streaming where the net sequence index is set.
+ *
+ * Return destination file descriptor or negative value on error.
+ */
+static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
+               size_t data_size, struct consumer_relayd_sock_pair *relayd)
+{
+       int outfd = -1, ret;
+       struct lttcomm_relayd_data_hdr data_hdr;
+
+       /* Safety net */
+       assert(stream);
+       assert(relayd);
+
+       /* Reset data header */
+       memset(&data_hdr, 0, sizeof(data_hdr));
+
+       if (stream->metadata_flag) {
+               /* Caller MUST acquire the relayd control socket lock */
+               ret = relayd_send_metadata(&relayd->control_sock, data_size);
+               if (ret < 0) {
+                       goto error;
+               }
+
+               /* Metadata are always sent on the control socket. */
+               outfd = relayd->control_sock.fd;
+       } else {
+               /* Set header with stream information */
+               data_hdr.stream_id = htobe64(stream->relayd_stream_id);
+               data_hdr.data_size = htobe32(data_size);
+               data_hdr.net_seq_num = htobe64(stream->next_net_seq_num++);
+               /* Other fields are zeroed previously */
+
+               ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
+                               sizeof(data_hdr));
+               if (ret < 0) {
+                       goto error;
+               }
+
+               /* Set to go on data socket */
+               outfd = relayd->data_sock.fd;
+       }
+
+error:
+       return outfd;
+}
+
 /*
  * Update a stream according to what we just received.
  */
@@ -320,6 +613,17 @@ void consumer_change_stream_state(int stream_key,
        pthread_mutex_unlock(&consumer_data.lock);
 }
 
+static
+void consumer_free_channel(struct rcu_head *head)
+{
+       struct lttng_ht_node_ulong *node =
+               caa_container_of(head, struct lttng_ht_node_ulong, head);
+       struct lttng_consumer_channel *channel =
+               caa_container_of(node, struct lttng_consumer_channel, node);
+
+       free(channel);
+}
+
 /*
  * Remove a channel from the global list protected by a mutex. This
  * function is also responsible for freeing its data structures.
@@ -345,12 +649,9 @@ void consumer_del_channel(struct lttng_consumer_channel *channel)
        }
 
        rcu_read_lock();
-
-       lttng_ht_lookup(consumer_data.channel_ht,
-                       (void *)((unsigned long) channel->key), &iter);
+       iter.iter.node = &channel->node.node;
        ret = lttng_ht_del(consumer_data.channel_ht, &iter);
        assert(!ret);
-
        rcu_read_unlock();
 
        if (channel->mmap_base != NULL) {
@@ -360,26 +661,23 @@ void consumer_del_channel(struct lttng_consumer_channel *channel)
                }
        }
        if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
-               close(channel->wait_fd);
+               ret = close(channel->wait_fd);
+               if (ret) {
+                       PERROR("close");
+               }
        }
        if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
-               close(channel->shm_fd);
+               ret = close(channel->shm_fd);
+               if (ret) {
+                       PERROR("close");
+               }
        }
-       free(channel);
+
+       call_rcu(&channel->node.head, consumer_free_channel);
 end:
        pthread_mutex_unlock(&consumer_data.lock);
 }
 
-static void consumer_del_channel_rcu(struct rcu_head *head)
-{
-       struct lttng_ht_node_ulong *node =
-               caa_container_of(head, struct lttng_ht_node_ulong, head);
-       struct lttng_consumer_channel *channel=
-               caa_container_of(node, struct lttng_consumer_channel, node);
-
-       consumer_del_channel(channel);
-}
-
 struct lttng_consumer_channel *consumer_allocate_channel(
                int channel_key,
                int shm_fd, int wait_fd,
@@ -422,9 +720,7 @@ struct lttng_consumer_channel *consumer_allocate_channel(
                goto end;
        }
        DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
-                       channel->key,
-                       channel->shm_fd,
-                       channel->wait_fd,
+                       channel->key, channel->shm_fd, channel->wait_fd,
                        (unsigned long long) channel->mmap_len,
                        (unsigned long long) channel->max_sb_size);
 end:
@@ -436,13 +732,28 @@ end:
  */
 int consumer_add_channel(struct lttng_consumer_channel *channel)
 {
+       struct lttng_ht_node_ulong *node;
+       struct lttng_ht_iter iter;
+
        pthread_mutex_lock(&consumer_data.lock);
        /* Steal channel identifier, for UST */
        consumer_steal_channel_key(channel->key);
        rcu_read_lock();
+
+       lttng_ht_lookup(consumer_data.channel_ht,
+                       (void *)((unsigned long) channel->key), &iter);
+       node = lttng_ht_iter_get_node_ulong(&iter);
+       if (node != NULL) {
+               /* Channel already exist. Ignore the insertion */
+               goto end;
+       }
+
        lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
+
+end:
        rcu_read_unlock();
        pthread_mutex_unlock(&consumer_data.lock);
+
        return 0;
 }
 
@@ -455,13 +766,15 @@ int consumer_add_channel(struct lttng_consumer_channel *channel)
  */
 int consumer_update_poll_array(
                struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
-               struct lttng_consumer_stream **local_stream)
+               struct lttng_consumer_stream **local_stream,
+               struct lttng_ht *metadata_ht)
 {
        int i = 0;
        struct lttng_ht_iter iter;
        struct lttng_consumer_stream *stream;
 
        DBG("Updating poll fd array");
+       rcu_read_lock();
        cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream,
                        node.node) {
                if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) {
@@ -470,16 +783,21 @@ int consumer_update_poll_array(
                DBG("Active FD %d", stream->wait_fd);
                (*pollfd)[i].fd = stream->wait_fd;
                (*pollfd)[i].events = POLLIN | POLLPRI;
+               if (stream->metadata_flag && metadata_ht) {
+                       lttng_ht_add_unique_ulong(metadata_ht, &stream->waitfd_node);
+                       DBG("Active FD added to metadata hash table");
+               }
                local_stream[i] = stream;
                i++;
        }
+       rcu_read_unlock();
 
        /*
         * Insert the consumer_poll_pipe at the end of the array and don't
         * increment i so nb_fd is the number of real FD.
         */
        (*pollfd)[i].fd = ctx->consumer_poll_pipe[0];
-       (*pollfd)[i].events = POLLIN;
+       (*pollfd)[i].events = POLLIN | POLLPRI;
        return i;
 }
 
@@ -503,7 +821,7 @@ restart:
                perror("Poll error");
                goto exit;
        }
-       if (consumer_sockpoll[0].revents == POLLIN) {
+       if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
                DBG("consumer_should_quit wake up");
                goto exit;
        }
@@ -525,7 +843,6 @@ void lttng_consumer_set_error_sock(
 /*
  * Set the command socket path.
  */
-
 void lttng_consumer_set_command_sock_path(
                struct lttng_consumer_local_data *ctx, char *sock)
 {
@@ -553,7 +870,6 @@ int lttng_consumer_send_error(
  */
 void lttng_consumer_cleanup(void)
 {
-       int ret;
        struct lttng_ht_iter iter;
        struct lttng_ht_node_ulong *node;
 
@@ -565,19 +881,22 @@ void lttng_consumer_cleanup(void)
         */
        cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node,
                        node) {
-               ret = lttng_ht_del(consumer_data.stream_ht, &iter);
-               assert(!ret);
-               call_rcu(&node->head, consumer_del_stream_rcu);
+               struct lttng_consumer_stream *stream =
+                       caa_container_of(node, struct lttng_consumer_stream, node);
+               consumer_del_stream(stream);
        }
 
        cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
                        node) {
-               ret = lttng_ht_del(consumer_data.channel_ht, &iter);
-               assert(!ret);
-               call_rcu(&node->head, consumer_del_channel_rcu);
+               struct lttng_consumer_channel *channel =
+                       caa_container_of(node, struct lttng_consumer_channel, node);
+               consumer_del_channel(channel);
        }
 
        rcu_read_unlock();
+
+       lttng_ht_destroy(consumer_data.stream_ht);
+       lttng_ht_destroy(consumer_data.channel_ht);
 }
 
 /*
@@ -587,14 +906,16 @@ void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
 {
        int ret;
        consumer_quit = 1;
-       ret = write(ctx->consumer_should_quit[1], "4", 1);
+       do {
+               ret = write(ctx->consumer_should_quit[1], "4", 1);
+       } while (ret < 0 && errno == EINTR);
        if (ret < 0) {
                perror("write consumer quit");
        }
 }
 
-void lttng_consumer_sync_trace_file(
-               struct lttng_consumer_stream *stream, off_t orig_offset)
+void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
+               off_t orig_offset)
 {
        int outfd = stream->out_fd;
 
@@ -678,6 +999,20 @@ struct lttng_consumer_local_data *lttng_consumer_create(
                goto error_poll_pipe;
        }
 
+       /* set read end of the pipe to non-blocking */
+       ret = fcntl(ctx->consumer_poll_pipe[0], F_SETFL, O_NONBLOCK);
+       if (ret < 0) {
+               perror("fcntl O_NONBLOCK");
+               goto error_poll_fcntl;
+       }
+
+       /* set write end of the pipe to non-blocking */
+       ret = fcntl(ctx->consumer_poll_pipe[1], F_SETFL, O_NONBLOCK);
+       if (ret < 0) {
+               perror("fcntl O_NONBLOCK");
+               goto error_poll_fcntl;
+       }
+
        ret = pipe(ctx->consumer_should_quit);
        if (ret < 0) {
                perror("Error creating recv pipe");
@@ -698,14 +1033,19 @@ error_thread_pipe:
                int err;
 
                err = close(ctx->consumer_should_quit[i]);
-               assert(!err);
+               if (err) {
+                       PERROR("close");
+               }
        }
+error_poll_fcntl:
 error_quit_pipe:
        for (i = 0; i < 2; i++) {
                int err;
 
                err = close(ctx->consumer_poll_pipe[i]);
-               assert(!err);
+               if (err) {
+                       PERROR("close");
+               }
        }
 error_poll_pipe:
        free(ctx);
@@ -718,19 +1058,72 @@ error:
  */
 void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
 {
-       close(ctx->consumer_error_socket);
-       close(ctx->consumer_thread_pipe[0]);
-       close(ctx->consumer_thread_pipe[1]);
-       close(ctx->consumer_poll_pipe[0]);
-       close(ctx->consumer_poll_pipe[1]);
-       close(ctx->consumer_should_quit[0]);
-       close(ctx->consumer_should_quit[1]);
+       int ret;
+
+       ret = close(ctx->consumer_error_socket);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_thread_pipe[0]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_thread_pipe[1]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_poll_pipe[0]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_poll_pipe[1]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_should_quit[0]);
+       if (ret) {
+               PERROR("close");
+       }
+       ret = close(ctx->consumer_should_quit[1]);
+       if (ret) {
+               PERROR("close");
+       }
        unlink(ctx->consumer_command_sock_path);
        free(ctx);
 }
 
 /*
- * Mmap the ring buffer, read it and write the data to the tracefile.
+ * Write the metadata stream id on the specified file descriptor.
+ */
+static int write_relayd_metadata_id(int fd,
+               struct lttng_consumer_stream *stream,
+               struct consumer_relayd_sock_pair *relayd)
+{
+       int ret;
+       uint64_t metadata_id;
+
+       metadata_id = htobe64(stream->relayd_stream_id);
+       do {
+               ret = write(fd, (void *) &metadata_id,
+                               sizeof(stream->relayd_stream_id));
+       } while (ret < 0 && errno == EINTR);
+       if (ret < 0) {
+               PERROR("write metadata stream id");
+               goto end;
+       }
+       DBG("Metadata stream id %" PRIu64 " written before data",
+                       stream->relayd_stream_id);
+
+end:
+       return ret;
+}
+
+/*
+ * Mmap the ring buffer, read it and write the data to the tracefile. This is a
+ * core function for writing trace buffers to either the local filesystem or
+ * the network.
+ *
+ * Careful review MUST be put if any changes occur!
  *
  * Returns the number of bytes written
  */
@@ -738,18 +1131,115 @@ ssize_t lttng_consumer_on_read_subbuffer_mmap(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
+       unsigned long mmap_offset;
+       ssize_t ret = 0, written = 0;
+       off_t orig_offset = stream->out_fd_offset;
+       /* Default is on the disk */
+       int outfd = stream->out_fd;
+       struct consumer_relayd_sock_pair *relayd = NULL;
+
+       /* RCU lock for the relayd pointer */
+       rcu_read_lock();
+
+       /* Flag that the current stream if set for network streaming. */
+       if (stream->net_seq_idx != -1) {
+               relayd = consumer_find_relayd(stream->net_seq_idx);
+               if (relayd == NULL) {
+                       goto end;
+               }
+       }
+
+       /* get the offset inside the fd to mmap */
        switch (consumer_data.type) {
        case LTTNG_CONSUMER_KERNEL:
-               return lttng_kconsumer_on_read_subbuffer_mmap(ctx, stream, len);
+               ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
+               break;
        case LTTNG_CONSUMER32_UST:
        case LTTNG_CONSUMER64_UST:
-               return lttng_ustconsumer_on_read_subbuffer_mmap(ctx, stream, len);
+               ret = lttng_ustctl_get_mmap_read_offset(stream->chan->handle,
+                               stream->buf, &mmap_offset);
+               break;
        default:
                ERR("Unknown consumer_data type");
                assert(0);
        }
+       if (ret != 0) {
+               errno = -ret;
+               PERROR("tracer ctl get_mmap_read_offset");
+               written = ret;
+               goto end;
+       }
 
-       return 0;
+       /* Handle stream on the relayd if the output is on the network */
+       if (relayd) {
+               unsigned long netlen = len;
+
+               /*
+                * Lock the control socket for the complete duration of the function
+                * since from this point on we will use the socket.
+                */
+               if (stream->metadata_flag) {
+                       /* Metadata requires the control socket. */
+                       pthread_mutex_lock(&relayd->ctrl_sock_mutex);
+                       netlen += sizeof(stream->relayd_stream_id);
+               }
+
+               ret = write_relayd_stream_header(stream, netlen, relayd);
+               if (ret >= 0) {
+                       /* Use the returned socket. */
+                       outfd = ret;
+
+                       /* Write metadata stream id before payload */
+                       if (stream->metadata_flag) {
+                               ret = write_relayd_metadata_id(outfd, stream, relayd);
+                               if (ret < 0) {
+                                       written = ret;
+                                       goto end;
+                               }
+                       }
+               }
+               /* Else, use the default set before which is the filesystem. */
+       }
+
+       while (len > 0) {
+               do {
+                       ret = write(outfd, stream->mmap_base + mmap_offset, len);
+               } while (ret < 0 && errno == EINTR);
+               if (ret < 0) {
+                       PERROR("Error in file write");
+                       if (written == 0) {
+                               written = ret;
+                       }
+                       goto end;
+               } else if (ret > len) {
+                       PERROR("Error in file write (ret %zd > len %lu)", ret, len);
+                       written += ret;
+                       goto end;
+               } else {
+                       len -= ret;
+                       mmap_offset += ret;
+               }
+               DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
+
+               /* This call is useless on a socket so better save a syscall. */
+               if (!relayd) {
+                       /* This won't block, but will start writeout asynchronously */
+                       lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
+                                       SYNC_FILE_RANGE_WRITE);
+                       stream->out_fd_offset += ret;
+               }
+               written += ret;
+       }
+       lttng_consumer_sync_trace_file(stream, orig_offset);
+
+end:
+       /* Unlock only if ctrl socket used */
+       if (relayd && stream->metadata_flag) {
+               pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
+       }
+
+       rcu_read_unlock();
+       return written;
 }
 
 /*
@@ -761,18 +1251,151 @@ ssize_t lttng_consumer_on_read_subbuffer_splice(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
+       ssize_t ret = 0, written = 0, ret_splice = 0;
+       loff_t offset = 0;
+       off_t orig_offset = stream->out_fd_offset;
+       int fd = stream->wait_fd;
+       /* Default is on the disk */
+       int outfd = stream->out_fd;
+       struct consumer_relayd_sock_pair *relayd = NULL;
+
        switch (consumer_data.type) {
        case LTTNG_CONSUMER_KERNEL:
-               return lttng_kconsumer_on_read_subbuffer_splice(ctx, stream, len);
+               break;
        case LTTNG_CONSUMER32_UST:
        case LTTNG_CONSUMER64_UST:
+               /* Not supported for user space tracing */
                return -ENOSYS;
        default:
                ERR("Unknown consumer_data type");
                assert(0);
-               return -ENOSYS;
        }
 
+       /* RCU lock for the relayd pointer */
+       rcu_read_lock();
+
+       /* Flag that the current stream if set for network streaming. */
+       if (stream->net_seq_idx != -1) {
+               relayd = consumer_find_relayd(stream->net_seq_idx);
+               if (relayd == NULL) {
+                       goto end;
+               }
+       }
+
+       /* Write metadata stream id before payload */
+       if (stream->metadata_flag && relayd) {
+               /*
+                * Lock the control socket for the complete duration of the function
+                * since from this point on we will use the socket.
+                */
+               pthread_mutex_lock(&relayd->ctrl_sock_mutex);
+
+               ret = write_relayd_metadata_id(ctx->consumer_thread_pipe[1],
+                               stream, relayd);
+               if (ret < 0) {
+                       written = ret;
+                       goto end;
+               }
+       }
+
+       while (len > 0) {
+               DBG("splice chan to pipe offset %lu of len %lu (fd : %d)",
+                               (unsigned long)offset, len, fd);
+               ret_splice = splice(fd, &offset, ctx->consumer_thread_pipe[1], NULL, len,
+                               SPLICE_F_MOVE | SPLICE_F_MORE);
+               DBG("splice chan to pipe, ret %zd", ret_splice);
+               if (ret_splice < 0) {
+                       PERROR("Error in relay splice");
+                       if (written == 0) {
+                               written = ret_splice;
+                       }
+                       ret = errno;
+                       goto splice_error;
+               }
+
+               /* Handle stream on the relayd if the output is on the network */
+               if (relayd) {
+                       if (stream->metadata_flag) {
+                               /* Update counter to fit the spliced data */
+                               ret_splice += sizeof(stream->relayd_stream_id);
+                               len += sizeof(stream->relayd_stream_id);
+                               /*
+                                * We do this so the return value can match the len passed as
+                                * argument to this function.
+                                */
+                               written -= sizeof(stream->relayd_stream_id);
+                       }
+
+                       ret = write_relayd_stream_header(stream, ret_splice, relayd);
+                       if (ret >= 0) {
+                               /* Use the returned socket. */
+                               outfd = ret;
+                       } else {
+                               ERR("Remote relayd disconnected. Stopping");
+                               goto end;
+                       }
+               }
+
+               /* Splice data out */
+               ret_splice = splice(ctx->consumer_thread_pipe[0], NULL, outfd, NULL,
+                               ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
+               DBG("Kernel consumer splice pipe to file, ret %zd", ret_splice);
+               if (ret_splice < 0) {
+                       PERROR("Error in file splice");
+                       if (written == 0) {
+                               written = ret_splice;
+                       }
+                       ret = errno;
+                       goto splice_error;
+               } else if (ret_splice > len) {
+                       errno = EINVAL;
+                       PERROR("Wrote more data than requested %zd (len: %lu)",
+                                       ret_splice, len);
+                       written += ret_splice;
+                       ret = errno;
+                       goto splice_error;
+               }
+               len -= ret_splice;
+
+               /* This call is useless on a socket so better save a syscall. */
+               if (!relayd) {
+                       /* This won't block, but will start writeout asynchronously */
+                       lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
+                                       SYNC_FILE_RANGE_WRITE);
+                       stream->out_fd_offset += ret_splice;
+               }
+               written += ret_splice;
+       }
+       lttng_consumer_sync_trace_file(stream, orig_offset);
+
+       ret = ret_splice;
+
+       goto end;
+
+splice_error:
+       /* send the appropriate error description to sessiond */
+       switch (ret) {
+       case EBADF:
+               lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EBADF);
+               break;
+       case EINVAL:
+               lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
+               break;
+       case ENOMEM:
+               lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
+               break;
+       case ESPIPE:
+               lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
+               break;
+       }
+
+end:
+       if (relayd && stream->metadata_flag) {
+               pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
+       }
+
+       rcu_read_unlock();
+       return written;
 }
 
 /*
@@ -848,9 +1471,14 @@ void *lttng_consumer_thread_poll_fds(void *data)
        struct lttng_consumer_stream **local_stream = NULL;
        /* local view of consumer_data.fds_count */
        int nb_fd = 0;
-       char tmp;
-       int tmp2;
        struct lttng_consumer_local_data *ctx = data;
+       struct lttng_ht *metadata_ht;
+       struct lttng_ht_iter iter;
+       struct lttng_ht_node_ulong *node;
+       struct lttng_consumer_stream *metadata_stream;
+       ssize_t len;
+
+       metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
 
        rcu_register_thread();
 
@@ -891,10 +1519,11 @@ void *lttng_consumer_thread_poll_fds(void *data)
                                pthread_mutex_unlock(&consumer_data.lock);
                                goto end;
                        }
-                       ret = consumer_update_poll_array(ctx, &pollfd, local_stream);
+                       ret = consumer_update_poll_array(ctx, &pollfd, local_stream,
+                                       metadata_ht);
                        if (ret < 0) {
                                ERR("Error in allocating pollfd or local_outfds");
-                               lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
+                               lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
                                pthread_mutex_unlock(&consumer_data.lock);
                                goto end;
                        }
@@ -920,7 +1549,7 @@ void *lttng_consumer_thread_poll_fds(void *data)
                                goto restart;
                        }
                        perror("Poll error");
-                       lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
+                       lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
                        goto end;
                } else if (num_rdy == 0) {
                        DBG("Polling thread timed out");
@@ -928,25 +1557,42 @@ void *lttng_consumer_thread_poll_fds(void *data)
                }
 
                /*
-                * If the consumer_poll_pipe triggered poll go
-                * directly to the beginning of the loop to update the
-                * array. We want to prioritize array update over
-                * low-priority reads.
+                * If the consumer_poll_pipe triggered poll go directly to the
+                * beginning of the loop to update the array. We want to prioritize
+                * array update over low-priority reads.
                 */
-               if (pollfd[nb_fd].revents & POLLIN) {
+               if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
+                       size_t pipe_readlen;
+                       char tmp;
+
                        DBG("consumer_poll_pipe wake up");
-                       tmp2 = read(ctx->consumer_poll_pipe[0], &tmp, 1);
-                       if (tmp2 < 0) {
-                               perror("read consumer poll");
-                       }
+                       /* Consume 1 byte of pipe data */
+                       do {
+                               pipe_readlen = read(ctx->consumer_poll_pipe[0], &tmp, 1);
+                       } while (pipe_readlen == -1 && errno == EINTR);
                        continue;
                }
 
                /* Take care of high priority channels first. */
                for (i = 0; i < nb_fd; i++) {
-                       if (pollfd[i].revents & POLLPRI) {
-                               ssize_t len;
-
+                       /* Lookup for metadata which is the highest priority */
+                       lttng_ht_lookup(metadata_ht,
+                                       (void *)((unsigned long) pollfd[i].fd), &iter);
+                       node = lttng_ht_iter_get_node_ulong(&iter);
+                       if (node != NULL &&
+                                       (pollfd[i].revents & (POLLIN | POLLPRI))) {
+                               DBG("Urgent metadata read on fd %d", pollfd[i].fd);
+                               metadata_stream = caa_container_of(node,
+                                               struct lttng_consumer_stream, waitfd_node);
+                               high_prio = 1;
+                               len = ctx->on_buffer_ready(metadata_stream, ctx);
+                               /* it's ok to have an unavailable sub-buffer */
+                               if (len < 0 && len != -EAGAIN) {
+                                       goto end;
+                               } else if (len > 0) {
+                                       metadata_stream->data_read = 1;
+                               }
+                       } else if (pollfd[i].revents & POLLPRI) {
                                DBG("Urgent read on fd %d", pollfd[i].fd);
                                high_prio = 1;
                                len = ctx->on_buffer_ready(local_stream[i], ctx);
@@ -971,10 +1617,6 @@ void *lttng_consumer_thread_poll_fds(void *data)
                for (i = 0; i < nb_fd; i++) {
                        if ((pollfd[i].revents & POLLIN) ||
                                        local_stream[i]->hangup_flush_done) {
-                               ssize_t len;
-
-                               assert(!(pollfd[i].revents & POLLERR));
-                               assert(!(pollfd[i].revents & POLLNVAL));
                                DBG("Normal read on fd %d", pollfd[i].fd);
                                len = ctx->on_buffer_ready(local_stream[i], ctx);
                                /* it's ok to have an unavailable sub-buffer */
@@ -1006,25 +1648,34 @@ void *lttng_consumer_thread_poll_fds(void *data)
                        if ((pollfd[i].revents & POLLHUP)) {
                                DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
                                if (!local_stream[i]->data_read) {
-                                       rcu_read_lock();
-                                       consumer_del_stream_rcu(&local_stream[i]->node.head);
-                                       rcu_read_unlock();
+                                       if (local_stream[i]->metadata_flag) {
+                                               iter.iter.node = &local_stream[i]->waitfd_node.node;
+                                               ret = lttng_ht_del(metadata_ht, &iter);
+                                               assert(!ret);
+                                       }
+                                       consumer_del_stream(local_stream[i]);
                                        num_hup++;
                                }
                        } else if (pollfd[i].revents & POLLERR) {
                                ERR("Error returned in polling fd %d.", pollfd[i].fd);
                                if (!local_stream[i]->data_read) {
-                                       rcu_read_lock();
-                                       consumer_del_stream_rcu(&local_stream[i]->node.head);
-                                       rcu_read_unlock();
+                                       if (local_stream[i]->metadata_flag) {
+                                               iter.iter.node = &local_stream[i]->waitfd_node.node;
+                                               ret = lttng_ht_del(metadata_ht, &iter);
+                                               assert(!ret);
+                                       }
+                                       consumer_del_stream(local_stream[i]);
                                        num_hup++;
                                }
                        } else if (pollfd[i].revents & POLLNVAL) {
                                ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
                                if (!local_stream[i]->data_read) {
-                                       rcu_read_lock();
-                                       consumer_del_stream_rcu(&local_stream[i]->node.head);
-                                       rcu_read_unlock();
+                                       if (local_stream[i]->metadata_flag) {
+                                               iter.iter.node = &local_stream[i]->waitfd_node.node;
+                                               ret = lttng_ht_del(metadata_ht, &iter);
+                                               assert(!ret);
+                                       }
+                                       consumer_del_stream(local_stream[i]);
                                        num_hup++;
                                }
                        }
@@ -1075,7 +1726,7 @@ void *lttng_consumer_thread_receive_fds(void *data)
        }
 
        DBG("Sending ready command to lttng-sessiond");
-       ret = lttng_consumer_send_error(ctx, CONSUMERD_COMMAND_SOCK_READY);
+       ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
        /* return < 0 on error, but == 0 is not fatal */
        if (ret < 0) {
                ERR("Error sending ready command to lttng-sessiond");
@@ -1151,11 +1802,20 @@ end:
         */
        consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT;
 
-       /* wake up the polling thread */
-       ret = write(ctx->consumer_poll_pipe[1], "4", 1);
-       if (ret < 0) {
-               perror("poll pipe write");
-       }
+       /*
+        * Wake-up the other end by writing a null byte in the pipe
+        * (non-blocking). Important note: Because writing into the
+        * pipe is non-blocking (and therefore we allow dropping wakeup
+        * data, as long as there is wakeup data present in the pipe
+        * buffer to wake up the other end), the other end should
+        * perform the following sequence for waiting:
+        * 1) empty the pipe (reads).
+        * 2) perform update operation.
+        * 3) wait on the pipe (poll).
+        */
+       do {
+               ret = write(ctx->consumer_poll_pipe[1], "", 1);
+       } while (ret < 0 && errno == EINTR);
        rcu_unregister_thread();
        return NULL;
 }
@@ -1198,5 +1858,97 @@ void lttng_consumer_init(void)
 {
        consumer_data.stream_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
        consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
+       consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
 }
 
+/*
+ * Process the ADD_RELAYD command receive by a consumer.
+ *
+ * This will create a relayd socket pair and add it to the relayd hash table.
+ * The caller MUST acquire a RCU read side lock before calling it.
+ */
+int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
+               struct lttng_consumer_local_data *ctx, int sock,
+               struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock)
+{
+       int fd, ret = -1;
+       struct consumer_relayd_sock_pair *relayd;
+
+       DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx);
+
+       /* Get relayd reference if exists. */
+       relayd = consumer_find_relayd(net_seq_idx);
+       if (relayd == NULL) {
+               /* Not found. Allocate one. */
+               relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
+               if (relayd == NULL) {
+                       lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
+                       goto error;
+               }
+       }
+
+       /* Poll on consumer socket. */
+       if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
+               ret = -EINTR;
+               goto error;
+       }
+
+       /* Get relayd socket from session daemon */
+       ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
+       if (ret != sizeof(fd)) {
+               lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
+               ret = -1;
+               goto error;
+       }
+
+       /* Copy socket information and received FD */
+       switch (sock_type) {
+       case LTTNG_STREAM_CONTROL:
+               /* Copy received lttcomm socket */
+               lttcomm_copy_sock(&relayd->control_sock, relayd_sock);
+               ret = lttcomm_create_sock(&relayd->control_sock);
+               if (ret < 0) {
+                       goto error;
+               }
+
+               /* Close the created socket fd which is useless */
+               close(relayd->control_sock.fd);
+
+               /* Assign new file descriptor */
+               relayd->control_sock.fd = fd;
+               break;
+       case LTTNG_STREAM_DATA:
+               /* Copy received lttcomm socket */
+               lttcomm_copy_sock(&relayd->data_sock, relayd_sock);
+               ret = lttcomm_create_sock(&relayd->data_sock);
+               if (ret < 0) {
+                       goto error;
+               }
+
+               /* Close the created socket fd which is useless */
+               close(relayd->data_sock.fd);
+
+               /* Assign new file descriptor */
+               relayd->data_sock.fd = fd;
+               break;
+       default:
+               ERR("Unknown relayd socket type (%d)", sock_type);
+               goto error;
+       }
+
+       DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
+                       sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
+                       relayd->net_seq_idx, fd);
+
+       /*
+        * Add relayd socket pair to consumer data hashtable. If object already
+        * exists or on error, the function gracefully returns.
+        */
+       consumer_add_relayd(relayd);
+
+       /* All good! */
+       ret = 0;
+
+error:
+       return ret;
+}
This page took 0.038564 seconds and 4 git commands to generate.