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