2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
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>
21 #include <sys/eventfd.h>
25 #include "notification-thread.h"
26 #include "notification-thread-events.h"
27 #include "notification-thread-commands.h"
28 #include "lttng-sessiond.h"
29 #include "health-sessiond.h"
31 #include "testpoint.h"
34 #include <common/kernel-ctl/kernel-ctl.h>
37 #include <urcu/list.h>
38 #include <urcu/rculfhash.h>
41 int notifier_consumption_paused
;
43 * Destroy the thread data previously created by the init function.
45 void notification_thread_handle_destroy(
46 struct notification_thread_handle
*handle
)
54 assert(cds_list_empty(&handle
->cmd_queue
.list
));
55 pthread_mutex_destroy(&handle
->cmd_queue
.lock
);
56 sem_destroy(&handle
->ready
);
58 if (handle
->cmd_queue
.event_fd
>= 0) {
59 ret
= close(handle
->cmd_queue
.event_fd
);
61 PERROR("Failed to close notification command queue event fd");
64 if (handle
->channel_monitoring_pipes
.ust32_consumer
>= 0) {
65 ret
= close(handle
->channel_monitoring_pipes
.ust32_consumer
);
67 PERROR("close 32-bit consumer channel monitoring pipe");
70 if (handle
->channel_monitoring_pipes
.ust64_consumer
>= 0) {
71 ret
= close(handle
->channel_monitoring_pipes
.ust64_consumer
);
73 PERROR("close 64-bit consumer channel monitoring pipe");
76 if (handle
->channel_monitoring_pipes
.kernel_consumer
>= 0) {
77 ret
= close(handle
->channel_monitoring_pipes
.kernel_consumer
);
79 PERROR("close kernel consumer channel monitoring pipe");
87 struct notification_thread_handle
*notification_thread_handle_create(
88 struct lttng_pipe
*ust32_channel_monitor_pipe
,
89 struct lttng_pipe
*ust64_channel_monitor_pipe
,
90 struct lttng_pipe
*kernel_channel_monitor_pipe
)
93 struct notification_thread_handle
*handle
;
96 handle
= zmalloc(sizeof(*handle
));
101 sem_init(&handle
->ready
, 0, 0);
103 event_fd
= eventfd(0, EFD_CLOEXEC
);
105 PERROR("event_fd creation");
109 handle
->cmd_queue
.event_fd
= event_fd
;
111 CDS_INIT_LIST_HEAD(&handle
->cmd_queue
.list
);
112 ret
= pthread_mutex_init(&handle
->cmd_queue
.lock
, NULL
);
117 if (ust32_channel_monitor_pipe
) {
118 handle
->channel_monitoring_pipes
.ust32_consumer
=
119 lttng_pipe_release_readfd(
120 ust32_channel_monitor_pipe
);
121 if (handle
->channel_monitoring_pipes
.ust32_consumer
< 0) {
125 handle
->channel_monitoring_pipes
.ust32_consumer
= -1;
127 if (ust64_channel_monitor_pipe
) {
128 handle
->channel_monitoring_pipes
.ust64_consumer
=
129 lttng_pipe_release_readfd(
130 ust64_channel_monitor_pipe
);
131 if (handle
->channel_monitoring_pipes
.ust64_consumer
< 0) {
135 handle
->channel_monitoring_pipes
.ust64_consumer
= -1;
137 if (kernel_channel_monitor_pipe
) {
138 handle
->channel_monitoring_pipes
.kernel_consumer
=
139 lttng_pipe_release_readfd(
140 kernel_channel_monitor_pipe
);
141 if (handle
->channel_monitoring_pipes
.kernel_consumer
< 0) {
145 handle
->channel_monitoring_pipes
.kernel_consumer
= -1;
151 notification_thread_handle_destroy(handle
);
156 char *get_notification_channel_sock_path(void)
159 bool is_root
= !getuid();
162 sock_path
= zmalloc(LTTNG_PATH_MAX
);
168 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
169 DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK
);
174 const char *home_path
= utils_get_home_dir();
177 ERR("Can't get HOME directory for socket creation");
181 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
182 DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK
,
196 void notification_channel_socket_destroy(int fd
)
199 char *sock_path
= get_notification_channel_sock_path();
201 DBG("Destroying notification channel socket");
204 ret
= unlink(sock_path
);
207 PERROR("unlink notification channel socket");
213 PERROR("close notification channel socket");
218 int notification_channel_socket_create(void)
221 char *sock_path
= get_notification_channel_sock_path();
223 DBG("Creating notification channel UNIX socket at %s",
226 ret
= lttcomm_create_unix_sock(sock_path
);
228 ERR("Failed to create notification socket");
233 ret
= chmod(sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
235 ERR("Set file permissions failed: %s", sock_path
);
236 PERROR("chmod notification channel socket");
243 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
,
246 /* Default to root group. */
250 ret
= chown(sock_path
, 0, gid
);
252 ERR("Failed to set the notification channel socket's group");
258 DBG("Notification channel UNIX socket created (fd = %i)",
263 if (fd
>= 0 && close(fd
) < 0) {
264 PERROR("close notification channel socket");
271 int init_poll_set(struct lttng_poll_event
*poll_set
,
272 struct notification_thread_handle
*handle
,
273 int notification_channel_socket
)
278 * Create pollset with size 5:
279 * - notification channel socket (listen for new connections),
280 * - command queue event fd (internal sessiond commands),
281 * - consumerd (32-bit user space) channel monitor pipe,
282 * - consumerd (64-bit user space) channel monitor pipe,
283 * - consumerd (kernel) channel monitor pipe.
285 ret
= lttng_poll_create(poll_set
, 5, LTTNG_CLOEXEC
);
290 ret
= lttng_poll_add(poll_set
, notification_channel_socket
,
291 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
293 ERR("Failed to add notification channel socket to pollset");
296 ret
= lttng_poll_add(poll_set
, handle
->cmd_queue
.event_fd
,
299 ERR("Failed to add notification command queue event fd to pollset");
302 ret
= lttng_poll_add(poll_set
,
303 handle
->channel_monitoring_pipes
.ust32_consumer
,
306 ERR("Failed to add ust-32 channel monitoring pipe fd to pollset");
309 ret
= lttng_poll_add(poll_set
,
310 handle
->channel_monitoring_pipes
.ust64_consumer
,
313 ERR("Failed to add ust-64 channel monitoring pipe fd to pollset");
316 if (handle
->channel_monitoring_pipes
.kernel_consumer
< 0) {
319 ret
= lttng_poll_add(poll_set
,
320 handle
->channel_monitoring_pipes
.kernel_consumer
,
323 ERR("Failed to add kernel channel monitoring pipe fd to pollset");
329 lttng_poll_clean(poll_set
);
334 void fini_thread_state(struct notification_thread_state
*state
)
338 if (state
->client_socket_ht
) {
339 ret
= handle_notification_thread_client_disconnect_all(state
);
341 ret
= cds_lfht_destroy(state
->client_socket_ht
, NULL
);
344 if (state
->client_id_ht
) {
345 ret
= cds_lfht_destroy(state
->client_id_ht
, NULL
);
348 if (state
->triggers_ht
) {
349 ret
= handle_notification_thread_trigger_unregister_all(state
);
351 ret
= cds_lfht_destroy(state
->triggers_ht
, NULL
);
354 if (state
->channel_triggers_ht
) {
355 ret
= cds_lfht_destroy(state
->channel_triggers_ht
, NULL
);
358 if (state
->channel_state_ht
) {
359 ret
= cds_lfht_destroy(state
->channel_state_ht
, NULL
);
362 if (state
->notification_trigger_clients_ht
) {
363 ret
= cds_lfht_destroy(state
->notification_trigger_clients_ht
,
367 if (state
->channels_ht
) {
368 ret
= cds_lfht_destroy(state
->channels_ht
, NULL
);
371 if (state
->sessions_ht
) {
372 ret
= cds_lfht_destroy(state
->sessions_ht
, NULL
);
375 if (state
->triggers_by_name_uid_ht
) {
376 ret
= cds_lfht_destroy(state
->triggers_by_name_uid_ht
, NULL
);
379 if (state
->trigger_tokens_ht
) {
380 ret
= cds_lfht_destroy(state
->trigger_tokens_ht
, NULL
);
384 * Must be destroyed after all channels have been destroyed.
385 * See comment in struct lttng_session_trigger_list.
387 if (state
->session_triggers_ht
) {
388 ret
= cds_lfht_destroy(state
->session_triggers_ht
, NULL
);
391 if (state
->notification_channel_socket
>= 0) {
392 notification_channel_socket_destroy(
393 state
->notification_channel_socket
);
396 assert(cds_list_empty(&state
->tracer_event_sources_list
));
398 if (state
->executor
) {
399 action_executor_destroy(state
->executor
);
401 lttng_poll_clean(&state
->events
);
405 void mark_thread_as_ready(struct notification_thread_handle
*handle
)
407 DBG("Marking notification thread as ready");
408 sem_post(&handle
->ready
);
412 void wait_until_thread_is_ready(struct notification_thread_handle
*handle
)
414 DBG("Waiting for notification thread to be ready");
415 sem_wait(&handle
->ready
);
416 DBG("Notification thread is ready");
420 int init_thread_state(struct notification_thread_handle
*handle
,
421 struct notification_thread_state
*state
)
425 memset(state
, 0, sizeof(*state
));
426 state
->notification_channel_socket
= -1;
427 state
->trigger_id
.next_tracer_token
= 1;
428 lttng_poll_init(&state
->events
);
430 ret
= notification_channel_socket_create();
434 state
->notification_channel_socket
= ret
;
436 ret
= init_poll_set(&state
->events
, handle
,
437 state
->notification_channel_socket
);
442 DBG("Listening on notification channel socket");
443 ret
= lttcomm_listen_unix_sock(state
->notification_channel_socket
);
445 ERR("Listen failed on notification channel socket");
449 state
->client_socket_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
450 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
451 if (!state
->client_socket_ht
) {
455 state
->client_id_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
456 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
457 if (!state
->client_id_ht
) {
461 state
->channel_triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
462 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
463 if (!state
->channel_triggers_ht
) {
467 state
->session_triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
468 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
469 if (!state
->session_triggers_ht
) {
473 state
->channel_state_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
474 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
475 if (!state
->channel_state_ht
) {
479 state
->notification_trigger_clients_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
480 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
481 if (!state
->notification_trigger_clients_ht
) {
485 state
->channels_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
486 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
487 if (!state
->channels_ht
) {
490 state
->sessions_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
491 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
492 if (!state
->sessions_ht
) {
495 state
->triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
496 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
497 if (!state
->triggers_ht
) {
500 state
->triggers_by_name_uid_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
501 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
502 if (!state
->triggers_by_name_uid_ht
) {
506 state
->trigger_tokens_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
507 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
508 if (!state
->trigger_tokens_ht
) {
512 CDS_INIT_LIST_HEAD(&state
->tracer_event_sources_list
);
514 state
->executor
= action_executor_create(handle
);
515 if (!state
->executor
) {
519 state
->restart_poll
= false;
521 mark_thread_as_ready(handle
);
525 fini_thread_state(state
);
530 int handle_channel_monitoring_pipe(int fd
, uint32_t revents
,
531 struct notification_thread_handle
*handle
,
532 struct notification_thread_state
*state
)
535 enum lttng_domain_type domain
;
537 if (fd
== handle
->channel_monitoring_pipes
.ust32_consumer
||
538 fd
== handle
->channel_monitoring_pipes
.ust64_consumer
) {
539 domain
= LTTNG_DOMAIN_UST
;
540 } else if (fd
== handle
->channel_monitoring_pipes
.kernel_consumer
) {
541 domain
= LTTNG_DOMAIN_KERNEL
;
546 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
547 ret
= lttng_poll_del(&state
->events
, fd
);
549 ERR("Failed to remove consumer monitoring pipe from poll set");
554 ret
= handle_notification_thread_channel_sample(
557 ERR("Consumer sample handling error occurred");
565 static int handle_event_notification_pipe(int event_source_fd
,
566 enum lttng_domain_type domain
,
568 struct notification_thread_state
*state
)
572 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
573 ret
= handle_notification_thread_tracer_event_source_died(
574 state
, event_source_fd
);
576 ERR("Failed to remove event notification pipe from poll set: fd = %d",
582 if (testpoint(sessiond_handle_notifier_event_pipe
)) {
587 if (caa_unlikely(notifier_consumption_paused
)) {
588 DBG("Event notifier notification consumption paused, sleeping...");
593 ret
= handle_notification_thread_event_notification(
594 state
, event_source_fd
, domain
);
596 ERR("Event notification handling error occurred for fd: %d",
607 * Return the event source domain type via parameter.
609 static bool fd_is_event_notification_source(const struct notification_thread_state
*state
,
611 enum lttng_domain_type
*domain
)
613 struct notification_event_tracer_event_source_element
*source_element
;
617 cds_list_for_each_entry(source_element
,
618 &state
->tracer_event_sources_list
, node
) {
619 if (source_element
->fd
!= fd
) {
623 *domain
= source_element
->domain
;
631 * This thread services notification channel clients and commands received
632 * from various lttng-sessiond components over a command queue.
635 void *thread_notification(void *data
)
638 struct notification_thread_handle
*handle
= data
;
639 struct notification_thread_state state
;
640 enum lttng_domain_type domain
;
642 DBG("Started notification thread");
644 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_NOTIFICATION
);
645 rcu_register_thread();
649 ERR("Invalid thread context provided");
653 health_code_update();
655 ret
= init_thread_state(handle
, &state
);
660 if (testpoint(sessiond_thread_notification
)) {
668 DBG("Entering poll wait");
669 ret
= lttng_poll_wait(&state
.events
, -1);
670 DBG("Poll wait returned (%i)", ret
);
674 * Restart interrupted system call.
676 if (errno
== EINTR
) {
679 ERR("Error encountered during lttng_poll_wait (%i)", ret
);
684 * Reset restart_poll flag so that calls below might turn it
687 state
.restart_poll
= false;
690 for (i
= 0; i
< fd_count
; i
++) {
691 int fd
= LTTNG_POLL_GETFD(&state
.events
, i
);
692 uint32_t revents
= LTTNG_POLL_GETEV(&state
.events
, i
);
694 DBG("Handling fd (%i) activity (%u)", fd
, revents
);
696 if (fd
== state
.notification_channel_socket
) {
697 if (revents
& LPOLLIN
) {
698 ret
= handle_notification_thread_client_connect(
704 (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
705 ERR("Notification socket poll error");
708 ERR("Unexpected poll events %u for notification socket %i", revents
, fd
);
711 } else if (fd
== handle
->cmd_queue
.event_fd
) {
712 ret
= handle_notification_thread_command(handle
,
715 DBG("Error encountered while servicing command queue");
717 } else if (ret
> 0) {
720 } else if (fd
== handle
->channel_monitoring_pipes
.ust32_consumer
||
721 fd
== handle
->channel_monitoring_pipes
.ust64_consumer
||
722 fd
== handle
->channel_monitoring_pipes
.kernel_consumer
) {
723 ret
= handle_channel_monitoring_pipe(fd
,
724 revents
, handle
, &state
);
728 } else if (fd_is_event_notification_source(&state
, fd
, &domain
)) {
729 ret
= handle_event_notification_pipe(fd
, domain
, revents
, &state
);
734 /* Activity on a client's socket. */
735 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
737 * It doesn't matter if a command was
738 * pending on the client socket at this
739 * point since it now has no way to
740 * receive the notifications to which
741 * it was subscribing or unsubscribing.
743 ret
= handle_notification_thread_client_disconnect(
749 if (revents
& LPOLLIN
) {
750 ret
= handle_notification_thread_client_in(
757 if (revents
& LPOLLOUT
) {
758 ret
= handle_notification_thread_client_out(
768 * Calls above might have changed the state of the
769 * FDs in `state.events`. Call _poll_wait() again to
770 * ensure we have a consistent state.
772 if (state
.restart_poll
) {
779 fini_thread_state(&state
);
781 rcu_thread_offline();
782 rcu_unregister_thread();
783 health_unregister(the_health_sessiond
);
788 bool shutdown_notification_thread(void *thread_data
)
790 struct notification_thread_handle
*handle
= thread_data
;
792 notification_thread_command_quit(handle
);
796 struct lttng_thread
*launch_notification_thread(
797 struct notification_thread_handle
*handle
)
799 struct lttng_thread
*thread
;
801 thread
= lttng_thread_create("Notification",
803 shutdown_notification_thread
,
811 * Wait for the thread to be marked as "ready" before returning
812 * as other subsystems depend on the notification subsystem
813 * (e.g. rotation thread).
815 wait_until_thread_is_ready(handle
);