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