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