+static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
+
+static int modprobe_remove_kernel_modules(void);
+
+/*
+ * Pointer initialized before thread creation.
+ *
+ * This points to the tracing session list containing the session count and a
+ * mutex lock. The lock MUST be taken if you iterate over the list. The lock
+ * MUST NOT be taken if you call a public function in session.c.
+ *
+ * The lock is nested inside the structure: session_list_ptr->lock. Please use
+ * lock_session_list and unlock_session_list for lock acquisition.
+ */
+static struct ltt_session_list *session_list_ptr;
+
+static gid_t allowed_group(void)
+{
+ struct group *grp;
+
+ if (opt_tracing_group) {
+ grp = getgrnam(opt_tracing_group);
+ } else {
+ grp = getgrnam(default_tracing_group);
+ }
+ if (!grp) {
+ return -1;
+ } else {
+ return grp->gr_gid;
+ }
+}
+
+/*
+ * Init quit pipe.
+ *
+ * Return -1 on error or 0 if all pipes are created.
+ */
+static int init_thread_quit_pipe(void)
+{
+ int ret;
+
+ ret = pipe2(thread_quit_pipe, O_CLOEXEC);
+ if (ret < 0) {
+ perror("thread quit pipe");
+ goto error;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Complete teardown of a kernel session. This free all data structure related
+ * to a kernel session and update counter.
+ */
+static void teardown_kernel_session(struct ltt_session *session)
+{
+ if (session->kernel_session != NULL) {
+ DBG("Tearing down kernel session");
+
+ /*
+ * If a custom kernel consumer was registered, close the socket before
+ * tearing down the complete kernel session structure
+ */
+ if (session->kernel_session->consumer_fd != kconsumerd_cmd_sock) {
+ lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
+ }
+
+ trace_destroy_kernel_session(session->kernel_session);
+ /* Extra precaution */
+ session->kernel_session = NULL;
+ }
+}
+
+static void stop_threads(void)
+{
+ /* Stopping all threads */
+ DBG("Terminating all threads");
+ close(thread_quit_pipe[0]);
+ close(thread_quit_pipe[1]);
+}
+
+/*
+ * Cleanup the daemon
+ */
+static void cleanup(void)
+{
+ int ret;
+ char *cmd;
+ struct ltt_session *sess, *stmp;
+
+ DBG("Cleaning up");
+
+ /* <fun> */
+ MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
+ "Matthew, BEET driven development works!%c[%dm",
+ 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
+ /* </fun> */
+
+ DBG("Removing %s directory", LTTNG_RUNDIR);
+ ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
+ if (ret < 0) {
+ ERR("asprintf failed. Something is really wrong!");
+ }
+
+ /* Remove lttng run directory */
+ ret = system(cmd);
+ if (ret < 0) {
+ ERR("Unable to clean " LTTNG_RUNDIR);
+ }
+
+ DBG("Cleaning up all session");
+
+ /* Destroy session list mutex */
+ if (session_list_ptr != NULL) {
+ pthread_mutex_destroy(&session_list_ptr->lock);
+
+ /* Cleanup ALL session */
+ cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
+ teardown_kernel_session(sess);
+ // TODO complete session cleanup (including UST)
+ }
+ }
+
+ pthread_mutex_destroy(&kconsumerd_pid_mutex);
+
+ DBG("Closing kernel fd");
+ close(kernel_tracer_fd);
+
+ DBG("Unloading kernel modules");
+ modprobe_remove_kernel_modules();
+}
+
+/*
+ * Send data on a unix socket using the liblttsessiondcomm API.
+ *
+ * Return lttcomm error code.
+ */
+static int send_unix_sock(int sock, void *buf, size_t len)
+{
+ /* Check valid length */
+ if (len <= 0) {
+ return -1;
+ }
+
+ return lttcomm_send_unix_sock(sock, buf, len);
+}
+
+/*
+ * Free memory of a command context structure.
+ */
+static void clean_command_ctx(struct command_ctx **cmd_ctx)
+{
+ DBG("Clean command context structure");
+ if (*cmd_ctx) {
+ if ((*cmd_ctx)->llm) {
+ free((*cmd_ctx)->llm);
+ }
+ if ((*cmd_ctx)->lsm) {
+ free((*cmd_ctx)->lsm);
+ }
+ free(*cmd_ctx);
+ *cmd_ctx = NULL;
+ }
+}
+
+/*
+ * Send all stream fds of kernel channel to the consumer.
+ */
+static int send_kconsumerd_channel_fds(int sock, struct ltt_kernel_channel *channel)
+{
+ int ret;
+ size_t nb_fd;
+ struct ltt_kernel_stream *stream;
+ struct lttcomm_kconsumerd_header lkh;
+ struct lttcomm_kconsumerd_msg lkm;
+
+ DBG("Sending fds of channel %s to kernel consumer", channel->channel->name);
+
+ nb_fd = channel->stream_count;
+
+ /* Setup header */
+ lkh.payload_size = nb_fd * 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;
+ }
+
+ cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
+ if (stream->fd != 0) {
+ lkm.fd = stream->fd;
+ lkm.state = stream->state;
+ lkm.max_sb_size = channel->channel->attr.subbuf_size;
+ lkm.output = channel->channel->attr.output;
+ strncpy(lkm.path_name, stream->pathname, PATH_MAX);
+ lkm.path_name[PATH_MAX - 1] = '\0';
+
+ 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 channel fds sent");
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * Send all stream fds of the kernel session to the consumer.
+ */
+static int send_kconsumerd_fds(struct ltt_kernel_session *session)
+{
+ int ret;
+ struct ltt_kernel_channel *chan;
+ struct lttcomm_kconsumerd_header lkh;
+ struct lttcomm_kconsumerd_msg lkm;
+
+ /* Setup header */
+ lkh.payload_size = sizeof(struct lttcomm_kconsumerd_msg);
+ lkh.cmd_type = ADD_STREAM;
+
+ DBG("Sending kconsumerd header for metadata");
+
+ ret = lttcomm_send_unix_sock(session->consumer_fd, &lkh, sizeof(struct lttcomm_kconsumerd_header));
+ if (ret < 0) {
+ perror("send kconsumerd header");
+ goto error;
+ }
+
+ DBG("Sending metadata stream fd");
+
+ /* Extra protection. It's NOT suppose to be set to 0 at this point */
+ if (session->consumer_fd == 0) {
+ session->consumer_fd = kconsumerd_cmd_sock;
+ }
+
+ if (session->metadata_stream_fd != 0) {
+ /* 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;
+ lkm.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
+ strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
+ lkm.path_name[PATH_MAX - 1] = '\0';
+
+ ret = lttcomm_send_fds_unix_sock(session->consumer_fd, &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) {
+ ret = send_kconsumerd_channel_fds(session->consumer_fd, chan);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+ DBG("Kconsumerd fds (metadata and channel streams) sent");
+
+ return 0;
+
+error:
+ return ret;
+}