Fix: create_kernel_session asserts on failure
[lttng-tools.git] / src / bin / lttng-sessiond / client.c
index 119062c15c648f2dfcea8e395751e69d396080e3..a889529a76967f881c9c05159ccdb55300336f51 100644 (file)
 #include "health-sessiond.h"
 #include "testpoint.h"
 #include "utils.h"
+#include "manage-consumer.h"
 
 static bool is_root;
 
 static struct thread_state {
-       pthread_cond_t cond;
-       pthread_mutex_t lock;
-       bool is_running;
-} thread_state = {
-       .cond = PTHREAD_COND_INITIALIZER,
-       .lock = PTHREAD_MUTEX_INITIALIZER,
-       .is_running = false
-};
-
-void set_thread_state_running(void)
+       sem_t ready;
+       bool running;
+} thread_state;
+
+static void set_thread_status(bool running)
 {
-       pthread_mutex_lock(&thread_state.lock);
-        thread_state.is_running = true;
-       pthread_cond_broadcast(&thread_state.cond);
-       pthread_mutex_unlock(&thread_state.lock);
+       DBG("Marking client thread's state as %s", running ? "running" : "error");
+       thread_state.running = running;
+       sem_post(&thread_state.ready);
 }
 
-static void wait_thread_state_running(void)
+static bool wait_thread_status(void)
 {
-       pthread_mutex_lock(&thread_state.lock);
-       while (!thread_state.is_running) {
-               pthread_cond_wait(&thread_state.cond,
-                               &thread_state.lock);
+       DBG("Waiting for client thread to be ready");
+       sem_wait(&thread_state.ready);
+       if (thread_state.running) {
+               DBG("Client thread is ready");
+       } else {
+               ERR("Initialization of client thread failed");
        }
-       pthread_mutex_unlock(&thread_state.lock);
+
+       return thread_state.running;
 }
 
 /*
@@ -116,133 +114,11 @@ end:
 
 /*
  * Start the thread_manage_consumer. This must be done after a lttng-consumerd
- * exec or it will fails.
+ * exec or it will fail.
  */
 static int spawn_consumer_thread(struct consumer_data *consumer_data)
 {
-       int ret, clock_ret;
-       struct timespec timeout;
-
-       /*
-        * Make sure we set the readiness flag to 0 because we are NOT ready.
-        * This access to consumer_thread_is_ready does not need to be
-        * protected by consumer_data.cond_mutex (yet) since the consumer
-        * management thread has not been started at this point.
-        */
-       consumer_data->consumer_thread_is_ready = 0;
-
-       /* Setup pthread condition */
-       ret = pthread_condattr_init(&consumer_data->condattr);
-       if (ret) {
-               errno = ret;
-               PERROR("pthread_condattr_init consumer data");
-               goto error;
-       }
-
-       /*
-        * Set the monotonic clock in order to make sure we DO NOT jump in time
-        * between the clock_gettime() call and the timedwait call. See bug #324
-        * for a more details and how we noticed it.
-        */
-       ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
-       if (ret) {
-               errno = ret;
-               PERROR("pthread_condattr_setclock consumer data");
-               goto error;
-       }
-
-       ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
-       if (ret) {
-               errno = ret;
-               PERROR("pthread_cond_init consumer data");
-               goto error;
-       }
-
-       ret = pthread_create(&consumer_data->thread, default_pthread_attr(),
-                       thread_manage_consumer, consumer_data);
-       if (ret) {
-               errno = ret;
-               PERROR("pthread_create consumer");
-               ret = -1;
-               goto error;
-       }
-
-       /* We are about to wait on a pthread condition */
-       pthread_mutex_lock(&consumer_data->cond_mutex);
-
-       /* Get time for sem_timedwait absolute timeout */
-       clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout);
-       /*
-        * Set the timeout for the condition timed wait even if the clock gettime
-        * call fails since we might loop on that call and we want to avoid to
-        * increment the timeout too many times.
-        */
-       timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
-
-       /*
-        * The following loop COULD be skipped in some conditions so this is why we
-        * set ret to 0 in order to make sure at least one round of the loop is
-        * done.
-        */
-       ret = 0;
-
-       /*
-        * Loop until the condition is reached or when a timeout is reached. Note
-        * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
-        * be returned but the pthread_cond(3), from the glibc-doc, says that it is
-        * possible. This loop does not take any chances and works with both of
-        * them.
-        */
-       while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
-               if (clock_ret < 0) {
-                       PERROR("clock_gettime spawn consumer");
-                       /* Infinite wait for the consumerd thread to be ready */
-                       ret = pthread_cond_wait(&consumer_data->cond,
-                                       &consumer_data->cond_mutex);
-               } else {
-                       ret = pthread_cond_timedwait(&consumer_data->cond,
-                                       &consumer_data->cond_mutex, &timeout);
-               }
-       }
-
-       /* Release the pthread condition */
-       pthread_mutex_unlock(&consumer_data->cond_mutex);
-
-       if (ret != 0) {
-               errno = ret;
-               if (ret == ETIMEDOUT) {
-                       int pth_ret;
-
-                       /*
-                        * Call has timed out so we kill the kconsumerd_thread and return
-                        * an error.
-                        */
-                       ERR("Condition timed out. The consumer thread was never ready."
-                                       " Killing it");
-                       pth_ret = pthread_cancel(consumer_data->thread);
-                       if (pth_ret < 0) {
-                               PERROR("pthread_cancel consumer thread");
-                       }
-               } else {
-                       PERROR("pthread_cond_wait failed consumer thread");
-               }
-               /* Caller is expecting a negative value on failure. */
-               ret = -1;
-               goto error;
-       }
-
-       pthread_mutex_lock(&consumer_data->pid_mutex);
-       if (consumer_data->pid == 0) {
-               ERR("Consumerd did not start");
-               pthread_mutex_unlock(&consumer_data->pid_mutex);
-               goto error;
-       }
-       pthread_mutex_unlock(&consumer_data->pid_mutex);
-
-       return 0;
-
-error:
-       return ret;
+       return launch_consumer_management_thread(consumer_data) ? 0 : -1;
 }
 
 /*
@@ -599,7 +475,7 @@ static int create_kernel_session(struct ltt_session *session)
        ret = kernel_create_session(session, kernel_tracer_fd);
        if (ret < 0) {
                ret = LTTNG_ERR_KERN_SESS_FAIL;
-               goto error;
+               goto error_create;
        }
 
        /* Code flow safety */
