2 * Copyright (C) 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <urcu/list.h>
41 #include <urcu/compiler.h>
45 #include <common/defaults.h>
46 #include <common/common.h>
47 #include <common/consumer.h>
48 #include <common/consumer-timer.h>
49 #include <common/compat/poll.h>
50 #include <common/sessiond-comm/sessiond-comm.h>
51 #include <common/utils.h>
53 #include "lttng-consumerd.h"
54 #include "health-consumerd.h"
56 /* Global health check unix path */
57 static char health_unix_sock_path
[PATH_MAX
];
59 int health_quit_pipe
[2];
62 * Check if the thread quit pipe was triggered.
64 * Return 1 if it was triggered else 0;
67 int check_health_quit_pipe(int fd
, uint32_t events
)
69 if (fd
== health_quit_pipe
[0] && (events
& LPOLLIN
)) {
77 * Send data on a unix socket using the liblttsessiondcomm API.
79 * Return lttcomm error code.
81 static int send_unix_sock(int sock
, void *buf
, size_t len
)
83 /* Check valid length */
88 return lttcomm_send_unix_sock(sock
, buf
, len
);
92 int setup_health_path(void)
95 enum lttng_consumer_type type
;
96 const char *home_path
;
98 type
= lttng_consumer_get_type();
102 if (strlen(health_unix_sock_path
) != 0) {
106 case LTTNG_CONSUMER_KERNEL
:
107 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
108 DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK
);
110 case LTTNG_CONSUMER64_UST
:
111 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
112 DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK
);
114 case LTTNG_CONSUMER32_UST
:
115 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
116 DEFAULT_GLOBAL_USTCONSUMER32_HEALTH_UNIX_SOCK
);
123 home_path
= utils_get_home_dir();
124 if (home_path
== NULL
) {
125 /* TODO: Add --socket PATH option */
126 ERR("Can't get HOME directory for sockets creation.");
131 /* Set health check Unix path */
132 if (strlen(health_unix_sock_path
) != 0) {
136 case LTTNG_CONSUMER_KERNEL
:
137 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
138 DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK
, home_path
);
140 case LTTNG_CONSUMER64_UST
:
141 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
142 DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK
, home_path
);
144 case LTTNG_CONSUMER32_UST
:
145 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
146 DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK
, home_path
);
158 * Thread managing health check socket.
160 void *thread_manage_health(void *data
)
162 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
163 uint32_t revents
, nb_fd
;
164 struct lttng_poll_event events
;
165 struct health_comm_msg msg
;
166 struct health_comm_reply reply
;
169 DBG("[thread] Manage health check started");
173 rcu_register_thread();
175 /* We might hit an error path before this is created. */
176 lttng_poll_init(&events
);
178 /* Create unix socket */
179 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
181 ERR("Unable to create health check Unix socket");
188 /* lttng health client socket path permissions */
189 ret
= chown(health_unix_sock_path
, 0,
190 utils_get_group_id(tracing_group_name
));
192 ERR("Unable to set group on %s", health_unix_sock_path
);
198 ret
= chmod(health_unix_sock_path
,
199 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
201 ERR("Unable to set permissions on %s", health_unix_sock_path
);
209 * Set the CLOEXEC flag. Return code is useless because either way, the
212 (void) utils_set_fd_cloexec(sock
);
214 ret
= lttcomm_listen_unix_sock(sock
);
219 /* Size is set to 1 for the consumer_channel pipe */
220 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
222 ERR("Poll set creation failed");
226 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
231 /* Add the application registration socket */
232 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
237 /* Perform prior memory accesses before decrementing ready */
238 cmm_smp_mb__before_uatomic_dec();
239 uatomic_dec(<tng_consumer_ready
);
242 DBG("Health check ready");
244 /* Inifinite blocking call, waiting for transmission */
246 ret
= lttng_poll_wait(&events
, -1);
249 * Restart interrupted system call.
251 if (errno
== EINTR
) {
259 for (i
= 0; i
< nb_fd
; i
++) {
260 /* Fetch once the poll data */
261 revents
= LTTNG_POLL_GETEV(&events
, i
);
262 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
265 /* No activity for this FD (poll implementation). */
269 /* Thread quit pipe has been closed. Killing thread. */
270 ret
= check_health_quit_pipe(pollfd
, revents
);
276 /* Event on the registration socket */
277 if (pollfd
== sock
) {
278 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
279 ERR("Health socket poll error");
285 new_sock
= lttcomm_accept_unix_sock(sock
);
291 * Set the CLOEXEC flag. Return code is useless because either way, the
294 (void) utils_set_fd_cloexec(new_sock
);
296 DBG("Receiving data from client for health...");
297 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
299 DBG("Nothing recv() from client... continuing");
300 ret
= close(new_sock
);
310 assert(msg
.cmd
== HEALTH_CMD_CHECK
);
312 memset(&reply
, 0, sizeof(reply
));
313 for (i
= 0; i
< NR_HEALTH_CONSUMERD_TYPES
; i
++) {
315 * health_check_state return 0 if thread is in
318 if (!health_check_state(health_consumerd
, i
)) {
319 reply
.ret_code
|= 1ULL << i
;
323 DBG("Health check return value %" PRIx64
, reply
.ret_code
);
325 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
327 ERR("Failed to send health data back to client");
330 /* End of transmission */
331 ret
= close(new_sock
);
341 ERR("Health error occurred in %s", __func__
);
343 DBG("Health check thread dying");
344 unlink(health_unix_sock_path
);
352 lttng_poll_clean(&events
);
354 rcu_unregister_thread();