/*
- * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
- * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
+ * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
*
- * 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"
#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;
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");
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");
goto error;
}
- sessiond_notify_ready();
-
+ mark_thread_as_ready(notifiers);
while (1) {
DBG("Health check ready");
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) {
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;
}