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>
28 #include <urcu/compiler.h>
31 #include <common/defaults.hpp>
32 #include <common/common.hpp>
33 #include <common/consumer/consumer.hpp>
34 #include <common/consumer/consumer-timer.hpp>
35 #include <common/compat/poll.hpp>
36 #include <common/sessiond-comm/sessiond-comm.hpp>
37 #include <common/utils.hpp>
38 #include <common/compat/getenv.hpp>
39 #include <common/fd-tracker/utils.hpp>
41 #include "lttng-relayd.hpp"
42 #include "health-relayd.hpp"
44 /* Global health check unix path */
46 char health_unix_sock_path
[PATH_MAX
];
48 int health_quit_pipe
[2] = { -1, -1 };
51 * Send data on a unix socket using the liblttsessiondcomm API.
53 * Return lttcomm error code.
55 static int send_unix_sock(int sock
, void *buf
, size_t len
)
57 /* Check valid length */
62 return lttcomm_send_unix_sock(sock
, buf
, len
);
65 static int create_lttng_rundir_with_perm(const char *rundir
)
69 DBG3("Creating LTTng run directory: %s", rundir
);
71 ret
= mkdir(rundir
, S_IRWXU
);
73 if (errno
!= EEXIST
) {
74 ERR("Unable to create %s", rundir
);
79 } else if (ret
== 0) {
80 int is_root
= !getuid();
85 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
87 /* Default to root group. */
91 ret
= chown(rundir
, 0, gid
);
93 ERR("Unable to set group on %s", rundir
);
100 S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
102 ERR("Unable to set permissions on %s", rundir
);
115 int parse_health_env(void)
117 const char *health_path
;
119 health_path
= lttng_secure_getenv(LTTNG_RELAYD_HEALTH_ENV
);
121 strncpy(health_unix_sock_path
, health_path
,
123 health_unix_sock_path
[PATH_MAX
- 1] = '\0';
130 int setup_health_path(void)
132 int is_root
, ret
= 0;
133 const char *home_path
= NULL
;
134 char *rundir
= NULL
, *relayd_path
= NULL
;
136 ret
= parse_health_env();
144 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
151 * Create rundir from home path. This will create something like
154 home_path
= utils_get_home_dir();
156 if (home_path
== NULL
) {
157 /* TODO: Add --socket PATH option */
158 ERR("Can't get HOME directory for sockets creation.");
163 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
170 ret
= asprintf(&relayd_path
, DEFAULT_RELAYD_PATH
, rundir
);
176 ret
= create_lttng_rundir_with_perm(rundir
);
181 ret
= create_lttng_rundir_with_perm(relayd_path
);
187 if (strlen(health_unix_sock_path
) != 0) {
190 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
191 DEFAULT_GLOBAL_RELAY_HEALTH_UNIX_SOCK
,
194 /* Set health check Unix path */
195 if (strlen(health_unix_sock_path
) != 0) {
199 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
200 DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK
,
201 home_path
, (int) getpid());
211 int accept_unix_socket(void *data
, int *out_fd
)
214 int accepting_sock
= *((int *) data
);
216 ret
= lttcomm_accept_unix_sock(accepting_sock
);
228 int open_unix_socket(void *data
, int *out_fd
)
231 const char *path
= (const char *) data
;
233 ret
= lttcomm_create_unix_sock(path
);
245 * Thread managing health check socket.
247 void *thread_manage_health_relayd(void *data
__attribute__((unused
)))
249 int sock
= -1, new_sock
= -1, ret
, i
, err
= -1;
251 struct lttng_poll_event events
;
252 struct health_comm_msg msg
;
253 struct health_comm_reply reply
;
257 DBG("[thread] Manage health check started");
261 rcu_register_thread();
263 /* We might hit an error path before this is created. */
264 lttng_poll_init(&events
);
266 /* Create unix socket */
267 ret
= asprintf(&sock_name
, "Unix socket @ %s", health_unix_sock_path
);
269 PERROR("Failed to allocate unix socket name");
273 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &sock
,
274 (const char **) &sock_name
, 1, open_unix_socket
,
275 health_unix_sock_path
);
278 ERR("Unable to create health check Unix socket");
285 /* lttng health client socket path permissions */
288 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
290 /* Default to root group. */
294 ret
= chown(health_unix_sock_path
, 0, gid
);
296 ERR("Unable to set group on %s", health_unix_sock_path
);
302 ret
= chmod(health_unix_sock_path
,
303 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(the_fd_tracker
,
325 "Health management thread epoll", &events
, 2,
328 ERR("Poll set creation failed");
332 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
337 /* Add the application registration socket */
338 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
343 lttng_relay_notify_ready();
346 char *accepted_socket_name
;
348 DBG("Health check ready");
350 /* Inifinite blocking call, waiting for transmission */
352 ret
= lttng_poll_wait(&events
, -1);
355 * Restart interrupted system call.
357 if (errno
== EINTR
) {
365 for (i
= 0; i
< nb_fd
; i
++) {
366 /* Fetch once the poll data */
367 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
368 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
370 /* Activity on thread quit pipe, exiting. */
371 if (pollfd
== health_quit_pipe
[0]) {
372 DBG("Activity on thread quit pipe");
377 /* Event on the registration socket */
378 if (pollfd
== sock
) {
379 if (revents
& LPOLLIN
) {
381 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
382 ERR("Health socket poll error");
385 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
391 ret
= asprintf(&accepted_socket_name
, "Socket accepted from unix socket @ %s",
392 health_unix_sock_path
);
394 PERROR("Failed to allocate name of accepted socket from unix socket @ %s",
395 health_unix_sock_path
);
398 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &new_sock
,
399 (const char **) &accepted_socket_name
, 1,
400 accept_unix_socket
, &sock
);
401 free(accepted_socket_name
);
407 * Set the CLOEXEC flag. Return code is useless because either way, the
410 (void) utils_set_fd_cloexec(new_sock
);
412 DBG("Receiving data from client for health...");
413 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
415 DBG("Nothing recv() from client... continuing");
416 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
417 &new_sock
, 1, fd_tracker_util_close_fd
,
428 LTTNG_ASSERT(msg
.cmd
== HEALTH_CMD_CHECK
);
430 memset(&reply
, 0, sizeof(reply
));
431 for (i
= 0; i
< NR_HEALTH_RELAYD_TYPES
; i
++) {
433 * health_check_state return 0 if thread is in
436 if (!health_check_state(health_relayd
, i
)) {
437 reply
.ret_code
|= 1ULL << i
;
441 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
443 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
445 ERR("Failed to send health data back to client");
448 /* End of transmission */
449 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
450 &new_sock
, 1, fd_tracker_util_close_fd
,
459 lttng_relay_stop_threads();
462 ERR("Health error occurred in %s", __func__
);
464 DBG("Health check thread dying");
465 unlink(health_unix_sock_path
);
467 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
, &sock
,
468 1, fd_tracker_util_close_fd
, NULL
);
475 * We do NOT rmdir rundir nor the relayd path because there are
476 * other processes using them.
479 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
481 rcu_unregister_thread();