+/*
+ * Sending a single channel to the consumer with command ADD_CHANNEL.
+ */
+int kernel_consumer_add_channel(int sock, struct ltt_kernel_channel *channel)
+{
+ int ret;
+ struct lttcomm_consumer_msg lkm;
+
+ /* Safety net */
+ assert(channel);
+
+ DBG("Kernel consumer adding channel %s to kernel consumer",
+ channel->channel->name);
+
+ /* Prep channel message structure */
+ consumer_init_channel_comm_msg(&lkm,
+ LTTNG_CONSUMER_ADD_CHANNEL,
+ channel->fd,
+ channel->channel->attr.subbuf_size,
+ 0, /* Kernel */
+ channel->channel->name);
+
+ ret = consumer_send_channel(sock, &lkm);
+ if (ret < 0) {
+ goto error;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Sending metadata to the consumer with command ADD_CHANNEL and ADD_STREAM.
+ */
+int kernel_consumer_add_metadata(int sock, struct ltt_kernel_session *session)
+{
+ int ret;
+ const char *pathname;
+ struct lttcomm_consumer_msg lkm;
+ struct consumer_output *output;
+
+ /* Safety net */
+ assert(session);
+ assert(session->consumer);
+
+ DBG("Sending metadata %d to kernel consumer", session->metadata_stream_fd);
+
+ /* Get consumer output pointer */
+ output = session->consumer;
+
+ /* Get correct path name destination */
+ if (output->type == CONSUMER_DST_LOCAL) {
+ pathname = output->dst.trace_path;
+ } else {
+ pathname = output->subdir;
+ }
+
+ /* Prep channel message structure */
+ consumer_init_channel_comm_msg(&lkm,
+ LTTNG_CONSUMER_ADD_CHANNEL,
+ session->metadata->fd,
+ session->metadata->conf->attr.subbuf_size,
+ 0, /* for kernel */
+ "metadata");
+
+ ret = consumer_send_channel(sock, &lkm);
+ if (ret < 0) {
+ goto error;
+ }
+
+ /* Prep stream message structure */
+ consumer_init_stream_comm_msg(&lkm,
+ LTTNG_CONSUMER_ADD_STREAM,
+ session->metadata->fd,
+ session->metadata_stream_fd,
+ LTTNG_CONSUMER_ACTIVE_STREAM,
+ DEFAULT_KERNEL_CHANNEL_OUTPUT,
+ 0, /* Kernel */
+ session->uid,
+ session->gid,
+ output->net_seq_index,
+ 1, /* Metadata flag set */
+ "metadata",
+ pathname);
+
+ /* Send stream and file descriptor */
+ ret = consumer_send_stream(sock, output, &lkm,
+ &session->metadata_stream_fd, 1);
+ if (ret < 0) {
+ goto error;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Sending a single stream to the consumer with command ADD_STREAM.
+ */
+int kernel_consumer_add_stream(int sock, struct ltt_kernel_channel *channel,
+ struct ltt_kernel_stream *stream, struct ltt_kernel_session *session)
+{
+ int ret;
+ const char *pathname;
+ struct lttcomm_consumer_msg lkm;
+ struct consumer_output *output;
+
+ assert(channel);
+ assert(stream);
+ assert(session);
+ assert(session->consumer);
+
+ DBG("Sending stream %d of channel %s to kernel consumer",
+ stream->fd, channel->channel->name);
+
+ /* Get consumer output pointer */
+ output = session->consumer;
+
+ /* Get correct path name destination */
+ if (output->type == CONSUMER_DST_LOCAL) {
+ pathname = output->dst.trace_path;
+ DBG3("Consumer is local to %s", pathname);
+ } else {
+ pathname = output->subdir;
+ DBG3("Consumer is network to subdir %s", pathname);
+ }
+
+ /* Prep stream consumer message */
+ consumer_init_stream_comm_msg(&lkm, LTTNG_CONSUMER_ADD_STREAM,
+ channel->fd,
+ stream->fd,
+ stream->state,
+ channel->channel->attr.output,
+ 0, /* Kernel */
+ session->uid,
+ session->gid,
+ output->net_seq_index,
+ 0, /* Metadata flag unset */
+ stream->name,
+ pathname);
+
+ /* Send stream and file descriptor */
+ ret = consumer_send_stream(sock, output, &lkm, &stream->fd, 1);
+ if (ret < 0) {
+ goto error;
+ }
+
+error:
+ return ret;
+}
+