+ /* 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;
+}
+
+/*
+ * update_kernel_stream
+ *
+ * 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;
+ }
+ 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(kconsumerd_cmd_sock, channel);
+ if (ret < 0) {
+ goto end;
+ }
+ }
+ goto end;
+ }
+ }
+ unlock_session(session);
+ }
+
+end:
+ unlock_session_list();
+ if (session) {
+ unlock_session(session);
+ }
+ return ret;
+}
+
+/*
+ * thread_manage_kernel
+ *
+ * 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;
+ }
+
+ DBG("Polling on %d fds", nb_fd);
+
+ /* Poll infinite value of time */
+ ret = poll(kernel_pollfd, nb_fd, -1);
+ if (ret < 0) {
+ perror("poll kernel thread");
+ goto error;
+ } else if (ret == 0) {
+ /* Should not happen since timeout is infinite */
+ continue;
+ }
+
+ /* Thread quit pipe has been closed. Killing thread. */
+ if (kernel_pollfd[nb_fd - 1].revents == POLLNVAL) {
+ goto error;
+ }
+
+ DBG("Kernel poll event triggered");
+
+ /*
+ * Check if the wake up pipe was triggered. If so, the kernel_pollfd
+ * must be updated.
+ */
+ switch (kernel_pollfd[nb_fd - 2].revents) {
+ case POLLIN:
+ ret = read(kernel_poll_pipe[0], &tmp, 1);
+ update_poll_flag = 1;
+ continue;
+ case POLLERR:
+ goto error;
+ default:
+ break;
+ }
+
+ for (i = 0; i < nb_fd; i++) {
+ switch (kernel_pollfd[i].revents) {
+ /*
+ * New CPU detected by the kernel. Adding kernel stream to kernel
+ * session and updating the kernel consumer
+ */
+ case POLLIN | POLLRDNORM:
+ ret = update_kernel_stream(kernel_pollfd[i].fd);
+ if (ret < 0) {
+ continue;
+ }
+ break;
+ }
+ }
+ }
+
+error:
+ DBG("Kernel thread dying");
+ if (kernel_pollfd) {
+ free(kernel_pollfd);
+ }
+
+ close(kernel_poll_pipe[0]);
+ close(kernel_poll_pipe[1]);
+ return NULL;
+}
+
+/*
+ * thread_manage_kconsumerd
+ *
+ * This thread manage the kconsumerd error sent
+ * back to the session daemon.
+ */
+static void *thread_manage_kconsumerd(void *data)
+{
+ int sock = 0, ret;
+ enum lttcomm_return_code code;
+ struct pollfd pollfd[2];
+
+ DBG("[thread] Manage kconsumerd started");
+
+ ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
+ if (ret < 0) {
+ goto error;
+ }
+
+ /* First fd is always the quit pipe */
+ pollfd[0].fd = thread_quit_pipe[0];
+
+ /* Apps socket */
+ pollfd[1].fd = kconsumerd_err_sock;
+ pollfd[1].events = POLLIN;
+
+ /* Inifinite blocking call, waiting for transmission */
+ ret = poll(pollfd, 2, -1);
+ if (ret < 0) {
+ perror("poll kconsumerd thread");
+ goto error;
+ }
+
+ /* Thread quit pipe has been closed. Killing thread. */
+ if (pollfd[0].revents == POLLNVAL) {
+ goto error;
+ } else if (pollfd[1].revents == POLLERR) {
+ ERR("Kconsumerd err socket poll error");
+ goto error;
+ }
+
+ sock = lttcomm_accept_unix_sock(kconsumerd_err_sock);
+ if (sock < 0) {
+ goto error;
+ }
+
+ /* Getting status code from kconsumerd */
+ ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
+ if (ret <= 0) {
+ goto error;
+ }
+
+ if (code == KCONSUMERD_COMMAND_SOCK_READY) {
+ kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path);
+ if (kconsumerd_cmd_sock < 0) {
+ sem_post(&kconsumerd_sem);
+ perror("kconsumerd connect");
+ goto error;
+ }
+ /* Signal condition to tell that the kconsumerd is ready */
+ sem_post(&kconsumerd_sem);
+ DBG("Kconsumerd command socket ready");
+ } else {
+ DBG("Kconsumerd error when waiting for SOCK_READY : %s",
+ lttcomm_get_readable_code(-code));
+ goto error;
+ }
+
+ /* Wait for any kconsumerd error */
+ ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
+ if (ret <= 0) {
+ ERR("Kconsumerd closed the command socket");
+ goto error;
+ }
+
+ ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code));
+
+error:
+ DBG("Kconsumerd thread dying");
+ if (kconsumerd_err_sock) {
+ close(kconsumerd_err_sock);
+ }
+ if (kconsumerd_cmd_sock) {
+ close(kconsumerd_cmd_sock);
+ }
+ if (sock) {
+ close(sock);
+ }
+
+ unlink(kconsumerd_err_unix_sock_path);
+ unlink(kconsumerd_cmd_unix_sock_path);
+
+ kconsumerd_pid = 0;
+ return NULL;
+}
+
+/*
+ * thread_manage_apps
+ *
+ * This thread manage the application socket communication
+ */
+static void *thread_manage_apps(void *data)
+{
+ int sock = 0, ret;
+ struct pollfd pollfd[2];
+
+ /* TODO: Something more elegant is needed but fine for now */
+ /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
+ * for 32-bit vs 64-bit compat processes. */
+ /* replicate in ust with version number */
+ struct {
+ int reg; /* 1:register, 0:unregister */
+ pid_t pid;
+ uid_t uid;
+ } reg_msg;
+
+ DBG("[thread] Manage apps started");
+
+ ret = lttcomm_listen_unix_sock(apps_sock);
+ if (ret < 0) {
+ goto error;
+ }
+
+ /* First fd is always the quit pipe */
+ pollfd[0].fd = thread_quit_pipe[0];
+
+ /* Apps socket */
+ pollfd[1].fd = apps_sock;
+ pollfd[1].events = POLLIN;
+
+ /* Notify all applications to register */
+ notify_apps(default_global_apps_pipe);
+
+ while (1) {
+ DBG("Accepting application registration");
+
+ /* Inifinite blocking call, waiting for transmission */
+ ret = poll(pollfd, 2, -1);
+ if (ret < 0) {
+ perror("poll apps thread");
+ goto error;
+ }
+
+ /* Thread quit pipe has been closed. Killing thread. */
+ if (pollfd[0].revents == POLLNVAL) {
+ goto error;
+ } else if (pollfd[1].revents == POLLERR) {
+ ERR("Apps socket poll error");
+ goto error;
+ }
+
+ sock = lttcomm_accept_unix_sock(apps_sock);
+ if (sock < 0) {
+ goto error;
+ }
+
+ /*
+ * Basic recv here to handle the very simple data
+ * that the libust send to register (reg_msg).
+ */
+ ret = recv(sock, ®_msg, sizeof(reg_msg), 0);
+ if (ret < 0) {
+ perror("recv");
+ continue;
+ }
+
+ /* Add application to the global traceable list */
+ if (reg_msg.reg == 1) {
+ /* Registering */
+ ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
+ if (ret < 0) {
+ /* register_traceable_app only return an error with
+ * ENOMEM. At this point, we better stop everything.
+ */
+ goto error;
+ }
+ } else {
+ /* Unregistering */
+ unregister_traceable_app(reg_msg.pid);
+ }
+ }
+
+error:
+ DBG("Apps thread dying");
+ if (apps_sock) {
+ close(apps_sock);
+ }
+ if (sock) {
+ close(sock);
+ }
+
+ unlink(apps_unix_sock_path);
+ return NULL;
+}
+
+/*
+ * spawn_kconsumerd_thread
+ *
+ * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
+ * exec or it will fails.
+ */
+static int spawn_kconsumerd_thread(void)
+{
+ int ret;
+
+ /* Setup semaphore */
+ sem_init(&kconsumerd_sem, 0, 0);
+
+ ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
+ if (ret != 0) {
+ perror("pthread_create kconsumerd");
+ goto error;
+ }
+
+ /* Wait for the kconsumerd thread to be ready */
+ sem_wait(&kconsumerd_sem);
+
+ if (kconsumerd_pid == 0) {
+ ERR("Kconsumerd did not start");
+ goto error;
+ }
+
+ return 0;
+
+error:
+ ret = LTTCOMM_KERN_CONSUMER_FAIL;
+ return ret;
+}
+
+/*
+ * spawn_kconsumerd
+ *
+ * Fork and exec a kernel consumer daemon (kconsumerd).
+ *
+ * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
+ * file descriptor using the libkernelctl or kernel-ctl functions. So, a
+ * kernel consumer MUST only be spawned before creating a kernel session.
+ *
+ * Return pid if successful else -1.
+ */
+static pid_t spawn_kconsumerd(void)
+{
+ int ret;
+ pid_t pid;
+
+ DBG("Spawning kconsumerd");
+
+ pid = fork();
+ if (pid == 0) {
+ /*
+ * Exec kconsumerd.
+ */
+ execlp("ltt-kconsumerd", "ltt-kconsumerd", "--quiet", NULL);
+ if (errno != 0) {
+ perror("kernel start consumer exec");
+ }
+ exit(EXIT_FAILURE);
+ } else if (pid > 0) {
+ ret = pid;
+ goto error;
+ } else {
+ perror("kernel start consumer fork");
+ ret = -errno;
+ goto error;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * start_kconsumerd
+ *
+ * Spawn the kconsumerd daemon and session daemon thread.
+ */
+static int start_kconsumerd(void)
+{
+ int ret;
+
+ pthread_mutex_lock(&kconsumerd_pid_mutex);
+ if (kconsumerd_pid != 0) {
+ pthread_mutex_unlock(&kconsumerd_pid_mutex);
+ goto end;
+ }
+
+ ret = spawn_kconsumerd();
+ if (ret < 0) {
+ ERR("Spawning kconsumerd failed");
+ ret = LTTCOMM_KERN_CONSUMER_FAIL;
+ pthread_mutex_unlock(&kconsumerd_pid_mutex);
+ goto error;
+ }
+
+ /* Setting up the global kconsumerd_pid */
+ kconsumerd_pid = ret;
+ pthread_mutex_unlock(&kconsumerd_pid_mutex);
+
+ DBG("Kconsumerd pid %d", ret);
+
+ DBG("Spawning kconsumerd thread");
+ ret = spawn_kconsumerd_thread();
+ if (ret < 0) {
+ ERR("Fatal error spawning kconsumerd thread");
+ goto error;
+ }
+
+end:
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * modprobe_kernel_modules
+ */
+static int modprobe_kernel_modules(void)
+{
+ int ret = 0, i = 0;
+ char modprobe[256];
+
+ while (kernel_modules_list[i] != NULL) {
+ ret = snprintf(modprobe, sizeof(modprobe), "/sbin/modprobe %s",
+ kernel_modules_list[i]);
+ if (ret < 0) {
+ perror("snprintf modprobe");
+ goto error;
+ }
+ ret = system(modprobe);
+ if (ret < 0) {
+ ERR("Unable to load module %s", kernel_modules_list[i]);
+ }
+ DBG("Modprobe successfully %s", kernel_modules_list[i]);
+ i++;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * mount_debugfs
+ */
+static int mount_debugfs(char *path)
+{
+ int ret;
+ char *type = "debugfs";
+
+ ret = mkdir_recursive(path, S_IRWXU | S_IRWXG);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ret = mount(type, path, type, 0, NULL);
+ if (ret < 0) {
+ perror("mount debugfs");
+ goto error;
+ }
+
+ DBG("Mounted debugfs successfully at %s", path);
+
+error:
+ return ret;
+}
+
+/*
+ * init_kernel_tracer
+ *
+ * Setup necessary data for kernel tracer action.
+ */
+static void init_kernel_tracer(void)
+{
+ int ret;
+ char *proc_mounts = "/proc/mounts";
+ char line[256];
+ char *debugfs_path = NULL, *lttng_path;
+ FILE *fp;
+
+ /* Detect debugfs */
+ fp = fopen(proc_mounts, "r");
+ if (fp == NULL) {
+ ERR("Unable to probe %s", proc_mounts);
+ goto error;
+ }
+
+ while (fgets(line, sizeof(line), fp) != NULL) {
+ if (strstr(line, "debugfs") != NULL) {
+ /* Remove first string */
+ strtok(line, " ");
+ /* Dup string here so we can reuse line later on */
+ debugfs_path = strdup(strtok(NULL, " "));
+ DBG("Got debugfs path : %s", debugfs_path);
+ break;
+ }
+ }
+
+ fclose(fp);
+
+ /* Mount debugfs if needded */
+ if (debugfs_path == NULL) {
+ ret = asprintf(&debugfs_path, "/mnt/debugfs");
+ if (ret < 0) {
+ perror("asprintf debugfs path");
+ goto error;
+ }
+ ret = mount_debugfs(debugfs_path);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+ /* Modprobe lttng kernel modules */
+ ret = modprobe_kernel_modules();
+ if (ret < 0) {
+ goto error;
+ }
+
+ /* Setup lttng kernel path */
+ ret = asprintf(<tng_path, "%s/lttng", debugfs_path);
+ if (ret < 0) {
+ perror("asprintf lttng path");
+ goto error;
+ }
+
+ /* Open debugfs lttng */
+ kernel_tracer_fd = open(lttng_path, O_RDWR);
+ if (kernel_tracer_fd < 0) {
+ DBG("Failed to open %s", lttng_path);
+ goto error;
+ }
+
+ free(lttng_path);
+ free(debugfs_path);
+ DBG("Kernel tracer fd %d", kernel_tracer_fd);
+ return;
+
+error:
+ if (lttng_path) {
+ free(lttng_path);
+ }
+ if (debugfs_path) {
+ free(debugfs_path);
+ }
+ WARN("No kernel tracer available");
+ kernel_tracer_fd = 0;
+ return;
+}
+
+/*
+ * start_kernel_trace
+ *
+ * Start tracing by creating trace directory and sending FDs to the kernel
+ * consumer.
+ */
+static int start_kernel_trace(struct ltt_kernel_session *session)
+{
+ int ret = 0;
+
+ if (session->kconsumer_fds_sent == 0) {
+ ret = send_kconsumerd_fds(kconsumerd_cmd_sock, session);
+ if (ret < 0) {
+ ERR("Send kconsumerd fds failed");
+ ret = LTTCOMM_KERN_CONSUMER_FAIL;
+ goto error;
+ }
+
+ session->kconsumer_fds_sent = 1;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Notify kernel thread to update it's pollfd.
+ */
+static int notify_kernel_pollfd(void)
+{
+ int ret;
+
+ /* Inform kernel thread of the new kernel channel */
+ ret = write(kernel_poll_pipe[1], "!", 1);
+ if (ret < 0) {
+ perror("write kernel poll pipe");
+ }
+
+ return ret;
+}
+
+/*
+ * init_default_channel
+ *
+ * Allocate a channel structure and fill it.
+ */
+static struct lttng_channel *init_default_channel(void)
+{
+ struct lttng_channel *chan;
+
+ chan = malloc(sizeof(struct lttng_channel));
+ if (chan == NULL) {
+ perror("init channel malloc");
+ goto error;
+ }
+
+ if (snprintf(chan->name, NAME_MAX, DEFAULT_CHANNEL_NAME) < 0) {
+ perror("snprintf defautl channel name");
+ return NULL;
+ }
+
+ chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
+ chan->attr.subbuf_size = DEFAULT_CHANNEL_SUBBUF_SIZE;
+ chan->attr.num_subbuf = DEFAULT_CHANNEL_SUBBUF_NUM;
+ chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
+ chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
+ chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
+
+error:
+ return chan;
+}
+
+/*
+ * create_kernel_session
+ *
+ * Create a kernel tracer session then create the default channel.
+ */
+static int create_kernel_session(struct ltt_session *session)
+{
+ int ret;
+
+ DBG("Creating kernel session");
+
+ ret = kernel_create_session(session, kernel_tracer_fd);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_SESS_FAIL;
+ goto error;
+ }
+
+ ret = mkdir_recursive(session->path, S_IRWXU | S_IRWXG );
+ if (ret < 0) {
+ if (ret != EEXIST) {
+ ERR("Trace directory creation error");
+ goto error;
+ }
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Using the session list, filled a lttng_session array to send back to the
+ * client for session listing.
+ *
+ * The session list lock MUST be acquired before calling this function. Use
+ * lock_session_list() and unlock_session_list().
+ */
+static void list_lttng_sessions(struct lttng_session *sessions)
+{
+ int i = 0;
+ struct ltt_session *session;
+
+ DBG("Getting all available session");
+ /*
+ * Iterate over session list and append data after the control struct in
+ * the buffer.
+ */
+ cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+ strncpy(sessions[i].path, session->path, PATH_MAX);
+ strncpy(sessions[i].name, session->name, NAME_MAX);
+ i++;
+ }
+}
+
+/*
+ * process_client_msg
+ *
+ * Process the command requested by the lttng client within the command
+ * context structure. This function make sure that the return structure (llm)
+ * is set and ready for transmission before returning.
+ *
+ * Return any error encountered or 0 for success.
+ */
+static int process_client_msg(struct command_ctx *cmd_ctx)
+{
+ int ret = LTTCOMM_OK;
+
+ DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
+
+ /* Listing commands don't need a session */
+ switch (cmd_ctx->lsm->cmd_type) {
+ case LTTNG_CREATE_SESSION:
+ case LTTNG_LIST_SESSIONS:
+ case LTTNG_LIST_EVENTS:
+ case LTTNG_KERNEL_LIST_EVENTS:
+ case LTTNG_LIST_TRACEABLE_APPS:
+ break;
+ default:
+ DBG("Getting session %s by name", cmd_ctx->lsm->session_name);
+ cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session_name);
+ if (cmd_ctx->session == NULL) {
+ /* If session name not found */
+ if (cmd_ctx->lsm->session_name != NULL) {
+ ret = LTTCOMM_SESS_NOT_FOUND;
+ } else { /* If no session name specified */
+ ret = LTTCOMM_SELECT_SESS;
+ }
+ goto error;
+ } else {
+ /* Acquire lock for the session */
+ lock_session(cmd_ctx->session);
+ }
+ break;
+ }
+
+ /*
+ * Check kernel command for kernel session.
+ */
+ switch (cmd_ctx->lsm->cmd_type) {
+ case LTTNG_KERNEL_ADD_CONTEXT:
+ case LTTNG_KERNEL_DISABLE_ALL_EVENT:
+ case LTTNG_KERNEL_DISABLE_CHANNEL:
+ case LTTNG_KERNEL_DISABLE_EVENT:
+ case LTTNG_KERNEL_ENABLE_ALL_EVENT:
+ case LTTNG_KERNEL_ENABLE_CHANNEL:
+ case LTTNG_KERNEL_ENABLE_EVENT:
+ case LTTNG_KERNEL_LIST_EVENTS:
+ /* Kernel tracer check */
+ if (kernel_tracer_fd == 0) {
+ init_kernel_tracer();
+ if (kernel_tracer_fd == 0) {
+ ret = LTTCOMM_KERN_NA;
+ goto error;
+ }
+ }
+
+ /* Need a session for kernel command */
+ if (cmd_ctx->lsm->cmd_type != LTTNG_KERNEL_LIST_EVENTS &&
+ cmd_ctx->session->kernel_session == NULL) {
+
+ ret = create_kernel_session(cmd_ctx->session);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_SESS_FAIL;
+ goto error;
+ }
+
+ /* Start the kernel consumer daemon */
+ if (kconsumerd_pid == 0) {
+ ret = start_kconsumerd();
+ if (ret < 0) {
+ goto error;
+ }
+ }
+ }
+ }
+
+#ifdef DISABLED