Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / bin / lttng-relayd / health-relayd.cpp
index d43c371856b9fdc45e3dd9ccb9287eef4b614f57..d1b619d3972d9f0a21d30adb2cd164a990fe4c6c 100644 (file)
@@ -6,62 +6,46 @@
  */
 
 #define _LGPL_SOURCE
+#include "health-relayd.hpp"
+#include "lttng-relayd.hpp"
+
+#include <common/common.hpp>
+#include <common/compat/getenv.hpp>
+#include <common/compat/poll.hpp>
+#include <common/consumer/consumer-timer.hpp>
+#include <common/consumer/consumer.hpp>
+#include <common/defaults.hpp>
+#include <common/fd-tracker/utils.hpp>
+#include <common/sessiond-comm/sessiond-comm.hpp>
+#include <common/utils.hpp>
+
 #include <fcntl.h>
 #include <getopt.h>
 #include <grp.h>
+#include <inttypes.h>
 #include <limits.h>
+#include <poll.h>
 #include <pthread.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/ipc.h>
+#include <sys/mman.h>
 #include <sys/resource.h>
 #include <sys/shm.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <urcu/list.h>
-#include <poll.h>
 #include <unistd.h>
-#include <sys/mman.h>
 #include <urcu/compiler.h>
-#include <inttypes.h>
-
-#include <common/defaults.h>
-#include <common/common.h>
-#include <common/consumer/consumer.h>
-#include <common/consumer/consumer-timer.h>
-#include <common/compat/poll.h>
-#include <common/sessiond-comm/sessiond-comm.h>
-#include <common/utils.h>
-#include <common/compat/getenv.h>
-#include <common/fd-tracker/utils.h>
-
-#include "lttng-relayd.h"
-#include "health-relayd.h"
+#include <urcu/list.h>
 
 /* 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;
@@ -261,8 +244,8 @@ end:
  */
 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 __attribute__((unused)))
                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 __attribute__((unused)))
                        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 __attribute__((unused)))
        }
 
        /* 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 __attribute__((unused)))
 
        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;
 }
This page took 0.027245 seconds and 4 git commands to generate.