+#include "ust-ctl.h"
+#include "utils.h"
+#include "ust-ctl.h"
+
+/* Const values */
+const char default_home_dir[] = DEFAULT_HOME_DIR;
+const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
+const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
+const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
+
+/* Variables */
+int opt_verbose; /* Not static for lttngerr.h */
+int opt_verbose_kconsumerd; /* Not static for lttngerr.h */
+int opt_quiet; /* Not static for lttngerr.h */
+
+const char *progname;
+const char *opt_tracing_group;
+static int opt_sig_parent;
+static int opt_daemon;
+static int is_root; /* Set to 1 if the daemon is running as root */
+static pid_t ppid; /* Parent PID for --sig-parent option */
+static pid_t kconsumerd_pid;
+static struct pollfd *kernel_pollfd;
+static int dispatch_thread_exit;
+
+static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
+static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
+static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
+static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
+static char wait_shm_path[PATH_MAX]; /* global wait shm path for UST */
+
+/* Sockets and FDs */
+static int client_sock;
+static int apps_sock;
+static int kconsumerd_err_sock;
+static int kconsumerd_cmd_sock;
+static int kernel_tracer_fd;
+static int kernel_poll_pipe[2];
+
+/*
+ * Quit pipe for all threads. This permits a single cancellation point
+ * for all threads when receiving an event on the pipe.
+ */
+static int thread_quit_pipe[2];
+
+/*
+ * This pipe is used to inform the thread managing application communication
+ * that a command is queued and ready to be processed.
+ */
+static int apps_cmd_pipe[2];
+
+/* Pthread, Mutexes and Semaphores */
+static pthread_t kconsumerd_thread;
+static pthread_t apps_thread;
+static pthread_t reg_apps_thread;
+static pthread_t client_thread;
+static pthread_t kernel_thread;
+static pthread_t dispatch_thread;
+static sem_t kconsumerd_sem;
+
+static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
+
+/*
+ * UST registration command queue. This queue is tied with a futex and uses a N
+ * wakers / 1 waiter implemented and detailed in futex.c/.h
+ *
+ * The thread_manage_apps and thread_dispatch_ust_registration interact with
+ * this queue and the wait/wake scheme.
+ */
+static struct ust_cmd_queue ust_cmd_queue;
+
+/*
+ * 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;
+
+/*
+ * Remove modules in reverse load order.
+ */
+static int modprobe_remove_kernel_modules(void)
+{
+ int ret = 0, i;
+ char modprobe[256];
+
+ for (i = ARRAY_SIZE(kernel_modules_list) - 1; i >= 0; i--) {
+ ret = snprintf(modprobe, sizeof(modprobe),
+ "/sbin/modprobe --remove --quiet %s",
+ kernel_modules_list[i].name);
+ if (ret < 0) {
+ perror("snprintf modprobe --remove");
+ goto error;
+ }
+ modprobe[sizeof(modprobe) - 1] = '\0';
+ ret = system(modprobe);
+ if (ret == -1) {
+ ERR("Unable to launch modprobe --remove for module %s",
+ kernel_modules_list[i].name);
+ } else if (kernel_modules_list[i].required
+ && WEXITSTATUS(ret) != 0) {
+ ERR("Unable to remove module %s",
+ kernel_modules_list[i].name);
+ } else {
+ DBG("Modprobe removal successful %s",
+ kernel_modules_list[i].name);
+ }
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Return group ID of the tracing group or -1 if not found.
+ */
+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_kernel_destroy_session(session->kernel_session);
+ /* Extra precaution */
+ session->kernel_session = NULL;
+ }
+}
+
+/*
+ * Stop all threads by closing the thread quit pipe.
+ */
+static void stop_threads(void)
+{
+ /* Stopping all threads */
+ DBG("Terminating all threads");
+ close(thread_quit_pipe[0]);
+ close(thread_quit_pipe[1]);
+ /* Dispatch thread */
+ dispatch_thread_exit = 1;
+ futex_nto1_wake(&ust_cmd_queue.futex);
+}
+
+/*
+ * Cleanup the daemon
+ */
+static void cleanup(void)
+{
+ int ret;
+ char *cmd;
+ struct ltt_session *sess, *stmp;
+
+ DBG("Cleaning up");
+
+ /* <fun> */
+ MSG("%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)
+ }
+ }
+
+ DBG("Closing all UST sockets");
+ clean_traceable_apps_list();
+
+ pthread_mutex_destroy(&kconsumerd_pid_mutex);
+
+ DBG("Closing kernel fd");
+ close(kernel_tracer_fd);
+
+ if (is_root) {
+ 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;
+}
+
+/*
+ * Notify UST applications using the shm mmap futex.
+ */
+static int notify_ust_apps(int active)
+{
+ char *wait_shm_mmap;
+
+ DBG("Notifying applications of session daemon state: %d", active);
+
+ /* See shm.c for this call implying mmap, shm and futex calls */
+ wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
+ if (wait_shm_mmap == NULL) {
+ goto error;
+ }
+
+ /* Wake waiting process */
+ futex_wait_update((int32_t *) wait_shm_mmap, active);
+
+ /* Apps notified successfully */
+ return 0;
+
+error:
+ return -1;
+}
+
+/*
+ * Setup the outgoing data buffer for the response (llm) by allocating the
+ * right amount of memory and copying the original information from the lsm
+ * structure.
+ *
+ * Return total size of the buffer pointed by buf.
+ */
+static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
+{
+ int ret, buf_size;
+
+ buf_size = size;
+
+ cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
+ if (cmd_ctx->llm == NULL) {
+ perror("malloc");
+ ret = -ENOMEM;
+ goto error;
+ }
+
+ /* Copy common data */
+ cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
+ cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
+
+ cmd_ctx->llm->data_size = size;
+ cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
+
+ return buf_size;
+
+error:
+ return ret;
+}
+
+/*
+ * Update the kernel pollfd set of all channel fd available over all tracing
+ * session. Add the wakeup pipe at the end of the set.
+ */
+static int update_kernel_pollfd(void)
+{
+ int i = 0;
+ /*
+ * The wakup pipe and the quit pipe are needed so the number of fds starts
+ * at 2 for those pipes.
+ */
+ unsigned int nb_fd = 2;
+ struct ltt_session *session;
+ struct ltt_kernel_channel *channel;
+
+ DBG("Updating kernel_pollfd");
+
+ /* Get the number of channel of all kernel session */
+ lock_session_list();
+ cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+ lock_session(session);
+ if (session->kernel_session == NULL) {
+ unlock_session(session);
+ continue;
+ }
+ nb_fd += session->kernel_session->channel_count;
+ unlock_session(session);
+ }
+
+ DBG("Resizing kernel_pollfd to size %d", nb_fd);
+
+ kernel_pollfd = realloc(kernel_pollfd, nb_fd * sizeof(struct pollfd));
+ if (kernel_pollfd == NULL) {
+ perror("malloc kernel_pollfd");
+ goto error;
+ }
+
+ cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+ lock_session(session);
+ if (session->kernel_session == NULL) {
+ unlock_session(session);
+ continue;
+ }
+ if (i >= nb_fd) {
+ ERR("To much channel for kernel_pollfd size");
+ unlock_session(session);
+ break;
+ }
+ cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
+ kernel_pollfd[i].fd = channel->fd;
+ kernel_pollfd[i].events = POLLIN | POLLRDNORM;
+ i++;
+ }
+ unlock_session(session);
+ }
+ unlock_session_list();
+
+ /* Adding wake up pipe */
+ kernel_pollfd[nb_fd - 2].fd = kernel_poll_pipe[0];
+ kernel_pollfd[nb_fd - 2].events = POLLIN;
+
+ /* Adding the quit pipe */
+ kernel_pollfd[nb_fd - 1].fd = thread_quit_pipe[0];
+
+ return nb_fd;
+
+error:
+ unlock_session_list();
+ return -1;
+}
+
+/*
+ * Find the channel fd from 'fd' over all tracing session. When found, check
+ * for new channel stream and send those stream fds to the kernel consumer.
+ *
+ * Useful for CPU hotplug feature.
+ */
+static int update_kernel_stream(int fd)
+{
+ int ret = 0;
+ struct ltt_session *session;
+ struct ltt_kernel_channel *channel;
+
+ DBG("Updating kernel streams for channel fd %d", fd);
+
+ lock_session_list();
+ cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+ lock_session(session);
+ if (session->kernel_session == NULL) {
+ unlock_session(session);
+ continue;
+ }
+
+ /* This is not suppose to be 0 but this is an extra security check */
+ if (session->kernel_session->consumer_fd == 0) {
+ session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
+ }
+
+ cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
+ if (channel->fd == fd) {
+ DBG("Channel found, updating kernel streams");
+ ret = kernel_open_channel_stream(channel);
+ if (ret < 0) {
+ goto end;
+ }
+
+ /*
+ * Have we already sent fds to the consumer? If yes, it means that
+ * tracing is started so it is safe to send our updated stream fds.
+ */
+ if (session->kernel_session->kconsumer_fds_sent == 1) {
+ ret = send_kconsumerd_channel_fds(session->kernel_session->consumer_fd,
+ channel);
+ if (ret < 0) {
+ goto end;
+ }
+ }
+ goto end;
+ }
+ }
+ unlock_session(session);
+ }
+
+end:
+ unlock_session_list();
+ if (session) {
+ unlock_session(session);
+ }
+ return ret;
+}
+
+/*
+ * This thread manage event coming from the kernel.
+ *
+ * Features supported in this thread:
+ * -) CPU Hotplug
+ */
+static void *thread_manage_kernel(void *data)
+{
+ int ret, i, nb_fd = 0;
+ char tmp;
+ int update_poll_flag = 1;
+
+ DBG("Thread manage kernel started");
+
+ while (1) {
+ if (update_poll_flag == 1) {
+ nb_fd = update_kernel_pollfd();
+ if (nb_fd < 0) {
+ goto error;
+ }
+ update_poll_flag = 0;
+ }