+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;
+ }