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>
20 #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"
33 #include <urcu/list.h>
34 #include <urcu/rculfhash.h>
37 * Destroy the thread data previously created by the init function.
39 void notification_thread_handle_destroy(
40 struct notification_thread_handle
*handle
)
48 assert(cds_list_empty(&handle
->cmd_queue
.list
));
49 pthread_mutex_destroy(&handle
->cmd_queue
.lock
);
50 sem_destroy(&handle
->ready
);
52 if (handle
->cmd_queue
.event_pipe
) {
53 lttng_pipe_destroy(handle
->cmd_queue
.event_pipe
);
55 if (handle
->channel_monitoring_pipes
.ust32_consumer
>= 0) {
56 ret
= close(handle
->channel_monitoring_pipes
.ust32_consumer
);
58 PERROR("close 32-bit consumer channel monitoring pipe");
61 if (handle
->channel_monitoring_pipes
.ust64_consumer
>= 0) {
62 ret
= close(handle
->channel_monitoring_pipes
.ust64_consumer
);
64 PERROR("close 64-bit consumer channel monitoring pipe");
67 if (handle
->channel_monitoring_pipes
.kernel_consumer
>= 0) {
68 ret
= close(handle
->channel_monitoring_pipes
.kernel_consumer
);
70 PERROR("close kernel consumer channel monitoring pipe");
77 struct notification_thread_handle
*notification_thread_handle_create(
78 struct lttng_pipe
*ust32_channel_monitor_pipe
,
79 struct lttng_pipe
*ust64_channel_monitor_pipe
,
80 struct lttng_pipe
*kernel_channel_monitor_pipe
)
83 struct notification_thread_handle
*handle
;
84 struct lttng_pipe
*event_pipe
= NULL
;
86 handle
= zmalloc(sizeof(*handle
));
91 sem_init(&handle
->ready
, 0, 0);
93 event_pipe
= lttng_pipe_open(FD_CLOEXEC
);
95 ERR("event_pipe creation");
99 handle
->cmd_queue
.event_pipe
= event_pipe
;
102 CDS_INIT_LIST_HEAD(&handle
->cmd_queue
.list
);
103 ret
= pthread_mutex_init(&handle
->cmd_queue
.lock
, NULL
);
108 if (ust32_channel_monitor_pipe
) {
109 handle
->channel_monitoring_pipes
.ust32_consumer
=
110 lttng_pipe_release_readfd(
111 ust32_channel_monitor_pipe
);
112 if (handle
->channel_monitoring_pipes
.ust32_consumer
< 0) {
116 handle
->channel_monitoring_pipes
.ust32_consumer
= -1;
118 if (ust64_channel_monitor_pipe
) {
119 handle
->channel_monitoring_pipes
.ust64_consumer
=
120 lttng_pipe_release_readfd(
121 ust64_channel_monitor_pipe
);
122 if (handle
->channel_monitoring_pipes
.ust64_consumer
< 0) {
126 handle
->channel_monitoring_pipes
.ust64_consumer
= -1;
128 if (kernel_channel_monitor_pipe
) {
129 handle
->channel_monitoring_pipes
.kernel_consumer
=
130 lttng_pipe_release_readfd(
131 kernel_channel_monitor_pipe
);
132 if (handle
->channel_monitoring_pipes
.kernel_consumer
< 0) {
136 handle
->channel_monitoring_pipes
.kernel_consumer
= -1;
141 lttng_pipe_destroy(event_pipe
);
142 notification_thread_handle_destroy(handle
);
147 char *get_notification_channel_sock_path(void)
150 bool is_root
= !getuid();
153 sock_path
= zmalloc(LTTNG_PATH_MAX
);
159 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
160 DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK
);
165 const char *home_path
= utils_get_home_dir();
168 ERR("Can't get HOME directory for socket creation");
172 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
173 DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK
,
187 void notification_channel_socket_destroy(int fd
)
190 char *sock_path
= get_notification_channel_sock_path();
192 DBG("[notification-thread] Destroying notification channel socket");
195 ret
= unlink(sock_path
);
198 PERROR("unlink notification channel socket");
204 PERROR("close notification channel socket");
209 int notification_channel_socket_create(void)
212 char *sock_path
= get_notification_channel_sock_path();
214 DBG("[notification-thread] Creating notification channel UNIX socket at %s",
217 ret
= lttcomm_create_unix_sock(sock_path
);
219 ERR("[notification-thread] Failed to create notification socket");
224 ret
= chmod(sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
226 ERR("Set file permissions failed: %s", sock_path
);
227 PERROR("chmod notification channel socket");
234 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
237 /* Default to root group. */
241 ret
= chown(sock_path
, 0, gid
);
243 ERR("Failed to set the notification channel socket's group");
249 DBG("[notification-thread] Notification channel UNIX socket created (fd = %i)",
254 if (fd
>= 0 && close(fd
) < 0) {
255 PERROR("close notification channel socket");
262 int init_poll_set(struct lttng_poll_event
*poll_set
,
263 struct notification_thread_handle
*handle
,
264 int notification_channel_socket
)
269 * Create pollset with size 5:
270 * - notification channel socket (listen for new connections),
271 * - command queue event fd (internal sessiond commands),
272 * - consumerd (32-bit user space) channel monitor pipe,
273 * - consumerd (64-bit user space) channel monitor pipe,
274 * - consumerd (kernel) channel monitor pipe.
276 ret
= lttng_poll_create(poll_set
, 5, LTTNG_CLOEXEC
);
281 ret
= lttng_poll_add(poll_set
, notification_channel_socket
,
282 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
284 ERR("[notification-thread] Failed to add notification channel socket to pollset");
287 ret
= lttng_poll_add(poll_set
, lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
),
290 ERR("[notification-thread] Failed to add notification command queue event fd to pollset");
293 ret
= lttng_poll_add(poll_set
,
294 handle
->channel_monitoring_pipes
.ust32_consumer
,
297 ERR("[notification-thread] Failed to add ust-32 channel monitoring pipe fd to pollset");
300 ret
= lttng_poll_add(poll_set
,
301 handle
->channel_monitoring_pipes
.ust64_consumer
,
304 ERR("[notification-thread] Failed to add ust-64 channel monitoring pipe fd to pollset");
307 if (handle
->channel_monitoring_pipes
.kernel_consumer
< 0) {
310 ret
= lttng_poll_add(poll_set
,
311 handle
->channel_monitoring_pipes
.kernel_consumer
,
314 ERR("[notification-thread] Failed to add kernel channel monitoring pipe fd to pollset");
320 lttng_poll_clean(poll_set
);
325 void fini_thread_state(struct notification_thread_state
*state
)
329 if (state
->client_socket_ht
) {
330 ret
= handle_notification_thread_client_disconnect_all(state
);
332 ret
= cds_lfht_destroy(state
->client_socket_ht
, NULL
);
335 if (state
->client_id_ht
) {
336 ret
= cds_lfht_destroy(state
->client_id_ht
, NULL
);
339 if (state
->triggers_ht
) {
340 ret
= handle_notification_thread_trigger_unregister_all(state
);
342 ret
= cds_lfht_destroy(state
->triggers_ht
, NULL
);
345 if (state
->channel_triggers_ht
) {
346 ret
= cds_lfht_destroy(state
->channel_triggers_ht
, NULL
);
349 if (state
->channel_state_ht
) {
350 ret
= cds_lfht_destroy(state
->channel_state_ht
, NULL
);
353 if (state
->notification_trigger_clients_ht
) {
354 ret
= cds_lfht_destroy(state
->notification_trigger_clients_ht
,
358 if (state
->channels_ht
) {
359 ret
= cds_lfht_destroy(state
->channels_ht
, NULL
);
362 if (state
->sessions_ht
) {
363 ret
= cds_lfht_destroy(state
->sessions_ht
, NULL
);
367 * Must be destroyed after all channels have been destroyed.
368 * See comment in struct lttng_session_trigger_list.
370 if (state
->session_triggers_ht
) {
371 ret
= cds_lfht_destroy(state
->session_triggers_ht
, NULL
);
374 if (state
->notification_channel_socket
>= 0) {
375 notification_channel_socket_destroy(
376 state
->notification_channel_socket
);
378 lttng_poll_clean(&state
->events
);
382 void mark_thread_as_ready(struct notification_thread_handle
*handle
)
384 DBG("Marking notification thread as ready");
385 sem_post(&handle
->ready
);
389 void wait_until_thread_is_ready(struct notification_thread_handle
*handle
)
391 DBG("Waiting for notification thread to be ready");
392 sem_wait(&handle
->ready
);
393 DBG("Notification thread is ready");
397 int init_thread_state(struct notification_thread_handle
*handle
,
398 struct notification_thread_state
*state
)
402 memset(state
, 0, sizeof(*state
));
403 state
->notification_channel_socket
= -1;
404 lttng_poll_init(&state
->events
);
406 ret
= notification_channel_socket_create();
410 state
->notification_channel_socket
= ret
;
412 ret
= init_poll_set(&state
->events
, handle
,
413 state
->notification_channel_socket
);
418 DBG("[notification-thread] Listening on notification channel socket");
419 ret
= lttcomm_listen_unix_sock(state
->notification_channel_socket
);
421 ERR("[notification-thread] Listen failed on notification channel socket");
425 state
->client_socket_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
426 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
427 if (!state
->client_socket_ht
) {
431 state
->client_id_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
432 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
433 if (!state
->client_id_ht
) {
437 state
->channel_triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
438 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
439 if (!state
->channel_triggers_ht
) {
443 state
->session_triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
444 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
445 if (!state
->session_triggers_ht
) {
449 state
->channel_state_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
450 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
451 if (!state
->channel_state_ht
) {
455 state
->notification_trigger_clients_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
456 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
457 if (!state
->notification_trigger_clients_ht
) {
461 state
->channels_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
462 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
463 if (!state
->channels_ht
) {
466 state
->sessions_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
467 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
468 if (!state
->sessions_ht
) {
471 state
->triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
472 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
473 if (!state
->triggers_ht
) {
476 mark_thread_as_ready(handle
);
480 fini_thread_state(state
);
485 int handle_channel_monitoring_pipe(int fd
, uint32_t revents
,
486 struct notification_thread_handle
*handle
,
487 struct notification_thread_state
*state
)
490 enum lttng_domain_type domain
;
492 if (fd
== handle
->channel_monitoring_pipes
.ust32_consumer
||
493 fd
== handle
->channel_monitoring_pipes
.ust64_consumer
) {
494 domain
= LTTNG_DOMAIN_UST
;
495 } else if (fd
== handle
->channel_monitoring_pipes
.kernel_consumer
) {
496 domain
= LTTNG_DOMAIN_KERNEL
;
501 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
502 ret
= lttng_poll_del(&state
->events
, fd
);
504 ERR("[notification-thread] Failed to remove consumer monitoring pipe from poll set");
509 ret
= handle_notification_thread_channel_sample(
512 ERR("[notification-thread] Consumer sample handling error occurred");
521 * This thread services notification channel clients and commands received
522 * from various lttng-sessiond components over a command queue.
525 void *thread_notification(void *data
)
528 struct notification_thread_handle
*handle
= data
;
529 struct notification_thread_state state
;
531 DBG("[notification-thread] Started notification thread");
533 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_NOTIFICATION
);
534 rcu_register_thread();
538 ERR("[notification-thread] Invalid thread context provided");
542 health_code_update();
544 ret
= init_thread_state(handle
, &state
);
553 DBG("[notification-thread] Entering poll wait");
554 ret
= lttng_poll_wait(&state
.events
, -1);
555 DBG("[notification-thread] Poll wait returned (%i)", ret
);
559 * Restart interrupted system call.
561 if (errno
== EINTR
) {
564 ERR("[notification-thread] Error encountered during lttng_poll_wait (%i)", ret
);
569 for (i
= 0; i
< fd_count
; i
++) {
570 int fd
= LTTNG_POLL_GETFD(&state
.events
, i
);
571 uint32_t revents
= LTTNG_POLL_GETEV(&state
.events
, i
);
573 DBG("[notification-thread] Handling fd (%i) activity (%u)", fd
, revents
);
575 if (fd
== state
.notification_channel_socket
) {
576 if (revents
& LPOLLIN
) {
577 ret
= handle_notification_thread_client_connect(
583 (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
584 ERR("[notification-thread] Notification socket poll error");
587 ERR("[notification-thread] Unexpected poll events %u for notification socket %i", revents
, fd
);
590 } else if (fd
== lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
)) {
591 ret
= handle_notification_thread_command(handle
,
594 DBG("[notification-thread] Error encountered while servicing command queue");
596 } else if (ret
> 0) {
599 } else if (fd
== handle
->channel_monitoring_pipes
.ust32_consumer
||
600 fd
== handle
->channel_monitoring_pipes
.ust64_consumer
||
601 fd
== handle
->channel_monitoring_pipes
.kernel_consumer
) {
602 ret
= handle_channel_monitoring_pipe(fd
,
603 revents
, handle
, &state
);
608 /* Activity on a client's socket. */
609 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
611 * It doesn't matter if a command was
612 * pending on the client socket at this
613 * point since it now has no way to
614 * receive the notifications to which
615 * it was subscribing or unsubscribing.
617 ret
= handle_notification_thread_client_disconnect(
623 if (revents
& LPOLLIN
) {
624 ret
= handle_notification_thread_client_in(
631 if (revents
& LPOLLOUT
) {
632 ret
= handle_notification_thread_client_out(
644 fini_thread_state(&state
);
646 rcu_thread_offline();
647 rcu_unregister_thread();
648 health_unregister(health_sessiond
);
653 bool shutdown_notification_thread(void *thread_data
)
655 struct notification_thread_handle
*handle
= thread_data
;
657 notification_thread_command_quit(handle
);
661 struct lttng_thread
*launch_notification_thread(
662 struct notification_thread_handle
*handle
)
664 struct lttng_thread
*thread
;
666 thread
= lttng_thread_create("Notification",
668 shutdown_notification_thread
,
676 * Wait for the thread to be marked as "ready" before returning
677 * as other subsystems depend on the notification subsystem
678 * (e.g. rotation thread).
680 wait_until_thread_is_ready(handle
);