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