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