+/*
+ * send_kconsumerd_fds
+ *
+ * Send all stream fds of the kernel session to the consumer.
+ */
+static int send_kconsumerd_fds(int sock, struct ltt_kernel_session *session)
+{
+ int ret;
+ size_t nb_fd;
+ struct ltt_kernel_stream *stream;
+ struct ltt_kernel_channel *chan;
+ struct lttcomm_kconsumerd_header lkh;
+ struct lttcomm_kconsumerd_msg lkm;
+
+ nb_fd = session->stream_count_global;
+
+ /* Setup header */
+ lkh.payload_size = (nb_fd + 1) * sizeof(struct lttcomm_kconsumerd_msg);
+ lkh.cmd_type = ADD_STREAM;
+
+ DBG("Sending kconsumerd header");
+
+ ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
+ if (ret < 0) {
+ perror("send kconsumerd header");
+ goto error;
+ }
+
+ DBG("Sending metadata stream fd");
+
+ /* Send metadata stream fd first */
+ lkm.fd = session->metadata_stream_fd;
+ lkm.state = ACTIVE_FD;
+ lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
+ strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
+
+ ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
+ if (ret < 0) {
+ perror("send kconsumerd fd");
+ goto error;
+ }
+
+ cds_list_for_each_entry(chan, &session->channel_list.head, list) {
+ cds_list_for_each_entry(stream, &chan->stream_list.head, list) {
+ lkm.fd = stream->fd;
+ lkm.state = stream->state;
+ lkm.max_sb_size = chan->channel->attr.subbuf_size;
+ strncpy(lkm.path_name, stream->pathname, PATH_MAX);
+
+ DBG("Sending fd %d to kconsumerd", lkm.fd);
+
+ ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
+ if (ret < 0) {
+ perror("send kconsumerd fd");
+ goto error;
+ }
+ }
+ }
+
+ DBG("Kconsumerd fds sent");
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * create_trace_dir
+ *
+ * Create the trace output directory.
+ */
+static int create_trace_dir(struct ltt_kernel_session *session)
+{
+ int ret;
+ struct ltt_kernel_channel *chan;
+
+ /* Create all channel directories */
+ cds_list_for_each_entry(chan, &session->channel_list.head, list) {
+ DBG("Creating trace directory at %s", chan->pathname);
+ // TODO: recursive create dir
+ ret = mkdir(chan->pathname, S_IRWXU | S_IRWXG );
+ if (ret < 0) {
+ if (ret != EEXIST) {
+ perror("mkdir trace path");
+ ret = -errno;
+ goto error;
+ }
+ }
+ }
+
+ return 0;
+
+error:
+ return ret;
+}
+