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