Fix: prioritize control socket communication in relayd
[lttng-tools.git] / src / bin / lttng-relayd / main.c
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 <inttypes.h>
36 #include <urcu/futex.h>
37 #include <urcu/uatomic.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <config.h>
41
42 #include <lttng/lttng.h>
43 #include <common/common.h>
44 #include <common/compat/poll.h>
45 #include <common/compat/socket.h>
46 #include <common/defaults.h>
47 #include <common/futex.h>
48 #include <common/sessiond-comm/sessiond-comm.h>
49 #include <common/sessiond-comm/inet.h>
50 #include <common/hashtable/hashtable.h>
51 #include <common/sessiond-comm/relayd.h>
52 #include <common/uri.h>
53 #include <common/utils.h>
54
55 #include "lttng-relayd.h"
56
57 /* command line options */
58 static int opt_daemon;
59 static char *opt_output_path;
60 static struct lttng_uri *control_uri;
61 static struct lttng_uri *data_uri;
62
63 const char *progname;
64 static int is_root; /* Set to 1 if the daemon is running as root */
65
66 /*
67 * Quit pipe for all threads. This permits a single cancellation point
68 * for all threads when receiving an event on the pipe.
69 */
70 static int thread_quit_pipe[2] = { -1, -1 };
71
72 /*
73 * This pipe is used to inform the worker thread that a command is queued and
74 * ready to be processed.
75 */
76 static int relay_cmd_pipe[2] = { -1, -1 };
77
78 /* Shared between threads */
79 static int dispatch_thread_exit;
80
81 static pthread_t listener_thread;
82 static pthread_t dispatcher_thread;
83 static pthread_t worker_thread;
84
85 static uint64_t last_relay_stream_id;
86 static uint64_t last_relay_session_id;
87
88 /*
89 * Relay command queue.
90 *
91 * The relay_thread_listener and relay_thread_dispatcher communicate with this
92 * queue.
93 */
94 static struct relay_cmd_queue relay_cmd_queue;
95
96 /* buffer allocated at startup, used to store the trace data */
97 static char *data_buffer;
98 static unsigned int data_buffer_size;
99
100 /*
101 * usage function on stderr
102 */
103 static
104 void usage(void)
105 {
106 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
107 fprintf(stderr, " -h, --help Display this usage.\n");
108 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
109 fprintf(stderr, " -C, --control-port Control port listening (URI)\n");
110 fprintf(stderr, " -D, --data-port Data port listening (URI)\n");
111 fprintf(stderr, " -o, --output Output path for traces (PATH)\n");
112 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
113 }
114
115 static
116 int parse_args(int argc, char **argv)
117 {
118 int c;
119 int ret = 0;
120 char *default_address;
121
122 static struct option long_options[] = {
123 { "control-port", 1, 0, 'C', },
124 { "data-port", 1, 0, 'D', },
125 { "daemonize", 0, 0, 'd', },
126 { "help", 0, 0, 'h', },
127 { "output", 1, 0, 'o', },
128 { "verbose", 0, 0, 'v', },
129 { NULL, 0, 0, 0, },
130 };
131
132 while (1) {
133 int option_index = 0;
134 c = getopt_long(argc, argv, "dhv" "C:D:o:",
135 long_options, &option_index);
136 if (c == -1) {
137 break;
138 }
139
140 switch (c) {
141 case 0:
142 fprintf(stderr, "option %s", long_options[option_index].name);
143 if (optarg) {
144 fprintf(stderr, " with arg %s\n", optarg);
145 }
146 break;
147 case 'C':
148 ret = uri_parse(optarg, &control_uri);
149 if (ret < 0) {
150 ERR("Invalid control URI specified");
151 goto exit;
152 }
153 if (control_uri->port == 0) {
154 control_uri->port = DEFAULT_NETWORK_CONTROL_PORT;
155 }
156 break;
157 case 'D':
158 ret = uri_parse(optarg, &data_uri);
159 if (ret < 0) {
160 ERR("Invalid data URI specified");
161 goto exit;
162 }
163 if (data_uri->port == 0) {
164 data_uri->port = DEFAULT_NETWORK_DATA_PORT;
165 }
166 break;
167 case 'd':
168 opt_daemon = 1;
169 break;
170 case 'h':
171 usage();
172 exit(EXIT_FAILURE);
173 case 'o':
174 ret = asprintf(&opt_output_path, "%s", optarg);
175 if (ret < 0) {
176 PERROR("asprintf opt_output_path");
177 goto exit;
178 }
179 break;
180 case 'v':
181 /* Verbose level can increase using multiple -v */
182 lttng_opt_verbose += 1;
183 break;
184 default:
185 /* Unknown option or other error.
186 * Error is printed by getopt, just return */
187 ret = -1;
188 goto exit;
189 }
190 }
191
192 /* assign default values */
193 if (control_uri == NULL) {
194 ret = asprintf(&default_address, "tcp://0.0.0.0:%d",
195 DEFAULT_NETWORK_CONTROL_PORT);
196 if (ret < 0) {
197 PERROR("asprintf default data address");
198 goto exit;
199 }
200
201 ret = uri_parse(default_address, &control_uri);
202 free(default_address);
203 if (ret < 0) {
204 ERR("Invalid control URI specified");
205 goto exit;
206 }
207 }
208 if (data_uri == NULL) {
209 ret = asprintf(&default_address, "tcp://0.0.0.0:%d",
210 DEFAULT_NETWORK_DATA_PORT);
211 if (ret < 0) {
212 PERROR("asprintf default data address");
213 goto exit;
214 }
215
216 ret = uri_parse(default_address, &data_uri);
217 free(default_address);
218 if (ret < 0) {
219 ERR("Invalid data URI specified");
220 goto exit;
221 }
222 }
223
224 exit:
225 return ret;
226 }
227
228 /*
229 * Cleanup the daemon
230 */
231 static
232 void cleanup(void)
233 {
234 DBG("Cleaning up");
235
236 /* free the dynamically allocated opt_output_path */
237 free(opt_output_path);
238
239 /* Close thread quit pipes */
240 utils_close_pipe(thread_quit_pipe);
241
242 uri_free(control_uri);
243 uri_free(data_uri);
244 }
245
246 /*
247 * Write to writable pipe used to notify a thread.
248 */
249 static
250 int notify_thread_pipe(int wpipe)
251 {
252 int ret;
253
254 do {
255 ret = write(wpipe, "!", 1);
256 } while (ret < 0 && errno == EINTR);
257 if (ret < 0) {
258 PERROR("write poll pipe");
259 }
260
261 return ret;
262 }
263
264 /*
265 * Stop all threads by closing the thread quit pipe.
266 */
267 static
268 void stop_threads(void)
269 {
270 int ret;
271
272 /* Stopping all threads */
273 DBG("Terminating all threads");
274 ret = notify_thread_pipe(thread_quit_pipe[1]);
275 if (ret < 0) {
276 ERR("write error on thread quit pipe");
277 }
278
279 /* Dispatch thread */
280 CMM_STORE_SHARED(dispatch_thread_exit, 1);
281 futex_nto1_wake(&relay_cmd_queue.futex);
282 }
283
284 /*
285 * Signal handler for the daemon
286 *
287 * Simply stop all worker threads, leaving main() return gracefully after
288 * joining all threads and calling cleanup().
289 */
290 static
291 void sighandler(int sig)
292 {
293 switch (sig) {
294 case SIGPIPE:
295 DBG("SIGPIPE caught");
296 return;
297 case SIGINT:
298 DBG("SIGINT caught");
299 stop_threads();
300 break;
301 case SIGTERM:
302 DBG("SIGTERM caught");
303 stop_threads();
304 break;
305 default:
306 break;
307 }
308 }
309
310 /*
311 * Setup signal handler for :
312 * SIGINT, SIGTERM, SIGPIPE
313 */
314 static
315 int set_signal_handler(void)
316 {
317 int ret = 0;
318 struct sigaction sa;
319 sigset_t sigset;
320
321 if ((ret = sigemptyset(&sigset)) < 0) {
322 PERROR("sigemptyset");
323 return ret;
324 }
325
326 sa.sa_handler = sighandler;
327 sa.sa_mask = sigset;
328 sa.sa_flags = 0;
329 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
330 PERROR("sigaction");
331 return ret;
332 }
333
334 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
335 PERROR("sigaction");
336 return ret;
337 }
338
339 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
340 PERROR("sigaction");
341 return ret;
342 }
343
344 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
345
346 return ret;
347 }
348
349 /*
350 * Init thread quit pipe.
351 *
352 * Return -1 on error or 0 if all pipes are created.
353 */
354 static
355 int init_thread_quit_pipe(void)
356 {
357 int ret;
358
359 ret = utils_create_pipe_cloexec(thread_quit_pipe);
360
361 return ret;
362 }
363
364 /*
365 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
366 */
367 static
368 int create_thread_poll_set(struct lttng_poll_event *events, int size)
369 {
370 int ret;
371
372 if (events == NULL || size == 0) {
373 ret = -1;
374 goto error;
375 }
376
377 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
378 if (ret < 0) {
379 goto error;
380 }
381
382 /* Add quit pipe */
383 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
384 if (ret < 0) {
385 goto error;
386 }
387
388 return 0;
389
390 error:
391 return ret;
392 }
393
394 /*
395 * Check if the thread quit pipe was triggered.
396 *
397 * Return 1 if it was triggered else 0;
398 */
399 static
400 int check_thread_quit_pipe(int fd, uint32_t events)
401 {
402 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
403 return 1;
404 }
405
406 return 0;
407 }
408
409 /*
410 * Create and init socket from uri.
411 */
412 static
413 struct lttcomm_sock *relay_init_sock(struct lttng_uri *uri)
414 {
415 int ret;
416 struct lttcomm_sock *sock = NULL;
417
418 sock = lttcomm_alloc_sock_from_uri(uri);
419 if (sock == NULL) {
420 ERR("Allocating socket");
421 goto error;
422 }
423
424 ret = lttcomm_create_sock(sock);
425 if (ret < 0) {
426 goto error;
427 }
428 DBG("Listening on sock %d", sock->fd);
429
430 ret = sock->ops->bind(sock);
431 if (ret < 0) {
432 goto error;
433 }
434
435 ret = sock->ops->listen(sock, -1);
436 if (ret < 0) {
437 goto error;
438
439 }
440
441 return sock;
442
443 error:
444 if (sock) {
445 lttcomm_destroy_sock(sock);
446 }
447 return NULL;
448 }
449
450 /*
451 * Return nonzero if stream needs to be closed.
452 */
453 static
454 int close_stream_check(struct relay_stream *stream)
455 {
456
457 if (stream->close_flag && stream->prev_seq == stream->last_net_seq_num) {
458 /*
459 * We are about to close the stream so set the data pending flag to 1
460 * which will make the end data pending command skip the stream which
461 * is now closed and ready. Note that after proceeding to a file close,
462 * the written file is ready for reading.
463 */
464 stream->data_pending_check_done = 1;
465 return 1;
466 }
467 return 0;
468 }
469
470 /*
471 * This thread manages the listening for new connections on the network
472 */
473 static
474 void *relay_thread_listener(void *data)
475 {
476 int i, ret, pollfd, err = -1;
477 int val = 1;
478 uint32_t revents, nb_fd;
479 struct lttng_poll_event events;
480 struct lttcomm_sock *control_sock, *data_sock;
481
482 DBG("[thread] Relay listener started");
483
484 control_sock = relay_init_sock(control_uri);
485 if (!control_sock) {
486 goto error_sock_control;
487 }
488
489 data_sock = relay_init_sock(data_uri);
490 if (!data_sock) {
491 goto error_sock_relay;
492 }
493
494 /*
495 * Pass 3 as size here for the thread quit pipe, control and data socket.
496 */
497 ret = create_thread_poll_set(&events, 3);
498 if (ret < 0) {
499 goto error_create_poll;
500 }
501
502 /* Add the control socket */
503 ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP);
504 if (ret < 0) {
505 goto error_poll_add;
506 }
507
508 /* Add the data socket */
509 ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP);
510 if (ret < 0) {
511 goto error_poll_add;
512 }
513
514 while (1) {
515 DBG("Listener accepting connections");
516
517 restart:
518 ret = lttng_poll_wait(&events, -1);
519 if (ret < 0) {
520 /*
521 * Restart interrupted system call.
522 */
523 if (errno == EINTR) {
524 goto restart;
525 }
526 goto error;
527 }
528
529 nb_fd = ret;
530
531 DBG("Relay new connection received");
532 for (i = 0; i < nb_fd; i++) {
533 /* Fetch once the poll data */
534 revents = LTTNG_POLL_GETEV(&events, i);
535 pollfd = LTTNG_POLL_GETFD(&events, i);
536
537 /* Thread quit pipe has been closed. Killing thread. */
538 ret = check_thread_quit_pipe(pollfd, revents);
539 if (ret) {
540 err = 0;
541 goto exit;
542 }
543
544 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
545 ERR("socket poll error");
546 goto error;
547 } else if (revents & LPOLLIN) {
548 /*
549 * Get allocated in this thread,
550 * enqueued to a global queue, dequeued
551 * and freed in the worker thread.
552 */
553 struct relay_command *relay_cmd;
554 struct lttcomm_sock *newsock;
555
556 relay_cmd = zmalloc(sizeof(struct relay_command));
557 if (relay_cmd == NULL) {
558 PERROR("relay command zmalloc");
559 goto error;
560 }
561
562 if (pollfd == data_sock->fd) {
563 newsock = data_sock->ops->accept(data_sock);
564 if (!newsock) {
565 PERROR("accepting data sock");
566 free(relay_cmd);
567 goto error;
568 }
569 relay_cmd->type = RELAY_DATA;
570 DBG("Relay data connection accepted, socket %d", newsock->fd);
571 } else {
572 assert(pollfd == control_sock->fd);
573 newsock = control_sock->ops->accept(control_sock);
574 if (!newsock) {
575 PERROR("accepting control sock");
576 free(relay_cmd);
577 goto error;
578 }
579 relay_cmd->type = RELAY_CONTROL;
580 DBG("Relay control connection accepted, socket %d", newsock->fd);
581 }
582 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR,
583 &val, sizeof(int));
584 if (ret < 0) {
585 PERROR("setsockopt inet");
586 lttcomm_destroy_sock(newsock);
587 free(relay_cmd);
588 goto error;
589 }
590 relay_cmd->sock = newsock;
591 /*
592 * Lock free enqueue the request.
593 */
594 cds_wfq_enqueue(&relay_cmd_queue.queue, &relay_cmd->node);
595
596 /*
597 * Wake the dispatch queue futex. Implicit memory
598 * barrier with the exchange in cds_wfq_enqueue.
599 */
600 futex_nto1_wake(&relay_cmd_queue.futex);
601 }
602 }
603 }
604
605 exit:
606 error:
607 error_poll_add:
608 lttng_poll_clean(&events);
609 error_create_poll:
610 if (data_sock->fd >= 0) {
611 ret = data_sock->ops->close(data_sock);
612 if (ret) {
613 PERROR("close");
614 }
615 }
616 lttcomm_destroy_sock(data_sock);
617 error_sock_relay:
618 if (control_sock->fd >= 0) {
619 ret = control_sock->ops->close(control_sock);
620 if (ret) {
621 PERROR("close");
622 }
623 }
624 lttcomm_destroy_sock(control_sock);
625 error_sock_control:
626 if (err) {
627 DBG("Thread exited with error");
628 }
629 DBG("Relay listener thread cleanup complete");
630 stop_threads();
631 return NULL;
632 }
633
634 /*
635 * This thread manages the dispatching of the requests to worker threads
636 */
637 static
638 void *relay_thread_dispatcher(void *data)
639 {
640 int ret;
641 struct cds_wfq_node *node;
642 struct relay_command *relay_cmd = NULL;
643
644 DBG("[thread] Relay dispatcher started");
645
646 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
647 /* Atomically prepare the queue futex */
648 futex_nto1_prepare(&relay_cmd_queue.futex);
649
650 do {
651 /* Dequeue commands */
652 node = cds_wfq_dequeue_blocking(&relay_cmd_queue.queue);
653 if (node == NULL) {
654 DBG("Woken up but nothing in the relay command queue");
655 /* Continue thread execution */
656 break;
657 }
658
659 relay_cmd = caa_container_of(node, struct relay_command, node);
660 DBG("Dispatching request waiting on sock %d", relay_cmd->sock->fd);
661
662 /*
663 * Inform worker thread of the new request. This
664 * call is blocking so we can be assured that the data will be read
665 * at some point in time or wait to the end of the world :)
666 */
667 do {
668 ret = write(relay_cmd_pipe[1], relay_cmd,
669 sizeof(struct relay_command));
670 } while (ret < 0 && errno == EINTR);
671 free(relay_cmd);
672 if (ret < 0) {
673 PERROR("write cmd pipe");
674 goto error;
675 }
676 } while (node != NULL);
677
678 /* Futex wait on queue. Blocking call on futex() */
679 futex_nto1_wait(&relay_cmd_queue.futex);
680 }
681
682 error:
683 DBG("Dispatch thread dying");
684 stop_threads();
685 return NULL;
686 }
687
688 /*
689 * Return the realpath(3) of the path even if the last directory token does not
690 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
691 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
692 * fails if the end point directory does not exist.
693 */
694 static
695 char *expand_full_path(const char *path)
696 {
697 const char *end_path = path;
698 char *next, *cut_path, *expanded_path, *respath;
699
700 /* Find last token delimited by '/' */
701 while ((next = strpbrk(end_path + 1, "/"))) {
702 end_path = next;
703 }
704
705 /* Cut last token from original path */
706 cut_path = strndup(path, end_path - path);
707
708 expanded_path = malloc(PATH_MAX);
709 if (expanded_path == NULL) {
710 respath = NULL;
711 goto end;
712 }
713
714 respath = realpath(cut_path, expanded_path);
715 if (respath == NULL) {
716 switch (errno) {
717 case ENOENT:
718 ERR("%s: No such file or directory", cut_path);
719 break;
720 default:
721 PERROR("realpath");
722 break;
723 }
724 free(expanded_path);
725 } else {
726 /* Add end part to expanded path */
727 strcat(respath, end_path);
728 }
729 end:
730 free(cut_path);
731 return respath;
732 }
733
734
735 /*
736 * config_get_default_path
737 *
738 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
739 */
740 static
741 char *config_get_default_path(void)
742 {
743 return getenv("HOME");
744 }
745
746 /*
747 * Create recursively directory using the FULL path.
748 */
749 static
750 int mkdir_recursive(char *path, mode_t mode)
751 {
752 char *p, tmp[PATH_MAX];
753 struct stat statbuf;
754 size_t len;
755 int ret;
756
757 ret = snprintf(tmp, sizeof(tmp), "%s", path);
758 if (ret < 0) {
759 PERROR("snprintf mkdir");
760 goto error;
761 }
762
763 len = ret;
764 if (tmp[len - 1] == '/') {
765 tmp[len - 1] = 0;
766 }
767
768 for (p = tmp + 1; *p; p++) {
769 if (*p == '/') {
770 *p = 0;
771 if (tmp[strlen(tmp) - 1] == '.' &&
772 tmp[strlen(tmp) - 2] == '.' &&
773 tmp[strlen(tmp) - 3] == '/') {
774 ERR("Using '/../' is not permitted in the trace path (%s)",
775 tmp);
776 ret = -1;
777 goto error;
778 }
779 ret = stat(tmp, &statbuf);
780 if (ret < 0) {
781 ret = mkdir(tmp, mode);
782 if (ret < 0) {
783 if (errno != EEXIST) {
784 PERROR("mkdir recursive");
785 ret = -errno;
786 goto error;
787 }
788 }
789 }
790 *p = '/';
791 }
792 }
793
794 ret = mkdir(tmp, mode);
795 if (ret < 0) {
796 if (errno != EEXIST) {
797 PERROR("mkdir recursive last piece");
798 ret = -errno;
799 } else {
800 ret = 0;
801 }
802 }
803
804 error:
805 return ret;
806 }
807
808 static
809 char *create_output_path_auto(char *path_name)
810 {
811 int ret;
812 char *traces_path = NULL;
813 char *alloc_path = NULL;
814 char *default_path;
815
816 default_path = config_get_default_path();
817 if (default_path == NULL) {
818 ERR("Home path not found.\n \
819 Please specify an output path using -o, --output PATH");
820 goto exit;
821 }
822 alloc_path = strdup(default_path);
823 if (alloc_path == NULL) {
824 PERROR("Path allocation");
825 goto exit;
826 }
827 ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME
828 "/%s", alloc_path, path_name);
829 if (ret < 0) {
830 PERROR("asprintf trace dir name");
831 goto exit;
832 }
833 exit:
834 free(alloc_path);
835 return traces_path;
836 }
837
838 static
839 char *create_output_path_noauto(char *path_name)
840 {
841 int ret;
842 char *traces_path = NULL;
843 char *full_path;
844
845 full_path = expand_full_path(opt_output_path);
846 ret = asprintf(&traces_path, "%s/%s", full_path, path_name);
847 if (ret < 0) {
848 PERROR("asprintf trace dir name");
849 goto exit;
850 }
851 exit:
852 free(full_path);
853 return traces_path;
854 }
855
856 /*
857 * create_output_path: create the output trace directory
858 */
859 static
860 char *create_output_path(char *path_name)
861 {
862 if (opt_output_path == NULL) {
863 return create_output_path_auto(path_name);
864 } else {
865 return create_output_path_noauto(path_name);
866 }
867 }
868
869 static
870 void deferred_free_stream(struct rcu_head *head)
871 {
872 struct relay_stream *stream =
873 caa_container_of(head, struct relay_stream, rcu_node);
874 free(stream);
875 }
876
877 /*
878 * relay_delete_session: Free all memory associated with a session and
879 * close all the FDs
880 */
881 static
882 void relay_delete_session(struct relay_command *cmd, struct lttng_ht *streams_ht)
883 {
884 struct lttng_ht_iter iter;
885 struct lttng_ht_node_ulong *node;
886 struct relay_stream *stream;
887 int ret;
888
889 if (!cmd->session) {
890 return;
891 }
892
893 DBG("Relay deleting session %" PRIu64, cmd->session->id);
894
895 rcu_read_lock();
896 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, node, node) {
897 node = lttng_ht_iter_get_node_ulong(&iter);
898 if (node) {
899 stream = caa_container_of(node,
900 struct relay_stream, stream_n);
901 if (stream->session == cmd->session) {
902 ret = close(stream->fd);
903 if (ret < 0) {
904 PERROR("close stream fd on delete session");
905 }
906 ret = lttng_ht_del(streams_ht, &iter);
907 assert(!ret);
908 call_rcu(&stream->rcu_node,
909 deferred_free_stream);
910 }
911 }
912 }
913 rcu_read_unlock();
914
915 free(cmd->session);
916 }
917
918 /*
919 * Handle the RELAYD_CREATE_SESSION command.
920 *
921 * On success, send back the session id or else return a negative value.
922 */
923 static
924 int relay_create_session(struct lttcomm_relayd_hdr *recv_hdr,
925 struct relay_command *cmd)
926 {
927 int ret = 0, send_ret;
928 struct relay_session *session;
929 struct lttcomm_relayd_status_session reply;
930
931 assert(recv_hdr);
932 assert(cmd);
933
934 memset(&reply, 0, sizeof(reply));
935
936 session = zmalloc(sizeof(struct relay_session));
937 if (session == NULL) {
938 PERROR("relay session zmalloc");
939 ret = -1;
940 goto error;
941 }
942
943 session->id = ++last_relay_session_id;
944 session->sock = cmd->sock;
945 cmd->session = session;
946
947 reply.session_id = htobe64(session->id);
948
949 DBG("Created session %" PRIu64, session->id);
950
951 error:
952 if (ret < 0) {
953 reply.ret_code = htobe32(LTTNG_ERR_FATAL);
954 } else {
955 reply.ret_code = htobe32(LTTNG_OK);
956 }
957
958 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
959 if (send_ret < 0) {
960 ERR("Relayd sending session id");
961 }
962
963 return ret;
964 }
965
966 /*
967 * relay_add_stream: allocate a new stream for a session
968 */
969 static
970 int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
971 struct relay_command *cmd, struct lttng_ht *streams_ht)
972 {
973 struct relay_session *session = cmd->session;
974 struct lttcomm_relayd_add_stream stream_info;
975 struct relay_stream *stream = NULL;
976 struct lttcomm_relayd_status_stream reply;
977 char *path = NULL, *root_path = NULL;
978 int ret, send_ret;
979
980 if (!session || cmd->version_check_done == 0) {
981 ERR("Trying to add a stream before version check");
982 ret = -1;
983 goto end_no_session;
984 }
985
986 /* FIXME : use data_size for something ? */
987 ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
988 sizeof(struct lttcomm_relayd_add_stream), 0);
989 if (ret < sizeof(struct lttcomm_relayd_add_stream)) {
990 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
991 ret = -1;
992 goto end_no_session;
993 }
994 stream = zmalloc(sizeof(struct relay_stream));
995 if (stream == NULL) {
996 PERROR("relay stream zmalloc");
997 ret = -1;
998 goto end_no_session;
999 }
1000
1001 rcu_read_lock();
1002 stream->stream_handle = ++last_relay_stream_id;
1003 stream->prev_seq = -1ULL;
1004 stream->session = session;
1005
1006 root_path = create_output_path(stream_info.pathname);
1007 if (!root_path) {
1008 ret = -1;
1009 goto end;
1010 }
1011 ret = mkdir_recursive(root_path, S_IRWXU | S_IRWXG);
1012 if (ret < 0) {
1013 ERR("relay creating output directory");
1014 goto end;
1015 }
1016
1017 ret = asprintf(&path, "%s/%s", root_path, stream_info.channel_name);
1018 if (ret < 0) {
1019 PERROR("asprintf stream path");
1020 goto end;
1021 }
1022
1023 ret = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
1024 if (ret < 0) {
1025 PERROR("Relay creating trace file");
1026 goto end;
1027 }
1028
1029 stream->fd = ret;
1030 DBG("Tracefile %s created", path);
1031
1032 lttng_ht_node_init_ulong(&stream->stream_n,
1033 (unsigned long) stream->stream_handle);
1034 lttng_ht_add_unique_ulong(streams_ht,
1035 &stream->stream_n);
1036
1037 DBG("Relay new stream added %s", stream_info.channel_name);
1038
1039 end:
1040 free(path);
1041 free(root_path);
1042 /* send the session id to the client or a negative return code on error */
1043 if (ret < 0) {
1044 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1045 } else {
1046 reply.ret_code = htobe32(LTTNG_OK);
1047 }
1048 reply.handle = htobe64(stream->stream_handle);
1049 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1050 sizeof(struct lttcomm_relayd_status_stream), 0);
1051 if (send_ret < 0) {
1052 ERR("Relay sending stream id");
1053 }
1054 rcu_read_unlock();
1055
1056 end_no_session:
1057 return ret;
1058 }
1059
1060 /*
1061 * relay_close_stream: close a specific stream
1062 */
1063 static
1064 int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
1065 struct relay_command *cmd, struct lttng_ht *streams_ht)
1066 {
1067 struct relay_session *session = cmd->session;
1068 struct lttcomm_relayd_close_stream stream_info;
1069 struct lttcomm_relayd_generic_reply reply;
1070 struct relay_stream *stream;
1071 int ret, send_ret;
1072 struct lttng_ht_node_ulong *node;
1073 struct lttng_ht_iter iter;
1074
1075 DBG("Close stream received");
1076
1077 if (!session || cmd->version_check_done == 0) {
1078 ERR("Trying to close a stream before version check");
1079 ret = -1;
1080 goto end_no_session;
1081 }
1082
1083 ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
1084 sizeof(struct lttcomm_relayd_close_stream), 0);
1085 if (ret < sizeof(struct lttcomm_relayd_close_stream)) {
1086 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
1087 ret = -1;
1088 goto end_no_session;
1089 }
1090
1091 rcu_read_lock();
1092 lttng_ht_lookup(streams_ht,
1093 (void *)((unsigned long) be64toh(stream_info.stream_id)),
1094 &iter);
1095 node = lttng_ht_iter_get_node_ulong(&iter);
1096 if (node == NULL) {
1097 DBG("Relay stream %" PRIu64 " not found", be64toh(stream_info.stream_id));
1098 ret = -1;
1099 goto end_unlock;
1100 }
1101
1102 stream = caa_container_of(node, struct relay_stream, stream_n);
1103 if (!stream) {
1104 ret = -1;
1105 goto end_unlock;
1106 }
1107
1108 stream->last_net_seq_num = be64toh(stream_info.last_net_seq_num);
1109 stream->close_flag = 1;
1110
1111 if (close_stream_check(stream)) {
1112 int delret;
1113
1114 delret = close(stream->fd);
1115 if (delret < 0) {
1116 PERROR("close stream");
1117 }
1118 delret = lttng_ht_del(streams_ht, &iter);
1119 assert(!delret);
1120 call_rcu(&stream->rcu_node,
1121 deferred_free_stream);
1122 DBG("Closed tracefile %d from close stream", stream->fd);
1123 }
1124
1125 end_unlock:
1126 rcu_read_unlock();
1127
1128 if (ret < 0) {
1129 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1130 } else {
1131 reply.ret_code = htobe32(LTTNG_OK);
1132 }
1133 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1134 sizeof(struct lttcomm_relayd_generic_reply), 0);
1135 if (send_ret < 0) {
1136 ERR("Relay sending stream id");
1137 }
1138
1139 end_no_session:
1140 return ret;
1141 }
1142
1143 /*
1144 * relay_unknown_command: send -1 if received unknown command
1145 */
1146 static
1147 void relay_unknown_command(struct relay_command *cmd)
1148 {
1149 struct lttcomm_relayd_generic_reply reply;
1150 int ret;
1151
1152 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1153 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1154 sizeof(struct lttcomm_relayd_generic_reply), 0);
1155 if (ret < 0) {
1156 ERR("Relay sending unknown command");
1157 }
1158 }
1159
1160 /*
1161 * relay_start: send an acknowledgment to the client to tell if we are
1162 * ready to receive data. We are ready if a session is established.
1163 */
1164 static
1165 int relay_start(struct lttcomm_relayd_hdr *recv_hdr,
1166 struct relay_command *cmd)
1167 {
1168 int ret = htobe32(LTTNG_OK);
1169 struct lttcomm_relayd_generic_reply reply;
1170 struct relay_session *session = cmd->session;
1171
1172 if (!session) {
1173 DBG("Trying to start the streaming without a session established");
1174 ret = htobe32(LTTNG_ERR_UNK);
1175 }
1176
1177 reply.ret_code = ret;
1178 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1179 sizeof(struct lttcomm_relayd_generic_reply), 0);
1180 if (ret < 0) {
1181 ERR("Relay sending start ack");
1182 }
1183
1184 return ret;
1185 }
1186
1187 /*
1188 * Get stream from stream id.
1189 * Need to be called with RCU read-side lock held.
1190 */
1191 static
1192 struct relay_stream *relay_stream_from_stream_id(uint64_t stream_id,
1193 struct lttng_ht *streams_ht)
1194 {
1195 struct lttng_ht_node_ulong *node;
1196 struct lttng_ht_iter iter;
1197 struct relay_stream *ret;
1198
1199 lttng_ht_lookup(streams_ht,
1200 (void *)((unsigned long) stream_id),
1201 &iter);
1202 node = lttng_ht_iter_get_node_ulong(&iter);
1203 if (node == NULL) {
1204 DBG("Relay stream %" PRIu64 " not found", stream_id);
1205 ret = NULL;
1206 goto end;
1207 }
1208
1209 ret = caa_container_of(node, struct relay_stream, stream_n);
1210
1211 end:
1212 return ret;
1213 }
1214
1215 /*
1216 * Append padding to the file pointed by the file descriptor fd.
1217 */
1218 static int write_padding_to_file(int fd, uint32_t size)
1219 {
1220 int ret = 0;
1221 char *zeros;
1222
1223 if (size == 0) {
1224 goto end;
1225 }
1226
1227 zeros = zmalloc(size);
1228 if (zeros == NULL) {
1229 PERROR("zmalloc zeros for padding");
1230 ret = -1;
1231 goto end;
1232 }
1233
1234 do {
1235 ret = write(fd, zeros, size);
1236 } while (ret < 0 && errno == EINTR);
1237 if (ret < 0) {
1238 PERROR("write padding to file");
1239 }
1240
1241 free(zeros);
1242
1243 end:
1244 return ret;
1245 }
1246
1247 /*
1248 * relay_recv_metadata: receive the metada for the session.
1249 */
1250 static
1251 int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr,
1252 struct relay_command *cmd, struct lttng_ht *streams_ht)
1253 {
1254 int ret = htobe32(LTTNG_OK);
1255 struct relay_session *session = cmd->session;
1256 struct lttcomm_relayd_metadata_payload *metadata_struct;
1257 struct relay_stream *metadata_stream;
1258 uint64_t data_size, payload_size;
1259
1260 if (!session) {
1261 ERR("Metadata sent before version check");
1262 ret = -1;
1263 goto end;
1264 }
1265
1266 data_size = payload_size = be64toh(recv_hdr->data_size);
1267 if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) {
1268 ERR("Incorrect data size");
1269 ret = -1;
1270 goto end;
1271 }
1272 payload_size -= sizeof(struct lttcomm_relayd_metadata_payload);
1273
1274 if (data_buffer_size < data_size) {
1275 /* In case the realloc fails, we can free the memory */
1276 char *tmp_data_ptr;
1277
1278 tmp_data_ptr = realloc(data_buffer, data_size);
1279 if (!tmp_data_ptr) {
1280 ERR("Allocating data buffer");
1281 free(data_buffer);
1282 ret = -1;
1283 goto end;
1284 }
1285 data_buffer = tmp_data_ptr;
1286 data_buffer_size = data_size;
1287 }
1288 memset(data_buffer, 0, data_size);
1289 DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size);
1290 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
1291 if (ret < 0 || ret != data_size) {
1292 ret = -1;
1293 ERR("Relay didn't receive the whole metadata");
1294 goto end;
1295 }
1296 metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer;
1297
1298 rcu_read_lock();
1299 metadata_stream = relay_stream_from_stream_id(
1300 be64toh(metadata_struct->stream_id), streams_ht);
1301 if (!metadata_stream) {
1302 ret = -1;
1303 goto end_unlock;
1304 }
1305
1306 do {
1307 ret = write(metadata_stream->fd, metadata_struct->payload,
1308 payload_size);
1309 } while (ret < 0 && errno == EINTR);
1310 if (ret < payload_size) {
1311 ERR("Relay error writing metadata on file");
1312 ret = -1;
1313 goto end_unlock;
1314 }
1315
1316 ret = write_padding_to_file(metadata_stream->fd,
1317 be32toh(metadata_struct->padding_size));
1318 if (ret < 0) {
1319 goto end_unlock;
1320 }
1321
1322 DBG2("Relay metadata written");
1323
1324 end_unlock:
1325 rcu_read_unlock();
1326 end:
1327 return ret;
1328 }
1329
1330 /*
1331 * relay_send_version: send relayd version number
1332 */
1333 static
1334 int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
1335 struct relay_command *cmd)
1336 {
1337 int ret;
1338 struct lttcomm_relayd_version reply, msg;
1339
1340 assert(cmd);
1341
1342 cmd->version_check_done = 1;
1343
1344 /* Get version from the other side. */
1345 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1346 if (ret < 0 || ret != sizeof(msg)) {
1347 ret = -1;
1348 ERR("Relay failed to receive the version values.");
1349 goto end;
1350 }
1351
1352 /*
1353 * For now, we just ignore the received version but after 2.1 stable
1354 * release, a check must be done to see if we either adapt to the other
1355 * side version (which MUST be lower than us) or keep the latest data
1356 * structure considering that the other side will adapt.
1357 */
1358
1359 ret = sscanf(VERSION, "%10u.%10u", &reply.major, &reply.minor);
1360 if (ret < 2) {
1361 ERR("Error in scanning version");
1362 ret = -1;
1363 goto end;
1364 }
1365 reply.major = htobe32(reply.major);
1366 reply.minor = htobe32(reply.minor);
1367 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1368 sizeof(struct lttcomm_relayd_version), 0);
1369 if (ret < 0) {
1370 ERR("Relay sending version");
1371 }
1372 DBG("Version check done (%u.%u)", be32toh(reply.major),
1373 be32toh(reply.minor));
1374
1375 end:
1376 return ret;
1377 }
1378
1379 /*
1380 * Check for data pending for a given stream id from the session daemon.
1381 */
1382 static
1383 int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
1384 struct relay_command *cmd, struct lttng_ht *streams_ht)
1385 {
1386 struct relay_session *session = cmd->session;
1387 struct lttcomm_relayd_data_pending msg;
1388 struct lttcomm_relayd_generic_reply reply;
1389 struct relay_stream *stream;
1390 int ret;
1391 struct lttng_ht_node_ulong *node;
1392 struct lttng_ht_iter iter;
1393 uint64_t last_net_seq_num, stream_id;
1394
1395 DBG("Data pending command received");
1396
1397 if (!session || cmd->version_check_done == 0) {
1398 ERR("Trying to check for data before version check");
1399 ret = -1;
1400 goto end_no_session;
1401 }
1402
1403 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1404 if (ret < sizeof(msg)) {
1405 ERR("Relay didn't receive valid data_pending struct size : %d", ret);
1406 ret = -1;
1407 goto end_no_session;
1408 }
1409
1410 stream_id = be64toh(msg.stream_id);
1411 last_net_seq_num = be64toh(msg.last_net_seq_num);
1412
1413 rcu_read_lock();
1414 lttng_ht_lookup(streams_ht, (void *)((unsigned long) stream_id), &iter);
1415 node = lttng_ht_iter_get_node_ulong(&iter);
1416 if (node == NULL) {
1417 DBG("Relay stream %" PRIu64 " not found", stream_id);
1418 ret = -1;
1419 goto end_unlock;
1420 }
1421
1422 stream = caa_container_of(node, struct relay_stream, stream_n);
1423 assert(stream);
1424
1425 DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64
1426 " and last_seq %" PRIu64, stream_id, stream->prev_seq,
1427 last_net_seq_num);
1428
1429 /* Avoid wrapping issue */
1430 if (((int64_t) (stream->prev_seq - last_net_seq_num)) <= 0) {
1431 /* Data has in fact been written and is NOT pending */
1432 ret = 0;
1433 } else {
1434 /* Data still being streamed thus pending */
1435 ret = 1;
1436 }
1437
1438 /* Pending check is now done. */
1439 stream->data_pending_check_done = 1;
1440
1441 end_unlock:
1442 rcu_read_unlock();
1443
1444 reply.ret_code = htobe32(ret);
1445 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1446 if (ret < 0) {
1447 ERR("Relay data pending ret code failed");
1448 }
1449
1450 end_no_session:
1451 return ret;
1452 }
1453
1454 /*
1455 * Wait for the control socket to reach a quiescent state.
1456 *
1457 * Note that for now, when receiving this command from the session daemon, this
1458 * means that every subsequent commands or data received on the control socket
1459 * has been handled. So, this is why we simply return OK here.
1460 */
1461 static
1462 int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr,
1463 struct relay_command *cmd)
1464 {
1465 int ret;
1466 struct lttcomm_relayd_generic_reply reply;
1467
1468 DBG("Checking quiescent state on control socket");
1469
1470 reply.ret_code = htobe32(LTTNG_OK);
1471 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1472 if (ret < 0) {
1473 ERR("Relay data quiescent control ret code failed");
1474 }
1475
1476 return ret;
1477 }
1478
1479 /*
1480 * Initialize a data pending command. This means that a client is about to ask
1481 * for data pending for each stream he/she holds. Simply iterate over all
1482 * streams of a session and set the data_pending_check_done flag.
1483 *
1484 * This command returns to the client a LTTNG_OK code.
1485 */
1486 static
1487 int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
1488 struct relay_command *cmd, struct lttng_ht *streams_ht)
1489 {
1490 int ret;
1491 struct lttng_ht_iter iter;
1492 struct lttcomm_relayd_begin_data_pending msg;
1493 struct lttcomm_relayd_generic_reply reply;
1494 struct relay_stream *stream;
1495 uint64_t session_id;
1496
1497 assert(recv_hdr);
1498 assert(cmd);
1499 assert(streams_ht);
1500
1501 DBG("Init streams for data pending");
1502
1503 if (!cmd->session || cmd->version_check_done == 0) {
1504 ERR("Trying to check for data before version check");
1505 ret = -1;
1506 goto end_no_session;
1507 }
1508
1509 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1510 if (ret < sizeof(msg)) {
1511 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1512 ret);
1513 ret = -1;
1514 goto end_no_session;
1515 }
1516
1517 session_id = be64toh(msg.session_id);
1518
1519 /*
1520 * Iterate over all streams to set the begin data pending flag. For now, the
1521 * streams are indexed by stream handle so we have to iterate over all
1522 * streams to find the one associated with the right session_id.
1523 */
1524 rcu_read_lock();
1525 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, stream, stream_n.node) {
1526 if (stream->session->id == session_id) {
1527 stream->data_pending_check_done = 0;
1528 DBG("Set begin data pending flag to stream %" PRIu64,
1529 stream->stream_handle);
1530 }
1531 }
1532 rcu_read_unlock();
1533
1534 /* All good, send back reply. */
1535 reply.ret_code = htobe32(LTTNG_OK);
1536
1537 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1538 if (ret < 0) {
1539 ERR("Relay begin data pending send reply failed");
1540 }
1541
1542 end_no_session:
1543 return ret;
1544 }
1545
1546 /*
1547 * End data pending command. This will check, for a given session id, if each
1548 * stream associated with it has its data_pending_check_done flag set. If not,
1549 * this means that the client lost track of the stream but the data is still
1550 * being streamed on our side. In this case, we inform the client that data is
1551 * inflight.
1552 *
1553 * Return to the client if there is data in flight or not with a ret_code.
1554 */
1555 static
1556 int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
1557 struct relay_command *cmd, struct lttng_ht *streams_ht)
1558 {
1559 int ret;
1560 struct lttng_ht_iter iter;
1561 struct lttcomm_relayd_end_data_pending msg;
1562 struct lttcomm_relayd_generic_reply reply;
1563 struct relay_stream *stream;
1564 uint64_t session_id;
1565 uint32_t is_data_inflight = 0;
1566
1567 assert(recv_hdr);
1568 assert(cmd);
1569 assert(streams_ht);
1570
1571 DBG("End data pending command");
1572
1573 if (!cmd->session || cmd->version_check_done == 0) {
1574 ERR("Trying to check for data before version check");
1575 ret = -1;
1576 goto end_no_session;
1577 }
1578
1579 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1580 if (ret < sizeof(msg)) {
1581 ERR("Relay didn't receive valid end data_pending struct size: %d",
1582 ret);
1583 ret = -1;
1584 goto end_no_session;
1585 }
1586
1587 session_id = be64toh(msg.session_id);
1588
1589 /* Iterate over all streams to see if the begin data pending flag is set. */
1590 rcu_read_lock();
1591 cds_lfht_for_each_entry(streams_ht->ht, &iter.iter, stream, stream_n.node) {
1592 if (stream->session->id == session_id &&
1593 !stream->data_pending_check_done) {
1594 is_data_inflight = 1;
1595 DBG("Data is still in flight for stream %" PRIu64,
1596 stream->stream_handle);
1597 break;
1598 }
1599 }
1600 rcu_read_unlock();
1601
1602 /* All good, send back reply. */
1603 reply.ret_code = htobe32(is_data_inflight);
1604
1605 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1606 if (ret < 0) {
1607 ERR("Relay end data pending send reply failed");
1608 }
1609
1610 end_no_session:
1611 return ret;
1612 }
1613
1614 /*
1615 * relay_process_control: Process the commands received on the control socket
1616 */
1617 static
1618 int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
1619 struct relay_command *cmd, struct lttng_ht *streams_ht)
1620 {
1621 int ret = 0;
1622
1623 switch (be32toh(recv_hdr->cmd)) {
1624 case RELAYD_CREATE_SESSION:
1625 ret = relay_create_session(recv_hdr, cmd);
1626 break;
1627 case RELAYD_ADD_STREAM:
1628 ret = relay_add_stream(recv_hdr, cmd, streams_ht);
1629 break;
1630 case RELAYD_START_DATA:
1631 ret = relay_start(recv_hdr, cmd);
1632 break;
1633 case RELAYD_SEND_METADATA:
1634 ret = relay_recv_metadata(recv_hdr, cmd, streams_ht);
1635 break;
1636 case RELAYD_VERSION:
1637 ret = relay_send_version(recv_hdr, cmd);
1638 break;
1639 case RELAYD_CLOSE_STREAM:
1640 ret = relay_close_stream(recv_hdr, cmd, streams_ht);
1641 break;
1642 case RELAYD_DATA_PENDING:
1643 ret = relay_data_pending(recv_hdr, cmd, streams_ht);
1644 break;
1645 case RELAYD_QUIESCENT_CONTROL:
1646 ret = relay_quiescent_control(recv_hdr, cmd);
1647 break;
1648 case RELAYD_BEGIN_DATA_PENDING:
1649 ret = relay_begin_data_pending(recv_hdr, cmd, streams_ht);
1650 break;
1651 case RELAYD_END_DATA_PENDING:
1652 ret = relay_end_data_pending(recv_hdr, cmd, streams_ht);
1653 break;
1654 case RELAYD_UPDATE_SYNC_INFO:
1655 default:
1656 ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd));
1657 relay_unknown_command(cmd);
1658 ret = -1;
1659 goto end;
1660 }
1661
1662 end:
1663 return ret;
1664 }
1665
1666 /*
1667 * relay_process_data: Process the data received on the data socket
1668 */
1669 static
1670 int relay_process_data(struct relay_command *cmd, struct lttng_ht *streams_ht)
1671 {
1672 int ret = 0;
1673 struct relay_stream *stream;
1674 struct lttcomm_relayd_data_hdr data_hdr;
1675 uint64_t stream_id;
1676 uint64_t net_seq_num;
1677 uint32_t data_size;
1678
1679 ret = cmd->sock->ops->recvmsg(cmd->sock, &data_hdr,
1680 sizeof(struct lttcomm_relayd_data_hdr), 0);
1681 if (ret <= 0) {
1682 ERR("Connections seems to be closed");
1683 ret = -1;
1684 goto end;
1685 }
1686
1687 stream_id = be64toh(data_hdr.stream_id);
1688
1689 rcu_read_lock();
1690 stream = relay_stream_from_stream_id(stream_id, streams_ht);
1691 if (!stream) {
1692 ret = -1;
1693 goto end_unlock;
1694 }
1695
1696 data_size = be32toh(data_hdr.data_size);
1697 if (data_buffer_size < data_size) {
1698 char *tmp_data_ptr;
1699
1700 tmp_data_ptr = realloc(data_buffer, data_size);
1701 if (!tmp_data_ptr) {
1702 ERR("Allocating data buffer");
1703 free(data_buffer);
1704 ret = -1;
1705 goto end_unlock;
1706 }
1707 data_buffer = tmp_data_ptr;
1708 data_buffer_size = data_size;
1709 }
1710 memset(data_buffer, 0, data_size);
1711
1712 net_seq_num = be64toh(data_hdr.net_seq_num);
1713
1714 DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64,
1715 data_size, stream_id, net_seq_num);
1716 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
1717 if (ret <= 0) {
1718 ret = -1;
1719 goto end_unlock;
1720 }
1721
1722 do {
1723 ret = write(stream->fd, data_buffer, data_size);
1724 } while (ret < 0 && errno == EINTR);
1725 if (ret < data_size) {
1726 ERR("Relay error writing data to file");
1727 ret = -1;
1728 goto end_unlock;
1729 }
1730
1731 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64,
1732 ret, stream->stream_handle);
1733
1734 ret = write_padding_to_file(stream->fd, be32toh(data_hdr.padding_size));
1735 if (ret < 0) {
1736 goto end_unlock;
1737 }
1738
1739 stream->prev_seq = net_seq_num;
1740
1741 /* Check if we need to close the FD */
1742 if (close_stream_check(stream)) {
1743 int cret;
1744 struct lttng_ht_iter iter;
1745
1746 cret = close(stream->fd);
1747 if (cret < 0) {
1748 PERROR("close stream process data");
1749 }
1750 iter.iter.node = &stream->stream_n.node;
1751 ret = lttng_ht_del(streams_ht, &iter);
1752 assert(!ret);
1753 call_rcu(&stream->rcu_node,
1754 deferred_free_stream);
1755 DBG("Closed tracefile %d after recv data", stream->fd);
1756 }
1757
1758 end_unlock:
1759 rcu_read_unlock();
1760 end:
1761 return ret;
1762 }
1763
1764 static
1765 void relay_cleanup_poll_connection(struct lttng_poll_event *events, int pollfd)
1766 {
1767 int ret;
1768
1769 lttng_poll_del(events, pollfd);
1770
1771 ret = close(pollfd);
1772 if (ret < 0) {
1773 ERR("Closing pollfd %d", pollfd);
1774 }
1775 }
1776
1777 static
1778 int relay_add_connection(int fd, struct lttng_poll_event *events,
1779 struct lttng_ht *relay_connections_ht)
1780 {
1781 struct relay_command *relay_connection;
1782 int ret;
1783
1784 relay_connection = zmalloc(sizeof(struct relay_command));
1785 if (relay_connection == NULL) {
1786 PERROR("Relay command zmalloc");
1787 goto error;
1788 }
1789 ret = read(fd, relay_connection, sizeof(struct relay_command));
1790 if (ret < 0 || ret < sizeof(struct relay_command)) {
1791 PERROR("read relay cmd pipe");
1792 goto error_read;
1793 }
1794
1795 lttng_ht_node_init_ulong(&relay_connection->sock_n,
1796 (unsigned long) relay_connection->sock->fd);
1797 rcu_read_lock();
1798 lttng_ht_add_unique_ulong(relay_connections_ht,
1799 &relay_connection->sock_n);
1800 rcu_read_unlock();
1801 return lttng_poll_add(events,
1802 relay_connection->sock->fd,
1803 LPOLLIN | LPOLLRDHUP);
1804
1805 error_read:
1806 free(relay_connection);
1807 error:
1808 return -1;
1809 }
1810
1811 static
1812 void deferred_free_connection(struct rcu_head *head)
1813 {
1814 struct relay_command *relay_connection =
1815 caa_container_of(head, struct relay_command, rcu_node);
1816
1817 lttcomm_destroy_sock(relay_connection->sock);
1818 free(relay_connection);
1819 }
1820
1821 static
1822 void relay_del_connection(struct lttng_ht *relay_connections_ht,
1823 struct lttng_ht *streams_ht, struct lttng_ht_iter *iter,
1824 struct relay_command *relay_connection)
1825 {
1826 int ret;
1827
1828 ret = lttng_ht_del(relay_connections_ht, iter);
1829 assert(!ret);
1830 if (relay_connection->type == RELAY_CONTROL) {
1831 relay_delete_session(relay_connection, streams_ht);
1832 }
1833
1834 call_rcu(&relay_connection->rcu_node,
1835 deferred_free_connection);
1836 }
1837
1838 /*
1839 * This thread does the actual work
1840 */
1841 static
1842 void *relay_thread_worker(void *data)
1843 {
1844 int ret, err = -1, last_seen_data_fd = -1;
1845 uint32_t nb_fd;
1846 struct relay_command *relay_connection;
1847 struct lttng_poll_event events;
1848 struct lttng_ht *relay_connections_ht;
1849 struct lttng_ht_node_ulong *node;
1850 struct lttng_ht_iter iter;
1851 struct lttng_ht *streams_ht;
1852 struct lttcomm_relayd_hdr recv_hdr;
1853
1854 DBG("[thread] Relay worker started");
1855
1856 rcu_register_thread();
1857
1858 /* table of connections indexed on socket */
1859 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1860 if (!relay_connections_ht) {
1861 goto relay_connections_ht_error;
1862 }
1863
1864 /* tables of streams indexed by stream ID */
1865 streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1866 if (!streams_ht) {
1867 goto streams_ht_error;
1868 }
1869
1870 ret = create_thread_poll_set(&events, 2);
1871 if (ret < 0) {
1872 goto error_poll_create;
1873 }
1874
1875 ret = lttng_poll_add(&events, relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1876 if (ret < 0) {
1877 goto error;
1878 }
1879
1880 restart:
1881 while (1) {
1882 int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1;
1883
1884 /* Infinite blocking call, waiting for transmission */
1885 DBG3("Relayd worker thread polling...");
1886 ret = lttng_poll_wait(&events, -1);
1887 if (ret < 0) {
1888 /*
1889 * Restart interrupted system call.
1890 */
1891 if (errno == EINTR) {
1892 goto restart;
1893 }
1894 goto error;
1895 }
1896
1897 nb_fd = ret;
1898
1899 /*
1900 * Process control. The control connection is prioritised so we don't
1901 * starve it with high throughout put tracing data on the data
1902 * connection.
1903 */
1904 for (i = 0; i < nb_fd; i++) {
1905 /* Fetch once the poll data */
1906 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1907 int pollfd = LTTNG_POLL_GETFD(&events, i);
1908
1909 /* Thread quit pipe has been closed. Killing thread. */
1910 ret = check_thread_quit_pipe(pollfd, revents);
1911 if (ret) {
1912 err = 0;
1913 goto exit;
1914 }
1915
1916 /* Inspect the relay cmd pipe for new connection */
1917 if (pollfd == relay_cmd_pipe[0]) {
1918 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1919 ERR("Relay pipe error");
1920 goto error;
1921 } else if (revents & LPOLLIN) {
1922 DBG("Relay command received");
1923 ret = relay_add_connection(relay_cmd_pipe[0],
1924 &events, relay_connections_ht);
1925 if (ret < 0) {
1926 goto error;
1927 }
1928 }
1929 } else if (revents) {
1930 rcu_read_lock();
1931 lttng_ht_lookup(relay_connections_ht,
1932 (void *)((unsigned long) pollfd),
1933 &iter);
1934 node = lttng_ht_iter_get_node_ulong(&iter);
1935 if (node == NULL) {
1936 DBG2("Relay sock %d not found", pollfd);
1937 rcu_read_unlock();
1938 goto error;
1939 }
1940 relay_connection = caa_container_of(node,
1941 struct relay_command, sock_n);
1942
1943 if (revents & (LPOLLERR)) {
1944 ERR("POLL ERROR");
1945 relay_cleanup_poll_connection(&events, pollfd);
1946 relay_del_connection(relay_connections_ht,
1947 streams_ht, &iter,
1948 relay_connection);
1949 if (last_seen_data_fd == pollfd) {
1950 last_seen_data_fd = last_notdel_data_fd;
1951 }
1952 } else if (revents & (LPOLLHUP | LPOLLRDHUP)) {
1953 DBG("Socket %d hung up", pollfd);
1954 relay_cleanup_poll_connection(&events, pollfd);
1955 relay_del_connection(relay_connections_ht,
1956 streams_ht, &iter,
1957 relay_connection);
1958 if (last_seen_data_fd == pollfd) {
1959 last_seen_data_fd = last_notdel_data_fd;
1960 }
1961 } else if (revents & LPOLLIN) {
1962 /* control socket */
1963 if (relay_connection->type == RELAY_CONTROL) {
1964 ret = relay_connection->sock->ops->recvmsg(
1965 relay_connection->sock, &recv_hdr,
1966 sizeof(struct lttcomm_relayd_hdr), 0);
1967 /* connection closed */
1968 if (ret <= 0) {
1969 relay_cleanup_poll_connection(&events, pollfd);
1970 relay_del_connection(relay_connections_ht,
1971 streams_ht, &iter,
1972 relay_connection);
1973 DBG("Control connection closed with %d", pollfd);
1974 } else {
1975 if (relay_connection->session) {
1976 DBG2("Relay worker receiving data for session : %" PRIu64,
1977 relay_connection->session->id);
1978 }
1979 ret = relay_process_control(&recv_hdr,
1980 relay_connection,
1981 streams_ht);
1982 if (ret < 0) {
1983 /* Clear the session on error. */
1984 relay_cleanup_poll_connection(&events, pollfd);
1985 relay_del_connection(relay_connections_ht,
1986 streams_ht, &iter,
1987 relay_connection);
1988 DBG("Connection closed with %d", pollfd);
1989 }
1990 seen_control = 1;
1991 }
1992 } else {
1993 /*
1994 * Flag the last seen data fd not deleted. It will be
1995 * used as the last seen fd if any fd gets deleted in
1996 * this first loop.
1997 */
1998 last_notdel_data_fd = pollfd;
1999 }
2000 }
2001 rcu_read_unlock();
2002 }
2003 }
2004
2005 /*
2006 * The last loop handled a control request, go back to poll to make
2007 * sure we prioritise the control socket.
2008 */
2009 if (seen_control) {
2010 continue;
2011 }
2012
2013 if (last_seen_data_fd >= 0) {
2014 for (i = 0; i < nb_fd; i++) {
2015 int pollfd = LTTNG_POLL_GETFD(&events, i);
2016 if (last_seen_data_fd == pollfd) {
2017 idx = i;
2018 break;
2019 }
2020 }
2021 }
2022
2023 /* Process data connection. */
2024 for (i = idx + 1; i < nb_fd; i++) {
2025 /* Fetch the poll data. */
2026 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
2027 int pollfd = LTTNG_POLL_GETFD(&events, i);
2028
2029 /* Skip the command pipe. It's handled in the first loop. */
2030 if (pollfd == relay_cmd_pipe[0]) {
2031 continue;
2032 }
2033
2034 if (revents) {
2035 rcu_read_lock();
2036 lttng_ht_lookup(relay_connections_ht,
2037 (void *)((unsigned long) pollfd),
2038 &iter);
2039 node = lttng_ht_iter_get_node_ulong(&iter);
2040 if (node == NULL) {
2041 /* Skip it. Might be removed before. */
2042 rcu_read_unlock();
2043 continue;
2044 }
2045 relay_connection = caa_container_of(node,
2046 struct relay_command, sock_n);
2047
2048 if (revents & LPOLLIN) {
2049 if (relay_connection->type != RELAY_DATA) {
2050 continue;
2051 }
2052
2053 ret = relay_process_data(relay_connection, streams_ht);
2054 /* connection closed */
2055 if (ret < 0) {
2056 relay_cleanup_poll_connection(&events, pollfd);
2057 relay_del_connection(relay_connections_ht,
2058 streams_ht, &iter,
2059 relay_connection);
2060 DBG("Data connection closed with %d", pollfd);
2061 /*
2062 * Every goto restart call sets the last seen fd where
2063 * here we don't really care since we gracefully
2064 * continue the loop after the connection is deleted.
2065 */
2066 } else {
2067 /* Keep last seen port. */
2068 last_seen_data_fd = pollfd;
2069 rcu_read_unlock();
2070 goto restart;
2071 }
2072 }
2073 rcu_read_unlock();
2074 }
2075 }
2076 last_seen_data_fd = -1;
2077 }
2078
2079 exit:
2080 error:
2081 lttng_poll_clean(&events);
2082
2083 /* empty the hash table and free the memory */
2084 rcu_read_lock();
2085 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) {
2086 node = lttng_ht_iter_get_node_ulong(&iter);
2087 if (node) {
2088 relay_connection = caa_container_of(node,
2089 struct relay_command, sock_n);
2090 relay_del_connection(relay_connections_ht,
2091 streams_ht, &iter,
2092 relay_connection);
2093 }
2094 }
2095 rcu_read_unlock();
2096 error_poll_create:
2097 lttng_ht_destroy(streams_ht);
2098 streams_ht_error:
2099 lttng_ht_destroy(relay_connections_ht);
2100 relay_connections_ht_error:
2101 /* Close relay cmd pipes */
2102 utils_close_pipe(relay_cmd_pipe);
2103 if (err) {
2104 DBG("Thread exited with error");
2105 }
2106 DBG("Worker thread cleanup complete");
2107 free(data_buffer);
2108 stop_threads();
2109 rcu_unregister_thread();
2110 return NULL;
2111 }
2112
2113 /*
2114 * Create the relay command pipe to wake thread_manage_apps.
2115 * Closed in cleanup().
2116 */
2117 static int create_relay_cmd_pipe(void)
2118 {
2119 int ret;
2120
2121 ret = utils_create_pipe_cloexec(relay_cmd_pipe);
2122
2123 return ret;
2124 }
2125
2126 /*
2127 * main
2128 */
2129 int main(int argc, char **argv)
2130 {
2131 int ret = 0;
2132 void *status;
2133
2134 /* Create thread quit pipe */
2135 if ((ret = init_thread_quit_pipe()) < 0) {
2136 goto error;
2137 }
2138
2139 /* Parse arguments */
2140 progname = argv[0];
2141 if ((ret = parse_args(argc, argv)) < 0) {
2142 goto exit;
2143 }
2144
2145 if ((ret = set_signal_handler()) < 0) {
2146 goto exit;
2147 }
2148
2149 /* Daemonize */
2150 if (opt_daemon) {
2151 ret = daemon(0, 0);
2152 if (ret < 0) {
2153 PERROR("daemon");
2154 goto exit;
2155 }
2156 }
2157
2158 /* Check if daemon is UID = 0 */
2159 is_root = !getuid();
2160
2161 if (!is_root) {
2162 if (control_uri->port < 1024 || data_uri->port < 1024) {
2163 ERR("Need to be root to use ports < 1024");
2164 ret = -1;
2165 goto exit;
2166 }
2167 }
2168
2169 /* Setup the thread apps communication pipe. */
2170 if ((ret = create_relay_cmd_pipe()) < 0) {
2171 goto exit;
2172 }
2173
2174 /* Init relay command queue. */
2175 cds_wfq_init(&relay_cmd_queue.queue);
2176
2177 /* Set up max poll set size */
2178 lttng_poll_set_max_size();
2179
2180 /* Setup the dispatcher thread */
2181 ret = pthread_create(&dispatcher_thread, NULL,
2182 relay_thread_dispatcher, (void *) NULL);
2183 if (ret != 0) {
2184 PERROR("pthread_create dispatcher");
2185 goto exit_dispatcher;
2186 }
2187
2188 /* Setup the worker thread */
2189 ret = pthread_create(&worker_thread, NULL,
2190 relay_thread_worker, (void *) NULL);
2191 if (ret != 0) {
2192 PERROR("pthread_create worker");
2193 goto exit_worker;
2194 }
2195
2196 /* Setup the listener thread */
2197 ret = pthread_create(&listener_thread, NULL,
2198 relay_thread_listener, (void *) NULL);
2199 if (ret != 0) {
2200 PERROR("pthread_create listener");
2201 goto exit_listener;
2202 }
2203
2204 exit_listener:
2205 ret = pthread_join(listener_thread, &status);
2206 if (ret != 0) {
2207 PERROR("pthread_join");
2208 goto error; /* join error, exit without cleanup */
2209 }
2210
2211 exit_worker:
2212 ret = pthread_join(worker_thread, &status);
2213 if (ret != 0) {
2214 PERROR("pthread_join");
2215 goto error; /* join error, exit without cleanup */
2216 }
2217
2218 exit_dispatcher:
2219 ret = pthread_join(dispatcher_thread, &status);
2220 if (ret != 0) {
2221 PERROR("pthread_join");
2222 goto error; /* join error, exit without cleanup */
2223 }
2224
2225 exit:
2226 cleanup();
2227 if (!ret) {
2228 exit(EXIT_SUCCESS);
2229 }
2230
2231 error:
2232 exit(EXIT_FAILURE);
2233 }
This page took 0.114445 seconds and 4 git commands to generate.