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