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>
39 #include <urcu/compiler.h>
43 #include <common/defaults.h>
44 #include <common/common.h>
45 #include <common/consumer/consumer.h>
46 #include <common/consumer/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
);
121 home_path
= utils_get_home_dir();
122 if (home_path
== NULL
) {
123 /* TODO: Add --socket PATH option */
124 ERR("Can't get HOME directory for sockets creation.");
129 /* Set health check Unix path */
130 if (strlen(health_unix_sock_path
) != 0) {
134 case LTTNG_CONSUMER_KERNEL
:
135 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
136 DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK
, home_path
);
138 case LTTNG_CONSUMER64_UST
:
139 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
140 DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK
, home_path
);
142 case LTTNG_CONSUMER32_UST
:
143 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
144 DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK
, home_path
);
156 * Thread managing health check socket.
158 void *thread_manage_health(void *data
)
160 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
161 uint32_t revents
, nb_fd
;
162 struct lttng_poll_event events
;
163 struct health_comm_msg msg
;
164 struct health_comm_reply reply
;
167 DBG("[thread] Manage health check started");
171 rcu_register_thread();
173 /* We might hit an error path before this is created. */
174 lttng_poll_init(&events
);
176 /* Create unix socket */
177 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
179 ERR("Unable to create health check Unix socket");
186 /* lttng health client socket path permissions */
187 ret
= chown(health_unix_sock_path
, 0,
188 utils_get_group_id(tracing_group_name
));
190 ERR("Unable to set group on %s", health_unix_sock_path
);
196 ret
= chmod(health_unix_sock_path
,
197 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
199 ERR("Unable to set permissions on %s", health_unix_sock_path
);
207 * Set the CLOEXEC flag. Return code is useless because either way, the
210 (void) utils_set_fd_cloexec(sock
);
212 ret
= lttcomm_listen_unix_sock(sock
);
217 /* Size is set to 1 for the consumer_channel pipe */
218 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
220 ERR("Poll set creation failed");
224 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
229 /* Add the application registration socket */
230 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
235 /* Perform prior memory accesses before decrementing ready */
236 cmm_smp_mb__before_uatomic_dec();
237 uatomic_dec(<tng_consumer_ready
);
240 DBG("Health check ready");
242 /* Inifinite blocking call, waiting for transmission */
244 ret
= lttng_poll_wait(&events
, -1);
247 * Restart interrupted system call.
249 if (errno
== EINTR
) {
257 for (i
= 0; i
< nb_fd
; i
++) {
258 /* Fetch once the poll data */
259 revents
= LTTNG_POLL_GETEV(&events
, i
);
260 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
263 /* No activity for this FD (poll implementation). */
267 /* Thread quit pipe has been closed. Killing thread. */
268 ret
= check_health_quit_pipe(pollfd
, revents
);
274 /* Event on the registration socket */
275 if (pollfd
== sock
) {
276 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
277 && !(revents
& LPOLLIN
)) {
278 ERR("Health socket poll error");
284 new_sock
= lttcomm_accept_unix_sock(sock
);
290 * Set the CLOEXEC flag. Return code is useless because either way, the
293 (void) utils_set_fd_cloexec(new_sock
);
295 DBG("Receiving data from client for health...");
296 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
298 DBG("Nothing recv() from client... continuing");
299 ret
= close(new_sock
);
309 assert(msg
.cmd
== HEALTH_CMD_CHECK
);
311 memset(&reply
, 0, sizeof(reply
));
312 for (i
= 0; i
< NR_HEALTH_CONSUMERD_TYPES
; i
++) {
314 * health_check_state return 0 if thread is in
317 if (!health_check_state(health_consumerd
, i
)) {
318 reply
.ret_code
|= 1ULL << i
;
322 DBG("Health check return value %" PRIx64
, reply
.ret_code
);
324 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
326 ERR("Failed to send health data back to client");
329 /* End of transmission */
330 ret
= close(new_sock
);
340 ERR("Health error occurred in %s", __func__
);
342 DBG("Health check thread dying");
343 unlink(health_unix_sock_path
);
351 lttng_poll_clean(&events
);
353 rcu_unregister_thread();