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