| 1 | /* |
| 2 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | #include <lttng/trigger/trigger.h> |
| 10 | #include <lttng/notification/channel-internal.h> |
| 11 | #include <lttng/notification/notification-internal.h> |
| 12 | #include <lttng/condition/condition-internal.h> |
| 13 | #include <lttng/condition/buffer-usage-internal.h> |
| 14 | #include <common/error.h> |
| 15 | #include <common/config/session-config.h> |
| 16 | #include <common/defaults.h> |
| 17 | #include <common/utils.h> |
| 18 | #include <common/align.h> |
| 19 | #include <common/time.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <time.h> |
| 22 | #include <signal.h> |
| 23 | |
| 24 | #include "notification-thread.h" |
| 25 | #include "notification-thread-events.h" |
| 26 | #include "notification-thread-commands.h" |
| 27 | #include "lttng-sessiond.h" |
| 28 | #include "health-sessiond.h" |
| 29 | #include "thread.h" |
| 30 | #include "testpoint.h" |
| 31 | |
| 32 | #include "kernel.h" |
| 33 | #include <common/kernel-ctl/kernel-ctl.h> |
| 34 | |
| 35 | #include <urcu.h> |
| 36 | #include <urcu/list.h> |
| 37 | #include <urcu/rculfhash.h> |
| 38 | |
| 39 | /* |
| 40 | * Flag used to temporarily pause data consumption from testpoints. |
| 41 | * |
| 42 | * This variable is dlsym-ed from a test, so needs to be exported. |
| 43 | */ |
| 44 | LTTNG_EXPORT int notifier_consumption_paused; |
| 45 | |
| 46 | /* |
| 47 | * Destroy the thread data previously created by the init function. |
| 48 | */ |
| 49 | void notification_thread_handle_destroy( |
| 50 | struct notification_thread_handle *handle) |
| 51 | { |
| 52 | int ret; |
| 53 | |
| 54 | if (!handle) { |
| 55 | goto end; |
| 56 | } |
| 57 | |
| 58 | LTTNG_ASSERT(cds_list_empty(&handle->cmd_queue.list)); |
| 59 | pthread_mutex_destroy(&handle->cmd_queue.lock); |
| 60 | sem_destroy(&handle->ready); |
| 61 | |
| 62 | if (handle->cmd_queue.event_pipe) { |
| 63 | lttng_pipe_destroy(handle->cmd_queue.event_pipe); |
| 64 | } |
| 65 | if (handle->channel_monitoring_pipes.ust32_consumer >= 0) { |
| 66 | ret = close(handle->channel_monitoring_pipes.ust32_consumer); |
| 67 | if (ret) { |
| 68 | PERROR("close 32-bit consumer channel monitoring pipe"); |
| 69 | } |
| 70 | } |
| 71 | if (handle->channel_monitoring_pipes.ust64_consumer >= 0) { |
| 72 | ret = close(handle->channel_monitoring_pipes.ust64_consumer); |
| 73 | if (ret) { |
| 74 | PERROR("close 64-bit consumer channel monitoring pipe"); |
| 75 | } |
| 76 | } |
| 77 | if (handle->channel_monitoring_pipes.kernel_consumer >= 0) { |
| 78 | ret = close(handle->channel_monitoring_pipes.kernel_consumer); |
| 79 | if (ret) { |
| 80 | PERROR("close kernel consumer channel monitoring pipe"); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | end: |
| 85 | free(handle); |
| 86 | } |
| 87 | |
| 88 | struct notification_thread_handle *notification_thread_handle_create( |
| 89 | struct lttng_pipe *ust32_channel_monitor_pipe, |
| 90 | struct lttng_pipe *ust64_channel_monitor_pipe, |
| 91 | struct lttng_pipe *kernel_channel_monitor_pipe) |
| 92 | { |
| 93 | int ret; |
| 94 | struct notification_thread_handle *handle; |
| 95 | struct lttng_pipe *event_pipe = NULL; |
| 96 | |
| 97 | handle = (notification_thread_handle *) zmalloc(sizeof(*handle)); |
| 98 | if (!handle) { |
| 99 | goto end; |
| 100 | } |
| 101 | |
| 102 | sem_init(&handle->ready, 0, 0); |
| 103 | |
| 104 | event_pipe = lttng_pipe_open(FD_CLOEXEC); |
| 105 | if (!event_pipe) { |
| 106 | ERR("event_pipe creation"); |
| 107 | goto error; |
| 108 | } |
| 109 | |
| 110 | handle->cmd_queue.event_pipe = event_pipe; |
| 111 | event_pipe = NULL; |
| 112 | |
| 113 | CDS_INIT_LIST_HEAD(&handle->cmd_queue.list); |
| 114 | ret = pthread_mutex_init(&handle->cmd_queue.lock, NULL); |
| 115 | if (ret) { |
| 116 | goto error; |
| 117 | } |
| 118 | |
| 119 | if (ust32_channel_monitor_pipe) { |
| 120 | handle->channel_monitoring_pipes.ust32_consumer = |
| 121 | lttng_pipe_release_readfd( |
| 122 | ust32_channel_monitor_pipe); |
| 123 | if (handle->channel_monitoring_pipes.ust32_consumer < 0) { |
| 124 | goto error; |
| 125 | } |
| 126 | } else { |
| 127 | handle->channel_monitoring_pipes.ust32_consumer = -1; |
| 128 | } |
| 129 | if (ust64_channel_monitor_pipe) { |
| 130 | handle->channel_monitoring_pipes.ust64_consumer = |
| 131 | lttng_pipe_release_readfd( |
| 132 | ust64_channel_monitor_pipe); |
| 133 | if (handle->channel_monitoring_pipes.ust64_consumer < 0) { |
| 134 | goto error; |
| 135 | } |
| 136 | } else { |
| 137 | handle->channel_monitoring_pipes.ust64_consumer = -1; |
| 138 | } |
| 139 | if (kernel_channel_monitor_pipe) { |
| 140 | handle->channel_monitoring_pipes.kernel_consumer = |
| 141 | lttng_pipe_release_readfd( |
| 142 | kernel_channel_monitor_pipe); |
| 143 | if (handle->channel_monitoring_pipes.kernel_consumer < 0) { |
| 144 | goto error; |
| 145 | } |
| 146 | } else { |
| 147 | handle->channel_monitoring_pipes.kernel_consumer = -1; |
| 148 | } |
| 149 | |
| 150 | end: |
| 151 | return handle; |
| 152 | error: |
| 153 | lttng_pipe_destroy(event_pipe); |
| 154 | notification_thread_handle_destroy(handle); |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | static |
| 159 | char *get_notification_channel_sock_path(void) |
| 160 | { |
| 161 | int ret; |
| 162 | bool is_root = !getuid(); |
| 163 | char *sock_path; |
| 164 | |
| 165 | sock_path = (char *) zmalloc(LTTNG_PATH_MAX); |
| 166 | if (!sock_path) { |
| 167 | goto error; |
| 168 | } |
| 169 | |
| 170 | if (is_root) { |
| 171 | ret = snprintf(sock_path, LTTNG_PATH_MAX, |
| 172 | DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK); |
| 173 | if (ret < 0) { |
| 174 | goto error; |
| 175 | } |
| 176 | } else { |
| 177 | const char *home_path = utils_get_home_dir(); |
| 178 | |
| 179 | if (!home_path) { |
| 180 | ERR("Can't get HOME directory for socket creation"); |
| 181 | goto error; |
| 182 | } |
| 183 | |
| 184 | ret = snprintf(sock_path, LTTNG_PATH_MAX, |
| 185 | DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK, |
| 186 | home_path); |
| 187 | if (ret < 0) { |
| 188 | goto error; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return sock_path; |
| 193 | error: |
| 194 | free(sock_path); |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | static |
| 199 | void notification_channel_socket_destroy(int fd) |
| 200 | { |
| 201 | int ret; |
| 202 | char *sock_path = get_notification_channel_sock_path(); |
| 203 | |
| 204 | DBG("Destroying notification channel socket"); |
| 205 | |
| 206 | if (sock_path) { |
| 207 | ret = unlink(sock_path); |
| 208 | free(sock_path); |
| 209 | if (ret < 0) { |
| 210 | PERROR("unlink notification channel socket"); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | ret = close(fd); |
| 215 | if (ret) { |
| 216 | PERROR("close notification channel socket"); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static |
| 221 | int notification_channel_socket_create(void) |
| 222 | { |
| 223 | int fd = -1, ret; |
| 224 | char *sock_path = get_notification_channel_sock_path(); |
| 225 | |
| 226 | DBG("Creating notification channel UNIX socket at %s", |
| 227 | sock_path); |
| 228 | |
| 229 | ret = lttcomm_create_unix_sock(sock_path); |
| 230 | if (ret < 0) { |
| 231 | ERR("Failed to create notification socket"); |
| 232 | goto error; |
| 233 | } |
| 234 | fd = ret; |
| 235 | |
| 236 | ret = chmod(sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 237 | if (ret < 0) { |
| 238 | ERR("Set file permissions failed: %s", sock_path); |
| 239 | PERROR("chmod notification channel socket"); |
| 240 | goto error; |
| 241 | } |
| 242 | |
| 243 | if (getuid() == 0) { |
| 244 | gid_t gid; |
| 245 | |
| 246 | ret = utils_get_group_id(the_config.tracing_group_name.value, |
| 247 | true, &gid); |
| 248 | if (ret) { |
| 249 | /* Default to root group. */ |
| 250 | gid = 0; |
| 251 | } |
| 252 | |
| 253 | ret = chown(sock_path, 0, gid); |
| 254 | if (ret) { |
| 255 | ERR("Failed to set the notification channel socket's group"); |
| 256 | ret = -1; |
| 257 | goto error; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | DBG("Notification channel UNIX socket created (fd = %i)", |
| 262 | fd); |
| 263 | free(sock_path); |
| 264 | return fd; |
| 265 | error: |
| 266 | if (fd >= 0 && close(fd) < 0) { |
| 267 | PERROR("close notification channel socket"); |
| 268 | } |
| 269 | free(sock_path); |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | static |
| 274 | int init_poll_set(struct lttng_poll_event *poll_set, |
| 275 | struct notification_thread_handle *handle, |
| 276 | int notification_channel_socket) |
| 277 | { |
| 278 | int ret; |
| 279 | |
| 280 | /* |
| 281 | * Create pollset with size 5: |
| 282 | * - notification channel socket (listen for new connections), |
| 283 | * - command queue event fd (internal sessiond commands), |
| 284 | * - consumerd (32-bit user space) channel monitor pipe, |
| 285 | * - consumerd (64-bit user space) channel monitor pipe, |
| 286 | * - consumerd (kernel) channel monitor pipe. |
| 287 | */ |
| 288 | ret = lttng_poll_create(poll_set, 5, LTTNG_CLOEXEC); |
| 289 | if (ret < 0) { |
| 290 | goto end; |
| 291 | } |
| 292 | |
| 293 | ret = lttng_poll_add(poll_set, notification_channel_socket, |
| 294 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); |
| 295 | if (ret < 0) { |
| 296 | ERR("Failed to add notification channel socket to pollset"); |
| 297 | goto error; |
| 298 | } |
| 299 | ret = lttng_poll_add(poll_set, lttng_pipe_get_readfd(handle->cmd_queue.event_pipe), |
| 300 | LPOLLIN | LPOLLERR); |
| 301 | if (ret < 0) { |
| 302 | ERR("Failed to add notification command queue event fd to pollset"); |
| 303 | goto error; |
| 304 | } |
| 305 | ret = lttng_poll_add(poll_set, |
| 306 | handle->channel_monitoring_pipes.ust32_consumer, |
| 307 | LPOLLIN | LPOLLERR); |
| 308 | if (ret < 0) { |
| 309 | ERR("Failed to add ust-32 channel monitoring pipe fd to pollset"); |
| 310 | goto error; |
| 311 | } |
| 312 | ret = lttng_poll_add(poll_set, |
| 313 | handle->channel_monitoring_pipes.ust64_consumer, |
| 314 | LPOLLIN | LPOLLERR); |
| 315 | if (ret < 0) { |
| 316 | ERR("Failed to add ust-64 channel monitoring pipe fd to pollset"); |
| 317 | goto error; |
| 318 | } |
| 319 | if (handle->channel_monitoring_pipes.kernel_consumer < 0) { |
| 320 | goto end; |
| 321 | } |
| 322 | ret = lttng_poll_add(poll_set, |
| 323 | handle->channel_monitoring_pipes.kernel_consumer, |
| 324 | LPOLLIN | LPOLLERR); |
| 325 | if (ret < 0) { |
| 326 | ERR("Failed to add kernel channel monitoring pipe fd to pollset"); |
| 327 | goto error; |
| 328 | } |
| 329 | end: |
| 330 | return ret; |
| 331 | error: |
| 332 | lttng_poll_clean(poll_set); |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static |
| 337 | void fini_thread_state(struct notification_thread_state *state) |
| 338 | { |
| 339 | int ret; |
| 340 | |
| 341 | if (state->client_socket_ht) { |
| 342 | ret = handle_notification_thread_client_disconnect_all(state); |
| 343 | LTTNG_ASSERT(!ret); |
| 344 | ret = cds_lfht_destroy(state->client_socket_ht, NULL); |
| 345 | LTTNG_ASSERT(!ret); |
| 346 | } |
| 347 | if (state->client_id_ht) { |
| 348 | ret = cds_lfht_destroy(state->client_id_ht, NULL); |
| 349 | LTTNG_ASSERT(!ret); |
| 350 | } |
| 351 | if (state->triggers_ht) { |
| 352 | ret = handle_notification_thread_trigger_unregister_all(state); |
| 353 | LTTNG_ASSERT(!ret); |
| 354 | ret = cds_lfht_destroy(state->triggers_ht, NULL); |
| 355 | LTTNG_ASSERT(!ret); |
| 356 | } |
| 357 | if (state->channel_triggers_ht) { |
| 358 | ret = cds_lfht_destroy(state->channel_triggers_ht, NULL); |
| 359 | LTTNG_ASSERT(!ret); |
| 360 | } |
| 361 | if (state->channel_state_ht) { |
| 362 | ret = cds_lfht_destroy(state->channel_state_ht, NULL); |
| 363 | LTTNG_ASSERT(!ret); |
| 364 | } |
| 365 | if (state->notification_trigger_clients_ht) { |
| 366 | ret = cds_lfht_destroy(state->notification_trigger_clients_ht, |
| 367 | NULL); |
| 368 | LTTNG_ASSERT(!ret); |
| 369 | } |
| 370 | if (state->channels_ht) { |
| 371 | ret = cds_lfht_destroy(state->channels_ht, NULL); |
| 372 | LTTNG_ASSERT(!ret); |
| 373 | } |
| 374 | if (state->sessions_ht) { |
| 375 | ret = cds_lfht_destroy(state->sessions_ht, NULL); |
| 376 | LTTNG_ASSERT(!ret); |
| 377 | } |
| 378 | if (state->triggers_by_name_uid_ht) { |
| 379 | ret = cds_lfht_destroy(state->triggers_by_name_uid_ht, NULL); |
| 380 | LTTNG_ASSERT(!ret); |
| 381 | } |
| 382 | if (state->trigger_tokens_ht) { |
| 383 | ret = cds_lfht_destroy(state->trigger_tokens_ht, NULL); |
| 384 | LTTNG_ASSERT(!ret); |
| 385 | } |
| 386 | /* |
| 387 | * Must be destroyed after all channels have been destroyed. |
| 388 | * See comment in struct lttng_session_trigger_list. |
| 389 | */ |
| 390 | if (state->session_triggers_ht) { |
| 391 | ret = cds_lfht_destroy(state->session_triggers_ht, NULL); |
| 392 | LTTNG_ASSERT(!ret); |
| 393 | } |
| 394 | if (state->notification_channel_socket >= 0) { |
| 395 | notification_channel_socket_destroy( |
| 396 | state->notification_channel_socket); |
| 397 | } |
| 398 | |
| 399 | LTTNG_ASSERT(cds_list_empty(&state->tracer_event_sources_list)); |
| 400 | |
| 401 | if (state->executor) { |
| 402 | action_executor_destroy(state->executor); |
| 403 | } |
| 404 | lttng_poll_clean(&state->events); |
| 405 | } |
| 406 | |
| 407 | static |
| 408 | void mark_thread_as_ready(struct notification_thread_handle *handle) |
| 409 | { |
| 410 | DBG("Marking notification thread as ready"); |
| 411 | sem_post(&handle->ready); |
| 412 | } |
| 413 | |
| 414 | static |
| 415 | void wait_until_thread_is_ready(struct notification_thread_handle *handle) |
| 416 | { |
| 417 | DBG("Waiting for notification thread to be ready"); |
| 418 | sem_wait(&handle->ready); |
| 419 | DBG("Notification thread is ready"); |
| 420 | } |
| 421 | |
| 422 | static |
| 423 | int init_thread_state(struct notification_thread_handle *handle, |
| 424 | struct notification_thread_state *state) |
| 425 | { |
| 426 | int ret; |
| 427 | |
| 428 | memset(state, 0, sizeof(*state)); |
| 429 | state->notification_channel_socket = -1; |
| 430 | state->trigger_id.next_tracer_token = 1; |
| 431 | lttng_poll_init(&state->events); |
| 432 | |
| 433 | ret = notification_channel_socket_create(); |
| 434 | if (ret < 0) { |
| 435 | goto end; |
| 436 | } |
| 437 | state->notification_channel_socket = ret; |
| 438 | |
| 439 | ret = init_poll_set(&state->events, handle, |
| 440 | state->notification_channel_socket); |
| 441 | if (ret) { |
| 442 | goto end; |
| 443 | } |
| 444 | |
| 445 | DBG("Listening on notification channel socket"); |
| 446 | ret = lttcomm_listen_unix_sock(state->notification_channel_socket); |
| 447 | if (ret < 0) { |
| 448 | ERR("Listen failed on notification channel socket"); |
| 449 | goto error; |
| 450 | } |
| 451 | |
| 452 | state->client_socket_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0, |
| 453 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 454 | if (!state->client_socket_ht) { |
| 455 | goto error; |
| 456 | } |
| 457 | |
| 458 | state->client_id_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0, |
| 459 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 460 | if (!state->client_id_ht) { |
| 461 | goto error; |
| 462 | } |
| 463 | |
| 464 | state->channel_triggers_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0, |
| 465 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 466 | if (!state->channel_triggers_ht) { |
| 467 | goto error; |
| 468 | } |
| 469 | |
| 470 | state->session_triggers_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0, |
| 471 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 472 | if (!state->session_triggers_ht) { |
| 473 | goto error; |
| 474 | } |
| 475 | |
| 476 | state->channel_state_ht = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0, |
| 477 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 478 | if (!state->channel_state_ht) { |
| 479 | goto error; |
| 480 | } |
| 481 | |
| 482 | state->notification_trigger_clients_ht = cds_lfht_new(DEFAULT_HT_SIZE, |
| 483 | 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 484 | if (!state->notification_trigger_clients_ht) { |
| 485 | goto error; |
| 486 | } |
| 487 | |
| 488 | state->channels_ht = cds_lfht_new(DEFAULT_HT_SIZE, |
| 489 | 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 490 | if (!state->channels_ht) { |
| 491 | goto error; |
| 492 | } |
| 493 | state->sessions_ht = cds_lfht_new(DEFAULT_HT_SIZE, |
| 494 | 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 495 | if (!state->sessions_ht) { |
| 496 | goto error; |
| 497 | } |
| 498 | state->triggers_ht = cds_lfht_new(DEFAULT_HT_SIZE, |
| 499 | 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 500 | if (!state->triggers_ht) { |
| 501 | goto error; |
| 502 | } |
| 503 | state->triggers_by_name_uid_ht = cds_lfht_new(DEFAULT_HT_SIZE, |
| 504 | 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 505 | if (!state->triggers_by_name_uid_ht) { |
| 506 | goto error; |
| 507 | } |
| 508 | |
| 509 | state->trigger_tokens_ht = cds_lfht_new(DEFAULT_HT_SIZE, |
| 510 | 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); |
| 511 | if (!state->trigger_tokens_ht) { |
| 512 | goto error; |
| 513 | } |
| 514 | |
| 515 | CDS_INIT_LIST_HEAD(&state->tracer_event_sources_list); |
| 516 | |
| 517 | state->executor = action_executor_create(handle); |
| 518 | if (!state->executor) { |
| 519 | goto error; |
| 520 | } |
| 521 | |
| 522 | state->restart_poll = false; |
| 523 | |
| 524 | mark_thread_as_ready(handle); |
| 525 | end: |
| 526 | return 0; |
| 527 | error: |
| 528 | fini_thread_state(state); |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | static |
| 533 | int handle_channel_monitoring_pipe(int fd, uint32_t revents, |
| 534 | struct notification_thread_handle *handle, |
| 535 | struct notification_thread_state *state) |
| 536 | { |
| 537 | int ret = 0; |
| 538 | enum lttng_domain_type domain; |
| 539 | |
| 540 | if (fd == handle->channel_monitoring_pipes.ust32_consumer || |
| 541 | fd == handle->channel_monitoring_pipes.ust64_consumer) { |
| 542 | domain = LTTNG_DOMAIN_UST; |
| 543 | } else if (fd == handle->channel_monitoring_pipes.kernel_consumer) { |
| 544 | domain = LTTNG_DOMAIN_KERNEL; |
| 545 | } else { |
| 546 | abort(); |
| 547 | } |
| 548 | |
| 549 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 550 | ret = lttng_poll_del(&state->events, fd); |
| 551 | if (ret) { |
| 552 | ERR("Failed to remove consumer monitoring pipe from poll set"); |
| 553 | } |
| 554 | goto end; |
| 555 | } |
| 556 | |
| 557 | ret = handle_notification_thread_channel_sample( |
| 558 | state, fd, domain); |
| 559 | if (ret) { |
| 560 | ERR("Consumer sample handling error occurred"); |
| 561 | ret = -1; |
| 562 | goto end; |
| 563 | } |
| 564 | end: |
| 565 | return ret; |
| 566 | } |
| 567 | |
| 568 | static int handle_event_notification_pipe(int event_source_fd, |
| 569 | enum lttng_domain_type domain, |
| 570 | uint32_t revents, |
| 571 | struct notification_thread_state *state) |
| 572 | { |
| 573 | int ret = 0; |
| 574 | |
| 575 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 576 | ret = handle_notification_thread_tracer_event_source_died( |
| 577 | state, event_source_fd); |
| 578 | if (ret) { |
| 579 | ERR("Failed to remove event notification pipe from poll set: fd = %d", |
| 580 | event_source_fd); |
| 581 | } |
| 582 | goto end; |
| 583 | } |
| 584 | |
| 585 | if (testpoint(sessiond_handle_notifier_event_pipe)) { |
| 586 | ret = 0; |
| 587 | goto end; |
| 588 | } |
| 589 | |
| 590 | if (caa_unlikely(notifier_consumption_paused)) { |
| 591 | DBG("Event notifier notification consumption paused, sleeping..."); |
| 592 | sleep(1); |
| 593 | goto end; |
| 594 | } |
| 595 | |
| 596 | ret = handle_notification_thread_event_notification( |
| 597 | state, event_source_fd, domain); |
| 598 | if (ret) { |
| 599 | ERR("Event notification handling error occurred for fd: %d", |
| 600 | event_source_fd); |
| 601 | ret = -1; |
| 602 | goto end; |
| 603 | } |
| 604 | |
| 605 | end: |
| 606 | return ret; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * Return the event source domain type via parameter. |
| 611 | */ |
| 612 | static bool fd_is_event_notification_source(const struct notification_thread_state *state, |
| 613 | int fd, |
| 614 | enum lttng_domain_type *domain) |
| 615 | { |
| 616 | struct notification_event_tracer_event_source_element *source_element; |
| 617 | |
| 618 | LTTNG_ASSERT(domain); |
| 619 | |
| 620 | cds_list_for_each_entry(source_element, |
| 621 | &state->tracer_event_sources_list, node) { |
| 622 | if (source_element->fd != fd) { |
| 623 | continue; |
| 624 | } |
| 625 | |
| 626 | *domain = source_element->domain; |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * This thread services notification channel clients and commands received |
| 635 | * from various lttng-sessiond components over a command queue. |
| 636 | */ |
| 637 | static |
| 638 | void *thread_notification(void *data) |
| 639 | { |
| 640 | int ret; |
| 641 | struct notification_thread_handle *handle = (notification_thread_handle *) data; |
| 642 | struct notification_thread_state state; |
| 643 | enum lttng_domain_type domain; |
| 644 | |
| 645 | DBG("Started notification thread"); |
| 646 | |
| 647 | health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_NOTIFICATION); |
| 648 | rcu_register_thread(); |
| 649 | rcu_thread_online(); |
| 650 | |
| 651 | if (!handle) { |
| 652 | ERR("Invalid thread context provided"); |
| 653 | goto end; |
| 654 | } |
| 655 | |
| 656 | health_code_update(); |
| 657 | |
| 658 | ret = init_thread_state(handle, &state); |
| 659 | if (ret) { |
| 660 | goto end; |
| 661 | } |
| 662 | |
| 663 | if (testpoint(sessiond_thread_notification)) { |
| 664 | goto end; |
| 665 | } |
| 666 | |
| 667 | while (true) { |
| 668 | int fd_count, i; |
| 669 | |
| 670 | health_poll_entry(); |
| 671 | DBG("Entering poll wait"); |
| 672 | ret = lttng_poll_wait(&state.events, -1); |
| 673 | DBG("Poll wait returned (%i)", ret); |
| 674 | health_poll_exit(); |
| 675 | if (ret < 0) { |
| 676 | /* |
| 677 | * Restart interrupted system call. |
| 678 | */ |
| 679 | if (errno == EINTR) { |
| 680 | continue; |
| 681 | } |
| 682 | ERR("Error encountered during lttng_poll_wait (%i)", ret); |
| 683 | goto error; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Reset restart_poll flag so that calls below might turn it |
| 688 | * on. |
| 689 | */ |
| 690 | state.restart_poll = false; |
| 691 | |
| 692 | fd_count = ret; |
| 693 | for (i = 0; i < fd_count; i++) { |
| 694 | int fd = LTTNG_POLL_GETFD(&state.events, i); |
| 695 | uint32_t revents = LTTNG_POLL_GETEV(&state.events, i); |
| 696 | |
| 697 | DBG("Handling fd (%i) activity (%u)", fd, revents); |
| 698 | |
| 699 | if (fd == state.notification_channel_socket) { |
| 700 | if (revents & LPOLLIN) { |
| 701 | ret = handle_notification_thread_client_connect( |
| 702 | &state); |
| 703 | if (ret < 0) { |
| 704 | goto error; |
| 705 | } |
| 706 | } else if (revents & |
| 707 | (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 708 | ERR("Notification socket poll error"); |
| 709 | goto error; |
| 710 | } else { |
| 711 | ERR("Unexpected poll events %u for notification socket %i", revents, fd); |
| 712 | goto error; |
| 713 | } |
| 714 | } else if (fd == lttng_pipe_get_readfd(handle->cmd_queue.event_pipe)) { |
| 715 | ret = handle_notification_thread_command(handle, |
| 716 | &state); |
| 717 | if (ret < 0) { |
| 718 | DBG("Error encountered while servicing command queue"); |
| 719 | goto error; |
| 720 | } else if (ret > 0) { |
| 721 | goto exit; |
| 722 | } |
| 723 | } else if (fd == handle->channel_monitoring_pipes.ust32_consumer || |
| 724 | fd == handle->channel_monitoring_pipes.ust64_consumer || |
| 725 | fd == handle->channel_monitoring_pipes.kernel_consumer) { |
| 726 | ret = handle_channel_monitoring_pipe(fd, |
| 727 | revents, handle, &state); |
| 728 | if (ret) { |
| 729 | goto error; |
| 730 | } |
| 731 | } else if (fd_is_event_notification_source(&state, fd, &domain)) { |
| 732 | ret = handle_event_notification_pipe(fd, domain, revents, &state); |
| 733 | if (ret) { |
| 734 | goto error; |
| 735 | } |
| 736 | } else { |
| 737 | /* Activity on a client's socket. */ |
| 738 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 739 | /* |
| 740 | * It doesn't matter if a command was |
| 741 | * pending on the client socket at this |
| 742 | * point since it now has no way to |
| 743 | * receive the notifications to which |
| 744 | * it was subscribing or unsubscribing. |
| 745 | */ |
| 746 | ret = handle_notification_thread_client_disconnect( |
| 747 | fd, &state); |
| 748 | if (ret) { |
| 749 | goto error; |
| 750 | } |
| 751 | } else { |
| 752 | if (revents & LPOLLIN) { |
| 753 | ret = handle_notification_thread_client_in( |
| 754 | &state, fd); |
| 755 | if (ret) { |
| 756 | goto error; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | if (revents & LPOLLOUT) { |
| 761 | ret = handle_notification_thread_client_out( |
| 762 | &state, fd); |
| 763 | if (ret) { |
| 764 | goto error; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Calls above might have changed the state of the |
| 772 | * FDs in `state.events`. Call _poll_wait() again to |
| 773 | * ensure we have a consistent state. |
| 774 | */ |
| 775 | if (state.restart_poll) { |
| 776 | break; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | exit: |
| 781 | error: |
| 782 | fini_thread_state(&state); |
| 783 | end: |
| 784 | rcu_thread_offline(); |
| 785 | rcu_unregister_thread(); |
| 786 | health_unregister(the_health_sessiond); |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | static |
| 791 | bool shutdown_notification_thread(void *thread_data) |
| 792 | { |
| 793 | struct notification_thread_handle *handle = (notification_thread_handle *) thread_data; |
| 794 | |
| 795 | notification_thread_command_quit(handle); |
| 796 | return true; |
| 797 | } |
| 798 | |
| 799 | struct lttng_thread *launch_notification_thread( |
| 800 | struct notification_thread_handle *handle) |
| 801 | { |
| 802 | struct lttng_thread *thread; |
| 803 | |
| 804 | thread = lttng_thread_create("Notification", |
| 805 | thread_notification, |
| 806 | shutdown_notification_thread, |
| 807 | NULL, |
| 808 | handle); |
| 809 | if (!thread) { |
| 810 | goto error; |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Wait for the thread to be marked as "ready" before returning |
| 815 | * as other subsystems depend on the notification subsystem |
| 816 | * (e.g. rotation thread). |
| 817 | */ |
| 818 | wait_until_thread_is_ready(handle); |
| 819 | return thread; |
| 820 | error: |
| 821 | return NULL; |
| 822 | } |