Fix: handle new streams in live mode in relayd
[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;
091c3237 1027 pthread_mutex_init(&session->viewer_ready_lock, NULL);
c5b6f4f0
DG
1028 cmd->session = session;
1029
1030 reply.session_id = htobe64(session->id);
1031
d3e2ba59 1032 switch (cmd->minor) {
b0dd7a75
JD
1033 case 1:
1034 case 2:
1035 case 3:
1036 break;
d3e2ba59
JD
1037 case 4: /* LTTng sessiond 2.4 */
1038 default:
1039 ret = cmd_create_session_2_4(cmd, session);
1040 break;
1041 }
1042
1043 lttng_ht_node_init_ulong(&session->session_n,
1044 (unsigned long) session->id);
1045 lttng_ht_add_unique_ulong(sessions_ht,
1046 &session->session_n);
1047
c5b6f4f0
DG
1048 DBG("Created session %" PRIu64, session->id);
1049
1050error:
1051 if (ret < 0) {
1052 reply.ret_code = htobe32(LTTNG_ERR_FATAL);
1053 } else {
1054 reply.ret_code = htobe32(LTTNG_OK);
1055 }
1056
1057 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1058 if (send_ret < 0) {
1059 ERR("Relayd sending session id");
4169f5ad 1060 ret = send_ret;
c5b6f4f0
DG
1061 }
1062
1063 return ret;
1064}
1065
814fcae4
JD
1066/*
1067 * When we have received all the streams and the metadata for a channel,
1068 * we make them visible to the viewer threads.
1069 */
1070static
1071void set_viewer_ready_flag(struct relay_command *cmd)
1072{
1073 struct relay_stream_recv_handle *node, *tmp_node;
1074
091c3237
JD
1075 pthread_mutex_lock(&cmd->session->viewer_ready_lock);
1076
814fcae4
JD
1077 cds_list_for_each_entry_safe(node, tmp_node, &cmd->recv_head, node) {
1078 struct relay_stream *stream;
1079
1080 rcu_read_lock();
1081 stream = relay_stream_find_by_id(node->id);
1082 if (!stream) {
1083 /*
1084 * Stream is most probably being cleaned up by the data thread thus
1085 * simply continue to the next one.
1086 */
f188956b 1087 rcu_read_unlock();
814fcae4
JD
1088 continue;
1089 }
1090
814fcae4
JD
1091 stream->viewer_ready = 1;
1092 rcu_read_unlock();
1093
1094 /* Clean stream handle node. */
1095 cds_list_del(&node->node);
1096 free(node);
1097 }
1098
091c3237 1099 pthread_mutex_unlock(&cmd->session->viewer_ready_lock);
814fcae4
JD
1100 return;
1101}
1102
1103/*
1104 * Add a recv handle node to the connection recv list with the given stream
1105 * handle. A new node is allocated thus must be freed when the node is deleted
1106 * from the list.
1107 */
1108static void queue_stream_handle(uint64_t handle, struct relay_command *cmd)
1109{
1110 struct relay_stream_recv_handle *node;
1111
1112 assert(cmd);
1113
1114 node = zmalloc(sizeof(*node));
1115 if (!node) {
1116 PERROR("zmalloc queue stream handle");
1117 return;
1118 }
1119
1120 node->id = handle;
1121 cds_list_add(&node->node, &cmd->recv_head);
1122}
1123
b8aa1682
JD
1124/*
1125 * relay_add_stream: allocate a new stream for a session
1126 */
1127static
1128int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1129 struct relay_command *cmd, struct lttng_ht *sessions_ht)
b8aa1682
JD
1130{
1131 struct relay_session *session = cmd->session;
b8aa1682
JD
1132 struct relay_stream *stream = NULL;
1133 struct lttcomm_relayd_status_stream reply;
b8aa1682
JD
1134 int ret, send_ret;
1135
c5b6f4f0 1136 if (!session || cmd->version_check_done == 0) {
b8aa1682
JD
1137 ERR("Trying to add a stream before version check");
1138 ret = -1;
1139 goto end_no_session;
1140 }
1141
b8aa1682
JD
1142 stream = zmalloc(sizeof(struct relay_stream));
1143 if (stream == NULL) {
1144 PERROR("relay stream zmalloc");
1145 ret = -1;
1146 goto end_no_session;
1147 }
1148
0f907de1
JD
1149 switch (cmd->minor) {
1150 case 1: /* LTTng sessiond 2.1 */
1151 ret = cmd_recv_stream_2_1(cmd, stream);
1152 break;
1153 case 2: /* LTTng sessiond 2.2 */
1154 default:
1155 ret = cmd_recv_stream_2_2(cmd, stream);
1156 break;
1157 }
1158 if (ret < 0) {
1159 goto err_free_stream;
1160 }
1161
9d1bbf21 1162 rcu_read_lock();
b8aa1682 1163 stream->stream_handle = ++last_relay_stream_id;
173af62f 1164 stream->prev_seq = -1ULL;
b8aa1682 1165 stream->session = session;
1c20f0e2 1166 stream->index_fd = -1;
d3e2ba59
JD
1167 stream->read_index_fd = -1;
1168 stream->ctf_trace = NULL;
1169 pthread_mutex_init(&stream->lock, NULL);
b8aa1682 1170
0f907de1 1171 ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG);
b8aa1682 1172 if (ret < 0) {
b8aa1682
JD
1173 ERR("relay creating output directory");
1174 goto end;
1175 }
1176
be96a7d1
DG
1177 /*
1178 * No need to use run_as API here because whatever we receives, the relayd
1179 * uses its own credentials for the stream files.
1180 */
0f907de1 1181 ret = utils_create_stream_file(stream->path_name, stream->channel_name,
1c20f0e2 1182 stream->tracefile_size, 0, relayd_uid, relayd_gid, NULL);
b8aa1682 1183 if (ret < 0) {
0f907de1 1184 ERR("Create output file");
b8aa1682
JD
1185 goto end;
1186 }
b8aa1682 1187 stream->fd = ret;
0f907de1
JD
1188 if (stream->tracefile_size) {
1189 DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name);
1190 } else {
1191 DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name);
1192 }
b8aa1682 1193
d3e2ba59
JD
1194 if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, NAME_MAX)) {
1195 stream->metadata_flag = 1;
1196 /*
1197 * When we receive a new metadata stream, we create a new
1198 * ctf_trace and we assign this ctf_trace to all streams with
1199 * the same path.
1200 *
1201 * If later on we receive a new stream for the same ctf_trace,
1202 * we copy the information from the first hit in the HT to the
1203 * new stream.
1204 */
1205 stream->ctf_trace = ctf_trace_create();
1206 if (!stream->ctf_trace) {
1207 ret = -1;
1208 goto end;
1209 }
1210 stream->ctf_trace->refcount++;
1211 stream->ctf_trace->metadata_stream = stream;
1212 }
1213 ctf_trace_assign(cmd->ctf_traces_ht, stream);
c07d8a78 1214 stream->ctf_traces_ht = cmd->ctf_traces_ht;
d3e2ba59 1215
814fcae4
JD
1216 /*
1217 * Add the stream handle in the recv list of the connection. Once the end
1218 * stream message is received, this list is emptied and streams are set
1219 * with the viewer ready flag.
1220 */
e37dd78f 1221 queue_stream_handle(stream->stream_handle, cmd);
814fcae4 1222
b8aa1682
JD
1223 lttng_ht_node_init_ulong(&stream->stream_n,
1224 (unsigned long) stream->stream_handle);
d3e2ba59 1225 lttng_ht_add_unique_ulong(relay_streams_ht,
b8aa1682
JD
1226 &stream->stream_n);
1227
d3e2ba59
JD
1228 lttng_ht_node_init_str(&stream->ctf_trace_node, stream->path_name);
1229 lttng_ht_add_str(cmd->ctf_traces_ht, &stream->ctf_trace_node);
87b576ec 1230 session->stream_count++;
d3e2ba59 1231
1c20f0e2
JD
1232 DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name,
1233 stream->stream_handle);
b8aa1682
JD
1234
1235end:
5af40280 1236 reply.handle = htobe64(stream->stream_handle);
b8aa1682
JD
1237 /* send the session id to the client or a negative return code on error */
1238 if (ret < 0) {
f73fabfd 1239 reply.ret_code = htobe32(LTTNG_ERR_UNK);
5af40280
CB
1240 /* stream was not properly added to the ht, so free it */
1241 free(stream);
b8aa1682 1242 } else {
f73fabfd 1243 reply.ret_code = htobe32(LTTNG_OK);
b8aa1682 1244 }
5af40280 1245
b8aa1682
JD
1246 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1247 sizeof(struct lttcomm_relayd_status_stream), 0);
1248 if (send_ret < 0) {
1249 ERR("Relay sending stream id");
4169f5ad 1250 ret = send_ret;
b8aa1682 1251 }
9d1bbf21 1252 rcu_read_unlock();
b8aa1682
JD
1253
1254end_no_session:
1255 return ret;
0f907de1
JD
1256
1257err_free_stream:
1258 free(stream->path_name);
1259 free(stream->channel_name);
1260 free(stream);
1261 return ret;
b8aa1682
JD
1262}
1263
173af62f
DG
1264/*
1265 * relay_close_stream: close a specific stream
1266 */
1267static
1268int relay_close_stream(struct lttcomm_relayd_hdr *recv_hdr,
92c6ca54 1269 struct relay_command *cmd)
173af62f 1270{
94d49140 1271 int ret, send_ret;
173af62f
DG
1272 struct relay_session *session = cmd->session;
1273 struct lttcomm_relayd_close_stream stream_info;
1274 struct lttcomm_relayd_generic_reply reply;
1275 struct relay_stream *stream;
173af62f
DG
1276
1277 DBG("Close stream received");
1278
c5b6f4f0 1279 if (!session || cmd->version_check_done == 0) {
173af62f
DG
1280 ERR("Trying to close a stream before version check");
1281 ret = -1;
1282 goto end_no_session;
1283 }
1284
1285 ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info,
7c5aef62 1286 sizeof(struct lttcomm_relayd_close_stream), 0);
173af62f 1287 if (ret < sizeof(struct lttcomm_relayd_close_stream)) {
a6cd2b97
DG
1288 if (ret == 0) {
1289 /* Orderly shutdown. Not necessary to print an error. */
1290 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1291 } else {
1292 ERR("Relay didn't receive valid add_stream struct size : %d", ret);
1293 }
173af62f
DG
1294 ret = -1;
1295 goto end_no_session;
1296 }
1297
1298 rcu_read_lock();
d3e2ba59 1299 stream = relay_stream_find_by_id(be64toh(stream_info.stream_id));
173af62f
DG
1300 if (!stream) {
1301 ret = -1;
1302 goto end_unlock;
1303 }
1304
8e2583a4 1305 stream->last_net_seq_num = be64toh(stream_info.last_net_seq_num);
173af62f 1306 stream->close_flag = 1;
87b576ec
JD
1307 session->stream_count--;
1308 assert(session->stream_count >= 0);
173af62f
DG
1309
1310 if (close_stream_check(stream)) {
c07d8a78 1311 destroy_stream(stream);
173af62f
DG
1312 }
1313
1314end_unlock:
1315 rcu_read_unlock();
1316
1317 if (ret < 0) {
f73fabfd 1318 reply.ret_code = htobe32(LTTNG_ERR_UNK);
173af62f 1319 } else {
f73fabfd 1320 reply.ret_code = htobe32(LTTNG_OK);
173af62f
DG
1321 }
1322 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1323 sizeof(struct lttcomm_relayd_generic_reply), 0);
1324 if (send_ret < 0) {
1325 ERR("Relay sending stream id");
4169f5ad 1326 ret = send_ret;
173af62f
DG
1327 }
1328
1329end_no_session:
1330 return ret;
1331}
1332
b8aa1682
JD
1333/*
1334 * relay_unknown_command: send -1 if received unknown command
1335 */
1336static
1337void relay_unknown_command(struct relay_command *cmd)
1338{
1339 struct lttcomm_relayd_generic_reply reply;
1340 int ret;
1341
f73fabfd 1342 reply.ret_code = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1343 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1344 sizeof(struct lttcomm_relayd_generic_reply), 0);
1345 if (ret < 0) {
1346 ERR("Relay sending unknown command");
1347 }
1348}
1349
1350/*
1351 * relay_start: send an acknowledgment to the client to tell if we are
1352 * ready to receive data. We are ready if a session is established.
1353 */
1354static
1355int relay_start(struct lttcomm_relayd_hdr *recv_hdr,
1356 struct relay_command *cmd)
1357{
f73fabfd 1358 int ret = htobe32(LTTNG_OK);
b8aa1682
JD
1359 struct lttcomm_relayd_generic_reply reply;
1360 struct relay_session *session = cmd->session;
1361
1362 if (!session) {
1363 DBG("Trying to start the streaming without a session established");
f73fabfd 1364 ret = htobe32(LTTNG_ERR_UNK);
b8aa1682
JD
1365 }
1366
1367 reply.ret_code = ret;
1368 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1369 sizeof(struct lttcomm_relayd_generic_reply), 0);
1370 if (ret < 0) {
1371 ERR("Relay sending start ack");
1372 }
1373
1374 return ret;
1375}
1376
1d4dfdef
DG
1377/*
1378 * Append padding to the file pointed by the file descriptor fd.
1379 */
1380static int write_padding_to_file(int fd, uint32_t size)
1381{
6cd525e8 1382 ssize_t ret = 0;
1d4dfdef
DG
1383 char *zeros;
1384
1385 if (size == 0) {
1386 goto end;
1387 }
1388
1389 zeros = zmalloc(size);
1390 if (zeros == NULL) {
1391 PERROR("zmalloc zeros for padding");
1392 ret = -1;
1393 goto end;
1394 }
1395
6cd525e8
MD
1396 ret = lttng_write(fd, zeros, size);
1397 if (ret < size) {
1d4dfdef
DG
1398 PERROR("write padding to file");
1399 }
1400
e986c7a1
DG
1401 free(zeros);
1402
1d4dfdef
DG
1403end:
1404 return ret;
1405}
1406
b8aa1682
JD
1407/*
1408 * relay_recv_metadata: receive the metada for the session.
1409 */
1410static
1411int relay_recv_metadata(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1412 struct relay_command *cmd)
b8aa1682 1413{
f73fabfd 1414 int ret = htobe32(LTTNG_OK);
6cd525e8 1415 ssize_t size_ret;
b8aa1682
JD
1416 struct relay_session *session = cmd->session;
1417 struct lttcomm_relayd_metadata_payload *metadata_struct;
1418 struct relay_stream *metadata_stream;
1419 uint64_t data_size, payload_size;
1420
1421 if (!session) {
1422 ERR("Metadata sent before version check");
1423 ret = -1;
1424 goto end;
1425 }
1426
f6416125
MD
1427 data_size = payload_size = be64toh(recv_hdr->data_size);
1428 if (data_size < sizeof(struct lttcomm_relayd_metadata_payload)) {
1429 ERR("Incorrect data size");
1430 ret = -1;
1431 goto end;
1432 }
1433 payload_size -= sizeof(struct lttcomm_relayd_metadata_payload);
1434
b8aa1682 1435 if (data_buffer_size < data_size) {
d7b3776f 1436 /* In case the realloc fails, we can free the memory */
c617c0c6
MD
1437 char *tmp_data_ptr;
1438
1439 tmp_data_ptr = realloc(data_buffer, data_size);
1440 if (!tmp_data_ptr) {
b8aa1682 1441 ERR("Allocating data buffer");
c617c0c6 1442 free(data_buffer);
b8aa1682
JD
1443 ret = -1;
1444 goto end;
1445 }
c617c0c6 1446 data_buffer = tmp_data_ptr;
b8aa1682
JD
1447 data_buffer_size = data_size;
1448 }
1449 memset(data_buffer, 0, data_size);
77c7c900 1450 DBG2("Relay receiving metadata, waiting for %" PRIu64 " bytes", data_size);
7c5aef62 1451 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
b8aa1682 1452 if (ret < 0 || ret != data_size) {
a6cd2b97
DG
1453 if (ret == 0) {
1454 /* Orderly shutdown. Not necessary to print an error. */
1455 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1456 } else {
1457 ERR("Relay didn't receive the whole metadata");
1458 }
b8aa1682 1459 ret = -1;
b8aa1682
JD
1460 goto end;
1461 }
1462 metadata_struct = (struct lttcomm_relayd_metadata_payload *) data_buffer;
9d1bbf21
MD
1463
1464 rcu_read_lock();
d3e2ba59
JD
1465 metadata_stream = relay_stream_find_by_id(
1466 be64toh(metadata_struct->stream_id));
b8aa1682
JD
1467 if (!metadata_stream) {
1468 ret = -1;
9d1bbf21 1469 goto end_unlock;
b8aa1682
JD
1470 }
1471
6cd525e8
MD
1472 size_ret = lttng_write(metadata_stream->fd, metadata_struct->payload,
1473 payload_size);
1474 if (size_ret < payload_size) {
b8aa1682
JD
1475 ERR("Relay error writing metadata on file");
1476 ret = -1;
9d1bbf21 1477 goto end_unlock;
b8aa1682 1478 }
1d4dfdef
DG
1479
1480 ret = write_padding_to_file(metadata_stream->fd,
1481 be32toh(metadata_struct->padding_size));
1482 if (ret < 0) {
1483 goto end_unlock;
1484 }
d3e2ba59
JD
1485 metadata_stream->ctf_trace->metadata_received +=
1486 payload_size + be32toh(metadata_struct->padding_size);
1d4dfdef 1487
b8aa1682
JD
1488 DBG2("Relay metadata written");
1489
9d1bbf21 1490end_unlock:
6e3c5836 1491 rcu_read_unlock();
b8aa1682
JD
1492end:
1493 return ret;
1494}
1495
1496/*
1497 * relay_send_version: send relayd version number
1498 */
1499static
1500int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1501 struct relay_command *cmd, struct lttng_ht *sessions_ht)
b8aa1682 1502{
7f51dcba 1503 int ret;
092b6259 1504 struct lttcomm_relayd_version reply, msg;
b8aa1682 1505
c5b6f4f0
DG
1506 assert(cmd);
1507
1508 cmd->version_check_done = 1;
b8aa1682 1509
092b6259 1510 /* Get version from the other side. */
7c5aef62 1511 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
092b6259 1512 if (ret < 0 || ret != sizeof(msg)) {
a6cd2b97
DG
1513 if (ret == 0) {
1514 /* Orderly shutdown. Not necessary to print an error. */
1515 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1516 } else {
1517 ERR("Relay failed to receive the version values.");
1518 }
092b6259 1519 ret = -1;
092b6259
DG
1520 goto end;
1521 }
1522
d83a952c
MD
1523 reply.major = RELAYD_VERSION_COMM_MAJOR;
1524 reply.minor = RELAYD_VERSION_COMM_MINOR;
d4519fa3
JD
1525
1526 /* Major versions must be the same */
1527 if (reply.major != be32toh(msg.major)) {
6151a90f
JD
1528 DBG("Incompatible major versions (%u vs %u), deleting session",
1529 reply.major, be32toh(msg.major));
d3e2ba59 1530 relay_delete_session(cmd, sessions_ht);
d4519fa3
JD
1531 ret = 0;
1532 goto end;
1533 }
1534
0f907de1
JD
1535 cmd->major = reply.major;
1536 /* We adapt to the lowest compatible version */
1537 if (reply.minor <= be32toh(msg.minor)) {
1538 cmd->minor = reply.minor;
1539 } else {
1540 cmd->minor = be32toh(msg.minor);
1541 }
1542
6151a90f
JD
1543 reply.major = htobe32(reply.major);
1544 reply.minor = htobe32(reply.minor);
1545 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
1546 sizeof(struct lttcomm_relayd_version), 0);
1547 if (ret < 0) {
1548 ERR("Relay sending version");
1549 }
1550
0f907de1
JD
1551 DBG("Version check done using protocol %u.%u", cmd->major,
1552 cmd->minor);
b8aa1682
JD
1553
1554end:
1555 return ret;
1556}
1557
c8f59ee5 1558/*
6d805429 1559 * Check for data pending for a given stream id from the session daemon.
c8f59ee5
DG
1560 */
1561static
6d805429 1562int relay_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1563 struct relay_command *cmd)
c8f59ee5
DG
1564{
1565 struct relay_session *session = cmd->session;
6d805429 1566 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
1567 struct lttcomm_relayd_generic_reply reply;
1568 struct relay_stream *stream;
1569 int ret;
c8f59ee5
DG
1570 uint64_t last_net_seq_num, stream_id;
1571
6d805429 1572 DBG("Data pending command received");
c8f59ee5 1573
c5b6f4f0 1574 if (!session || cmd->version_check_done == 0) {
c8f59ee5
DG
1575 ERR("Trying to check for data before version check");
1576 ret = -1;
1577 goto end_no_session;
1578 }
1579
7c5aef62 1580 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
c8f59ee5 1581 if (ret < sizeof(msg)) {
a6cd2b97
DG
1582 if (ret == 0) {
1583 /* Orderly shutdown. Not necessary to print an error. */
1584 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1585 } else {
1586 ERR("Relay didn't receive valid data_pending struct size : %d",
1587 ret);
1588 }
c8f59ee5
DG
1589 ret = -1;
1590 goto end_no_session;
1591 }
1592
1593 stream_id = be64toh(msg.stream_id);
1594 last_net_seq_num = be64toh(msg.last_net_seq_num);
1595
1596 rcu_read_lock();
d3e2ba59 1597 stream = relay_stream_find_by_id(stream_id);
de91f48a 1598 if (stream == NULL) {
c8f59ee5
DG
1599 ret = -1;
1600 goto end_unlock;
1601 }
1602
6d805429 1603 DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64
c8f59ee5
DG
1604 " and last_seq %" PRIu64, stream_id, stream->prev_seq,
1605 last_net_seq_num);
1606
33832e64 1607 /* Avoid wrapping issue */
39df6d9f 1608 if (((int64_t) (stream->prev_seq - last_net_seq_num)) >= 0) {
6d805429 1609 /* Data has in fact been written and is NOT pending */
c8f59ee5 1610 ret = 0;
6d805429
DG
1611 } else {
1612 /* Data still being streamed thus pending */
1613 ret = 1;
c8f59ee5
DG
1614 }
1615
f7079f67
DG
1616 /* Pending check is now done. */
1617 stream->data_pending_check_done = 1;
1618
c8f59ee5
DG
1619end_unlock:
1620 rcu_read_unlock();
1621
1622 reply.ret_code = htobe32(ret);
1623 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1624 if (ret < 0) {
6d805429 1625 ERR("Relay data pending ret code failed");
c8f59ee5
DG
1626 }
1627
1628end_no_session:
1629 return ret;
1630}
1631
1632/*
1633 * Wait for the control socket to reach a quiescent state.
1634 *
1635 * Note that for now, when receiving this command from the session daemon, this
1636 * means that every subsequent commands or data received on the control socket
1637 * has been handled. So, this is why we simply return OK here.
1638 */
1639static
1640int relay_quiescent_control(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1641 struct relay_command *cmd)
c8f59ee5
DG
1642{
1643 int ret;
ad7051c0
DG
1644 uint64_t stream_id;
1645 struct relay_stream *stream;
1646 struct lttng_ht_iter iter;
1647 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
1648 struct lttcomm_relayd_generic_reply reply;
1649
1650 DBG("Checking quiescent state on control socket");
1651
ad7051c0
DG
1652 if (!cmd->session || cmd->version_check_done == 0) {
1653 ERR("Trying to check for data before version check");
1654 ret = -1;
1655 goto end_no_session;
1656 }
1657
1658 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1659 if (ret < sizeof(msg)) {
a6cd2b97
DG
1660 if (ret == 0) {
1661 /* Orderly shutdown. Not necessary to print an error. */
1662 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1663 } else {
1664 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1665 ret);
1666 }
ad7051c0
DG
1667 ret = -1;
1668 goto end_no_session;
1669 }
1670
1671 stream_id = be64toh(msg.stream_id);
1672
1673 rcu_read_lock();
d3e2ba59
JD
1674 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
1675 stream_n.node) {
ad7051c0
DG
1676 if (stream->stream_handle == stream_id) {
1677 stream->data_pending_check_done = 1;
1678 DBG("Relay quiescent control pending flag set to %" PRIu64,
1679 stream_id);
1680 break;
1681 }
1682 }
1683 rcu_read_unlock();
1684
c8f59ee5
DG
1685 reply.ret_code = htobe32(LTTNG_OK);
1686 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1687 if (ret < 0) {
6d805429 1688 ERR("Relay data quiescent control ret code failed");
c8f59ee5
DG
1689 }
1690
ad7051c0 1691end_no_session:
c8f59ee5
DG
1692 return ret;
1693}
1694
f7079f67
DG
1695/*
1696 * Initialize a data pending command. This means that a client is about to ask
1697 * for data pending for each stream he/she holds. Simply iterate over all
1698 * streams of a session and set the data_pending_check_done flag.
1699 *
1700 * This command returns to the client a LTTNG_OK code.
1701 */
1702static
1703int relay_begin_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1704 struct relay_command *cmd)
f7079f67
DG
1705{
1706 int ret;
1707 struct lttng_ht_iter iter;
1708 struct lttcomm_relayd_begin_data_pending msg;
1709 struct lttcomm_relayd_generic_reply reply;
1710 struct relay_stream *stream;
1711 uint64_t session_id;
1712
1713 assert(recv_hdr);
1714 assert(cmd);
f7079f67
DG
1715
1716 DBG("Init streams for data pending");
1717
1718 if (!cmd->session || cmd->version_check_done == 0) {
1719 ERR("Trying to check for data before version check");
1720 ret = -1;
1721 goto end_no_session;
1722 }
1723
1724 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1725 if (ret < sizeof(msg)) {
a6cd2b97
DG
1726 if (ret == 0) {
1727 /* Orderly shutdown. Not necessary to print an error. */
1728 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1729 } else {
1730 ERR("Relay didn't receive valid begin data_pending struct size: %d",
1731 ret);
1732 }
f7079f67
DG
1733 ret = -1;
1734 goto end_no_session;
1735 }
1736
1737 session_id = be64toh(msg.session_id);
1738
1739 /*
1740 * Iterate over all streams to set the begin data pending flag. For now, the
1741 * streams are indexed by stream handle so we have to iterate over all
1742 * streams to find the one associated with the right session_id.
1743 */
1744 rcu_read_lock();
d3e2ba59
JD
1745 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
1746 stream_n.node) {
f7079f67
DG
1747 if (stream->session->id == session_id) {
1748 stream->data_pending_check_done = 0;
1749 DBG("Set begin data pending flag to stream %" PRIu64,
1750 stream->stream_handle);
1751 }
1752 }
1753 rcu_read_unlock();
1754
1755 /* All good, send back reply. */
1756 reply.ret_code = htobe32(LTTNG_OK);
1757
1758 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1759 if (ret < 0) {
1760 ERR("Relay begin data pending send reply failed");
1761 }
1762
1763end_no_session:
1764 return ret;
1765}
1766
1767/*
1768 * End data pending command. This will check, for a given session id, if each
1769 * stream associated with it has its data_pending_check_done flag set. If not,
1770 * this means that the client lost track of the stream but the data is still
1771 * being streamed on our side. In this case, we inform the client that data is
1772 * inflight.
1773 *
1774 * Return to the client if there is data in flight or not with a ret_code.
1775 */
1776static
1777int relay_end_data_pending(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 1778 struct relay_command *cmd)
f7079f67
DG
1779{
1780 int ret;
1781 struct lttng_ht_iter iter;
1782 struct lttcomm_relayd_end_data_pending msg;
1783 struct lttcomm_relayd_generic_reply reply;
1784 struct relay_stream *stream;
1785 uint64_t session_id;
1786 uint32_t is_data_inflight = 0;
1787
1788 assert(recv_hdr);
1789 assert(cmd);
f7079f67
DG
1790
1791 DBG("End data pending command");
1792
1793 if (!cmd->session || cmd->version_check_done == 0) {
1794 ERR("Trying to check for data before version check");
1795 ret = -1;
1796 goto end_no_session;
1797 }
1798
1799 ret = cmd->sock->ops->recvmsg(cmd->sock, &msg, sizeof(msg), 0);
1800 if (ret < sizeof(msg)) {
a6cd2b97
DG
1801 if (ret == 0) {
1802 /* Orderly shutdown. Not necessary to print an error. */
1803 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1804 } else {
1805 ERR("Relay didn't receive valid end data_pending struct size: %d",
1806 ret);
1807 }
f7079f67
DG
1808 ret = -1;
1809 goto end_no_session;
1810 }
1811
1812 session_id = be64toh(msg.session_id);
1813
1814 /* Iterate over all streams to see if the begin data pending flag is set. */
1815 rcu_read_lock();
d3e2ba59
JD
1816 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
1817 stream_n.node) {
f7079f67
DG
1818 if (stream->session->id == session_id &&
1819 !stream->data_pending_check_done) {
1820 is_data_inflight = 1;
1821 DBG("Data is still in flight for stream %" PRIu64,
1822 stream->stream_handle);
1823 break;
1824 }
1825 }
1826 rcu_read_unlock();
1827
1828 /* All good, send back reply. */
1829 reply.ret_code = htobe32(is_data_inflight);
1830
1831 ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1832 if (ret < 0) {
1833 ERR("Relay end data pending send reply failed");
1834 }
1835
1836end_no_session:
1837 return ret;
1838}
1839
1c20f0e2
JD
1840/*
1841 * Receive an index for a specific stream.
1842 *
1843 * Return 0 on success else a negative value.
1844 */
1845static
1846int relay_recv_index(struct lttcomm_relayd_hdr *recv_hdr,
0a6518b0 1847 struct relay_command *cmd)
1c20f0e2
JD
1848{
1849 int ret, send_ret, index_created = 0;
1850 struct relay_session *session = cmd->session;
1851 struct lttcomm_relayd_index index_info;
1852 struct relay_index *index, *wr_index = NULL;
1853 struct lttcomm_relayd_generic_reply reply;
1854 struct relay_stream *stream;
1855 uint64_t net_seq_num;
1856
1857 assert(cmd);
1c20f0e2
JD
1858
1859 DBG("Relay receiving index");
1860
1861 if (!session || cmd->version_check_done == 0) {
1862 ERR("Trying to close a stream before version check");
1863 ret = -1;
1864 goto end_no_session;
1865 }
1866
1867 ret = cmd->sock->ops->recvmsg(cmd->sock, &index_info,
1868 sizeof(index_info), 0);
1869 if (ret < sizeof(index_info)) {
1870 if (ret == 0) {
1871 /* Orderly shutdown. Not necessary to print an error. */
1872 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
1873 } else {
1874 ERR("Relay didn't receive valid index struct size : %d", ret);
1875 }
1876 ret = -1;
1877 goto end_no_session;
1878 }
1879
1880 net_seq_num = be64toh(index_info.net_seq_num);
1881
1882 rcu_read_lock();
d3e2ba59 1883 stream = relay_stream_find_by_id(be64toh(index_info.relay_stream_id));
1c20f0e2
JD
1884 if (!stream) {
1885 ret = -1;
1886 goto end_rcu_unlock;
1887 }
1888
d3e2ba59
JD
1889 /* Live beacon handling */
1890 if (index_info.packet_size == 0) {
1891 DBG("Received live beacon for stream %" PRIu64, stream->stream_handle);
1892
1893 /*
1894 * Only flag a stream inactive when it has already received data.
1895 */
1896 if (stream->total_index_received > 0) {
1897 stream->beacon_ts_end = be64toh(index_info.timestamp_end);
1898 }
1899 ret = 0;
1900 goto end_rcu_unlock;
1901 } else {
1902 stream->beacon_ts_end = -1ULL;
1903 }
1904
0a6518b0 1905 index = relay_index_find(stream->stream_handle, net_seq_num);
1c20f0e2
JD
1906 if (!index) {
1907 /* A successful creation will add the object to the HT. */
1908 index = relay_index_create(stream->stream_handle, net_seq_num);
1909 if (!index) {
1910 goto end_rcu_unlock;
1911 }
1912 index_created = 1;
1913 }
1914
1915 copy_index_control_data(index, &index_info);
1916
1917 if (index_created) {
1918 /*
1919 * Try to add the relay index object to the hash table. If an object
1920 * already exist, destroy back the index created, set the data in this
1921 * object and write it on disk.
1922 */
0a6518b0 1923 relay_index_add(index, &wr_index);
1c20f0e2
JD
1924 if (wr_index) {
1925 copy_index_control_data(wr_index, &index_info);
1926 free(index);
1927 }
1928 } else {
1929 /* The index already exists so write it on disk. */
1930 wr_index = index;
1931 }
1932
1933 /* Do we have a writable ready index to write on disk. */
1934 if (wr_index) {
1935 /* Starting at 2.4, create the index file if none available. */
1936 if (cmd->minor >= 4 && stream->index_fd < 0) {
1937 ret = index_create_file(stream->path_name, stream->channel_name,
1938 relayd_uid, relayd_gid, stream->tracefile_size,
1939 stream->tracefile_count_current);
1940 if (ret < 0) {
1941 goto end_rcu_unlock;
1942 }
1943 stream->index_fd = ret;
1944 }
1945
0a6518b0 1946 ret = relay_index_write(wr_index->fd, wr_index);
1c20f0e2
JD
1947 if (ret < 0) {
1948 goto end_rcu_unlock;
1949 }
d3e2ba59 1950 stream->total_index_received++;
1c20f0e2
JD
1951 }
1952
1953end_rcu_unlock:
1954 rcu_read_unlock();
1955
1956 if (ret < 0) {
1957 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1958 } else {
1959 reply.ret_code = htobe32(LTTNG_OK);
1960 }
1961 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
1962 if (send_ret < 0) {
1963 ERR("Relay sending close index id reply");
1964 ret = send_ret;
1965 }
1966
1967end_no_session:
1968 return ret;
1969}
1970
814fcae4
JD
1971/*
1972 * Receive the streams_sent message.
1973 *
1974 * Return 0 on success else a negative value.
1975 */
1976static
1977int relay_streams_sent(struct lttcomm_relayd_hdr *recv_hdr,
1978 struct relay_command *cmd)
1979{
1980 int ret, send_ret;
1981 struct lttcomm_relayd_generic_reply reply;
1982
1983 assert(cmd);
1984
1985 DBG("Relay receiving streams_sent");
1986
1987 if (!cmd->session || cmd->version_check_done == 0) {
1988 ERR("Trying to close a stream before version check");
1989 ret = -1;
1990 goto end_no_session;
1991 }
1992
1993 /*
1994 * Flag every pending stream in the connection recv list that they are
1995 * ready to be used by the viewer.
1996 */
1997 set_viewer_ready_flag(cmd);
1998
829007d9
JD
1999 /*
2000 * Inform the viewer that there are new streams in the session.
2001 */
2002 uatomic_set(&cmd->session->new_streams, 1);
2003
814fcae4
JD
2004 reply.ret_code = htobe32(LTTNG_OK);
2005 send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, sizeof(reply), 0);
2006 if (send_ret < 0) {
2007 ERR("Relay sending sent_stream reply");
2008 ret = send_ret;
2009 } else {
2010 /* Success. */
2011 ret = 0;
2012 }
2013
2014end_no_session:
2015 return ret;
2016}
2017
b8aa1682 2018/*
d3e2ba59 2019 * Process the commands received on the control socket
b8aa1682
JD
2020 */
2021static
2022int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
d3e2ba59 2023 struct relay_command *cmd, struct relay_local_data *ctx)
b8aa1682
JD
2024{
2025 int ret = 0;
2026
2027 switch (be32toh(recv_hdr->cmd)) {
b8aa1682 2028 case RELAYD_CREATE_SESSION:
d3e2ba59 2029 ret = relay_create_session(recv_hdr, cmd, ctx->sessions_ht);
b8aa1682 2030 break;
b8aa1682 2031 case RELAYD_ADD_STREAM:
d3e2ba59 2032 ret = relay_add_stream(recv_hdr, cmd, ctx->sessions_ht);
b8aa1682
JD
2033 break;
2034 case RELAYD_START_DATA:
2035 ret = relay_start(recv_hdr, cmd);
2036 break;
2037 case RELAYD_SEND_METADATA:
d3e2ba59 2038 ret = relay_recv_metadata(recv_hdr, cmd);
b8aa1682
JD
2039 break;
2040 case RELAYD_VERSION:
d3e2ba59 2041 ret = relay_send_version(recv_hdr, cmd, ctx->sessions_ht);
b8aa1682 2042 break;
173af62f 2043 case RELAYD_CLOSE_STREAM:
92c6ca54 2044 ret = relay_close_stream(recv_hdr, cmd);
173af62f 2045 break;
6d805429 2046 case RELAYD_DATA_PENDING:
d3e2ba59 2047 ret = relay_data_pending(recv_hdr, cmd);
c8f59ee5
DG
2048 break;
2049 case RELAYD_QUIESCENT_CONTROL:
d3e2ba59 2050 ret = relay_quiescent_control(recv_hdr, cmd);
c8f59ee5 2051 break;
f7079f67 2052 case RELAYD_BEGIN_DATA_PENDING:
d3e2ba59 2053 ret = relay_begin_data_pending(recv_hdr, cmd);
f7079f67
DG
2054 break;
2055 case RELAYD_END_DATA_PENDING:
d3e2ba59 2056 ret = relay_end_data_pending(recv_hdr, cmd);
f7079f67 2057 break;
1c20f0e2 2058 case RELAYD_SEND_INDEX:
0a6518b0 2059 ret = relay_recv_index(recv_hdr, cmd);
1c20f0e2 2060 break;
814fcae4
JD
2061 case RELAYD_STREAMS_SENT:
2062 ret = relay_streams_sent(recv_hdr, cmd);
2063 break;
b8aa1682
JD
2064 case RELAYD_UPDATE_SYNC_INFO:
2065 default:
2066 ERR("Received unknown command (%u)", be32toh(recv_hdr->cmd));
2067 relay_unknown_command(cmd);
2068 ret = -1;
2069 goto end;
2070 }
2071
2072end:
2073 return ret;
2074}
2075
7d2f7452
DG
2076/*
2077 * Handle index for a data stream.
2078 *
2079 * RCU read side lock MUST be acquired.
2080 *
2081 * Return 0 on success else a negative value.
2082 */
2083static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num,
2084 int rotate_index)
2085{
2086 int ret = 0, index_created = 0;
2087 uint64_t stream_id, data_offset;
2088 struct relay_index *index, *wr_index = NULL;
2089
2090 assert(stream);
2091
2092 stream_id = stream->stream_handle;
2093 /* Get data offset because we are about to update the index. */
2094 data_offset = htobe64(stream->tracefile_size_current);
2095
2096 /*
2097 * Lookup for an existing index for that stream id/sequence number. If on
2098 * exists, the control thread already received the data for it thus we need
2099 * to write it on disk.
2100 */
2101 index = relay_index_find(stream_id, net_seq_num);
2102 if (!index) {
2103 /* A successful creation will add the object to the HT. */
2104 index = relay_index_create(stream_id, net_seq_num);
2105 if (!index) {
2106 ret = -1;
2107 goto error;
2108 }
2109 index_created = 1;
2110 }
2111
2112 if (rotate_index || stream->index_fd < 0) {
2113 index->to_close_fd = stream->index_fd;
2114 ret = index_create_file(stream->path_name, stream->channel_name,
2115 relayd_uid, relayd_gid, stream->tracefile_size,
2116 stream->tracefile_count_current);
2117 if (ret < 0) {
2118 /* This will close the stream's index fd if one. */
2119 relay_index_free_safe(index);
2120 goto error;
2121 }
2122 stream->index_fd = ret;
2123 }
2124 index->fd = stream->index_fd;
2125 index->index_data.offset = data_offset;
2126
2127 if (index_created) {
2128 /*
2129 * Try to add the relay index object to the hash table. If an object
2130 * already exist, destroy back the index created and set the data.
2131 */
2132 relay_index_add(index, &wr_index);
2133 if (wr_index) {
2134 /* Copy back data from the created index. */
2135 wr_index->fd = index->fd;
2136 wr_index->to_close_fd = index->to_close_fd;
2137 wr_index->index_data.offset = data_offset;
2138 free(index);
2139 }
2140 } else {
2141 /* The index already exists so write it on disk. */
2142 wr_index = index;
2143 }
2144
2145 /* Do we have a writable ready index to write on disk. */
2146 if (wr_index) {
2147 ret = relay_index_write(wr_index->fd, wr_index);
2148 if (ret < 0) {
2149 goto error;
2150 }
2151 stream->total_index_received++;
2152 }
2153
2154error:
2155 return ret;
2156}
2157
b8aa1682
JD
2158/*
2159 * relay_process_data: Process the data received on the data socket
2160 */
2161static
0a6518b0 2162int relay_process_data(struct relay_command *cmd)
b8aa1682 2163{
7d2f7452 2164 int ret = 0, rotate_index = 0;
6cd525e8 2165 ssize_t size_ret;
b8aa1682
JD
2166 struct relay_stream *stream;
2167 struct lttcomm_relayd_data_hdr data_hdr;
7d2f7452 2168 uint64_t stream_id;
173af62f 2169 uint64_t net_seq_num;
b8aa1682
JD
2170 uint32_t data_size;
2171
2172 ret = cmd->sock->ops->recvmsg(cmd->sock, &data_hdr,
7c5aef62 2173 sizeof(struct lttcomm_relayd_data_hdr), 0);
b8aa1682 2174 if (ret <= 0) {
a6cd2b97
DG
2175 if (ret == 0) {
2176 /* Orderly shutdown. Not necessary to print an error. */
2177 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
2178 } else {
2179 ERR("Unable to receive data header on sock %d", cmd->sock->fd);
2180 }
b8aa1682
JD
2181 ret = -1;
2182 goto end;
2183 }
2184
2185 stream_id = be64toh(data_hdr.stream_id);
9d1bbf21
MD
2186
2187 rcu_read_lock();
d3e2ba59 2188 stream = relay_stream_find_by_id(stream_id);
b8aa1682
JD
2189 if (!stream) {
2190 ret = -1;
1c20f0e2 2191 goto end_rcu_unlock;
b8aa1682
JD
2192 }
2193
2194 data_size = be32toh(data_hdr.data_size);
2195 if (data_buffer_size < data_size) {
c617c0c6
MD
2196 char *tmp_data_ptr;
2197
2198 tmp_data_ptr = realloc(data_buffer, data_size);
2199 if (!tmp_data_ptr) {
b8aa1682 2200 ERR("Allocating data buffer");
c617c0c6 2201 free(data_buffer);
b8aa1682 2202 ret = -1;
1c20f0e2 2203 goto end_rcu_unlock;
b8aa1682 2204 }
c617c0c6 2205 data_buffer = tmp_data_ptr;
b8aa1682
JD
2206 data_buffer_size = data_size;
2207 }
2208 memset(data_buffer, 0, data_size);
2209
173af62f
DG
2210 net_seq_num = be64toh(data_hdr.net_seq_num);
2211
77c7c900 2212 DBG3("Receiving data of size %u for stream id %" PRIu64 " seqnum %" PRIu64,
173af62f 2213 data_size, stream_id, net_seq_num);
7c5aef62 2214 ret = cmd->sock->ops->recvmsg(cmd->sock, data_buffer, data_size, 0);
b8aa1682 2215 if (ret <= 0) {
a6cd2b97
DG
2216 if (ret == 0) {
2217 /* Orderly shutdown. Not necessary to print an error. */
2218 DBG("Socket %d did an orderly shutdown", cmd->sock->fd);
2219 }
b8aa1682 2220 ret = -1;
1c20f0e2 2221 goto end_rcu_unlock;
b8aa1682
JD
2222 }
2223
1c20f0e2 2224 /* Check if a rotation is needed. */
0f907de1
JD
2225 if (stream->tracefile_size > 0 &&
2226 (stream->tracefile_size_current + data_size) >
2227 stream->tracefile_size) {
6b6b9a5a
JD
2228 struct relay_viewer_stream *vstream;
2229 uint64_t new_id;
2230
2231 new_id = (stream->tracefile_count_current + 1) %
2232 stream->tracefile_count;
2233 /*
2234 * When we wrap-around back to 0, we start overwriting old
2235 * trace data.
2236 */
2237 if (!stream->tracefile_overwrite && new_id == 0) {
2238 stream->tracefile_overwrite = 1;
2239 }
2240 pthread_mutex_lock(&stream->viewer_stream_rotation_lock);
2241 if (stream->tracefile_overwrite) {
2242 stream->oldest_tracefile_id =
2243 (stream->oldest_tracefile_id + 1) %
2244 stream->tracefile_count;
2245 }
2246 vstream = live_find_viewer_stream_by_id(stream->stream_handle);
2247 if (vstream) {
2248 /*
2249 * The viewer is reading a file about to be
2250 * overwritten. Close the FDs it is
2251 * currently using and let it handle the fault.
2252 */
2253 if (vstream->tracefile_count_current == new_id) {
cef0f7d5 2254 pthread_mutex_lock(&vstream->overwrite_lock);
6b6b9a5a 2255 vstream->abort_flag = 1;
cef0f7d5 2256 pthread_mutex_unlock(&vstream->overwrite_lock);
6b6b9a5a
JD
2257 DBG("Streaming side setting abort_flag on stream %s_%lu\n",
2258 stream->channel_name, new_id);
2259 } else if (vstream->tracefile_count_current ==
2260 stream->tracefile_count_current) {
2261 /*
2262 * The reader and writer were in the
2263 * same trace file, inform the viewer
2264 * that no new index will ever be added
2265 * to this file.
2266 */
2267 vstream->close_write_flag = 1;
2268 }
2269 }
1c20f0e2
JD
2270 ret = utils_rotate_stream_file(stream->path_name, stream->channel_name,
2271 stream->tracefile_size, stream->tracefile_count,
2272 relayd_uid, relayd_gid, stream->fd,
2273 &(stream->tracefile_count_current), &stream->fd);
cef0f7d5 2274 stream->total_index_received = 0;
6b6b9a5a 2275 pthread_mutex_unlock(&stream->viewer_stream_rotation_lock);
0f907de1 2276 if (ret < 0) {
1c20f0e2
JD
2277 ERR("Rotating stream output file");
2278 goto end_rcu_unlock;
0f907de1 2279 }
a6976990
DG
2280 /* Reset current size because we just perform a stream rotation. */
2281 stream->tracefile_size_current = 0;
1c20f0e2
JD
2282 rotate_index = 1;
2283 }
2284
1c20f0e2 2285 /*
7d2f7452
DG
2286 * Index are handled in protocol version 2.4 and above. Also, snapshot and
2287 * index are NOT supported.
1c20f0e2 2288 */
7d2f7452
DG
2289 if (stream->session->minor >= 4 && !stream->session->snapshot) {
2290 ret = handle_index_data(stream, net_seq_num, rotate_index);
1c20f0e2 2291 if (ret < 0) {
1c20f0e2
JD
2292 goto end_rcu_unlock;
2293 }
1c20f0e2
JD
2294 }
2295
7d2f7452 2296 /* Write data to stream output fd. */
6cd525e8
MD
2297 size_ret = lttng_write(stream->fd, data_buffer, data_size);
2298 if (size_ret < data_size) {
b8aa1682
JD
2299 ERR("Relay error writing data to file");
2300 ret = -1;
1c20f0e2 2301 goto end_rcu_unlock;
b8aa1682 2302 }
1d4dfdef 2303
5ab7344e
JD
2304 DBG2("Relay wrote %d bytes to tracefile for stream id %" PRIu64,
2305 ret, stream->stream_handle);
2306
1d4dfdef
DG
2307 ret = write_padding_to_file(stream->fd, be32toh(data_hdr.padding_size));
2308 if (ret < 0) {
1c20f0e2 2309 goto end_rcu_unlock;
1d4dfdef 2310 }
1c20f0e2 2311 stream->tracefile_size_current += data_size + be32toh(data_hdr.padding_size);
1d4dfdef 2312
173af62f
DG
2313 stream->prev_seq = net_seq_num;
2314
2315 /* Check if we need to close the FD */
2316 if (close_stream_check(stream)) {
c07d8a78 2317 destroy_stream(stream);
173af62f
DG
2318 }
2319
1c20f0e2 2320end_rcu_unlock:
9d1bbf21 2321 rcu_read_unlock();
b8aa1682
JD
2322end:
2323 return ret;
2324}
2325
2326static
9d1bbf21 2327void relay_cleanup_poll_connection(struct lttng_poll_event *events, int pollfd)
b8aa1682
JD
2328{
2329 int ret;
2330
b8aa1682
JD
2331 lttng_poll_del(events, pollfd);
2332
2333 ret = close(pollfd);
2334 if (ret < 0) {
2335 ERR("Closing pollfd %d", pollfd);
2336 }
2337}
2338
2339static
2340int relay_add_connection(int fd, struct lttng_poll_event *events,
2341 struct lttng_ht *relay_connections_ht)
2342{
b8aa1682 2343 struct relay_command *relay_connection;
6cd525e8 2344 ssize_t ret;
b8aa1682
JD
2345
2346 relay_connection = zmalloc(sizeof(struct relay_command));
2347 if (relay_connection == NULL) {
2348 PERROR("Relay command zmalloc");
9d1bbf21 2349 goto error;
b8aa1682 2350 }
6cd525e8
MD
2351 ret = lttng_read(fd, relay_connection, sizeof(struct relay_command));
2352 if (ret < sizeof(struct relay_command)) {
b8aa1682 2353 PERROR("read relay cmd pipe");
9d1bbf21 2354 goto error_read;
b8aa1682 2355 }
814fcae4 2356 CDS_INIT_LIST_HEAD(&relay_connection->recv_head);
b8aa1682 2357
c07d8a78
DG
2358 /*
2359 * Only used by the control side and the reference is copied inside each
2360 * stream from that connection. Thus a destroy HT must be done after every
2361 * stream has been destroyed.
2362 */
2363 if (relay_connection->type == RELAY_CONTROL) {
2364 relay_connection->ctf_traces_ht = lttng_ht_new(0,
2365 LTTNG_HT_TYPE_STRING);
2366 if (!relay_connection->ctf_traces_ht) {
2367 goto error_read;
2368 }
d3e2ba59
JD
2369 }
2370
b8aa1682
JD
2371 lttng_ht_node_init_ulong(&relay_connection->sock_n,
2372 (unsigned long) relay_connection->sock->fd);
9d1bbf21 2373 rcu_read_lock();
b8aa1682
JD
2374 lttng_ht_add_unique_ulong(relay_connections_ht,
2375 &relay_connection->sock_n);
9d1bbf21
MD
2376 rcu_read_unlock();
2377 return lttng_poll_add(events,
b8aa1682
JD
2378 relay_connection->sock->fd,
2379 LPOLLIN | LPOLLRDHUP);
2380
9d1bbf21
MD
2381error_read:
2382 free(relay_connection);
2383error:
2384 return -1;
2385}
2386
2387static
2388void deferred_free_connection(struct rcu_head *head)
2389{
2390 struct relay_command *relay_connection =
2391 caa_container_of(head, struct relay_command, rcu_node);
5b6d8097
DG
2392
2393 lttcomm_destroy_sock(relay_connection->sock);
9d1bbf21
MD
2394 free(relay_connection);
2395}
2396
2397static
2398void relay_del_connection(struct lttng_ht *relay_connections_ht,
d3e2ba59
JD
2399 struct lttng_ht_iter *iter, struct relay_command *relay_connection,
2400 struct lttng_ht *sessions_ht)
9d1bbf21
MD
2401{
2402 int ret;
2403
2404 ret = lttng_ht_del(relay_connections_ht, iter);
2405 assert(!ret);
c07d8a78 2406
9d1bbf21 2407 if (relay_connection->type == RELAY_CONTROL) {
814fcae4
JD
2408 struct relay_stream_recv_handle *node, *tmp_node;
2409
d3e2ba59 2410 relay_delete_session(relay_connection, sessions_ht);
c07d8a78 2411 lttng_ht_destroy(relay_connection->ctf_traces_ht);
814fcae4
JD
2412
2413 /* Clean up recv list. */
2414 cds_list_for_each_entry_safe(node, tmp_node,
2415 &relay_connection->recv_head, node) {
2416 cds_list_del(&node->node);
2417 free(node);
2418 }
9d1bbf21 2419 }
5b6d8097 2420
c07d8a78 2421 call_rcu(&relay_connection->rcu_node, deferred_free_connection);
b8aa1682
JD
2422}
2423
2424/*
2425 * This thread does the actual work
2426 */
2427static
2428void *relay_thread_worker(void *data)
2429{
beaad64c
DG
2430 int ret, err = -1, last_seen_data_fd = -1;
2431 uint32_t nb_fd;
b8aa1682
JD
2432 struct relay_command *relay_connection;
2433 struct lttng_poll_event events;
2434 struct lttng_ht *relay_connections_ht;
2435 struct lttng_ht_node_ulong *node;
2436 struct lttng_ht_iter iter;
b8aa1682 2437 struct lttcomm_relayd_hdr recv_hdr;
d3e2ba59
JD
2438 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
2439 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
b8aa1682
JD
2440
2441 DBG("[thread] Relay worker started");
2442
9d1bbf21
MD
2443 rcu_register_thread();
2444
55706a7d
MD
2445 health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER);
2446
4e5b8b82
MD
2447 if (testpoint(relayd_thread_worker)) {
2448 goto error_testpoint;
2449 }
2450
f385ae0a
MD
2451 health_code_update();
2452
b8aa1682
JD
2453 /* table of connections indexed on socket */
2454 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
095a4ae5
MD
2455 if (!relay_connections_ht) {
2456 goto relay_connections_ht_error;
2457 }
b8aa1682 2458
1c20f0e2
JD
2459 /* Tables of received indexes indexed by index handle and net_seq_num. */
2460 indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_TWO_U64);
2461 if (!indexes_ht) {
2462 goto indexes_ht_error;
2463 }
2464
b8aa1682
JD
2465 ret = create_thread_poll_set(&events, 2);
2466 if (ret < 0) {
2467 goto error_poll_create;
2468 }
2469
2470 ret = lttng_poll_add(&events, relay_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
2471 if (ret < 0) {
2472 goto error;
2473 }
2474
beaad64c 2475restart:
b8aa1682 2476 while (1) {
beaad64c
DG
2477 int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1;
2478
f385ae0a
MD
2479 health_code_update();
2480
b8aa1682 2481 /* Infinite blocking call, waiting for transmission */
87c1611d 2482 DBG3("Relayd worker thread polling...");
f385ae0a 2483 health_poll_entry();
b8aa1682 2484 ret = lttng_poll_wait(&events, -1);
f385ae0a 2485 health_poll_exit();
b8aa1682
JD
2486 if (ret < 0) {
2487 /*
2488 * Restart interrupted system call.
2489 */
2490 if (errno == EINTR) {
2491 goto restart;
2492 }
2493 goto error;
2494 }
2495
0d9c5d77
DG
2496 nb_fd = ret;
2497
beaad64c
DG
2498 /*
2499 * Process control. The control connection is prioritised so we don't
2500 * starve it with high throughout put tracing data on the data
2501 * connection.
2502 */
b8aa1682
JD
2503 for (i = 0; i < nb_fd; i++) {
2504 /* Fetch once the poll data */
beaad64c
DG
2505 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
2506 int pollfd = LTTNG_POLL_GETFD(&events, i);
b8aa1682 2507
f385ae0a
MD
2508 health_code_update();
2509
b8aa1682
JD
2510 /* Thread quit pipe has been closed. Killing thread. */
2511 ret = check_thread_quit_pipe(pollfd, revents);
2512 if (ret) {
095a4ae5
MD
2513 err = 0;
2514 goto exit;
b8aa1682
JD
2515 }
2516
2517 /* Inspect the relay cmd pipe for new connection */
2518 if (pollfd == relay_cmd_pipe[0]) {
2519 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2520 ERR("Relay pipe error");
2521 goto error;
2522 } else if (revents & LPOLLIN) {
2523 DBG("Relay command received");
2524 ret = relay_add_connection(relay_cmd_pipe[0],
2525 &events, relay_connections_ht);
2526 if (ret < 0) {
2527 goto error;
2528 }
2529 }
beaad64c 2530 } else if (revents) {
9d1bbf21 2531 rcu_read_lock();
b8aa1682
JD
2532 lttng_ht_lookup(relay_connections_ht,
2533 (void *)((unsigned long) pollfd),
2534 &iter);
2535 node = lttng_ht_iter_get_node_ulong(&iter);
2536 if (node == NULL) {
2537 DBG2("Relay sock %d not found", pollfd);
9d1bbf21 2538 rcu_read_unlock();
b8aa1682
JD
2539 goto error;
2540 }
2541 relay_connection = caa_container_of(node,
2542 struct relay_command, sock_n);
2543
2544 if (revents & (LPOLLERR)) {
2545 ERR("POLL ERROR");
9d1bbf21
MD
2546 relay_cleanup_poll_connection(&events, pollfd);
2547 relay_del_connection(relay_connections_ht,
d3e2ba59 2548 &iter, relay_connection, sessions_ht);
beaad64c
DG
2549 if (last_seen_data_fd == pollfd) {
2550 last_seen_data_fd = last_notdel_data_fd;
2551 }
b8aa1682
JD
2552 } else if (revents & (LPOLLHUP | LPOLLRDHUP)) {
2553 DBG("Socket %d hung up", pollfd);
9d1bbf21
MD
2554 relay_cleanup_poll_connection(&events, pollfd);
2555 relay_del_connection(relay_connections_ht,
d3e2ba59 2556 &iter, relay_connection, sessions_ht);
beaad64c
DG
2557 if (last_seen_data_fd == pollfd) {
2558 last_seen_data_fd = last_notdel_data_fd;
2559 }
b8aa1682
JD
2560 } else if (revents & LPOLLIN) {
2561 /* control socket */
2562 if (relay_connection->type == RELAY_CONTROL) {
2563 ret = relay_connection->sock->ops->recvmsg(
2564 relay_connection->sock, &recv_hdr,
7c5aef62 2565 sizeof(struct lttcomm_relayd_hdr), 0);
b8aa1682
JD
2566 /* connection closed */
2567 if (ret <= 0) {
9d1bbf21
MD
2568 relay_cleanup_poll_connection(&events, pollfd);
2569 relay_del_connection(relay_connections_ht,
d3e2ba59 2570 &iter, relay_connection, sessions_ht);
b8aa1682
JD
2571 DBG("Control connection closed with %d", pollfd);
2572 } else {
2573 if (relay_connection->session) {
77c7c900 2574 DBG2("Relay worker receiving data for session : %" PRIu64,
b8aa1682
JD
2575 relay_connection->session->id);
2576 }
2577 ret = relay_process_control(&recv_hdr,
d3e2ba59 2578 relay_connection, relay_ctx);
b8aa1682 2579 if (ret < 0) {
beaad64c 2580 /* Clear the session on error. */
9d1bbf21
MD
2581 relay_cleanup_poll_connection(&events, pollfd);
2582 relay_del_connection(relay_connections_ht,
d3e2ba59 2583 &iter, relay_connection, sessions_ht);
b8aa1682
JD
2584 DBG("Connection closed with %d", pollfd);
2585 }
beaad64c 2586 seen_control = 1;
b8aa1682 2587 }
beaad64c
DG
2588 } else {
2589 /*
2590 * Flag the last seen data fd not deleted. It will be
2591 * used as the last seen fd if any fd gets deleted in
2592 * this first loop.
2593 */
2594 last_notdel_data_fd = pollfd;
2595 }
2596 }
2597 rcu_read_unlock();
2598 }
2599 }
2600
2601 /*
2602 * The last loop handled a control request, go back to poll to make
2603 * sure we prioritise the control socket.
2604 */
2605 if (seen_control) {
2606 continue;
2607 }
2608
2609 if (last_seen_data_fd >= 0) {
2610 for (i = 0; i < nb_fd; i++) {
2611 int pollfd = LTTNG_POLL_GETFD(&events, i);
f385ae0a
MD
2612
2613 health_code_update();
2614
beaad64c
DG
2615 if (last_seen_data_fd == pollfd) {
2616 idx = i;
2617 break;
2618 }
2619 }
2620 }
2621
2622 /* Process data connection. */
2623 for (i = idx + 1; i < nb_fd; i++) {
2624 /* Fetch the poll data. */
2625 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
2626 int pollfd = LTTNG_POLL_GETFD(&events, i);
2627
f385ae0a
MD
2628 health_code_update();
2629
beaad64c
DG
2630 /* Skip the command pipe. It's handled in the first loop. */
2631 if (pollfd == relay_cmd_pipe[0]) {
2632 continue;
2633 }
2634
2635 if (revents) {
2636 rcu_read_lock();
2637 lttng_ht_lookup(relay_connections_ht,
2638 (void *)((unsigned long) pollfd),
2639 &iter);
2640 node = lttng_ht_iter_get_node_ulong(&iter);
2641 if (node == NULL) {
2642 /* Skip it. Might be removed before. */
2643 rcu_read_unlock();
2644 continue;
2645 }
2646 relay_connection = caa_container_of(node,
2647 struct relay_command, sock_n);
2648
2649 if (revents & LPOLLIN) {
2650 if (relay_connection->type != RELAY_DATA) {
2651 continue;
2652 }
2653
0a6518b0 2654 ret = relay_process_data(relay_connection);
beaad64c
DG
2655 /* connection closed */
2656 if (ret < 0) {
2657 relay_cleanup_poll_connection(&events, pollfd);
2658 relay_del_connection(relay_connections_ht,
d3e2ba59 2659 &iter, relay_connection, sessions_ht);
beaad64c
DG
2660 DBG("Data connection closed with %d", pollfd);
2661 /*
2662 * Every goto restart call sets the last seen fd where
2663 * here we don't really care since we gracefully
2664 * continue the loop after the connection is deleted.
2665 */
2666 } else {
2667 /* Keep last seen port. */
2668 last_seen_data_fd = pollfd;
2669 rcu_read_unlock();
2670 goto restart;
b8aa1682
JD
2671 }
2672 }
9d1bbf21 2673 rcu_read_unlock();
b8aa1682
JD
2674 }
2675 }
beaad64c 2676 last_seen_data_fd = -1;
b8aa1682
JD
2677 }
2678
f385ae0a
MD
2679 /* Normal exit, no error */
2680 ret = 0;
2681
095a4ae5 2682exit:
b8aa1682
JD
2683error:
2684 lttng_poll_clean(&events);
2685
2686 /* empty the hash table and free the memory */
9d1bbf21 2687 rcu_read_lock();
b8aa1682 2688 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, node, node) {
f385ae0a
MD
2689 health_code_update();
2690
b8aa1682
JD
2691 node = lttng_ht_iter_get_node_ulong(&iter);
2692 if (node) {
2693 relay_connection = caa_container_of(node,
2694 struct relay_command, sock_n);
9d1bbf21 2695 relay_del_connection(relay_connections_ht,
d3e2ba59 2696 &iter, relay_connection, sessions_ht);
b8aa1682 2697 }
b8aa1682 2698 }
94d49140 2699 rcu_read_unlock();
7d2f7452
DG
2700error_poll_create:
2701 lttng_ht_destroy(indexes_ht);
1c20f0e2 2702indexes_ht_error:
b8aa1682 2703 lttng_ht_destroy(relay_connections_ht);
095a4ae5 2704relay_connections_ht_error:
6620da75
DG
2705 /* Close relay cmd pipes */
2706 utils_close_pipe(relay_cmd_pipe);
095a4ae5
MD
2707 if (err) {
2708 DBG("Thread exited with error");
2709 }
b8aa1682 2710 DBG("Worker thread cleanup complete");
095a4ae5 2711 free(data_buffer);
4e5b8b82 2712error_testpoint:
f385ae0a
MD
2713 if (err) {
2714 health_error();
2715 ERR("Health error occurred in %s", __func__);
2716 }
2717 health_unregister(health_relayd);
9d1bbf21 2718 rcu_unregister_thread();
f385ae0a 2719 stop_threads();
b8aa1682
JD
2720 return NULL;
2721}
2722
2723/*
2724 * Create the relay command pipe to wake thread_manage_apps.
2725 * Closed in cleanup().
2726 */
2727static int create_relay_cmd_pipe(void)
2728{
a02de639 2729 int ret;
b8aa1682 2730
a02de639 2731 ret = utils_create_pipe_cloexec(relay_cmd_pipe);
b8aa1682 2732
b8aa1682
JD
2733 return ret;
2734}
2735
2736/*
2737 * main
2738 */
2739int main(int argc, char **argv)
2740{
2741 int ret = 0;
2742 void *status;
d3e2ba59 2743 struct relay_local_data *relay_ctx;
b8aa1682 2744
b8aa1682
JD
2745 /* Parse arguments */
2746 progname = argv[0];
c617c0c6 2747 if ((ret = parse_args(argc, argv)) < 0) {
a02de639 2748 goto exit;
b8aa1682
JD
2749 }
2750
2751 if ((ret = set_signal_handler()) < 0) {
2752 goto exit;
2753 }
2754
4d513a50
DG
2755 /* Try to create directory if -o, --output is specified. */
2756 if (opt_output_path) {
994fa64f
DG
2757 if (*opt_output_path != '/') {
2758 ERR("Please specify an absolute path for -o, --output PATH");
2759 goto exit;
2760 }
2761
4d513a50
DG
2762 ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG);
2763 if (ret < 0) {
2764 ERR("Unable to create %s", opt_output_path);
2765 goto exit;
2766 }
2767 }
2768
b8aa1682 2769 /* Daemonize */
d1a3048a 2770 if (opt_daemon || opt_background) {
02a6bb53
MD
2771 int i;
2772
2773 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
2774 !opt_background);
b8aa1682 2775 if (ret < 0) {
a02de639 2776 goto exit;
b8aa1682 2777 }
02a6bb53
MD
2778
2779 /*
2780 * We are in the child. Make sure all other file
2781 * descriptors are closed, in case we are called with
2782 * more opened file descriptors than the standard ones.
2783 */
2784 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
2785 (void) close(i);
2786 }
2787 }
2788
2789 /* Create thread quit pipe */
2790 if ((ret = init_thread_quit_pipe()) < 0) {
2791 goto error;
b8aa1682
JD
2792 }
2793
1c20f0e2
JD
2794 /* We need those values for the file/dir creation. */
2795 relayd_uid = getuid();
2796 relayd_gid = getgid();
b8aa1682 2797
1c20f0e2
JD
2798 /* Check if daemon is UID = 0 */
2799 if (relayd_uid == 0) {
a6a062a4
AM
2800 if (control_uri->port < 1024 || data_uri->port < 1024 ||
2801 live_uri->port < 1024) {
b8aa1682
JD
2802 ERR("Need to be root to use ports < 1024");
2803 ret = -1;
a02de639 2804 goto exit;
b8aa1682
JD
2805 }
2806 }
2807
2808 /* Setup the thread apps communication pipe. */
2809 if ((ret = create_relay_cmd_pipe()) < 0) {
2810 goto exit;
2811 }
2812
2813 /* Init relay command queue. */
2814 cds_wfq_init(&relay_cmd_queue.queue);
2815
2816 /* Set up max poll set size */
2817 lttng_poll_set_max_size();
2818
554831e7
MD
2819 /* Initialize communication library */
2820 lttcomm_init();
d5a5e58f 2821 lttcomm_inet_init();
554831e7 2822
d3e2ba59
JD
2823 relay_ctx = zmalloc(sizeof(struct relay_local_data));
2824 if (!relay_ctx) {
2825 PERROR("relay_ctx");
2826 goto exit;
2827 }
2828
2829 /* tables of sessions indexed by session ID */
2830 relay_ctx->sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2831 if (!relay_ctx->sessions_ht) {
2832 goto exit_relay_ctx_sessions;
2833 }
2834
2835 /* tables of streams indexed by stream ID */
2836 relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2837 if (!relay_streams_ht) {
2838 goto exit_relay_ctx_streams;
2839 }
2840
2841 /* tables of streams indexed by stream ID */
92c6ca54
DG
2842 viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2843 if (!viewer_streams_ht) {
d3e2ba59
JD
2844 goto exit_relay_ctx_viewer_streams;
2845 }
2846
55706a7d
MD
2847 /* Initialize thread health monitoring */
2848 health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES);
2849 if (!health_relayd) {
2850 PERROR("health_app_create error");
2851 goto exit_health_app_create;
2852 }
2853
65931c8b
MD
2854 ret = utils_create_pipe(health_quit_pipe);
2855 if (ret < 0) {
2856 goto error_health_pipe;
2857 }
2858
2859 /* Create thread to manage the client socket */
2860 ret = pthread_create(&health_thread, NULL,
2861 thread_manage_health, (void *) NULL);
2862 if (ret != 0) {
2863 PERROR("pthread_create health");
2864 goto health_error;
2865 }
2866
b8aa1682
JD
2867 /* Setup the dispatcher thread */
2868 ret = pthread_create(&dispatcher_thread, NULL,
2869 relay_thread_dispatcher, (void *) NULL);
2870 if (ret != 0) {
2871 PERROR("pthread_create dispatcher");
2872 goto exit_dispatcher;
2873 }
2874
2875 /* Setup the worker thread */
2876 ret = pthread_create(&worker_thread, NULL,
d3e2ba59 2877 relay_thread_worker, (void *) relay_ctx);
b8aa1682
JD
2878 if (ret != 0) {
2879 PERROR("pthread_create worker");
2880 goto exit_worker;
2881 }
2882
2883 /* Setup the listener thread */
2884 ret = pthread_create(&listener_thread, NULL,
2885 relay_thread_listener, (void *) NULL);
2886 if (ret != 0) {
2887 PERROR("pthread_create listener");
2888 goto exit_listener;
2889 }
2890
3557d456 2891 ret = live_start_threads(live_uri, relay_ctx);
d3e2ba59
JD
2892 if (ret != 0) {
2893 ERR("Starting live viewer threads");
50138f51 2894 goto exit_live;
d3e2ba59
JD
2895 }
2896
50138f51 2897exit_live:
b8aa1682
JD
2898 ret = pthread_join(listener_thread, &status);
2899 if (ret != 0) {
2900 PERROR("pthread_join");
2901 goto error; /* join error, exit without cleanup */
2902 }
2903
50138f51 2904exit_listener:
b8aa1682
JD
2905 ret = pthread_join(worker_thread, &status);
2906 if (ret != 0) {
2907 PERROR("pthread_join");
2908 goto error; /* join error, exit without cleanup */
2909 }
2910
50138f51 2911exit_worker:
b8aa1682
JD
2912 ret = pthread_join(dispatcher_thread, &status);
2913 if (ret != 0) {
2914 PERROR("pthread_join");
2915 goto error; /* join error, exit without cleanup */
2916 }
42415026 2917
50138f51 2918exit_dispatcher:
65931c8b
MD
2919 ret = pthread_join(health_thread, &status);
2920 if (ret != 0) {
2921 PERROR("pthread_join health thread");
2922 goto error; /* join error, exit without cleanup */
2923 }
2924
bd11e201
MD
2925 /*
2926 * Stop live threads only after joining other threads.
2927 */
2928 live_stop_threads();
2929
65931c8b
MD
2930health_error:
2931 utils_close_pipe(health_quit_pipe);
2932
2933error_health_pipe:
55706a7d
MD
2934 health_app_destroy(health_relayd);
2935
2936exit_health_app_create:
92c6ca54 2937 lttng_ht_destroy(viewer_streams_ht);
d3e2ba59
JD
2938
2939exit_relay_ctx_viewer_streams:
2940 lttng_ht_destroy(relay_streams_ht);
2941
2942exit_relay_ctx_streams:
2943 lttng_ht_destroy(relay_ctx->sessions_ht);
2944
2945exit_relay_ctx_sessions:
2946 free(relay_ctx);
b8aa1682
JD
2947
2948exit:
2949 cleanup();
2950 if (!ret) {
2951 exit(EXIT_SUCCESS);
2952 }
a02de639 2953
b8aa1682
JD
2954error:
2955 exit(EXIT_FAILURE);
2956}
This page took 0.187355 seconds and 4 git commands to generate.