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>
44 #include <common/defaults.h>
45 #include <common/common.h>
46 #include <common/consumer.h>
47 #include <common/consumer-timer.h>
48 #include <common/compat/poll.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
50 #include <common/utils.h>
52 #include "lttng-consumerd.h"
53 #include "health-consumerd.h"
55 /* Global health check unix path */
56 static char health_unix_sock_path
[PATH_MAX
];
58 int health_quit_pipe
[2];
61 * Check if the thread quit pipe was triggered.
63 * Return 1 if it was triggered else 0;
66 int check_health_quit_pipe(int fd
, uint32_t events
)
68 if (fd
== health_quit_pipe
[0] && (events
& LPOLLIN
)) {
76 * Send data on a unix socket using the liblttsessiondcomm API.
78 * Return lttcomm error code.
80 static int send_unix_sock(int sock
, void *buf
, size_t len
)
82 /* Check valid length */
87 return lttcomm_send_unix_sock(sock
, buf
, len
);
91 int setup_health_path(void)
94 enum lttng_consumer_type type
;
95 const char *home_path
;
97 type
= lttng_consumer_get_type();
101 if (strlen(health_unix_sock_path
) != 0) {
105 case LTTNG_CONSUMER_KERNEL
:
106 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
107 DEFAULT_GLOBAL_KCONSUMER_HEALTH_UNIX_SOCK
);
109 case LTTNG_CONSUMER64_UST
:
110 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
111 DEFAULT_GLOBAL_USTCONSUMER64_HEALTH_UNIX_SOCK
);
113 case LTTNG_CONSUMER32_UST
:
114 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
115 DEFAULT_GLOBAL_USTCONSUMER32_HEALTH_UNIX_SOCK
);
122 home_path
= utils_get_home_dir();
123 if (home_path
== NULL
) {
124 /* TODO: Add --socket PATH option */
125 ERR("Can't get HOME directory for sockets creation.");
130 /* Set health check Unix path */
131 if (strlen(health_unix_sock_path
) != 0) {
135 case LTTNG_CONSUMER_KERNEL
:
136 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
137 DEFAULT_HOME_KCONSUMER_HEALTH_UNIX_SOCK
, home_path
);
139 case LTTNG_CONSUMER64_UST
:
140 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
141 DEFAULT_HOME_USTCONSUMER64_HEALTH_UNIX_SOCK
, home_path
);
143 case LTTNG_CONSUMER32_UST
:
144 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
145 DEFAULT_HOME_USTCONSUMER32_HEALTH_UNIX_SOCK
, home_path
);
157 * Thread managing health check socket.
159 void *thread_manage_health(void *data
)
161 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
162 uint32_t revents
, nb_fd
;
163 struct lttng_poll_event events
;
164 struct health_comm_msg msg
;
165 struct health_comm_reply reply
;
168 DBG("[thread] Manage health check started");
172 rcu_register_thread();
174 /* We might hit an error path before this is created. */
175 lttng_poll_init(&events
);
177 /* Create unix socket */
178 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
180 ERR("Unable to create health check Unix socket");
187 /* lttng health client socket path permissions */
188 ret
= chown(health_unix_sock_path
, 0,
189 utils_get_group_id(tracing_group_name
));
191 ERR("Unable to set group on %s", health_unix_sock_path
);
197 ret
= chmod(health_unix_sock_path
,
198 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
200 ERR("Unable to set permissions on %s", health_unix_sock_path
);
208 * Set the CLOEXEC flag. Return code is useless because either way, the
211 (void) utils_set_fd_cloexec(sock
);
213 ret
= lttcomm_listen_unix_sock(sock
);
218 /* Size is set to 1 for the consumer_channel pipe */
219 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
221 ERR("Poll set creation failed");
225 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
230 /* Add the application registration socket */
231 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
236 /* Perform prior memory accesses before decrementing ready */
237 cmm_smp_mb__before_uatomic_dec();
238 uatomic_dec(<tng_consumer_ready
);
241 DBG("Health check ready");
243 /* Inifinite blocking call, waiting for transmission */
245 ret
= lttng_poll_wait(&events
, -1);
248 * Restart interrupted system call.
250 if (errno
== EINTR
) {
258 for (i
= 0; i
< nb_fd
; i
++) {
259 /* Fetch once the poll data */
260 revents
= LTTNG_POLL_GETEV(&events
, i
);
261 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
263 /* Thread quit pipe has been closed. Killing thread. */
264 ret
= check_health_quit_pipe(pollfd
, revents
);
270 /* Event on the registration socket */
271 if (pollfd
== sock
) {
272 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
273 ERR("Health socket poll error");
279 new_sock
= lttcomm_accept_unix_sock(sock
);
285 * Set the CLOEXEC flag. Return code is useless because either way, the
288 (void) utils_set_fd_cloexec(new_sock
);
290 DBG("Receiving data from client for health...");
291 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
293 DBG("Nothing recv() from client... continuing");
294 ret
= close(new_sock
);
304 assert(msg
.cmd
== HEALTH_CMD_CHECK
);
306 memset(&reply
, 0, sizeof(reply
));
307 for (i
= 0; i
< NR_HEALTH_CONSUMERD_TYPES
; i
++) {
309 * health_check_state return 0 if thread is in
312 if (!health_check_state(health_consumerd
, i
)) {
313 reply
.ret_code
|= 1ULL << i
;
317 DBG("Health check return value %" PRIx64
, reply
.ret_code
);
319 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
321 ERR("Failed to send health data back to client");
324 /* End of transmission */
325 ret
= close(new_sock
);
335 ERR("Health error occurred in %s", __func__
);
337 DBG("Health check thread dying");
338 unlink(health_unix_sock_path
);
346 lttng_poll_clean(&events
);
348 rcu_unregister_thread();