@@ -621,6 +497,7 @@ static int create_kernel_session(struct ltt_session *session)
 error:
        trace_kernel_destroy_session(session->kernel_session);
        session->kernel_session = NULL;
+error_create:
        return ret;
 }
 
@@ -757,27 +634,6 @@ error:
        return ret;
 }
 
-/*
- * Join consumer thread
- */
-static int join_consumer_thread(struct consumer_data *consumer_data)
-{
-       void *status;
-
-       /* Consumer pid must be a real one. */
-       if (consumer_data->pid > 0) {
-               int ret;
-               ret = kill(consumer_data->pid, SIGTERM);
-               if (ret) {
-                       PERROR("Error killing consumer daemon");
-                       return ret;
-               }
-               return pthread_join(consumer_data->thread, &status);
-       } else {
-               return 0;
-       }
-}
-
 /*
  * Version of setup_lttng_msg() without command header.
  */
@@ -1029,7 +885,7 @@ static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
                if (need_tracing_session) {
                        if (cmd_ctx->session->kernel_session == NULL) {
                                ret = create_kernel_session(cmd_ctx->session);
-                               if (ret < 0) {
+                               if (ret != LTTNG_OK) {
                                        ret = LTTNG_ERR_KERN_SESS_FAIL;
                                        goto error;
                                }
@@ -2213,6 +2069,11 @@ static void cleanup_client_thread(void *data)
        lttng_pipe_destroy(quit_pipe);
 }
 
+static void thread_init_cleanup(void *data)
+{
+       set_thread_status(false);
+}
+
 /*
  * This thread manage all clients request using the unix client socket for
  * communication.
@@ -2232,6 +2093,7 @@ static void *thread_manage_clients(void *data)
 
        is_root = (getuid() == 0);
 
+       pthread_cleanup_push(thread_init_cleanup, NULL);
        client_sock = create_client_sock();
        if (client_sock < 0) {
                goto error_listen;
@@ -2269,6 +2131,10 @@ static void *thread_manage_clients(void *data)
                goto error;
        }
 
+       /* Set state as running. */
+        set_thread_status(true);
+       pthread_cleanup_pop(0);
+
        /* This testpoint is after we signal readiness to the parent. */
        if (testpoint(sessiond_thread_manage_clients)) {
                goto error;
@@ -2280,9 +2146,6 @@ static void *thread_manage_clients(void *data)
 
        health_code_update();
 
-       /* Set state as running. */
-        set_thread_state_running();
-
        while (1) {
                const struct cmd_completion_handler *cmd_completion_handler;
 
@@ -2490,28 +2353,6 @@ error_create_poll:
        DBG("Client thread dying");
 
        rcu_unregister_thread();
-
-       /*
-        * Since we are creating the consumer threads, we own them, so we need
-        * to join them before our thread exits.
-        */
-       ret = join_consumer_thread(&kconsumer_data);
-       if (ret) {
-               errno = ret;
-               PERROR("join_consumer");
-       }
-
-       ret = join_consumer_thread(&ustconsumer32_data);
-       if (ret) {
-               errno = ret;
-               PERROR("join_consumer ust32");
-       }
-
-       ret = join_consumer_thread(&ustconsumer64_data);
-       if (ret) {
-               errno = ret;
-               PERROR("join_consumer ust64");
-       }
        return NULL;
 }
 
@@ -2526,9 +2367,11 @@ bool shutdown_client_thread(void *thread_data)
 
 struct lttng_thread *launch_client_thread(void)
 {
+       bool thread_running;
        struct lttng_pipe *client_quit_pipe;
        struct lttng_thread *thread;
 
+       sem_init(&thread_state.ready, 0, 0);
        client_quit_pipe = lttng_pipe_open(FD_CLOEXEC);
        if (!client_quit_pipe) {
                goto error;
@@ -2547,8 +2390,11 @@ struct lttng_thread *launch_client_thread(void)
         * This thread is part of the threads that need to be fully
         * initialized before the session daemon is marked as "ready".
         */
-       wait_thread_state_running();
-
+       thread_running = wait_thread_status();
+       if (!thread_running) {
+               lttng_thread_put(thread);
+               thread = NULL;
+       }
        return thread;
 error:
        cleanup_client_thread(client_quit_pipe);
This page took 0.028684 seconds and 5 git commands to generate.