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