2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <lttng/trigger/trigger.h>
20 #include <lttng/notification/channel-internal.h>
21 #include <lttng/notification/notification-internal.h>
22 #include <lttng/condition/condition-internal.h>
23 #include <lttng/condition/buffer-usage-internal.h>
24 #include <common/error.h>
25 #include <common/config/session-config.h>
26 #include <common/defaults.h>
27 #include <common/utils.h>
28 #include <common/align.h>
29 #include <common/time.h>
30 #include <sys/eventfd.h>
35 #include "notification-thread.h"
36 #include "notification-thread-events.h"
37 #include "notification-thread-commands.h"
38 #include "lttng-sessiond.h"
39 #include "health-sessiond.h"
42 #include <urcu/list.h>
43 #include <urcu/rculfhash.h>
46 * Destroy the thread data previously created by the init function.
48 void notification_thread_handle_destroy(
49 struct notification_thread_handle
*handle
)
57 assert(cds_list_empty(&handle
->cmd_queue
.list
));
58 pthread_mutex_destroy(&handle
->cmd_queue
.lock
);
60 if (handle
->cmd_queue
.event_pipe
) {
61 lttng_pipe_destroy(handle
->cmd_queue
.event_pipe
);
63 if (handle
->channel_monitoring_pipes
.ust32_consumer
>= 0) {
64 ret
= close(handle
->channel_monitoring_pipes
.ust32_consumer
);
66 PERROR("close 32-bit consumer channel monitoring pipe");
69 if (handle
->channel_monitoring_pipes
.ust64_consumer
>= 0) {
70 ret
= close(handle
->channel_monitoring_pipes
.ust64_consumer
);
72 PERROR("close 64-bit consumer channel monitoring pipe");
75 if (handle
->channel_monitoring_pipes
.kernel_consumer
>= 0) {
76 ret
= close(handle
->channel_monitoring_pipes
.kernel_consumer
);
78 PERROR("close kernel consumer channel monitoring pipe");
85 struct notification_thread_handle
*notification_thread_handle_create(
86 struct lttng_pipe
*ust32_channel_monitor_pipe
,
87 struct lttng_pipe
*ust64_channel_monitor_pipe
,
88 struct lttng_pipe
*kernel_channel_monitor_pipe
,
89 sem_t
*notification_thread_ready
)
92 struct notification_thread_handle
*handle
;
93 struct lttng_pipe
*event_pipe
= NULL
;
95 handle
= zmalloc(sizeof(*handle
));
100 event_pipe
= lttng_pipe_open(FD_CLOEXEC
);
102 ERR("event_pipe creation");
106 handle
->cmd_queue
.event_pipe
= event_pipe
;
109 CDS_INIT_LIST_HEAD(&handle
->cmd_queue
.list
);
110 ret
= pthread_mutex_init(&handle
->cmd_queue
.lock
, NULL
);
115 if (ust32_channel_monitor_pipe
) {
116 handle
->channel_monitoring_pipes
.ust32_consumer
=
117 lttng_pipe_release_readfd(
118 ust32_channel_monitor_pipe
);
119 if (handle
->channel_monitoring_pipes
.ust32_consumer
< 0) {
123 handle
->channel_monitoring_pipes
.ust32_consumer
= -1;
125 if (ust64_channel_monitor_pipe
) {
126 handle
->channel_monitoring_pipes
.ust64_consumer
=
127 lttng_pipe_release_readfd(
128 ust64_channel_monitor_pipe
);
129 if (handle
->channel_monitoring_pipes
.ust64_consumer
< 0) {
133 handle
->channel_monitoring_pipes
.ust64_consumer
= -1;
135 if (kernel_channel_monitor_pipe
) {
136 handle
->channel_monitoring_pipes
.kernel_consumer
=
137 lttng_pipe_release_readfd(
138 kernel_channel_monitor_pipe
);
139 if (handle
->channel_monitoring_pipes
.kernel_consumer
< 0) {
143 handle
->channel_monitoring_pipes
.kernel_consumer
= -1;
145 handle
->notification_thread_ready
= notification_thread_ready
;
149 lttng_pipe_destroy(event_pipe
);
150 notification_thread_handle_destroy(handle
);
155 char *get_notification_channel_sock_path(void)
158 bool is_root
= !getuid();
161 sock_path
= zmalloc(LTTNG_PATH_MAX
);
167 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
168 DEFAULT_GLOBAL_NOTIFICATION_CHANNEL_UNIX_SOCK
);
173 char *home_path
= utils_get_home_dir();
176 ERR("Can't get HOME directory for socket creation");
180 ret
= snprintf(sock_path
, LTTNG_PATH_MAX
,
181 DEFAULT_HOME_NOTIFICATION_CHANNEL_UNIX_SOCK
,
195 void notification_channel_socket_destroy(int fd
)
198 char *sock_path
= get_notification_channel_sock_path();
200 DBG("[notification-thread] Destroying notification channel socket");
203 ret
= unlink(sock_path
);
206 PERROR("unlink notification channel socket");
212 PERROR("close notification channel socket");
217 int notification_channel_socket_create(void)
220 char *sock_path
= get_notification_channel_sock_path();
222 DBG("[notification-thread] Creating notification channel UNIX socket at %s",
225 ret
= lttcomm_create_unix_sock(sock_path
);
227 ERR("[notification-thread] Failed to create notification socket");
232 ret
= chmod(sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
234 ERR("Set file permissions failed: %s", sock_path
);
235 PERROR("chmod notification channel socket");
240 ret
= chown(sock_path
, 0,
241 utils_get_group_id(config
.tracing_group_name
.value
));
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
->triggers_ht
) {
336 ret
= handle_notification_thread_trigger_unregister_all(state
);
338 ret
= cds_lfht_destroy(state
->triggers_ht
, NULL
);
341 if (state
->channel_triggers_ht
) {
342 ret
= cds_lfht_destroy(state
->channel_triggers_ht
, NULL
);
345 if (state
->channel_state_ht
) {
346 ret
= cds_lfht_destroy(state
->channel_state_ht
, NULL
);
349 if (state
->notification_trigger_clients_ht
) {
350 ret
= cds_lfht_destroy(state
->notification_trigger_clients_ht
,
354 if (state
->channels_ht
) {
355 ret
= cds_lfht_destroy(state
->channels_ht
, NULL
);
358 if (state
->sessions_ht
) {
359 ret
= cds_lfht_destroy(state
->sessions_ht
, NULL
);
362 if (state
->notification_channel_socket
>= 0) {
363 notification_channel_socket_destroy(
364 state
->notification_channel_socket
);
366 lttng_poll_clean(&state
->events
);
370 int init_thread_state(struct notification_thread_handle
*handle
,
371 struct notification_thread_state
*state
)
375 memset(state
, 0, sizeof(*state
));
376 state
->notification_channel_socket
= -1;
377 lttng_poll_init(&state
->events
);
379 ret
= notification_channel_socket_create();
383 state
->notification_channel_socket
= ret
;
385 ret
= init_poll_set(&state
->events
, handle
,
386 state
->notification_channel_socket
);
391 DBG("[notification-thread] Listening on notification channel socket");
392 ret
= lttcomm_listen_unix_sock(state
->notification_channel_socket
);
394 ERR("[notification-thread] Listen failed on notification channel socket");
398 state
->client_socket_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
399 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
400 if (!state
->client_socket_ht
) {
404 state
->channel_triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
405 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
406 if (!state
->channel_triggers_ht
) {
410 state
->channel_state_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
411 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
412 if (!state
->channel_state_ht
) {
416 state
->notification_trigger_clients_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
417 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
418 if (!state
->notification_trigger_clients_ht
) {
422 state
->channels_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
423 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
424 if (!state
->channels_ht
) {
427 state
->sessions_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
428 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
429 if (!state
->sessions_ht
) {
432 state
->triggers_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
433 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
434 if (!state
->triggers_ht
) {
437 sem_post(handle
->notification_thread_ready
);
441 fini_thread_state(state
);
446 int handle_channel_monitoring_pipe(int fd
, uint32_t revents
,
447 struct notification_thread_handle
*handle
,
448 struct notification_thread_state
*state
)
451 enum lttng_domain_type domain
;
453 if (fd
== handle
->channel_monitoring_pipes
.ust32_consumer
||
454 fd
== handle
->channel_monitoring_pipes
.ust64_consumer
) {
455 domain
= LTTNG_DOMAIN_UST
;
456 } else if (fd
== handle
->channel_monitoring_pipes
.kernel_consumer
) {
457 domain
= LTTNG_DOMAIN_KERNEL
;
462 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
463 ret
= lttng_poll_del(&state
->events
, fd
);
465 ERR("[notification-thread] Failed to remove consumer monitoring pipe from poll set");
470 ret
= handle_notification_thread_channel_sample(
473 ERR("[notification-thread] Consumer sample handling error occurred");
482 * This thread services notification channel clients and commands received
483 * from various lttng-sessiond components over a command queue.
485 void *thread_notification(void *data
)
488 struct notification_thread_handle
*handle
= data
;
489 struct notification_thread_state state
;
491 DBG("[notification-thread] Started notification thread");
494 ERR("[notification-thread] Invalid thread context provided");
498 rcu_register_thread();
501 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_NOTIFICATION
);
502 health_code_update();
504 ret
= init_thread_state(handle
, &state
);
509 /* Ready to handle client connections. */
510 sessiond_notify_ready();
516 DBG("[notification-thread] Entering poll wait");
517 ret
= lttng_poll_wait(&state
.events
, -1);
518 DBG("[notification-thread] Poll wait returned (%i)", ret
);
522 * Restart interrupted system call.
524 if (errno
== EINTR
) {
527 ERR("[notification-thread] Error encountered during lttng_poll_wait (%i)", ret
);
532 for (i
= 0; i
< fd_count
; i
++) {
533 int fd
= LTTNG_POLL_GETFD(&state
.events
, i
);
534 uint32_t revents
= LTTNG_POLL_GETEV(&state
.events
, i
);
539 DBG("[notification-thread] Handling fd (%i) activity (%u)", fd
, revents
);
541 if (fd
== state
.notification_channel_socket
) {
542 if (revents
& LPOLLIN
) {
543 ret
= handle_notification_thread_client_connect(
549 (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
550 ERR("[notification-thread] Notification socket poll error");
553 ERR("[notification-thread] Unexpected poll events %u for notification socket %i", revents
, fd
);
556 } else if (fd
== lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
)) {
557 ret
= handle_notification_thread_command(handle
,
560 DBG("[notification-thread] Error encountered while servicing command queue");
562 } else if (ret
> 0) {
565 } else if (fd
== handle
->channel_monitoring_pipes
.ust32_consumer
||
566 fd
== handle
->channel_monitoring_pipes
.ust64_consumer
||
567 fd
== handle
->channel_monitoring_pipes
.kernel_consumer
) {
568 ret
= handle_channel_monitoring_pipe(fd
,
569 revents
, handle
, &state
);
574 /* Activity on a client's socket. */
575 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
577 * It doesn't matter if a command was
578 * pending on the client socket at this
579 * point since it now has no way to
580 * receive the notifications to which
581 * it was subscribing or unsubscribing.
583 ret
= handle_notification_thread_client_disconnect(
589 if (revents
& LPOLLIN
) {
590 ret
= handle_notification_thread_client_in(
597 if (revents
& LPOLLOUT
) {
598 ret
= handle_notification_thread_client_out(
610 fini_thread_state(&state
);
611 health_unregister(health_sessiond
);
612 rcu_thread_offline();
613 rcu_unregister_thread();