X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fhealth.c;h=483616ac26a3d7b160b30d691a99a0d8f8bf9164;hp=e2b8cc5a3e1b3794b75299737f064e9ac3fff220;hb=b8a1b0bd3964381c9ec6a8df26bd37cd7405a83f;hpb=173fca4fb73279d919e1738b61e007231f43d519 diff --git a/src/bin/lttng-sessiond/health.c b/src/bin/lttng-sessiond/health.c index e2b8cc5a3..483616ac2 100644 --- a/src/bin/lttng-sessiond/health.c +++ b/src/bin/lttng-sessiond/health.c @@ -1,19 +1,9 @@ /* - * Copyright (C) 2012 - David Goulet - * Copyright (C) 2018 - Jérémie Galarneau + * Copyright (C) 2012 David Goulet + * Copyright (C) 2018 Jérémie Galarneau * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License, version 2 only, as - * published by the Free Software Foundation. + * SPDX-License-Identifier: GPL-2.0-only * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 51 - * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "lttng-sessiond.h" @@ -27,17 +17,39 @@ #include "utils.h" #include "thread.h" -static void cleanup_health_management_thread(void *thread_data) +struct thread_notifiers { + struct lttng_pipe *quit_pipe; + sem_t ready; +}; + +static +void mark_thread_as_ready(struct thread_notifiers *notifiers) { - struct lttng_pipe *quit_pipe = thread_data; + DBG("Marking health management thread as ready"); + sem_post(¬ifiers->ready); +} - lttng_pipe_destroy(quit_pipe); +static +void wait_until_thread_is_ready(struct thread_notifiers *notifiers) +{ + DBG("Waiting for health management thread to be ready"); + sem_wait(¬ifiers->ready); + DBG("Health management thread is ready"); +} + +static void cleanup_health_management_thread(void *data) +{ + struct thread_notifiers *notifiers = data; + + lttng_pipe_destroy(notifiers->quit_pipe); + sem_destroy(¬ifiers->ready); + free(notifiers); } /* * Thread managing health check socket. */ -static void *thread_manage_health(void *thread_data) +static void *thread_manage_health(void *data) { const bool is_root = (getuid() == 0); int sock = -1, new_sock = -1, ret, i, pollfd, err = -1; @@ -46,8 +58,9 @@ static void *thread_manage_health(void *thread_data) struct health_comm_msg msg; struct health_comm_reply reply; /* Thread-specific quit pipe. */ - struct lttng_pipe *quit_pipe = thread_data; - const int quit_pipe_read_fd = lttng_pipe_get_readfd(quit_pipe); + struct thread_notifiers *notifiers = data; + const int quit_pipe_read_fd = lttng_pipe_get_readfd( + notifiers->quit_pipe); DBG("[thread] Manage health check started"); @@ -72,8 +85,15 @@ static void *thread_manage_health(void *thread_data) if (is_root) { /* lttng health client socket path permissions */ - ret = chown(config.health_unix_sock_path.value, 0, - utils_get_group_id(config.tracing_group_name.value)); + gid_t gid; + + ret = utils_get_group_id(config.tracing_group_name.value, true, &gid); + if (ret) { + /* Default to root group. */ + gid = 0; + } + + ret = chown(config.health_unix_sock_path.value, 0, gid); if (ret < 0) { ERR("Unable to set group on %s", config.health_unix_sock_path.value); PERROR("chown"); @@ -111,8 +131,7 @@ static void *thread_manage_health(void *thread_data) goto error; } - sessiond_notify_ready(); - + mark_thread_as_ready(notifiers); while (1) { DBG("Health check ready"); @@ -136,11 +155,6 @@ restart: revents = LTTNG_POLL_GETEV(&events, i); pollfd = LTTNG_POLL_GETFD(&events, i); - if (!revents) { - /* No activity for this FD (poll implementation). */ - continue; - } - /* Event on the registration socket */ if (pollfd == sock) { if (revents & LPOLLIN) { @@ -228,44 +242,43 @@ error: return NULL; } -static bool shutdown_health_management_thread(void *thread_data) +static bool shutdown_health_management_thread(void *data) { - int ret; - int pipe_write_fd; - struct lttng_pipe *health_quit_pipe = thread_data; + struct thread_notifiers *notifiers = data; + const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe); - pipe_write_fd = lttng_pipe_get_writefd(health_quit_pipe); - ret = notify_thread_pipe(pipe_write_fd); - if (ret < 0) { - ERR("Failed to notify Health management thread's quit pipe"); - goto error; - } - return true; -error: - return false; + return notify_thread_pipe(write_fd) == 1; } bool launch_health_management_thread(void) { + struct thread_notifiers *notifiers; struct lttng_thread *thread; - struct lttng_pipe *health_quit_pipe = NULL; - health_quit_pipe = lttng_pipe_open(FD_CLOEXEC); - if (!health_quit_pipe) { - goto error; + notifiers = zmalloc(sizeof(*notifiers)); + if (!notifiers) { + goto error_alloc; } + sem_init(¬ifiers->ready, 0, 0); + notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC); + if (!notifiers->quit_pipe) { + goto error; + } thread = lttng_thread_create("Health management", thread_manage_health, shutdown_health_management_thread, cleanup_health_management_thread, - health_quit_pipe); + notifiers); if (!thread) { goto error; } + + wait_until_thread_is_ready(notifiers); lttng_thread_put(thread); return true; error: - cleanup_health_management_thread(health_quit_pipe); + cleanup_health_management_thread(notifiers); +error_alloc: return false; }