Commit | Line | Data |
---|---|---|
b8aa1682 JD |
1 | /* |
2 | * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * David Goulet <dgoulet@efficios.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along | |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
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/mman.h> | |
29 | #include <sys/mount.h> | |
30 | #include <sys/resource.h> | |
31 | #include <sys/socket.h> | |
32 | #include <sys/stat.h> | |
33 | #include <sys/types.h> | |
34 | #include <sys/wait.h> | |
35 | #include <urcu/futex.h> | |
36 | #include <urcu/uatomic.h> | |
37 | #include <unistd.h> | |
38 | #include <fcntl.h> | |
39 | #include <config.h> | |
40 | ||
41 | #include <lttng/lttng.h> | |
42 | #include <common/common.h> | |
43 | #include <common/compat/poll.h> | |
44 | #include <common/compat/socket.h> | |
45 | #include <common/defaults.h> | |
46 | #include <common/futex.h> | |
47 | #include <common/sessiond-comm/sessiond-comm.h> | |
48 | #include <common/sessiond-comm/inet.h> | |
49 | #include <common/hashtable/hashtable.h> | |
50 | #include <common/sessiond-comm/relayd.h> | |
51 | #include <common/uri.h> | |
52 | ||
53 | #include "lttng-relayd.h" | |
54 | ||
55 | /* command line options */ | |
56 | static int opt_daemon; | |
57 | static char *opt_output_path; | |
58 | static struct lttng_uri *control_uri = NULL; | |
59 | static struct lttng_uri *data_uri = NULL; | |
60 | ||
61 | const char *progname; | |
62 | static int is_root; /* Set to 1 if the daemon is running as root */ | |
63 | ||
64 | /* | |
65 | * Quit pipe for all threads. This permits a single cancellation point | |
66 | * for all threads when receiving an event on the pipe. | |
67 | */ | |
68 | static int thread_quit_pipe[2] = { -1, -1 }; | |
69 | ||
70 | /* | |
71 | * This pipe is used to inform the worker thread that a command is queued and | |
72 | * ready to be processed. | |
73 | */ | |
74 | static int relay_cmd_pipe[2] = { -1, -1 }; | |
75 | ||
76 | static int dispatch_thread_exit; | |
77 | ||
78 | static pthread_t listener_thread; | |
79 | static pthread_t dispatcher_thread; | |
80 | static pthread_t worker_thread; | |
81 | ||
82 | static uint64_t last_relay_stream_id = 0; | |
83 | static uint64_t last_relay_session_id = 0; | |
84 | ||
85 | /* | |
86 | * Relay command queue. | |
87 | * | |
88 | * The relay_thread_listener and relay_thread_dispatcher communicate with this | |
89 | * queue. | |
90 | */ | |
91 | static struct relay_cmd_queue relay_cmd_queue; | |
92 | ||
93 | /* buffer allocated at startup, used to store the trace data */ | |
94 | static char *data_buffer = NULL; | |
95 | static unsigned int data_buffer_size = 0; | |
96 | ||
97 | /* | |
98 | * usage function on stderr | |
99 | */ | |
100 | static | |
101 | void usage(void) | |
102 | { | |
103 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); | |
104 | fprintf(stderr, " -h, --help Display this usage.\n"); | |
105 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); | |
106 | fprintf(stderr, " -C, --control-port Control port listening (URI)\n"); | |
107 | fprintf(stderr, " -D, --data-port Data port listening (URI)\n"); | |
108 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
109 | } | |
110 | ||
111 | static | |
112 | int parse_args(int argc, char **argv) | |
113 | { | |
114 | int c; | |
115 | int ret = 0; | |
116 | char *default_address; | |
117 | ||
118 | static struct option long_options[] = { | |
119 | { "control-port", 1, 0, 'C' }, | |
120 | { "data-port", 1, 0, 'D' }, | |
121 | { "daemonize", 0, 0, 'd' }, | |
122 | { "output", 1, 0, 'o' }, | |
123 | { "verbose", 0, 0, 'v' }, | |
124 | { NULL, 0, 0, 0 } | |
125 | }; | |
126 | ||
127 | while (1) { | |
128 | int option_index = 0; | |
129 | c = getopt_long(argc, argv, "dhv" "C:D:o:", | |
130 | long_options, &option_index); | |
131 | if (c == -1) { | |
132 | break; | |
133 | } | |
134 | ||
135 | switch (c) { | |
136 | case 0: | |
137 | fprintf(stderr, "option %s", long_options[option_index].name); | |
138 | if (optarg) { | |
139 | fprintf(stderr, " with arg %s\n", optarg); | |
140 | } | |
141 | break; | |
142 | case 'C': | |
143 | ret = uri_parse(optarg, &control_uri); | |
144 | if (ret < 0) { | |
145 | ERR("Invalid control URI specified"); | |
146 | goto exit; | |
147 | } | |
148 | if (control_uri->port == 0) { | |
149 | control_uri->port = DEFAULT_NETWORK_CONTROL_PORT; | |
150 | } | |
151 | break; | |
152 | case 'D': | |
153 | ret = uri_parse(optarg, &data_uri); | |
154 | if (ret < 0) { | |
155 | ERR("Invalid data URI specified"); | |
156 | goto exit; | |
157 | } | |
158 | if (data_uri->port == 0) { | |
159 | data_uri->port = DEFAULT_NETWORK_DATA_PORT; | |
160 | } | |
161 | break; | |
162 | case 'd': | |
163 | opt_daemon = 1; | |
164 | break; | |
165 | case 'h': | |
166 | usage(); | |
167 | exit(EXIT_FAILURE); | |
168 | case 'o': | |
169 | ret = asprintf(&opt_output_path, "%s", optarg); | |
170 | if (ret < 0) { | |
171 | PERROR("asprintf opt_output_path"); | |
172 | goto exit; | |
173 | } | |
174 | break; | |
175 | case 'v': | |
176 | /* Verbose level can increase using multiple -v */ | |
177 | lttng_opt_verbose += 1; | |
178 | break; | |
179 | default: | |
180 | /* Unknown option or other error. | |
181 | * Error is printed by getopt, just return */ | |
182 | ret = -1; | |
183 | goto exit; | |
184 | } | |
185 | } | |
186 | ||
187 | /* assign default values */ | |
188 | if (control_uri == NULL) { | |
189 | ret = asprintf(&default_address, "tcp://0.0.0.0:%d", | |
190 | DEFAULT_NETWORK_CONTROL_PORT); | |
191 | if (ret < 0) { | |
192 | PERROR("asprintf default data address"); | |
193 | goto exit; | |
194 | } | |
195 | ||
196 | ret = uri_parse(default_address, &control_uri); | |
197 | free(default_address); | |
198 | if (ret < 0) { | |
199 | ERR("Invalid control URI specified"); | |
200 | goto exit; | |
201 | } | |
202 | } | |
203 | if (data_uri == NULL) { | |
204 | ret = asprintf(&default_address, "tcp://0.0.0.0:%d", | |
205 | DEFAULT_NETWORK_DATA_PORT); | |
206 | if (ret < 0) { | |
207 | PERROR("asprintf default data address"); | |
208 | goto exit; | |
209 | } | |
210 | ||
211 | ret = uri_parse(default_address, &data_uri); | |
212 | free(default_address); | |
213 | if (ret < 0) { | |
214 | ERR("Invalid data URI specified"); | |
215 | goto exit; | |
216 | } | |
217 | } | |
218 | ||
219 | exit: | |
220 | return ret; | |
221 | } | |
222 | ||
223 | /* | |
224 | * Cleanup the daemon | |
225 | */ | |
226 | static | |
227 | void cleanup(void) | |
228 | { | |
229 | int i, ret; | |
230 | ||
231 | DBG("Cleaning up"); | |
232 | ||
233 | for (i = 0; i < 2; i++) { | |
234 | if (thread_quit_pipe[i] >= 0) { | |
235 | ret = close(thread_quit_pipe[i]); | |
236 | if (ret) { | |
237 | PERROR("close"); | |
238 | } | |
239 | } | |
240 | } | |
241 | } | |
242 | ||
243 | /* | |
244 | * Write to writable pipe used to notify a thread. | |
245 | */ | |
246 | static | |
247 | int notify_thread_pipe(int wpipe) | |
248 | { | |
249 | int ret; | |
250 | ||
251 | ret = write(wpipe, "!", 1); | |
252 | if (ret < 0) { | |
253 | PERROR("write poll pipe"); | |
254 | } | |
255 | ||
256 | return ret; | |
257 | } | |
258 | ||
259 | /* | |
260 | * Stop all threads by closing the thread quit pipe. | |
261 | */ | |
262 | static | |
263 | void stop_threads(void) | |
264 | { | |
265 | int ret; | |
266 | ||
267 | /* Stopping all threads */ | |
268 | DBG("Terminating all threads"); | |
269 | ret = notify_thread_pipe(thread_quit_pipe[1]); | |
270 | if (ret < 0) { | |
271 | ERR("write error on thread quit pipe"); | |
272 | } | |
273 | ||
274 | /* Dispatch thread */ | |
275 | dispatch_thread_exit = 1; | |
276 | futex_nto1_wake(&relay_cmd_queue.futex); | |
277 | } | |
278 | ||
279 | /* | |
280 | * Signal handler for the daemon | |
281 | * | |
282 | * Simply stop all worker threads, leaving main() return gracefully after | |
283 | * joining all threads and calling cleanup(). | |
284 | */ | |
285 | static | |
286 | void sighandler(int sig) | |
287 | { | |
288 | switch (sig) { | |
289 | case SIGPIPE: | |
290 | DBG("SIGPIPE caught"); | |
291 | return; | |
292 | case SIGINT: | |
293 | DBG("SIGINT caught"); | |
294 | stop_threads(); | |
295 | break; | |
296 | case SIGTERM: | |
297 | DBG("SIGTERM caught"); | |
298 | stop_threads(); | |
299 | break; | |
300 | default: | |
301 | break; | |
302 | } | |
303 | } | |
304 | ||
305 | /* | |
306 | * Setup signal handler for : | |
307 | * SIGINT, SIGTERM, SIGPIPE | |
308 | */ | |
309 | static | |
310 | int set_signal_handler(void) | |
311 | { | |
312 | int ret = 0; | |
313 | struct sigaction sa; | |
314 | sigset_t sigset; | |
315 | ||
316 | if ((ret = sigemptyset(&sigset)) < 0) { | |
317 | PERROR("sigemptyset"); | |
318 | return ret; | |
319 | } | |
320 | ||
321 | sa.sa_handler = sighandler; | |
322 | sa.sa_mask = sigset; | |
323 | sa.sa_flags = 0; | |
324 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
325 | PERROR("sigaction"); | |
326 | return ret; | |
327 | } | |
328 | ||
329 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
330 | PERROR("sigaction"); | |
331 | return ret; | |
332 | } | |
333 | ||
334 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
335 | PERROR("sigaction"); | |
336 | return ret; | |
337 | } | |
338 | ||
339 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); | |
340 | ||
341 | return ret; | |
342 | } | |
343 | ||
344 | /* | |
345 | * Init thread quit pipe. | |
346 | * | |
347 | * Return -1 on error or 0 if all pipes are created. | |
348 | */ | |
349 | static | |
350 | int init_thread_quit_pipe(void) | |
351 | { | |
352 | int ret, i; | |
353 | ||
354 | ret = pipe(thread_quit_pipe); | |
355 | if (ret < 0) { | |
356 | PERROR("thread quit pipe"); | |
357 | goto error; | |
358 | } | |
359 | ||
360 | for (i = 0; i < 2; i++) { | |
361 | ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC); | |
362 | if (ret < 0) { | |
363 | PERROR("fcntl"); | |
364 | goto error; | |
365 | } | |
366 | } | |
367 | ||
368 | error: | |
369 | return ret; | |
370 | } | |
371 | ||
372 | /* | |
373 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
374 | */ | |
375 | static | |
376 | int create_thread_poll_set(struct lttng_poll_event *events, int size) | |
377 | { | |
378 | int ret; | |
379 | ||
380 | if (events == NULL || size == 0) { | |
381 | ret = -1; | |
382 | goto error; | |
383 | } | |
384 | ||
385 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
386 | if (ret < 0) { | |
387 | goto error; | |
388 | } | |
389 | ||
390 | /* Add quit pipe */ | |
391 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN); | |
392 | if (ret < 0) { | |
393 | goto error; | |
394 | } | |
395 | ||
396 | return 0; | |
397 | ||
398 | error: | |
399 | return ret; | |
400 | } | |
401 | ||
402 | /* | |
403 | * Check if the thread quit pipe was triggered. | |
404 | * | |
405 | * Return 1 if it was triggered else 0; | |
406 | */ | |
407 | static | |
408 | int check_thread_quit_pipe(int fd, uint32_t events) | |
409 | { | |
410 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
411 | return 1; | |
412 | } | |
413 | ||
414 | return 0; | |
415 | } | |
416 | ||
417 | /* | |
418 | * Create and init socket from uri. | |
419 | */ | |
420 | static | |
421 | struct lttcomm_sock *relay_init_sock(struct lttng_uri *uri) | |
422 | { | |
423 | int ret; | |
424 | struct lttcomm_sock *sock = NULL; | |
425 | ||
426 | sock = lttcomm_alloc_sock_from_uri(uri); | |
427 | if (sock == NULL) { | |
428 | ERR("Allocating socket"); | |
429 | goto error; | |
430 | } | |
431 | ||
432 | ret = lttcomm_create_sock(sock); | |
433 | if (ret < 0) { | |
434 | goto error; | |
435 | } | |
436 | DBG("Listening on sock %d", sock->fd); | |
437 | ||
438 | ret = sock->ops->bind(sock); | |
439 | if (ret < 0) { | |
440 | goto error; | |
441 | } | |
442 | ||
443 | ret = sock->ops->listen(sock, -1); | |
444 | if (ret < 0) { | |
445 | goto error; | |
446 | ||
447 | } | |
448 | ||
449 | return sock; | |
450 | ||
451 | error: | |
452 | if (sock) { | |
453 | lttcomm_destroy_sock(sock); | |
454 | } | |
455 | return NULL; | |
456 | } | |
457 | ||
458 | /* | |
459 | * This thread manages the listening for new connections on the network | |
460 | */ | |
461 | static | |
462 | void *relay_thread_listener(void *data) | |
463 | { | |
464 | int i, ret, pollfd; | |
465 | int val = 1; | |
466 | uint32_t revents, nb_fd; | |
467 | struct lttng_poll_event events; | |
468 | struct lttcomm_sock *control_sock, *data_sock; | |
469 | ||
470 | /* | |
471 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
472 | * freed in the worker thread. | |
473 | */ | |
474 | struct relay_command *relay_cmd = NULL; | |
475 | ||
476 | DBG("[thread] Relay listener started"); | |
477 | ||
478 | control_sock = relay_init_sock(control_uri); | |
479 | if (!control_sock) { | |
480 | goto error_sock; | |
481 | } | |
482 | ||
483 | data_sock = relay_init_sock(data_uri); | |
484 | if (!data_sock) { | |
485 | goto error_sock; | |
486 | } | |
487 | ||
488 | /* | |
489 | * Pass 3 as size here for the thread quit pipe, control and data socket. | |
490 | */ | |
491 | ret = create_thread_poll_set(&events, 3); | |
492 | if (ret < 0) { | |
493 | goto error_create_poll; | |
494 | } | |
495 | ||
496 | /* Add the control socket */ | |
497 | ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP); | |
498 | if (ret < 0) { | |
499 | goto error_poll_add; | |
500 | } | |
501 | ||
502 | /* Add the data socket */ | |
503 | ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP); | |
504 | if (ret < 0) { | |
505 | goto error_poll_add; | |
506 | } | |
507 | ||
508 | while (1) { | |
509 | DBG("Listener accepting connections"); | |
510 | ||
511 | nb_fd = LTTNG_POLL_GETNB(&events); | |
512 | ||
513 | restart: | |
514 | ret = lttng_poll_wait(&events, -1); | |
515 | if (ret < 0) { | |
516 | /* | |
517 | * Restart interrupted system call. | |
518 | */ | |
519 | if (errno == EINTR) { | |
520 | goto restart; | |
521 | } | |
522 | goto error; | |
523 | } | |
524 | ||
525 | DBG("Relay new connection received"); | |
526 | for (i = 0; i < nb_fd; i++) { | |
527 | /* Fetch once the poll data */ | |
528 | revents = LTTNG_POLL_GETEV(&events, i); | |
529 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
530 | ||
531 | /* Thread quit pipe has been closed. Killing thread. */ | |
532 | ret = check_thread_quit_pipe(pollfd, revents); | |
533 | if (ret) { | |
534 | goto error; | |
535 | } | |
536 | ||
537 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
538 | ERR("socket poll error"); | |
539 | goto error; | |
540 | } else if (revents & LPOLLIN) { | |
541 | struct lttcomm_sock *newsock = NULL; | |
542 | ||
543 | relay_cmd = zmalloc(sizeof(struct relay_command)); | |
544 | if (relay_cmd == NULL) { | |
545 | PERROR("relay command zmalloc"); | |
546 | goto error; | |
547 | } | |
548 | ||
549 | if (pollfd == data_sock->fd) { | |
550 | newsock = data_sock->ops->accept(data_sock); | |
551 | if (newsock < 0) { | |
552 | PERROR("accepting data sock"); | |
553 | goto error; | |
554 | } | |
555 | relay_cmd->type = RELAY_DATA; | |
556 | DBG("Relay data connection accepted, socket %d", newsock->fd); | |
557 | } else if (pollfd == control_sock->fd) { | |
558 | newsock = control_sock->ops->accept(control_sock); | |
559 | if (newsock < 0) { | |
560 | PERROR("accepting control sock"); | |
561 | goto error; | |
562 | } | |
563 | relay_cmd->type = RELAY_CONTROL; | |
564 | DBG("Relay control connection accepted, socket %d", newsock->fd); | |
565 | } | |
566 | ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, | |
567 | &val, sizeof(int)); | |
568 | if (ret < 0) { | |
569 | PERROR("setsockopt inet"); | |
570 | goto error; | |
571 | } | |
572 | relay_cmd->sock = newsock; | |
573 | /* | |
574 | * Lock free enqueue the request. | |
575 | */ | |
576 | cds_wfq_enqueue(&relay_cmd_queue.queue, &relay_cmd->node); | |
577 | ||
578 | /* | |
579 | * Wake the dispatch queue futex. Implicit memory | |
580 | * barrier with the exchange in cds_wfq_enqueue. | |
581 | */ | |
582 | futex_nto1_wake(&relay_cmd_queue.futex); | |
583 | } | |
584 | } | |
585 | } | |
586 | ||
587 | error: | |
588 | error_poll_add: | |
589 | lttng_poll_clean(&events); | |
590 | error_create_poll: | |
591 | if (control_sock->fd >= 0) { | |
592 | ret = control_sock->ops->close(control_sock); | |
593 | if (ret) { | |
594 | PERROR("close"); | |
595 | } | |
596 | lttcomm_destroy_sock(control_sock); | |
597 | } | |
598 | if (data_sock->fd >= 0) { | |
599 | ret = data_sock->ops->close(data_sock); | |
600 | if (ret) { | |
601 | PERROR("close"); | |
602 | } | |
603 | lttcomm_destroy_sock(data_sock); | |
604 | } | |
605 | ||
606 | DBG("Relay listener thread cleanup complete"); | |
607 | stop_threads(); | |
608 | error_sock: | |
609 | return NULL; | |
610 | } | |
611 | ||
612 | /* | |
613 | * This thread manages the dispatching of the requests to worker threads | |
614 | */ | |
615 | static | |
616 | void *relay_thread_dispatcher(void *data) | |
617 | { | |
618 | int ret; | |
619 | struct cds_wfq_node *node; | |
620 | struct relay_command *relay_cmd = NULL; | |
621 | ||
622 | DBG("[thread] Relay dispatcher started"); | |
623 | ||
624 | while (!dispatch_thread_exit) { | |
625 | /* Atomically prepare the queue futex */ | |
626 | futex_nto1_prepare(&relay_cmd_queue.futex); | |
627 | ||
628 | do { | |
629 | /* Dequeue commands */ | |
630 | node = cds_wfq_dequeue_blocking(&relay_cmd_queue.queue); | |
631 | if (node == NULL) { | |
632 | DBG("Woken up but nothing in the relay command queue"); | |
633 | /* Continue thread execution */ | |
634 | break; | |
635 | } | |
636 | ||
637 | relay_cmd = caa_container_of(node, struct relay_command, node); | |
638 | DBG("Dispatching request waiting on sock %d", relay_cmd->sock->fd); | |
639 | ||
640 | /* | |
641 | * Inform worker thread of the new request. This | |
642 | * call is blocking so we can be assured that the data will be read | |
643 | * at some point in time or wait to the end of the world :) | |
644 | */ | |
645 | ret = write(relay_cmd_pipe[1], relay_cmd, | |
646 | sizeof(struct relay_command)); | |
647 | free(relay_cmd); | |
648 | if (ret < 0) { | |
649 | PERROR("write cmd pipe"); | |
650 | goto error; | |
651 | } | |
652 | } while (node != NULL); | |
653 | ||
654 | /* Futex wait on queue. Blocking call on futex() */ | |
655 | futex_nto1_wait(&relay_cmd_queue.futex); | |
656 | } | |
657 | ||
658 | error: | |
659 | DBG("Dispatch thread dying"); | |
660 | stop_threads(); | |
661 | return NULL; | |
662 | } | |
663 | ||
664 | /* | |
665 | * Return the realpath(3) of the path even if the last directory token does not | |
666 | * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the | |
667 | * /tmp/test1 does, the real path is returned. In normal time, realpath(3) | |
668 | * fails if the end point directory does not exist. | |
669 | */ | |
670 | static | |
671 | char *expand_full_path(const char *path) | |
672 | { | |
673 | const char *end_path = path; | |
674 | char *next, *cut_path, *expanded_path; | |
675 | ||
676 | /* Find last token delimited by '/' */ | |
677 | while ((next = strpbrk(end_path + 1, "/"))) { | |
678 | end_path = next; | |
679 | } | |
680 | ||
681 | /* Cut last token from original path */ | |
682 | cut_path = strndup(path, end_path - path); | |
683 | ||
684 | expanded_path = malloc(PATH_MAX); | |
685 | if (expanded_path == NULL) { | |
686 | goto error; | |
687 | } | |
688 | ||
689 | expanded_path = realpath((char *)cut_path, expanded_path); | |
690 | if (expanded_path == NULL) { | |
691 | switch (errno) { | |
692 | case ENOENT: | |
693 | ERR("%s: No such file or directory", cut_path); | |
694 | break; | |
695 | default: | |
696 | PERROR("realpath"); | |
697 | break; | |
698 | } | |
699 | goto error; | |
700 | } | |
701 | ||
702 | /* Add end part to expanded path */ | |
703 | strcat(expanded_path, end_path); | |
704 | ||
705 | free(cut_path); | |
706 | return expanded_path; | |
707 | ||
708 | error: | |
709 | free(cut_path); | |
710 | return NULL; | |
711 | } | |
712 | ||
713 | ||
714 | /* | |
715 | * config_get_default_path | |
716 | * | |
717 | * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer. | |
718 | */ | |
719 | static | |
720 | char *config_get_default_path(void) | |
721 | { | |
722 | return getenv("HOME"); | |
723 | } | |
724 | ||
725 | /* | |
726 | * Create recursively directory using the FULL path. | |
727 | */ | |
728 | static | |
729 | int mkdir_recursive(char *path, mode_t mode) | |
730 | { | |
731 | char *p, tmp[PATH_MAX]; | |
732 | struct stat statbuf; | |
733 | size_t len; | |
734 | int ret; | |
735 | ||
736 | ret = snprintf(tmp, sizeof(tmp), "%s", path); | |
737 | if (ret < 0) { | |
738 | PERROR("snprintf mkdir"); | |
739 | goto error; | |
740 | } | |
741 | ||
742 | len = ret; | |
743 | if (tmp[len - 1] == '/') { | |
744 | tmp[len - 1] = 0; | |
745 | } | |
746 | ||
747 | for (p = tmp + 1; *p; p++) { | |
748 | if (*p == '/') { | |
749 | *p = 0; | |
750 | if (tmp[strlen(tmp) - 1] == '.' && | |
751 | tmp[strlen(tmp) - 2] == '.' && | |
752 | tmp[strlen(tmp) - 3] == '/') { | |
753 | ERR("Using '/../' is not permitted in the trace path (%s)", | |
754 | tmp); | |
755 | ret = -1; | |
756 | goto error; | |
757 | } | |
758 | ret = stat(tmp, &statbuf); | |
759 | if (ret < 0) { | |
760 | ret = mkdir(tmp, mode); | |
761 | if (ret < 0) { | |
762 | if (!(errno == EEXIST)) { | |
763 | PERROR("mkdir recursive"); | |
764 | ret = -errno; | |
765 | goto error; | |
766 | } | |
767 | } | |
768 | } | |
769 | *p = '/'; | |
770 | } | |
771 | } | |
772 | ||
773 | ret = mkdir(tmp, mode); | |
774 | if (ret < 0) { | |
775 | if (!(errno == EEXIST)) { | |
776 | PERROR("mkdir recursive last piece"); | |
777 | ret = -errno; | |
778 | } else { | |
779 | ret = 0; | |
780 | } | |
781 | } | |
782 | ||
783 | error: | |
784 | return ret; | |
785 | } | |
786 | ||
787 | /* | |
788 | * create_output_path: create the output trace directory | |
789 | */ | |
790 | static | |
791 | char *create_output_path(char *path_name) | |
792 | { | |
793 | int ret = 0; | |
794 | char *alloc_path = NULL; | |
795 | char *traces_path = NULL; | |
796 | char *full_path = NULL; | |
797 | ||
798 | /* Auto output path */ | |
799 | if (opt_output_path == NULL) { | |
800 | alloc_path = strdup(config_get_default_path()); | |
801 | if (alloc_path == NULL) { | |
802 | ERR("Home path not found.\n \ | |
803 | Please specify an output path using -o, --output PATH"); | |
804 | ret = -1; | |
805 | goto exit; | |
806 | } | |
807 | ||
808 | ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME | |
809 | "/%s", alloc_path, path_name); | |
810 | if (ret < 0) { | |
811 | PERROR("asprintf trace dir name"); | |
812 | goto exit; | |
813 | } | |
814 | } else { | |
815 | full_path = expand_full_path(opt_output_path); | |
816 | ret = asprintf(&traces_path, "%s/%s", full_path, path_name); | |
817 | if (ret < 0) { | |
818 | PERROR("asprintf trace dir name"); | |
819 | goto exit; | |
820 | } | |
821 | } | |
822 | free(alloc_path); | |
823 | free(full_path); | |
824 | ||
825 | exit: | |
826 | return traces_path; | |
827 | } | |
828 | ||
829 | /* | |
830 | * relay_delete_session: Free all memory associated with a session and | |
831 | * close all the FDs | |
832 | */ | |
833 | static | |
834 | void relay_delete_session(struct relay_command *cmd, struct lttng_ht *streams_ht) | |
835 | { | |
836 | struct lttng_ht_iter iter; | |
837 | struct lttng_ht_node_ulong *node; | |
838 | struct relay_stream *stream; | |
839 | int ret; | |
840 | ||
841 | if (!cmd->session) | |
842 | return; | |
843 | ||
844 | DBG("Relay deleting session %lu", cmd->session->id); | |
845 | free(cmd->session->sock); | |
846 | ||
847 | cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, node, node) { | |
848 | node = lttng_ht_iter_get_node_ulong(&iter); | |
849 | if (node) { | |
850 | stream = caa_container_of(node, | |
851 | struct relay_stream, stream_n); | |
852 | if (stream->session == cmd->session) { | |
853 | close(stream->fd); | |
854 | ret = lttng_ht_del(streams_ht, &iter); | |
855 | assert(!ret); | |
856 | free(stream); | |
857 | } | |
858 | } | |
859 | } | |
860 | } | |
861 | ||
862 | /* | |
863 | * relay_add_stream: allocate a new stream for a session | |
864 | */ | |
865 | static | |
866 | int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, | |
867 | struct relay_command *cmd, struct lttng_ht *streams_ht) | |
868 | { | |
869 | struct relay_session *session = cmd->session; | |
870 | struct lttcomm_relayd_add_stream stream_info; | |
871 | struct relay_stream *stream = NULL; | |
872 | struct lttcomm_relayd_status_stream reply; | |
873 | char *path = NULL, *root_path = NULL; | |
874 | int ret, send_ret; | |
875 | ||
876 | if (!session || session->version_check_done == 0) { | |
877 | ERR("Trying to add a stream before version check"); | |
878 | ret = -1; | |
879 | goto end_no_session; | |
880 | } | |
881 | ||
882 | /* FIXME : use data_size for something ? */ | |
883 | ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info, | |
884 | sizeof(struct lttcomm_relayd_add_stream), MSG_WAITALL); | |
885 | if (ret < sizeof(struct lttcomm_relayd_add_stream)) { | |
886 | ERR("Relay didn't receive valid add_stream struct size : %d", ret); | |
887 | ret = -1; | |
888 | goto end_no_session; | |
889 | } | |
890 | stream = zmalloc(sizeof(struct relay_stream)); | |
891 | if (stream == NULL) { | |
892 | PERROR("relay stream zmalloc"); | |
893 | ret = -1; | |
894 | goto end_no_session; | |
895 | } | |
896 | ||
897 | stream->stream_handle = ++last_relay_stream_id; | |
898 | stream->seq = 0; | |
899 | stream->session = session; | |
900 | ||
901 | root_path = create_output_path(stream_info.pathname); | |
902 | if (!root_path) { | |
903 | ret = -1; | |
904 | goto end; | |
905 | } | |
906 | ret = mkdir_recursive(root_path, S_IRWXU | S_IRWXG); | |
907 | if (ret < 0) { | |
908 | free(root_path); | |
909 | ERR("relay creating output directory"); | |
910 | goto end; | |
911 | } | |
912 | ||
913 | ret = asprintf(&path, "%s/%s", root_path, stream_info.channel_name); | |
914 | if (ret < 0) { | |
915 | PERROR("asprintf stream path"); | |
916 | goto end; | |
917 | } | |
918 | ||
919 | ret = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO); | |
920 | if (ret < 0) { | |
921 | PERROR("Relay creating trace file"); | |
922 | goto end; | |
923 | } | |
924 | ||
925 | stream->fd = ret; | |
926 | DBG("Tracefile %s created", path); | |
927 | ||
928 | lttng_ht_node_init_ulong(&stream->stream_n, | |
929 | (unsigned long) stream->stream_handle); | |
930 | lttng_ht_add_unique_ulong(streams_ht, | |
931 | &stream->stream_n); | |
932 | ||
933 | DBG("Relay new stream added %s", stream_info.channel_name); | |
934 | ||
935 | end: | |
936 | free(path); | |
937 | free(root_path); | |
938 | /* send the session id to the client or a negative return code on error */ | |
939 | if (ret < 0) { | |
940 | reply.ret_code = htobe32(LTTCOMM_ERR); | |
941 | } else { | |
942 | reply.ret_code = htobe32(LTTCOMM_OK); | |
943 | } | |
944 | reply.handle = htobe64(stream->stream_handle); | |
945 | send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, | |
946 | sizeof(struct lttcomm_relayd_status_stream), 0); | |
947 | if (send_ret < 0) { | |
948 | ERR("Relay sending stream id"); | |
949 | } | |
950 | ||
951 | end_no_session: | |
952 | return ret; | |
953 | } | |
954 | ||
955 | /* | |
956 | * relay_unknown_command: send -1 if received unknown command | |
957 | */ | |
958 | static | |
959 | void relay_unknown_command(struct relay_command *cmd) | |
960 | { | |
961 | struct lttcomm_relayd_generic_reply reply; | |
962 | int ret; | |
963 | ||
964 | reply.ret_code = htobe32(LTTCOMM_ERR); | |
965 | ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, | |
966 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
967 | if (ret < 0) { | |
968 | ERR("Relay sending unknown command"); | |
969 | } | |
970 | } | |
971 | ||
972 | /* | |
973 | * relay_start: send an acknowledgment to the client to tell if we are | |
974 | * ready to receive data. We are ready if a session is established. | |
975 | */ | |
976 | static | |
977 | int relay_start(struct lttcomm_relayd_hdr *recv_hdr, | |
978 | struct relay_command *cmd) | |
979 | { | |
980 | int ret = htobe32(LTTCOMM_OK); | |
981 | struct lttcomm_relayd_generic_reply reply; | |
982 | struct relay_session *session = cmd->session; | |
983 | ||
984 | if (!session) { | |
985 | DBG("Trying to start the streaming without a session established"); | |
986 | ret = htobe32(LTTCOMM_ERR); | |
987 | } | |
988 | ||
989 | reply.ret_code = ret; | |
990 | ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, | |
991 | sizeof(struct lttcomm_relayd_generic_reply), 0); | |
992 | if (ret < 0) { | |
993 | ERR("Relay sending start ack"); | |
994 | } | |
995 | ||
996 | return ret; | |
997 | } | |
998 | ||
999 | /* | |
1000 | * Get stream from stream id. | |
1001 | */ | |
1002 | static | |
1003 | struct relay_stream *relay_stream_from_stream_id(uint64_t stream_id, | |
1004 | struct lttng_ht *streams_ht) | |
1005 | { | |
1006 | struct lttng_ht_node_ulong *node; | |
1007 | struct lttng_ht_iter iter; | |
1008 | struct relay_stream *ret; | |
1009 | ||
1010 | lttng_ht_lookup(streams_ht, | |
1011 | (void *)((unsigned long) stream_id), | |
1012 | &iter); | |
1013 | node = lttng_ht_iter_get_node_ulong(&iter); | |
1014 | if (node == NULL) { | |
1015 | DBG("Relay stream %lu not found", stream_id); | |
1016 | ret = NULL; | |
1017 | goto end; | |
1018 | } | |
1019 | ||
1020 | ret = caa_container_of(node, struct relay_stream, stream_n); | |
1021 | ||
1022 | end: | |
1023 | return ret; | |
1024 | } | |
1025 | ||
1026 | /* | |
1027 | * relay_recv_metadata: receive the metada for the session. | |
1028 | */ | |
1029 | static | |
1030 | int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr, | |
1031 | struct relay_command *cmd, struct lttng_ht *streams_ht) | |
1032 | { | |
1033 | int ret = htobe32(LTTCOMM_OK); | |
1034 | struct relay_session *session = cmd->session; | |
1035 | struct lttcomm_relayd_metadata_payload *metadata_struct; | |
1036 | struct relay_stream *metadata_stream; | |
1037 | uint64_t data_size, payload_size; | |
1038 | ||
1039 | if (!session) { | |
1040 | ERR("Metadata sent before version check"); | |
1041 | ret = -1; | |
1042 | goto end; | |
1043 | } | |
1044 | ||
1045 | data_size = be64toh(recv_hdr->data_size); | |
1046 | payload_size = data_size - sizeof(uint64_t); | |
1047 | if (data_buffer_size < data_size) { | |
1048 | data_buffer = realloc(data_buffer, data_size); | |
1049 | if (!data_buffer) { | |
1050 | ERR("Allocating data buffer"); | |
1051 | ret = -1; | |
1052 | goto end; | |
1053 | } | |
1054 | data_buffer_size = data_size; | |
1055 | } | |
1056 | memset(data_buffer, 0, data_size); | |
1057 | DBG2("Relay receiving metadata, waiting for %lu bytes", data_size); | |
1058 | ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, MSG_WAITALL); | |
1059 | if (ret < 0 || ret != data_size) { | |
1060 | ret = -1; | |
1061 | ERR("Relay didn't receive the whole metadata"); | |
1062 | goto end; | |
1063 | } | |
1064 | metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer; | |
1065 | metadata_stream = relay_stream_from_stream_id( | |
1066 | be64toh(metadata_struct->stream_id), streams_ht); | |
1067 | if (!metadata_stream) { | |
1068 | ret = -1; | |
1069 | goto end; | |
1070 | } | |
1071 | ||
1072 | ret = write(metadata_stream->fd, metadata_struct->payload, | |
1073 | payload_size); | |
1074 | if (ret < (payload_size)) { | |
1075 | ERR("Relay error writing metadata on file"); | |
1076 | ret = -1; | |
1077 | goto end; | |
1078 | } | |
1079 | DBG2("Relay metadata written"); | |
1080 | ||
1081 | end: | |
1082 | return ret; | |
1083 | } | |
1084 | ||
1085 | /* | |
1086 | * relay_send_version: send relayd version number | |
1087 | */ | |
1088 | static | |
1089 | int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr, | |
1090 | struct relay_command *cmd) | |
1091 | { | |
1092 | int ret = htobe32(LTTCOMM_OK); | |
1093 | struct lttcomm_relayd_version reply; | |
1094 | struct relay_session *session = NULL; | |
1095 | ||
1096 | if (cmd->session == NULL) { | |
1097 | session = zmalloc(sizeof(struct relay_session)); | |
1098 | if (session == NULL) { | |
1099 | PERROR("relay session zmalloc"); | |
1100 | ret = -1; | |
1101 | goto end; | |
1102 | } | |
1103 | session->id = ++last_relay_session_id; | |
1104 | DBG("Created session %lu", session->id); | |
1105 | cmd->session = session; | |
1106 | } | |
1107 | session->version_check_done = 1; | |
1108 | ||
1109 | sscanf(VERSION, "%u.%u", &reply.major, &reply.minor); | |
1110 | reply.major = htobe32(reply.major); | |
1111 | reply.minor = htobe32(reply.minor); | |
1112 | ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, | |
1113 | sizeof(struct lttcomm_relayd_version), 0); | |
1114 | if (ret < 0) { | |
1115 | ERR("Relay sending version"); | |
1116 | } | |
1117 | DBG("Version check done"); | |
1118 | ||
1119 | end: | |
1120 | return ret; | |
1121 | } | |
1122 | ||
1123 | /* | |
1124 | * relay_process_control: Process the commands received on the control socket | |
1125 | */ | |
1126 | static | |
1127 | int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr, | |
1128 | struct relay_command *cmd, struct lttng_ht *streams_ht) | |
1129 | { | |
1130 | int ret = 0; | |
1131 | ||
1132 | switch (be32toh(recv_hdr->cmd)) { | |
1133 | /* | |
1134 | case RELAYD_CREATE_SESSION: | |
1135 | ret = relay_create_session(recv_hdr, cmd); | |
1136 | break; | |
1137 | */ | |
1138 | case RELAYD_ADD_STREAM: | |
1139 | ret = relay_add_stream(recv_hdr, cmd, streams_ht); | |
1140 | break; | |
1141 | case RELAYD_START_DATA: | |
1142 | ret = relay_start(recv_hdr, cmd); | |
1143 | break; | |
1144 | case RELAYD_SEND_METADATA: | |
1145 | ret = relay_recv_metadata(recv_hdr, cmd, streams_ht); | |
1146 | break; | |
1147 | case RELAYD_VERSION: | |
1148 | ret = relay_send_version(recv_hdr, cmd); | |
1149 | break; | |
1150 | case RELAYD_UPDATE_SYNC_INFO: | |
1151 | default: | |
1152 | ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd)); | |
1153 | relay_unknown_command(cmd); | |
1154 | ret = -1; | |
1155 | goto end; | |
1156 | } | |
1157 | ||
1158 | end: | |
1159 | return ret; | |
1160 | } | |
1161 | ||
1162 | /* | |
1163 | * relay_process_data: Process the data received on the data socket | |
1164 | */ | |
1165 | static | |
1166 | int relay_process_data(struct relay_command *cmd, struct lttng_ht *streams_ht) | |
1167 | { | |
1168 | int ret = 0; | |
1169 | struct relay_stream *stream; | |
1170 | struct lttcomm_relayd_data_hdr data_hdr; | |
1171 | uint64_t stream_id; | |
1172 | uint32_t data_size; | |
1173 | ||
1174 | ret = cmd->sock->ops->recvmsg(cmd->sock, &data_hdr, | |
1175 | sizeof(struct lttcomm_relayd_data_hdr), MSG_WAITALL); | |
1176 | if (ret <= 0) { | |
1177 | ERR("Connections seems to be closed"); | |
1178 | ret = -1; | |
1179 | goto end; | |
1180 | } | |
1181 | ||
1182 | stream_id = be64toh(data_hdr.stream_id); | |
1183 | stream = relay_stream_from_stream_id(stream_id, streams_ht); | |
1184 | if (!stream) { | |
1185 | ret = -1; | |
1186 | goto end; | |
1187 | } | |
1188 | ||
1189 | data_size = be32toh(data_hdr.data_size); | |
1190 | if (data_buffer_size < data_size) { | |
1191 | data_buffer = realloc(data_buffer, data_size); | |
1192 | if (!data_buffer) { | |
1193 | ERR("Allocating data buffer"); | |
1194 | ret = -1; | |
1195 | goto end; | |
1196 | } | |
1197 | data_buffer_size = data_size; | |
1198 | } | |
1199 | memset(data_buffer, 0, data_size); | |
1200 | ||
1201 | ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, MSG_WAITALL); | |
1202 | if (ret <= 0) { | |
1203 | ret = -1; | |
1204 | goto end; | |
1205 | } | |
1206 | ||
1207 | ret = write(stream->fd, data_buffer, data_size); | |
1208 | if (ret < data_size) { | |
1209 | ERR("Relay error writing data to file"); | |
1210 | ret = -1; | |
1211 | goto end; | |
1212 | } | |
1213 | DBG2("Relay wrote %d bytes to tracefile for stream id %lu", ret, stream->stream_handle); | |
1214 | ||
1215 | end: | |
1216 | return ret; | |
1217 | } | |
1218 | ||
1219 | static | |
1220 | void relay_cleanup_connection(struct lttng_ht *relay_connections_ht, struct lttng_poll_event *events, | |
1221 | struct lttng_ht *streams_ht, int pollfd, struct lttng_ht_iter *iter) | |
1222 | { | |
1223 | int ret; | |
1224 | ||
1225 | ret = lttng_ht_del(relay_connections_ht, iter); | |
1226 | assert(!ret); | |
1227 | lttng_poll_del(events, pollfd); | |
1228 | ||
1229 | ret = close(pollfd); | |
1230 | if (ret < 0) { | |
1231 | ERR("Closing pollfd %d", pollfd); | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | static | |
1236 | int relay_add_connection(int fd, struct lttng_poll_event *events, | |
1237 | struct lttng_ht *relay_connections_ht) | |
1238 | { | |
1239 | int ret; | |
1240 | struct relay_command *relay_connection; | |
1241 | ||
1242 | relay_connection = zmalloc(sizeof(struct relay_command)); | |
1243 | if (relay_connection == NULL) { | |
1244 | PERROR("Relay command zmalloc"); | |
1245 | ret = -1; | |
1246 | goto end; | |
1247 | } | |
1248 | ret = read(fd, relay_connection, sizeof(struct relay_command)); | |
1249 | if (ret < 0 || ret < sizeof(relay_connection)) { | |
1250 | PERROR("read relay cmd pipe"); | |
1251 | ret = -1; | |
1252 | goto end; | |
1253 | } | |
1254 | ||
1255 | lttng_ht_node_init_ulong(&relay_connection->sock_n, | |
1256 | (unsigned long) relay_connection->sock->fd); | |
1257 | lttng_ht_add_unique_ulong(relay_connections_ht, | |
1258 | &relay_connection->sock_n); | |
1259 | ret = lttng_poll_add(events, | |
1260 | relay_connection->sock->fd, | |
1261 | LPOLLIN | LPOLLRDHUP); | |
1262 | ||
1263 | end: | |
1264 | return ret; | |
1265 | } | |
1266 | ||
1267 | /* | |
1268 | * This thread does the actual work | |
1269 | */ | |
1270 | static | |
1271 | void *relay_thread_worker(void *data) | |
1272 | { | |
1273 | int i, ret, pollfd; | |
1274 | uint32_t revents, nb_fd; | |
1275 | struct relay_command *relay_connection; | |
1276 | struct lttng_poll_event events; | |
1277 | struct lttng_ht *relay_connections_ht; | |
1278 | struct lttng_ht_node_ulong *node; | |
1279 | struct lttng_ht_iter iter; | |
1280 | struct lttng_ht *streams_ht; | |
1281 | struct lttcomm_relayd_hdr recv_hdr; | |
1282 | ||
1283 | DBG("[thread] Relay worker started"); | |
1284 | ||
1285 | /* table of connections indexed on socket */ | |
1286 | relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
1287 | ||
1288 | /* tables of streams indexed by stream ID */ | |
1289 | streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
1290 | ||
1291 | ret = create_thread_poll_set(&events, 2); | |
1292 | if (ret < 0) { | |
1293 | goto error_poll_create; | |
1294 | } | |
1295 | ||
1296 | ret = lttng_poll_add(&events, relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); | |
1297 | if (ret < 0) { | |
1298 | goto error; | |
1299 | } | |
1300 | ||
1301 | while (1) { | |
1302 | /* Zeroed the events structure */ | |
1303 | lttng_poll_reset(&events); | |
1304 | ||
1305 | nb_fd = LTTNG_POLL_GETNB(&events); | |
1306 | ||
1307 | /* Infinite blocking call, waiting for transmission */ | |
1308 | restart: | |
1309 | ret = lttng_poll_wait(&events, -1); | |
1310 | if (ret < 0) { | |
1311 | /* | |
1312 | * Restart interrupted system call. | |
1313 | */ | |
1314 | if (errno == EINTR) { | |
1315 | goto restart; | |
1316 | } | |
1317 | goto error; | |
1318 | } | |
1319 | ||
1320 | for (i = 0; i < nb_fd; i++) { | |
1321 | /* Fetch once the poll data */ | |
1322 | revents = LTTNG_POLL_GETEV(&events, i); | |
1323 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1324 | ||
1325 | /* Thread quit pipe has been closed. Killing thread. */ | |
1326 | ret = check_thread_quit_pipe(pollfd, revents); | |
1327 | if (ret) { | |
1328 | goto error; | |
1329 | } | |
1330 | ||
1331 | /* Inspect the relay cmd pipe for new connection */ | |
1332 | if (pollfd == relay_cmd_pipe[0]) { | |
1333 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1334 | ERR("Relay pipe error"); | |
1335 | goto error; | |
1336 | } else if (revents & LPOLLIN) { | |
1337 | DBG("Relay command received"); | |
1338 | ret = relay_add_connection(relay_cmd_pipe[0], | |
1339 | &events, relay_connections_ht); | |
1340 | if (ret < 0) { | |
1341 | goto error; | |
1342 | } | |
1343 | } | |
1344 | } else if (revents > 0) { | |
1345 | lttng_ht_lookup(relay_connections_ht, | |
1346 | (void *)((unsigned long) pollfd), | |
1347 | &iter); | |
1348 | node = lttng_ht_iter_get_node_ulong(&iter); | |
1349 | if (node == NULL) { | |
1350 | DBG2("Relay sock %d not found", pollfd); | |
1351 | goto error; | |
1352 | } | |
1353 | relay_connection = caa_container_of(node, | |
1354 | struct relay_command, sock_n); | |
1355 | ||
1356 | if (revents & (LPOLLERR)) { | |
1357 | ERR("POLL ERROR"); | |
1358 | relay_cleanup_connection(relay_connections_ht, | |
1359 | &events, streams_ht, pollfd, &iter); | |
1360 | free(relay_connection); | |
1361 | } else if (revents & (LPOLLHUP | LPOLLRDHUP)) { | |
1362 | DBG("Socket %d hung up", pollfd); | |
1363 | relay_cleanup_connection(relay_connections_ht, | |
1364 | &events, streams_ht, pollfd, &iter); | |
1365 | if (relay_connection->type == RELAY_CONTROL) { | |
1366 | relay_delete_session(relay_connection, streams_ht); | |
1367 | } | |
1368 | free(relay_connection); | |
1369 | } else if (revents & LPOLLIN) { | |
1370 | /* control socket */ | |
1371 | if (relay_connection->type == RELAY_CONTROL) { | |
1372 | ret = relay_connection->sock->ops->recvmsg( | |
1373 | relay_connection->sock, &recv_hdr, | |
1374 | sizeof(struct lttcomm_relayd_hdr), MSG_WAITALL); | |
1375 | /* connection closed */ | |
1376 | if (ret <= 0) { | |
1377 | relay_cleanup_connection(relay_connections_ht, | |
1378 | &events, streams_ht, pollfd, &iter); | |
1379 | relay_delete_session(relay_connection, streams_ht); | |
1380 | free(relay_connection); | |
1381 | DBG("Control connection closed with %d", pollfd); | |
1382 | } else { | |
1383 | if (relay_connection->session) { | |
1384 | DBG2("Relay worker receiving data for session : %lu", | |
1385 | relay_connection->session->id); | |
1386 | } | |
1387 | ret = relay_process_control(&recv_hdr, | |
1388 | relay_connection, | |
1389 | streams_ht); | |
1390 | /* | |
1391 | * there was an error in processing a control | |
1392 | * command: clear the session | |
1393 | * */ | |
1394 | if (ret < 0) { | |
1395 | relay_cleanup_connection(relay_connections_ht, | |
1396 | &events, streams_ht, pollfd, &iter); | |
1397 | free(relay_connection); | |
1398 | DBG("Connection closed with %d", pollfd); | |
1399 | } | |
1400 | } | |
1401 | /* data socket */ | |
1402 | } else if (relay_connection->type == RELAY_DATA) { | |
1403 | ret = relay_process_data(relay_connection, streams_ht); | |
1404 | /* connection closed */ | |
1405 | if (ret < 0) { | |
1406 | relay_cleanup_connection(relay_connections_ht, | |
1407 | &events, streams_ht, pollfd, &iter); | |
1408 | relay_delete_session(relay_connection, streams_ht); | |
1409 | DBG("Data connection closed with %d", pollfd); | |
1410 | } | |
1411 | } | |
1412 | } | |
1413 | } | |
1414 | } | |
1415 | } | |
1416 | ||
1417 | error: | |
1418 | lttng_poll_clean(&events); | |
1419 | ||
1420 | /* empty the hash table and free the memory */ | |
1421 | cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) { | |
1422 | node = lttng_ht_iter_get_node_ulong(&iter); | |
1423 | if (node) { | |
1424 | relay_connection = caa_container_of(node, | |
1425 | struct relay_command, sock_n); | |
1426 | free(relay_connection); | |
1427 | } | |
1428 | ret = lttng_ht_del(relay_connections_ht, &iter); | |
1429 | assert(!ret); | |
1430 | } | |
1431 | error_poll_create: | |
1432 | free(data_buffer); | |
1433 | lttng_ht_destroy(relay_connections_ht); | |
1434 | DBG("Worker thread cleanup complete"); | |
1435 | stop_threads(); | |
1436 | return NULL; | |
1437 | } | |
1438 | ||
1439 | /* | |
1440 | * Create the relay command pipe to wake thread_manage_apps. | |
1441 | * Closed in cleanup(). | |
1442 | */ | |
1443 | static int create_relay_cmd_pipe(void) | |
1444 | { | |
1445 | int ret, i; | |
1446 | ||
1447 | ret = pipe(relay_cmd_pipe); | |
1448 | if (ret < 0) { | |
1449 | PERROR("relay cmd pipe"); | |
1450 | goto error; | |
1451 | } | |
1452 | ||
1453 | for (i = 0; i < 2; i++) { | |
1454 | ret = fcntl(relay_cmd_pipe[i], F_SETFD, FD_CLOEXEC); | |
1455 | if (ret < 0) { | |
1456 | PERROR("fcntl relay_cmd_pipe"); | |
1457 | goto error; | |
1458 | } | |
1459 | } | |
1460 | ||
1461 | error: | |
1462 | return ret; | |
1463 | } | |
1464 | ||
1465 | /* | |
1466 | * main | |
1467 | */ | |
1468 | int main(int argc, char **argv) | |
1469 | { | |
1470 | int ret = 0; | |
1471 | void *status; | |
1472 | ||
1473 | /* Create thread quit pipe */ | |
1474 | if ((ret = init_thread_quit_pipe()) < 0) { | |
1475 | goto error; | |
1476 | } | |
1477 | ||
1478 | /* Parse arguments */ | |
1479 | progname = argv[0]; | |
1480 | if ((ret = parse_args(argc, argv) < 0)) { | |
1481 | goto error; | |
1482 | } | |
1483 | ||
1484 | if ((ret = set_signal_handler()) < 0) { | |
1485 | goto exit; | |
1486 | } | |
1487 | ||
1488 | /* Daemonize */ | |
1489 | if (opt_daemon) { | |
1490 | ret = daemon(0, 0); | |
1491 | if (ret < 0) { | |
1492 | PERROR("daemon"); | |
1493 | goto error; | |
1494 | } | |
1495 | } | |
1496 | ||
1497 | /* Check if daemon is UID = 0 */ | |
1498 | is_root = !getuid(); | |
1499 | ||
1500 | if (!is_root) { | |
1501 | if (control_uri->port < 1024 || data_uri->port < 1024) { | |
1502 | ERR("Need to be root to use ports < 1024"); | |
1503 | ret = -1; | |
1504 | goto error; | |
1505 | } | |
1506 | } | |
1507 | ||
1508 | /* Setup the thread apps communication pipe. */ | |
1509 | if ((ret = create_relay_cmd_pipe()) < 0) { | |
1510 | goto exit; | |
1511 | } | |
1512 | ||
1513 | /* Init relay command queue. */ | |
1514 | cds_wfq_init(&relay_cmd_queue.queue); | |
1515 | ||
1516 | /* Set up max poll set size */ | |
1517 | lttng_poll_set_max_size(); | |
1518 | ||
1519 | /* Setup the dispatcher thread */ | |
1520 | ret = pthread_create(&dispatcher_thread, NULL, | |
1521 | relay_thread_dispatcher, (void *) NULL); | |
1522 | if (ret != 0) { | |
1523 | PERROR("pthread_create dispatcher"); | |
1524 | goto exit_dispatcher; | |
1525 | } | |
1526 | ||
1527 | /* Setup the worker thread */ | |
1528 | ret = pthread_create(&worker_thread, NULL, | |
1529 | relay_thread_worker, (void *) NULL); | |
1530 | if (ret != 0) { | |
1531 | PERROR("pthread_create worker"); | |
1532 | goto exit_worker; | |
1533 | } | |
1534 | ||
1535 | /* Setup the listener thread */ | |
1536 | ret = pthread_create(&listener_thread, NULL, | |
1537 | relay_thread_listener, (void *) NULL); | |
1538 | if (ret != 0) { | |
1539 | PERROR("pthread_create listener"); | |
1540 | goto exit_listener; | |
1541 | } | |
1542 | ||
1543 | exit_listener: | |
1544 | ret = pthread_join(listener_thread, &status); | |
1545 | if (ret != 0) { | |
1546 | PERROR("pthread_join"); | |
1547 | goto error; /* join error, exit without cleanup */ | |
1548 | } | |
1549 | ||
1550 | exit_worker: | |
1551 | ret = pthread_join(worker_thread, &status); | |
1552 | if (ret != 0) { | |
1553 | PERROR("pthread_join"); | |
1554 | goto error; /* join error, exit without cleanup */ | |
1555 | } | |
1556 | ||
1557 | exit_dispatcher: | |
1558 | ret = pthread_join(dispatcher_thread, &status); | |
1559 | if (ret != 0) { | |
1560 | PERROR("pthread_join"); | |
1561 | goto error; /* join error, exit without cleanup */ | |
1562 | } | |
1563 | ||
1564 | exit: | |
1565 | cleanup(); | |
1566 | if (!ret) { | |
1567 | exit(EXIT_SUCCESS); | |
1568 | } | |
1569 | error: | |
1570 | exit(EXIT_FAILURE); | |
1571 | } |