Fix: remove relayd rundir on cleanup
[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 #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 */
56 static char health_unix_sock_path[PATH_MAX];
57 static char *relayd_path;
58 static char *lttng_rundir;
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 setup_health_path(void)
136 {
137 int is_root, ret = 0;
138 char *home_path = NULL;
139
140 is_root = !getuid();
141
142 if (is_root) {
143 lttng_rundir = strdup(DEFAULT_LTTNG_RUNDIR);
144 } else {
145 /*
146 * Create rundir from home path. This will create something like
147 * $HOME/.lttng
148 */
149 home_path = utils_get_home_dir();
150
151 if (home_path == NULL) {
152 /* TODO: Add --socket PATH option */
153 ERR("Can't get HOME directory for sockets creation.");
154 ret = -EPERM;
155 goto end;
156 }
157
158 ret = asprintf(&lttng_rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
159 if (ret < 0) {
160 ret = -ENOMEM;
161 goto end;
162 }
163 }
164
165 ret = asprintf(&relayd_path, DEFAULT_RELAYD_PATH, lttng_rundir);
166 if (ret < 0) {
167 ret = -ENOMEM;
168 goto end;
169 }
170
171 ret = create_lttng_rundir_with_perm(lttng_rundir);
172 if (ret < 0) {
173 goto end;
174 }
175
176 ret = create_lttng_rundir_with_perm(relayd_path);
177 if (ret < 0) {
178 goto end;
179 }
180
181 if (is_root) {
182 if (strlen(health_unix_sock_path) != 0) {
183 goto end;
184 }
185 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
186 DEFAULT_GLOBAL_RELAY_HEALTH_UNIX_SOCK,
187 getpid());
188 } else {
189 /* Set health check Unix path */
190 if (strlen(health_unix_sock_path) != 0) {
191 goto end;
192 }
193
194 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
195 DEFAULT_HOME_RELAY_HEALTH_UNIX_SOCK,
196 home_path, getpid());
197 }
198
199 end:
200 return ret;
201 }
202
203 /*
204 * Thread managing health check socket.
205 */
206 void *thread_manage_health(void *data)
207 {
208 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
209 uint32_t revents, nb_fd;
210 struct lttng_poll_event events;
211 struct health_comm_msg msg;
212 struct health_comm_reply reply;
213 int is_root;
214
215 DBG("[thread] Manage health check started");
216
217 setup_health_path();
218
219 rcu_register_thread();
220
221 /* We might hit an error path before this is created. */
222 lttng_poll_init(&events);
223
224 /* Create unix socket */
225 sock = lttcomm_create_unix_sock(health_unix_sock_path);
226 if (sock < 0) {
227 ERR("Unable to create health check Unix socket");
228 ret = -1;
229 goto error;
230 }
231
232 is_root = !getuid();
233 if (is_root) {
234 /* lttng health client socket path permissions */
235 ret = chown(health_unix_sock_path, 0,
236 utils_get_group_id(tracing_group_name));
237 if (ret < 0) {
238 ERR("Unable to set group on %s", health_unix_sock_path);
239 PERROR("chown");
240 ret = -1;
241 goto error;
242 }
243
244 ret = chmod(health_unix_sock_path,
245 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
246 if (ret < 0) {
247 ERR("Unable to set permissions on %s", health_unix_sock_path);
248 PERROR("chmod");
249 ret = -1;
250 goto error;
251 }
252 }
253
254 /*
255 * Set the CLOEXEC flag. Return code is useless because either way, the
256 * show must go on.
257 */
258 (void) utils_set_fd_cloexec(sock);
259
260 ret = lttcomm_listen_unix_sock(sock);
261 if (ret < 0) {
262 goto error;
263 }
264
265 /* Size is set to 1 for the consumer_channel pipe */
266 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
267 if (ret < 0) {
268 ERR("Poll set creation failed");
269 goto error;
270 }
271
272 ret = lttng_poll_add(&events, health_quit_pipe[0], LPOLLIN);
273 if (ret < 0) {
274 goto error;
275 }
276
277 /* Add the application registration socket */
278 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
279 if (ret < 0) {
280 goto error;
281 }
282
283 while (1) {
284 DBG("Health check ready");
285
286 /* Inifinite blocking call, waiting for transmission */
287 restart:
288 ret = lttng_poll_wait(&events, -1);
289 if (ret < 0) {
290 /*
291 * Restart interrupted system call.
292 */
293 if (errno == EINTR) {
294 goto restart;
295 }
296 goto error;
297 }
298
299 nb_fd = ret;
300
301 for (i = 0; i < nb_fd; i++) {
302 /* Fetch once the poll data */
303 revents = LTTNG_POLL_GETEV(&events, i);
304 pollfd = LTTNG_POLL_GETFD(&events, i);
305
306 /* Thread quit pipe has been closed. Killing thread. */
307 ret = check_health_quit_pipe(pollfd, revents);
308 if (ret) {
309 err = 0;
310 goto exit;
311 }
312
313 /* Event on the registration socket */
314 if (pollfd == sock) {
315 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
316 ERR("Health socket poll error");
317 goto error;
318 }
319 }
320 }
321
322 new_sock = lttcomm_accept_unix_sock(sock);
323 if (new_sock < 0) {
324 goto error;
325 }
326
327 /*
328 * Set the CLOEXEC flag. Return code is useless because either way, the
329 * show must go on.
330 */
331 (void) utils_set_fd_cloexec(new_sock);
332
333 DBG("Receiving data from client for health...");
334 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
335 if (ret <= 0) {
336 DBG("Nothing recv() from client... continuing");
337 ret = close(new_sock);
338 if (ret) {
339 PERROR("close");
340 }
341 new_sock = -1;
342 continue;
343 }
344
345 rcu_thread_online();
346
347 assert(msg.cmd == HEALTH_CMD_CHECK);
348
349 reply.ret_code = 0;
350 for (i = 0; i < NR_HEALTH_RELAYD_TYPES; i++) {
351 /*
352 * health_check_state return 0 if thread is in
353 * error.
354 */
355 if (!health_check_state(health_relayd, i)) {
356 reply.ret_code |= 1ULL << i;
357 }
358 }
359
360 DBG2("Health check return value %" PRIx64, reply.ret_code);
361
362 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
363 if (ret < 0) {
364 ERR("Failed to send health data back to client");
365 }
366
367 /* End of transmission */
368 ret = close(new_sock);
369 if (ret) {
370 PERROR("close");
371 }
372 new_sock = -1;
373 }
374
375 exit:
376 error:
377 if (err) {
378 ERR("Health error occurred in %s", __func__);
379 }
380 DBG("Health check thread dying");
381 unlink(health_unix_sock_path);
382 (void) rmdir(relayd_path);
383 free(relayd_path);
384 (void) rmdir(lttng_rundir);
385 free(lttng_rundir);
386
387 if (sock >= 0) {
388 ret = close(sock);
389 if (ret) {
390 PERROR("close");
391 }
392 }
393
394 lttng_poll_clean(&events);
395
396 rcu_unregister_thread();
397 return NULL;
398 }
This page took 0.038623 seconds and 5 git commands to generate.