X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-relayd%2Fhealth-relayd.cpp;h=8e22dfe66335f1332ff8740e6e64ee0869480411;hp=b832ffe0e5e81c8c09e794f53f015dd8342228b3;hb=HEAD;hpb=1970828049d051d5cb485dfab995081b4550fd2a diff --git a/src/bin/lttng-relayd/health-relayd.cpp b/src/bin/lttng-relayd/health-relayd.cpp index b832ffe0e..d1b619d39 100644 --- a/src/bin/lttng-relayd/health-relayd.cpp +++ b/src/bin/lttng-relayd/health-relayd.cpp @@ -6,62 +6,46 @@ */ #define _LGPL_SOURCE +#include "health-relayd.hpp" +#include "lttng-relayd.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + #include #include #include +#include #include +#include #include #include #include #include #include #include +#include #include #include #include #include #include -#include -#include #include -#include #include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "lttng-relayd.h" -#include "health-relayd.h" +#include /* Global health check unix path */ -static -char health_unix_sock_path[PATH_MAX]; +static char health_unix_sock_path[PATH_MAX]; int health_quit_pipe[2] = { -1, -1 }; -/* - * Check if the thread quit pipe was triggered. - * - * Return 1 if it was triggered else 0; - */ -static -int check_health_quit_pipe(int fd, uint32_t events) -{ - if (fd == health_quit_pipe[0] && (events & LPOLLIN)) { - return 1; - } - - return 0; -} - /* * Send data on a unix socket using the liblttsessiondcomm API. * @@ -112,7 +96,8 @@ static int create_lttng_rundir_with_perm(const char *rundir) } ret = chmod(rundir, - S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); + S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | + S_IXOTH); if (ret < 0) { ERR("Unable to set permissions on %s", rundir); PERROR("chmod"); @@ -126,27 +111,24 @@ error: return ret; } -static -int parse_health_env(void) +static int parse_health_env() { const char *health_path; health_path = lttng_secure_getenv(LTTNG_RELAYD_HEALTH_ENV); if (health_path) { - strncpy(health_unix_sock_path, health_path, - PATH_MAX); + strncpy(health_unix_sock_path, health_path, PATH_MAX); health_unix_sock_path[PATH_MAX - 1] = '\0'; } return 0; } -static -int setup_health_path(void) +static int setup_health_path() { int is_root, ret = 0; - const char *home_path = NULL; - char *rundir = NULL, *relayd_path = NULL; + const char *home_path = nullptr; + char *rundir = nullptr, *relayd_path = nullptr; ret = parse_health_env(); if (ret) { @@ -168,7 +150,7 @@ int setup_health_path(void) */ home_path = utils_get_home_dir(); - if (home_path == NULL) { + if (home_path == nullptr) { /* TODO: Add --socket PATH option */ ERR("Can't get HOME directory for sockets creation."); ret = -EPERM; @@ -202,18 +184,21 @@ int setup_health_path(void) if (strlen(health_unix_sock_path) != 0) { goto end; } - snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), - DEFAULT_GLOBAL_RELAY_HEALTH_UNIX_SOCK, - (int) getpid()); + snprintf(health_unix_sock_path, + sizeof(health_unix_sock_path), + DEFAULT_GLOBAL_RELAY_HEALTH_UNIX_SOCK, + (int) getpid()); } else { /* Set health check Unix path */ if (strlen(health_unix_sock_path) != 0) { goto end; } - snprintf(health_unix_sock_path, sizeof(health_unix_sock_path), - DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK, - home_path, (int) getpid()); + snprintf(health_unix_sock_path, + sizeof(health_unix_sock_path), + DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK, + home_path, + (int) getpid()); } end: @@ -222,8 +207,7 @@ end: return ret; } -static -int accept_unix_socket(void *data, int *out_fd) +static int accept_unix_socket(void *data, int *out_fd) { int ret; int accepting_sock = *((int *) data); @@ -239,8 +223,7 @@ end: return ret; } -static -int open_unix_socket(void *data, int *out_fd) +static int open_unix_socket(void *data, int *out_fd) { int ret; const char *path = (const char *) data; @@ -259,10 +242,10 @@ end: /* * Thread managing health check socket. */ -void *thread_manage_health_relayd(void *data) +void *thread_manage_health_relayd(void *data __attribute__((unused))) { - int sock = -1, new_sock = -1, ret, i, pollfd, err = -1; - uint32_t revents, nb_fd; + int sock = -1, new_sock = -1, ret, i, err = -1; + uint32_t nb_fd; struct lttng_poll_event events; struct health_comm_msg msg; struct health_comm_reply reply; @@ -285,9 +268,12 @@ void *thread_manage_health_relayd(void *data) err = -1; goto error; } - ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &sock, - (const char **) &sock_name, 1, open_unix_socket, - health_unix_sock_path); + ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, + &sock, + (const char **) &sock_name, + 1, + open_unix_socket, + health_unix_sock_path); free(sock_name); if (ret < 0) { ERR("Unable to create health check Unix socket"); @@ -314,8 +300,7 @@ void *thread_manage_health_relayd(void *data) goto error; } - ret = chmod(health_unix_sock_path, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + ret = chmod(health_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (ret < 0) { ERR("Unable to set permissions on %s", health_unix_sock_path); PERROR("chmod"); @@ -336,9 +321,8 @@ void *thread_manage_health_relayd(void *data) } /* Size is set to 2 for the unix socket and quit pipe. */ - ret = fd_tracker_util_poll_create(the_fd_tracker, - "Health management thread epoll", &events, 2, - LTTNG_CLOEXEC); + ret = fd_tracker_util_poll_create( + the_fd_tracker, "Health management thread epoll", &events, 2, LTTNG_CLOEXEC); if (ret < 0) { ERR("Poll set creation failed"); goto error; @@ -357,13 +341,13 @@ void *thread_manage_health_relayd(void *data) lttng_relay_notify_ready(); - while (1) { + while (true) { char *accepted_socket_name; DBG("Health check ready"); /* Inifinite blocking call, waiting for transmission */ -restart: + restart: ret = lttng_poll_wait(&events, -1); if (ret < 0) { /* @@ -379,12 +363,12 @@ restart: for (i = 0; i < nb_fd; i++) { /* Fetch once the poll data */ - revents = LTTNG_POLL_GETEV(&events, i); - pollfd = LTTNG_POLL_GETFD(&events, i); + const auto revents = LTTNG_POLL_GETEV(&events, i); + const auto pollfd = LTTNG_POLL_GETFD(&events, i); - /* Thread quit pipe has been closed. Killing thread. */ - ret = check_health_quit_pipe(pollfd, revents); - if (ret) { + /* Activity on thread quit pipe, exiting. */ + if (pollfd == health_quit_pipe[0]) { + DBG("Activity on thread quit pipe"); err = 0; goto exit; } @@ -397,22 +381,28 @@ restart: ERR("Health socket poll error"); goto error; } else { - ERR("Unexpected poll events %u for sock %d", revents, pollfd); + ERR("Unexpected poll events %u for sock %d", + revents, + pollfd); goto error; } } } - ret = asprintf(&accepted_socket_name, "Socket accepted from unix socket @ %s", - health_unix_sock_path); + ret = asprintf(&accepted_socket_name, + "Socket accepted from unix socket @ %s", + health_unix_sock_path); if (ret == -1) { PERROR("Failed to allocate name of accepted socket from unix socket @ %s", - health_unix_sock_path); + health_unix_sock_path); goto error; } - ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &new_sock, - (const char **) &accepted_socket_name, 1, - accept_unix_socket, &sock); + ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, + &new_sock, + (const char **) &accepted_socket_name, + 1, + accept_unix_socket, + &sock); free(accepted_socket_name); if (ret < 0) { goto error; @@ -425,12 +415,11 @@ restart: (void) utils_set_fd_cloexec(new_sock); DBG("Receiving data from client for health..."); - ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg)); + ret = lttcomm_recv_unix_sock(new_sock, (void *) &msg, sizeof(msg)); if (ret <= 0) { DBG("Nothing recv() from client... continuing"); - ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, - &new_sock, 1, fd_tracker_util_close_fd, - NULL); + ret = fd_tracker_close_unsuspendable_fd( + the_fd_tracker, &new_sock, 1, fd_tracker_util_close_fd, nullptr); if (ret) { PERROR("close"); } @@ -461,9 +450,8 @@ restart: } /* End of transmission */ - ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, - &new_sock, 1, fd_tracker_util_close_fd, - NULL); + ret = fd_tracker_close_unsuspendable_fd( + the_fd_tracker, &new_sock, 1, fd_tracker_util_close_fd, nullptr); if (ret) { PERROR("close"); } @@ -479,8 +467,8 @@ exit: DBG("Health check thread dying"); unlink(health_unix_sock_path); if (sock >= 0) { - ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, &sock, - 1, fd_tracker_util_close_fd, NULL); + ret = fd_tracker_close_unsuspendable_fd( + the_fd_tracker, &sock, 1, fd_tracker_util_close_fd, nullptr); if (ret) { PERROR("close"); } @@ -494,5 +482,5 @@ exit: (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); rcu_unregister_thread(); - return NULL; + return nullptr; }