X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fregister.c;h=ecb0dfef07457e1ca68aa76d749ed9981cbf4c10;hp=b75db4ed9533f9c0e73025602b3729514d89d111;hb=d9c6b5f258e9b72fb1da7492f4001dd0976e9886;hpb=1785d7f29bf3f162f37152ee9ea8907b9bc6d9d2 diff --git a/src/bin/lttng-sessiond/register.c b/src/bin/lttng-sessiond/register.c index b75db4ed9..ecb0dfef0 100644 --- a/src/bin/lttng-sessiond/register.c +++ b/src/bin/lttng-sessiond/register.c @@ -37,6 +37,8 @@ struct thread_notifiers { struct lttng_pipe *quit_pipe; struct ust_cmd_queue *ust_cmd_queue; + sem_t ready; + bool running; }; /* @@ -70,7 +72,7 @@ static int create_application_socket(void) if (ret < 0) { PERROR("Set file permissions failed on %s", config.apps_unix_sock_path.value); - goto end; + goto error_close_socket; } DBG3("Session daemon application socket created (fd = %d) ", apps_sock); @@ -78,6 +80,13 @@ static int create_application_socket(void) end: umask(old_umask); return ret; +error_close_socket: + if (close(apps_sock)) { + PERROR("Failed to close application socket in error path"); + } + apps_sock = -1; + ret = -1; + goto end; } /* @@ -113,6 +122,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(¬ifiers->ready); +} + +static bool wait_thread_status(struct thread_notifiers *notifiers) +{ + DBG("Waiting for application registration thread to be ready"); + sem_wait(¬ifiers->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 +170,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 +183,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. @@ -206,11 +246,6 @@ static void *thread_application_registration(void *data) revents = LTTNG_POLL_GETEV(&events, i); pollfd = LTTNG_POLL_GETFD(&events, i); - if (!revents) { - /* No activity for this FD (poll implementation). */ - continue; - } - /* Thread quit pipe has been closed. Killing thread. */ if (pollfd == quit_pipe_read_fd) { err = 0; @@ -343,7 +378,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(); @@ -361,24 +395,24 @@ static bool shutdown_application_registration_thread(void *data) return notify_thread_pipe(write_fd) == 1; } -bool launch_application_registration_thread( +struct lttng_thread *launch_application_registration_thread( struct ust_cmd_queue *cmd_queue) { struct lttng_pipe *quit_pipe; struct thread_notifiers *notifiers = NULL; struct lttng_thread *thread; - quit_pipe = lttng_pipe_open(FD_CLOEXEC); - if (!quit_pipe) { - goto error; - } - notifiers = zmalloc(sizeof(*notifiers)); if (!notifiers) { + goto error_alloc; + } + quit_pipe = lttng_pipe_open(FD_CLOEXEC); + if (!quit_pipe) { goto error; } notifiers->quit_pipe = quit_pipe; notifiers->ust_cmd_queue = cmd_queue; + sem_init(¬ifiers->ready, 0, 0); thread = lttng_thread_create("UST application registration", thread_application_registration, @@ -388,9 +422,13 @@ bool launch_application_registration_thread( if (!thread) { goto error; } - lttng_thread_put(thread); - return true; + if (!wait_thread_status(notifiers)) { + lttng_thread_put(thread); + thread = NULL; + } + return thread; error: cleanup_application_registration_thread(notifiers); - return false; +error_alloc: + return NULL; }