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-relayd.h"
54 #include "health-relayd.h"
56 /* Global health check unix path */
58 char health_unix_sock_path
[PATH_MAX
];
60 int health_quit_pipe
[2];
63 * Check if the thread quit pipe was triggered.
65 * Return 1 if it was triggered else 0;
68 int check_health_quit_pipe(int fd
, uint32_t events
)
70 if (fd
== health_quit_pipe
[0] && (events
& LPOLLIN
)) {
78 * Send data on a unix socket using the liblttsessiondcomm API.
80 * Return lttcomm error code.
82 static int send_unix_sock(int sock
, void *buf
, size_t len
)
84 /* Check valid length */
89 return lttcomm_send_unix_sock(sock
, buf
, len
);
92 static int create_lttng_rundir_with_perm(const char *rundir
)
96 DBG3("Creating LTTng run directory: %s", rundir
);
98 ret
= mkdir(rundir
, S_IRWXU
);
100 if (errno
!= EEXIST
) {
101 ERR("Unable to create %s", rundir
);
106 } else if (ret
== 0) {
107 int is_root
= !getuid();
110 ret
= chown(rundir
, 0,
111 utils_get_group_id(tracing_group_name
));
113 ERR("Unable to set group on %s", rundir
);
120 S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
122 ERR("Unable to set permissions on %s", health_unix_sock_path
);
135 int parse_health_env(void)
137 const char *health_path
;
139 health_path
= getenv(LTTNG_RELAYD_HEALTH_ENV
);
141 strncpy(health_unix_sock_path
, health_path
,
143 health_unix_sock_path
[PATH_MAX
- 1] = '\0';
150 int setup_health_path(void)
152 int is_root
, ret
= 0;
153 char *home_path
= NULL
, *rundir
= NULL
, *relayd_path
;
155 ret
= parse_health_env();
163 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
166 * Create rundir from home path. This will create something like
169 home_path
= utils_get_home_dir();
171 if (home_path
== NULL
) {
172 /* TODO: Add --socket PATH option */
173 ERR("Can't get HOME directory for sockets creation.");
178 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
185 ret
= asprintf(&relayd_path
, DEFAULT_RELAYD_PATH
, rundir
);
191 ret
= create_lttng_rundir_with_perm(rundir
);
196 ret
= create_lttng_rundir_with_perm(relayd_path
);
202 if (strlen(health_unix_sock_path
) != 0) {
205 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
206 DEFAULT_GLOBAL_RELAY_HEALTH_UNIX_SOCK
,
209 /* Set health check Unix path */
210 if (strlen(health_unix_sock_path
) != 0) {
214 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
215 DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK
,
216 home_path
, getpid());
225 * Thread managing health check socket.
227 void *thread_manage_health(void *data
)
229 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
230 uint32_t revents
, nb_fd
;
231 struct lttng_poll_event events
;
232 struct health_comm_msg msg
;
233 struct health_comm_reply reply
;
236 DBG("[thread] Manage health check started");
240 rcu_register_thread();
242 /* We might hit an error path before this is created. */
243 lttng_poll_init(&events
);
245 /* Create unix socket */
246 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
248 ERR("Unable to create health check Unix socket");
255 /* lttng health client socket path permissions */
256 ret
= chown(health_unix_sock_path
, 0,
257 utils_get_group_id(tracing_group_name
));
259 ERR("Unable to set group on %s", health_unix_sock_path
);
265 ret
= chmod(health_unix_sock_path
,
266 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
268 ERR("Unable to set permissions on %s", health_unix_sock_path
);
276 * Set the CLOEXEC flag. Return code is useless because either way, the
279 (void) utils_set_fd_cloexec(sock
);
281 ret
= lttcomm_listen_unix_sock(sock
);
286 /* Size is set to 1 for the consumer_channel pipe */
287 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
289 ERR("Poll set creation failed");
293 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
298 /* Add the application registration socket */
299 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
304 lttng_relay_notify_ready();
307 DBG("Health check ready");
309 /* Inifinite blocking call, waiting for transmission */
311 ret
= lttng_poll_wait(&events
, -1);
314 * Restart interrupted system call.
316 if (errno
== EINTR
) {
324 for (i
= 0; i
< nb_fd
; i
++) {
325 /* Fetch once the poll data */
326 revents
= LTTNG_POLL_GETEV(&events
, i
);
327 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
329 /* Thread quit pipe has been closed. Killing thread. */
330 ret
= check_health_quit_pipe(pollfd
, revents
);
336 /* Event on the registration socket */
337 if (pollfd
== sock
) {
338 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
339 ERR("Health socket poll error");
345 new_sock
= lttcomm_accept_unix_sock(sock
);
351 * Set the CLOEXEC flag. Return code is useless because either way, the
354 (void) utils_set_fd_cloexec(new_sock
);
356 DBG("Receiving data from client for health...");
357 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
359 DBG("Nothing recv() from client... continuing");
360 ret
= close(new_sock
);
370 assert(msg
.cmd
== HEALTH_CMD_CHECK
);
372 memset(&reply
, 0, sizeof(reply
));
373 for (i
= 0; i
< NR_HEALTH_RELAYD_TYPES
; i
++) {
375 * health_check_state return 0 if thread is in
378 if (!health_check_state(health_relayd
, i
)) {
379 reply
.ret_code
|= 1ULL << i
;
383 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
385 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
387 ERR("Failed to send health data back to client");
390 /* End of transmission */
391 ret
= close(new_sock
);
401 ERR("Health error occurred in %s", __func__
);
403 DBG("Health check thread dying");
404 unlink(health_unix_sock_path
);
413 * We do NOT rmdir rundir nor the relayd path because there are
414 * other processes using them.
417 lttng_poll_clean(&events
);
419 rcu_unregister_thread();