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