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