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