| 1 | /* |
| 2 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | |
| 10 | #include <common/common.hpp> |
| 11 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 12 | #include <common/uri.hpp> |
| 13 | #include <common/utils.hpp> |
| 14 | |
| 15 | #include <common/compat/endian.hpp> |
| 16 | |
| 17 | #include "fd-limit.hpp" |
| 18 | #include "agent-thread.hpp" |
| 19 | #include "agent.hpp" |
| 20 | #include "lttng-sessiond.hpp" |
| 21 | #include "session.hpp" |
| 22 | #include "utils.hpp" |
| 23 | #include "thread.hpp" |
| 24 | |
| 25 | namespace { |
| 26 | struct thread_notifiers { |
| 27 | struct lttng_pipe *quit_pipe; |
| 28 | sem_t ready; |
| 29 | }; |
| 30 | |
| 31 | struct agent_app_id { |
| 32 | pid_t pid; |
| 33 | enum lttng_domain_type domain; |
| 34 | }; |
| 35 | |
| 36 | struct agent_protocol_version { |
| 37 | unsigned int major, minor; |
| 38 | }; |
| 39 | |
| 40 | int agent_tracing_enabled = -1; |
| 41 | |
| 42 | /* |
| 43 | * Note that there is not port here. It's set after this URI is parsed so we |
| 44 | * can let the user define a custom one. However, localhost is ALWAYS the |
| 45 | * default listening address. |
| 46 | */ |
| 47 | const char *default_reg_uri = "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS; |
| 48 | } /* namespace */ |
| 49 | |
| 50 | /* |
| 51 | * Update agent application using the given socket. This is done just after |
| 52 | * registration was successful. |
| 53 | * |
| 54 | * This will acquire the various sessions' lock; none must be held by the |
| 55 | * caller. |
| 56 | * The caller must hold the session list lock. |
| 57 | */ |
| 58 | static void update_agent_app(const struct agent_app *app) |
| 59 | { |
| 60 | struct ltt_session *session, *stmp; |
| 61 | struct ltt_session_list *list; |
| 62 | struct agent *trigger_agent; |
| 63 | struct lttng_ht_iter iter; |
| 64 | |
| 65 | list = session_get_list(); |
| 66 | LTTNG_ASSERT(list); |
| 67 | |
| 68 | cds_list_for_each_entry_safe(session, stmp, &list->head, list) { |
| 69 | if (!session_get(session)) { |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | session_lock(session); |
| 74 | if (session->ust_session) { |
| 75 | const struct agent *agt; |
| 76 | |
| 77 | rcu_read_lock(); |
| 78 | agt = trace_ust_find_agent(session->ust_session, app->domain); |
| 79 | if (agt) { |
| 80 | agent_update(agt, app); |
| 81 | } |
| 82 | rcu_read_unlock(); |
| 83 | } |
| 84 | session_unlock(session); |
| 85 | session_put(session); |
| 86 | } |
| 87 | |
| 88 | rcu_read_lock(); |
| 89 | /* |
| 90 | * We are protected against the addition of new events by the session |
| 91 | * list lock being held. |
| 92 | */ |
| 93 | cds_lfht_for_each_entry(the_trigger_agents_ht_by_domain->ht, |
| 94 | &iter.iter, trigger_agent, node.node) { |
| 95 | agent_update(trigger_agent, app); |
| 96 | } |
| 97 | rcu_read_unlock(); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Create and init socket from uri. |
| 102 | */ |
| 103 | static struct lttcomm_sock *init_tcp_socket(void) |
| 104 | { |
| 105 | int ret; |
| 106 | struct lttng_uri *uri = NULL; |
| 107 | struct lttcomm_sock *sock = NULL; |
| 108 | unsigned int port; |
| 109 | bool bind_succeeded = false; |
| 110 | |
| 111 | /* |
| 112 | * This should never fail since the URI is hardcoded and the port is set |
| 113 | * before this thread is launched. |
| 114 | */ |
| 115 | ret = uri_parse(default_reg_uri, &uri); |
| 116 | LTTNG_ASSERT(ret); |
| 117 | LTTNG_ASSERT(the_config.agent_tcp_port.begin > 0); |
| 118 | uri->port = the_config.agent_tcp_port.begin; |
| 119 | |
| 120 | sock = lttcomm_alloc_sock_from_uri(uri); |
| 121 | uri_free(uri); |
| 122 | if (sock == NULL) { |
| 123 | ERR("agent allocating TCP socket"); |
| 124 | goto error; |
| 125 | } |
| 126 | |
| 127 | ret = lttcomm_create_sock(sock); |
| 128 | if (ret < 0) { |
| 129 | goto error; |
| 130 | } |
| 131 | |
| 132 | for (port = the_config.agent_tcp_port.begin; |
| 133 | port <= the_config.agent_tcp_port.end; port++) { |
| 134 | ret = lttcomm_sock_set_port(sock, (uint16_t) port); |
| 135 | if (ret) { |
| 136 | ERR("Failed to set port %u on socket", |
| 137 | port); |
| 138 | goto error; |
| 139 | } |
| 140 | DBG3("Trying to bind on port %u", port); |
| 141 | ret = sock->ops->bind(sock); |
| 142 | if (!ret) { |
| 143 | bind_succeeded = true; |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | if (errno == EADDRINUSE) { |
| 148 | DBG("Failed to bind to port %u since it is already in use", |
| 149 | port); |
| 150 | } else { |
| 151 | PERROR("Failed to bind to port %u", port); |
| 152 | goto error; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (!bind_succeeded) { |
| 157 | if (the_config.agent_tcp_port.begin == |
| 158 | the_config.agent_tcp_port.end) { |
| 159 | WARN("Another process is already using the agent port %i. " |
| 160 | "Agent support will be deactivated.", |
| 161 | the_config.agent_tcp_port.begin); |
| 162 | goto error; |
| 163 | } else { |
| 164 | WARN("All ports in the range [%i, %i] are already in use. " |
| 165 | "Agent support will be deactivated.", |
| 166 | the_config.agent_tcp_port.begin, |
| 167 | the_config.agent_tcp_port.end); |
| 168 | goto error; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | ret = sock->ops->listen(sock, -1); |
| 173 | if (ret < 0) { |
| 174 | goto error; |
| 175 | } |
| 176 | |
| 177 | DBG("Listening on TCP port %u and socket %d", |
| 178 | port, sock->fd); |
| 179 | |
| 180 | return sock; |
| 181 | |
| 182 | error: |
| 183 | if (sock) { |
| 184 | lttcomm_destroy_sock(sock); |
| 185 | } |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Close and destroy the given TCP socket. |
| 191 | */ |
| 192 | static void destroy_tcp_socket(struct lttcomm_sock *sock) |
| 193 | { |
| 194 | int ret; |
| 195 | uint16_t port; |
| 196 | |
| 197 | LTTNG_ASSERT(sock); |
| 198 | |
| 199 | ret = lttcomm_sock_get_port(sock, &port); |
| 200 | if (ret) { |
| 201 | ERR("Failed to get port of agent TCP socket"); |
| 202 | port = 0; |
| 203 | } |
| 204 | |
| 205 | DBG3("Destroy TCP socket on port %" PRIu16, |
| 206 | port); |
| 207 | |
| 208 | /* This will return gracefully if fd is invalid. */ |
| 209 | sock->ops->close(sock); |
| 210 | lttcomm_destroy_sock(sock); |
| 211 | } |
| 212 | |
| 213 | static const char *domain_type_str(enum lttng_domain_type domain_type) |
| 214 | { |
| 215 | switch (domain_type) { |
| 216 | case LTTNG_DOMAIN_NONE: |
| 217 | return "none"; |
| 218 | case LTTNG_DOMAIN_KERNEL: |
| 219 | return "kernel"; |
| 220 | case LTTNG_DOMAIN_UST: |
| 221 | return "ust"; |
| 222 | case LTTNG_DOMAIN_JUL: |
| 223 | return "jul"; |
| 224 | case LTTNG_DOMAIN_LOG4J: |
| 225 | return "log4j"; |
| 226 | case LTTNG_DOMAIN_PYTHON: |
| 227 | return "python"; |
| 228 | default: |
| 229 | return "unknown"; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static bool is_agent_protocol_version_supported( |
| 234 | const struct agent_protocol_version *version) |
| 235 | { |
| 236 | const bool is_supported = version->major == AGENT_MAJOR_VERSION && |
| 237 | version->minor == AGENT_MINOR_VERSION; |
| 238 | |
| 239 | if (!is_supported) { |
| 240 | WARN("Refusing agent connection: unsupported protocol version %ui.%ui, expected %i.%i", |
| 241 | version->major, version->minor, |
| 242 | AGENT_MAJOR_VERSION, AGENT_MINOR_VERSION); |
| 243 | } |
| 244 | |
| 245 | return is_supported; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Handle a new agent connection on the registration socket. |
| 250 | * |
| 251 | * Returns 0 on success, or else a negative errno value. |
| 252 | * On success, the resulting socket is returned through `agent_app_socket` |
| 253 | * and the application's reported id is updated through `agent_app_id`. |
| 254 | */ |
| 255 | static int accept_agent_connection( |
| 256 | struct lttcomm_sock *reg_sock, |
| 257 | struct agent_app_id *agent_app_id, |
| 258 | struct lttcomm_sock **agent_app_socket) |
| 259 | { |
| 260 | int ret; |
| 261 | struct agent_protocol_version agent_version; |
| 262 | ssize_t size; |
| 263 | struct agent_register_msg msg; |
| 264 | struct lttcomm_sock *new_sock; |
| 265 | |
| 266 | LTTNG_ASSERT(reg_sock); |
| 267 | |
| 268 | new_sock = reg_sock->ops->accept(reg_sock); |
| 269 | if (!new_sock) { |
| 270 | ret = -ENOTCONN; |
| 271 | goto end; |
| 272 | } |
| 273 | |
| 274 | size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0); |
| 275 | if (size < sizeof(msg)) { |
| 276 | if (size < 0) { |
| 277 | PERROR("Failed to register new agent application"); |
| 278 | } else if (size != 0) { |
| 279 | ERR("Failed to register new agent application: invalid registration message length: expected length = %zu, message length = %zd", |
| 280 | sizeof(msg), size); |
| 281 | } else { |
| 282 | DBG("Failed to register new agent application: connection closed"); |
| 283 | } |
| 284 | ret = -EINVAL; |
| 285 | goto error_close_socket; |
| 286 | } |
| 287 | |
| 288 | agent_version = (struct agent_protocol_version) { |
| 289 | be32toh(msg.major_version), |
| 290 | be32toh(msg.minor_version), |
| 291 | }; |
| 292 | |
| 293 | /* Test communication protocol version of the registering agent. */ |
| 294 | if (!is_agent_protocol_version_supported(&agent_version)) { |
| 295 | ret = -EINVAL; |
| 296 | goto error_close_socket; |
| 297 | } |
| 298 | |
| 299 | *agent_app_id = (struct agent_app_id) { |
| 300 | .pid = (pid_t) be32toh(msg.pid), |
| 301 | .domain = (lttng_domain_type) be32toh(msg.domain), |
| 302 | }; |
| 303 | |
| 304 | DBG2("New registration for agent application: pid = %ld, domain = %s, socket fd = %d", |
| 305 | (long) agent_app_id->pid, |
| 306 | domain_type_str(agent_app_id->domain), new_sock->fd); |
| 307 | |
| 308 | *agent_app_socket = new_sock; |
| 309 | new_sock = NULL; |
| 310 | ret = 0; |
| 311 | goto end; |
| 312 | |
| 313 | error_close_socket: |
| 314 | new_sock->ops->close(new_sock); |
| 315 | lttcomm_destroy_sock(new_sock); |
| 316 | end: |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | bool agent_tracing_is_enabled(void) |
| 321 | { |
| 322 | int enabled; |
| 323 | |
| 324 | enabled = uatomic_read(&agent_tracing_enabled); |
| 325 | LTTNG_ASSERT(enabled != -1); |
| 326 | return enabled == 1; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Write agent TCP port using the rundir. |
| 331 | */ |
| 332 | static int write_agent_port(uint16_t port) |
| 333 | { |
| 334 | return utils_create_pid_file( |
| 335 | (pid_t) port, the_config.agent_port_file_path.value); |
| 336 | } |
| 337 | |
| 338 | static |
| 339 | void mark_thread_as_ready(struct thread_notifiers *notifiers) |
| 340 | { |
| 341 | DBG("Marking agent management thread as ready"); |
| 342 | sem_post(¬ifiers->ready); |
| 343 | } |
| 344 | |
| 345 | static |
| 346 | void wait_until_thread_is_ready(struct thread_notifiers *notifiers) |
| 347 | { |
| 348 | DBG("Waiting for agent management thread to be ready"); |
| 349 | sem_wait(¬ifiers->ready); |
| 350 | DBG("Agent management thread is ready"); |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * This thread manage application notify communication. |
| 355 | */ |
| 356 | static void *thread_agent_management(void *data) |
| 357 | { |
| 358 | int i, ret; |
| 359 | uint32_t nb_fd; |
| 360 | struct lttng_poll_event events; |
| 361 | struct lttcomm_sock *reg_sock; |
| 362 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
| 363 | const auto thread_quit_pipe_fd = lttng_pipe_get_readfd( |
| 364 | notifiers->quit_pipe); |
| 365 | |
| 366 | DBG("Manage agent application registration."); |
| 367 | |
| 368 | rcu_register_thread(); |
| 369 | rcu_thread_online(); |
| 370 | |
| 371 | /* Agent initialization call MUST be called before starting the thread. */ |
| 372 | LTTNG_ASSERT(the_agent_apps_ht_by_sock); |
| 373 | |
| 374 | /* Create pollset with size 2, quit pipe and registration socket. */ |
| 375 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); |
| 376 | if (ret < 0) { |
| 377 | goto error_poll_create; |
| 378 | } |
| 379 | |
| 380 | ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN); |
| 381 | if (ret < 0) { |
| 382 | goto error_tcp_socket; |
| 383 | } |
| 384 | |
| 385 | reg_sock = init_tcp_socket(); |
| 386 | if (reg_sock) { |
| 387 | uint16_t port; |
| 388 | |
| 389 | ret = lttcomm_sock_get_port(reg_sock, &port); |
| 390 | LTTNG_ASSERT(ret == 0); |
| 391 | |
| 392 | ret = write_agent_port(port); |
| 393 | if (ret) { |
| 394 | ERR("Failed to create agent port file: agent tracing will be unavailable"); |
| 395 | /* Don't prevent the launch of the sessiond on error. */ |
| 396 | mark_thread_as_ready(notifiers); |
| 397 | goto error; |
| 398 | } |
| 399 | } else { |
| 400 | /* Don't prevent the launch of the sessiond on error. */ |
| 401 | mark_thread_as_ready(notifiers); |
| 402 | goto error_tcp_socket; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Signal that the agent thread is ready. The command thread |
| 407 | * may start to query whether or not agent tracing is enabled. |
| 408 | */ |
| 409 | uatomic_set(&agent_tracing_enabled, 1); |
| 410 | mark_thread_as_ready(notifiers); |
| 411 | |
| 412 | /* Add TCP socket to the poll set. */ |
| 413 | ret = lttng_poll_add(&events, reg_sock->fd, LPOLLIN | LPOLLRDHUP); |
| 414 | if (ret < 0) { |
| 415 | goto error; |
| 416 | } |
| 417 | |
| 418 | while (1) { |
| 419 | DBG3("Manage agent polling"); |
| 420 | |
| 421 | /* Inifinite blocking call, waiting for transmission */ |
| 422 | restart: |
| 423 | ret = lttng_poll_wait(&events, -1); |
| 424 | DBG3("Manage agent return from poll on %d fds", |
| 425 | LTTNG_POLL_GETNB(&events)); |
| 426 | if (ret < 0) { |
| 427 | /* |
| 428 | * Restart interrupted system call. |
| 429 | */ |
| 430 | if (errno == EINTR) { |
| 431 | goto restart; |
| 432 | } |
| 433 | goto error; |
| 434 | } |
| 435 | nb_fd = ret; |
| 436 | DBG3("%d fd ready", nb_fd); |
| 437 | |
| 438 | for (i = 0; i < nb_fd; i++) { |
| 439 | /* Fetch once the poll data */ |
| 440 | const auto revents = LTTNG_POLL_GETEV(&events, i); |
| 441 | const auto pollfd = LTTNG_POLL_GETFD(&events, i); |
| 442 | |
| 443 | /* Activity on thread quit pipe, exiting. */ |
| 444 | if (pollfd == thread_quit_pipe_fd) { |
| 445 | DBG("Activity on thread quit pipe"); |
| 446 | goto exit; |
| 447 | } |
| 448 | |
| 449 | /* Activity on the registration socket. */ |
| 450 | if (revents & LPOLLIN) { |
| 451 | struct agent_app_id new_app_id; |
| 452 | struct agent_app *new_app = NULL; |
| 453 | struct lttcomm_sock *new_app_socket; |
| 454 | int new_app_socket_fd; |
| 455 | |
| 456 | LTTNG_ASSERT(pollfd == reg_sock->fd); |
| 457 | |
| 458 | ret = accept_agent_connection( |
| 459 | reg_sock, &new_app_id, &new_app_socket); |
| 460 | if (ret < 0) { |
| 461 | /* Errors are already logged. */ |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * new_app_socket's ownership has been |
| 467 | * transferred to the new agent app. |
| 468 | */ |
| 469 | new_app = agent_create_app(new_app_id.pid, |
| 470 | new_app_id.domain, |
| 471 | new_app_socket); |
| 472 | if (!new_app) { |
| 473 | new_app_socket->ops->close( |
| 474 | new_app_socket); |
| 475 | continue; |
| 476 | } |
| 477 | new_app_socket_fd = new_app_socket->fd; |
| 478 | new_app_socket = NULL; |
| 479 | |
| 480 | /* |
| 481 | * Since this is a command socket (write then |
| 482 | * read), only add poll error event to only |
| 483 | * detect shutdown. |
| 484 | */ |
| 485 | ret = lttng_poll_add(&events, new_app_socket_fd, LPOLLRDHUP); |
| 486 | if (ret < 0) { |
| 487 | agent_destroy_app(new_app); |
| 488 | continue; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Prevent sessions from being modified while |
| 493 | * the agent application's configuration is |
| 494 | * updated. |
| 495 | */ |
| 496 | session_lock_list(); |
| 497 | |
| 498 | /* |
| 499 | * Update the newly registered applications's |
| 500 | * configuration. |
| 501 | */ |
| 502 | update_agent_app(new_app); |
| 503 | |
| 504 | ret = agent_send_registration_done(new_app); |
| 505 | if (ret < 0) { |
| 506 | agent_destroy_app(new_app); |
| 507 | /* Removing from the poll set. */ |
| 508 | ret = lttng_poll_del(&events, |
| 509 | new_app_socket_fd); |
| 510 | if (ret < 0) { |
| 511 | session_unlock_list(); |
| 512 | goto error; |
| 513 | } |
| 514 | continue; |
| 515 | } |
| 516 | |
| 517 | /* Publish the new agent app. */ |
| 518 | agent_add_app(new_app); |
| 519 | |
| 520 | session_unlock_list(); |
| 521 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 522 | /* Removing from the poll set */ |
| 523 | ret = lttng_poll_del(&events, pollfd); |
| 524 | if (ret < 0) { |
| 525 | goto error; |
| 526 | } |
| 527 | agent_destroy_app_by_sock(pollfd); |
| 528 | } else { |
| 529 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 530 | goto error; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | exit: |
| 536 | /* Whatever happens, try to delete it and exit. */ |
| 537 | (void) lttng_poll_del(&events, reg_sock->fd); |
| 538 | error: |
| 539 | destroy_tcp_socket(reg_sock); |
| 540 | error_tcp_socket: |
| 541 | lttng_poll_clean(&events); |
| 542 | error_poll_create: |
| 543 | uatomic_set(&agent_tracing_enabled, 0); |
| 544 | DBG("Cleaning up and stopping."); |
| 545 | rcu_thread_offline(); |
| 546 | rcu_unregister_thread(); |
| 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | static bool shutdown_agent_management_thread(void *data) |
| 551 | { |
| 552 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
| 553 | const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe); |
| 554 | |
| 555 | return notify_thread_pipe(write_fd) == 1; |
| 556 | } |
| 557 | |
| 558 | static void cleanup_agent_management_thread(void *data) |
| 559 | { |
| 560 | struct thread_notifiers *notifiers = (thread_notifiers *) data; |
| 561 | |
| 562 | lttng_pipe_destroy(notifiers->quit_pipe); |
| 563 | sem_destroy(¬ifiers->ready); |
| 564 | free(notifiers); |
| 565 | } |
| 566 | |
| 567 | bool launch_agent_management_thread(void) |
| 568 | { |
| 569 | struct thread_notifiers *notifiers; |
| 570 | struct lttng_thread *thread; |
| 571 | |
| 572 | notifiers = zmalloc<thread_notifiers>(); |
| 573 | if (!notifiers) { |
| 574 | goto error_alloc; |
| 575 | } |
| 576 | |
| 577 | sem_init(¬ifiers->ready, 0, 0); |
| 578 | notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC); |
| 579 | if (!notifiers->quit_pipe) { |
| 580 | goto error; |
| 581 | } |
| 582 | thread = lttng_thread_create("Agent management", |
| 583 | thread_agent_management, |
| 584 | shutdown_agent_management_thread, |
| 585 | cleanup_agent_management_thread, |
| 586 | notifiers); |
| 587 | if (!thread) { |
| 588 | goto error; |
| 589 | } |
| 590 | wait_until_thread_is_ready(notifiers); |
| 591 | lttng_thread_put(thread); |
| 592 | return true; |
| 593 | error: |
| 594 | cleanup_agent_management_thread(notifiers); |
| 595 | error_alloc: |
| 596 | return false; |
| 597 | } |