2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "lttng-sessiond.h"
20 #include "health-sessiond.h"
21 #include <common/macros.h>
22 #include <common/error.h>
23 #include <common/utils.h>
24 #include <common/pipe.h>
30 struct thread_notifiers
{
31 struct lttng_pipe
*quit_pipe
;
36 void mark_thread_as_ready(struct thread_notifiers
*notifiers
)
38 DBG("Marking health management thread as ready");
39 sem_post(¬ifiers
->ready
);
43 void wait_until_thread_is_ready(struct thread_notifiers
*notifiers
)
45 DBG("Waiting for health management thread to be ready");
46 sem_wait(¬ifiers
->ready
);
47 DBG("Health management thread is ready");
50 static void cleanup_health_management_thread(void *data
)
52 struct thread_notifiers
*notifiers
= data
;
54 lttng_pipe_destroy(notifiers
->quit_pipe
);
55 sem_destroy(¬ifiers
->ready
);
60 * Thread managing health check socket.
62 static void *thread_manage_health(void *data
)
64 const bool is_root
= (getuid() == 0);
65 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
66 uint32_t revents
, nb_fd
;
67 struct lttng_poll_event events
;
68 struct health_comm_msg msg
;
69 struct health_comm_reply reply
;
70 /* Thread-specific quit pipe. */
71 struct thread_notifiers
*notifiers
= data
;
72 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
73 notifiers
->quit_pipe
);
75 DBG("[thread] Manage health check started");
77 rcu_register_thread();
80 * Created with a size of two for:
84 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
89 /* Create unix socket */
90 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
92 ERR("Unable to create health check Unix socket");
97 /* lttng health client socket path permissions */
100 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true, &gid
);
102 /* Default to root group. */
106 ret
= chown(config
.health_unix_sock_path
.value
, 0, gid
);
108 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
113 ret
= chmod(config
.health_unix_sock_path
.value
,
114 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
116 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
123 * Set the CLOEXEC flag. Return code is useless because either way, the
126 (void) utils_set_fd_cloexec(sock
);
128 ret
= lttcomm_listen_unix_sock(sock
);
133 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLERR
);
138 /* Add the application registration socket */
139 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
144 mark_thread_as_ready(notifiers
);
146 DBG("Health check ready");
148 /* Infinite blocking call, waiting for transmission */
150 ret
= lttng_poll_wait(&events
, -1);
153 * Restart interrupted system call.
155 if (errno
== EINTR
) {
163 for (i
= 0; i
< nb_fd
; i
++) {
164 /* Fetch once the poll data */
165 revents
= LTTNG_POLL_GETEV(&events
, i
);
166 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
169 /* No activity for this FD (poll implementation). */
173 /* Event on the registration socket */
174 if (pollfd
== sock
) {
175 if (revents
& LPOLLIN
) {
177 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
178 ERR("Health socket poll error");
181 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
185 /* Event on the thread's quit pipe. */
191 new_sock
= lttcomm_accept_unix_sock(sock
);
197 * Set the CLOEXEC flag. Return code is useless because either way, the
200 (void) utils_set_fd_cloexec(new_sock
);
202 DBG("Receiving data from client for health...");
203 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
205 DBG("Nothing recv() from client... continuing");
206 ret
= close(new_sock
);
215 memset(&reply
, 0, sizeof(reply
));
216 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
218 * health_check_state returns 0 if health is
221 if (!health_check_state(health_sessiond
, i
)) {
222 reply
.ret_code
|= 1ULL << i
;
226 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
228 ret
= lttcomm_send_unix_sock(new_sock
, (void *) &reply
,
231 ERR("Failed to send health data back to client");
234 /* End of transmission */
235 ret
= close(new_sock
);
244 ERR("Health error occurred in %s", __func__
);
246 DBG("Health check thread dying");
247 unlink(config
.health_unix_sock_path
.value
);
255 lttng_poll_clean(&events
);
256 rcu_unregister_thread();
260 static bool shutdown_health_management_thread(void *data
)
262 struct thread_notifiers
*notifiers
= data
;
263 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
265 return notify_thread_pipe(write_fd
) == 1;
268 bool launch_health_management_thread(void)
270 struct thread_notifiers
*notifiers
;
271 struct lttng_thread
*thread
;
273 notifiers
= zmalloc(sizeof(*notifiers
));
278 sem_init(¬ifiers
->ready
, 0, 0);
279 notifiers
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
280 if (!notifiers
->quit_pipe
) {
283 thread
= lttng_thread_create("Health management",
284 thread_manage_health
,
285 shutdown_health_management_thread
,
286 cleanup_health_management_thread
,
292 wait_until_thread_is_ready(notifiers
);
293 lttng_thread_put(thread
);
296 cleanup_health_management_thread(notifiers
);