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