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.
29 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
34 #include <urcu/list.h>
40 #include <urcu/compiler.h>
43 #include <common/defaults.h>
44 #include <common/common.h>
45 #include <common/consumer.h>
46 #include <common/consumer-timer.h>
47 #include <common/compat/poll.h>
48 #include <common/sessiond-comm/sessiond-comm.h>
49 #include <common/utils.h>
51 #include "lttng-consumerd.h"
52 #include "health-consumerd.h"
54 /* Global health check unix path */
55 static char health_unix_sock_path
[PATH_MAX
];
57 int health_quit_pipe
[2];
60 * Check if the thread quit pipe was triggered.
62 * Return 1 if it was triggered else 0;
65 int check_health_quit_pipe(int fd
, uint32_t events
)
67 if (fd
== health_quit_pipe
[0] && (events
& LPOLLIN
)) {
75 * Send data on a unix socket using the liblttsessiondcomm API.
77 * Return lttcomm error code.
79 static int send_unix_sock(int sock
, void *buf
, size_t len
)
81 /* Check valid length */
86 return lttcomm_send_unix_sock(sock
, buf
, len
);
90 int setup_health_path(void)
93 enum lttng_consumer_type type
;
94 const char *home_path
;
96 type
= lttng_consumer_get_type();
100 if (strlen(health_unix_sock_path
) != 0) {
104 case LTTNG_CONSUMER_KERNEL
:
105 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
106 DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK
);
108 case LTTNG_CONSUMER64_UST
:
109 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
110 DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK
);
112 case LTTNG_CONSUMER32_UST
:
113 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
114 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.");
132 * Create rundir from home path. This will create something like
135 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
141 /* Set health check Unix path */
142 if (strlen(health_unix_sock_path
) != 0) {
146 case LTTNG_CONSUMER_KERNEL
:
147 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
148 DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK
, rundir
);
150 case LTTNG_CONSUMER64_UST
:
151 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
152 DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK
, rundir
);
154 case LTTNG_CONSUMER32_UST
:
155 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
156 DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK
, rundir
);
169 * Thread managing health check socket.
171 void *thread_manage_health(void *data
)
173 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
174 uint32_t revents
, nb_fd
;
175 struct lttng_poll_event events
;
176 struct health_comm_msg msg
;
177 struct health_comm_reply reply
;
179 DBG("[thread] Manage health check started");
183 rcu_register_thread();
185 /* We might hit an error path before this is created. */
186 lttng_poll_init(&events
);
188 /* Create unix socket */
189 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
191 ERR("Unable to create health check Unix socket");
197 * Set the CLOEXEC flag. Return code is useless because either way, the
200 (void) utils_set_fd_cloexec(sock
);
202 ret
= lttcomm_listen_unix_sock(sock
);
207 /* Size is set to 1 for the consumer_channel pipe */
208 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
210 ERR("Poll set creation failed");
214 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
219 /* Add the application registration socket */
220 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
226 DBG("Health check ready");
228 /* Inifinite blocking call, waiting for transmission */
230 ret
= lttng_poll_wait(&events
, -1);
233 * Restart interrupted system call.
235 if (errno
== EINTR
) {
243 for (i
= 0; i
< nb_fd
; i
++) {
244 /* Fetch once the poll data */
245 revents
= LTTNG_POLL_GETEV(&events
, i
);
246 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
248 /* Thread quit pipe has been closed. Killing thread. */
249 ret
= check_health_quit_pipe(pollfd
, revents
);
255 /* Event on the registration socket */
256 if (pollfd
== sock
) {
257 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
258 ERR("Health socket poll error");
264 new_sock
= lttcomm_accept_unix_sock(sock
);
270 * Set the CLOEXEC flag. Return code is useless because either way, the
273 (void) utils_set_fd_cloexec(new_sock
);
275 DBG("Receiving data from client for health...");
276 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
278 DBG("Nothing recv() from client... continuing");
279 ret
= close(new_sock
);
289 assert(msg
.cmd
== HEALTH_CMD_CHECK
);
291 switch (msg
.component
) {
292 case LTTNG_HEALTH_CONSUMERD_CHANNEL
:
293 reply
.ret_code
= health_check_state(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
295 case LTTNG_HEALTH_CONSUMERD_METADATA
:
296 reply
.ret_code
= health_check_state(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
298 case LTTNG_HEALTH_CONSUMERD_DATA
:
299 reply
.ret_code
= health_check_state(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
301 case LTTNG_HEALTH_CONSUMERD_SESSIOND
:
302 reply
.ret_code
= health_check_state(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
304 case LTTNG_HEALTH_CONSUMERD_METADATA_TIMER
:
305 reply
.ret_code
= health_check_state(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
308 case LTTNG_HEALTH_CONSUMERD_ALL
:
310 health_check_state(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
) &&
311 health_check_state(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
) &&
312 health_check_state(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
) &&
313 health_check_state(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
) &&
314 health_check_state(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA_TIMER
);
317 reply
.ret_code
= LTTNG_ERR_UND
;
322 * Flip ret value since 0 is a success and 1 indicates a bad health for
323 * the client where in the sessiond it is the opposite. Again, this is
324 * just to make things easier for us poor developer which enjoy a lot
327 if (reply
.ret_code
== 0 || reply
.ret_code
== 1) {
328 reply
.ret_code
= !reply
.ret_code
;
331 DBG2("Health check return value %d", reply
.ret_code
);
333 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
335 ERR("Failed to send health data back to client");
338 /* End of transmission */
339 ret
= close(new_sock
);
349 ERR("Health error occurred in %s", __func__
);
351 DBG("Health check thread dying");
352 unlink(health_unix_sock_path
);
360 lttng_poll_clean(&events
);
362 rcu_unregister_thread();