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>
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"
32 #include <common/kernel-ctl/kernel-ctl.h>
35 #include <urcu/list.h>
36 #include <urcu/rculfhash.h>
39 * Destroy the thread data previously created by the init function.
41 void notification_thread_handle_destroy(
42 struct notification_thread_handle
*handle
)
50 assert(cds_list_empty(&handle
->cmd_queue
.list
));
51 pthread_mutex_destroy(&handle
->cmd_queue
.lock
);
52 sem_destroy(&handle
->ready
);
54 if (handle
->cmd_queue
.event_pipe
) {
55 lttng_pipe_destroy(handle
->cmd_queue
.event_pipe
);
57 if (handle
->channel_monitoring_pipes
.ust32_consumer
>= 0) {
58 ret
= close(handle
->channel_monitoring_pipes
.ust32_consumer
);
60 PERROR("close 32-bit consumer channel monitoring pipe");
63 if (handle
->channel_monitoring_pipes
.ust64_consumer
>= 0) {
64 ret
= close(handle
->channel_monitoring_pipes
.ust64_consumer
);
66 PERROR("close 64-bit consumer channel monitoring pipe");
69 if (handle
->channel_monitoring_pipes
.kernel_consumer
>= 0) {
70 ret
= close(handle
->channel_monitoring_pipes
.kernel_consumer
);
72 PERROR("close kernel consumer channel monitoring pipe");
80 struct notification_thread_handle
*notification_thread_handle_create(
81 struct lttng_pipe
*ust32_channel_monitor_pipe
,
82 struct lttng_pipe
*ust64_channel_monitor_pipe
,
83 struct lttng_pipe
*kernel_channel_monitor_pipe
)
86 struct notification_thread_handle
*handle
;
87 struct lttng_pipe
*event_pipe
= NULL
;
89 handle
= zmalloc(sizeof(*handle
));
94 sem_init(&handle
->ready
, 0, 0);
96 event_pipe
= lttng_pipe_open(FD_CLOEXEC
);
98 ERR("event_pipe creation");
102 handle
->cmd_queue
.event_pipe
= event_pipe
;
105 CDS_INIT_LIST_HEAD(&handle
->cmd_queue
.list
);
106 ret
= pthread_mutex_init(&handle
->cmd_queue
.lock
, NULL
);
111 if (ust32_channel_monitor_pipe
) {
112 handle
->channel_monitoring_pipes
.ust32_consumer
=
113 lttng_pipe_release_readfd(
114 ust32_channel_monitor_pipe
);
115 if (handle
->channel_monitoring_pipes
.ust32_consumer
< 0) {
119 handle
->channel_monitoring_pipes
.ust32_consumer
= -1;
121 if (ust64_channel_monitor_pipe
) {
122 handle
->channel_monitoring_pipes
.ust64_consumer
=
123 lttng_pipe_release_readfd(
124 ust64_channel_monitor_pipe
);
125 if (handle
->channel_monitoring_pipes
.ust64_consumer
< 0) {
129 handle
->channel_monitoring_pipes
.ust64_consumer
= -1;
131 if (kernel_channel_monitor_pipe
) {
132 handle
->channel_monitoring_pipes
.kernel_consumer
=
133 lttng_pipe_release_readfd(
134 kernel_channel_monitor_pipe
);
135 if (handle
->channel_monitoring_pipes
.kernel_consumer
< 0) {
139 handle
->channel_monitoring_pipes
.kernel_consumer
= -1;
145 lttng_pipe_destroy(event_pipe
);
146 notification_thread_handle_destroy(handle
);
151 char *get_notification_channel_sock_path(void)
154 bool is_root
= !getuid();
157 sock_path
= zmalloc(LTTNG_PATH_MAX
);
163 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
164 DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK
);
169 const char *home_path
= utils_get_home_dir();
172 ERR("Can't get HOME directory for socket creation");
176 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
177 DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK
,
191 void notification_channel_socket_destroy(int fd
)
194 char *sock_path
= get_notification_channel_sock_path();
196 DBG("[notification-thread] Destroying notification channel socket");
199 ret
= unlink(sock_path
);
202 PERROR("unlink notification channel socket");
208 PERROR("close notification channel socket");
213 int notification_channel_socket_create(void)
216 char *sock_path
= get_notification_channel_sock_path();
218 DBG("[notification-thread] Creating notification channel UNIX socket at %s",
221 ret
= lttcomm_create_unix_sock(sock_path
);
223 ERR("[notification-thread] Failed to create notification socket");
228 ret
= chmod(sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
230 ERR("Set file permissions failed: %s", sock_path
);
231 PERROR("chmod notification channel socket");
238 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
241 /* Default to root group. */
245 ret
= chown(sock_path
, 0, gid
);
247 ERR("Failed to set the notification channel socket's group");
253 DBG("[notification-thread] Notification channel UNIX socket created (fd = %i)",
258 if (fd
>= 0 && close(fd
) < 0) {
259 PERROR("close notification channel socket");
266 int init_poll_set(struct lttng_poll_event
*poll_set
,
267 struct notification_thread_handle
*handle
,
268 int notification_channel_socket
)
273 * Create pollset with size 5:
274 * - notification channel socket (listen for new connections),
275 * - command queue event fd (internal sessiond commands),
276 * - consumerd (32-bit user space) channel monitor pipe,
277 * - consumerd (64-bit user space) channel monitor pipe,
278 * - consumerd (kernel) channel monitor pipe.
280 ret
= lttng_poll_create(poll_set
, 5, LTTNG_CLOEXEC
);
285 ret
= lttng_poll_add(poll_set
, notification_channel_socket
,
286 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
288 ERR("[notification-thread] Failed to add notification channel socket to pollset");
291 ret
= lttng_poll_add(poll_set
, lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
),
294 ERR("[notification-thread] Failed to add notification command queue event fd to pollset");
297 ret
= lttng_poll_add(poll_set
,
298 handle
->channel_monitoring_pipes
.ust32_consumer
,
301 ERR("[notification-thread] Failed to add ust-32 channel monitoring pipe fd to pollset");
304 ret
= lttng_poll_add(poll_set
,
305 handle
->channel_monitoring_pipes
.ust64_consumer
,
308 ERR("[notification-thread] Failed to add ust-64 channel monitoring pipe fd to pollset");
311 if (handle
->channel_monitoring_pipes
.kernel_consumer
< 0) {
314 ret
= lttng_poll_add(poll_set
,
315 handle
->channel_monitoring_pipes
.kernel_consumer
,
318 ERR("[notification-thread] Failed to add kernel channel monitoring pipe fd to pollset");
324 lttng_poll_clean(poll_set
);
329 void fini_thread_state(struct notification_thread_state
*state
)
333 if (state
->client_socket_ht
) {
334 ret
= handle_notification_thread_client_disconnect_all(state
);
336 ret
= cds_lfht_destroy(state
->client_socket_ht
, NULL
);
339 if (state
->client_id_ht
) {
340 ret
= cds_lfht_destroy(state
->client_id_ht
, NULL
);
343 if (state
->triggers_ht
) {
344 ret
= handle_notification_thread_trigger_unregister_all(state
);
346 ret
= cds_lfht_destroy(state
->triggers_ht
, NULL
);
349 if (state
->channel_triggers_ht
) {
350 ret
= cds_lfht_destroy(state
->channel_triggers_ht
, NULL
);
353 if (state
->channel_state_ht
) {
354 ret
= cds_lfht_destroy(state
->channel_state_ht
, NULL
);
357 if (state
->notification_trigger_clients_ht
) {
358 ret
= cds_lfht_destroy(state
->notification_trigger_clients_ht
,
362 if (state
->channels_ht
) {
363 ret
= cds_lfht_destroy(state
->channels_ht
, NULL
);
366 if (state
->sessions_ht
) {
367 ret
= cds_lfht_destroy(state
->sessions_ht
, NULL
);
370 if (state
->triggers_by_name_uid_ht
) {
371 ret
= cds_lfht_destroy(state
->triggers_by_name_uid_ht
, NULL
);
374 if (state
->trigger_tokens_ht
) {
375 ret
= cds_lfht_destroy(state
->trigger_tokens_ht
, NULL
);
379 * Must be destroyed after all channels have been destroyed.
380 * See comment in struct lttng_session_trigger_list.
382 if (state
->session_triggers_ht
) {
383 ret
= cds_lfht_destroy(state
->session_triggers_ht
, NULL
);
386 if (state
->notification_channel_socket
>= 0) {
387 notification_channel_socket_destroy(
388 state
->notification_channel_socket
);
391 assert(cds_list_empty(&state
->tracer_event_sources_list
));
393 if (state
->executor
) {
394 action_executor_destroy(state
->executor
);
396 lttng_poll_clean(&state
->events
);
400 void mark_thread_as_ready(struct notification_thread_handle
*handle
)
402 DBG("Marking notification thread as ready");
403 sem_post(&handle
->ready
);
407 void wait_until_thread_is_ready(struct notification_thread_handle
*handle
)
409 DBG("Waiting for notification thread to be ready");
410 sem_wait(&handle
->ready
);
411 DBG("Notification thread is ready");
415 int init_thread_state(struct notification_thread_handle
*handle
,
416 struct notification_thread_state
*state
)
420 memset(state
, 0, sizeof(*state
));
421 state
->notification_channel_socket
= -1;
422 state
->trigger_id
.next_tracer_token
= 1;
423 lttng_poll_init(&state
->events
);
425 ret
= notification_channel_socket_create();
429 state
->notification_channel_socket
= ret
;
431 ret
= init_poll_set(&state
->events
, handle
,
432 state
->notification_channel_socket
);
437 DBG("[notification-thread] Listening on notification channel socket");
438 ret
= lttcomm_listen_unix_sock(state
->notification_channel_socket
);
440 ERR("[notification-thread] Listen failed on notification channel socket");
444 state
->client_socket_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
445 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
446 if (!state
->client_socket_ht
) {
450 state
->client_id_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
451 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
452 if (!state
->client_id_ht
) {
456 state
->channel_triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
457 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
458 if (!state
->channel_triggers_ht
) {
462 state
->session_triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
463 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
464 if (!state
->session_triggers_ht
) {
468 state
->channel_state_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
469 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
470 if (!state
->channel_state_ht
) {
474 state
->notification_trigger_clients_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
475 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
476 if (!state
->notification_trigger_clients_ht
) {
480 state
->channels_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
481 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
482 if (!state
->channels_ht
) {
485 state
->sessions_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
486 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
487 if (!state
->sessions_ht
) {
490 state
->triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
491 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
492 if (!state
->triggers_ht
) {
495 state
->triggers_by_name_uid_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
496 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
497 if (!state
->triggers_by_name_uid_ht
) {
501 state
->trigger_tokens_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
502 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
503 if (!state
->trigger_tokens_ht
) {
507 CDS_INIT_LIST_HEAD(&state
->tracer_event_sources_list
);
509 state
->executor
= action_executor_create(handle
);
510 if (!state
->executor
) {
514 state
->restart_poll
= false;
516 mark_thread_as_ready(handle
);
520 fini_thread_state(state
);
525 int handle_channel_monitoring_pipe(int fd
, uint32_t revents
,
526 struct notification_thread_handle
*handle
,
527 struct notification_thread_state
*state
)
530 enum lttng_domain_type domain
;
532 if (fd
== handle
->channel_monitoring_pipes
.ust32_consumer
||
533 fd
== handle
->channel_monitoring_pipes
.ust64_consumer
) {
534 domain
= LTTNG_DOMAIN_UST
;
535 } else if (fd
== handle
->channel_monitoring_pipes
.kernel_consumer
) {
536 domain
= LTTNG_DOMAIN_KERNEL
;
541 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
542 ret
= lttng_poll_del(&state
->events
, fd
);
544 ERR("[notification-thread] Failed to remove consumer monitoring pipe from poll set");
549 ret
= handle_notification_thread_channel_sample(
552 ERR("[notification-thread] Consumer sample handling error occurred");
560 static int handle_event_notification_pipe(int event_source_fd
,
561 enum lttng_domain_type domain
,
563 struct notification_thread_state
*state
)
567 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
568 ret
= handle_notification_thread_remove_tracer_event_source_no_result(
569 state
, event_source_fd
);
571 ERR("[notification-thread] Failed to remove event notification pipe from poll set: fd = %d",
577 ret
= handle_notification_thread_event_notification(
578 state
, event_source_fd
, domain
);
580 ERR("[notification-thread] Event notification handling error occurred for fd: %d",
590 * Return the event source domain type via parameter.
592 static bool fd_is_event_notification_source(const struct notification_thread_state
*state
,
594 enum lttng_domain_type
*domain
)
596 struct notification_event_tracer_event_source_element
*source_element
;
600 cds_list_for_each_entry(source_element
,
601 &state
->tracer_event_sources_list
, node
) {
602 if (source_element
->fd
!= fd
) {
606 *domain
= source_element
->domain
;
614 * This thread services notification channel clients and commands received
615 * from various lttng-sessiond components over a command queue.
618 void *thread_notification(void *data
)
621 struct notification_thread_handle
*handle
= data
;
622 struct notification_thread_state state
;
623 enum lttng_domain_type domain
;
625 DBG("[notification-thread] Started notification thread");
627 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_NOTIFICATION
);
628 rcu_register_thread();
632 ERR("[notification-thread] Invalid thread context provided");
636 health_code_update();
638 ret
= init_thread_state(handle
, &state
);
647 DBG("[notification-thread] Entering poll wait");
648 ret
= lttng_poll_wait(&state
.events
, -1);
649 DBG("[notification-thread] Poll wait returned (%i)", ret
);
653 * Restart interrupted system call.
655 if (errno
== EINTR
) {
658 ERR("[notification-thread] Error encountered during lttng_poll_wait (%i)", ret
);
663 * Reset restart_poll flag so that calls below might turn it
666 state
.restart_poll
= false;
669 for (i
= 0; i
< fd_count
; i
++) {
670 int fd
= LTTNG_POLL_GETFD(&state
.events
, i
);
671 uint32_t revents
= LTTNG_POLL_GETEV(&state
.events
, i
);
673 DBG("[notification-thread] Handling fd (%i) activity (%u)", fd
, revents
);
675 if (fd
== state
.notification_channel_socket
) {
676 if (revents
& LPOLLIN
) {
677 ret
= handle_notification_thread_client_connect(
683 (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
684 ERR("[notification-thread] Notification socket poll error");
687 ERR("[notification-thread] Unexpected poll events %u for notification socket %i", revents
, fd
);
690 } else if (fd
== lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
)) {
691 ret
= handle_notification_thread_command(handle
,
694 DBG("[notification-thread] Error encountered while servicing command queue");
696 } else if (ret
> 0) {
699 } else if (fd
== handle
->channel_monitoring_pipes
.ust32_consumer
||
700 fd
== handle
->channel_monitoring_pipes
.ust64_consumer
||
701 fd
== handle
->channel_monitoring_pipes
.kernel_consumer
) {
702 ret
= handle_channel_monitoring_pipe(fd
,
703 revents
, handle
, &state
);
707 } else if (fd_is_event_notification_source(&state
, fd
, &domain
)) {
708 ret
= handle_event_notification_pipe(fd
, domain
, revents
, &state
);
713 /* Activity on a client's socket. */
714 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
716 * It doesn't matter if a command was
717 * pending on the client socket at this
718 * point since it now has no way to
719 * receive the notifications to which
720 * it was subscribing or unsubscribing.
722 ret
= handle_notification_thread_client_disconnect(
728 if (revents
& LPOLLIN
) {
729 ret
= handle_notification_thread_client_in(
736 if (revents
& LPOLLOUT
) {
737 ret
= handle_notification_thread_client_out(
747 * Calls above might have changed the state of the
748 * FDs in `state.events`. Call _poll_wait() again to
749 * ensure we have a consistent state.
751 if (state
.restart_poll
) {
758 fini_thread_state(&state
);
760 rcu_thread_offline();
761 rcu_unregister_thread();
762 health_unregister(health_sessiond
);
767 bool shutdown_notification_thread(void *thread_data
)
769 struct notification_thread_handle
*handle
= thread_data
;
771 notification_thread_command_quit(handle
);
775 struct lttng_thread
*launch_notification_thread(
776 struct notification_thread_handle
*handle
)
778 struct lttng_thread
*thread
;
780 thread
= lttng_thread_create("Notification",
782 shutdown_notification_thread
,
790 * Wait for the thread to be marked as "ready" before returning
791 * as other subsystems depend on the notification subsystem
792 * (e.g. rotation thread).
794 wait_until_thread_is_ready(handle
);