Fix: report initialization error of app registration thread
[lttng-tools.git] / src / bin / lttng-sessiond / register.c
index 6776019c68d3cdef3cfd4439906363a56b345c39..d0a1ff14a69dc3173d89fbcf866de09fdb339816 100644 (file)
@@ -37,6 +37,8 @@
 struct thread_notifiers {
        struct lttng_pipe *quit_pipe;
        struct ust_cmd_queue *ust_cmd_queue;
+       sem_t ready;
+       bool running;
 };
 
 /*
@@ -113,6 +115,33 @@ static void cleanup_application_registration_thread(void *data)
        free(notifiers);
 }
 
+static void set_thread_status(struct thread_notifiers *notifiers, bool running)
+{
+       DBG("Marking application registration thread's state as %s", running ? "running" : "error");
+       notifiers->running = running;
+       sem_post(&notifiers->ready);
+}
+
+static bool wait_thread_status(struct thread_notifiers *notifiers)
+{
+       DBG("Waiting for application registration thread to be ready");
+       sem_wait(&notifiers->ready);
+       if (notifiers->running) {
+               DBG("Application registration thread is ready");
+       } else {
+               ERR("Initialization of application registration thread failed");
+       }
+
+       return notifiers->running;
+}
+
+static void thread_init_cleanup(void *data)
+{
+       struct thread_notifiers *notifiers = data;
+
+       set_thread_status(notifiers, false);
+}
+
 /*
  * This thread manage application registration.
  */
@@ -134,12 +163,9 @@ static void *thread_application_registration(void *data)
 
        DBG("[thread] Manage application registration started");
 
+       pthread_cleanup_push(thread_init_cleanup, NULL);
        health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
 
-       if (testpoint(sessiond_thread_registration_apps)) {
-               goto error_testpoint;
-       }
-
        apps_sock = create_application_socket();
        if (apps_sock < 0) {
                goto error_listen;
@@ -150,6 +176,13 @@ static void *thread_application_registration(void *data)
                goto error_listen;
        }
 
+       set_thread_status(notifiers, true);
+       pthread_cleanup_pop(0);
+
+       if (testpoint(sessiond_thread_registration_apps)) {
+               goto error_create_poll;
+       }
+
        /*
         * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing
         * more will be added to this poll set.
@@ -343,7 +376,6 @@ error_poll_add:
        lttng_poll_clean(&events);
 error_listen:
 error_create_poll:
-error_testpoint:
        DBG("UST Registration thread cleanup complete");
        if (err) {
                health_error();
@@ -379,6 +411,7 @@ struct lttng_thread *launch_application_registration_thread(
        }
        notifiers->quit_pipe = quit_pipe;
        notifiers->ust_cmd_queue = cmd_queue;
+       sem_init(&notifiers->ready, 0, 0);
 
        thread = lttng_thread_create("UST application registration",
                        thread_application_registration,
@@ -388,6 +421,10 @@ struct lttng_thread *launch_application_registration_thread(
        if (!thread) {
                goto error;
        }
+       if (!wait_thread_status(notifiers)) {
+               lttng_thread_put(thread);
+               thread = NULL;
+       }
        return thread;
 error:
        cleanup_application_registration_thread(notifiers);
This page took 0.024932 seconds and 5 git commands to generate.