relayd: track relayd control connection sockets
[deliverable/lttng-tools.git] / src / bin / lttng-relayd / main.c
1 /*
2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 2 only,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #define _LGPL_SOURCE
22 #include <getopt.h>
23 #include <grp.h>
24 #include <limits.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/mman.h>
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <sys/resource.h>
38 #include <inttypes.h>
39 #include <urcu/futex.h>
40 #include <urcu/uatomic.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <ctype.h>
44
45 #include <lttng/lttng.h>
46 #include <common/common.h>
47 #include <common/compat/poll.h>
48 #include <common/compat/socket.h>
49 #include <common/compat/endian.h>
50 #include <common/compat/getenv.h>
51 #include <common/defaults.h>
52 #include <common/daemonize.h>
53 #include <common/futex.h>
54 #include <common/sessiond-comm/sessiond-comm.h>
55 #include <common/sessiond-comm/inet.h>
56 #include <common/sessiond-comm/relayd.h>
57 #include <common/uri.h>
58 #include <common/utils.h>
59 #include <common/align.h>
60 #include <common/config/session-config.h>
61 #include <common/dynamic-buffer.h>
62 #include <common/buffer-view.h>
63 #include <common/fd-tracker/utils.h>
64 #include <urcu/rculist.h>
65
66 #include "cmd.h"
67 #include "ctf-trace.h"
68 #include "index.h"
69 #include "utils.h"
70 #include "lttng-relayd.h"
71 #include "live.h"
72 #include "health-relayd.h"
73 #include "testpoint.h"
74 #include "viewer-stream.h"
75 #include "session.h"
76 #include "stream.h"
77 #include "connection.h"
78 #include "tracefile-array.h"
79 #include "tcp_keep_alive.h"
80
81 static const char *help_msg =
82 #ifdef LTTNG_EMBED_HELP
83 #include <lttng-relayd.8.h>
84 #else
85 NULL
86 #endif
87 ;
88
89 enum relay_connection_status {
90 RELAY_CONNECTION_STATUS_OK,
91 /* An error occured while processing an event on the connection. */
92 RELAY_CONNECTION_STATUS_ERROR,
93 /* Connection closed/shutdown cleanly. */
94 RELAY_CONNECTION_STATUS_CLOSED,
95 };
96
97 /* command line options */
98 char *opt_output_path;
99 static int opt_daemon, opt_background;
100
101 /*
102 * We need to wait for listener and live listener threads, as well as
103 * health check thread, before being ready to signal readiness.
104 */
105 #define NR_LTTNG_RELAY_READY 3
106 static int lttng_relay_ready = NR_LTTNG_RELAY_READY;
107
108 /* Size of receive buffer. */
109 #define RECV_DATA_BUFFER_SIZE 65536
110 #define FILE_COPY_BUFFER_SIZE 65536
111
112 static int recv_child_signal; /* Set to 1 when a SIGUSR1 signal is received. */
113 static pid_t child_ppid; /* Internal parent PID use with daemonize. */
114
115 static struct lttng_uri *control_uri;
116 static struct lttng_uri *data_uri;
117 static struct lttng_uri *live_uri;
118
119 const char *progname;
120
121 const char *tracing_group_name = DEFAULT_TRACING_GROUP;
122 static int tracing_group_name_override;
123
124 const char * const config_section_name = "relayd";
125
126 /*
127 * Quit pipe for all threads. This permits a single cancellation point
128 * for all threads when receiving an event on the pipe.
129 */
130 int thread_quit_pipe[2] = { -1, -1 };
131
132 /*
133 * This pipe is used to inform the worker thread that a command is queued and
134 * ready to be processed.
135 */
136 static int relay_conn_pipe[2] = { -1, -1 };
137
138 /* Shared between threads */
139 static int dispatch_thread_exit;
140
141 static pthread_t listener_thread;
142 static pthread_t dispatcher_thread;
143 static pthread_t worker_thread;
144 static pthread_t health_thread;
145
146 /*
147 * last_relay_stream_id_lock protects last_relay_stream_id increment
148 * atomicity on 32-bit architectures.
149 */
150 static pthread_mutex_t last_relay_stream_id_lock = PTHREAD_MUTEX_INITIALIZER;
151 static uint64_t last_relay_stream_id;
152
153 /*
154 * Relay command queue.
155 *
156 * The relay_thread_listener and relay_thread_dispatcher communicate with this
157 * queue.
158 */
159 static struct relay_conn_queue relay_conn_queue;
160
161 /* Cap of file desriptors to be in simultaneous use by the relay daemon. */
162 static unsigned int lttng_opt_fd_cap;
163
164 /* Global relay stream hash table. */
165 struct lttng_ht *relay_streams_ht;
166
167 /* Global relay viewer stream hash table. */
168 struct lttng_ht *viewer_streams_ht;
169
170 /* Global relay sessions hash table. */
171 struct lttng_ht *sessions_ht;
172
173 /* Relayd health monitoring */
174 struct health_app *health_relayd;
175
176 /* Global fd tracker. */
177 struct fd_tracker *the_fd_tracker;
178
179 static struct option long_options[] = {
180 { "control-port", 1, 0, 'C', },
181 { "data-port", 1, 0, 'D', },
182 { "live-port", 1, 0, 'L', },
183 { "daemonize", 0, 0, 'd', },
184 { "background", 0, 0, 'b', },
185 { "group", 1, 0, 'g', },
186 { "fd-cap", 1, 0, '\0', },
187 { "help", 0, 0, 'h', },
188 { "output", 1, 0, 'o', },
189 { "verbose", 0, 0, 'v', },
190 { "config", 1, 0, 'f' },
191 { "version", 0, 0, 'V' },
192 { NULL, 0, 0, 0, },
193 };
194
195 static const char *config_ignore_options[] = { "help", "config", "version" };
196
197 /*
198 * Take an option from the getopt output and set it in the right variable to be
199 * used later.
200 *
201 * Return 0 on success else a negative value.
202 */
203 static int set_option(int opt, const char *arg, const char *optname)
204 {
205 int ret;
206
207 switch (opt) {
208 case 0:
209 if (!strcmp(optname, "fd-cap")) {
210 unsigned long v;
211
212 errno = 0;
213 v = strtoul(arg, NULL, 0);
214 if (errno != 0 || !isdigit(arg[0])) {
215 ERR("Wrong value in --fd-cap parameter: %s", arg);
216 ret = -1;
217 goto end;
218 }
219 if (v < DEFAULT_RELAYD_MINIMAL_FD_CAP) {
220 ERR("File descriptor cap must be set to at least %d",
221 DEFAULT_RELAYD_MINIMAL_FD_CAP);
222 }
223 if (v >= UINT_MAX) {
224 ERR("File descriptor cap overflow in --fd-cap parameter: %s", arg);
225 ret = -1;
226 goto end;
227 }
228 lttng_opt_fd_cap = (unsigned int) v;
229 DBG3("File descriptor cap set to %u", lttng_opt_fd_cap);
230
231 } else {
232 fprintf(stderr, "unknown option %s", optname);
233 if (arg) {
234 fprintf(stderr, " with arg %s\n", arg);
235 }
236 }
237 break;
238 case 'C':
239 if (lttng_is_setuid_setgid()) {
240 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
241 "-C, --control-port");
242 } else {
243 ret = uri_parse(arg, &control_uri);
244 if (ret < 0) {
245 ERR("Invalid control URI specified");
246 goto end;
247 }
248 if (control_uri->port == 0) {
249 control_uri->port = DEFAULT_NETWORK_CONTROL_PORT;
250 }
251 }
252 break;
253 case 'D':
254 if (lttng_is_setuid_setgid()) {
255 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
256 "-D, -data-port");
257 } else {
258 ret = uri_parse(arg, &data_uri);
259 if (ret < 0) {
260 ERR("Invalid data URI specified");
261 goto end;
262 }
263 if (data_uri->port == 0) {
264 data_uri->port = DEFAULT_NETWORK_DATA_PORT;
265 }
266 }
267 break;
268 case 'L':
269 if (lttng_is_setuid_setgid()) {
270 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
271 "-L, -live-port");
272 } else {
273 ret = uri_parse(arg, &live_uri);
274 if (ret < 0) {
275 ERR("Invalid live URI specified");
276 goto end;
277 }
278 if (live_uri->port == 0) {
279 live_uri->port = DEFAULT_NETWORK_VIEWER_PORT;
280 }
281 }
282 break;
283 case 'd':
284 opt_daemon = 1;
285 break;
286 case 'b':
287 opt_background = 1;
288 break;
289 case 'g':
290 if (lttng_is_setuid_setgid()) {
291 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
292 "-g, --group");
293 } else {
294 tracing_group_name = strdup(arg);
295 if (tracing_group_name == NULL) {
296 ret = -errno;
297 PERROR("strdup");
298 goto end;
299 }
300 tracing_group_name_override = 1;
301 }
302 break;
303 case 'h':
304 ret = utils_show_help(8, "lttng-relayd", help_msg);
305 if (ret) {
306 ERR("Cannot show --help for `lttng-relayd`");
307 perror("exec");
308 }
309 exit(EXIT_FAILURE);
310 case 'V':
311 fprintf(stdout, "%s\n", VERSION);
312 exit(EXIT_SUCCESS);
313 case 'o':
314 if (lttng_is_setuid_setgid()) {
315 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
316 "-o, --output");
317 } else {
318 ret = asprintf(&opt_output_path, "%s", arg);
319 if (ret < 0) {
320 ret = -errno;
321 PERROR("asprintf opt_output_path");
322 goto end;
323 }
324 }
325 break;
326 case 'v':
327 /* Verbose level can increase using multiple -v */
328 if (arg) {
329 lttng_opt_verbose = config_parse_value(arg);
330 } else {
331 /* Only 3 level of verbosity (-vvv). */
332 if (lttng_opt_verbose < 3) {
333 lttng_opt_verbose += 1;
334 }
335 }
336 break;
337 default:
338 /* Unknown option or other error.
339 * Error is printed by getopt, just return */
340 ret = -1;
341 goto end;
342 }
343
344 /* All good. */
345 ret = 0;
346
347 end:
348 return ret;
349 }
350
351 /*
352 * config_entry_handler_cb used to handle options read from a config file.
353 * See config_entry_handler_cb comment in common/config/session-config.h for the
354 * return value conventions.
355 */
356 static int config_entry_handler(const struct config_entry *entry, void *unused)
357 {
358 int ret = 0, i;
359
360 if (!entry || !entry->name || !entry->value) {
361 ret = -EINVAL;
362 goto end;
363 }
364
365 /* Check if the option is to be ignored */
366 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
367 if (!strcmp(entry->name, config_ignore_options[i])) {
368 goto end;
369 }
370 }
371
372 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; i++) {
373 /* Ignore if entry name is not fully matched. */
374 if (strcmp(entry->name, long_options[i].name)) {
375 continue;
376 }
377
378 /*
379 * If the option takes no argument on the command line,
380 * we have to check if the value is "true". We support
381 * non-zero numeric values, true, on and yes.
382 */
383 if (!long_options[i].has_arg) {
384 ret = config_parse_value(entry->value);
385 if (ret <= 0) {
386 if (ret) {
387 WARN("Invalid configuration value \"%s\" for option %s",
388 entry->value, entry->name);
389 }
390 /* False, skip boolean config option. */
391 goto end;
392 }
393 }
394
395 ret = set_option(long_options[i].val, entry->value, entry->name);
396 goto end;
397 }
398
399 WARN("Unrecognized option \"%s\" in daemon configuration file.",
400 entry->name);
401
402 end:
403 return ret;
404 }
405
406 static int set_options(int argc, char **argv)
407 {
408 int c, ret = 0, option_index = 0, retval = 0;
409 int orig_optopt = optopt, orig_optind = optind;
410 char *default_address, *optstring;
411 const char *config_path = NULL;
412
413 optstring = utils_generate_optstring(long_options,
414 sizeof(long_options) / sizeof(struct option));
415 if (!optstring) {
416 retval = -ENOMEM;
417 goto exit;
418 }
419
420 /* Check for the --config option */
421
422 while ((c = getopt_long(argc, argv, optstring, long_options,
423 &option_index)) != -1) {
424 if (c == '?') {
425 retval = -EINVAL;
426 goto exit;
427 } else if (c != 'f') {
428 continue;
429 }
430
431 if (lttng_is_setuid_setgid()) {
432 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
433 "-f, --config");
434 } else {
435 config_path = utils_expand_path(optarg);
436 if (!config_path) {
437 ERR("Failed to resolve path: %s", optarg);
438 }
439 }
440 }
441
442 ret = config_get_section_entries(config_path, config_section_name,
443 config_entry_handler, NULL);
444 if (ret) {
445 if (ret > 0) {
446 ERR("Invalid configuration option at line %i", ret);
447 }
448 retval = -1;
449 goto exit;
450 }
451
452 /* Reset getopt's global state */
453 optopt = orig_optopt;
454 optind = orig_optind;
455 while (1) {
456 c = getopt_long(argc, argv, optstring, long_options, &option_index);
457 if (c == -1) {
458 break;
459 }
460
461 ret = set_option(c, optarg, long_options[option_index].name);
462 if (ret < 0) {
463 retval = -1;
464 goto exit;
465 }
466 }
467
468 /* assign default values */
469 if (control_uri == NULL) {
470 ret = asprintf(&default_address,
471 "tcp://" DEFAULT_NETWORK_CONTROL_BIND_ADDRESS ":%d",
472 DEFAULT_NETWORK_CONTROL_PORT);
473 if (ret < 0) {
474 PERROR("asprintf default data address");
475 retval = -1;
476 goto exit;
477 }
478
479 ret = uri_parse(default_address, &control_uri);
480 free(default_address);
481 if (ret < 0) {
482 ERR("Invalid control URI specified");
483 retval = -1;
484 goto exit;
485 }
486 }
487 if (data_uri == NULL) {
488 ret = asprintf(&default_address,
489 "tcp://" DEFAULT_NETWORK_DATA_BIND_ADDRESS ":%d",
490 DEFAULT_NETWORK_DATA_PORT);
491 if (ret < 0) {
492 PERROR("asprintf default data address");
493 retval = -1;
494 goto exit;
495 }
496
497 ret = uri_parse(default_address, &data_uri);
498 free(default_address);
499 if (ret < 0) {
500 ERR("Invalid data URI specified");
501 retval = -1;
502 goto exit;
503 }
504 }
505 if (live_uri == NULL) {
506 ret = asprintf(&default_address,
507 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS ":%d",
508 DEFAULT_NETWORK_VIEWER_PORT);
509 if (ret < 0) {
510 PERROR("asprintf default viewer control address");
511 retval = -1;
512 goto exit;
513 }
514
515 ret = uri_parse(default_address, &live_uri);
516 free(default_address);
517 if (ret < 0) {
518 ERR("Invalid viewer control URI specified");
519 retval = -1;
520 goto exit;
521 }
522 }
523 if (lttng_opt_fd_cap == 0) {
524 int ret;
525 struct rlimit rlimit;
526
527 ret = getrlimit(RLIMIT_NOFILE, &rlimit);
528 if (ret) {
529 PERROR("Failed to get file descriptor limit");
530 retval = -1;
531 }
532
533 lttng_opt_fd_cap = rlimit.rlim_cur;
534 }
535
536 exit:
537 free(optstring);
538 return retval;
539 }
540
541 static void print_global_objects(void)
542 {
543 print_viewer_streams();
544 print_relay_streams();
545 print_sessions();
546 }
547
548 /*
549 * Cleanup the daemon
550 */
551 static void relayd_cleanup(void)
552 {
553 print_global_objects();
554
555 DBG("Cleaning up");
556
557 if (viewer_streams_ht)
558 lttng_ht_destroy(viewer_streams_ht);
559 if (relay_streams_ht)
560 lttng_ht_destroy(relay_streams_ht);
561 if (sessions_ht)
562 lttng_ht_destroy(sessions_ht);
563
564 /* free the dynamically allocated opt_output_path */
565 free(opt_output_path);
566
567 /* Close thread quit pipes */
568 (void) fd_tracker_util_pipe_close(the_fd_tracker, thread_quit_pipe);
569
570 uri_free(control_uri);
571 uri_free(data_uri);
572 /* Live URI is freed in the live thread. */
573
574 if (tracing_group_name_override) {
575 free((void *) tracing_group_name);
576 }
577 }
578
579 /*
580 * Write to writable pipe used to notify a thread.
581 */
582 static int notify_thread_pipe(int wpipe)
583 {
584 ssize_t ret;
585
586 ret = lttng_write(wpipe, "!", 1);
587 if (ret < 1) {
588 PERROR("write poll pipe");
589 goto end;
590 }
591 ret = 0;
592 end:
593 return ret;
594 }
595
596 static int notify_health_quit_pipe(int *pipe)
597 {
598 ssize_t ret;
599
600 ret = lttng_write(pipe[1], "4", 1);
601 if (ret < 1) {
602 PERROR("write relay health quit");
603 goto end;
604 }
605 ret = 0;
606 end:
607 return ret;
608 }
609
610 /*
611 * Stop all relayd and relayd-live threads.
612 */
613 int lttng_relay_stop_threads(void)
614 {
615 int retval = 0;
616
617 /* Stopping all threads */
618 DBG("Terminating all threads");
619 if (notify_thread_pipe(thread_quit_pipe[1])) {
620 ERR("write error on thread quit pipe");
621 retval = -1;
622 }
623
624 if (notify_health_quit_pipe(health_quit_pipe)) {
625 ERR("write error on health quit pipe");
626 }
627
628 /* Dispatch thread */
629 CMM_STORE_SHARED(dispatch_thread_exit, 1);
630 futex_nto1_wake(&relay_conn_queue.futex);
631
632 if (relayd_live_stop()) {
633 ERR("Error stopping live threads");
634 retval = -1;
635 }
636 return retval;
637 }
638
639 /*
640 * Signal handler for the daemon
641 *
642 * Simply stop all worker threads, leaving main() return gracefully after
643 * joining all threads and calling cleanup().
644 */
645 static void sighandler(int sig)
646 {
647 switch (sig) {
648 case SIGINT:
649 DBG("SIGINT caught");
650 if (lttng_relay_stop_threads()) {
651 ERR("Error stopping threads");
652 }
653 break;
654 case SIGTERM:
655 DBG("SIGTERM caught");
656 if (lttng_relay_stop_threads()) {
657 ERR("Error stopping threads");
658 }
659 break;
660 case SIGUSR1:
661 CMM_STORE_SHARED(recv_child_signal, 1);
662 break;
663 default:
664 break;
665 }
666 }
667
668 /*
669 * Setup signal handler for :
670 * SIGINT, SIGTERM, SIGPIPE
671 */
672 static int set_signal_handler(void)
673 {
674 int ret = 0;
675 struct sigaction sa;
676 sigset_t sigset;
677
678 if ((ret = sigemptyset(&sigset)) < 0) {
679 PERROR("sigemptyset");
680 return ret;
681 }
682
683 sa.sa_mask = sigset;
684 sa.sa_flags = 0;
685
686 sa.sa_handler = sighandler;
687 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
688 PERROR("sigaction");
689 return ret;
690 }
691
692 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
693 PERROR("sigaction");
694 return ret;
695 }
696
697 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
698 PERROR("sigaction");
699 return ret;
700 }
701
702 sa.sa_handler = SIG_IGN;
703 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
704 PERROR("sigaction");
705 return ret;
706 }
707
708 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
709
710 return ret;
711 }
712
713 void lttng_relay_notify_ready(void)
714 {
715 /* Notify the parent of the fork() process that we are ready. */
716 if (opt_daemon || opt_background) {
717 if (uatomic_sub_return(&lttng_relay_ready, 1) == 0) {
718 kill(child_ppid, SIGUSR1);
719 }
720 }
721 }
722
723 /*
724 * Init thread quit pipe.
725 *
726 * Return -1 on error or 0 if all pipes are created.
727 */
728 static int init_thread_quit_pipe(void)
729 {
730 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
731 "Quit pipe", thread_quit_pipe);
732 }
733
734 /*
735 * Init health quit pipe.
736 *
737 * Return -1 on error or 0 if all pipes are created.
738 */
739 static int init_health_quit_pipe(void)
740 {
741 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
742 "Health quit pipe", health_quit_pipe);
743 }
744
745 /*
746 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
747 */
748 static int create_named_thread_poll_set(struct lttng_poll_event *events,
749 int size, const char *name)
750 {
751 int ret;
752
753 if (events == NULL || size == 0) {
754 ret = -1;
755 goto error;
756 }
757
758 ret = fd_tracker_util_poll_create(the_fd_tracker,
759 name, events, 1, LTTNG_CLOEXEC);
760
761 /* Add quit pipe */
762 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
763 if (ret < 0) {
764 goto error;
765 }
766
767 return 0;
768
769 error:
770 return ret;
771 }
772
773 /*
774 * Check if the thread quit pipe was triggered.
775 *
776 * Return 1 if it was triggered else 0;
777 */
778 static int check_thread_quit_pipe(int fd, uint32_t events)
779 {
780 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
781 return 1;
782 }
783
784 return 0;
785 }
786
787 static int create_sock(void *data, int *out_fd)
788 {
789 int ret;
790 struct lttcomm_sock *sock = data;
791
792 ret = lttcomm_create_sock(sock);
793 if (ret < 0) {
794 goto end;
795 }
796
797 *out_fd = sock->fd;
798 end:
799 return ret;
800 }
801
802 static int close_sock(void *data, int *in_fd)
803 {
804 struct lttcomm_sock *sock = data;
805
806 return sock->ops->close(sock);
807 }
808
809 static int accept_sock(void *data, int *out_fd)
810 {
811 int ret = 0;
812 /* Socks is an array of in_sock, out_sock. */
813 struct lttcomm_sock **socks = data;
814 struct lttcomm_sock *in_sock = socks[0];
815
816 socks[1] = in_sock->ops->accept(in_sock);
817 if (!socks[1]) {
818 ret = -1;
819 goto end;
820 }
821 *out_fd = socks[1]->fd;
822 end:
823 return ret;
824 }
825
826 /*
827 * Create and init socket from uri.
828 */
829 static struct lttcomm_sock *relay_socket_create(struct lttng_uri *uri,
830 const char *name)
831 {
832 int ret, sock_fd;
833 struct lttcomm_sock *sock = NULL;
834 char uri_str[PATH_MAX];
835 char *formated_name = NULL;
836
837 sock = lttcomm_alloc_sock_from_uri(uri);
838 if (sock == NULL) {
839 ERR("Allocating socket");
840 goto error;
841 }
842
843 /*
844 * Don't fail to create the socket if the name can't be built as it is
845 * only used for debugging purposes.
846 */
847 ret = uri_to_str_url(uri, uri_str, sizeof(uri_str));
848 uri_str[sizeof(uri_str) - 1] = '\0';
849 if (ret >= 0) {
850 ret = asprintf(&formated_name, "%s socket @ %s", name,
851 uri_str);
852 if (ret < 0) {
853 formated_name = NULL;
854 }
855 }
856
857 ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, &sock_fd,
858 (const char **) (formated_name ? &formated_name : NULL),
859 1, create_sock, sock);
860 free(formated_name);
861 DBG("Listening on %s socket %d", name, sock->fd);
862
863 ret = sock->ops->bind(sock);
864 if (ret < 0) {
865 PERROR("Failed to bind socket");
866 goto error;
867 }
868
869 ret = sock->ops->listen(sock, -1);
870 if (ret < 0) {
871 goto error;
872
873 }
874
875 return sock;
876
877 error:
878 if (sock) {
879 lttcomm_destroy_sock(sock);
880 }
881 return NULL;
882 }
883
884 static
885 struct lttcomm_sock *accept_relayd_sock(struct lttcomm_sock *listening_sock,
886 const char *name)
887 {
888 int out_fd, ret;
889 struct lttcomm_sock *socks[2] = { listening_sock, NULL };
890 struct lttcomm_sock *new_sock = NULL;
891
892 ret = fd_tracker_open_unsuspendable_fd(
893 the_fd_tracker, &out_fd,
894 (const char **) &name,
895 1, accept_sock, &socks);
896 if (ret) {
897 goto end;
898 }
899 new_sock = socks[1];
900 DBG("%s accepted, socket %d", name, new_sock->fd);
901 end:
902 return new_sock;
903 }
904
905 /*
906 * This thread manages the listening for new connections on the network
907 */
908 static void *relay_thread_listener(void *data)
909 {
910 int i, ret, pollfd, err = -1;
911 uint32_t revents, nb_fd;
912 struct lttng_poll_event events;
913 struct lttcomm_sock *control_sock, *data_sock;
914
915 DBG("[thread] Relay listener started");
916
917 health_register(health_relayd, HEALTH_RELAYD_TYPE_LISTENER);
918
919 health_code_update();
920
921 control_sock = relay_socket_create(control_uri, "Control listener");
922 if (!control_sock) {
923 goto error_sock_control;
924 }
925
926 data_sock = relay_socket_create(data_uri, "Data listener");
927 if (!data_sock) {
928 goto error_sock_relay;
929 }
930
931 /*
932 * Pass 3 as size here for the thread quit pipe, control and
933 * data socket.
934 */
935 ret = create_named_thread_poll_set(&events, 3, "Listener thread epoll");
936 if (ret < 0) {
937 goto error_create_poll;
938 }
939
940 /* Add the control socket */
941 ret = lttng_poll_add(&events, control_sock->fd, LPOLLIN | LPOLLRDHUP);
942 if (ret < 0) {
943 goto error_poll_add;
944 }
945
946 /* Add the data socket */
947 ret = lttng_poll_add(&events, data_sock->fd, LPOLLIN | LPOLLRDHUP);
948 if (ret < 0) {
949 goto error_poll_add;
950 }
951
952 lttng_relay_notify_ready();
953
954 if (testpoint(relayd_thread_listener)) {
955 goto error_testpoint;
956 }
957
958 while (1) {
959 health_code_update();
960
961 DBG("Listener accepting connections");
962
963 restart:
964 health_poll_entry();
965 ret = lttng_poll_wait(&events, -1);
966 health_poll_exit();
967 if (ret < 0) {
968 /*
969 * Restart interrupted system call.
970 */
971 if (errno == EINTR) {
972 goto restart;
973 }
974 goto error;
975 }
976
977 nb_fd = ret;
978
979 DBG("Relay new connection received");
980 for (i = 0; i < nb_fd; i++) {
981 health_code_update();
982
983 /* Fetch once the poll data */
984 revents = LTTNG_POLL_GETEV(&events, i);
985 pollfd = LTTNG_POLL_GETFD(&events, i);
986
987 if (!revents) {
988 /*
989 * No activity for this FD (poll
990 * implementation).
991 */
992 continue;
993 }
994
995 /* Thread quit pipe has been closed. Killing thread. */
996 ret = check_thread_quit_pipe(pollfd, revents);
997 if (ret) {
998 err = 0;
999 goto exit;
1000 }
1001
1002 if (revents & LPOLLIN) {
1003 /*
1004 * A new connection is requested, therefore a
1005 * sessiond/consumerd connection is allocated in
1006 * this thread, enqueued to a global queue and
1007 * dequeued (and freed) in the worker thread.
1008 */
1009 int val = 1;
1010 struct relay_connection *new_conn;
1011 struct lttcomm_sock *newsock = NULL;
1012 enum connection_type type;
1013
1014 if (pollfd == data_sock->fd) {
1015 type = RELAY_DATA;
1016 newsock = accept_relayd_sock(data_sock,
1017 "Data socket to relayd");
1018 } else {
1019 assert(pollfd == control_sock->fd);
1020 type = RELAY_CONTROL;
1021 newsock = accept_relayd_sock(control_sock,
1022 "Control socket to relayd");
1023 }
1024 if (!newsock) {
1025 PERROR("accepting sock");
1026 goto error;
1027 }
1028
1029 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
1030 sizeof(val));
1031 if (ret < 0) {
1032 PERROR("setsockopt inet");
1033 lttcomm_destroy_sock(newsock);
1034 goto error;
1035 }
1036
1037 ret = socket_apply_keep_alive_config(newsock->fd);
1038 if (ret < 0) {
1039 ERR("Failed to apply TCP keep-alive configuration on socket (%i)",
1040 newsock->fd);
1041 lttcomm_destroy_sock(newsock);
1042 goto error;
1043 }
1044
1045 new_conn = connection_create(newsock, type);
1046 if (!new_conn) {
1047 lttcomm_destroy_sock(newsock);
1048 goto error;
1049 }
1050
1051 /* Enqueue request for the dispatcher thread. */
1052 cds_wfcq_enqueue(&relay_conn_queue.head, &relay_conn_queue.tail,
1053 &new_conn->qnode);
1054
1055 /*
1056 * Wake the dispatch queue futex.
1057 * Implicit memory barrier with the
1058 * exchange in cds_wfcq_enqueue.
1059 */
1060 futex_nto1_wake(&relay_conn_queue.futex);
1061 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1062 ERR("socket poll error");
1063 goto error;
1064 } else {
1065 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1066 goto error;
1067 }
1068 }
1069 }
1070
1071 exit:
1072 error:
1073 error_poll_add:
1074 error_testpoint:
1075 (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
1076 error_create_poll:
1077 if (data_sock->fd >= 0) {
1078 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker,
1079 &data_sock->fd, 1, close_sock,
1080 data_sock);
1081 if (ret) {
1082 PERROR("close");
1083 }
1084 }
1085 lttcomm_destroy_sock(data_sock);
1086 error_sock_relay:
1087 if (control_sock->fd >= 0) {
1088 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker,
1089 &control_sock->fd, 1, close_sock,
1090 control_sock);
1091 if (ret) {
1092 PERROR("close");
1093 }
1094 }
1095 lttcomm_destroy_sock(control_sock);
1096 error_sock_control:
1097 if (err) {
1098 health_error();
1099 ERR("Health error occurred in %s", __func__);
1100 }
1101 health_unregister(health_relayd);
1102 DBG("Relay listener thread cleanup complete");
1103 lttng_relay_stop_threads();
1104 return NULL;
1105 }
1106
1107 /*
1108 * This thread manages the dispatching of the requests to worker threads
1109 */
1110 static void *relay_thread_dispatcher(void *data)
1111 {
1112 int err = -1;
1113 ssize_t ret;
1114 struct cds_wfcq_node *node;
1115 struct relay_connection *new_conn = NULL;
1116
1117 DBG("[thread] Relay dispatcher started");
1118
1119 health_register(health_relayd, HEALTH_RELAYD_TYPE_DISPATCHER);
1120
1121 if (testpoint(relayd_thread_dispatcher)) {
1122 goto error_testpoint;
1123 }
1124
1125 health_code_update();
1126
1127 for (;;) {
1128 health_code_update();
1129
1130 /* Atomically prepare the queue futex */
1131 futex_nto1_prepare(&relay_conn_queue.futex);
1132
1133 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
1134 break;
1135 }
1136
1137 do {
1138 health_code_update();
1139
1140 /* Dequeue commands */
1141 node = cds_wfcq_dequeue_blocking(&relay_conn_queue.head,
1142 &relay_conn_queue.tail);
1143 if (node == NULL) {
1144 DBG("Woken up but nothing in the relay command queue");
1145 /* Continue thread execution */
1146 break;
1147 }
1148 new_conn = caa_container_of(node, struct relay_connection, qnode);
1149
1150 DBG("Dispatching request waiting on sock %d", new_conn->sock->fd);
1151
1152 /*
1153 * Inform worker thread of the new request. This
1154 * call is blocking so we can be assured that
1155 * the data will be read at some point in time
1156 * or wait to the end of the world :)
1157 */
1158 ret = lttng_write(relay_conn_pipe[1], &new_conn, sizeof(new_conn));
1159 if (ret < 0) {
1160 PERROR("write connection pipe");
1161 connection_put(new_conn);
1162 goto error;
1163 }
1164 } while (node != NULL);
1165
1166 /* Futex wait on queue. Blocking call on futex() */
1167 health_poll_entry();
1168 futex_nto1_wait(&relay_conn_queue.futex);
1169 health_poll_exit();
1170 }
1171
1172 /* Normal exit, no error */
1173 err = 0;
1174
1175 error:
1176 error_testpoint:
1177 if (err) {
1178 health_error();
1179 ERR("Health error occurred in %s", __func__);
1180 }
1181 health_unregister(health_relayd);
1182 DBG("Dispatch thread dying");
1183 lttng_relay_stop_threads();
1184 return NULL;
1185 }
1186
1187 /*
1188 * Set index data from the control port to a given index object.
1189 */
1190 static int set_index_control_data(struct relay_index *index,
1191 struct lttcomm_relayd_index *data,
1192 struct relay_connection *conn)
1193 {
1194 struct ctf_packet_index index_data;
1195
1196 /*
1197 * The index on disk is encoded in big endian.
1198 */
1199 index_data.packet_size = htobe64(data->packet_size);
1200 index_data.content_size = htobe64(data->content_size);
1201 index_data.timestamp_begin = htobe64(data->timestamp_begin);
1202 index_data.timestamp_end = htobe64(data->timestamp_end);
1203 index_data.events_discarded = htobe64(data->events_discarded);
1204 index_data.stream_id = htobe64(data->stream_id);
1205
1206 if (conn->minor >= 8) {
1207 index->index_data.stream_instance_id = htobe64(data->stream_instance_id);
1208 index->index_data.packet_seq_num = htobe64(data->packet_seq_num);
1209 }
1210
1211 return relay_index_set_data(index, &index_data);
1212 }
1213
1214 /*
1215 * Handle the RELAYD_CREATE_SESSION command.
1216 *
1217 * On success, send back the session id or else return a negative value.
1218 */
1219 static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr,
1220 struct relay_connection *conn,
1221 const struct lttng_buffer_view *payload)
1222 {
1223 int ret = 0;
1224 ssize_t send_ret;
1225 struct relay_session *session;
1226 struct lttcomm_relayd_status_session reply;
1227 char session_name[LTTNG_NAME_MAX];
1228 char hostname[LTTNG_HOST_NAME_MAX];
1229 uint32_t live_timer = 0;
1230 bool snapshot = false;
1231
1232 memset(session_name, 0, LTTNG_NAME_MAX);
1233 memset(hostname, 0, LTTNG_HOST_NAME_MAX);
1234
1235 memset(&reply, 0, sizeof(reply));
1236
1237 if (conn->minor < 4) {
1238 /* From 2.1 to 2.3 */
1239 ret = 0;
1240 } else if (conn->minor >= 4 && conn->minor < 11) {
1241 /* From 2.4 to 2.10 */
1242 ret = cmd_create_session_2_4(payload, session_name,
1243 hostname, &live_timer, &snapshot);
1244 } else {
1245 /* From 2.11 to ... */
1246 ret = cmd_create_session_2_11(payload, session_name,
1247 hostname, &live_timer, &snapshot);
1248 }
1249
1250 if (ret < 0) {
1251 goto send_reply;
1252 }
1253
1254 session = session_create(session_name, hostname, live_timer,
1255 snapshot, conn->major, conn->minor);
1256 if (!session) {
1257 ret = -1;
1258 goto send_reply;
1259 }
1260 assert(!conn->session);
1261 conn->session = session;
1262 DBG("Created session %" PRIu64, session->id);
1263
1264 reply.session_id = htobe64(session->id);
1265
1266 send_reply:
1267 if (ret < 0) {
1268 reply.ret_code = htobe32(LTTNG_ERR_FATAL);
1269 } else {
1270 reply.ret_code = htobe32(LTTNG_OK);
1271 }
1272
1273 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
1274 if (send_ret < (ssize_t) sizeof(reply)) {
1275 ERR("Failed to send \"create session\" command reply (ret = %zd)",
1276 send_ret);
1277 ret = -1;
1278 }
1279
1280 return ret;
1281 }
1282
1283 /*
1284 * When we have received all the streams and the metadata for a channel,
1285 * we make them visible to the viewer threads.
1286 */
1287 static void publish_connection_local_streams(struct relay_connection *conn)
1288 {
1289 struct relay_stream *stream;
1290 struct relay_session *session = conn->session;
1291
1292 /*
1293 * We publish all streams belonging to a session atomically wrt
1294 * session lock.
1295 */
1296 pthread_mutex_lock(&session->lock);
1297 rcu_read_lock();
1298 cds_list_for_each_entry_rcu(stream, &session->recv_list,
1299 recv_node) {
1300 stream_publish(stream);
1301 }
1302 rcu_read_unlock();
1303
1304 /*
1305 * Inform the viewer that there are new streams in the session.
1306 */
1307 if (session->viewer_attached) {
1308 uatomic_set(&session->new_streams, 1);
1309 }
1310 pthread_mutex_unlock(&session->lock);
1311 }
1312
1313 /*
1314 * relay_add_stream: allocate a new stream for a session
1315 */
1316 static int relay_add_stream(const struct lttcomm_relayd_hdr *recv_hdr,
1317 struct relay_connection *conn,
1318 const struct lttng_buffer_view *payload)
1319 {
1320 int ret;
1321 ssize_t send_ret;
1322 struct relay_session *session = conn->session;
1323 struct relay_stream *stream = NULL;
1324 struct lttcomm_relayd_status_stream reply;
1325 struct ctf_trace *trace = NULL;
1326 uint64_t stream_handle = -1ULL;
1327 char *path_name = NULL, *channel_name = NULL;
1328 uint64_t tracefile_size = 0, tracefile_count = 0;
1329 struct relay_stream_chunk_id stream_chunk_id = { 0 };
1330
1331 if (!session || !conn->version_check_done) {
1332 ERR("Trying to add a stream before version check");
1333 ret = -1;
1334 goto end_no_session;
1335 }
1336
1337 if (session->minor == 1) {
1338 /* For 2.1 */
1339 ret = cmd_recv_stream_2_1(payload, &path_name,
1340 &channel_name);
1341 } else if (session->minor > 1 && session->minor < 11) {
1342 /* From 2.2 to 2.10 */
1343 ret = cmd_recv_stream_2_2(payload, &path_name,
1344 &channel_name, &tracefile_size, &tracefile_count);
1345 } else {
1346 /* From 2.11 to ... */
1347 ret = cmd_recv_stream_2_11(payload, &path_name,
1348 &channel_name, &tracefile_size, &tracefile_count,
1349 &stream_chunk_id.value);
1350 stream_chunk_id.is_set = true;
1351 }
1352
1353 if (ret < 0) {
1354 goto send_reply;
1355 }
1356
1357 trace = ctf_trace_get_by_path_or_create(session, path_name);
1358 if (!trace) {
1359 goto send_reply;
1360 }
1361 /* This stream here has one reference on the trace. */
1362
1363 pthread_mutex_lock(&last_relay_stream_id_lock);
1364 stream_handle = ++last_relay_stream_id;
1365 pthread_mutex_unlock(&last_relay_stream_id_lock);
1366
1367 /* We pass ownership of path_name and channel_name. */
1368 stream = stream_create(trace, stream_handle, path_name,
1369 channel_name, tracefile_size, tracefile_count,
1370 &stream_chunk_id);
1371 path_name = NULL;
1372 channel_name = NULL;
1373
1374 /*
1375 * Streams are the owners of their trace. Reference to trace is
1376 * kept within stream_create().
1377 */
1378 ctf_trace_put(trace);
1379
1380 send_reply:
1381 memset(&reply, 0, sizeof(reply));
1382 reply.handle = htobe64(stream_handle);
1383 if (!stream) {
1384 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1385 } else {
1386 reply.ret_code = htobe32(LTTNG_OK);
1387 }
1388
1389 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1390 sizeof(struct lttcomm_relayd_status_stream), 0);
1391 if (send_ret < (ssize_t) sizeof(reply)) {
1392 ERR("Failed to send \"add stream\" command reply (ret = %zd)",
1393 send_ret);
1394 ret = -1;
1395 }
1396
1397 end_no_session:
1398 free(path_name);
1399 free(channel_name);
1400 return ret;
1401 }
1402
1403 /*
1404 * relay_close_stream: close a specific stream
1405 */
1406 static int relay_close_stream(const struct lttcomm_relayd_hdr *recv_hdr,
1407 struct relay_connection *conn,
1408 const struct lttng_buffer_view *payload)
1409 {
1410 int ret;
1411 ssize_t send_ret;
1412 struct relay_session *session = conn->session;
1413 struct lttcomm_relayd_close_stream stream_info;
1414 struct lttcomm_relayd_generic_reply reply;
1415 struct relay_stream *stream;
1416
1417 DBG("Close stream received");
1418
1419 if (!session || !conn->version_check_done) {
1420 ERR("Trying to close a stream before version check");
1421 ret = -1;
1422 goto end_no_session;
1423 }
1424
1425 if (payload->size < sizeof(stream_info)) {
1426 ERR("Unexpected payload size in \"relay_close_stream\": expected >= %zu bytes, got %zu bytes",
1427 sizeof(stream_info), payload->size);
1428 ret = -1;
1429 goto end_no_session;
1430 }
1431 memcpy(&stream_info, payload->data, sizeof(stream_info));
1432 stream_info.stream_id = be64toh(stream_info.stream_id);
1433 stream_info.last_net_seq_num = be64toh(stream_info.last_net_seq_num);
1434
1435 stream = stream_get_by_id(stream_info.stream_id);
1436 if (!stream) {
1437 ret = -1;
1438 goto end;
1439 }
1440
1441 /*
1442 * Set last_net_seq_num before the close flag. Required by data
1443 * pending check.
1444 */
1445 pthread_mutex_lock(&stream->lock);
1446 stream->last_net_seq_num = stream_info.last_net_seq_num;
1447 pthread_mutex_unlock(&stream->lock);
1448
1449 /*
1450 * This is one of the conditions which may trigger a stream close
1451 * with the others being:
1452 * 1) A close command is received for a stream
1453 * 2) The control connection owning the stream is closed
1454 * 3) We have received all of the stream's data _after_ a close
1455 * request.
1456 */
1457 try_stream_close(stream);
1458 if (stream->is_metadata) {
1459 struct relay_viewer_stream *vstream;
1460
1461 vstream = viewer_stream_get_by_id(stream->stream_handle);
1462 if (vstream) {
1463 if (vstream->metadata_sent == stream->metadata_received) {
1464 /*
1465 * Since all the metadata has been sent to the
1466 * viewer and that we have a request to close
1467 * its stream, we can safely teardown the
1468 * corresponding metadata viewer stream.
1469 */
1470 viewer_stream_put(vstream);
1471 }
1472 /* Put local reference. */
1473 viewer_stream_put(vstream);
1474 }
1475 }
1476 stream_put(stream);
1477 ret = 0;
1478
1479 end:
1480 memset(&reply, 0, sizeof(reply));
1481 if (ret < 0) {
1482 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1483 } else {
1484 reply.ret_code = htobe32(LTTNG_OK);
1485 }
1486 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1487 sizeof(struct lttcomm_relayd_generic_reply), 0);
1488 if (send_ret < (ssize_t) sizeof(reply)) {
1489 ERR("Failed to send \"close stream\" command reply (ret = %zd)",
1490 send_ret);
1491 ret = -1;
1492 }
1493
1494 end_no_session:
1495 return ret;
1496 }
1497
1498 /*
1499 * relay_reset_metadata: reset a metadata stream
1500 */
1501 static
1502 int relay_reset_metadata(const struct lttcomm_relayd_hdr *recv_hdr,
1503 struct relay_connection *conn,
1504 const struct lttng_buffer_view *payload)
1505 {
1506 int ret;
1507 ssize_t send_ret;
1508 struct relay_session *session = conn->session;
1509 struct lttcomm_relayd_reset_metadata stream_info;
1510 struct lttcomm_relayd_generic_reply reply;
1511 struct relay_stream *stream;
1512
1513 DBG("Reset metadata received");
1514
1515 if (!session || !conn->version_check_done) {
1516 ERR("Trying to reset a metadata stream before version check");
1517 ret = -1;
1518 goto end_no_session;
1519 }
1520
1521 if (payload->size < sizeof(stream_info)) {
1522 ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
1523 sizeof(stream_info), payload->size);
1524 ret = -1;
1525 goto end_no_session;
1526 }
1527 memcpy(&stream_info, payload->data, sizeof(stream_info));
1528 stream_info.stream_id = be64toh(stream_info.stream_id);
1529 stream_info.version = be64toh(stream_info.version);
1530
1531 DBG("Update metadata to version %" PRIu64, stream_info.version);
1532
1533 /* Unsupported for live sessions for now. */
1534 if (session->live_timer != 0) {
1535 ret = -1;
1536 goto end;
1537 }
1538
1539 stream = stream_get_by_id(stream_info.stream_id);
1540 if (!stream) {
1541 ret = -1;
1542 goto end;
1543 }
1544 pthread_mutex_lock(&stream->lock);
1545 if (!stream->is_metadata) {
1546 ret = -1;
1547 goto end_unlock;
1548 }
1549
1550 ret = utils_rotate_stream_file(stream->path_name, stream->channel_name,
1551 0, 0, -1, -1, stream->stream_fd->fd, NULL,
1552 &stream->stream_fd->fd);
1553 if (ret < 0) {
1554 ERR("Failed to rotate metadata file %s of channel %s",
1555 stream->path_name, stream->channel_name);
1556 goto end_unlock;
1557 }
1558
1559 end_unlock:
1560 pthread_mutex_unlock(&stream->lock);
1561 stream_put(stream);
1562
1563 end:
1564 memset(&reply, 0, sizeof(reply));
1565 if (ret < 0) {
1566 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1567 } else {
1568 reply.ret_code = htobe32(LTTNG_OK);
1569 }
1570 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1571 sizeof(struct lttcomm_relayd_generic_reply), 0);
1572 if (send_ret < (ssize_t) sizeof(reply)) {
1573 ERR("Failed to send \"reset metadata\" command reply (ret = %zd)",
1574 send_ret);
1575 ret = -1;
1576 }
1577
1578 end_no_session:
1579 return ret;
1580 }
1581
1582 /*
1583 * relay_unknown_command: send -1 if received unknown command
1584 */
1585 static void relay_unknown_command(struct relay_connection *conn)
1586 {
1587 struct lttcomm_relayd_generic_reply reply;
1588 ssize_t send_ret;
1589
1590 memset(&reply, 0, sizeof(reply));
1591 reply.ret_code = htobe32(LTTNG_ERR_UNK);
1592 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
1593 if (send_ret < sizeof(reply)) {
1594 ERR("Failed to send \"unknown command\" command reply (ret = %zd)", send_ret);
1595 }
1596 }
1597
1598 /*
1599 * relay_start: send an acknowledgment to the client to tell if we are
1600 * ready to receive data. We are ready if a session is established.
1601 */
1602 static int relay_start(const struct lttcomm_relayd_hdr *recv_hdr,
1603 struct relay_connection *conn,
1604 const struct lttng_buffer_view *payload)
1605 {
1606 int ret = 0;
1607 ssize_t send_ret;
1608 struct lttcomm_relayd_generic_reply reply;
1609 struct relay_session *session = conn->session;
1610
1611 if (!session) {
1612 DBG("Trying to start the streaming without a session established");
1613 ret = htobe32(LTTNG_ERR_UNK);
1614 }
1615
1616 memset(&reply, 0, sizeof(reply));
1617 reply.ret_code = htobe32(LTTNG_OK);
1618 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
1619 sizeof(reply), 0);
1620 if (send_ret < (ssize_t) sizeof(reply)) {
1621 ERR("Failed to send \"relay_start\" command reply (ret = %zd)",
1622 send_ret);
1623 ret = -1;
1624 }
1625
1626 return ret;
1627 }
1628
1629 /*
1630 * Append padding to the file pointed by the file descriptor fd.
1631 */
1632 static int write_padding_to_file(int fd, uint32_t size)
1633 {
1634 ssize_t ret = 0;
1635 char *zeros;
1636
1637 if (size == 0) {
1638 goto end;
1639 }
1640
1641 zeros = zmalloc(size);
1642 if (zeros == NULL) {
1643 PERROR("zmalloc zeros for padding");
1644 ret = -1;
1645 goto end;
1646 }
1647
1648 ret = lttng_write(fd, zeros, size);
1649 if (ret < size) {
1650 PERROR("write padding to file");
1651 }
1652
1653 free(zeros);
1654
1655 end:
1656 return ret;
1657 }
1658
1659 /*
1660 * Close the current index file if it is open, and create a new one.
1661 *
1662 * Return 0 on success, -1 on error.
1663 */
1664 static
1665 int create_rotate_index_file(struct relay_stream *stream)
1666 {
1667 int ret;
1668 uint32_t major, minor;
1669
1670 /* Put ref on previous index_file. */
1671 if (stream->index_file) {
1672 lttng_index_file_put(stream->index_file);
1673 stream->index_file = NULL;
1674 }
1675 major = stream->trace->session->major;
1676 minor = stream->trace->session->minor;
1677 stream->index_file = lttng_index_file_create(stream->path_name,
1678 stream->channel_name,
1679 -1, -1, stream->tracefile_size,
1680 tracefile_array_get_file_index_head(stream->tfa),
1681 lttng_to_index_major(major, minor),
1682 lttng_to_index_minor(major, minor));
1683 if (!stream->index_file) {
1684 ret = -1;
1685 goto end;
1686 }
1687
1688 ret = 0;
1689
1690 end:
1691 return ret;
1692 }
1693
1694 static
1695 int do_rotate_stream(struct relay_stream *stream)
1696 {
1697 int ret;
1698
1699 /* Perform the stream rotation. */
1700 ret = utils_rotate_stream_file(stream->path_name,
1701 stream->channel_name, stream->tracefile_size,
1702 stream->tracefile_count, -1,
1703 -1, stream->stream_fd->fd,
1704 NULL, &stream->stream_fd->fd);
1705 if (ret < 0) {
1706 ERR("Rotating stream output file");
1707 goto end;
1708 }
1709 stream->tracefile_size_current = 0;
1710
1711 /* Rotate also the index if the stream is not a metadata stream. */
1712 if (!stream->is_metadata) {
1713 ret = create_rotate_index_file(stream);
1714 if (ret < 0) {
1715 ERR("Failed to rotate index file");
1716 goto end;
1717 }
1718 }
1719
1720 stream->rotate_at_seq_num = -1ULL;
1721 stream->pos_after_last_complete_data_index = 0;
1722
1723 end:
1724 return ret;
1725 }
1726
1727 /*
1728 * If too much data has been written in a tracefile before we received the
1729 * rotation command, we have to move the excess data to the new tracefile and
1730 * perform the rotation. This can happen because the control and data
1731 * connections are separate, the indexes as well as the commands arrive from
1732 * the control connection and we have no control over the order so we could be
1733 * in a situation where too much data has been received on the data connection
1734 * before the rotation command on the control connection arrives. We don't need
1735 * to update the index because its order is guaranteed with the rotation
1736 * command message.
1737 */
1738 static
1739 int rotate_truncate_stream(struct relay_stream *stream)
1740 {
1741 int ret, new_fd;
1742 off_t lseek_ret;
1743 uint64_t diff, pos = 0;
1744 char buf[FILE_COPY_BUFFER_SIZE];
1745
1746 assert(!stream->is_metadata);
1747
1748 assert(stream->tracefile_size_current >
1749 stream->pos_after_last_complete_data_index);
1750 diff = stream->tracefile_size_current -
1751 stream->pos_after_last_complete_data_index;
1752
1753 /* Create the new tracefile. */
1754 new_fd = utils_create_stream_file(stream->path_name,
1755 stream->channel_name,
1756 stream->tracefile_size, stream->tracefile_count,
1757 /* uid */ -1, /* gid */ -1, /* suffix */ NULL);
1758 if (new_fd < 0) {
1759 ERR("Failed to create new stream file at path %s for channel %s",
1760 stream->path_name, stream->channel_name);
1761 ret = -1;
1762 goto end;
1763 }
1764
1765 /*
1766 * Rewind the current tracefile to the position at which the rotation
1767 * should have occured.
1768 */
1769 lseek_ret = lseek(stream->stream_fd->fd,
1770 stream->pos_after_last_complete_data_index, SEEK_SET);
1771 if (lseek_ret < 0) {
1772 PERROR("seek truncate stream");
1773 ret = -1;
1774 goto end;
1775 }
1776
1777 /* Move data from the old file to the new file. */
1778 while (pos < diff) {
1779 uint64_t count, bytes_left;
1780 ssize_t io_ret;
1781
1782 bytes_left = diff - pos;
1783 count = bytes_left > sizeof(buf) ? sizeof(buf) : bytes_left;
1784 assert(count <= SIZE_MAX);
1785
1786 io_ret = lttng_read(stream->stream_fd->fd, buf, count);
1787 if (io_ret < (ssize_t) count) {
1788 char error_string[256];
1789
1790 snprintf(error_string, sizeof(error_string),
1791 "Failed to read %" PRIu64 " bytes from fd %i in rotate_truncate_stream(), returned %zi",
1792 count, stream->stream_fd->fd, io_ret);
1793 if (io_ret == -1) {
1794 PERROR("%s", error_string);
1795 } else {
1796 ERR("%s", error_string);
1797 }
1798 ret = -1;
1799 goto end;
1800 }
1801
1802 io_ret = lttng_write(new_fd, buf, count);
1803 if (io_ret < (ssize_t) count) {
1804 char error_string[256];
1805
1806 snprintf(error_string, sizeof(error_string),
1807 "Failed to write %" PRIu64 " bytes from fd %i in rotate_truncate_stream(), returned %zi",
1808 count, new_fd, io_ret);
1809 if (io_ret == -1) {
1810 PERROR("%s", error_string);
1811 } else {
1812 ERR("%s", error_string);
1813 }
1814 ret = -1;
1815 goto end;
1816 }
1817
1818 pos += count;
1819 }
1820
1821 /* Truncate the file to get rid of the excess data. */
1822 ret = ftruncate(stream->stream_fd->fd,
1823 stream->pos_after_last_complete_data_index);
1824 if (ret) {
1825 PERROR("ftruncate");
1826 goto end;
1827 }
1828
1829 ret = close(stream->stream_fd->fd);
1830 if (ret < 0) {
1831 PERROR("Closing tracefile");
1832 goto end;
1833 }
1834
1835 ret = create_rotate_index_file(stream);
1836 if (ret < 0) {
1837 ERR("Rotate stream index file");
1838 goto end;
1839 }
1840
1841 /*
1842 * Update the offset and FD of all the eventual indexes created by the
1843 * data connection before the rotation command arrived.
1844 */
1845 ret = relay_index_switch_all_files(stream);
1846 if (ret < 0) {
1847 ERR("Failed to rotate index file");
1848 goto end;
1849 }
1850
1851 stream->stream_fd->fd = new_fd;
1852 stream->tracefile_size_current = diff;
1853 stream->pos_after_last_complete_data_index = 0;
1854 stream->rotate_at_seq_num = -1ULL;
1855
1856 ret = 0;
1857
1858 end:
1859 return ret;
1860 }
1861
1862 /*
1863 * Check if a stream should perform a rotation (for session rotation).
1864 * Must be called with the stream lock held.
1865 *
1866 * Return 0 on success, a negative value on error.
1867 */
1868 static
1869 int try_rotate_stream(struct relay_stream *stream)
1870 {
1871 int ret = 0;
1872
1873 /* No rotation expected. */
1874 if (stream->rotate_at_seq_num == -1ULL) {
1875 goto end;
1876 }
1877
1878 if (stream->prev_seq < stream->rotate_at_seq_num ||
1879 stream->prev_seq == -1ULL) {
1880 DBG("Stream %" PRIu64 " no yet ready for rotation",
1881 stream->stream_handle);
1882 goto end;
1883 } else if (stream->prev_seq > stream->rotate_at_seq_num) {
1884 DBG("Rotation after too much data has been written in tracefile "
1885 "for stream %" PRIu64 ", need to truncate before "
1886 "rotating", stream->stream_handle);
1887 ret = rotate_truncate_stream(stream);
1888 if (ret) {
1889 ERR("Failed to truncate stream");
1890 goto end;
1891 }
1892 } else {
1893 /* stream->prev_seq == stream->rotate_at_seq_num */
1894 DBG("Stream %" PRIu64 " ready for rotation",
1895 stream->stream_handle);
1896 ret = do_rotate_stream(stream);
1897 }
1898
1899 end:
1900 return ret;
1901 }
1902
1903 /*
1904 * relay_recv_metadata: receive the metadata for the session.
1905 */
1906 static int relay_recv_metadata(const struct lttcomm_relayd_hdr *recv_hdr,
1907 struct relay_connection *conn,
1908 const struct lttng_buffer_view *payload)
1909 {
1910 int ret = 0;
1911 ssize_t size_ret;
1912 struct relay_session *session = conn->session;
1913 struct lttcomm_relayd_metadata_payload metadata_payload_header;
1914 struct relay_stream *metadata_stream;
1915 uint64_t metadata_payload_size;
1916
1917 if (!session) {
1918 ERR("Metadata sent before version check");
1919 ret = -1;
1920 goto end;
1921 }
1922
1923 if (recv_hdr->data_size < sizeof(struct lttcomm_relayd_metadata_payload)) {
1924 ERR("Incorrect data size");
1925 ret = -1;
1926 goto end;
1927 }
1928 metadata_payload_size = recv_hdr->data_size -
1929 sizeof(struct lttcomm_relayd_metadata_payload);
1930
1931 memcpy(&metadata_payload_header, payload->data,
1932 sizeof(metadata_payload_header));
1933 metadata_payload_header.stream_id = be64toh(
1934 metadata_payload_header.stream_id);
1935 metadata_payload_header.padding_size = be32toh(
1936 metadata_payload_header.padding_size);
1937
1938 metadata_stream = stream_get_by_id(metadata_payload_header.stream_id);
1939 if (!metadata_stream) {
1940 ret = -1;
1941 goto end;
1942 }
1943
1944 pthread_mutex_lock(&metadata_stream->lock);
1945
1946 size_ret = lttng_write(metadata_stream->stream_fd->fd,
1947 payload->data + sizeof(metadata_payload_header),
1948 metadata_payload_size);
1949 if (size_ret < metadata_payload_size) {
1950 ERR("Relay error writing metadata on file");
1951 ret = -1;
1952 goto end_put;
1953 }
1954
1955 size_ret = write_padding_to_file(metadata_stream->stream_fd->fd,
1956 metadata_payload_header.padding_size);
1957 if (size_ret < (int64_t) metadata_payload_header.padding_size) {
1958 ret = -1;
1959 goto end_put;
1960 }
1961
1962 metadata_stream->metadata_received +=
1963 metadata_payload_size + metadata_payload_header.padding_size;
1964 DBG2("Relay metadata written. Updated metadata_received %" PRIu64,
1965 metadata_stream->metadata_received);
1966
1967 ret = try_rotate_stream(metadata_stream);
1968 if (ret < 0) {
1969 goto end_put;
1970 }
1971
1972 end_put:
1973 pthread_mutex_unlock(&metadata_stream->lock);
1974 stream_put(metadata_stream);
1975 end:
1976 return ret;
1977 }
1978
1979 /*
1980 * relay_send_version: send relayd version number
1981 */
1982 static int relay_send_version(const struct lttcomm_relayd_hdr *recv_hdr,
1983 struct relay_connection *conn,
1984 const struct lttng_buffer_view *payload)
1985 {
1986 int ret;
1987 ssize_t send_ret;
1988 struct lttcomm_relayd_version reply, msg;
1989 bool compatible = true;
1990
1991 conn->version_check_done = true;
1992
1993 /* Get version from the other side. */
1994 if (payload->size < sizeof(msg)) {
1995 ERR("Unexpected payload size in \"relay_send_version\": expected >= %zu bytes, got %zu bytes",
1996 sizeof(msg), payload->size);
1997 ret = -1;
1998 goto end;
1999 }
2000
2001 memcpy(&msg, payload->data, sizeof(msg));
2002 msg.major = be32toh(msg.major);
2003 msg.minor = be32toh(msg.minor);
2004
2005 memset(&reply, 0, sizeof(reply));
2006 reply.major = RELAYD_VERSION_COMM_MAJOR;
2007 reply.minor = RELAYD_VERSION_COMM_MINOR;
2008
2009 /* Major versions must be the same */
2010 if (reply.major != msg.major) {
2011 DBG("Incompatible major versions (%u vs %u), deleting session",
2012 reply.major, msg.major);
2013 compatible = false;
2014 }
2015
2016 conn->major = reply.major;
2017 /* We adapt to the lowest compatible version */
2018 if (reply.minor <= msg.minor) {
2019 conn->minor = reply.minor;
2020 } else {
2021 conn->minor = msg.minor;
2022 }
2023
2024 reply.major = htobe32(reply.major);
2025 reply.minor = htobe32(reply.minor);
2026 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
2027 sizeof(reply), 0);
2028 if (send_ret < (ssize_t) sizeof(reply)) {
2029 ERR("Failed to send \"send version\" command reply (ret = %zd)",
2030 send_ret);
2031 ret = -1;
2032 goto end;
2033 } else {
2034 ret = 0;
2035 }
2036
2037 if (!compatible) {
2038 ret = -1;
2039 goto end;
2040 }
2041
2042 DBG("Version check done using protocol %u.%u", conn->major,
2043 conn->minor);
2044
2045 end:
2046 return ret;
2047 }
2048
2049 /*
2050 * Check for data pending for a given stream id from the session daemon.
2051 */
2052 static int relay_data_pending(const struct lttcomm_relayd_hdr *recv_hdr,
2053 struct relay_connection *conn,
2054 const struct lttng_buffer_view *payload)
2055 {
2056 struct relay_session *session = conn->session;
2057 struct lttcomm_relayd_data_pending msg;
2058 struct lttcomm_relayd_generic_reply reply;
2059 struct relay_stream *stream;
2060 ssize_t send_ret;
2061 int ret;
2062
2063 DBG("Data pending command received");
2064
2065 if (!session || !conn->version_check_done) {
2066 ERR("Trying to check for data before version check");
2067 ret = -1;
2068 goto end_no_session;
2069 }
2070
2071 if (payload->size < sizeof(msg)) {
2072 ERR("Unexpected payload size in \"relay_data_pending\": expected >= %zu bytes, got %zu bytes",
2073 sizeof(msg), payload->size);
2074 ret = -1;
2075 goto end_no_session;
2076 }
2077 memcpy(&msg, payload->data, sizeof(msg));
2078 msg.stream_id = be64toh(msg.stream_id);
2079 msg.last_net_seq_num = be64toh(msg.last_net_seq_num);
2080
2081 stream = stream_get_by_id(msg.stream_id);
2082 if (stream == NULL) {
2083 ret = -1;
2084 goto end;
2085 }
2086
2087 pthread_mutex_lock(&stream->lock);
2088
2089 DBG("Data pending for stream id %" PRIu64 " prev_seq %" PRIu64
2090 " and last_seq %" PRIu64, msg.stream_id,
2091 stream->prev_seq, msg.last_net_seq_num);
2092
2093 /* Avoid wrapping issue */
2094 if (((int64_t) (stream->prev_seq - msg.last_net_seq_num)) >= 0) {
2095 /* Data has in fact been written and is NOT pending */
2096 ret = 0;
2097 } else {
2098 /* Data still being streamed thus pending */
2099 ret = 1;
2100 }
2101
2102 stream->data_pending_check_done = true;
2103 pthread_mutex_unlock(&stream->lock);
2104
2105 stream_put(stream);
2106 end:
2107
2108 memset(&reply, 0, sizeof(reply));
2109 reply.ret_code = htobe32(ret);
2110 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2111 if (send_ret < (ssize_t) sizeof(reply)) {
2112 ERR("Failed to send \"data pending\" command reply (ret = %zd)",
2113 send_ret);
2114 ret = -1;
2115 }
2116
2117 end_no_session:
2118 return ret;
2119 }
2120
2121 /*
2122 * Wait for the control socket to reach a quiescent state.
2123 *
2124 * Note that for now, when receiving this command from the session
2125 * daemon, this means that every subsequent commands or data received on
2126 * the control socket has been handled. So, this is why we simply return
2127 * OK here.
2128 */
2129 static int relay_quiescent_control(const struct lttcomm_relayd_hdr *recv_hdr,
2130 struct relay_connection *conn,
2131 const struct lttng_buffer_view *payload)
2132 {
2133 int ret;
2134 ssize_t send_ret;
2135 struct relay_stream *stream;
2136 struct lttcomm_relayd_quiescent_control msg;
2137 struct lttcomm_relayd_generic_reply reply;
2138
2139 DBG("Checking quiescent state on control socket");
2140
2141 if (!conn->session || !conn->version_check_done) {
2142 ERR("Trying to check for data before version check");
2143 ret = -1;
2144 goto end_no_session;
2145 }
2146
2147 if (payload->size < sizeof(msg)) {
2148 ERR("Unexpected payload size in \"relay_quiescent_control\": expected >= %zu bytes, got %zu bytes",
2149 sizeof(msg), payload->size);
2150 ret = -1;
2151 goto end_no_session;
2152 }
2153 memcpy(&msg, payload->data, sizeof(msg));
2154 msg.stream_id = be64toh(msg.stream_id);
2155
2156 stream = stream_get_by_id(msg.stream_id);
2157 if (!stream) {
2158 goto reply;
2159 }
2160 pthread_mutex_lock(&stream->lock);
2161 stream->data_pending_check_done = true;
2162 pthread_mutex_unlock(&stream->lock);
2163
2164 DBG("Relay quiescent control pending flag set to %" PRIu64, msg.stream_id);
2165 stream_put(stream);
2166 reply:
2167 memset(&reply, 0, sizeof(reply));
2168 reply.ret_code = htobe32(LTTNG_OK);
2169 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2170 if (send_ret < (ssize_t) sizeof(reply)) {
2171 ERR("Failed to send \"quiescent control\" command reply (ret = %zd)",
2172 send_ret);
2173 ret = -1;
2174 } else {
2175 ret = 0;
2176 }
2177
2178 end_no_session:
2179 return ret;
2180 }
2181
2182 /*
2183 * Initialize a data pending command. This means that a consumer is about
2184 * to ask for data pending for each stream it holds. Simply iterate over
2185 * all streams of a session and set the data_pending_check_done flag.
2186 *
2187 * This command returns to the client a LTTNG_OK code.
2188 */
2189 static int relay_begin_data_pending(const struct lttcomm_relayd_hdr *recv_hdr,
2190 struct relay_connection *conn,
2191 const struct lttng_buffer_view *payload)
2192 {
2193 int ret;
2194 ssize_t send_ret;
2195 struct lttng_ht_iter iter;
2196 struct lttcomm_relayd_begin_data_pending msg;
2197 struct lttcomm_relayd_generic_reply reply;
2198 struct relay_stream *stream;
2199
2200 assert(recv_hdr);
2201 assert(conn);
2202
2203 DBG("Init streams for data pending");
2204
2205 if (!conn->session || !conn->version_check_done) {
2206 ERR("Trying to check for data before version check");
2207 ret = -1;
2208 goto end_no_session;
2209 }
2210
2211 if (payload->size < sizeof(msg)) {
2212 ERR("Unexpected payload size in \"relay_begin_data_pending\": expected >= %zu bytes, got %zu bytes",
2213 sizeof(msg), payload->size);
2214 ret = -1;
2215 goto end_no_session;
2216 }
2217 memcpy(&msg, payload->data, sizeof(msg));
2218 msg.session_id = be64toh(msg.session_id);
2219
2220 /*
2221 * Iterate over all streams to set the begin data pending flag.
2222 * For now, the streams are indexed by stream handle so we have
2223 * to iterate over all streams to find the one associated with
2224 * the right session_id.
2225 */
2226 rcu_read_lock();
2227 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
2228 node.node) {
2229 if (!stream_get(stream)) {
2230 continue;
2231 }
2232 if (stream->trace->session->id == msg.session_id) {
2233 pthread_mutex_lock(&stream->lock);
2234 stream->data_pending_check_done = false;
2235 pthread_mutex_unlock(&stream->lock);
2236 DBG("Set begin data pending flag to stream %" PRIu64,
2237 stream->stream_handle);
2238 }
2239 stream_put(stream);
2240 }
2241 rcu_read_unlock();
2242
2243 memset(&reply, 0, sizeof(reply));
2244 /* All good, send back reply. */
2245 reply.ret_code = htobe32(LTTNG_OK);
2246
2247 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2248 if (send_ret < (ssize_t) sizeof(reply)) {
2249 ERR("Failed to send \"begin data pending\" command reply (ret = %zd)",
2250 send_ret);
2251 ret = -1;
2252 } else {
2253 ret = 0;
2254 }
2255
2256 end_no_session:
2257 return ret;
2258 }
2259
2260 /*
2261 * End data pending command. This will check, for a given session id, if
2262 * each stream associated with it has its data_pending_check_done flag
2263 * set. If not, this means that the client lost track of the stream but
2264 * the data is still being streamed on our side. In this case, we inform
2265 * the client that data is in flight.
2266 *
2267 * Return to the client if there is data in flight or not with a ret_code.
2268 */
2269 static int relay_end_data_pending(const struct lttcomm_relayd_hdr *recv_hdr,
2270 struct relay_connection *conn,
2271 const struct lttng_buffer_view *payload)
2272 {
2273 int ret;
2274 ssize_t send_ret;
2275 struct lttng_ht_iter iter;
2276 struct lttcomm_relayd_end_data_pending msg;
2277 struct lttcomm_relayd_generic_reply reply;
2278 struct relay_stream *stream;
2279 uint32_t is_data_inflight = 0;
2280
2281 DBG("End data pending command");
2282
2283 if (!conn->session || !conn->version_check_done) {
2284 ERR("Trying to check for data before version check");
2285 ret = -1;
2286 goto end_no_session;
2287 }
2288
2289 if (payload->size < sizeof(msg)) {
2290 ERR("Unexpected payload size in \"relay_end_data_pending\": expected >= %zu bytes, got %zu bytes",
2291 sizeof(msg), payload->size);
2292 ret = -1;
2293 goto end_no_session;
2294 }
2295 memcpy(&msg, payload->data, sizeof(msg));
2296 msg.session_id = be64toh(msg.session_id);
2297
2298 /*
2299 * Iterate over all streams to see if the begin data pending
2300 * flag is set.
2301 */
2302 rcu_read_lock();
2303 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
2304 node.node) {
2305 if (!stream_get(stream)) {
2306 continue;
2307 }
2308 if (stream->trace->session->id != msg.session_id) {
2309 stream_put(stream);
2310 continue;
2311 }
2312 pthread_mutex_lock(&stream->lock);
2313 if (!stream->data_pending_check_done) {
2314 if (!stream->closed || !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) {
2315 is_data_inflight = 1;
2316 DBG("Data is still in flight for stream %" PRIu64,
2317 stream->stream_handle);
2318 pthread_mutex_unlock(&stream->lock);
2319 stream_put(stream);
2320 break;
2321 }
2322 }
2323 pthread_mutex_unlock(&stream->lock);
2324 stream_put(stream);
2325 }
2326 rcu_read_unlock();
2327
2328 memset(&reply, 0, sizeof(reply));
2329 /* All good, send back reply. */
2330 reply.ret_code = htobe32(is_data_inflight);
2331
2332 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2333 if (send_ret < (ssize_t) sizeof(reply)) {
2334 ERR("Failed to send \"end data pending\" command reply (ret = %zd)",
2335 send_ret);
2336 ret = -1;
2337 } else {
2338 ret = 0;
2339 }
2340
2341 end_no_session:
2342 return ret;
2343 }
2344
2345 /*
2346 * Receive an index for a specific stream.
2347 *
2348 * Return 0 on success else a negative value.
2349 */
2350 static int relay_recv_index(const struct lttcomm_relayd_hdr *recv_hdr,
2351 struct relay_connection *conn,
2352 const struct lttng_buffer_view *payload)
2353 {
2354 int ret;
2355 ssize_t send_ret;
2356 struct relay_session *session = conn->session;
2357 struct lttcomm_relayd_index index_info;
2358 struct relay_index *index;
2359 struct lttcomm_relayd_generic_reply reply;
2360 struct relay_stream *stream;
2361 size_t msg_len;
2362
2363 assert(conn);
2364
2365 DBG("Relay receiving index");
2366
2367 if (!session || !conn->version_check_done) {
2368 ERR("Trying to close a stream before version check");
2369 ret = -1;
2370 goto end_no_session;
2371 }
2372
2373 msg_len = lttcomm_relayd_index_len(
2374 lttng_to_index_major(conn->major, conn->minor),
2375 lttng_to_index_minor(conn->major, conn->minor));
2376 if (payload->size < msg_len) {
2377 ERR("Unexpected payload size in \"relay_recv_index\": expected >= %zu bytes, got %zu bytes",
2378 msg_len, payload->size);
2379 ret = -1;
2380 goto end_no_session;
2381 }
2382 memcpy(&index_info, payload->data, msg_len);
2383 index_info.relay_stream_id = be64toh(index_info.relay_stream_id);
2384 index_info.net_seq_num = be64toh(index_info.net_seq_num);
2385 index_info.packet_size = be64toh(index_info.packet_size);
2386 index_info.content_size = be64toh(index_info.content_size);
2387 index_info.timestamp_begin = be64toh(index_info.timestamp_begin);
2388 index_info.timestamp_end = be64toh(index_info.timestamp_end);
2389 index_info.events_discarded = be64toh(index_info.events_discarded);
2390 index_info.stream_id = be64toh(index_info.stream_id);
2391
2392 if (conn->minor >= 8) {
2393 index_info.stream_instance_id =
2394 be64toh(index_info.stream_instance_id);
2395 index_info.packet_seq_num = be64toh(index_info.packet_seq_num);
2396 }
2397
2398 stream = stream_get_by_id(index_info.relay_stream_id);
2399 if (!stream) {
2400 ERR("stream_get_by_id not found");
2401 ret = -1;
2402 goto end;
2403 }
2404 pthread_mutex_lock(&stream->lock);
2405
2406 /* Live beacon handling */
2407 if (index_info.packet_size == 0) {
2408 DBG("Received live beacon for stream %" PRIu64,
2409 stream->stream_handle);
2410
2411 /*
2412 * Only flag a stream inactive when it has already
2413 * received data and no indexes are in flight.
2414 */
2415 if (stream->index_received_seqcount > 0
2416 && stream->indexes_in_flight == 0) {
2417 stream->beacon_ts_end = index_info.timestamp_end;
2418 }
2419 ret = 0;
2420 goto end_stream_put;
2421 } else {
2422 stream->beacon_ts_end = -1ULL;
2423 }
2424
2425 if (stream->ctf_stream_id == -1ULL) {
2426 stream->ctf_stream_id = index_info.stream_id;
2427 }
2428 index = relay_index_get_by_id_or_create(stream, index_info.net_seq_num);
2429 if (!index) {
2430 ret = -1;
2431 ERR("relay_index_get_by_id_or_create index NULL");
2432 goto end_stream_put;
2433 }
2434 if (set_index_control_data(index, &index_info, conn)) {
2435 ERR("set_index_control_data error");
2436 relay_index_put(index);
2437 ret = -1;
2438 goto end_stream_put;
2439 }
2440 ret = relay_index_try_flush(index);
2441 if (ret == 0) {
2442 tracefile_array_commit_seq(stream->tfa);
2443 stream->index_received_seqcount++;
2444 stream->pos_after_last_complete_data_index += index->total_size;
2445 } else if (ret > 0) {
2446 /* no flush. */
2447 ret = 0;
2448 } else {
2449 ERR("relay_index_try_flush error %d", ret);
2450 relay_index_put(index);
2451 ret = -1;
2452 }
2453
2454 end_stream_put:
2455 pthread_mutex_unlock(&stream->lock);
2456 stream_put(stream);
2457
2458 end:
2459
2460 memset(&reply, 0, sizeof(reply));
2461 if (ret < 0) {
2462 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2463 } else {
2464 reply.ret_code = htobe32(LTTNG_OK);
2465 }
2466 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2467 if (send_ret < (ssize_t) sizeof(reply)) {
2468 ERR("Failed to send \"recv index\" command reply (ret = %zd)", send_ret);
2469 ret = -1;
2470 }
2471
2472 end_no_session:
2473 return ret;
2474 }
2475
2476 /*
2477 * Receive the streams_sent message.
2478 *
2479 * Return 0 on success else a negative value.
2480 */
2481 static int relay_streams_sent(const struct lttcomm_relayd_hdr *recv_hdr,
2482 struct relay_connection *conn,
2483 const struct lttng_buffer_view *payload)
2484 {
2485 int ret;
2486 ssize_t send_ret;
2487 struct lttcomm_relayd_generic_reply reply;
2488
2489 assert(conn);
2490
2491 DBG("Relay receiving streams_sent");
2492
2493 if (!conn->session || !conn->version_check_done) {
2494 ERR("Trying to close a stream before version check");
2495 ret = -1;
2496 goto end_no_session;
2497 }
2498
2499 /*
2500 * Publish every pending stream in the connection recv list which are
2501 * now ready to be used by the viewer.
2502 */
2503 publish_connection_local_streams(conn);
2504
2505 memset(&reply, 0, sizeof(reply));
2506 reply.ret_code = htobe32(LTTNG_OK);
2507 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2508 if (send_ret < (ssize_t) sizeof(reply)) {
2509 ERR("Failed to send \"streams sent\" command reply (ret = %zd)",
2510 send_ret);
2511 ret = -1;
2512 } else {
2513 /* Success. */
2514 ret = 0;
2515 }
2516
2517 end_no_session:
2518 return ret;
2519 }
2520
2521 /*
2522 * relay_rotate_session_stream: rotate a stream to a new tracefile for the session
2523 * rotation feature (not the tracefile rotation feature).
2524 */
2525 static int relay_rotate_session_stream(const struct lttcomm_relayd_hdr *recv_hdr,
2526 struct relay_connection *conn,
2527 const struct lttng_buffer_view *payload)
2528 {
2529 int ret;
2530 ssize_t send_ret;
2531 struct relay_session *session = conn->session;
2532 struct lttcomm_relayd_rotate_stream stream_info;
2533 struct lttcomm_relayd_generic_reply reply;
2534 struct relay_stream *stream;
2535 size_t header_len;
2536 size_t path_len;
2537 struct lttng_buffer_view new_path_view;
2538
2539 DBG("Rotate stream received");
2540
2541 if (!session || !conn->version_check_done) {
2542 ERR("Trying to rotate a stream before version check");
2543 ret = -1;
2544 goto end_no_reply;
2545 }
2546
2547 if (session->major == 2 && session->minor < 11) {
2548 ERR("Unsupported feature before 2.11");
2549 ret = -1;
2550 goto end_no_reply;
2551 }
2552
2553 header_len = sizeof(struct lttcomm_relayd_rotate_stream);
2554
2555 if (payload->size < header_len) {
2556 ERR("Unexpected payload size in \"relay_rotate_session_stream\": expected >= %zu bytes, got %zu bytes",
2557 header_len, payload->size);
2558 ret = -1;
2559 goto end_no_reply;
2560 }
2561
2562 memcpy(&stream_info, payload->data, header_len);
2563
2564 /* Convert to host */
2565 stream_info.pathname_length = be32toh(stream_info.pathname_length);
2566 stream_info.stream_id = be64toh(stream_info.stream_id);
2567 stream_info.new_chunk_id = be64toh(stream_info.new_chunk_id);
2568 stream_info.rotate_at_seq_num = be64toh(stream_info.rotate_at_seq_num);
2569
2570 path_len = stream_info.pathname_length;
2571 if (payload->size < header_len + path_len) {
2572 ERR("Unexpected payload size in \"relay_rotate_session_stream\" including path: expected >= %zu bytes, got %zu bytes",
2573 header_len + path_len, payload->size);
2574 ret = -1;
2575 goto end_no_reply;
2576 }
2577
2578 /* Ensure it fits in local filename length. */
2579 if (path_len >= LTTNG_PATH_MAX) {
2580 ret = -ENAMETOOLONG;
2581 ERR("Length of relay_rotate_session_stream command's path name (%zu bytes) exceeds the maximal allowed length of %i bytes",
2582 path_len, LTTNG_PATH_MAX);
2583 goto end;
2584 }
2585
2586 new_path_view = lttng_buffer_view_from_view(payload, header_len,
2587 stream_info.pathname_length);
2588
2589 stream = stream_get_by_id(stream_info.stream_id);
2590 if (!stream) {
2591 ret = -1;
2592 goto end;
2593 }
2594
2595 pthread_mutex_lock(&stream->lock);
2596
2597 /*
2598 * Update the trace path (just the folder, the stream name does not
2599 * change).
2600 */
2601 free(stream->path_name);
2602 stream->path_name = create_output_path(new_path_view.data);
2603 if (!stream->path_name) {
2604 ERR("Failed to create a new output path");
2605 ret = -1;
2606 goto end_stream_unlock;
2607 }
2608 ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG,
2609 -1, -1);
2610 if (ret < 0) {
2611 ERR("relay creating output directory");
2612 ret = -1;
2613 goto end_stream_unlock;
2614 }
2615
2616 assert(stream->current_chunk_id.is_set);
2617 stream->current_chunk_id.value = stream_info.new_chunk_id;
2618
2619 if (stream->is_metadata) {
2620 /*
2621 * The metadata stream is sent only over the control connection
2622 * so we know we have all the data to perform the stream
2623 * rotation.
2624 */
2625 ret = do_rotate_stream(stream);
2626 } else {
2627 stream->rotate_at_seq_num = stream_info.rotate_at_seq_num;
2628 ret = try_rotate_stream(stream);
2629 }
2630 if (ret < 0) {
2631 goto end_stream_unlock;
2632 }
2633
2634 end_stream_unlock:
2635 pthread_mutex_unlock(&stream->lock);
2636 stream_put(stream);
2637 end:
2638 memset(&reply, 0, sizeof(reply));
2639 if (ret < 0) {
2640 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2641 } else {
2642 reply.ret_code = htobe32(LTTNG_OK);
2643 }
2644 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
2645 sizeof(struct lttcomm_relayd_generic_reply), 0);
2646 if (send_ret < (ssize_t) sizeof(reply)) {
2647 ERR("Failed to send \"rotate session stream\" command reply (ret = %zd)",
2648 send_ret);
2649 ret = -1;
2650 }
2651
2652 end_no_reply:
2653 return ret;
2654 }
2655
2656 /*
2657 * relay_mkdir: Create a folder on the disk.
2658 */
2659 static int relay_mkdir(const struct lttcomm_relayd_hdr *recv_hdr,
2660 struct relay_connection *conn,
2661 const struct lttng_buffer_view *payload)
2662 {
2663 int ret;
2664 struct relay_session *session = conn->session;
2665 struct lttcomm_relayd_mkdir path_info_header;
2666 struct lttcomm_relayd_generic_reply reply;
2667 char *path = NULL;
2668 size_t header_len;
2669 ssize_t send_ret;
2670 struct lttng_buffer_view path_view;
2671
2672 if (!session || !conn->version_check_done) {
2673 ERR("Trying to create a directory before version check");
2674 ret = -1;
2675 goto end_no_session;
2676 }
2677
2678 if (session->major == 2 && session->minor < 11) {
2679 /*
2680 * This client is not supposed to use this command since
2681 * it predates its introduction.
2682 */
2683 ERR("relay_mkdir command is unsupported before LTTng 2.11");
2684 ret = -1;
2685 goto end_no_session;
2686 }
2687
2688 header_len = sizeof(path_info_header);
2689 if (payload->size < header_len) {
2690 ERR("Unexpected payload size in \"relay_mkdir\": expected >= %zu bytes, got %zu bytes",
2691 header_len, payload->size);
2692 ret = -1;
2693 goto end_no_session;
2694 }
2695
2696 memcpy(&path_info_header, payload->data, header_len);
2697
2698 path_info_header.length = be32toh(path_info_header.length);
2699
2700 if (payload->size < header_len + path_info_header.length) {
2701 ERR("Unexpected payload size in \"relay_mkdir\" including path: expected >= %zu bytes, got %zu bytes",
2702 header_len + path_info_header.length, payload->size);
2703 ret = -1;
2704 goto end_no_session;
2705 }
2706
2707 /* Ensure that it fits in local path length. */
2708 if (path_info_header.length >= LTTNG_PATH_MAX) {
2709 ret = -ENAMETOOLONG;
2710 ERR("Path name argument of mkdir command (%" PRIu32 " bytes) exceeds the maximal length allowed (%d bytes)",
2711 path_info_header.length, LTTNG_PATH_MAX);
2712 goto end;
2713 }
2714
2715 path_view = lttng_buffer_view_from_view(payload, header_len,
2716 path_info_header.length);
2717
2718 path = create_output_path(path_view.data);
2719 if (!path) {
2720 ERR("Failed to create output path");
2721 ret = -1;
2722 goto end;
2723 }
2724
2725 ret = utils_mkdir_recursive(path, S_IRWXU | S_IRWXG, -1, -1);
2726 if (ret < 0) {
2727 ERR("relay creating output directory");
2728 goto end;
2729 }
2730
2731 ret = 0;
2732
2733 end:
2734 memset(&reply, 0, sizeof(reply));
2735 if (ret < 0) {
2736 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2737 } else {
2738 reply.ret_code = htobe32(LTTNG_OK);
2739 }
2740 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply, sizeof(reply), 0);
2741 if (send_ret < (ssize_t) sizeof(reply)) {
2742 ERR("Failed to send \"mkdir\" command reply (ret = %zd)", send_ret);
2743 ret = -1;
2744 }
2745
2746 end_no_session:
2747 free(path);
2748 return ret;
2749 }
2750
2751 static int validate_rotate_rename_path_length(const char *path_type,
2752 uint32_t path_length)
2753 {
2754 int ret = 0;
2755
2756 if (path_length > LTTNG_PATH_MAX) {
2757 ret = -ENAMETOOLONG;
2758 ERR("rotate rename \"%s\" path name length (%" PRIu32 " bytes) exceeds the allowed size of %i bytes",
2759 path_type, path_length, LTTNG_PATH_MAX);
2760 } else if (path_length == 0) {
2761 ret = -EINVAL;
2762 ERR("rotate rename \"%s\" path name has an illegal length of 0", path_type);
2763 }
2764 return ret;
2765 }
2766
2767 /*
2768 * relay_rotate_rename: rename the trace folder after a rotation is
2769 * completed. We are not closing any fd here, just moving the folder, so it
2770 * works even if data is still in-flight.
2771 */
2772 static int relay_rotate_rename(const struct lttcomm_relayd_hdr *recv_hdr,
2773 struct relay_connection *conn,
2774 const struct lttng_buffer_view *payload)
2775 {
2776 int ret;
2777 ssize_t send_ret;
2778 struct relay_session *session = conn->session;
2779 struct lttcomm_relayd_generic_reply reply;
2780 struct lttcomm_relayd_rotate_rename header;
2781 size_t header_len;
2782 size_t received_paths_size;
2783 char *complete_old_path = NULL, *complete_new_path = NULL;
2784 struct lttng_buffer_view old_path_view;
2785 struct lttng_buffer_view new_path_view;
2786
2787 if (!session || !conn->version_check_done) {
2788 ERR("Trying to rename a trace folder before version check");
2789 ret = -1;
2790 goto end_no_reply;
2791 }
2792
2793 if (session->major == 2 && session->minor < 11) {
2794 ERR("relay_rotate_rename command is unsupported before LTTng 2.11");
2795 ret = -1;
2796 goto end_no_reply;
2797 }
2798
2799 header_len = sizeof(header);
2800 if (payload->size < header_len) {
2801 ERR("Unexpected payload size in \"relay_rotate_rename\": expected >= %zu bytes, got %zu bytes",
2802 header_len, payload->size);
2803 ret = -1;
2804 goto end_no_reply;
2805 }
2806
2807 memcpy(&header, payload->data, header_len);
2808
2809 header.old_path_length = be32toh(header.old_path_length);
2810 header.new_path_length = be32toh(header.new_path_length);
2811 received_paths_size = header.old_path_length + header.new_path_length;
2812
2813 if (payload->size < header_len + received_paths_size) {
2814 ERR("Unexpected payload size in \"relay_rotate_rename\" including paths: expected >= %zu bytes, got %zu bytes",
2815 header_len, payload->size);
2816 ret = -1;
2817 goto end_no_reply;
2818 }
2819
2820 /* Ensure the paths don't exceed their allowed size. */
2821 ret = validate_rotate_rename_path_length("old", header.old_path_length);
2822 if (ret) {
2823 goto end;
2824 }
2825 ret = validate_rotate_rename_path_length("new", header.new_path_length);
2826 if (ret) {
2827 goto end;
2828 }
2829
2830 old_path_view = lttng_buffer_view_from_view(payload, header_len,
2831 header.old_path_length);
2832 new_path_view = lttng_buffer_view_from_view(payload,
2833 header_len + header.old_path_length,
2834 header.new_path_length);
2835
2836 /* Validate that both paths received are NULL terminated. */
2837 if (old_path_view.data[old_path_view.size - 1] != '\0') {
2838 ERR("relay_rotate_rename command's \"old\" path is invalid (not NULL terminated)");
2839 ret = -1;
2840 goto end;
2841 }
2842 if (new_path_view.data[new_path_view.size - 1] != '\0') {
2843 ERR("relay_rotate_rename command's \"new\" path is invalid (not NULL terminated)");
2844 ret = -1;
2845 goto end;
2846 }
2847
2848 complete_old_path = create_output_path(old_path_view.data);
2849 if (!complete_old_path) {
2850 ERR("Failed to build old output path in rotate_rename command");
2851 ret = -1;
2852 goto end;
2853 }
2854
2855 complete_new_path = create_output_path(new_path_view.data);
2856 if (!complete_new_path) {
2857 ERR("Failed to build new output path in rotate_rename command");
2858 ret = -1;
2859 goto end;
2860 }
2861
2862 ret = utils_mkdir_recursive(complete_new_path, S_IRWXU | S_IRWXG,
2863 -1, -1);
2864 if (ret < 0) {
2865 ERR("Failed to mkdir() rotate_rename's \"new\" output directory at \"%s\"",
2866 complete_new_path);
2867 goto end;
2868 }
2869
2870 /*
2871 * If a domain has not yet created its channel, the domain-specific
2872 * folder might not exist, but this is not an error.
2873 */
2874 ret = rename(complete_old_path, complete_new_path);
2875 if (ret < 0 && errno != ENOENT) {
2876 PERROR("Renaming chunk in rotate_rename command from \"%s\" to \"%s\"",
2877 complete_old_path, complete_new_path);
2878 goto end;
2879 }
2880 ret = 0;
2881
2882 end:
2883 memset(&reply, 0, sizeof(reply));
2884 if (ret < 0) {
2885 reply.ret_code = htobe32(LTTNG_ERR_UNK);
2886 } else {
2887 reply.ret_code = htobe32(LTTNG_OK);
2888 }
2889 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
2890 sizeof(reply), 0);
2891 if (send_ret < sizeof(reply)) {
2892 ERR("Failed to send \"rotate rename\" command reply (ret = %zd)",
2893 send_ret);
2894 ret = -1;
2895 }
2896
2897 end_no_reply:
2898 free(complete_old_path);
2899 free(complete_new_path);
2900 return ret;
2901 }
2902
2903 /*
2904 * Check if all the streams in the session have completed the last rotation.
2905 * The chunk_id value is used to distinguish the cases where a stream was
2906 * closed on the consumerd before the rotation started but it still active on
2907 * the relayd, and the case where a stream appeared on the consumerd/relayd
2908 * after the last rotation started (in that case, it is already writing in the
2909 * new chunk folder).
2910 */
2911 static
2912 int relay_rotate_pending(const struct lttcomm_relayd_hdr *recv_hdr,
2913 struct relay_connection *conn,
2914 const struct lttng_buffer_view *payload)
2915 {
2916 struct relay_session *session = conn->session;
2917 struct lttcomm_relayd_rotate_pending msg;
2918 struct lttcomm_relayd_rotate_pending_reply reply;
2919 struct lttng_ht_iter iter;
2920 struct relay_stream *stream;
2921 int ret = 0;
2922 ssize_t send_ret;
2923 uint64_t chunk_id;
2924 bool rotate_pending = false;
2925
2926 DBG("Rotate pending command received");
2927
2928 if (!session || !conn->version_check_done) {
2929 ERR("Trying to check for data before version check");
2930 ret = -1;
2931 goto end_no_reply;
2932 }
2933
2934 if (session->major == 2 && session->minor < 11) {
2935 ERR("Unsupported feature before 2.11");
2936 ret = -1;
2937 goto end_no_reply;
2938 }
2939
2940 if (payload->size < sizeof(msg)) {
2941 ERR("Unexpected payload size in \"relay_rotate_pending\": expected >= %zu bytes, got %zu bytes",
2942 sizeof(msg), payload->size);
2943 ret = -1;
2944 goto end_no_reply;
2945 }
2946
2947 memcpy(&msg, payload->data, sizeof(msg));
2948
2949 chunk_id = be64toh(msg.chunk_id);
2950
2951 DBG("Evaluating rotate pending for session \"%s\" and chunk id %" PRIu64,
2952 session->session_name, chunk_id);
2953
2954 /*
2955 * Iterate over all the streams in the session and check if they are
2956 * still waiting for data to perform their rotation.
2957 */
2958 rcu_read_lock();
2959 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
2960 node.node) {
2961 if (!stream_get(stream)) {
2962 continue;
2963 }
2964 if (stream->trace->session != session) {
2965 stream_put(stream);
2966 continue;
2967 }
2968 pthread_mutex_lock(&stream->lock);
2969 if (stream->rotate_at_seq_num != -1ULL) {
2970 /* We have not yet performed the rotation. */
2971 rotate_pending = true;
2972 DBG("Stream %" PRIu64 " is still rotating",
2973 stream->stream_handle);
2974 } else if (stream->current_chunk_id.value < chunk_id) {
2975 /*
2976 * Stream closed on the consumer but still active on the
2977 * relay.
2978 */
2979 rotate_pending = true;
2980 DBG("Stream %" PRIu64 " did not exist on the consumer "
2981 "when the last rotation started, but is"
2982 "still waiting for data before getting"
2983 "closed",
2984 stream->stream_handle);
2985 }
2986 pthread_mutex_unlock(&stream->lock);
2987 stream_put(stream);
2988 if (rotate_pending) {
2989 goto send_reply;
2990 }
2991 }
2992
2993 send_reply:
2994 rcu_read_unlock();
2995 memset(&reply, 0, sizeof(reply));
2996 reply.generic.ret_code = htobe32((uint32_t) LTTNG_OK);
2997 reply.is_pending = (uint8_t) !!rotate_pending;
2998 send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
2999 sizeof(reply), 0);
3000 if (send_ret < (ssize_t) sizeof(reply)) {
3001 ERR("Failed to send \"rotate pending\" command reply (ret = %zd)",
3002 send_ret);
3003 ret = -1;
3004 }
3005
3006 end_no_reply:
3007 return ret;
3008 }
3009
3010 #define DBG_CMD(cmd_name, conn) \
3011 DBG3("Processing \"%s\" command for socket %i", cmd_name, conn->sock->fd);
3012
3013 static int relay_process_control_command(struct relay_connection *conn,
3014 const struct lttcomm_relayd_hdr *header,
3015 const struct lttng_buffer_view *payload)
3016 {
3017 int ret = 0;
3018
3019 switch (header->cmd) {
3020 case RELAYD_CREATE_SESSION:
3021 DBG_CMD("RELAYD_CREATE_SESSION", conn);
3022 ret = relay_create_session(header, conn, payload);
3023 break;
3024 case RELAYD_ADD_STREAM:
3025 DBG_CMD("RELAYD_ADD_STREAM", conn);
3026 ret = relay_add_stream(header, conn, payload);
3027 break;
3028 case RELAYD_START_DATA:
3029 DBG_CMD("RELAYD_START_DATA", conn);
3030 ret = relay_start(header, conn, payload);
3031 break;
3032 case RELAYD_SEND_METADATA:
3033 DBG_CMD("RELAYD_SEND_METADATA", conn);
3034 ret = relay_recv_metadata(header, conn, payload);
3035 break;
3036 case RELAYD_VERSION:
3037 DBG_CMD("RELAYD_VERSION", conn);
3038 ret = relay_send_version(header, conn, payload);
3039 break;
3040 case RELAYD_CLOSE_STREAM:
3041 DBG_CMD("RELAYD_CLOSE_STREAM", conn);
3042 ret = relay_close_stream(header, conn, payload);
3043 break;
3044 case RELAYD_DATA_PENDING:
3045 DBG_CMD("RELAYD_DATA_PENDING", conn);
3046 ret = relay_data_pending(header, conn, payload);
3047 break;
3048 case RELAYD_QUIESCENT_CONTROL:
3049 DBG_CMD("RELAYD_QUIESCENT_CONTROL", conn);
3050 ret = relay_quiescent_control(header, conn, payload);
3051 break;
3052 case RELAYD_BEGIN_DATA_PENDING:
3053 DBG_CMD("RELAYD_BEGIN_DATA_PENDING", conn);
3054 ret = relay_begin_data_pending(header, conn, payload);
3055 break;
3056 case RELAYD_END_DATA_PENDING:
3057 DBG_CMD("RELAYD_END_DATA_PENDING", conn);
3058 ret = relay_end_data_pending(header, conn, payload);
3059 break;
3060 case RELAYD_SEND_INDEX:
3061 DBG_CMD("RELAYD_SEND_INDEX", conn);
3062 ret = relay_recv_index(header, conn, payload);
3063 break;
3064 case RELAYD_STREAMS_SENT:
3065 DBG_CMD("RELAYD_STREAMS_SENT", conn);
3066 ret = relay_streams_sent(header, conn, payload);
3067 break;
3068 case RELAYD_RESET_METADATA:
3069 DBG_CMD("RELAYD_RESET_METADATA", conn);
3070 ret = relay_reset_metadata(header, conn, payload);
3071 break;
3072 case RELAYD_ROTATE_STREAM:
3073 DBG_CMD("RELAYD_ROTATE_STREAM", conn);
3074 ret = relay_rotate_session_stream(header, conn, payload);
3075 break;
3076 case RELAYD_ROTATE_RENAME:
3077 DBG_CMD("RELAYD_ROTATE_RENAME", conn);
3078 ret = relay_rotate_rename(header, conn, payload);
3079 break;
3080 case RELAYD_ROTATE_PENDING:
3081 DBG_CMD("RELAYD_ROTATE_PENDING", conn);
3082 ret = relay_rotate_pending(header, conn, payload);
3083 break;
3084 case RELAYD_MKDIR:
3085 DBG_CMD("RELAYD_MKDIR", conn);
3086 ret = relay_mkdir(header, conn, payload);
3087 break;
3088 case RELAYD_UPDATE_SYNC_INFO:
3089 default:
3090 ERR("Received unknown command (%u)", header->cmd);
3091 relay_unknown_command(conn);
3092 ret = -1;
3093 goto end;
3094 }
3095
3096 end:
3097 return ret;
3098 }
3099
3100 static enum relay_connection_status relay_process_control_receive_payload(
3101 struct relay_connection *conn)
3102 {
3103 int ret = 0;
3104 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
3105 struct lttng_dynamic_buffer *reception_buffer =
3106 &conn->protocol.ctrl.reception_buffer;
3107 struct ctrl_connection_state_receive_payload *state =
3108 &conn->protocol.ctrl.state.receive_payload;
3109 struct lttng_buffer_view payload_view;
3110
3111 if (state->left_to_receive == 0) {
3112 /* Short-circuit for payload-less commands. */
3113 goto reception_complete;
3114 }
3115
3116 ret = conn->sock->ops->recvmsg(conn->sock,
3117 reception_buffer->data + state->received,
3118 state->left_to_receive, MSG_DONTWAIT);
3119 if (ret < 0) {
3120 if (errno != EAGAIN && errno != EWOULDBLOCK) {
3121 PERROR("Unable to receive command payload on sock %d",
3122 conn->sock->fd);
3123 status = RELAY_CONNECTION_STATUS_ERROR;
3124 }
3125 goto end;
3126 } else if (ret == 0) {
3127 DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd);
3128 status = RELAY_CONNECTION_STATUS_CLOSED;
3129 goto end;
3130 }
3131
3132 assert(ret > 0);
3133 assert(ret <= state->left_to_receive);
3134
3135 state->left_to_receive -= ret;
3136 state->received += ret;
3137
3138 if (state->left_to_receive > 0) {
3139 /*
3140 * Can't transition to the protocol's next state, wait to
3141 * receive the rest of the header.
3142 */
3143 DBG3("Partial reception of control connection protocol payload (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)",
3144 state->received, state->left_to_receive,
3145 conn->sock->fd);
3146 goto end;
3147 }
3148
3149 reception_complete:
3150 DBG("Done receiving control command payload: fd = %i, payload size = %" PRIu64 " bytes",
3151 conn->sock->fd, state->received);
3152 /*
3153 * The payload required to process the command has been received.
3154 * A view to the reception buffer is forwarded to the various
3155 * commands and the state of the control is reset on success.
3156 *
3157 * Commands are responsible for sending their reply to the peer.
3158 */
3159 payload_view = lttng_buffer_view_from_dynamic_buffer(reception_buffer,
3160 0, -1);
3161 ret = relay_process_control_command(conn,
3162 &state->header, &payload_view);
3163 if (ret < 0) {
3164 status = RELAY_CONNECTION_STATUS_ERROR;
3165 goto end;
3166 }
3167
3168 ret = connection_reset_protocol_state(conn);
3169 if (ret) {
3170 status = RELAY_CONNECTION_STATUS_ERROR;
3171 }
3172 end:
3173 return status;
3174 }
3175
3176 static enum relay_connection_status relay_process_control_receive_header(
3177 struct relay_connection *conn)
3178 {
3179 int ret = 0;
3180 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
3181 struct lttcomm_relayd_hdr header;
3182 struct lttng_dynamic_buffer *reception_buffer =
3183 &conn->protocol.ctrl.reception_buffer;
3184 struct ctrl_connection_state_receive_header *state =
3185 &conn->protocol.ctrl.state.receive_header;
3186
3187 assert(state->left_to_receive != 0);
3188
3189 ret = conn->sock->ops->recvmsg(conn->sock,
3190 reception_buffer->data + state->received,
3191 state->left_to_receive, MSG_DONTWAIT);
3192 if (ret < 0) {
3193 if (errno != EAGAIN && errno != EWOULDBLOCK) {
3194 PERROR("Unable to receive control command header on sock %d",
3195 conn->sock->fd);
3196 status = RELAY_CONNECTION_STATUS_ERROR;
3197 }
3198 goto end;
3199 } else if (ret == 0) {
3200 DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd);
3201 status = RELAY_CONNECTION_STATUS_CLOSED;
3202 goto end;
3203 }
3204
3205 assert(ret > 0);
3206 assert(ret <= state->left_to_receive);
3207
3208 state->left_to_receive -= ret;
3209 state->received += ret;
3210
3211 if (state->left_to_receive > 0) {
3212 /*
3213 * Can't transition to the protocol's next state, wait to
3214 * receive the rest of the header.
3215 */
3216 DBG3("Partial reception of control connection protocol header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)",
3217 state->received, state->left_to_receive,
3218 conn->sock->fd);
3219 goto end;
3220 }
3221
3222 /* Transition to next state: receiving the command's payload. */
3223 conn->protocol.ctrl.state_id =
3224 CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD;
3225 memcpy(&header, reception_buffer->data, sizeof(header));
3226 header.circuit_id = be64toh(header.circuit_id);
3227 header.data_size = be64toh(header.data_size);
3228 header.cmd = be32toh(header.cmd);
3229 header.cmd_version = be32toh(header.cmd_version);
3230 memcpy(&conn->protocol.ctrl.state.receive_payload.header,
3231 &header, sizeof(header));
3232
3233 DBG("Done receiving control command header: fd = %i, cmd = %" PRIu32 ", cmd_version = %" PRIu32 ", payload size = %" PRIu64 " bytes",
3234 conn->sock->fd, header.cmd, header.cmd_version,
3235 header.data_size);
3236
3237 if (header.data_size > DEFAULT_NETWORK_RELAYD_CTRL_MAX_PAYLOAD_SIZE) {
3238 ERR("Command header indicates a payload (%" PRIu64 " bytes) that exceeds the maximal payload size allowed on a control connection.",
3239 header.data_size);
3240 status = RELAY_CONNECTION_STATUS_ERROR;
3241 goto end;
3242 }
3243
3244 conn->protocol.ctrl.state.receive_payload.left_to_receive =
3245 header.data_size;
3246 conn->protocol.ctrl.state.receive_payload.received = 0;
3247 ret = lttng_dynamic_buffer_set_size(reception_buffer,
3248 header.data_size);
3249 if (ret) {
3250 status = RELAY_CONNECTION_STATUS_ERROR;
3251 goto end;
3252 }
3253
3254 if (header.data_size == 0) {
3255 /*
3256 * Manually invoke the next state as the poll loop
3257 * will not wake-up to allow us to proceed further.
3258 */
3259 status = relay_process_control_receive_payload(conn);
3260 }
3261 end:
3262 return status;
3263 }
3264
3265 /*
3266 * Process the commands received on the control socket
3267 */
3268 static enum relay_connection_status relay_process_control(
3269 struct relay_connection *conn)
3270 {
3271 enum relay_connection_status status;
3272
3273 switch (conn->protocol.ctrl.state_id) {
3274 case CTRL_CONNECTION_STATE_RECEIVE_HEADER:
3275 status = relay_process_control_receive_header(conn);
3276 break;
3277 case CTRL_CONNECTION_STATE_RECEIVE_PAYLOAD:
3278 status = relay_process_control_receive_payload(conn);
3279 break;
3280 default:
3281 ERR("Unknown control connection protocol state encountered.");
3282 abort();
3283 }
3284
3285 return status;
3286 }
3287
3288 /*
3289 * Handle index for a data stream.
3290 *
3291 * Called with the stream lock held.
3292 *
3293 * Return 0 on success else a negative value.
3294 */
3295 static int handle_index_data(struct relay_stream *stream, uint64_t net_seq_num,
3296 bool rotate_index, bool *flushed, uint64_t total_size)
3297 {
3298 int ret = 0;
3299 uint64_t data_offset;
3300 struct relay_index *index;
3301
3302 /* Get data offset because we are about to update the index. */
3303 data_offset = htobe64(stream->tracefile_size_current);
3304
3305 DBG("handle_index_data: stream %" PRIu64 " net_seq_num %" PRIu64 " data offset %" PRIu64,
3306 stream->stream_handle, net_seq_num, stream->tracefile_size_current);
3307
3308 /*
3309 * Lookup for an existing index for that stream id/sequence
3310 * number. If it exists, the control thread has already received the
3311 * data for it, thus we need to write it to disk.
3312 */
3313 index = relay_index_get_by_id_or_create(stream, net_seq_num);
3314 if (!index) {
3315 ret = -1;
3316 goto end;
3317 }
3318
3319 if (rotate_index || !stream->index_file) {
3320 ret = create_rotate_index_file(stream);
3321 if (ret < 0) {
3322 ERR("Failed to rotate index");
3323 /* Put self-ref for this index due to error. */
3324 relay_index_put(index);
3325 index = NULL;
3326 goto end;
3327 }
3328 }
3329
3330 if (relay_index_set_file(index, stream->index_file, data_offset)) {
3331 ret = -1;
3332 /* Put self-ref for this index due to error. */
3333 relay_index_put(index);
3334 index = NULL;
3335 goto end;
3336 }
3337
3338 ret = relay_index_try_flush(index);
3339 if (ret == 0) {
3340 tracefile_array_commit_seq(stream->tfa);
3341 stream->index_received_seqcount++;
3342 *flushed = true;
3343 } else if (ret > 0) {
3344 index->total_size = total_size;
3345 /* No flush. */
3346 ret = 0;
3347 } else {
3348 /* Put self-ref for this index due to error. */
3349 relay_index_put(index);
3350 index = NULL;
3351 ret = -1;
3352 }
3353 end:
3354 return ret;
3355 }
3356
3357 static enum relay_connection_status relay_process_data_receive_header(
3358 struct relay_connection *conn)
3359 {
3360 int ret;
3361 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
3362 struct data_connection_state_receive_header *state =
3363 &conn->protocol.data.state.receive_header;
3364 struct lttcomm_relayd_data_hdr header;
3365 struct relay_stream *stream;
3366
3367 assert(state->left_to_receive != 0);
3368
3369 ret = conn->sock->ops->recvmsg(conn->sock,
3370 state->header_reception_buffer + state->received,
3371 state->left_to_receive, MSG_DONTWAIT);
3372 if (ret < 0) {
3373 if (errno != EAGAIN && errno != EWOULDBLOCK) {
3374 PERROR("Unable to receive data header on sock %d", conn->sock->fd);
3375 status = RELAY_CONNECTION_STATUS_ERROR;
3376 }
3377 goto end;
3378 } else if (ret == 0) {
3379 /* Orderly shutdown. Not necessary to print an error. */
3380 DBG("Socket %d performed an orderly shutdown (received EOF)", conn->sock->fd);
3381 status = RELAY_CONNECTION_STATUS_CLOSED;
3382 goto end;
3383 }
3384
3385 assert(ret > 0);
3386 assert(ret <= state->left_to_receive);
3387
3388 state->left_to_receive -= ret;
3389 state->received += ret;
3390
3391 if (state->left_to_receive > 0) {
3392 /*
3393 * Can't transition to the protocol's next state, wait to
3394 * receive the rest of the header.
3395 */
3396 DBG3("Partial reception of data connection header (received %" PRIu64 " bytes, %" PRIu64 " bytes left to receive, fd = %i)",
3397 state->received, state->left_to_receive,
3398 conn->sock->fd);
3399 goto end;
3400 }
3401
3402 /* Transition to next state: receiving the payload. */
3403 conn->protocol.data.state_id = DATA_CONNECTION_STATE_RECEIVE_PAYLOAD;
3404
3405 memcpy(&header, state->header_reception_buffer, sizeof(header));
3406 header.circuit_id = be64toh(header.circuit_id);
3407 header.stream_id = be64toh(header.stream_id);
3408 header.data_size = be32toh(header.data_size);
3409 header.net_seq_num = be64toh(header.net_seq_num);
3410 header.padding_size = be32toh(header.padding_size);
3411 memcpy(&conn->protocol.data.state.receive_payload.header, &header, sizeof(header));
3412
3413 conn->protocol.data.state.receive_payload.left_to_receive =
3414 header.data_size;
3415 conn->protocol.data.state.receive_payload.received = 0;
3416 conn->protocol.data.state.receive_payload.rotate_index = false;
3417
3418 DBG("Received data connection header on fd %i: circuit_id = %" PRIu64 ", stream_id = %" PRIu64 ", data_size = %" PRIu32 ", net_seq_num = %" PRIu64 ", padding_size = %" PRIu32,
3419 conn->sock->fd, header.circuit_id,
3420 header.stream_id, header.data_size,
3421 header.net_seq_num, header.padding_size);
3422
3423 stream = stream_get_by_id(header.stream_id);
3424 if (!stream) {
3425 DBG("relay_process_data_receive_payload: Cannot find stream %" PRIu64,
3426 header.stream_id);
3427 /* Protocol error. */
3428 status = RELAY_CONNECTION_STATUS_ERROR;
3429 goto end;
3430 }
3431
3432 pthread_mutex_lock(&stream->lock);
3433
3434 /* Check if a rotation is needed. */
3435 if (stream->tracefile_size > 0 &&
3436 (stream->tracefile_size_current + header.data_size) >
3437 stream->tracefile_size) {
3438 uint64_t old_id, new_id;
3439
3440 old_id = tracefile_array_get_file_index_head(stream->tfa);
3441 tracefile_array_file_rotate(stream->tfa);
3442
3443 /* new_id is updated by utils_rotate_stream_file. */
3444 new_id = old_id;
3445
3446 ret = utils_rotate_stream_file(stream->path_name,
3447 stream->channel_name, stream->tracefile_size,
3448 stream->tracefile_count, -1,
3449 -1, stream->stream_fd->fd,
3450 &new_id, &stream->stream_fd->fd);
3451 if (ret < 0) {
3452 ERR("Failed to rotate stream output file");
3453 status = RELAY_CONNECTION_STATUS_ERROR;
3454 goto end_stream_unlock;
3455 }
3456
3457 /*
3458 * Reset current size because we just performed a stream
3459 * rotation.
3460 */
3461 stream->tracefile_size_current = 0;
3462 conn->protocol.data.state.receive_payload.rotate_index = true;
3463 }
3464
3465 end_stream_unlock:
3466 pthread_mutex_unlock(&stream->lock);
3467 stream_put(stream);
3468 end:
3469 return status;
3470 }
3471
3472 static enum relay_connection_status relay_process_data_receive_payload(
3473 struct relay_connection *conn)
3474 {
3475 int ret;
3476 enum relay_connection_status status = RELAY_CONNECTION_STATUS_OK;
3477 struct relay_stream *stream;
3478 struct data_connection_state_receive_payload *state =
3479 &conn->protocol.data.state.receive_payload;
3480 const size_t chunk_size = RECV_DATA_BUFFER_SIZE;
3481 char data_buffer[chunk_size];
3482 bool partial_recv = false;
3483 bool new_stream = false, close_requested = false, index_flushed = false;
3484 uint64_t left_to_receive = state->left_to_receive;
3485 struct relay_session *session;
3486
3487 DBG3("Receiving data for stream id %" PRIu64 " seqnum %" PRIu64 ", %" PRIu64" bytes received, %" PRIu64 " bytes left to receive",
3488 state->header.stream_id, state->header.net_seq_num,
3489 state->received, left_to_receive);
3490
3491 stream = stream_get_by_id(state->header.stream_id);
3492 if (!stream) {
3493 /* Protocol error. */
3494 ERR("relay_process_data_receive_payload: cannot find stream %" PRIu64,
3495 state->header.stream_id);
3496 status = RELAY_CONNECTION_STATUS_ERROR;
3497 goto end;
3498 }
3499
3500 pthread_mutex_lock(&stream->lock);
3501 session = stream->trace->session;
3502 if (!conn->session) {
3503 ret = connection_set_session(conn, session);
3504 if (ret) {
3505 status = RELAY_CONNECTION_STATUS_ERROR;
3506 goto end_stream_unlock;
3507 }
3508 }
3509
3510 /*
3511 * The size of the "chunk" received on any iteration is bounded by:
3512 * - the data left to receive,
3513 * - the data immediately available on the socket,
3514 * - the on-stack data buffer
3515 */
3516 while (left_to_receive > 0 && !partial_recv) {
3517 ssize_t write_ret;
3518 size_t recv_size = min(left_to_receive, chunk_size);
3519
3520 ret = conn->sock->ops->recvmsg(conn->sock, data_buffer,
3521 recv_size, MSG_DONTWAIT);
3522 if (ret < 0) {
3523 if (errno != EAGAIN && errno != EWOULDBLOCK) {
3524 PERROR("Socket %d error", conn->sock->fd);
3525 status = RELAY_CONNECTION_STATUS_ERROR;
3526 }
3527 goto end_stream_unlock;
3528 } else if (ret == 0) {
3529 /* No more data ready to be consumed on socket. */
3530 DBG3("No more data ready for consumption on data socket of stream id %" PRIu64,
3531 state->header.stream_id);
3532 status = RELAY_CONNECTION_STATUS_CLOSED;
3533 break;
3534 } else if (ret < (int) recv_size) {
3535 /*
3536 * All the data available on the socket has been
3537 * consumed.
3538 */
3539 partial_recv = true;
3540 }
3541
3542 recv_size = ret;
3543
3544 /* Write data to stream output fd. */
3545 write_ret = lttng_write(stream->stream_fd->fd, data_buffer,
3546 recv_size);
3547 if (write_ret < (ssize_t) recv_size) {
3548 ERR("Relay error writing data to file");
3549 status = RELAY_CONNECTION_STATUS_ERROR;
3550 goto end_stream_unlock;
3551 }
3552
3553 left_to_receive -= recv_size;
3554 state->received += recv_size;
3555 state->left_to_receive = left_to_receive;
3556
3557 DBG2("Relay wrote %zd bytes to tracefile for stream id %" PRIu64,
3558 write_ret, stream->stream_handle);
3559 }
3560
3561 if (state->left_to_receive > 0) {
3562 /*
3563 * Did not receive all the data expected, wait for more data to
3564 * become available on the socket.
3565 */
3566 DBG3("Partial receive on data connection of stream id %" PRIu64 ", %" PRIu64 " bytes received, %" PRIu64 " bytes left to receive",
3567 state->header.stream_id, state->received,
3568 state->left_to_receive);
3569 goto end_stream_unlock;
3570 }
3571
3572 ret = write_padding_to_file(stream->stream_fd->fd,
3573 state->header.padding_size);
3574 if ((int64_t) ret < (int64_t) state->header.padding_size) {
3575 ERR("write_padding_to_file: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d",
3576 stream->stream_handle,
3577 state->header.net_seq_num, ret);
3578 status = RELAY_CONNECTION_STATUS_ERROR;
3579 goto end_stream_unlock;
3580 }
3581
3582
3583 if (session->minor >= 4 && !session->snapshot) {
3584 ret = handle_index_data(stream, state->header.net_seq_num,
3585 state->rotate_index, &index_flushed, state->header.data_size + state->header.padding_size);
3586 if (ret < 0) {
3587 ERR("handle_index_data: fail stream %" PRIu64 " net_seq_num %" PRIu64 " ret %d",
3588 stream->stream_handle,
3589 state->header.net_seq_num, ret);
3590 status = RELAY_CONNECTION_STATUS_ERROR;
3591 goto end_stream_unlock;
3592 }
3593 }
3594
3595 stream->tracefile_size_current += state->header.data_size +
3596 state->header.padding_size;
3597
3598 if (stream->prev_seq == -1ULL) {
3599 new_stream = true;
3600 }
3601 if (index_flushed) {
3602 stream->pos_after_last_complete_data_index =
3603 stream->tracefile_size_current;
3604 }
3605
3606 stream->prev_seq = state->header.net_seq_num;
3607
3608 /*
3609 * Resetting the protocol state (to RECEIVE_HEADER) will trash the
3610 * contents of *state which are aliased (union) to the same location as
3611 * the new state. Don't use it beyond this point.
3612 */
3613 connection_reset_protocol_state(conn);
3614 state = NULL;
3615
3616 ret = try_rotate_stream(stream);
3617 if (ret < 0) {
3618 status = RELAY_CONNECTION_STATUS_ERROR;
3619 goto end_stream_unlock;
3620 }
3621
3622 end_stream_unlock:
3623 close_requested = stream->close_requested;
3624 pthread_mutex_unlock(&stream->lock);
3625 if (close_requested && left_to_receive == 0) {
3626 try_stream_close(stream);
3627 }
3628
3629 if (new_stream) {
3630 pthread_mutex_lock(&session->lock);
3631 uatomic_set(&session->new_streams, 1);
3632 pthread_mutex_unlock(&session->lock);
3633 }
3634
3635 stream_put(stream);
3636 end:
3637 return status;
3638 }
3639
3640 /*
3641 * relay_process_data: Process the data received on the data socket
3642 */
3643 static enum relay_connection_status relay_process_data(
3644 struct relay_connection *conn)
3645 {
3646 enum relay_connection_status status;
3647
3648 switch (conn->protocol.data.state_id) {
3649 case DATA_CONNECTION_STATE_RECEIVE_HEADER:
3650 status = relay_process_data_receive_header(conn);
3651 break;
3652 case DATA_CONNECTION_STATE_RECEIVE_PAYLOAD:
3653 status = relay_process_data_receive_payload(conn);
3654 break;
3655 default:
3656 ERR("Unexpected data connection communication state.");
3657 abort();
3658 }
3659
3660 return status;
3661 }
3662
3663 static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
3664 {
3665 int ret;
3666
3667 (void) lttng_poll_del(events, pollfd);
3668
3669 ret = fd_tracker_close_unsuspendable_fd(the_fd_tracker, &pollfd, 1,
3670 fd_tracker_util_close_fd, NULL);
3671 if (ret < 0) {
3672 ERR("Closing pollfd %d", pollfd);
3673 }
3674 }
3675
3676 static void relay_thread_close_connection(struct lttng_poll_event *events,
3677 int pollfd, struct relay_connection *conn)
3678 {
3679 const char *type_str;
3680
3681 switch (conn->type) {
3682 case RELAY_DATA:
3683 type_str = "Data";
3684 break;
3685 case RELAY_CONTROL:
3686 type_str = "Control";
3687 break;
3688 case RELAY_VIEWER_COMMAND:
3689 type_str = "Viewer Command";
3690 break;
3691 case RELAY_VIEWER_NOTIFICATION:
3692 type_str = "Viewer Notification";
3693 break;
3694 default:
3695 type_str = "Unknown";
3696 }
3697 cleanup_connection_pollfd(events, pollfd);
3698 connection_put(conn);
3699 DBG("%s connection closed with %d", type_str, pollfd);
3700 }
3701
3702 /*
3703 * This thread does the actual work
3704 */
3705 static void *relay_thread_worker(void *data)
3706 {
3707 int ret, err = -1, last_seen_data_fd = -1;
3708 uint32_t nb_fd;
3709 struct lttng_poll_event events;
3710 struct lttng_ht *relay_connections_ht;
3711 struct lttng_ht_iter iter;
3712 struct relay_connection *destroy_conn = NULL;
3713
3714 DBG("[thread] Relay worker started");
3715
3716 rcu_register_thread();
3717
3718 health_register(health_relayd, HEALTH_RELAYD_TYPE_WORKER);
3719
3720 if (testpoint(relayd_thread_worker)) {
3721 goto error_testpoint;
3722 }
3723
3724 health_code_update();
3725
3726 /* table of connections indexed on socket */
3727 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3728 if (!relay_connections_ht) {
3729 goto relay_connections_ht_error;
3730 }
3731
3732 ret = create_named_thread_poll_set(&events, 2, "Worker thread epoll");
3733 if (ret < 0) {
3734 goto error_poll_create;
3735 }
3736
3737 ret = lttng_poll_add(&events, relay_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
3738 if (ret < 0) {
3739 goto error;
3740 }
3741
3742 restart:
3743 while (1) {
3744 int idx = -1, i, seen_control = 0, last_notdel_data_fd = -1;
3745
3746 health_code_update();
3747
3748 /* Infinite blocking call, waiting for transmission */
3749 DBG3("Relayd worker thread polling...");
3750 health_poll_entry();
3751 ret = lttng_poll_wait(&events, -1);
3752 health_poll_exit();
3753 if (ret < 0) {
3754 /*
3755 * Restart interrupted system call.
3756 */
3757 if (errno == EINTR) {
3758 goto restart;
3759 }
3760 goto error;
3761 }
3762
3763 nb_fd = ret;
3764
3765 /*
3766 * Process control. The control connection is
3767 * prioritized so we don't starve it with high
3768 * throughput tracing data on the data connection.
3769 */
3770 for (i = 0; i < nb_fd; i++) {
3771 /* Fetch once the poll data */
3772 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
3773 int pollfd = LTTNG_POLL_GETFD(&events, i);
3774
3775 health_code_update();
3776
3777 if (!revents) {
3778 /*
3779 * No activity for this FD (poll
3780 * implementation).
3781 */
3782 continue;
3783 }
3784
3785 /* Thread quit pipe has been closed. Killing thread. */
3786 ret = check_thread_quit_pipe(pollfd, revents);
3787 if (ret) {
3788 err = 0;
3789 goto exit;
3790 }
3791
3792 /* Inspect the relay conn pipe for new connection */
3793 if (pollfd == relay_conn_pipe[0]) {
3794 if (revents & LPOLLIN) {
3795 struct relay_connection *conn;
3796
3797 ret = lttng_read(relay_conn_pipe[0], &conn, sizeof(conn));
3798 if (ret < 0) {
3799 goto error;
3800 }
3801 lttng_poll_add(&events, conn->sock->fd,
3802 LPOLLIN | LPOLLRDHUP);
3803 connection_ht_add(relay_connections_ht, conn);
3804 DBG("Connection socket %d added", conn->sock->fd);
3805 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3806 ERR("Relay connection pipe error");
3807 goto error;
3808 } else {
3809 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
3810 goto error;
3811 }
3812 } else {
3813 struct relay_connection *ctrl_conn;
3814
3815 ctrl_conn = connection_get_by_sock(relay_connections_ht, pollfd);
3816 /* If not found, there is a synchronization issue. */
3817 assert(ctrl_conn);
3818
3819 if (ctrl_conn->type == RELAY_DATA) {
3820 if (revents & LPOLLIN) {
3821 /*
3822 * Flag the last seen data fd not deleted. It will be
3823 * used as the last seen fd if any fd gets deleted in
3824 * this first loop.
3825 */
3826 last_notdel_data_fd = pollfd;
3827 }
3828 goto put_ctrl_connection;
3829 }
3830 assert(ctrl_conn->type == RELAY_CONTROL);
3831
3832 if (revents & LPOLLIN) {
3833 enum relay_connection_status status;
3834
3835 status = relay_process_control(ctrl_conn);
3836 if (status != RELAY_CONNECTION_STATUS_OK) {
3837 /*
3838 * On socket error flag the session as aborted to force
3839 * the cleanup of its stream otherwise it can leak
3840 * during the lifetime of the relayd.
3841 *
3842 * This prevents situations in which streams can be
3843 * left opened because an index was received, the
3844 * control connection is closed, and the data
3845 * connection is closed (uncleanly) before the packet's
3846 * data provided.
3847 *
3848 * Since the control connection encountered an error,
3849 * it is okay to be conservative and close the
3850 * session right now as we can't rely on the protocol
3851 * being respected anymore.
3852 */
3853 if (status == RELAY_CONNECTION_STATUS_ERROR) {
3854 session_abort(ctrl_conn->session);
3855 }
3856
3857 /* Clear the connection on error or close. */
3858 relay_thread_close_connection(&events,
3859 pollfd,
3860 ctrl_conn);
3861 }
3862 seen_control = 1;
3863 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3864 relay_thread_close_connection(&events,
3865 pollfd, ctrl_conn);
3866 if (last_seen_data_fd == pollfd) {
3867 last_seen_data_fd = last_notdel_data_fd;
3868 }
3869 } else {
3870 ERR("Unexpected poll events %u for control sock %d",
3871 revents, pollfd);
3872 connection_put(ctrl_conn);
3873 goto error;
3874 }
3875 put_ctrl_connection:
3876 connection_put(ctrl_conn);
3877 }
3878 }
3879
3880 /*
3881 * The last loop handled a control request, go back to poll to make
3882 * sure we prioritise the control socket.
3883 */
3884 if (seen_control) {
3885 continue;
3886 }
3887
3888 if (last_seen_data_fd >= 0) {
3889 for (i = 0; i < nb_fd; i++) {
3890 int pollfd = LTTNG_POLL_GETFD(&events, i);
3891
3892 health_code_update();
3893
3894 if (last_seen_data_fd == pollfd) {
3895 idx = i;
3896 break;
3897 }
3898 }
3899 }
3900
3901 /* Process data connection. */
3902 for (i = idx + 1; i < nb_fd; i++) {
3903 /* Fetch the poll data. */
3904 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
3905 int pollfd = LTTNG_POLL_GETFD(&events, i);
3906 struct relay_connection *data_conn;
3907
3908 health_code_update();
3909
3910 if (!revents) {
3911 /* No activity for this FD (poll implementation). */
3912 continue;
3913 }
3914
3915 /* Skip the command pipe. It's handled in the first loop. */
3916 if (pollfd == relay_conn_pipe[0]) {
3917 continue;
3918 }
3919
3920 data_conn = connection_get_by_sock(relay_connections_ht, pollfd);
3921 if (!data_conn) {
3922 /* Skip it. Might be removed before. */
3923 continue;
3924 }
3925 if (data_conn->type == RELAY_CONTROL) {
3926 goto put_data_connection;
3927 }
3928 assert(data_conn->type == RELAY_DATA);
3929
3930 if (revents & LPOLLIN) {
3931 enum relay_connection_status status;
3932
3933 status = relay_process_data(data_conn);
3934 /* Connection closed or error. */
3935 if (status != RELAY_CONNECTION_STATUS_OK) {
3936 /*
3937 * On socket error flag the session as aborted to force
3938 * the cleanup of its stream otherwise it can leak
3939 * during the lifetime of the relayd.
3940 *
3941 * This prevents situations in which streams can be
3942 * left opened because an index was received, the
3943 * control connection is closed, and the data
3944 * connection is closed (uncleanly) before the packet's
3945 * data provided.
3946 *
3947 * Since the data connection encountered an error,
3948 * it is okay to be conservative and close the
3949 * session right now as we can't rely on the protocol
3950 * being respected anymore.
3951 */
3952 if (status == RELAY_CONNECTION_STATUS_ERROR) {
3953 session_abort(data_conn->session);
3954 }
3955 relay_thread_close_connection(&events, pollfd,
3956 data_conn);
3957 /*
3958 * Every goto restart call sets the last seen fd where
3959 * here we don't really care since we gracefully
3960 * continue the loop after the connection is deleted.
3961 */
3962 } else {
3963 /* Keep last seen port. */
3964 last_seen_data_fd = pollfd;
3965 connection_put(data_conn);
3966 goto restart;
3967 }
3968 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3969 relay_thread_close_connection(&events, pollfd,
3970 data_conn);
3971 } else {
3972 ERR("Unknown poll events %u for data sock %d",
3973 revents, pollfd);
3974 }
3975 put_data_connection:
3976 connection_put(data_conn);
3977 }
3978 last_seen_data_fd = -1;
3979 }
3980
3981 /* Normal exit, no error */
3982 ret = 0;
3983
3984 exit:
3985 error:
3986 /* Cleanup remaining connection object. */
3987 rcu_read_lock();
3988 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter,
3989 destroy_conn,
3990 sock_n.node) {
3991 health_code_update();
3992
3993 session_abort(destroy_conn->session);
3994
3995 /*
3996 * No need to grab another ref, because we own
3997 * destroy_conn.
3998 */
3999 relay_thread_close_connection(&events, destroy_conn->sock->fd,
4000 destroy_conn);
4001 }
4002 rcu_read_unlock();
4003
4004 (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
4005 error_poll_create:
4006 lttng_ht_destroy(relay_connections_ht);
4007 relay_connections_ht_error:
4008 /* Close relay conn pipes */
4009 (void) fd_tracker_util_pipe_close(the_fd_tracker,
4010 relay_conn_pipe);
4011 if (err) {
4012 DBG("Thread exited with error");
4013 }
4014 DBG("Worker thread cleanup complete");
4015 error_testpoint:
4016 if (err) {
4017 health_error();
4018 ERR("Health error occurred in %s", __func__);
4019 }
4020 health_unregister(health_relayd);
4021 rcu_unregister_thread();
4022 lttng_relay_stop_threads();
4023 return NULL;
4024 }
4025
4026 /*
4027 * Create the relay command pipe to wake thread_manage_apps.
4028 * Closed in cleanup().
4029 */
4030 static int create_relay_conn_pipe(void)
4031 {
4032 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
4033 "Relayd connection pipe", relay_conn_pipe);
4034 }
4035
4036 static
4037 int stdio_open(void *data, int *fds)
4038 {
4039 fds[0] = fileno(stdout);
4040 fds[1] = fileno(stderr);
4041 return 0;
4042 }
4043
4044 static
4045 int noop_close(void *data, int *fds)
4046 {
4047 return 0;
4048 }
4049
4050 static
4051 int track_stdio(void)
4052 {
4053 int fds[2];
4054 const char *names[] = { "stdout", "stderr" };
4055
4056 return fd_tracker_open_unsuspendable_fd(the_fd_tracker, fds,
4057 names, 2, stdio_open, NULL);
4058 }
4059
4060 static
4061 void untrack_stdio(void)
4062 {
4063 int fds[] = { fileno(stdout), fileno(stderr) };
4064
4065 /*
4066 * noop_close is used since we don't really want to close
4067 * the stdio output fds; we merely want to stop tracking them.
4068 */
4069 (void) fd_tracker_close_unsuspendable_fd(the_fd_tracker,
4070 fds, 2, noop_close, NULL);
4071 }
4072
4073 /*
4074 * main
4075 */
4076 int main(int argc, char **argv)
4077 {
4078 int ret = 0, retval = 0;
4079 void *status;
4080
4081 /* Parse arguments */
4082 progname = argv[0];
4083 if (set_options(argc, argv)) {
4084 retval = -1;
4085 goto exit_options;
4086 }
4087
4088 if (set_signal_handler()) {
4089 retval = -1;
4090 goto exit_options;
4091 }
4092
4093 ret = fclose(stdin);
4094 if (ret) {
4095 PERROR("Failed to close stdin");
4096 goto exit_options;
4097 }
4098 /* Try to create directory if -o, --output is specified. */
4099 if (opt_output_path) {
4100 if (*opt_output_path != '/') {
4101 ERR("Please specify an absolute path for -o, --output PATH");
4102 retval = -1;
4103 goto exit_options;
4104 }
4105
4106 ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG,
4107 -1, -1);
4108 if (ret < 0) {
4109 ERR("Unable to create %s", opt_output_path);
4110 retval = -1;
4111 goto exit_options;
4112 }
4113 }
4114
4115 /* Daemonize */
4116 if (opt_daemon || opt_background) {
4117 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
4118 !opt_background);
4119 if (ret < 0) {
4120 retval = -1;
4121 goto exit_options;
4122 }
4123 }
4124
4125 /*
4126 * The RCU thread registration (and use, through the fd-tracker's
4127 * creation) is done after the daemonization to allow us to not
4128 * deal with liburcu's fork() management as the call RCU needs to
4129 * be restored.
4130 */
4131 rcu_register_thread();
4132
4133 the_fd_tracker = fd_tracker_create(lttng_opt_fd_cap);
4134 if (!the_fd_tracker) {
4135 retval = -1;
4136 goto exit_options;
4137 }
4138
4139 ret = track_stdio();
4140 if (ret) {
4141 retval = -1;
4142 goto exit_options;
4143 }
4144
4145 /* Initialize thread health monitoring */
4146 health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES);
4147 if (!health_relayd) {
4148 PERROR("health_app_create error");
4149 retval = -1;
4150 goto exit_health_app_create;
4151 }
4152
4153 /* Create thread quit pipe */
4154 if (init_thread_quit_pipe()) {
4155 retval = -1;
4156 goto exit_init_data;
4157 }
4158
4159 /* Setup the thread apps communication pipe. */
4160 if (create_relay_conn_pipe()) {
4161 retval = -1;
4162 goto exit_init_data;
4163 }
4164
4165 /* Init relay command queue. */
4166 cds_wfcq_init(&relay_conn_queue.head, &relay_conn_queue.tail);
4167
4168 /* Initialize communication library */
4169 lttcomm_init();
4170 lttcomm_inet_init();
4171
4172 /* tables of sessions indexed by session ID */
4173 sessions_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4174 if (!sessions_ht) {
4175 retval = -1;
4176 goto exit_init_data;
4177 }
4178
4179 /* tables of streams indexed by stream ID */
4180 relay_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4181 if (!relay_streams_ht) {
4182 retval = -1;
4183 goto exit_init_data;
4184 }
4185
4186 /* tables of streams indexed by stream ID */
4187 viewer_streams_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4188 if (!viewer_streams_ht) {
4189 retval = -1;
4190 goto exit_init_data;
4191 }
4192
4193 ret = init_health_quit_pipe();
4194 if (ret) {
4195 retval = -1;
4196 goto exit_health_quit_pipe;
4197 }
4198
4199 /* Create thread to manage the client socket */
4200 ret = pthread_create(&health_thread, default_pthread_attr(),
4201 thread_manage_health, (void *) NULL);
4202 if (ret) {
4203 errno = ret;
4204 PERROR("pthread_create health");
4205 retval = -1;
4206 goto exit_health_thread;
4207 }
4208
4209 /* Setup the dispatcher thread */
4210 ret = pthread_create(&dispatcher_thread, default_pthread_attr(),
4211 relay_thread_dispatcher, (void *) NULL);
4212 if (ret) {
4213 errno = ret;
4214 PERROR("pthread_create dispatcher");
4215 retval = -1;
4216 goto exit_dispatcher_thread;
4217 }
4218
4219 /* Setup the worker thread */
4220 ret = pthread_create(&worker_thread, default_pthread_attr(),
4221 relay_thread_worker, NULL);
4222 if (ret) {
4223 errno = ret;
4224 PERROR("pthread_create worker");
4225 retval = -1;
4226 goto exit_worker_thread;
4227 }
4228
4229 /* Setup the listener thread */
4230 ret = pthread_create(&listener_thread, default_pthread_attr(),
4231 relay_thread_listener, (void *) NULL);
4232 if (ret) {
4233 errno = ret;
4234 PERROR("pthread_create listener");
4235 retval = -1;
4236 goto exit_listener_thread;
4237 }
4238
4239 ret = relayd_live_create(live_uri);
4240 if (ret) {
4241 ERR("Starting live viewer threads");
4242 retval = -1;
4243 goto exit_live;
4244 }
4245
4246 /*
4247 * This is where we start awaiting program completion (e.g. through
4248 * signal that asks threads to teardown).
4249 */
4250
4251 ret = relayd_live_join();
4252 if (ret) {
4253 retval = -1;
4254 }
4255 exit_live:
4256
4257 ret = pthread_join(listener_thread, &status);
4258 if (ret) {
4259 errno = ret;
4260 PERROR("pthread_join listener_thread");
4261 retval = -1;
4262 }
4263
4264 exit_listener_thread:
4265 ret = pthread_join(worker_thread, &status);
4266 if (ret) {
4267 errno = ret;
4268 PERROR("pthread_join worker_thread");
4269 retval = -1;
4270 }
4271
4272 exit_worker_thread:
4273 ret = pthread_join(dispatcher_thread, &status);
4274 if (ret) {
4275 errno = ret;
4276 PERROR("pthread_join dispatcher_thread");
4277 retval = -1;
4278 }
4279 exit_dispatcher_thread:
4280
4281 ret = pthread_join(health_thread, &status);
4282 if (ret) {
4283 errno = ret;
4284 PERROR("pthread_join health_thread");
4285 retval = -1;
4286 }
4287 exit_health_thread:
4288
4289 (void) fd_tracker_util_pipe_close(the_fd_tracker, health_quit_pipe);
4290 exit_health_quit_pipe:
4291
4292 exit_init_data:
4293 health_app_destroy(health_relayd);
4294 exit_health_app_create:
4295 exit_options:
4296 /*
4297 * Wait for all pending call_rcu work to complete before tearing
4298 * down data structures. call_rcu worker may be trying to
4299 * perform lookups in those structures.
4300 */
4301 rcu_barrier();
4302 relayd_cleanup();
4303
4304 /* Ensure all prior call_rcu are done. */
4305 rcu_barrier();
4306
4307 untrack_stdio();
4308 /*
4309 * fd_tracker_destroy() will log the contents of the fd-tracker
4310 * if a leak is detected.
4311 */
4312 fd_tracker_destroy(the_fd_tracker);
4313 rcu_unregister_thread();
4314
4315 if (!retval) {
4316 exit(EXIT_SUCCESS);
4317 } else {
4318 exit(EXIT_FAILURE);
4319 }
4320 }
This page took 0.116318 seconds and 6 git commands to generate.