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