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>
32 #include <common/defaults.hpp>
33 #include <common/common.hpp>
34 #include <common/consumer/consumer.hpp>
35 #include <common/consumer/consumer-timer.hpp>
36 #include <common/compat/poll.hpp>
37 #include <common/sessiond-comm/sessiond-comm.hpp>
38 #include <common/utils.hpp>
40 #include "lttng-consumerd.hpp"
41 #include "health-consumerd.hpp"
43 /* Global health check unix path */
44 static char health_unix_sock_path
[PATH_MAX
];
46 int health_quit_pipe
[2];
49 * Check if the thread quit pipe was triggered.
51 * Return 1 if it was triggered else 0;
54 int check_health_quit_pipe(int fd
, uint32_t events
)
56 if (fd
== health_quit_pipe
[0] && (events
& LPOLLIN
)) {
64 * Send data on a unix socket using the liblttsessiondcomm API.
66 * Return lttcomm error code.
68 static int send_unix_sock(int sock
, void *buf
, size_t len
)
70 /* Check valid length */
75 return lttcomm_send_unix_sock(sock
, buf
, len
);
79 int setup_health_path(void)
82 enum lttng_consumer_type type
;
83 const char *home_path
;
85 type
= lttng_consumer_get_type();
89 if (strlen(health_unix_sock_path
) != 0) {
93 case LTTNG_CONSUMER_KERNEL
:
94 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
95 DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK
);
97 case LTTNG_CONSUMER64_UST
:
98 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
99 DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK
);
101 case LTTNG_CONSUMER32_UST
:
102 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
103 DEFAULT_GLOBAL_USTCONSUMER32_HEALTH_UNIX_SOCK
);
110 home_path
= utils_get_home_dir();
111 if (home_path
== NULL
) {
112 /* TODO: Add --socket PATH option */
113 ERR("Can't get HOME directory for sockets creation.");
118 /* Set health check Unix path */
119 if (strlen(health_unix_sock_path
) != 0) {
123 case LTTNG_CONSUMER_KERNEL
:
124 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
125 DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK
, home_path
);
127 case LTTNG_CONSUMER64_UST
:
128 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
129 DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK
, home_path
);
131 case LTTNG_CONSUMER32_UST
:
132 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
133 DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK
, home_path
);
145 * Thread managing health check socket.
147 void *thread_manage_health_consumerd(void *data
__attribute__((unused
)))
149 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
150 uint32_t revents
, nb_fd
;
151 struct lttng_poll_event events
;
152 struct health_comm_msg msg
;
153 struct health_comm_reply reply
;
156 DBG("[thread] Manage health check started");
160 rcu_register_thread();
162 /* We might hit an error path before this is created. */
163 lttng_poll_init(&events
);
165 /* Create unix socket */
166 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
168 ERR("Unable to create health check Unix socket");
175 /* lttng health client socket path permissions */
178 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
180 /* Default to root group. */
184 ret
= chown(health_unix_sock_path
, 0, gid
);
186 ERR("Unable to set group on %s", health_unix_sock_path
);
192 ret
= chmod(health_unix_sock_path
,
193 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
195 ERR("Unable to set permissions on %s", health_unix_sock_path
);
203 * Set the CLOEXEC flag. Return code is useless because either way, the
206 (void) utils_set_fd_cloexec(sock
);
208 ret
= lttcomm_listen_unix_sock(sock
);
213 /* Size is set to 2 for the quit pipe and registration socket. */
214 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
216 ERR("Poll set creation failed");
220 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
225 /* Add the application registration socket */
226 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
231 /* Perform prior memory accesses before decrementing ready */
232 cmm_smp_mb__before_uatomic_dec();
233 uatomic_dec(<tng_consumer_ready
);
236 DBG("Health check ready");
238 /* Inifinite blocking call, waiting for transmission */
240 ret
= lttng_poll_wait(&events
, -1);
243 * Restart interrupted system call.
245 if (errno
== EINTR
) {
253 for (i
= 0; i
< nb_fd
; i
++) {
254 /* Fetch once the poll data */
255 revents
= LTTNG_POLL_GETEV(&events
, i
);
256 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
258 /* Thread quit pipe has been closed. Killing thread. */
259 ret
= check_health_quit_pipe(pollfd
, revents
);
265 /* Event on the registration socket */
266 if (pollfd
== sock
) {
267 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
268 && !(revents
& LPOLLIN
)) {
269 ERR("Health socket poll error");
275 new_sock
= lttcomm_accept_unix_sock(sock
);
281 * Set the CLOEXEC flag. Return code is useless because either way, the
284 (void) utils_set_fd_cloexec(new_sock
);
286 DBG("Receiving data from client for health...");
287 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
289 DBG("Nothing recv() from client... continuing");
290 ret
= close(new_sock
);
300 LTTNG_ASSERT(msg
.cmd
== HEALTH_CMD_CHECK
);
302 memset(&reply
, 0, sizeof(reply
));
303 for (i
= 0; i
< NR_HEALTH_CONSUMERD_TYPES
; i
++) {
305 * health_check_state return 0 if thread is in
308 if (!health_check_state(health_consumerd
, i
)) {
309 reply
.ret_code
|= 1ULL << i
;
313 DBG("Health check return value %" PRIx64
, reply
.ret_code
);
315 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
317 ERR("Failed to send health data back to client");
320 /* End of transmission */
321 ret
= close(new_sock
);
331 ERR("Health error occurred in %s", __func__
);
333 DBG("Health check thread dying");
334 unlink(health_unix_sock_path
);
342 lttng_poll_clean(&events
);
344 rcu_unregister_thread();