+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) {