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 */
98 ret
= chown(config
.health_unix_sock_path
.value
, 0,
99 utils_get_group_id(config
.tracing_group_name
.value
));
101 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
106 ret
= chmod(config
.health_unix_sock_path
.value
,
107 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
109 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
116 * Set the CLOEXEC flag. Return code is useless because either way, the
119 (void) utils_set_fd_cloexec(sock
);
121 ret
= lttcomm_listen_unix_sock(sock
);
126 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
, LPOLLIN
| LPOLLERR
);
131 /* Add the application registration socket */
132 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
137 mark_thread_as_ready(notifiers
);
139 DBG("Health check ready");
141 /* Infinite blocking call, waiting for transmission */
143 ret
= lttng_poll_wait(&events
, -1);
146 * Restart interrupted system call.
148 if (errno
== EINTR
) {
156 for (i
= 0; i
< nb_fd
; i
++) {
157 /* Fetch once the poll data */
158 revents
= LTTNG_POLL_GETEV(&events
, i
);
159 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
162 /* No activity for this FD (poll implementation). */
166 /* Event on the registration socket */
167 if (pollfd
== sock
) {
168 if (revents
& LPOLLIN
) {
170 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
171 ERR("Health socket poll error");
174 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
178 /* Event on the thread's quit pipe. */
184 new_sock
= lttcomm_accept_unix_sock(sock
);
190 * Set the CLOEXEC flag. Return code is useless because either way, the
193 (void) utils_set_fd_cloexec(new_sock
);
195 DBG("Receiving data from client for health...");
196 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
198 DBG("Nothing recv() from client... continuing");
199 ret
= close(new_sock
);
208 memset(&reply
, 0, sizeof(reply
));
209 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
211 * health_check_state returns 0 if health is
214 if (!health_check_state(health_sessiond
, i
)) {
215 reply
.ret_code
|= 1ULL << i
;
219 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
221 ret
= lttcomm_send_unix_sock(new_sock
, (void *) &reply
,
224 ERR("Failed to send health data back to client");
227 /* End of transmission */
228 ret
= close(new_sock
);
237 ERR("Health error occurred in %s", __func__
);
239 DBG("Health check thread dying");
240 unlink(config
.health_unix_sock_path
.value
);
248 lttng_poll_clean(&events
);
249 rcu_unregister_thread();
253 static bool shutdown_health_management_thread(void *data
)
255 struct thread_notifiers
*notifiers
= data
;
256 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
258 return notify_thread_pipe(write_fd
) == 1;
261 bool launch_health_management_thread(void)
263 struct thread_notifiers
*notifiers
;
264 struct lttng_thread
*thread
;
266 notifiers
= zmalloc(sizeof(*notifiers
));
271 sem_init(¬ifiers
->ready
, 0, 0);
272 notifiers
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
273 if (!notifiers
->quit_pipe
) {
276 thread
= lttng_thread_create("Health management",
277 thread_manage_health
,
278 shutdown_health_management_thread
,
279 cleanup_health_management_thread
,
285 wait_until_thread_is_ready(notifiers
);
286 lttng_thread_put(thread
);
289 cleanup_health_management_thread(notifiers
);