2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
19 #include <sys/resource.h>
21 #include <sys/socket.h>
23 #include <sys/types.h>
24 #include <urcu/list.h>
29 #include <urcu/compiler.h>
32 #include <common/defaults.h>
33 #include <common/common.h>
34 #include <common/consumer/consumer.h>
35 #include <common/consumer/consumer-timer.h>
36 #include <common/compat/poll.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/utils.h>
39 #include <common/compat/getenv.h>
40 #include <common/fd-tracker/utils.h>
42 #include "lttng-relayd.h"
43 #include "health-relayd.h"
45 /* Global health check unix path */
47 char health_unix_sock_path
[PATH_MAX
];
49 int health_quit_pipe
[2] = { -1, -1 };
52 * Check if the thread quit pipe was triggered.
54 * Return 1 if it was triggered else 0;
57 int check_health_quit_pipe(int fd
, uint32_t events
)
59 if (fd
== health_quit_pipe
[0] && (events
& LPOLLIN
)) {
67 * Send data on a unix socket using the liblttsessiondcomm API.
69 * Return lttcomm error code.
71 static int send_unix_sock(int sock
, void *buf
, size_t len
)
73 /* Check valid length */
78 return lttcomm_send_unix_sock(sock
, buf
, len
);
81 static int create_lttng_rundir_with_perm(const char *rundir
)
85 DBG3("Creating LTTng run directory: %s", rundir
);
87 ret
= mkdir(rundir
, S_IRWXU
);
89 if (errno
!= EEXIST
) {
90 ERR("Unable to create %s", rundir
);
95 } else if (ret
== 0) {
96 int is_root
= !getuid();
101 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
103 /* Default to root group. */
107 ret
= chown(rundir
, 0, gid
);
109 ERR("Unable to set group on %s", rundir
);
116 S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
118 ERR("Unable to set permissions on %s", health_unix_sock_path
);
131 int parse_health_env(void)
133 const char *health_path
;
135 health_path
= lttng_secure_getenv(LTTNG_RELAYD_HEALTH_ENV
);
137 strncpy(health_unix_sock_path
, health_path
,
139 health_unix_sock_path
[PATH_MAX
- 1] = '\0';
146 int setup_health_path(void)
148 int is_root
, ret
= 0;
149 const char *home_path
= NULL
;
150 char *rundir
= NULL
, *relayd_path
= NULL
;
152 ret
= parse_health_env();
160 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
167 * Create rundir from home path. This will create something like
170 home_path
= utils_get_home_dir();
172 if (home_path
== NULL
) {
173 /* TODO: Add --socket PATH option */
174 ERR("Can't get HOME directory for sockets creation.");
179 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
186 ret
= asprintf(&relayd_path
, DEFAULT_RELAYD_PATH
, rundir
);
192 ret
= create_lttng_rundir_with_perm(rundir
);
197 ret
= create_lttng_rundir_with_perm(relayd_path
);
203 if (strlen(health_unix_sock_path
) != 0) {
206 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
207 DEFAULT_GLOBAL_RELAY_HEALTH_UNIX_SOCK
,
210 /* Set health check Unix path */
211 if (strlen(health_unix_sock_path
) != 0) {
215 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
216 DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK
,
217 home_path
, (int) getpid());
227 int accept_unix_socket(void *data
, int *out_fd
)
230 int accepting_sock
= *((int *) data
);
232 ret
= lttcomm_accept_unix_sock(accepting_sock
);
244 int open_unix_socket(void *data
, int *out_fd
)
247 const char *path
= data
;
249 ret
= lttcomm_create_unix_sock(path
);
261 * Thread managing health check socket.
263 void *thread_manage_health(void *data
)
265 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
266 uint32_t revents
, nb_fd
;
267 struct lttng_poll_event events
;
268 struct health_comm_msg msg
;
269 struct health_comm_reply reply
;
273 DBG("[thread] Manage health check started");
277 rcu_register_thread();
279 /* We might hit an error path before this is created. */
280 lttng_poll_init(&events
);
282 /* Create unix socket */
283 ret
= asprintf(&sock_name
, "Unix socket @ %s", health_unix_sock_path
);
285 PERROR("Failed to allocate unix socket name");
289 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &sock
,
290 (const char **) &sock_name
, 1, open_unix_socket
,
291 health_unix_sock_path
);
294 ERR("Unable to create health check Unix socket");
301 /* lttng health client socket path permissions */
304 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
306 /* Default to root group. */
310 ret
= chown(health_unix_sock_path
, 0, gid
);
312 ERR("Unable to set group on %s", health_unix_sock_path
);
318 ret
= chmod(health_unix_sock_path
,
319 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
321 ERR("Unable to set permissions on %s", health_unix_sock_path
);
329 * Set the CLOEXEC flag. Return code is useless because either way, the
332 (void) utils_set_fd_cloexec(sock
);
334 ret
= lttcomm_listen_unix_sock(sock
);
339 /* Size is set to 2 for the unix socket and quit pipe. */
340 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
341 "Health management thread epoll", &events
, 2,
344 ERR("Poll set creation failed");
348 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
353 /* Add the application registration socket */
354 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
359 lttng_relay_notify_ready();
362 char *accepted_socket_name
;
364 DBG("Health check ready");
366 /* Inifinite blocking call, waiting for transmission */
368 ret
= lttng_poll_wait(&events
, -1);
371 * Restart interrupted system call.
373 if (errno
== EINTR
) {
381 for (i
= 0; i
< nb_fd
; i
++) {
382 /* Fetch once the poll data */
383 revents
= LTTNG_POLL_GETEV(&events
, i
);
384 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
386 /* Thread quit pipe has been closed. Killing thread. */
387 ret
= check_health_quit_pipe(pollfd
, revents
);
393 /* Event on the registration socket */
394 if (pollfd
== sock
) {
395 if (revents
& LPOLLIN
) {
397 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
398 ERR("Health socket poll error");
401 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
407 ret
= asprintf(&accepted_socket_name
, "Socket accepted from unix socket @ %s",
408 health_unix_sock_path
);
410 PERROR("Failed to allocate name of accepted socket from unix socket @ %s",
411 health_unix_sock_path
);
414 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &new_sock
,
415 (const char **) &accepted_socket_name
, 1,
416 accept_unix_socket
, &sock
);
417 free(accepted_socket_name
);
423 * Set the CLOEXEC flag. Return code is useless because either way, the
426 (void) utils_set_fd_cloexec(new_sock
);
428 DBG("Receiving data from client for health...");
429 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
431 DBG("Nothing recv() from client... continuing");
432 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
433 &new_sock
, 1, fd_tracker_util_close_fd
,
444 assert(msg
.cmd
== HEALTH_CMD_CHECK
);
446 memset(&reply
, 0, sizeof(reply
));
447 for (i
= 0; i
< NR_HEALTH_RELAYD_TYPES
; i
++) {
449 * health_check_state return 0 if thread is in
452 if (!health_check_state(health_relayd
, i
)) {
453 reply
.ret_code
|= 1ULL << i
;
457 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
459 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
461 ERR("Failed to send health data back to client");
464 /* End of transmission */
465 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
466 &new_sock
, 1, fd_tracker_util_close_fd
,
475 lttng_relay_stop_threads();
478 ERR("Health error occurred in %s", __func__
);
480 DBG("Health check thread dying");
481 unlink(health_unix_sock_path
);
483 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
, &sock
,
484 1, fd_tracker_util_close_fd
, NULL
);
491 * We do NOT rmdir rundir nor the relayd path because there are
492 * other processes using them.
495 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
497 rcu_unregister_thread();