Comment why we cannot rmdir the lttng and relayd rundir
[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
58 int 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 */
65 static
66 int 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 */
80 static 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
90 static 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
128 error:
129 return ret;
130 }
131
132 static
133 int setup_health_path(void)
134 {
135 int is_root, ret = 0;
136 char *home_path = NULL, *rundir = NULL, *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
197 end:
198 free(rundir);
199 return ret;
200 }
201
202 /*
203 * Thread managing health check socket.
204 */
205 void *thread_manage_health(void *data)
206 {
207 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
208 uint32_t revents, nb_fd;
209 struct lttng_poll_event events;
210 struct health_comm_msg msg;
211 struct health_comm_reply reply;
212 int is_root;
213
214 DBG("[thread] Manage health check started");
215
216 setup_health_path();
217
218 rcu_register_thread();
219
220 /* We might hit an error path before this is created. */
221 lttng_poll_init(&events);
222
223 /* Create unix socket */
224 sock = lttcomm_create_unix_sock(health_unix_sock_path);
225 if (sock < 0) {
226 ERR("Unable to create health check Unix socket");
227 ret = -1;
228 goto error;
229 }
230
231 is_root = !getuid();
232 if (is_root) {
233 /* lttng health client socket path permissions */
234 ret = chown(health_unix_sock_path, 0,
235 utils_get_group_id(tracing_group_name));
236 if (ret < 0) {
237 ERR("Unable to set group on %s", health_unix_sock_path);
238 PERROR("chown");
239 ret = -1;
240 goto error;
241 }
242
243 ret = chmod(health_unix_sock_path,
244 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
245 if (ret < 0) {
246 ERR("Unable to set permissions on %s", health_unix_sock_path);
247 PERROR("chmod");
248 ret = -1;
249 goto error;
250 }
251 }
252
253 /*
254 * Set the CLOEXEC flag. Return code is useless because either way, the
255 * show must go on.
256 */
257 (void) utils_set_fd_cloexec(sock);
258
259 ret = lttcomm_listen_unix_sock(sock);
260 if (ret < 0) {
261 goto error;
262 }
263
264 /* Size is set to 1 for the consumer_channel pipe */
265 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
266 if (ret < 0) {
267 ERR("Poll set creation failed");
268 goto error;
269 }
270
271 ret = lttng_poll_add(&events, health_quit_pipe[0], LPOLLIN);
272 if (ret < 0) {
273 goto error;
274 }
275
276 /* Add the application registration socket */
277 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
278 if (ret < 0) {
279 goto error;
280 }
281
282 while (1) {
283 DBG("Health check ready");
284
285 /* Inifinite blocking call, waiting for transmission */
286 restart:
287 ret = lttng_poll_wait(&events, -1);
288 if (ret < 0) {
289 /*
290 * Restart interrupted system call.
291 */
292 if (errno == EINTR) {
293 goto restart;
294 }
295 goto error;
296 }
297
298 nb_fd = ret;
299
300 for (i = 0; i < nb_fd; i++) {
301 /* Fetch once the poll data */
302 revents = LTTNG_POLL_GETEV(&events, i);
303 pollfd = LTTNG_POLL_GETFD(&events, i);
304
305 /* Thread quit pipe has been closed. Killing thread. */
306 ret = check_health_quit_pipe(pollfd, revents);
307 if (ret) {
308 err = 0;
309 goto exit;
310 }
311
312 /* Event on the registration socket */
313 if (pollfd == sock) {
314 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
315 ERR("Health socket poll error");
316 goto error;
317 }
318 }
319 }
320
321 new_sock = lttcomm_accept_unix_sock(sock);
322 if (new_sock < 0) {
323 goto error;
324 }
325
326 /*
327 * Set the CLOEXEC flag. Return code is useless because either way, the
328 * show must go on.
329 */
330 (void) utils_set_fd_cloexec(new_sock);
331
332 DBG("Receiving data from client for health...");
333 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
334 if (ret <= 0) {
335 DBG("Nothing recv() from client... continuing");
336 ret = close(new_sock);
337 if (ret) {
338 PERROR("close");
339 }
340 new_sock = -1;
341 continue;
342 }
343
344 rcu_thread_online();
345
346 assert(msg.cmd == HEALTH_CMD_CHECK);
347
348 reply.ret_code = 0;
349 for (i = 0; i < NR_HEALTH_RELAYD_TYPES; i++) {
350 /*
351 * health_check_state return 0 if thread is in
352 * error.
353 */
354 if (!health_check_state(health_relayd, i)) {
355 reply.ret_code |= 1ULL << i;
356 }
357 }
358
359 DBG2("Health check return value %" PRIx64, reply.ret_code);
360
361 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
362 if (ret < 0) {
363 ERR("Failed to send health data back to client");
364 }
365
366 /* End of transmission */
367 ret = close(new_sock);
368 if (ret) {
369 PERROR("close");
370 }
371 new_sock = -1;
372 }
373
374 exit:
375 error:
376 if (err) {
377 ERR("Health error occurred in %s", __func__);
378 }
379 DBG("Health check thread dying");
380 unlink(health_unix_sock_path);
381 if (sock >= 0) {
382 ret = close(sock);
383 if (ret) {
384 PERROR("close");
385 }
386 }
387
388 /*
389 * We do NOT rmdir rundir nor the relayd path because there are
390 * other processes using them.
391 */
392
393 lttng_poll_clean(&events);
394
395 rcu_unregister_thread();
396 return NULL;
397 }
This page took 0.039407 seconds and 6 git commands to generate.