+ DBG("consumer fds (metadata and channel streams) sent");
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * Send all stream fds of the UST session to the consumer.
+ */
+static int send_ustconsumer_session_streams(struct consumer_data *consumer_data,
+ struct ltt_ust_session *session)
+{
+ int ret;
+ struct ltt_ust_channel *chan;
+ struct lttcomm_consumer_msg lum;
+ int sock = session->consumer_fd;
+
+ DBG("Sending metadata stream fd");
+
+ /* Extra protection. It's NOT supposed to be set to 0 at this point */
+ if (session->consumer_fd == 0) {
+ session->consumer_fd = consumer_data->cmd_sock;
+ }
+
+ if (session->metadata->obj->shm_fd != 0) {
+ int fds[2];
+
+ /* Send metadata channel fd */
+ lum.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL;
+ lum.u.channel.channel_key = session->metadata->obj->shm_fd;
+ lum.u.channel.max_sb_size = session->metadata->attr.subbuf_size;
+ lum.u.channel.mmap_len = 0; /* for kernel */
+ DBG("Sending metadata channel %d to consumer", lum.u.stream.stream_key);
+ ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
+ if (ret < 0) {
+ perror("send consumer channel");
+ goto error;
+ }
+ fds[0] = session->metadata->obj->shm_fd;
+ fds[1] = session->metadata->obj->wait_fd;
+ ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
+ if (ret < 0) {
+ perror("send consumer metadata channel");
+ goto error;
+ }
+
+ /* Send metadata stream fd */
+ lum.cmd_type = LTTNG_CONSUMER_ADD_STREAM;
+ lum.u.stream.channel_key = session->metadata->obj->shm_fd;
+ lum.u.stream.stream_key = session->metadata->stream_obj->shm_fd;
+ lum.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM;
+ lum.u.stream.output = DEFAULT_UST_CHANNEL_OUTPUT;
+ lum.u.stream.mmap_len = session->metadata->stream_obj->memory_map_size;
+ strncpy(lum.u.stream.path_name, session->metadata->pathname, PATH_MAX - 1);
+ lum.u.stream.path_name[PATH_MAX - 1] = '\0';
+ DBG("Sending metadata stream %d to consumer", lum.u.stream.stream_key);
+ ret = lttcomm_send_unix_sock(sock, &lum, sizeof(lum));
+ if (ret < 0) {
+ perror("send consumer metadata stream");
+ goto error;
+ }
+ fds[0] = session->metadata->stream_obj->shm_fd;
+ fds[1] = session->metadata->stream_obj->wait_fd;
+ ret = lttcomm_send_fds_unix_sock(sock, fds, 2);
+ if (ret < 0) {
+ perror("send consumer stream");
+ goto error;
+ }
+ }
+
+ cds_list_for_each_entry(chan, &session->channels.head, list) {
+ ret = send_ustconsumer_channel_streams(consumer_data, sock, chan);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+ DBG("consumer 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 poll set of all channel fd available over all tracing
+ * session. Add the wakeup pipe at the end of the set.
+ */
+static int update_kernel_poll(struct lttng_poll_event *events)
+{
+ int ret;
+ struct ltt_session *session;
+ struct ltt_kernel_channel *channel;
+
+ DBG("Updating kernel poll set");
+
+ session_lock_list();
+ cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+ session_lock(session);
+ if (session->kernel_session == NULL) {
+ session_unlock(session);
+ continue;
+ }
+
+ cds_list_for_each_entry(channel,
+ &session->kernel_session->channel_list.head, list) {
+ /* Add channel fd to the kernel poll set */
+ ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
+ if (ret < 0) {
+ session_unlock(session);
+ goto error;
+ }
+ DBG("Channel fd %d added to kernel set", channel->fd);
+ }
+ session_unlock(session);
+ }
+ session_unlock_list();
+
+ return 0;
+
+error:
+ session_unlock_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(struct consumer_data *consumer_data, int fd)
+{
+ int ret = 0;
+ struct ltt_session *session;
+ struct ltt_kernel_channel *channel;
+
+ DBG("Updating kernel streams for channel fd %d", fd);
+
+ session_lock_list();
+ cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+ session_lock(session);
+ if (session->kernel_session == NULL) {
+ session_unlock(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 = consumer_data->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 error;
+ }
+
+ /*
+ * 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->consumer_fds_sent == 1) {
+ ret = send_kconsumer_channel_streams(consumer_data,
+ session->kernel_session->consumer_fd, channel);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+ goto error;
+ }
+ }
+ session_unlock(session);
+ }
+ session_unlock_list();
+ return ret;
+
+error:
+ session_unlock(session);
+ session_unlock_list();
+ 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, pollfd, update_poll_flag = 1;
+ uint32_t revents, nb_fd;
+ char tmp;
+ struct lttng_poll_event events;
+
+ DBG("Thread manage kernel started");
+
+ ret = create_thread_poll_set(&events, 2);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
+ if (ret < 0) {
+ goto error;
+ }
+
+ while (1) {
+ if (update_poll_flag == 1) {
+ /*
+ * Reset number of fd in the poll set. Always 2 since there is the thread
+ * quit pipe and the kernel pipe.
+ */
+ events.nb_fd = 2;
+
+ ret = update_kernel_poll(&events);
+ if (ret < 0) {
+ goto error;
+ }
+ update_poll_flag = 0;
+ }
+
+ nb_fd = LTTNG_POLL_GETNB(&events);
+
+ DBG("Thread kernel polling on %d fds", nb_fd);
+
+ /* Zeroed the poll events */
+ lttng_poll_reset(&events);
+
+ /* Poll infinite value of time */
+ ret = lttng_poll_wait(&events, -1);
+ if (ret < 0) {
+ goto error;
+ } else if (ret == 0) {
+ /* Should not happen since timeout is infinite */
+ ERR("Return value of poll is 0 with an infinite timeout.\n"
+ "This should not have happened! Continuing...");
+ continue;
+ }
+
+ for (i = 0; i < nb_fd; i++) {
+ /* Fetch once the poll data */
+ revents = LTTNG_POLL_GETEV(&events, i);
+ pollfd = LTTNG_POLL_GETFD(&events, i);
+
+ /* Thread quit pipe has been closed. Killing thread. */
+ ret = check_thread_quit_pipe(pollfd, revents);
+ if (ret) {
+ goto error;
+ }
+
+ /* Check for data on kernel pipe */
+ if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
+ ret = read(kernel_poll_pipe[0], &tmp, 1);
+ update_poll_flag = 1;
+ continue;
+ } else {
+ /*
+ * New CPU detected by the kernel. Adding kernel stream to
+ * kernel session and updating the kernel consumer
+ */
+ if (revents & LPOLLIN) {
+ ret = update_kernel_stream(&kconsumer_data, pollfd);
+ if (ret < 0) {
+ continue;
+ }
+ break;
+ /*
+ * TODO: We might want to handle the LPOLLERR | LPOLLHUP
+ * and unregister kernel stream at this point.
+ */
+ }
+ }
+ }
+ }
+
+error:
+ DBG("Kernel thread dying");
+ close(kernel_poll_pipe[0]);
+ close(kernel_poll_pipe[1]);
+
+ lttng_poll_clean(&events);
+
+ return NULL;
+}
+
+/*
+ * This thread manage the consumer error sent back to the session daemon.
+ */
+static void *thread_manage_consumer(void *data)
+{
+ int sock = 0, i, ret, pollfd;
+ uint32_t revents, nb_fd;
+ enum lttcomm_return_code code;
+ struct lttng_poll_event events;
+ struct consumer_data *consumer_data = data;
+
+ DBG("[thread] Manage consumer started");
+
+ ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
+ if (ret < 0) {
+ goto error;
+ }
+
+ /*
+ * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
+ * Nothing more will be added to this poll set.
+ */
+ ret = create_thread_poll_set(&events, 2);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
+ if (ret < 0) {
+ goto error;
+ }
+
+ nb_fd = LTTNG_POLL_GETNB(&events);
+
+ /* Inifinite blocking call, waiting for transmission */
+ ret = lttng_poll_wait(&events, -1);
+ if (ret < 0) {
+ goto error;
+ }
+
+ for (i = 0; i < nb_fd; i++) {
+ /* Fetch once the poll data */
+ revents = LTTNG_POLL_GETEV(&events, i);
+ pollfd = LTTNG_POLL_GETFD(&events, i);
+
+ /* Thread quit pipe has been closed. Killing thread. */
+ ret = check_thread_quit_pipe(pollfd, revents);
+ if (ret) {
+ goto error;
+ }
+
+ /* Event on the registration socket */
+ if (pollfd == consumer_data->err_sock) {
+ if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+ ERR("consumer err socket poll error");
+ goto error;
+ }
+ }
+ }
+
+ sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
+ if (sock < 0) {
+ goto error;
+ }
+
+ DBG2("Receiving code from consumer err_sock");
+
+ /* Getting status code from kconsumerd */
+ ret = lttcomm_recv_unix_sock(sock, &code,
+ sizeof(enum lttcomm_return_code));
+ if (ret <= 0) {
+ goto error;
+ }
+
+ if (code == CONSUMERD_COMMAND_SOCK_READY) {
+ consumer_data->cmd_sock =
+ lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
+ if (consumer_data->cmd_sock < 0) {
+ sem_post(&consumer_data->sem);
+ perror("consumer connect");
+ goto error;
+ }
+ /* Signal condition to tell that the kconsumerd is ready */
+ sem_post(&consumer_data->sem);
+ DBG("consumer command socket ready");
+ } else {
+ ERR("consumer error when waiting for SOCK_READY : %s",
+ lttcomm_get_readable_code(-code));
+ goto error;
+ }
+
+ /* Remove the kconsumerd error sock since we've established a connexion */
+ ret = lttng_poll_del(&events, consumer_data->err_sock);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
+ if (ret < 0) {
+ goto error;
+ }
+
+ /* Update number of fd */
+ nb_fd = LTTNG_POLL_GETNB(&events);
+
+ /* Inifinite blocking call, waiting for transmission */
+ ret = lttng_poll_wait(&events, -1);
+ if (ret < 0) {
+ goto error;
+ }
+
+ for (i = 0; i < nb_fd; i++) {
+ /* Fetch once the poll data */
+ revents = LTTNG_POLL_GETEV(&events, i);
+ pollfd = LTTNG_POLL_GETFD(&events, i);
+
+ /* Thread quit pipe has been closed. Killing thread. */
+ ret = check_thread_quit_pipe(pollfd, revents);
+ if (ret) {
+ goto error;
+ }
+
+ /* Event on the kconsumerd socket */
+ if (pollfd == sock) {
+ if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+ ERR("consumer err socket second poll error");
+ goto error;
+ }
+ }
+ }
+
+ /* Wait for any kconsumerd error */
+ ret = lttcomm_recv_unix_sock(sock, &code,
+ sizeof(enum lttcomm_return_code));
+ if (ret <= 0) {
+ ERR("consumer closed the command socket");
+ goto error;
+ }
+
+ ERR("consumer return code : %s", lttcomm_get_readable_code(-code));
+
+error:
+ DBG("consumer thread dying");
+ close(consumer_data->err_sock);
+ close(consumer_data->cmd_sock);
+ close(sock);
+
+ unlink(consumer_data->err_unix_sock_path);
+ unlink(consumer_data->cmd_unix_sock_path);
+ consumer_data->pid = 0;
+
+ lttng_poll_clean(&events);
+
+ return NULL;
+}
+
+/*
+ * This thread manage application communication.
+ */
+static void *thread_manage_apps(void *data)
+{
+ int i, ret, pollfd;
+ uint32_t revents, nb_fd;
+ struct ust_command ust_cmd;
+ struct lttng_poll_event events;
+
+ DBG("[thread] Manage application started");
+
+ ret = create_thread_poll_set(&events, 2);
+ if (ret < 0) {
+ goto error;
+ }
+
+ ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
+ if (ret < 0) {
+ goto error;
+ }
+
+ while (1) {
+ /* Zeroed the events structure */
+ lttng_poll_reset(&events);
+
+ nb_fd = LTTNG_POLL_GETNB(&events);
+
+ DBG("Apps thread polling on %d fds", nb_fd);
+
+ /* Inifinite blocking call, waiting for transmission */
+ ret = lttng_poll_wait(&events, -1);
+ if (ret < 0) {
+ goto error;
+ }
+
+ for (i = 0; i < nb_fd; i++) {
+ /* Fetch once the poll data */
+ revents = LTTNG_POLL_GETEV(&events, i);
+ pollfd = LTTNG_POLL_GETFD(&events, i);
+
+ /* Thread quit pipe has been closed. Killing thread. */
+ ret = check_thread_quit_pipe(pollfd, revents);
+ if (ret) {
+ goto error;
+ }
+
+ /* Inspect the apps cmd pipe */
+ if (pollfd == apps_cmd_pipe[0]) {
+ if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+ ERR("Apps command pipe error");
+ goto error;
+ } else if (revents & LPOLLIN) {
+ /* Empty pipe */
+ ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
+ if (ret < 0 || ret < sizeof(ust_cmd)) {
+ perror("read apps cmd pipe");
+ goto error;
+ }
+
+ /* Register applicaton to the session daemon */
+ ret = ust_app_register(&ust_cmd.reg_msg,
+ ust_cmd.sock);
+ if (ret < 0) {
+ /* Only critical ENOMEM error can be returned here */
+ goto error;
+ }
+
+ ret = ustctl_register_done(ust_cmd.sock);
+ if (ret < 0) {
+ /*
+ * If the registration is not possible, we simply
+ * unregister the apps and continue
+ */
+ ust_app_unregister(ust_cmd.sock);
+ } else {
+ /*
+ * We just need here to monitor the close of the UST
+ * socket and poll set monitor those by default.
+ */
+ ret = lttng_poll_add(&events, ust_cmd.sock, 0);
+ if (ret < 0) {
+ goto error;
+ }
+
+ DBG("Apps with sock %d added to poll set",
+ ust_cmd.sock);
+ }
+ break;
+ }
+ } else {
+ /*
+ * At this point, we know that a registered application made
+ * the event at poll_wait.
+ */
+ if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+ /* Removing from the poll set */
+ ret = lttng_poll_del(&events, pollfd);
+ if (ret < 0) {
+ goto error;
+ }
+
+ /* Socket closed */
+ ust_app_unregister(pollfd);
+ break;
+ }
+ }
+ }
+ }
+
+error:
+ DBG("Application communication apps dying");
+ close(apps_cmd_pipe[0]);
+ close(apps_cmd_pipe[1]);
+
+ lttng_poll_clean(&events);
+
+ return NULL;
+}
+
+/*
+ * Dispatch request from the registration threads to the application
+ * communication thread.
+ */
+static void *thread_dispatch_ust_registration(void *data)
+{
+ int ret;
+ struct cds_wfq_node *node;
+ struct ust_command *ust_cmd = NULL;
+
+ DBG("[thread] Dispatch UST command started");
+
+ while (!dispatch_thread_exit) {
+ /* Atomically prepare the queue futex */
+ futex_nto1_prepare(&ust_cmd_queue.futex);
+
+ do {
+ /* Dequeue command for registration */
+ node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
+ if (node == NULL) {
+ DBG("Woken up but nothing in the UST command queue");
+ /* Continue thread execution */
+ break;
+ }
+
+ ust_cmd = caa_container_of(node, struct ust_command, node);
+
+ DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
+ " gid:%d sock:%d name:%s (version %d.%d)",
+ ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
+ ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
+ ust_cmd->sock, ust_cmd->reg_msg.name,
+ ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
+ /*
+ * Inform apps thread of the new application registration. This
+ * call is blocking so we can be assured that the data will be read
+ * at some point in time or wait to the end of the world :)
+ */
+ ret = write(apps_cmd_pipe[1], ust_cmd,
+ sizeof(struct ust_command));
+ if (ret < 0) {
+ perror("write apps cmd pipe");
+ if (errno == EBADF) {
+ /*
+ * We can't inform the application thread to process
+ * registration. We will exit or else application
+ * registration will not occur and tracing will never
+ * start.
+ */
+ goto error;
+ }
+ }
+ free(ust_cmd);
+ } while (node != NULL);
+
+ /* Futex wait on queue. Blocking call on futex() */
+ futex_nto1_wait(&ust_cmd_queue.futex);
+ }
+
+error:
+ DBG("Dispatch thread dying");
+ return NULL;
+}
+
+/*
+ * This thread manage application registration.
+ */
+static void *thread_registration_apps(void *data)
+{
+ int sock = 0, i, ret, pollfd;
+ uint32_t revents, nb_fd;
+ struct lttng_poll_event events;
+ /*
+ * Get allocated in this thread, enqueued to a global queue, dequeued and
+ * freed in the manage apps thread.
+ */
+ struct ust_command *ust_cmd = NULL;
+
+ DBG("[thread] Manage application registration started");
+
+ ret = lttcomm_listen_unix_sock(apps_sock);
+ if (ret < 0) {
+ goto error;
+ }
+
+ /*
+ * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
+ * more will be added to this poll set.
+ */
+ ret = create_thread_poll_set(&events, 2);
+ if (ret < 0) {
+ goto error;
+ }
+
+ /* Add the application registration socket */
+ ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
+ if (ret < 0) {
+ goto error;
+ }
+
+ /* Notify all applications to register */
+ ret = notify_ust_apps(1);
+ if (ret < 0) {
+ ERR("Failed to notify applications or create the wait shared memory.\n"
+ "Execution continues but there might be problem for already\n"
+ "running applications that wishes to register.");
+ }
+
+ while (1) {
+ DBG("Accepting application registration");
+
+ nb_fd = LTTNG_POLL_GETNB(&events);
+
+ /* Inifinite blocking call, waiting for transmission */
+ ret = lttng_poll_wait(&events, -1);
+ if (ret < 0) {
+ goto error;
+ }
+
+ for (i = 0; i < nb_fd; i++) {
+ /* Fetch once the poll data */
+ revents = LTTNG_POLL_GETEV(&events, i);
+ pollfd = LTTNG_POLL_GETFD(&events, i);
+
+ /* Thread quit pipe has been closed. Killing thread. */
+ ret = check_thread_quit_pipe(pollfd, revents);
+ if (ret) {
+ goto error;
+ }
+
+ /* Event on the registration socket */
+ if (pollfd == apps_sock) {
+ if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
+ ERR("Register apps socket poll error");
+ goto error;
+ } else if (revents & LPOLLIN) {
+ sock = lttcomm_accept_unix_sock(apps_sock);
+ if (sock < 0) {
+ goto error;
+ }
+
+ /* Create UST registration command for enqueuing */
+ ust_cmd = malloc(sizeof(struct ust_command));
+ if (ust_cmd == NULL) {
+ perror("ust command malloc");
+ goto error;
+ }
+
+ /*
+ * Using message-based transmissions to ensure we don't
+ * have to deal with partially received messages.
+ */
+ ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
+ sizeof(struct ust_register_msg));
+ if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
+ if (ret < 0) {
+ perror("lttcomm_recv_unix_sock register apps");
+ } else {
+ ERR("Wrong size received on apps register");
+ }
+ free(ust_cmd);
+ close(sock);
+ continue;
+ }
+
+ ust_cmd->sock = sock;
+
+ DBG("UST registration received with pid:%d ppid:%d uid:%d"
+ " gid:%d sock:%d name:%s (version %d.%d)",
+ ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
+ ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
+ ust_cmd->sock, ust_cmd->reg_msg.name,
+ ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
+
+ /*
+ * Lock free enqueue the registration request. The red pill
+ * has been taken! This apps will be part of the *system*.
+ */
+ cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
+
+ /*
+ * Wake the registration queue futex. Implicit memory
+ * barrier with the exchange in cds_wfq_enqueue.
+ */
+ futex_nto1_wake(&ust_cmd_queue.futex);
+ }
+ }
+ }
+ }
+
+error:
+ DBG("UST Registration thread dying");
+
+ /* Notify that the registration thread is gone */
+ notify_ust_apps(0);
+
+ close(apps_sock);
+ close(sock);
+ unlink(apps_unix_sock_path);
+
+ lttng_poll_clean(&events);
+
+ return NULL;
+}
+
+/*
+ * Start the thread_manage_consumer. This must be done after a lttng-consumerd
+ * exec or it will fails.
+ */
+static int spawn_consumer_thread(struct consumer_data *consumer_data)
+{
+ int ret;
+ struct timespec timeout;
+
+ timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT;
+ timeout.tv_nsec = 0;
+
+ /* Setup semaphore */
+ ret = sem_init(&consumer_data->sem, 0, 0);
+ if (ret < 0) {
+ PERROR("sem_init consumer semaphore");
+ goto error;
+ }
+
+ ret = pthread_create(&consumer_data->thread, NULL,
+ thread_manage_consumer, consumer_data);
+ if (ret != 0) {
+ PERROR("pthread_create consumer");
+ ret = -1;
+ goto error;
+ }
+
+ /* Get time for sem_timedwait absolute timeout */
+ ret = clock_gettime(CLOCK_REALTIME, &timeout);
+ if (ret < 0) {
+ PERROR("clock_gettime spawn consumer");
+ /* Infinite wait for the kconsumerd thread to be ready */
+ ret = sem_wait(&consumer_data->sem);
+ } else {
+ /* Normal timeout if the gettime was successful */
+ timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
+ ret = sem_timedwait(&consumer_data->sem, &timeout);
+ }
+
+ if (ret < 0) {
+ if (errno == ETIMEDOUT) {
+ /*
+ * Call has timed out so we kill the kconsumerd_thread and return
+ * an error.
+ */
+ ERR("The consumer thread was never ready. Killing it");
+ ret = pthread_cancel(consumer_data->thread);
+ if (ret < 0) {
+ PERROR("pthread_cancel consumer thread");
+ }
+ } else {
+ PERROR("semaphore wait failed consumer thread");
+ }
+ goto error;
+ }
+
+ pthread_mutex_lock(&consumer_data->pid_mutex);
+ if (consumer_data->pid == 0) {
+ ERR("Kconsumerd did not start");
+ pthread_mutex_unlock(&consumer_data->pid_mutex);
+ goto error;
+ }
+ pthread_mutex_unlock(&consumer_data->pid_mutex);
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * Join consumer thread
+ */
+static int join_consumer_thread(struct consumer_data *consumer_data)
+{
+ void *status;
+ int ret;
+
+ if (consumer_data->pid != 0) {
+ ret = kill(consumer_data->pid, SIGTERM);
+ if (ret) {
+ ERR("Error killing consumer daemon");
+ return ret;
+ }
+ return pthread_join(consumer_data->thread, &status);
+ } else {
+ return 0;
+ }
+}
+
+/*
+ * Fork and exec a consumer daemon (consumerd).
+ *
+ * Return pid if successful else -1.
+ */
+static pid_t spawn_consumerd(struct consumer_data *consumer_data)
+{
+ int ret;
+ pid_t pid;
+ const char *verbosity;
+
+ DBG("Spawning consumerd");
+
+ pid = fork();
+ if (pid == 0) {
+ /*
+ * Exec consumerd.
+ */
+ if (opt_verbose > 1 || opt_verbose_consumer) {
+ verbosity = "--verbose";
+ } else {
+ verbosity = "--quiet";
+ }
+ switch (consumer_data->type) {
+ case LTTNG_CONSUMER_KERNEL:
+ execl(INSTALL_BIN_PATH "/lttng-consumerd",
+ "lttng-consumerd", verbosity, "-k", NULL);
+ break;
+ case LTTNG_CONSUMER_UST:
+ execl(INSTALL_BIN_PATH "/lttng-consumerd",
+ "lttng-consumerd", verbosity, "-u", NULL);
+ break;
+ default:
+ perror("unknown consumer type");
+ exit(EXIT_FAILURE);
+ }
+ if (errno != 0) {
+ perror("kernel start consumer exec");
+ }
+ exit(EXIT_FAILURE);
+ } else if (pid > 0) {
+ ret = pid;
+ } else {
+ perror("start consumer fork");
+ ret = -errno;
+ }
+ return ret;
+}
+
+/*
+ * Spawn the consumerd daemon and session daemon thread.
+ */
+static int start_consumerd(struct consumer_data *consumer_data)
+{
+ int ret;
+
+ pthread_mutex_lock(&consumer_data->pid_mutex);
+ if (consumer_data->pid != 0) {
+ pthread_mutex_unlock(&consumer_data->pid_mutex);
+ goto end;
+ }
+
+ ret = spawn_consumerd(consumer_data);
+ if (ret < 0) {
+ ERR("Spawning consumerd failed");
+ pthread_mutex_unlock(&consumer_data->pid_mutex);
+ goto error;
+ }
+
+ /* Setting up the consumer_data pid */
+ consumer_data->pid = ret;
+ DBG2("consumer pid %d", consumer_data->pid);
+ pthread_mutex_unlock(&consumer_data->pid_mutex);
+
+ DBG2("Spawning consumer control thread");
+ ret = spawn_consumer_thread(consumer_data);
+ if (ret < 0) {
+ ERR("Fatal error spawning consumer control thread");
+ goto error;
+ }
+
+end:
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * modprobe_kernel_modules
+ */
+static int modprobe_kernel_modules(void)
+{
+ int ret = 0, i;
+ char modprobe[256];
+
+ for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) {
+ ret = snprintf(modprobe, sizeof(modprobe),
+ "/sbin/modprobe %s%s",
+ kernel_modules_list[i].required ? "" : "--quiet ",
+ kernel_modules_list[i].name);
+ if (ret < 0) {
+ perror("snprintf modprobe");
+ goto error;
+ }
+ modprobe[sizeof(modprobe) - 1] = '\0';
+ ret = system(modprobe);
+ if (ret == -1) {
+ ERR("Unable to launch modprobe for module %s",
+ kernel_modules_list[i].name);
+ } else if (kernel_modules_list[i].required
+ && WEXITSTATUS(ret) != 0) {
+ ERR("Unable to load module %s",
+ kernel_modules_list[i].name);
+ } else {
+ DBG("Modprobe successfully %s",
+ kernel_modules_list[i].name);
+ }
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * mount_debugfs
+ */
+static int mount_debugfs(char *path)
+{
+ int ret;
+ char *type = "debugfs";
+
+ ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
+ if (ret < 0) {
+ PERROR("Cannot create debugfs path");
+ goto error;
+ }
+
+ ret = mount(type, path, type, 0, NULL);
+ if (ret < 0) {
+ PERROR("Cannot mount debugfs");
+ goto error;
+ }
+
+ DBG("Mounted debugfs successfully at %s", path);
+
+error:
+ return ret;
+}
+
+/*
+ * 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 = NULL;
+ 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) {
+ perror("Cannot mount debugfs");
+ 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;
+}
+
+/*
+ * Init tracing by creating trace directory and sending fds kernel consumer.
+ */
+static int init_kernel_tracing(struct ltt_kernel_session *session)
+{
+ int ret = 0;
+
+ if (session->consumer_fds_sent == 0) {
+ /*
+ * Assign default kernel consumer socket if no consumer assigned to the
+ * kernel session. At this point, it's NOT suppose to be 0 but this is
+ * an extra security check.
+ */
+ if (session->consumer_fd == 0) {
+ session->consumer_fd = kconsumer_data.cmd_sock;
+ }
+
+ ret = send_kconsumer_session_streams(&kconsumer_data, session);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_CONSUMER_FAIL;
+ goto error;
+ }
+
+ session->consumer_fds_sent = 1;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Init tracing by creating trace directory and sending fds ust consumer.
+ */
+static int init_ust_tracing(struct ltt_ust_session *session)
+{
+ int ret = 0;
+
+ if (session->consumer_fds_sent == 0) {
+ /*
+ * Assign default ust consumer socket if no consumer assigned to the
+ * ust session. At this point, it's NOT suppose to be 0 but this is
+ * an extra security check.
+ */
+ if (session->consumer_fd == 0) {
+ session->consumer_fd = ustconsumer_data.cmd_sock;
+ }
+
+ ret = send_ustconsumer_session_streams(&ustconsumer_data, session);
+ if (ret < 0) {
+ ret = LTTCOMM_UST_CONSUMER_FAIL;
+ goto error;
+ }
+
+ session->consumer_fds_sent = 1;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Create an UST session and add it to the session ust list.
+ */
+static int create_ust_session(struct ltt_session *session,
+ struct lttng_domain *domain)
+{
+ int ret;
+ struct ltt_ust_session *lus = NULL;
+ struct ust_app *app;
+
+ switch (domain->type) {
+ case LTTNG_DOMAIN_UST_PID:
+ app = ust_app_get_by_pid(domain->attr.pid);
+ if (app == NULL) {
+ ret = LTTCOMM_APP_NOT_FOUND;
+ goto error;
+ }
+ break;
+ default:
+ ret = LTTCOMM_UNKNOWN_DOMAIN;
+ goto error;
+ }
+
+ DBG("Creating UST session");
+
+ lus = trace_ust_create_session(session->path, domain->attr.pid, domain);
+ if (lus == NULL) {
+ ret = LTTCOMM_UST_SESS_FAIL;
+ goto error;
+ }
+
+ ret = mkdir_recursive(lus->path, S_IRWXU | S_IRWXG,
+ geteuid(), allowed_group());
+ if (ret < 0) {
+ if (ret != -EEXIST) {
+ ERR("Trace directory creation error");
+ ret = LTTCOMM_UST_SESS_FAIL;
+ goto error;
+ }
+ }
+
+ /* Create session on the UST tracer */
+ ret = ustctl_create_session(app->sock);
+ if (ret < 0) {
+ ret = LTTCOMM_UST_SESS_FAIL;
+ goto error;
+ }
+ lus->handle = ret;
+ lus->sock = app->sock;
+
+ cds_list_add(&lus->list, &session->ust_session_list.head);
+ session->ust_session_list.count++;
+
+ return LTTCOMM_OK;
+
+error:
+ free(lus);
+ return ret;
+}
+
+/*
+ * 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;
+ }
+
+ /* Set kernel consumer socket fd */
+ if (kconsumer_data.cmd_sock) {
+ session->kernel_session->consumer_fd = kconsumer_data.cmd_sock;
+ }
+
+ ret = mkdir_recursive(session->kernel_session->trace_path,
+ S_IRWXU | S_IRWXG, geteuid(), allowed_group());
+ 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
+ * session_lock_list() and session_unlock_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);
+ sessions[i].path[PATH_MAX - 1] = '\0';
+ strncpy(sessions[i].name, session->name, NAME_MAX);
+ sessions[i].name[NAME_MAX - 1] = '\0';
+ i++;
+ }
+}
+
+/*
+ * Fill lttng_channel array of all channels.
+ */
+static void list_lttng_channels(struct ltt_session *session,
+ struct lttng_channel *channels)
+{
+ int i = 0;
+ struct ltt_kernel_channel *kchan;
+
+ DBG("Listing channels for session %s", session->name);
+
+ /* Kernel channels */
+ if (session->kernel_session != NULL) {
+ cds_list_for_each_entry(kchan,
+ &session->kernel_session->channel_list.head, list) {
+ /* Copy lttng_channel struct to array */
+ memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
+ channels[i].enabled = kchan->enabled;
+ i++;
+ }
+ }
+
+ /* TODO: Missing UST listing */
+}
+
+/*
+ * Fill lttng_event array of all events in the channel.
+ */
+static void list_lttng_events(struct ltt_kernel_channel *kchan,
+ struct lttng_event *events)
+{
+ /*
+ * TODO: This is ONLY kernel. Need UST support.
+ */
+ int i = 0;
+ struct ltt_kernel_event *event;
+
+ DBG("Listing events for channel %s", kchan->channel->name);
+
+ /* Kernel channels */
+ cds_list_for_each_entry(event, &kchan->events_list.head , list) {
+ strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
+ events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
+ events[i].enabled = event->enabled;
+ switch (event->event->instrumentation) {
+ case LTTNG_KERNEL_TRACEPOINT:
+ events[i].type = LTTNG_EVENT_TRACEPOINT;
+ break;
+ case LTTNG_KERNEL_KPROBE:
+ case LTTNG_KERNEL_KRETPROBE:
+ events[i].type = LTTNG_EVENT_PROBE;
+ memcpy(&events[i].attr.probe, &event->event->u.kprobe,
+ sizeof(struct lttng_kernel_kprobe));
+ break;
+ case LTTNG_KERNEL_FUNCTION:
+ events[i].type = LTTNG_EVENT_FUNCTION;
+ memcpy(&events[i].attr.ftrace, &event->event->u.ftrace,
+ sizeof(struct lttng_kernel_function));
+ break;
+ case LTTNG_KERNEL_NOOP:
+ events[i].type = LTTNG_EVENT_NOOP;
+ break;
+ case LTTNG_KERNEL_SYSCALL:
+ events[i].type = LTTNG_EVENT_SYSCALL;
+ break;
+ case LTTNG_KERNEL_ALL:
+ assert(0);
+ break;
+ }
+ i++;
+ }
+}
+
+/*
+ * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
+ */
+static int cmd_disable_channel(struct ltt_session *session,
+ int domain, char *channel_name)
+{
+ int ret;
+
+ switch (domain) {
+ case LTTNG_DOMAIN_KERNEL:
+ ret = channel_kernel_disable(session->kernel_session,
+ channel_name);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ kernel_wait_quiescent(kernel_tracer_fd);
+ break;
+ case LTTNG_DOMAIN_UST_PID:
+ break;
+ default:
+ ret = LTTCOMM_UNKNOWN_DOMAIN;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+
+error:
+ return ret;
+}
+
+/*
+ * Copy channel from attributes and set it in the application channel list.
+ */
+static int copy_ust_channel_to_app(struct ltt_ust_session *usess,
+ struct lttng_channel *attr, struct ust_app *app)
+{
+ int ret;
+ struct ltt_ust_channel *uchan, *new_chan;
+
+ uchan = trace_ust_get_channel_by_name(attr->name, usess);
+ if (uchan == NULL) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ new_chan = trace_ust_create_channel(attr, usess->path);
+ if (new_chan == NULL) {
+ PERROR("malloc ltt_ust_channel");
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ ret = channel_ust_copy(new_chan, uchan);
+ if (ret < 0) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ /* Add channel to the ust app channel list */
+ cds_list_add(&new_chan->list, &app->channels.head);
+ app->channels.count++;
+
+error:
+ return ret;
+}
+
+/*
+ * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
+ */
+static int cmd_enable_channel(struct ltt_session *session,
+ struct lttng_domain *domain, struct lttng_channel *attr)
+{
+ int ret;
+
+ switch (domain->type) {
+ case LTTNG_DOMAIN_KERNEL:
+ {
+ struct ltt_kernel_channel *kchan;
+
+ kchan = trace_kernel_get_channel_by_name(attr->name,
+ session->kernel_session);
+ if (kchan == NULL) {
+ ret = channel_kernel_create(session->kernel_session,
+ attr, kernel_poll_pipe[1]);
+ } else {
+ ret = channel_kernel_enable(session->kernel_session, kchan);
+ }
+
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ kernel_wait_quiescent(kernel_tracer_fd);
+ break;
+ }
+ case LTTNG_DOMAIN_UST_PID:
+ {
+ int sock;
+ struct ltt_ust_channel *uchan;
+ struct ltt_ust_session *usess;
+ struct ust_app *app;
+
+ usess = trace_ust_get_session_by_pid(&session->ust_session_list,
+ domain->attr.pid);
+ if (usess == NULL) {
+ ret = LTTCOMM_UST_CHAN_NOT_FOUND;
+ goto error;
+ }
+
+ app = ust_app_get_by_pid(domain->attr.pid);
+ if (app == NULL) {
+ ret = LTTCOMM_APP_NOT_FOUND;
+ goto error;
+ }
+ sock = app->sock;
+
+ uchan = trace_ust_get_channel_by_name(attr->name, usess);
+ if (uchan == NULL) {
+ ret = channel_ust_create(usess, attr, sock);
+ } else {
+ ret = channel_ust_enable(usess, uchan, sock);
+ }
+
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ ret = copy_ust_channel_to_app(usess, attr, app);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ DBG("UST channel %s created for app sock %d with pid %d",
+ attr->name, app->sock, domain->attr.pid);
+ break;
+ }
+ default:
+ ret = LTTCOMM_UNKNOWN_DOMAIN;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+
+error:
+ return ret;
+}
+
+/*
+ * Command LTTNG_DISABLE_EVENT processed by the client thread.
+ */
+static int cmd_disable_event(struct ltt_session *session, int domain,
+ char *channel_name, char *event_name)
+{
+ int ret;
+
+ switch (domain) {
+ case LTTNG_DOMAIN_KERNEL:
+ {
+ struct ltt_kernel_channel *kchan;
+
+ kchan = trace_kernel_get_channel_by_name(channel_name,
+ session->kernel_session);
+ if (kchan == NULL) {
+ ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
+ goto error;
+ }
+
+ ret = event_kernel_disable_tracepoint(session->kernel_session, kchan, event_name);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ kernel_wait_quiescent(kernel_tracer_fd);
+ break;
+ }
+ case LTTNG_DOMAIN_UST:
+ {
+ struct ltt_ust_session *ustsession;
+
+ cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) {
+ struct ltt_ust_channel *ustchan;
+
+ ustchan = trace_ust_get_channel_by_name(channel_name,
+ ustsession);
+ if (ustchan == NULL) {
+ ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
+ goto error;
+ }
+ ret = event_ust_disable_tracepoint(ustsession, ustchan, event_name);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ ustctl_wait_quiescent(ustsession->sock);
+ }
+ break;
+ }
+ case LTTNG_DOMAIN_UST_EXEC_NAME:
+ case LTTNG_DOMAIN_UST_PID:
+ case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
+ default:
+ /* TODO: Other UST domains */
+ ret = LTTCOMM_NOT_IMPLEMENTED;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+
+error:
+ return ret;
+}
+
+/*
+ * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
+ */
+static int cmd_disable_event_all(struct ltt_session *session, int domain,
+ char *channel_name)
+{
+ int ret;
+ struct ltt_kernel_channel *kchan;
+
+ switch (domain) {
+ case LTTNG_DOMAIN_KERNEL:
+ kchan = trace_kernel_get_channel_by_name(channel_name,
+ session->kernel_session);
+ if (kchan == NULL) {
+ ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
+ goto error;
+ }
+
+ ret = event_kernel_disable_all(session->kernel_session, kchan);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ kernel_wait_quiescent(kernel_tracer_fd);
+ break;
+ default:
+ /* TODO: Userspace tracing */
+ ret = LTTCOMM_NOT_IMPLEMENTED;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+
+error:
+ return ret;
+}
+
+/*
+ * Command LTTNG_ADD_CONTEXT processed by the client thread.
+ */
+static int cmd_add_context(struct ltt_session *session, int domain,
+ char *channel_name, char *event_name, struct lttng_event_context *ctx)
+{
+ int ret;
+
+ switch (domain) {
+ case LTTNG_DOMAIN_KERNEL:
+ /* Add kernel context to kernel tracer */
+ ret = context_kernel_add(session->kernel_session, ctx,
+ event_name, channel_name);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ break;
+ case LTTNG_DOMAIN_UST:
+ {
+ struct ltt_ust_session *ustsession;
+
+ cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) {
+ /* Add UST context to UST tracer */
+ ret = context_ust_add(ustsession, ctx,
+ event_name, channel_name);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ }
+ break;
+ }
+ default:
+ /* TODO: UST other domains */
+ ret = LTTCOMM_NOT_IMPLEMENTED;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+
+error:
+ return ret;
+}
+
+/*
+ * Command LTTNG_ENABLE_EVENT processed by the client thread.
+ */
+static int cmd_enable_event(struct ltt_session *session, int domain,
+ char *channel_name, struct lttng_event *event)
+{
+ int ret;
+ struct lttng_channel *attr;
+
+ switch (domain) {
+ case LTTNG_DOMAIN_KERNEL:
+ {
+ struct ltt_kernel_channel *kchan;
+
+ kchan = trace_kernel_get_channel_by_name(channel_name,
+ session->kernel_session);
+ if (kchan == NULL) {
+ attr = channel_new_default_attr(domain);
+ if (attr == NULL) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+ snprintf(attr->name, NAME_MAX, "%s", channel_name);
+
+ /* This call will notify the kernel thread */
+ ret = channel_kernel_create(session->kernel_session,
+ attr, kernel_poll_pipe[1]);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ }
+
+ /* Get the newly created kernel channel pointer */
+ kchan = trace_kernel_get_channel_by_name(channel_name,
+ session->kernel_session);
+ if (kchan == NULL) {
+ /* This sould not happen... */
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ ret = event_kernel_enable_tracepoint(session->kernel_session, kchan, event);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ kernel_wait_quiescent(kernel_tracer_fd);
+ break;
+ }
+ case LTTNG_DOMAIN_UST:
+ {
+ struct ltt_ust_session *ustsession;
+
+ cds_list_for_each_entry(ustsession, &session->ust_session_list.head, list) {
+ struct ltt_ust_channel *ustchan;
+
+ ustchan = trace_ust_get_channel_by_name(channel_name,
+ ustsession);
+ if (ustchan == NULL) {
+ attr = channel_new_default_attr(domain);
+ if (attr == NULL) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+ snprintf(attr->name, NAME_MAX, "%s", channel_name);
+
+ ret = channel_ust_create(ustsession,
+ attr, ustsession->sock);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ }
+
+ /* Get the newly created ust channel pointer */
+ ustchan = trace_ust_get_channel_by_name(channel_name,
+ ustsession);
+ if (ustchan == NULL) {
+ /* This sould not happen... */
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ ret = event_ust_enable_tracepoint(ustsession, ustchan, event);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ ustctl_wait_quiescent(ustsession->sock);
+ }
+ break;
+ }
+ case LTTNG_DOMAIN_UST_EXEC_NAME:
+ case LTTNG_DOMAIN_UST_PID:
+ case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
+ default:
+ /* TODO: UST other domains */
+ ret = LTTCOMM_NOT_IMPLEMENTED;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+
+error:
+ return ret;
+}
+
+/*
+ * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
+ */
+static int cmd_enable_event_all(struct ltt_session *session, int domain,
+ char *channel_name, int event_type)
+{
+ int ret;
+ struct ltt_kernel_channel *kchan;
+
+ switch (domain) {
+ case LTTNG_DOMAIN_KERNEL:
+ kchan = trace_kernel_get_channel_by_name(channel_name,
+ session->kernel_session);
+ if (kchan == NULL) {
+ /* This call will notify the kernel thread */
+ ret = channel_kernel_create(session->kernel_session, NULL,
+ kernel_poll_pipe[1]);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ }
+
+ /* Get the newly created kernel channel pointer */
+ kchan = trace_kernel_get_channel_by_name(channel_name,
+ session->kernel_session);
+ if (kchan == NULL) {
+ /* This sould not happen... */
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ switch (event_type) {
+ case LTTNG_KERNEL_SYSCALL:
+ ret = event_kernel_enable_all_syscalls(session->kernel_session,
+ kchan, kernel_tracer_fd);
+ break;
+ case LTTNG_KERNEL_TRACEPOINT:
+ /*
+ * This call enables all LTTNG_KERNEL_TRACEPOINTS and
+ * events already registered to the channel.
+ */
+ ret = event_kernel_enable_all_tracepoints(session->kernel_session,
+ kchan, kernel_tracer_fd);
+ break;
+ case LTTNG_KERNEL_ALL:
+ /* Enable syscalls and tracepoints */
+ ret = event_kernel_enable_all(session->kernel_session,
+ kchan, kernel_tracer_fd);
+ break;
+ default:
+ ret = LTTCOMM_KERN_ENABLE_FAIL;
+ goto error;
+ }
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ kernel_wait_quiescent(kernel_tracer_fd);
+ break;
+ default:
+ /* TODO: Userspace tracing */
+ ret = LTTCOMM_NOT_IMPLEMENTED;
+ goto error;
+ }
+
+ ret = LTTCOMM_OK;
+
+error:
+ return ret;
+}
+
+/*
+ * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
+ */
+static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
+{
+ int ret;
+ ssize_t nb_events = 0;
+
+ switch (domain) {
+ case LTTNG_DOMAIN_KERNEL:
+ nb_events = kernel_list_events(kernel_tracer_fd, events);
+ if (nb_events < 0) {
+ ret = LTTCOMM_KERN_LIST_FAIL;
+ goto error;
+ }
+ break;
+ default:
+ /* TODO: Userspace listing */
+ ret = LTTCOMM_NOT_IMPLEMENTED;
+ goto error;
+ }
+
+ return nb_events;
+
+error:
+ /* Return negative value to differentiate return code */
+ return -ret;
+}
+
+/*
+ * Command LTTNG_START_TRACE processed by the client thread.
+ */
+static int cmd_start_trace(struct ltt_session *session)
+{
+ int ret;
+ struct ltt_kernel_session *ksession;
+ struct ltt_ust_session *ustsession;
+
+ /* Short cut */
+ ksession = session->kernel_session;
+
+ /* Kernel tracing */
+ if (ksession != NULL) {
+ struct ltt_kernel_channel *kchan;
+
+ /* Open kernel metadata */
+ if (ksession->metadata == NULL) {
+ ret = kernel_open_metadata(ksession, ksession->trace_path);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_META_FAIL;
+ goto error;
+ }
+ }
+
+ /* Open kernel metadata stream */
+ if (ksession->metadata_stream_fd == 0) {
+ ret = kernel_open_metadata_stream(ksession);
+ if (ret < 0) {
+ ERR("Kernel create metadata stream failed");
+ ret = LTTCOMM_KERN_STREAM_FAIL;
+ goto error;
+ }
+ }
+
+ /* For each channel */
+ cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
+ if (kchan->stream_count == 0) {
+ ret = kernel_open_channel_stream(kchan);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_STREAM_FAIL;
+ goto error;
+ }
+ /* Update the stream global counter */
+ ksession->stream_count_global += ret;
+ }
+ }
+
+ /* Setup kernel consumer socket and send fds to it */
+ ret = init_kernel_tracing(ksession);