Fix: update liburcu URL
[lttng-tools.git] / src / bin / lttng-relayd / health-relayd.c
CommitLineData
65931c8b
MD
1/*
2 * Copyright (C) 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18#define _GNU_SOURCE
6c1c0768 19#define _LGPL_SOURCE
65931c8b
MD
20#include <fcntl.h>
21#include <getopt.h>
22#include <grp.h>
23#include <limits.h>
24#include <pthread.h>
25#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/ipc.h>
30#include <sys/resource.h>
31#include <sys/shm.h>
32#include <sys/socket.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <urcu/list.h>
36#include <poll.h>
37#include <unistd.h>
38#include <sys/mman.h>
39#include <assert.h>
40#include <config.h>
41#include <urcu/compiler.h>
42#include <ulimit.h>
43#include <inttypes.h>
44
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>
e8fa9fb0 52#include <common/compat/getenv.h>
65931c8b
MD
53
54#include "lttng-relayd.h"
55#include "health-relayd.h"
56
57/* Global health check unix path */
094fe907
MD
58static
59char health_unix_sock_path[PATH_MAX];
65931c8b
MD
60
61int health_quit_pipe[2];
62
63/*
64 * Check if the thread quit pipe was triggered.
65 *
66 * Return 1 if it was triggered else 0;
67 */
68static
69int check_health_quit_pipe(int fd, uint32_t events)
70{
71 if (fd == health_quit_pipe[0] && (events & LPOLLIN)) {
72 return 1;
73 }
74
75 return 0;
76}
77
78/*
79 * Send data on a unix socket using the liblttsessiondcomm API.
80 *
81 * Return lttcomm error code.
82 */
83static int send_unix_sock(int sock, void *buf, size_t len)
84{
85 /* Check valid length */
86 if (len == 0) {
87 return -1;
88 }
89
90 return lttcomm_send_unix_sock(sock, buf, len);
91}
92
93static int create_lttng_rundir_with_perm(const char *rundir)
94{
95 int ret;
96
97 DBG3("Creating LTTng run directory: %s", rundir);
98
99 ret = mkdir(rundir, S_IRWXU);
100 if (ret < 0) {
101 if (errno != EEXIST) {
102 ERR("Unable to create %s", rundir);
103 goto error;
104 } else {
105 ret = 0;
106 }
107 } else if (ret == 0) {
108 int is_root = !getuid();
109
110 if (is_root) {
111 ret = chown(rundir, 0,
112 utils_get_group_id(tracing_group_name));
113 if (ret < 0) {
114 ERR("Unable to set group on %s", rundir);
115 PERROR("chown");
116 ret = -1;
117 goto error;
118 }
119
120 ret = chmod(rundir,
121 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
122 if (ret < 0) {
123 ERR("Unable to set permissions on %s", health_unix_sock_path);
124 PERROR("chmod");
125 ret = -1;
126 goto error;
127 }
128 }
129 }
130
131error:
132 return ret;
133}
134
094fe907
MD
135static
136int parse_health_env(void)
137{
138 const char *health_path;
139
e8fa9fb0 140 health_path = lttng_secure_getenv(LTTNG_RELAYD_HEALTH_ENV);
094fe907
MD
141 if (health_path) {
142 strncpy(health_unix_sock_path, health_path,
143 PATH_MAX);
144 health_unix_sock_path[PATH_MAX - 1] = '\0';
145 }
146
147 return 0;
148}
149
65931c8b
MD
150static
151int setup_health_path(void)
152{
153 int is_root, ret = 0;
c3844e39 154 char *home_path = NULL, *rundir = NULL, *relayd_path;
65931c8b 155
094fe907
MD
156 ret = parse_health_env();
157 if (ret) {
158 return ret;
159 }
160
65931c8b
MD
161 is_root = !getuid();
162
163 if (is_root) {
c3844e39 164 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
b6ab01aa
MD
165 if (!rundir) {
166 ret = -ENOMEM;
167 goto end;
168 }
65931c8b
MD
169 } else {
170 /*
171 * Create rundir from home path. This will create something like
172 * $HOME/.lttng
173 */
174 home_path = utils_get_home_dir();
175
176 if (home_path == NULL) {
177 /* TODO: Add --socket PATH option */
178 ERR("Can't get HOME directory for sockets creation.");
179 ret = -EPERM;
180 goto end;
181 }
182
c3844e39 183 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
65931c8b
MD
184 if (ret < 0) {
185 ret = -ENOMEM;
186 goto end;
187 }
188 }
189
c3844e39 190 ret = asprintf(&relayd_path, DEFAULT_RELAYD_PATH, rundir);
65931c8b
MD
191 if (ret < 0) {
192 ret = -ENOMEM;
193 goto end;
194 }
195
c3844e39 196 ret = create_lttng_rundir_with_perm(rundir);
65931c8b
MD
197 if (ret < 0) {
198 goto end;
199 }
200
201 ret = create_lttng_rundir_with_perm(relayd_path);
202 if (ret < 0) {
203 goto end;
204 }
205
206 if (is_root) {
207 if (strlen(health_unix_sock_path) != 0) {
208 goto end;
209 }
210 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
211 DEFAULT_GLOBAL_RELAY_HEALTH_UNIX_SOCK,
212 getpid());
213 } else {
214 /* Set health check Unix path */
215 if (strlen(health_unix_sock_path) != 0) {
216 goto end;
217 }
218
219 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
220 DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK,
221 home_path, getpid());
222 }
223
224end:
c3844e39 225 free(rundir);
65931c8b
MD
226 return ret;
227}
228
229/*
230 * Thread managing health check socket.
231 */
232void *thread_manage_health(void *data)
233{
234 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
235 uint32_t revents, nb_fd;
236 struct lttng_poll_event events;
237 struct health_comm_msg msg;
238 struct health_comm_reply reply;
239 int is_root;
240
241 DBG("[thread] Manage health check started");
242
243 setup_health_path();
244
245 rcu_register_thread();
246
247 /* We might hit an error path before this is created. */
248 lttng_poll_init(&events);
249
250 /* Create unix socket */
251 sock = lttcomm_create_unix_sock(health_unix_sock_path);
252 if (sock < 0) {
253 ERR("Unable to create health check Unix socket");
254 ret = -1;
255 goto error;
256 }
257
258 is_root = !getuid();
259 if (is_root) {
260 /* lttng health client socket path permissions */
261 ret = chown(health_unix_sock_path, 0,
262 utils_get_group_id(tracing_group_name));
263 if (ret < 0) {
264 ERR("Unable to set group on %s", health_unix_sock_path);
265 PERROR("chown");
266 ret = -1;
267 goto error;
268 }
269
270 ret = chmod(health_unix_sock_path,
271 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
272 if (ret < 0) {
273 ERR("Unable to set permissions on %s", health_unix_sock_path);
274 PERROR("chmod");
275 ret = -1;
276 goto error;
277 }
278 }
279
280 /*
281 * Set the CLOEXEC flag. Return code is useless because either way, the
282 * show must go on.
283 */
284 (void) utils_set_fd_cloexec(sock);
285
286 ret = lttcomm_listen_unix_sock(sock);
287 if (ret < 0) {
288 goto error;
289 }
290
291 /* Size is set to 1 for the consumer_channel pipe */
292 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
293 if (ret < 0) {
294 ERR("Poll set creation failed");
295 goto error;
296 }
297
298 ret = lttng_poll_add(&events, health_quit_pipe[0], LPOLLIN);
299 if (ret < 0) {
300 goto error;
301 }
302
303 /* Add the application registration socket */
304 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
305 if (ret < 0) {
306 goto error;
307 }
308
3fd27398
MD
309 lttng_relay_notify_ready();
310
65931c8b
MD
311 while (1) {
312 DBG("Health check ready");
313
314 /* Inifinite blocking call, waiting for transmission */
315restart:
316 ret = lttng_poll_wait(&events, -1);
317 if (ret < 0) {
318 /*
319 * Restart interrupted system call.
320 */
321 if (errno == EINTR) {
322 goto restart;
323 }
324 goto error;
325 }
326
327 nb_fd = ret;
328
329 for (i = 0; i < nb_fd; i++) {
330 /* Fetch once the poll data */
331 revents = LTTNG_POLL_GETEV(&events, i);
332 pollfd = LTTNG_POLL_GETFD(&events, i);
333
fd20dac9
MD
334 if (!revents) {
335 /* No activity for this FD (poll implementation). */
336 continue;
337 }
338
65931c8b
MD
339 /* Thread quit pipe has been closed. Killing thread. */
340 ret = check_health_quit_pipe(pollfd, revents);
341 if (ret) {
342 err = 0;
343 goto exit;
344 }
345
346 /* Event on the registration socket */
347 if (pollfd == sock) {
348 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
349 ERR("Health socket poll error");
350 goto error;
351 }
352 }
353 }
354
355 new_sock = lttcomm_accept_unix_sock(sock);
356 if (new_sock < 0) {
357 goto error;
358 }
359
360 /*
361 * Set the CLOEXEC flag. Return code is useless because either way, the
362 * show must go on.
363 */
364 (void) utils_set_fd_cloexec(new_sock);
365
366 DBG("Receiving data from client for health...");
367 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
368 if (ret <= 0) {
369 DBG("Nothing recv() from client... continuing");
370 ret = close(new_sock);
371 if (ret) {
372 PERROR("close");
373 }
374 new_sock = -1;
375 continue;
376 }
377
378 rcu_thread_online();
379
380 assert(msg.cmd == HEALTH_CMD_CHECK);
381
53efb85a 382 memset(&reply, 0, sizeof(reply));
65931c8b
MD
383 for (i = 0; i < NR_HEALTH_RELAYD_TYPES; i++) {
384 /*
385 * health_check_state return 0 if thread is in
386 * error.
387 */
388 if (!health_check_state(health_relayd, i)) {
389 reply.ret_code |= 1ULL << i;
390 }
391 }
392
393 DBG2("Health check return value %" PRIx64, reply.ret_code);
394
395 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
396 if (ret < 0) {
397 ERR("Failed to send health data back to client");
398 }
399
400 /* End of transmission */
401 ret = close(new_sock);
402 if (ret) {
403 PERROR("close");
404 }
405 new_sock = -1;
406 }
407
408exit:
409error:
410 if (err) {
411 ERR("Health error occurred in %s", __func__);
412 }
413 DBG("Health check thread dying");
414 unlink(health_unix_sock_path);
415 if (sock >= 0) {
416 ret = close(sock);
417 if (ret) {
418 PERROR("close");
419 }
420 }
421
dcbcae3e
MD
422 /*
423 * We do NOT rmdir rundir nor the relayd path because there are
424 * other processes using them.
425 */
426
65931c8b
MD
427 lttng_poll_clean(&events);
428
429 rcu_unregister_thread();
430 return NULL;
431}
This page took 0.042608 seconds and 4 git commands to generate.