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 */
189 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
191 /* Default to root group. */
195 ret
= chown(health_unix_sock_path
, 0, gid
);
197 ERR("Unable to set group on %s", health_unix_sock_path
);
203 ret
= chmod(health_unix_sock_path
,
204 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
206 ERR("Unable to set permissions on %s", health_unix_sock_path
);
214 * Set the CLOEXEC flag. Return code is useless because either way, the
217 (void) utils_set_fd_cloexec(sock
);
219 ret
= lttcomm_listen_unix_sock(sock
);
224 /* Size is set to 1 for the consumer_channel pipe */
225 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
227 ERR("Poll set creation failed");
231 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
236 /* Add the application registration socket */
237 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
242 /* Perform prior memory accesses before decrementing ready */
243 cmm_smp_mb__before_uatomic_dec();
244 uatomic_dec(<tng_consumer_ready
);
247 DBG("Health check ready");
249 /* Inifinite blocking call, waiting for transmission */
251 ret
= lttng_poll_wait(&events
, -1);
254 * Restart interrupted system call.
256 if (errno
== EINTR
) {
264 for (i
= 0; i
< nb_fd
; i
++) {
265 /* Fetch once the poll data */
266 revents
= LTTNG_POLL_GETEV(&events
, i
);
267 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
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 && !(revents
& LPOLLIN
)) {
280 ERR("Health socket poll error");
286 new_sock
= lttcomm_accept_unix_sock(sock
);
292 * Set the CLOEXEC flag. Return code is useless because either way, the
295 (void) utils_set_fd_cloexec(new_sock
);
297 DBG("Receiving data from client for health...");
298 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
300 DBG("Nothing recv() from client... continuing");
301 ret
= close(new_sock
);
311 assert(msg
.cmd
== HEALTH_CMD_CHECK
);
313 memset(&reply
, 0, sizeof(reply
));
314 for (i
= 0; i
< NR_HEALTH_CONSUMERD_TYPES
; i
++) {
316 * health_check_state return 0 if thread is in
319 if (!health_check_state(health_consumerd
, i
)) {
320 reply
.ret_code
|= 1ULL << i
;
324 DBG("Health check return value %" PRIx64
, reply
.ret_code
);
326 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
328 ERR("Failed to send health data back to client");
331 /* End of transmission */
332 ret
= close(new_sock
);
342 ERR("Health error occurred in %s", __func__
);
344 DBG("Health check thread dying");
345 unlink(health_unix_sock_path
);
353 lttng_poll_clean(&events
);
355 rcu_unregister_thread();