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