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>
42 #include <common/defaults.h>
43 #include <common/common.h>
44 #include <common/consumer/consumer.h>
45 #include <common/consumer/consumer-timer.h>
46 #include <common/compat/poll.h>
47 #include <common/sessiond-comm/sessiond-comm.h>
48 #include <common/utils.h>
49 #include <common/compat/getenv.h>
51 #include "lttng-relayd.h"
52 #include "health-relayd.h"
54 /* Global health check unix path */
56 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
);
90 static int create_lttng_rundir_with_perm(const char *rundir
)
94 DBG3("Creating LTTng run directory: %s", rundir
);
96 ret
= mkdir(rundir
, S_IRWXU
);
98 if (errno
!= EEXIST
) {
99 ERR("Unable to create %s", rundir
);
104 } else if (ret
== 0) {
105 int is_root
= !getuid();
110 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
112 /* Default to root group. */
116 ret
= chown(rundir
, 0, gid
);
118 ERR("Unable to set group on %s", rundir
);
125 S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
127 ERR("Unable to set permissions on %s", health_unix_sock_path
);
140 int parse_health_env(void)
142 const char *health_path
;
144 health_path
= lttng_secure_getenv(LTTNG_RELAYD_HEALTH_ENV
);
146 strncpy(health_unix_sock_path
, health_path
,
148 health_unix_sock_path
[PATH_MAX
- 1] = '\0';
155 int setup_health_path(void)
157 int is_root
, ret
= 0;
158 char *home_path
= NULL
, *rundir
= NULL
, *relayd_path
= NULL
;
160 ret
= parse_health_env();
168 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
175 * Create rundir from home path. This will create something like
178 home_path
= utils_get_home_dir();
180 if (home_path
== NULL
) {
181 /* TODO: Add --socket PATH option */
182 ERR("Can't get HOME directory for sockets creation.");
187 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
194 ret
= asprintf(&relayd_path
, DEFAULT_RELAYD_PATH
, rundir
);
200 ret
= create_lttng_rundir_with_perm(rundir
);
205 ret
= create_lttng_rundir_with_perm(relayd_path
);
211 if (strlen(health_unix_sock_path
) != 0) {
214 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
215 DEFAULT_GLOBAL_RELAY_HEALTH_UNIX_SOCK
,
218 /* Set health check Unix path */
219 if (strlen(health_unix_sock_path
) != 0) {
223 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
224 DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK
,
225 home_path
, (int) getpid());
235 * Thread managing health check socket.
237 void *thread_manage_health(void *data
)
239 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
240 uint32_t revents
, nb_fd
;
241 struct lttng_poll_event events
;
242 struct health_comm_msg msg
;
243 struct health_comm_reply reply
;
246 DBG("[thread] Manage health check started");
250 rcu_register_thread();
252 /* We might hit an error path before this is created. */
253 lttng_poll_init(&events
);
255 /* Create unix socket */
256 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
258 ERR("Unable to create health check Unix socket");
265 /* lttng health client socket path permissions */
268 ret
= utils_get_group_id(tracing_group_name
, true, &gid
);
270 /* Default to root group. */
274 ret
= chown(health_unix_sock_path
, 0, gid
);
276 ERR("Unable to set group on %s", health_unix_sock_path
);
282 ret
= chmod(health_unix_sock_path
,
283 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
285 ERR("Unable to set permissions on %s", health_unix_sock_path
);
293 * Set the CLOEXEC flag. Return code is useless because either way, the
296 (void) utils_set_fd_cloexec(sock
);
298 ret
= lttcomm_listen_unix_sock(sock
);
303 /* Size is set to 1 for the consumer_channel pipe */
304 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
306 ERR("Poll set creation failed");
310 ret
= lttng_poll_add(&events
, health_quit_pipe
[0], LPOLLIN
);
315 /* Add the application registration socket */
316 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
321 lttng_relay_notify_ready();
324 DBG("Health check ready");
326 /* Inifinite blocking call, waiting for transmission */
328 ret
= lttng_poll_wait(&events
, -1);
331 * Restart interrupted system call.
333 if (errno
== EINTR
) {
341 for (i
= 0; i
< nb_fd
; i
++) {
342 /* Fetch once the poll data */
343 revents
= LTTNG_POLL_GETEV(&events
, i
);
344 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
346 /* Thread quit pipe has been closed. Killing thread. */
347 ret
= check_health_quit_pipe(pollfd
, revents
);
353 /* Event on the registration socket */
354 if (pollfd
== sock
) {
355 if (revents
& LPOLLIN
) {
357 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
358 ERR("Health socket poll error");
361 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
367 new_sock
= lttcomm_accept_unix_sock(sock
);
373 * Set the CLOEXEC flag. Return code is useless because either way, the
376 (void) utils_set_fd_cloexec(new_sock
);
378 DBG("Receiving data from client for health...");
379 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
381 DBG("Nothing recv() from client... continuing");
382 ret
= close(new_sock
);
392 assert(msg
.cmd
== HEALTH_CMD_CHECK
);
394 memset(&reply
, 0, sizeof(reply
));
395 for (i
= 0; i
< NR_HEALTH_RELAYD_TYPES
; i
++) {
397 * health_check_state return 0 if thread is in
400 if (!health_check_state(health_relayd
, i
)) {
401 reply
.ret_code
|= 1ULL << i
;
405 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
407 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
409 ERR("Failed to send health data back to client");
412 /* End of transmission */
413 ret
= close(new_sock
);
421 lttng_relay_stop_threads();
424 ERR("Health error occurred in %s", __func__
);
426 DBG("Health check thread dying");
427 unlink(health_unix_sock_path
);
436 * We do NOT rmdir rundir nor the relayd path because there are
437 * other processes using them.
440 lttng_poll_clean(&events
);
442 rcu_unregister_thread();