+static int kconsumerd_cmd_sock;
+static int kernel_tracer_fd;
+
+/* Pthread, Mutexes and Semaphores */
+static pthread_t kconsumerd_thread;
+static pthread_t apps_thread;
+static pthread_t client_thread;
+static sem_t kconsumerd_sem;
+
+static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
+
+/*
+ * teardown_kernel_session
+ *
+ * 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");
+ trace_destroy_kernel_session(session->kernel_session);
+ /* Extra precaution */
+ session->kernel_session = NULL;
+ /* Decrement session count */
+ session->kern_session_count--;
+ }
+}
+
+/*
+ * cleanup
+ *
+ * Cleanup the daemon on exit
+ */
+static void cleanup()
+{
+ int ret;
+ char *cmd;
+ struct ltt_session *sess;
+
+ DBG("Cleaning up");
+
+ /* <fun> */
+ MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
+ MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
+ /* </fun> */
+
+ /* Stopping all threads */
+ DBG("Terminating all threads");
+ pthread_cancel(client_thread);
+ pthread_cancel(apps_thread);
+ if (kconsumerd_pid != 0) {
+ pthread_cancel(kconsumerd_thread);
+ }
+
+ DBG("Unlinking all unix socket");
+ unlink(client_unix_sock_path);
+ unlink(apps_unix_sock_path);
+ unlink(kconsumerd_err_unix_sock_path);
+
+ 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");
+ /* Cleanup ALL session */
+ cds_list_for_each_entry(sess, <t_session_list.head, list) {
+ teardown_kernel_session(sess);
+ // TODO complete session cleanup (including UST)
+ }
+
+ close(kernel_tracer_fd);
+}
+
+/*
+ * send_unix_sock
+ *
+ * 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);
+}
+
+/*
+ * clean_command_ctx
+ *
+ * Free memory of a command context structure.
+ */
+static void clean_command_ctx(struct command_ctx *cmd_ctx)
+{
+ DBG("Clean command context structure %p", cmd_ctx);
+ 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;
+ }
+}
+
+/*
+ * ust_connect_app
+ *
+ * Return a socket connected to the libust communication socket
+ * of the application identified by the pid.
+ *
+ * If the pid is not found in the traceable list,
+ * return -1 to indicate error.
+ */
+static int ust_connect_app(pid_t pid)
+{
+ int sock;
+ struct ltt_traceable_app *lta;
+
+ DBG("Connect to application pid %d", pid);
+
+ lta = find_app_by_pid(pid);
+ if (lta == NULL) {
+ /* App not found */
+ DBG("Application pid %d not found", pid);
+ return -1;
+ }
+
+ sock = ustctl_connect_pid(lta->pid);
+ if (sock < 0) {
+ ERR("Fail connecting to the PID %d\n", pid);
+ }
+
+ return sock;
+}
+
+/*
+ * notify_apps
+ *
+ * Notify apps by writing 42 to a named pipe using name.
+ * Every applications waiting for a ltt-sessiond will be notified
+ * and re-register automatically to the session daemon.
+ *
+ * Return open or write error value.
+ */
+static int notify_apps(const char *name)
+{
+ int fd;
+ int ret = -1;
+
+ DBG("Notify the global application pipe");
+
+ /* Try opening the global pipe */
+ fd = open(name, O_WRONLY);
+ if (fd < 0) {
+ goto error;
+ }