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>
33 #include <common/defaults.h>
34 #include <common/common.h>
35 #include <common/consumer/consumer.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/compat/poll.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/utils.h>
41 #include "lttng-consumerd.h"
42 #include "health-consumerd.h"
44 /* Global health check unix path */
45 static char health_unix_sock_path
[PATH_MAX
];
47 int health_quit_pipe
[2];
50 * Check if the thread quit pipe was triggered.
52 * Return 1 if it was triggered else 0;
55 int check_health_quit_pipe(int fd
, uint32_t events
)
57 if (fd
== health_quit_pipe
[0] && (events
& LPOLLIN
)) {
65 * Send data on a unix socket using the liblttsessiondcomm API.
67 * Return lttcomm error code.
69 static int send_unix_sock(int sock
, void *buf
, size_t len
)
71 /* Check valid length */
76 return lttcomm_send_unix_sock(sock
, buf
, len
);
80 int setup_health_path(void)
83 enum lttng_consumer_type type
;
84 const char *home_path
;
86 type
= lttng_consumer_get_type();
90 if (strlen(health_unix_sock_path
) != 0) {
94 case LTTNG_CONSUMER_KERNEL
:
95 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
96 DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK
);
98 case LTTNG_CONSUMER64_UST
:
99 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
100 DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK
);
102 case LTTNG_CONSUMER32_UST
:
103 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
104 DEFAULT_GLOBAL_USTCONSUMER32_HEALTH_UNIX_SOCK
);
111 home_path
= utils_get_home_dir();
112 if (home_path
== NULL
) {
113 /* TODO: Add --socket PATH option */
114 ERR("Can't get HOME directory for sockets creation.");
119 /* Set health check Unix path */
120 if (strlen(health_unix_sock_path
) != 0) {
124 case LTTNG_CONSUMER_KERNEL
:
125 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
126 DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK
, home_path
);
128 case LTTNG_CONSUMER64_UST
:
129 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
130 DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK
, home_path
);
132 case LTTNG_CONSUMER32_UST
:
133 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
134 DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK
, home_path
);
146 * Thread managing health check socket.
148 void *thread_manage_health(void *data
)
150 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
151 uint32_t revents
, nb_fd
;
152 struct lttng_poll_event events
;
153 struct health_comm_msg msg
;
154 struct health_comm_reply reply
;
157 DBG("[thread] Manage health check started");
161 rcu_register_thread();
163 /* We might hit an error path before this is created. */
164 lttng_poll_init(&events
);
166 /* Create unix socket */
167 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
169 ERR("Unable to create health check Unix socket");
176 /* lttng health client socket path permissions */
179 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
181 /* Default to root group. */
185 ret
= chown(health_unix_sock_path
, 0, gid
);
187 ERR("Unable to set group on %s", health_unix_sock_path
);
193 ret
= chmod(health_unix_sock_path
,
194 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
196 ERR("Unable to set permissions on %s", health_unix_sock_path
);
204 * Set the CLOEXEC flag. Return code is useless because either way, the
207 (void) utils_set_fd_cloexec(sock
);
209 ret
= lttcomm_listen_unix_sock(sock
);
214 /* Size is set to 1 for the consumer_channel pipe */
215 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
217 ERR("Poll set creation failed");
221 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
226 /* Add the application registration socket */
227 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
232 /* Perform prior memory accesses before decrementing ready */
233 cmm_smp_mb__before_uatomic_dec();
234 uatomic_dec(<tng_consumer_ready
);
237 DBG("Health check ready");
239 /* Inifinite blocking call, waiting for transmission */
241 ret
= lttng_poll_wait(&events
, -1);
244 * Restart interrupted system call.
246 if (errno
== EINTR
) {
254 for (i
= 0; i
< nb_fd
; i
++) {
255 /* Fetch once the poll data */
256 revents
= LTTNG_POLL_GETEV(&events
, i
);
257 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
259 /* Thread quit pipe has been closed. Killing thread. */
260 ret
= check_health_quit_pipe(pollfd
, revents
);
266 /* Event on the registration socket */
267 if (pollfd
== sock
) {
268 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
269 && !(revents
& LPOLLIN
)) {
270 ERR("Health socket poll error");
276 new_sock
= lttcomm_accept_unix_sock(sock
);
282 * Set the CLOEXEC flag. Return code is useless because either way, the
285 (void) utils_set_fd_cloexec(new_sock
);
287 DBG("Receiving data from client for health...");
288 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
290 DBG("Nothing recv() from client... continuing");
291 ret
= close(new_sock
);
301 assert(msg
.cmd
== HEALTH_CMD_CHECK
);
303 memset(&reply
, 0, sizeof(reply
));
304 for (i
= 0; i
< NR_HEALTH_CONSUMERD_TYPES
; i
++) {
306 * health_check_state return 0 if thread is in
309 if (!health_check_state(health_consumerd
, i
)) {
310 reply
.ret_code
|= 1ULL << i
;
314 DBG("Health check return value %" PRIx64
, reply
.ret_code
);
316 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
318 ERR("Failed to send health data back to client");
321 /* End of transmission */
322 ret
= close(new_sock
);
332 ERR("Health error occurred in %s", __func__
);
334 DBG("Health check thread dying");
335 unlink(health_unix_sock_path
);
343 lttng_poll_clean(&events
);
345 rcu_unregister_thread();