clang-tidy: add Chrome-inspired checks
[lttng-tools.git] / src / bin / lttng-consumerd / health-consumerd.cpp
CommitLineData
5c635c72 1/*
ab5be9fa 2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5c635c72 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
5c635c72 5 *
5c635c72
MD
6 */
7
6c1c0768 8#define _LGPL_SOURCE
28ab034a
JG
9#include "health-consumerd.hpp"
10#include "lttng-consumerd.hpp"
11
12#include <common/common.hpp>
13#include <common/compat/poll.hpp>
14#include <common/consumer/consumer-timer.hpp>
15#include <common/consumer/consumer.hpp>
16#include <common/defaults.hpp>
17#include <common/sessiond-comm/sessiond-comm.hpp>
18#include <common/utils.hpp>
19
5c635c72
MD
20#include <fcntl.h>
21#include <getopt.h>
22#include <grp.h>
28ab034a 23#include <inttypes.h>
5c635c72 24#include <limits.h>
28ab034a 25#include <poll.h>
5c635c72
MD
26#include <pthread.h>
27#include <signal.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/ipc.h>
28ab034a 32#include <sys/mman.h>
5c635c72
MD
33#include <sys/resource.h>
34#include <sys/shm.h>
35#include <sys/socket.h>
36#include <sys/stat.h>
37#include <sys/types.h>
28ab034a 38#include <ulimit.h>
5c635c72 39#include <unistd.h>
5c635c72 40#include <urcu/compiler.h>
28ab034a 41#include <urcu/list.h>
5c635c72
MD
42
43/* Global health check unix path */
44static char health_unix_sock_path[PATH_MAX];
45
28ab034a 46int health_quit_pipe[2] = { -1, -1 };
5c635c72
MD
47
48/*
49 * Send data on a unix socket using the liblttsessiondcomm API.
50 *
51 * Return lttcomm error code.
52 */
53static int send_unix_sock(int sock, void *buf, size_t len)
54{
55 /* Check valid length */
56 if (len == 0) {
57 return -1;
58 }
59
60 return lttcomm_send_unix_sock(sock, buf, len);
61}
62
cd9adb8b 63static int setup_health_path()
5c635c72
MD
64{
65 int is_root, ret = 0;
66 enum lttng_consumer_type type;
67 const char *home_path;
68
69 type = lttng_consumer_get_type();
70 is_root = !getuid();
71
72 if (is_root) {
73 if (strlen(health_unix_sock_path) != 0) {
74 goto end;
75 }
76 switch (type) {
77 case LTTNG_CONSUMER_KERNEL:
28ab034a
JG
78 snprintf(health_unix_sock_path,
79 sizeof(health_unix_sock_path),
80 DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK);
5c635c72
MD
81 break;
82 case LTTNG_CONSUMER64_UST:
28ab034a
JG
83 snprintf(health_unix_sock_path,
84 sizeof(health_unix_sock_path),
85 DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK);
5c635c72
MD
86 break;
87 case LTTNG_CONSUMER32_UST:
28ab034a
JG
88 snprintf(health_unix_sock_path,
89 sizeof(health_unix_sock_path),
90 DEFAULT_GLOBAL_USTCONSUMER32_HEALTH_UNIX_SOCK);
5c635c72
MD
91 break;
92 default:
93 ret = -EINVAL;
94 goto end;
95 }
96 } else {
5c635c72 97 home_path = utils_get_home_dir();
cd9adb8b 98 if (home_path == nullptr) {
5c635c72
MD
99 /* TODO: Add --socket PATH option */
100 ERR("Can't get HOME directory for sockets creation.");
101 ret = -EPERM;
102 goto end;
103 }
104
5c635c72
MD
105 /* Set health check Unix path */
106 if (strlen(health_unix_sock_path) != 0) {
107 goto end;
108 }
109 switch (type) {
110 case LTTNG_CONSUMER_KERNEL:
28ab034a
JG
111 snprintf(health_unix_sock_path,
112 sizeof(health_unix_sock_path),
113 DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK,
114 home_path);
5c635c72
MD
115 break;
116 case LTTNG_CONSUMER64_UST:
28ab034a
JG
117 snprintf(health_unix_sock_path,
118 sizeof(health_unix_sock_path),
119 DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK,
120 home_path);
5c635c72
MD
121 break;
122 case LTTNG_CONSUMER32_UST:
28ab034a
JG
123 snprintf(health_unix_sock_path,
124 sizeof(health_unix_sock_path),
125 DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK,
126 home_path);
5c635c72
MD
127 break;
128 default:
129 ret = -EINVAL;
130 goto end;
131 }
132 }
5c635c72
MD
133end:
134 return ret;
135}
136
137/*
138 * Thread managing health check socket.
139 */
f46376a1 140void *thread_manage_health_consumerd(void *data __attribute__((unused)))
5c635c72 141{
8a00688e
MJ
142 int sock = -1, new_sock = -1, ret, i, err = -1;
143 uint32_t nb_fd;
5c635c72
MD
144 struct lttng_poll_event events;
145 struct health_comm_msg msg;
146 struct health_comm_reply reply;
6c71277b 147 int is_root;
5c635c72
MD
148
149 DBG("[thread] Manage health check started");
150
151 setup_health_path();
152
153 rcu_register_thread();
154
155 /* We might hit an error path before this is created. */
156 lttng_poll_init(&events);
157
158 /* Create unix socket */
159 sock = lttcomm_create_unix_sock(health_unix_sock_path);
160 if (sock < 0) {
161 ERR("Unable to create health check Unix socket");
67fe4075 162 err = -1;
5c635c72
MD
163 goto error;
164 }
165
6c71277b
MD
166 is_root = !getuid();
167 if (is_root) {
168 /* lttng health client socket path permissions */
28ab59d0
JR
169 gid_t gid;
170
171 ret = utils_get_group_id(tracing_group_name, true, &gid);
172 if (ret) {
173 /* Default to root group. */
174 gid = 0;
175 }
176
177 ret = chown(health_unix_sock_path, 0, gid);
6c71277b
MD
178 if (ret < 0) {
179 ERR("Unable to set group on %s", health_unix_sock_path);
180 PERROR("chown");
67fe4075 181 err = -1;
6c71277b
MD
182 goto error;
183 }
184
28ab034a 185 ret = chmod(health_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
6c71277b
MD
186 if (ret < 0) {
187 ERR("Unable to set permissions on %s", health_unix_sock_path);
188 PERROR("chmod");
67fe4075 189 err = -1;
6c71277b
MD
190 goto error;
191 }
192 }
193
5c635c72
MD
194 /*
195 * Set the CLOEXEC flag. Return code is useless because either way, the
196 * show must go on.
197 */
198 (void) utils_set_fd_cloexec(sock);
199
200 ret = lttcomm_listen_unix_sock(sock);
201 if (ret < 0) {
202 goto error;
203 }
204
3069d754 205 /* Size is set to 2 for the quit pipe and registration socket. */
5c635c72
MD
206 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
207 if (ret < 0) {
208 ERR("Poll set creation failed");
209 goto error;
210 }
211
212 ret = lttng_poll_add(&events, health_quit_pipe[0], LPOLLIN);
213 if (ret < 0) {
214 goto error;
215 }
216
217 /* Add the application registration socket */
218 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
219 if (ret < 0) {
220 goto error;
221 }
222
748b7b07
MD
223 /* Perform prior memory accesses before decrementing ready */
224 cmm_smp_mb__before_uatomic_dec();
225 uatomic_dec(&lttng_consumer_ready);
226
cd9adb8b 227 while (true) {
5c635c72
MD
228 DBG("Health check ready");
229
230 /* Inifinite blocking call, waiting for transmission */
28ab034a 231 restart:
5c635c72
MD
232 ret = lttng_poll_wait(&events, -1);
233 if (ret < 0) {
234 /*
235 * Restart interrupted system call.
236 */
237 if (errno == EINTR) {
238 goto restart;
239 }
240 goto error;
241 }
242
243 nb_fd = ret;
244
245 for (i = 0; i < nb_fd; i++) {
246 /* Fetch once the poll data */
8a00688e
MJ
247 const auto revents = LTTNG_POLL_GETEV(&events, i);
248 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
5c635c72 249
8a00688e
MJ
250 /* Activity on health quit pipe, exiting. */
251 if (pollfd == health_quit_pipe[0]) {
252 DBG("Activity on health quit pipe");
5c635c72
MD
253 err = 0;
254 goto exit;
255 }
256
257 /* Event on the registration socket */
258 if (pollfd == sock) {
28ab034a
JG
259 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) &&
260 !(revents & LPOLLIN)) {
5c635c72
MD
261 ERR("Health socket poll error");
262 goto error;
263 }
264 }
265 }
266
267 new_sock = lttcomm_accept_unix_sock(sock);
268 if (new_sock < 0) {
269 goto error;
270 }
271
272 /*
273 * Set the CLOEXEC flag. Return code is useless because either way, the
274 * show must go on.
275 */
276 (void) utils_set_fd_cloexec(new_sock);
277
278 DBG("Receiving data from client for health...");
28ab034a 279 ret = lttcomm_recv_unix_sock(new_sock, (void *) &msg, sizeof(msg));
5c635c72
MD
280 if (ret <= 0) {
281 DBG("Nothing recv() from client... continuing");
282 ret = close(new_sock);
283 if (ret) {
284 PERROR("close");
285 }
286 new_sock = -1;
287 continue;
288 }
289
290 rcu_thread_online();
291
a0377dfe 292 LTTNG_ASSERT(msg.cmd == HEALTH_CMD_CHECK);
5c635c72 293
53efb85a 294 memset(&reply, 0, sizeof(reply));
6c71277b
MD
295 for (i = 0; i < NR_HEALTH_CONSUMERD_TYPES; i++) {
296 /*
297 * health_check_state return 0 if thread is in
298 * error.
299 */
300 if (!health_check_state(health_consumerd, i)) {
301 reply.ret_code |= 1ULL << i;
302 }
5c635c72
MD
303 }
304
6137f630 305 DBG("Health check return value %" PRIx64, reply.ret_code);
5c635c72
MD
306
307 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
308 if (ret < 0) {
309 ERR("Failed to send health data back to client");
310 }
311
312 /* End of transmission */
313 ret = close(new_sock);
314 if (ret) {
315 PERROR("close");
316 }
317 new_sock = -1;
318 }
319
320exit:
321error:
322 if (err) {
323 ERR("Health error occurred in %s", __func__);
324 }
325 DBG("Health check thread dying");
326 unlink(health_unix_sock_path);
327 if (sock >= 0) {
328 ret = close(sock);
329 if (ret) {
330 PERROR("close");
331 }
332 }
333
334 lttng_poll_clean(&events);
335
336 rcu_unregister_thread();
cd9adb8b 337 return nullptr;
5c635c72 338}
This page took 0.071742 seconds and 4 git commands to generate.