2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "health-relayd.hpp"
10 #include "lttng-relayd.hpp"
12 #include <common/common.hpp>
13 #include <common/compat/getenv.hpp>
14 #include <common/compat/poll.hpp>
15 #include <common/consumer/consumer-timer.hpp>
16 #include <common/consumer/consumer.hpp>
17 #include <common/defaults.hpp>
18 #include <common/fd-tracker/utils.hpp>
19 #include <common/sessiond-comm/sessiond-comm.hpp>
20 #include <common/utils.hpp>
35 #include <sys/resource.h>
37 #include <sys/socket.h>
39 #include <sys/types.h>
41 #include <urcu/compiler.h>
42 #include <urcu/list.h>
44 /* Global health check unix path */
45 static char health_unix_sock_path
[PATH_MAX
];
47 int health_quit_pipe
[2] = { -1, -1 };
50 * Send data on a unix socket using the liblttsessiondcomm API.
52 * Return lttcomm error code.
54 static int send_unix_sock(int sock
, void *buf
, size_t len
)
56 /* Check valid length */
61 return lttcomm_send_unix_sock(sock
, buf
, len
);
64 static int create_lttng_rundir_with_perm(const char *rundir
)
68 DBG3("Creating LTTng run directory: %s", rundir
);
70 ret
= mkdir(rundir
, S_IRWXU
);
72 if (errno
!= EEXIST
) {
73 ERR("Unable to create %s", rundir
);
78 } else if (ret
== 0) {
79 int is_root
= !getuid();
84 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
86 /* Default to root group. */
90 ret
= chown(rundir
, 0, gid
);
92 ERR("Unable to set group on %s", rundir
);
99 S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
|
102 ERR("Unable to set permissions on %s", rundir
);
114 static int parse_health_env()
116 const char *health_path
;
118 health_path
= lttng_secure_getenv(LTTNG_RELAYD_HEALTH_ENV
);
120 strncpy(health_unix_sock_path
, health_path
, PATH_MAX
);
121 health_unix_sock_path
[PATH_MAX
- 1] = '\0';
127 static int setup_health_path()
129 int is_root
, ret
= 0;
130 const char *home_path
= nullptr;
131 char *rundir
= nullptr, *relayd_path
= nullptr;
133 ret
= parse_health_env();
141 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
148 * Create rundir from home path. This will create something like
151 home_path
= utils_get_home_dir();
153 if (home_path
== nullptr) {
154 /* TODO: Add --socket PATH option */
155 ERR("Can't get HOME directory for sockets creation.");
160 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
167 ret
= asprintf(&relayd_path
, DEFAULT_RELAYD_PATH
, rundir
);
173 ret
= create_lttng_rundir_with_perm(rundir
);
178 ret
= create_lttng_rundir_with_perm(relayd_path
);
184 if (strlen(health_unix_sock_path
) != 0) {
187 snprintf(health_unix_sock_path
,
188 sizeof(health_unix_sock_path
),
189 DEFAULT_GLOBAL_RELAY_HEALTH_UNIX_SOCK
,
192 /* Set health check Unix path */
193 if (strlen(health_unix_sock_path
) != 0) {
197 snprintf(health_unix_sock_path
,
198 sizeof(health_unix_sock_path
),
199 DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK
,
210 static int accept_unix_socket(void *data
, int *out_fd
)
213 int accepting_sock
= *((int *) data
);
215 ret
= lttcomm_accept_unix_sock(accepting_sock
);
226 static int open_unix_socket(void *data
, int *out_fd
)
229 const char *path
= (const char *) data
;
231 ret
= lttcomm_create_unix_sock(path
);
243 * Thread managing health check socket.
245 void *thread_manage_health_relayd(void *data
__attribute__((unused
)))
247 int sock
= -1, new_sock
= -1, ret
, i
, err
= -1;
249 struct lttng_poll_event events
;
250 struct health_comm_msg msg
;
251 struct health_comm_reply reply
;
255 DBG("[thread] Manage health check started");
259 rcu_register_thread();
261 /* We might hit an error path before this is created. */
262 lttng_poll_init(&events
);
264 /* Create unix socket */
265 ret
= asprintf(&sock_name
, "Unix socket @ %s", health_unix_sock_path
);
267 PERROR("Failed to allocate unix socket name");
271 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
,
273 (const char **) &sock_name
,
276 health_unix_sock_path
);
279 ERR("Unable to create health check Unix socket");
286 /* lttng health client socket path permissions */
289 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
291 /* Default to root group. */
295 ret
= chown(health_unix_sock_path
, 0, gid
);
297 ERR("Unable to set group on %s", health_unix_sock_path
);
303 ret
= chmod(health_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
305 ERR("Unable to set permissions on %s", health_unix_sock_path
);
313 * Set the CLOEXEC flag. Return code is useless because either way, the
316 (void) utils_set_fd_cloexec(sock
);
318 ret
= lttcomm_listen_unix_sock(sock
);
323 /* Size is set to 2 for the unix socket and quit pipe. */
324 ret
= fd_tracker_util_poll_create(
325 the_fd_tracker
, "Health management thread epoll", &events
, 2, LTTNG_CLOEXEC
);
327 ERR("Poll set creation failed");
331 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
336 /* Add the application registration socket */
337 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
342 lttng_relay_notify_ready();
345 char *accepted_socket_name
;
347 DBG("Health check ready");
349 /* Inifinite blocking call, waiting for transmission */
351 ret
= lttng_poll_wait(&events
, -1);
354 * Restart interrupted system call.
356 if (errno
== EINTR
) {
364 for (i
= 0; i
< nb_fd
; i
++) {
365 /* Fetch once the poll data */
366 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
367 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
369 /* Activity on thread quit pipe, exiting. */
370 if (pollfd
== health_quit_pipe
[0]) {
371 DBG("Activity on thread quit pipe");
376 /* Event on the registration socket */
377 if (pollfd
== sock
) {
378 if (revents
& LPOLLIN
) {
380 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
381 ERR("Health socket poll error");
384 ERR("Unexpected poll events %u for sock %d",
392 ret
= asprintf(&accepted_socket_name
,
393 "Socket accepted from unix socket @ %s",
394 health_unix_sock_path
);
396 PERROR("Failed to allocate name of accepted socket from unix socket @ %s",
397 health_unix_sock_path
);
400 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
,
402 (const char **) &accepted_socket_name
,
406 free(accepted_socket_name
);
412 * Set the CLOEXEC flag. Return code is useless because either way, the
415 (void) utils_set_fd_cloexec(new_sock
);
417 DBG("Receiving data from client for health...");
418 ret
= lttcomm_recv_unix_sock(new_sock
, (void *) &msg
, sizeof(msg
));
420 DBG("Nothing recv() from client... continuing");
421 ret
= fd_tracker_close_unsuspendable_fd(
422 the_fd_tracker
, &new_sock
, 1, fd_tracker_util_close_fd
, nullptr);
432 LTTNG_ASSERT(msg
.cmd
== HEALTH_CMD_CHECK
);
434 memset(&reply
, 0, sizeof(reply
));
435 for (i
= 0; i
< NR_HEALTH_RELAYD_TYPES
; i
++) {
437 * health_check_state return 0 if thread is in
440 if (!health_check_state(health_relayd
, i
)) {
441 reply
.ret_code
|= 1ULL << i
;
445 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
447 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
449 ERR("Failed to send health data back to client");
452 /* End of transmission */
453 ret
= fd_tracker_close_unsuspendable_fd(
454 the_fd_tracker
, &new_sock
, 1, fd_tracker_util_close_fd
, nullptr);
462 lttng_relay_stop_threads();
465 ERR("Health error occurred in %s", __func__
);
467 DBG("Health check thread dying");
468 unlink(health_unix_sock_path
);
470 ret
= fd_tracker_close_unsuspendable_fd(
471 the_fd_tracker
, &sock
, 1, fd_tracker_util_close_fd
, nullptr);
478 * We do NOT rmdir rundir nor the relayd path because there are
479 * other processes using them.
482 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
484 rcu_unregister_thread();