relayd: track the health unix socket with the fd-tracker
[lttng-tools.git] / src / bin / lttng-relayd / health-relayd.c
CommitLineData
65931c8b
MD
1/*
2 * Copyright (C) 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
6c1c0768 18#define _LGPL_SOURCE
65931c8b
MD
19#include <fcntl.h>
20#include <getopt.h>
21#include <grp.h>
22#include <limits.h>
23#include <pthread.h>
24#include <signal.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/ipc.h>
29#include <sys/resource.h>
30#include <sys/shm.h>
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <urcu/list.h>
35#include <poll.h>
36#include <unistd.h>
37#include <sys/mman.h>
38#include <assert.h>
65931c8b 39#include <urcu/compiler.h>
65931c8b
MD
40#include <inttypes.h>
41
42#include <common/defaults.h>
43#include <common/common.h>
c8fea79c
JR
44#include <common/consumer/consumer.h>
45#include <common/consumer/consumer-timer.h>
65931c8b
MD
46#include <common/compat/poll.h>
47#include <common/sessiond-comm/sessiond-comm.h>
48#include <common/utils.h>
e8fa9fb0 49#include <common/compat/getenv.h>
1c9bd75b 50#include <common/fd-tracker/utils.h>
65931c8b
MD
51
52#include "lttng-relayd.h"
53#include "health-relayd.h"
54
55/* Global health check unix path */
094fe907
MD
56static
57char health_unix_sock_path[PATH_MAX];
65931c8b 58
794e2e5f 59int health_quit_pipe[2] = { -1, -1 };
65931c8b
MD
60
61/*
62 * Check if the thread quit pipe was triggered.
63 *
64 * Return 1 if it was triggered else 0;
65 */
66static
67int check_health_quit_pipe(int fd, uint32_t events)
68{
69 if (fd == health_quit_pipe[0] && (events & LPOLLIN)) {
70 return 1;
71 }
72
73 return 0;
74}
75
76/*
77 * Send data on a unix socket using the liblttsessiondcomm API.
78 *
79 * Return lttcomm error code.
80 */
81static int send_unix_sock(int sock, void *buf, size_t len)
82{
83 /* Check valid length */
84 if (len == 0) {
85 return -1;
86 }
87
88 return lttcomm_send_unix_sock(sock, buf, len);
89}
90
91static int create_lttng_rundir_with_perm(const char *rundir)
92{
93 int ret;
94
95 DBG3("Creating LTTng run directory: %s", rundir);
96
97 ret = mkdir(rundir, S_IRWXU);
98 if (ret < 0) {
99 if (errno != EEXIST) {
100 ERR("Unable to create %s", rundir);
101 goto error;
102 } else {
103 ret = 0;
104 }
105 } else if (ret == 0) {
106 int is_root = !getuid();
107
108 if (is_root) {
28ab59d0
JR
109 gid_t gid;
110
111 ret = utils_get_group_id(tracing_group_name, true, &gid);
112 if (ret) {
113 /* Default to root group. */
114 gid = 0;
115 }
116
117 ret = chown(rundir, 0, gid);
65931c8b
MD
118 if (ret < 0) {
119 ERR("Unable to set group on %s", rundir);
120 PERROR("chown");
121 ret = -1;
122 goto error;
123 }
124
125 ret = chmod(rundir,
126 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
127 if (ret < 0) {
128 ERR("Unable to set permissions on %s", health_unix_sock_path);
129 PERROR("chmod");
130 ret = -1;
131 goto error;
132 }
133 }
134 }
135
136error:
137 return ret;
138}
139
094fe907
MD
140static
141int parse_health_env(void)
142{
143 const char *health_path;
144
e8fa9fb0 145 health_path = lttng_secure_getenv(LTTNG_RELAYD_HEALTH_ENV);
094fe907
MD
146 if (health_path) {
147 strncpy(health_unix_sock_path, health_path,
148 PATH_MAX);
149 health_unix_sock_path[PATH_MAX - 1] = '\0';
150 }
151
152 return 0;
153}
154
65931c8b
MD
155static
156int setup_health_path(void)
157{
158 int is_root, ret = 0;
4f00620d
JG
159 const char *home_path = NULL;
160 char *rundir = NULL, *relayd_path = NULL;
65931c8b 161
094fe907
MD
162 ret = parse_health_env();
163 if (ret) {
164 return ret;
165 }
166
65931c8b
MD
167 is_root = !getuid();
168
169 if (is_root) {
c3844e39 170 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
b6ab01aa
MD
171 if (!rundir) {
172 ret = -ENOMEM;
173 goto end;
174 }
65931c8b
MD
175 } else {
176 /*
177 * Create rundir from home path. This will create something like
178 * $HOME/.lttng
179 */
180 home_path = utils_get_home_dir();
181
182 if (home_path == NULL) {
183 /* TODO: Add --socket PATH option */
184 ERR("Can't get HOME directory for sockets creation.");
185 ret = -EPERM;
186 goto end;
187 }
188
c3844e39 189 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
65931c8b
MD
190 if (ret < 0) {
191 ret = -ENOMEM;
192 goto end;
193 }
194 }
195
c3844e39 196 ret = asprintf(&relayd_path, DEFAULT_RELAYD_PATH, rundir);
65931c8b
MD
197 if (ret < 0) {
198 ret = -ENOMEM;
199 goto end;
200 }
201
c3844e39 202 ret = create_lttng_rundir_with_perm(rundir);
65931c8b
MD
203 if (ret < 0) {
204 goto end;
205 }
206
207 ret = create_lttng_rundir_with_perm(relayd_path);
208 if (ret < 0) {
209 goto end;
210 }
211
212 if (is_root) {
213 if (strlen(health_unix_sock_path) != 0) {
214 goto end;
215 }
216 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
217 DEFAULT_GLOBAL_RELAY_HEALTH_UNIX_SOCK,
d1f721c5 218 (int) getpid());
65931c8b
MD
219 } else {
220 /* Set health check Unix path */
221 if (strlen(health_unix_sock_path) != 0) {
222 goto end;
223 }
224
225 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
226 DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK,
d1f721c5 227 home_path, (int) getpid());
65931c8b
MD
228 }
229
230end:
c3844e39 231 free(rundir);
edd94901 232 free(relayd_path);
65931c8b
MD
233 return ret;
234}
235
1c9bd75b
JG
236static
237int accept_unix_socket(void *data, int *out_fd)
238{
239 int ret;
240 int accepting_sock = *((int *) data);
241
242 ret = lttcomm_accept_unix_sock(accepting_sock);
243 if (ret < 0) {
244 goto end;
245 }
246
247 *out_fd = ret;
248 ret = 0;
249end:
250 return ret;
251}
252
d7eddab9
JG
253static
254int open_unix_socket(void *data, int *out_fd)
255{
256 int ret;
257 const char *path = data;
258
259 ret = lttcomm_create_unix_sock(path);
260 if (ret < 0) {
261 goto end;
262 }
263
264 *out_fd = ret;
265 ret = 0;
266end:
267 return ret;
268}
269
65931c8b
MD
270/*
271 * Thread managing health check socket.
272 */
273void *thread_manage_health(void *data)
274{
275 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
276 uint32_t revents, nb_fd;
277 struct lttng_poll_event events;
278 struct health_comm_msg msg;
279 struct health_comm_reply reply;
280 int is_root;
d7eddab9 281 char *sock_name;
65931c8b
MD
282
283 DBG("[thread] Manage health check started");
284
285 setup_health_path();
286
287 rcu_register_thread();
288
289 /* We might hit an error path before this is created. */
290 lttng_poll_init(&events);
291
292 /* Create unix socket */
d7eddab9
JG
293 ret = asprintf(&sock_name, "Unix socket @ %s", health_unix_sock_path);
294 if (ret == -1) {
295 PERROR("Failed to allocate unix socket name");
296 err = -1;
297 goto error;
298 }
299 ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &sock,
300 (const char **) &sock_name, 1, open_unix_socket,
301 health_unix_sock_path);
302 free(sock_name);
303 if (ret < 0) {
65931c8b 304 ERR("Unable to create health check Unix socket");
7568ddbf 305 err = -1;
65931c8b
MD
306 goto error;
307 }
308
309 is_root = !getuid();
310 if (is_root) {
311 /* lttng health client socket path permissions */
28ab59d0
JR
312 gid_t gid;
313
314 ret = utils_get_group_id(tracing_group_name, true, &gid);
315 if (ret) {
316 /* Default to root group. */
317 gid = 0;
318 }
319
320 ret = chown(health_unix_sock_path, 0, gid);
65931c8b
MD
321 if (ret < 0) {
322 ERR("Unable to set group on %s", health_unix_sock_path);
323 PERROR("chown");
7568ddbf 324 err = -1;
65931c8b
MD
325 goto error;
326 }
327
328 ret = chmod(health_unix_sock_path,
329 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
330 if (ret < 0) {
331 ERR("Unable to set permissions on %s", health_unix_sock_path);
332 PERROR("chmod");
7568ddbf 333 err = -1;
65931c8b
MD
334 goto error;
335 }
336 }
337
338 /*
339 * Set the CLOEXEC flag. Return code is useless because either way, the
340 * show must go on.
341 */
342 (void) utils_set_fd_cloexec(sock);
343
344 ret = lttcomm_listen_unix_sock(sock);
345 if (ret < 0) {
346 goto error;
347 }
348
aa91fbc5
JG
349 /* Size is set to 2 for the unix socket and quit pipe. */
350 ret = fd_tracker_util_poll_create(the_fd_tracker,
351 "Health management thread epoll", &events, 2,
352 LTTNG_CLOEXEC);
65931c8b
MD
353 if (ret < 0) {
354 ERR("Poll set creation failed");
355 goto error;
356 }
357
358 ret = lttng_poll_add(&events, health_quit_pipe[0], LPOLLIN);
359 if (ret < 0) {
360 goto error;
361 }
362
363 /* Add the application registration socket */
364 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
365 if (ret < 0) {
366 goto error;
367 }
368
3fd27398
MD
369 lttng_relay_notify_ready();
370
65931c8b 371 while (1) {
1c9bd75b
JG
372 char *accepted_socket_name;
373
65931c8b
MD
374 DBG("Health check ready");
375
376 /* Inifinite blocking call, waiting for transmission */
377restart:
378 ret = lttng_poll_wait(&events, -1);
379 if (ret < 0) {
380 /*
381 * Restart interrupted system call.
382 */
383 if (errno == EINTR) {
384 goto restart;
385 }
386 goto error;
387 }
388
389 nb_fd = ret;
390
391 for (i = 0; i < nb_fd; i++) {
392 /* Fetch once the poll data */
393 revents = LTTNG_POLL_GETEV(&events, i);
394 pollfd = LTTNG_POLL_GETFD(&events, i);
395
396 /* Thread quit pipe has been closed. Killing thread. */
397 ret = check_health_quit_pipe(pollfd, revents);
398 if (ret) {
399 err = 0;
400 goto exit;
401 }
402
403 /* Event on the registration socket */
404 if (pollfd == sock) {
03e43155
MD
405 if (revents & LPOLLIN) {
406 continue;
407 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
65931c8b
MD
408 ERR("Health socket poll error");
409 goto error;
03e43155
MD
410 } else {
411 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
412 goto error;
65931c8b
MD
413 }
414 }
415 }
416
1c9bd75b
JG
417 ret = asprintf(&accepted_socket_name, "Socket accepted from unix socket @ %s",
418 health_unix_sock_path);
419 if (ret == -1) {
420 PERROR("Failed to allocate name of accepted socket from unix socket @ %s",
421 health_unix_sock_path);
422 goto error;
423 }
424 ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &new_sock,
425 (const char **) &accepted_socket_name, 1,
426 accept_unix_socket, &sock);
427 free(accepted_socket_name);
428 if (ret < 0) {
65931c8b
MD
429 goto error;
430 }
431
432 /*
433 * Set the CLOEXEC flag. Return code is useless because either way, the
434 * show must go on.
435 */
436 (void) utils_set_fd_cloexec(new_sock);
437
438 DBG("Receiving data from client for health...");
439 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
440 if (ret <= 0) {
441 DBG("Nothing recv() from client... continuing");
1c9bd75b
JG
442 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker,
443 &new_sock, 1, fd_tracker_util_close_fd,
444 NULL);
65931c8b
MD
445 if (ret) {
446 PERROR("close");
447 }
448 new_sock = -1;
449 continue;
450 }
451
452 rcu_thread_online();
453
454 assert(msg.cmd == HEALTH_CMD_CHECK);
455
53efb85a 456 memset(&reply, 0, sizeof(reply));
65931c8b
MD
457 for (i = 0; i < NR_HEALTH_RELAYD_TYPES; i++) {
458 /*
459 * health_check_state return 0 if thread is in
460 * error.
461 */
462 if (!health_check_state(health_relayd, i)) {
463 reply.ret_code |= 1ULL << i;
464 }
465 }
466
467 DBG2("Health check return value %" PRIx64, reply.ret_code);
468
469 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
470 if (ret < 0) {
471 ERR("Failed to send health data back to client");
472 }
473
474 /* End of transmission */
1c9bd75b
JG
475 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker,
476 &new_sock, 1, fd_tracker_util_close_fd,
477 NULL);
65931c8b
MD
478 if (ret) {
479 PERROR("close");
480 }
481 new_sock = -1;
482 }
483
65931c8b 484error:
81714439
JG
485 lttng_relay_stop_threads();
486exit:
65931c8b
MD
487 if (err) {
488 ERR("Health error occurred in %s", __func__);
489 }
490 DBG("Health check thread dying");
491 unlink(health_unix_sock_path);
492 if (sock >= 0) {
d7eddab9
JG
493 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, &sock,
494 1, fd_tracker_util_close_fd, NULL);
65931c8b
MD
495 if (ret) {
496 PERROR("close");
497 }
498 }
499
dcbcae3e
MD
500 /*
501 * We do NOT rmdir rundir nor the relayd path because there are
502 * other processes using them.
503 */
504
aa91fbc5 505 (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
65931c8b
MD
506
507 rcu_unregister_thread();
508 return NULL;
509}
This page took 0.060739 seconds and 4 git commands to generate.