2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.h"
12 #include <urcu/rculfhash.h>
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/futex.h>
17 #include <common/unix.h>
18 #include <common/dynamic-buffer.h>
19 #include <common/hashtable/utils.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/macros.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.h>
24 #include <lttng/notification/notification-internal.h>
25 #include <lttng/condition/condition-internal.h>
26 #include <lttng/condition/buffer-usage-internal.h>
27 #include <lttng/condition/session-consumed-size-internal.h>
28 #include <lttng/condition/session-rotation-internal.h>
29 #include <lttng/condition/event-rule-internal.h>
30 #include <lttng/domain-internal.h>
31 #include <lttng/notification/channel-internal.h>
32 #include <lttng/trigger/trigger-internal.h>
33 #include <lttng/event-rule/event-rule-internal.h>
41 #include "condition-internal.h"
42 #include "notification-thread.h"
43 #include "notification-thread-events.h"
44 #include "notification-thread-commands.h"
45 #include "lttng-sessiond.h"
48 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
49 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
51 enum lttng_object_type
{
52 LTTNG_OBJECT_TYPE_UNKNOWN
,
53 LTTNG_OBJECT_TYPE_NONE
,
54 LTTNG_OBJECT_TYPE_CHANNEL
,
55 LTTNG_OBJECT_TYPE_SESSION
,
58 struct lttng_trigger_list_element
{
59 /* No ownership of the trigger object is assumed. */
60 struct lttng_trigger
*trigger
;
61 struct cds_list_head node
;
64 struct lttng_channel_trigger_list
{
65 struct channel_key channel_key
;
66 /* List of struct lttng_trigger_list_element. */
67 struct cds_list_head list
;
68 /* Node in the channel_triggers_ht */
69 struct cds_lfht_node channel_triggers_ht_node
;
70 /* call_rcu delayed reclaim. */
71 struct rcu_head rcu_node
;
75 * List of triggers applying to a given session.
78 * - lttng_session_trigger_list_create()
79 * - lttng_session_trigger_list_build()
80 * - lttng_session_trigger_list_destroy()
81 * - lttng_session_trigger_list_add()
83 struct lttng_session_trigger_list
{
85 * Not owned by this; points to the session_info structure's
88 const char *session_name
;
89 /* List of struct lttng_trigger_list_element. */
90 struct cds_list_head list
;
91 /* Node in the session_triggers_ht */
92 struct cds_lfht_node session_triggers_ht_node
;
94 * Weak reference to the notification system's session triggers
97 * The session trigger list structure structure is owned by
98 * the session's session_info.
100 * The session_info is kept alive the the channel_infos holding a
101 * reference to it (reference counting). When those channels are
102 * destroyed (at runtime or on teardown), the reference they hold
103 * to the session_info are released. On destruction of session_info,
104 * session_info_destroy() will remove the list of triggers applying
105 * to this session from the notification system's state.
107 * This implies that the session_triggers_ht must be destroyed
108 * after the channels.
110 struct cds_lfht
*session_triggers_ht
;
111 /* Used for delayed RCU reclaim. */
112 struct rcu_head rcu_node
;
115 struct lttng_trigger_ht_element
{
116 struct lttng_trigger
*trigger
;
117 struct cds_lfht_node node
;
118 struct cds_lfht_node node_by_name_uid
;
119 /* call_rcu delayed reclaim. */
120 struct rcu_head rcu_node
;
123 struct lttng_condition_list_element
{
124 struct lttng_condition
*condition
;
125 struct cds_list_head node
;
129 * Facilities to carry the different notifications type in the action processing
132 struct lttng_event_notifier_notification
{
134 struct lttng_ust_event_notifier_notification
*ust
;
135 struct lttng_kernel_event_notifier_notification
*kernel
;
138 enum lttng_domain_type type
;
141 struct channel_state_sample
{
142 struct channel_key key
;
143 struct cds_lfht_node channel_state_ht_node
;
144 uint64_t highest_usage
;
145 uint64_t lowest_usage
;
146 uint64_t channel_total_consumed
;
147 /* call_rcu delayed reclaim. */
148 struct rcu_head rcu_node
;
151 static unsigned long hash_channel_key(struct channel_key
*key
);
152 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
153 struct lttng_evaluation
**evaluation
,
154 const struct notification_thread_state
*state
,
155 const struct channel_state_sample
*previous_sample
,
156 const struct channel_state_sample
*latest_sample
,
157 uint64_t previous_session_consumed_total
,
158 uint64_t latest_session_consumed_total
,
159 struct channel_info
*channel_info
);
161 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
162 const struct lttng_evaluation
*evaluation
,
163 struct notification_client_list
*client_list
,
164 struct notification_thread_state
*state
,
165 uid_t channel_uid
, gid_t channel_gid
);
168 /* session_info API */
170 void session_info_destroy(void *_data
);
172 void session_info_get(struct session_info
*session_info
);
174 void session_info_put(struct session_info
*session_info
);
176 struct session_info
*session_info_create(const char *name
,
177 uid_t uid
, gid_t gid
,
178 struct lttng_session_trigger_list
*trigger_list
,
179 struct cds_lfht
*sessions_ht
);
181 void session_info_add_channel(struct session_info
*session_info
,
182 struct channel_info
*channel_info
);
184 void session_info_remove_channel(struct session_info
*session_info
,
185 struct channel_info
*channel_info
);
187 /* lttng_session_trigger_list API */
189 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
190 const char *session_name
,
191 struct cds_lfht
*session_triggers_ht
);
193 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
194 const struct notification_thread_state
*state
,
195 const char *session_name
);
197 void lttng_session_trigger_list_destroy(
198 struct lttng_session_trigger_list
*list
);
200 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
201 struct lttng_trigger
*trigger
);
204 int client_handle_transmission_status(
205 struct notification_client
*client
,
206 enum client_transmission_status transmission_status
,
207 struct notification_thread_state
*state
);
210 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
213 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
215 /* This double-cast is intended to supress pointer-to-cast warning. */
216 const int socket
= (int) (intptr_t) key
;
217 const struct notification_client
*client
= caa_container_of(node
,
218 struct notification_client
, client_socket_ht_node
);
220 return client
->socket
== socket
;
224 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
226 /* This double-cast is intended to supress pointer-to-cast warning. */
227 const notification_client_id id
= *((notification_client_id
*) key
);
228 const struct notification_client
*client
= caa_container_of(
229 node
, struct notification_client
, client_id_ht_node
);
231 return client
->id
== id
;
235 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
237 struct channel_key
*channel_key
= (struct channel_key
*) key
;
238 struct lttng_channel_trigger_list
*trigger_list
;
240 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
241 channel_triggers_ht_node
);
243 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
244 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
248 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
250 const char *session_name
= (const char *) key
;
251 struct lttng_session_trigger_list
*trigger_list
;
253 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
254 session_triggers_ht_node
);
256 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
260 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
262 struct channel_key
*channel_key
= (struct channel_key
*) key
;
263 struct channel_state_sample
*sample
;
265 sample
= caa_container_of(node
, struct channel_state_sample
,
266 channel_state_ht_node
);
268 return !!((channel_key
->key
== sample
->key
.key
) &&
269 (channel_key
->domain
== sample
->key
.domain
));
273 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
275 struct channel_key
*channel_key
= (struct channel_key
*) key
;
276 struct channel_info
*channel_info
;
278 channel_info
= caa_container_of(node
, struct channel_info
,
281 return !!((channel_key
->key
== channel_info
->key
.key
) &&
282 (channel_key
->domain
== channel_info
->key
.domain
));
286 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
288 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
289 struct lttng_trigger_ht_element
*trigger_ht_element
;
291 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
294 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
298 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
300 const uint64_t *_key
= key
;
301 struct notification_trigger_tokens_ht_element
*element
;
303 element
= caa_container_of(node
,
304 struct notification_trigger_tokens_ht_element
, node
);
305 return *_key
== element
->token
;
309 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
311 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
312 struct notification_client_list
*client_list
;
313 const struct lttng_condition
*condition
;
315 assert(condition_key
);
317 client_list
= caa_container_of(node
, struct notification_client_list
,
318 notification_trigger_clients_ht_node
);
319 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
321 return !!lttng_condition_is_equal(condition_key
, condition
);
325 int match_session(struct cds_lfht_node
*node
, const void *key
)
327 const char *name
= key
;
328 struct session_info
*session_info
= caa_container_of(
329 node
, struct session_info
, sessions_ht_node
);
331 return !strcmp(session_info
->name
, name
);
335 const char *notification_command_type_str(
336 enum notification_thread_command_type type
)
339 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
340 return "REGISTER_TRIGGER";
341 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
342 return "UNREGISTER_TRIGGER";
343 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
344 return "ADD_CHANNEL";
345 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
346 return "REMOVE_CHANNEL";
347 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
348 return "SESSION_ROTATION_ONGOING";
349 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
350 return "SESSION_ROTATION_COMPLETED";
351 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
352 return "ADD_TRACER_EVENT_SOURCE";
353 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
354 return "REMOVE_TRACER_EVENT_SOURCE";
355 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
356 return "LIST_TRIGGERS";
357 case NOTIFICATION_COMMAND_TYPE_QUIT
:
359 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
360 return "CLIENT_COMMUNICATION_UPDATE";
367 * Match trigger based on name and credentials only.
368 * Name duplication is NOT allowed for the same uid.
371 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
376 const char *key_name
;
377 enum lttng_trigger_status status
;
378 const struct lttng_credentials
*key_creds
;
379 const struct lttng_credentials
*node_creds
;
380 const struct lttng_trigger
*trigger_key
=
381 (const struct lttng_trigger
*) key
;
382 const struct lttng_trigger_ht_element
*trigger_ht_element
=
383 caa_container_of(node
,
384 struct lttng_trigger_ht_element
,
387 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
388 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
390 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
391 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
393 /* Compare the names. */
394 if (strcmp(name
, key_name
) != 0) {
398 /* Compare the owners' UIDs. */
399 key_creds
= lttng_trigger_get_credentials(trigger_key
);
400 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
402 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
409 * Hash trigger based on name and credentials only.
412 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
414 unsigned long hash
= 0;
415 const struct lttng_credentials
*trigger_creds
;
416 const char *trigger_name
;
417 enum lttng_trigger_status status
;
419 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
420 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
421 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
424 trigger_creds
= lttng_trigger_get_credentials(trigger
);
425 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
432 unsigned long hash_channel_key(struct channel_key
*key
)
434 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
435 unsigned long domain_hash
= hash_key_ulong(
436 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
438 return key_hash
^ domain_hash
;
442 unsigned long hash_client_socket(int socket
)
444 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
448 unsigned long hash_client_id(notification_client_id id
)
450 return hash_key_u64(&id
, lttng_ht_seed
);
454 * Get the type of object to which a given condition applies. Bindings let
455 * the notification system evaluate a trigger's condition when a given
456 * object's state is updated.
458 * For instance, a condition bound to a channel will be evaluated everytime
459 * the channel's state is changed by a channel monitoring sample.
462 enum lttng_object_type
get_condition_binding_object(
463 const struct lttng_condition
*condition
)
465 switch (lttng_condition_get_type(condition
)) {
466 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
467 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
468 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
469 return LTTNG_OBJECT_TYPE_CHANNEL
;
470 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
471 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
472 return LTTNG_OBJECT_TYPE_SESSION
;
473 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
474 return LTTNG_OBJECT_TYPE_NONE
;
476 return LTTNG_OBJECT_TYPE_UNKNOWN
;
481 void free_channel_info_rcu(struct rcu_head
*node
)
483 free(caa_container_of(node
, struct channel_info
, rcu_node
));
487 void channel_info_destroy(struct channel_info
*channel_info
)
493 if (channel_info
->session_info
) {
494 session_info_remove_channel(channel_info
->session_info
,
496 session_info_put(channel_info
->session_info
);
498 if (channel_info
->name
) {
499 free(channel_info
->name
);
501 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
505 void free_session_info_rcu(struct rcu_head
*node
)
507 free(caa_container_of(node
, struct session_info
, rcu_node
));
510 /* Don't call directly, use the ref-counting mechanism. */
512 void session_info_destroy(void *_data
)
514 struct session_info
*session_info
= _data
;
517 assert(session_info
);
518 if (session_info
->channel_infos_ht
) {
519 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
521 ERR("[notification-thread] Failed to destroy channel information hash table");
524 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
527 cds_lfht_del(session_info
->sessions_ht
,
528 &session_info
->sessions_ht_node
);
530 free(session_info
->name
);
531 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
535 void session_info_get(struct session_info
*session_info
)
540 lttng_ref_get(&session_info
->ref
);
544 void session_info_put(struct session_info
*session_info
)
549 lttng_ref_put(&session_info
->ref
);
553 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
554 struct lttng_session_trigger_list
*trigger_list
,
555 struct cds_lfht
*sessions_ht
)
557 struct session_info
*session_info
;
561 session_info
= zmalloc(sizeof(*session_info
));
565 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
567 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
568 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
569 if (!session_info
->channel_infos_ht
) {
573 cds_lfht_node_init(&session_info
->sessions_ht_node
);
574 session_info
->name
= strdup(name
);
575 if (!session_info
->name
) {
578 session_info
->uid
= uid
;
579 session_info
->gid
= gid
;
580 session_info
->trigger_list
= trigger_list
;
581 session_info
->sessions_ht
= sessions_ht
;
585 session_info_put(session_info
);
590 void session_info_add_channel(struct session_info
*session_info
,
591 struct channel_info
*channel_info
)
594 cds_lfht_add(session_info
->channel_infos_ht
,
595 hash_channel_key(&channel_info
->key
),
596 &channel_info
->session_info_channels_ht_node
);
601 void session_info_remove_channel(struct session_info
*session_info
,
602 struct channel_info
*channel_info
)
605 cds_lfht_del(session_info
->channel_infos_ht
,
606 &channel_info
->session_info_channels_ht_node
);
611 struct channel_info
*channel_info_create(const char *channel_name
,
612 struct channel_key
*channel_key
, uint64_t channel_capacity
,
613 struct session_info
*session_info
)
615 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
621 cds_lfht_node_init(&channel_info
->channels_ht_node
);
622 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
623 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
624 channel_info
->capacity
= channel_capacity
;
626 channel_info
->name
= strdup(channel_name
);
627 if (!channel_info
->name
) {
632 * Set the references between session and channel infos:
633 * - channel_info holds a strong reference to session_info
634 * - session_info holds a weak reference to channel_info
636 session_info_get(session_info
);
637 session_info_add_channel(session_info
, channel_info
);
638 channel_info
->session_info
= session_info
;
642 channel_info_destroy(channel_info
);
647 bool notification_client_list_get(struct notification_client_list
*list
)
649 return urcu_ref_get_unless_zero(&list
->ref
);
653 void free_notification_client_list_rcu(struct rcu_head
*node
)
655 free(caa_container_of(node
, struct notification_client_list
,
660 void notification_client_list_release(struct urcu_ref
*list_ref
)
662 struct notification_client_list
*list
=
663 container_of(list_ref
, typeof(*list
), ref
);
664 struct notification_client_list_element
*client_list_element
, *tmp
;
666 if (list
->notification_trigger_clients_ht
) {
668 cds_lfht_del(list
->notification_trigger_clients_ht
,
669 &list
->notification_trigger_clients_ht_node
);
671 list
->notification_trigger_clients_ht
= NULL
;
673 cds_list_for_each_entry_safe(client_list_element
, tmp
,
675 free(client_list_element
);
677 pthread_mutex_destroy(&list
->lock
);
678 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
682 struct notification_client_list
*notification_client_list_create(
683 const struct lttng_trigger
*trigger
)
685 struct notification_client_list
*client_list
=
686 zmalloc(sizeof(*client_list
));
691 pthread_mutex_init(&client_list
->lock
, NULL
);
692 urcu_ref_init(&client_list
->ref
);
693 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
694 CDS_INIT_LIST_HEAD(&client_list
->list
);
695 client_list
->trigger
= trigger
;
701 void publish_notification_client_list(
702 struct notification_thread_state
*state
,
703 struct notification_client_list
*list
)
705 const struct lttng_condition
*condition
=
706 lttng_trigger_get_const_condition(list
->trigger
);
708 assert(!list
->notification_trigger_clients_ht
);
709 notification_client_list_get(list
);
711 list
->notification_trigger_clients_ht
=
712 state
->notification_trigger_clients_ht
;
715 cds_lfht_add(state
->notification_trigger_clients_ht
,
716 lttng_condition_hash(condition
),
717 &list
->notification_trigger_clients_ht_node
);
722 void notification_client_list_put(struct notification_client_list
*list
)
727 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
730 /* Provides a reference to the returned list. */
732 struct notification_client_list
*get_client_list_from_condition(
733 struct notification_thread_state
*state
,
734 const struct lttng_condition
*condition
)
736 struct cds_lfht_node
*node
;
737 struct cds_lfht_iter iter
;
738 struct notification_client_list
*list
= NULL
;
741 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
742 lttng_condition_hash(condition
),
743 match_client_list_condition
,
746 node
= cds_lfht_iter_get_node(&iter
);
748 list
= container_of(node
, struct notification_client_list
,
749 notification_trigger_clients_ht_node
);
750 list
= notification_client_list_get(list
) ? list
: NULL
;
758 int evaluate_channel_condition_for_client(
759 const struct lttng_condition
*condition
,
760 struct notification_thread_state
*state
,
761 struct lttng_evaluation
**evaluation
,
762 uid_t
*session_uid
, gid_t
*session_gid
)
765 struct cds_lfht_iter iter
;
766 struct cds_lfht_node
*node
;
767 struct channel_info
*channel_info
= NULL
;
768 struct channel_key
*channel_key
= NULL
;
769 struct channel_state_sample
*last_sample
= NULL
;
770 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
774 /* Find the channel associated with the condition. */
775 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
776 channel_trigger_list
, channel_triggers_ht_node
) {
777 struct lttng_trigger_list_element
*element
;
779 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
780 const struct lttng_condition
*current_condition
=
781 lttng_trigger_get_const_condition(
784 assert(current_condition
);
785 if (!lttng_condition_is_equal(condition
,
786 current_condition
)) {
790 /* Found the trigger, save the channel key. */
791 channel_key
= &channel_trigger_list
->channel_key
;
795 /* The channel key was found stop iteration. */
801 /* No channel found; normal exit. */
802 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
807 /* Fetch channel info for the matching channel. */
808 cds_lfht_lookup(state
->channels_ht
,
809 hash_channel_key(channel_key
),
813 node
= cds_lfht_iter_get_node(&iter
);
815 channel_info
= caa_container_of(node
, struct channel_info
,
818 /* Retrieve the channel's last sample, if it exists. */
819 cds_lfht_lookup(state
->channel_state_ht
,
820 hash_channel_key(channel_key
),
821 match_channel_state_sample
,
824 node
= cds_lfht_iter_get_node(&iter
);
826 last_sample
= caa_container_of(node
,
827 struct channel_state_sample
,
828 channel_state_ht_node
);
830 /* Nothing to evaluate, no sample was ever taken. Normal exit */
831 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
836 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
838 0, channel_info
->session_info
->consumed_data_size
,
841 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
845 *session_uid
= channel_info
->session_info
->uid
;
846 *session_gid
= channel_info
->session_info
->gid
;
853 const char *get_condition_session_name(const struct lttng_condition
*condition
)
855 const char *session_name
= NULL
;
856 enum lttng_condition_status status
;
858 switch (lttng_condition_get_type(condition
)) {
859 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
860 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
861 status
= lttng_condition_buffer_usage_get_session_name(
862 condition
, &session_name
);
864 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
865 status
= lttng_condition_session_consumed_size_get_session_name(
866 condition
, &session_name
);
868 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
869 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
870 status
= lttng_condition_session_rotation_get_session_name(
871 condition
, &session_name
);
876 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
877 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
885 int evaluate_session_condition_for_client(
886 const struct lttng_condition
*condition
,
887 struct notification_thread_state
*state
,
888 struct lttng_evaluation
**evaluation
,
889 uid_t
*session_uid
, gid_t
*session_gid
)
892 struct cds_lfht_iter iter
;
893 struct cds_lfht_node
*node
;
894 const char *session_name
;
895 struct session_info
*session_info
= NULL
;
898 session_name
= get_condition_session_name(condition
);
900 /* Find the session associated with the trigger. */
901 cds_lfht_lookup(state
->sessions_ht
,
902 hash_key_str(session_name
, lttng_ht_seed
),
906 node
= cds_lfht_iter_get_node(&iter
);
908 DBG("[notification-thread] No known session matching name \"%s\"",
914 session_info
= caa_container_of(node
, struct session_info
,
916 session_info_get(session_info
);
919 * Evaluation is performed in-line here since only one type of
920 * session-bound condition is handled for the moment.
922 switch (lttng_condition_get_type(condition
)) {
923 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
924 if (!session_info
->rotation
.ongoing
) {
926 goto end_session_put
;
929 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
930 session_info
->rotation
.id
);
933 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
936 goto end_session_put
;
942 goto end_session_put
;
945 *session_uid
= session_info
->uid
;
946 *session_gid
= session_info
->gid
;
949 session_info_put(session_info
);
956 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
957 const struct lttng_condition
*condition
,
958 struct notification_client
*client
,
959 struct notification_thread_state
*state
)
962 struct lttng_evaluation
*evaluation
= NULL
;
963 struct notification_client_list client_list
= {
964 .lock
= PTHREAD_MUTEX_INITIALIZER
,
966 struct notification_client_list_element client_list_element
= { 0 };
967 uid_t object_uid
= 0;
968 gid_t object_gid
= 0;
975 switch (get_condition_binding_object(condition
)) {
976 case LTTNG_OBJECT_TYPE_SESSION
:
977 ret
= evaluate_session_condition_for_client(condition
, state
,
978 &evaluation
, &object_uid
, &object_gid
);
980 case LTTNG_OBJECT_TYPE_CHANNEL
:
981 ret
= evaluate_channel_condition_for_client(condition
, state
,
982 &evaluation
, &object_uid
, &object_gid
);
984 case LTTNG_OBJECT_TYPE_NONE
:
985 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
988 case LTTNG_OBJECT_TYPE_UNKNOWN
:
998 /* Evaluation yielded nothing. Normal exit. */
999 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
1005 * Create a temporary client list with the client currently
1008 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1009 CDS_INIT_LIST_HEAD(&client_list
.list
);
1010 client_list
.trigger
= trigger
;
1012 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1013 client_list_element
.client
= client
;
1014 cds_list_add(&client_list_element
.node
, &client_list
.list
);
1016 /* Send evaluation result to the newly-subscribed client. */
1017 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1018 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1019 state
, object_uid
, object_gid
);
1026 int notification_thread_client_subscribe(struct notification_client
*client
,
1027 struct lttng_condition
*condition
,
1028 struct notification_thread_state
*state
,
1029 enum lttng_notification_channel_status
*_status
)
1032 struct notification_client_list
*client_list
= NULL
;
1033 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1034 struct notification_client_list_element
*client_list_element
= NULL
;
1035 enum lttng_notification_channel_status status
=
1036 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1039 * Ensure that the client has not already subscribed to this condition
1042 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1043 if (lttng_condition_is_equal(condition_list_element
->condition
,
1045 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1050 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1051 if (!condition_list_element
) {
1055 client_list_element
= zmalloc(sizeof(*client_list_element
));
1056 if (!client_list_element
) {
1062 * Add the newly-subscribed condition to the client's subscription list.
1064 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1065 condition_list_element
->condition
= condition
;
1066 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1068 client_list
= get_client_list_from_condition(state
, condition
);
1071 * No notification-emiting trigger registered with this
1072 * condition. We don't evaluate the condition right away
1073 * since this trigger is not registered yet.
1075 free(client_list_element
);
1080 * The condition to which the client just subscribed is evaluated
1081 * at this point so that conditions that are already TRUE result
1082 * in a notification being sent out.
1084 * The client_list's trigger is used without locking the list itself.
1085 * This is correct since the list doesn't own the trigger and the
1086 * object is immutable.
1088 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1090 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1092 free(client_list_element
);
1097 * Add the client to the list of clients interested in a given trigger
1098 * if a "notification" trigger with a corresponding condition was
1101 client_list_element
->client
= client
;
1102 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1104 pthread_mutex_lock(&client_list
->lock
);
1105 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1106 pthread_mutex_unlock(&client_list
->lock
);
1112 notification_client_list_put(client_list
);
1116 free(condition_list_element
);
1117 free(client_list_element
);
1122 int notification_thread_client_unsubscribe(
1123 struct notification_client
*client
,
1124 struct lttng_condition
*condition
,
1125 struct notification_thread_state
*state
,
1126 enum lttng_notification_channel_status
*_status
)
1128 struct notification_client_list
*client_list
;
1129 struct lttng_condition_list_element
*condition_list_element
,
1131 struct notification_client_list_element
*client_list_element
,
1133 bool condition_found
= false;
1134 enum lttng_notification_channel_status status
=
1135 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1137 /* Remove the condition from the client's condition list. */
1138 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1139 &client
->condition_list
, node
) {
1140 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1145 cds_list_del(&condition_list_element
->node
);
1147 * The caller may be iterating on the client's conditions to
1148 * tear down a client's connection. In this case, the condition
1149 * will be destroyed at the end.
1151 if (condition
!= condition_list_element
->condition
) {
1152 lttng_condition_destroy(
1153 condition_list_element
->condition
);
1155 free(condition_list_element
);
1156 condition_found
= true;
1160 if (!condition_found
) {
1161 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1166 * Remove the client from the list of clients interested the trigger
1167 * matching the condition.
1169 client_list
= get_client_list_from_condition(state
, condition
);
1174 pthread_mutex_lock(&client_list
->lock
);
1175 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1176 &client_list
->list
, node
) {
1177 if (client_list_element
->client
->id
!= client
->id
) {
1180 cds_list_del(&client_list_element
->node
);
1181 free(client_list_element
);
1184 pthread_mutex_unlock(&client_list
->lock
);
1185 notification_client_list_put(client_list
);
1188 lttng_condition_destroy(condition
);
1196 void free_notification_client_rcu(struct rcu_head
*node
)
1198 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1202 void notification_client_destroy(struct notification_client
*client
,
1203 struct notification_thread_state
*state
)
1210 * The client object is not reachable by other threads, no need to lock
1213 if (client
->socket
>= 0) {
1214 (void) lttcomm_close_unix_sock(client
->socket
);
1215 client
->socket
= -1;
1217 client
->communication
.active
= false;
1218 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1219 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1220 pthread_mutex_destroy(&client
->lock
);
1221 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1225 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1229 struct notification_client
*get_client_from_socket(int socket
,
1230 struct notification_thread_state
*state
)
1232 struct cds_lfht_iter iter
;
1233 struct cds_lfht_node
*node
;
1234 struct notification_client
*client
= NULL
;
1236 cds_lfht_lookup(state
->client_socket_ht
,
1237 hash_client_socket(socket
),
1238 match_client_socket
,
1239 (void *) (unsigned long) socket
,
1241 node
= cds_lfht_iter_get_node(&iter
);
1246 client
= caa_container_of(node
, struct notification_client
,
1247 client_socket_ht_node
);
1253 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1257 struct notification_client
*get_client_from_id(notification_client_id id
,
1258 struct notification_thread_state
*state
)
1260 struct cds_lfht_iter iter
;
1261 struct cds_lfht_node
*node
;
1262 struct notification_client
*client
= NULL
;
1264 cds_lfht_lookup(state
->client_id_ht
,
1269 node
= cds_lfht_iter_get_node(&iter
);
1274 client
= caa_container_of(node
, struct notification_client
,
1281 bool buffer_usage_condition_applies_to_channel(
1282 const struct lttng_condition
*condition
,
1283 const struct channel_info
*channel_info
)
1285 enum lttng_condition_status status
;
1286 enum lttng_domain_type condition_domain
;
1287 const char *condition_session_name
= NULL
;
1288 const char *condition_channel_name
= NULL
;
1290 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1292 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1293 if (channel_info
->key
.domain
!= condition_domain
) {
1297 status
= lttng_condition_buffer_usage_get_session_name(
1298 condition
, &condition_session_name
);
1299 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1301 status
= lttng_condition_buffer_usage_get_channel_name(
1302 condition
, &condition_channel_name
);
1303 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1305 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1308 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1318 bool session_consumed_size_condition_applies_to_channel(
1319 const struct lttng_condition
*condition
,
1320 const struct channel_info
*channel_info
)
1322 enum lttng_condition_status status
;
1323 const char *condition_session_name
= NULL
;
1325 status
= lttng_condition_session_consumed_size_get_session_name(
1326 condition
, &condition_session_name
);
1327 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1329 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1339 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1340 const struct channel_info
*channel_info
)
1342 const struct lttng_condition
*condition
;
1343 bool trigger_applies
;
1345 condition
= lttng_trigger_get_const_condition(trigger
);
1350 switch (lttng_condition_get_type(condition
)) {
1351 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1352 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1353 trigger_applies
= buffer_usage_condition_applies_to_channel(
1354 condition
, channel_info
);
1356 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1357 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1358 condition
, channel_info
);
1364 return trigger_applies
;
1370 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1371 struct notification_client
*client
)
1373 bool applies
= false;
1374 struct lttng_condition_list_element
*condition_list_element
;
1376 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1378 applies
= lttng_condition_is_equal(
1379 condition_list_element
->condition
,
1380 lttng_trigger_get_condition(trigger
));
1388 /* Must be called with RCU read lock held. */
1390 struct lttng_session_trigger_list
*get_session_trigger_list(
1391 struct notification_thread_state
*state
,
1392 const char *session_name
)
1394 struct lttng_session_trigger_list
*list
= NULL
;
1395 struct cds_lfht_node
*node
;
1396 struct cds_lfht_iter iter
;
1398 cds_lfht_lookup(state
->session_triggers_ht
,
1399 hash_key_str(session_name
, lttng_ht_seed
),
1400 match_session_trigger_list
,
1403 node
= cds_lfht_iter_get_node(&iter
);
1406 * Not an error, the list of triggers applying to that session
1407 * will be initialized when the session is created.
1409 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1414 list
= caa_container_of(node
,
1415 struct lttng_session_trigger_list
,
1416 session_triggers_ht_node
);
1422 * Allocate an empty lttng_session_trigger_list for the session named
1425 * No ownership of 'session_name' is assumed by the session trigger list.
1426 * It is the caller's responsability to ensure the session name is alive
1427 * for as long as this list is.
1430 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1431 const char *session_name
,
1432 struct cds_lfht
*session_triggers_ht
)
1434 struct lttng_session_trigger_list
*list
;
1436 list
= zmalloc(sizeof(*list
));
1440 list
->session_name
= session_name
;
1441 CDS_INIT_LIST_HEAD(&list
->list
);
1442 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1443 list
->session_triggers_ht
= session_triggers_ht
;
1446 /* Publish the list through the session_triggers_ht. */
1447 cds_lfht_add(session_triggers_ht
,
1448 hash_key_str(session_name
, lttng_ht_seed
),
1449 &list
->session_triggers_ht_node
);
1456 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1458 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1463 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1465 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1467 /* Empty the list element by element, and then free the list itself. */
1468 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1469 &list
->list
, node
) {
1470 cds_list_del(&trigger_list_element
->node
);
1471 free(trigger_list_element
);
1474 /* Unpublish the list from the session_triggers_ht. */
1475 cds_lfht_del(list
->session_triggers_ht
,
1476 &list
->session_triggers_ht_node
);
1478 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1482 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1483 struct lttng_trigger
*trigger
)
1486 struct lttng_trigger_list_element
*new_element
=
1487 zmalloc(sizeof(*new_element
));
1493 CDS_INIT_LIST_HEAD(&new_element
->node
);
1494 new_element
->trigger
= trigger
;
1495 cds_list_add(&new_element
->node
, &list
->list
);
1501 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1502 const char *session_name
)
1504 bool applies
= false;
1505 const struct lttng_condition
*condition
;
1507 condition
= lttng_trigger_get_const_condition(trigger
);
1508 switch (lttng_condition_get_type(condition
)) {
1509 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1510 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1512 enum lttng_condition_status condition_status
;
1513 const char *condition_session_name
;
1515 condition_status
= lttng_condition_session_rotation_get_session_name(
1516 condition
, &condition_session_name
);
1517 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1518 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1522 assert(condition_session_name
);
1523 applies
= !strcmp(condition_session_name
, session_name
);
1534 * Allocate and initialize an lttng_session_trigger_list which contains
1535 * all triggers that apply to the session named 'session_name'.
1537 * No ownership of 'session_name' is assumed by the session trigger list.
1538 * It is the caller's responsability to ensure the session name is alive
1539 * for as long as this list is.
1542 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1543 const struct notification_thread_state
*state
,
1544 const char *session_name
)
1546 int trigger_count
= 0;
1547 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1548 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1549 struct cds_lfht_iter iter
;
1551 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1552 state
->session_triggers_ht
);
1554 /* Add all triggers applying to the session named 'session_name'. */
1555 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1559 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1564 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1565 trigger_ht_element
->trigger
);
1573 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1575 return session_trigger_list
;
1577 lttng_session_trigger_list_destroy(session_trigger_list
);
1582 struct session_info
*find_or_create_session_info(
1583 struct notification_thread_state
*state
,
1584 const char *name
, uid_t uid
, gid_t gid
)
1586 struct session_info
*session
= NULL
;
1587 struct cds_lfht_node
*node
;
1588 struct cds_lfht_iter iter
;
1589 struct lttng_session_trigger_list
*trigger_list
;
1592 cds_lfht_lookup(state
->sessions_ht
,
1593 hash_key_str(name
, lttng_ht_seed
),
1597 node
= cds_lfht_iter_get_node(&iter
);
1599 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1601 session
= caa_container_of(node
, struct session_info
,
1603 assert(session
->uid
== uid
);
1604 assert(session
->gid
== gid
);
1605 session_info_get(session
);
1609 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1610 if (!trigger_list
) {
1614 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1615 state
->sessions_ht
);
1617 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1619 lttng_session_trigger_list_destroy(trigger_list
);
1622 trigger_list
= NULL
;
1624 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1625 &session
->sessions_ht_node
);
1631 session_info_put(session
);
1636 int handle_notification_thread_command_add_channel(
1637 struct notification_thread_state
*state
,
1638 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1639 const char *channel_name
, enum lttng_domain_type channel_domain
,
1640 uint64_t channel_key_int
, uint64_t channel_capacity
,
1641 enum lttng_error_code
*cmd_result
)
1643 struct cds_list_head trigger_list
;
1644 struct channel_info
*new_channel_info
= NULL
;
1645 struct channel_key channel_key
= {
1646 .key
= channel_key_int
,
1647 .domain
= channel_domain
,
1649 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1650 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1651 int trigger_count
= 0;
1652 struct cds_lfht_iter iter
;
1653 struct session_info
*session_info
= NULL
;
1655 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1656 channel_name
, session_name
, channel_key_int
,
1657 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1659 CDS_INIT_LIST_HEAD(&trigger_list
);
1661 session_info
= find_or_create_session_info(state
, session_name
,
1662 session_uid
, session_gid
);
1663 if (!session_info
) {
1664 /* Allocation error or an internal error occurred. */
1668 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1669 channel_capacity
, session_info
);
1670 if (!new_channel_info
) {
1675 /* Build a list of all triggers applying to the new channel. */
1676 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1678 struct lttng_trigger_list_element
*new_element
;
1680 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1681 new_channel_info
)) {
1685 new_element
= zmalloc(sizeof(*new_element
));
1690 CDS_INIT_LIST_HEAD(&new_element
->node
);
1691 new_element
->trigger
= trigger_ht_element
->trigger
;
1692 cds_list_add(&new_element
->node
, &trigger_list
);
1697 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1699 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1700 if (!channel_trigger_list
) {
1703 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1704 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1705 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1706 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1709 /* Add channel to the channel_ht which owns the channel_infos. */
1710 cds_lfht_add(state
->channels_ht
,
1711 hash_channel_key(&new_channel_info
->key
),
1712 &new_channel_info
->channels_ht_node
);
1714 * Add the list of triggers associated with this channel to the
1715 * channel_triggers_ht.
1717 cds_lfht_add(state
->channel_triggers_ht
,
1718 hash_channel_key(&new_channel_info
->key
),
1719 &channel_trigger_list
->channel_triggers_ht_node
);
1721 session_info_put(session_info
);
1722 *cmd_result
= LTTNG_OK
;
1725 channel_info_destroy(new_channel_info
);
1726 session_info_put(session_info
);
1731 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1733 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1738 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1740 free(caa_container_of(node
, struct channel_state_sample
,
1745 int handle_notification_thread_command_remove_channel(
1746 struct notification_thread_state
*state
,
1747 uint64_t channel_key
, enum lttng_domain_type domain
,
1748 enum lttng_error_code
*cmd_result
)
1750 struct cds_lfht_node
*node
;
1751 struct cds_lfht_iter iter
;
1752 struct lttng_channel_trigger_list
*trigger_list
;
1753 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1754 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1755 struct channel_info
*channel_info
;
1757 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1758 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1762 cds_lfht_lookup(state
->channel_triggers_ht
,
1763 hash_channel_key(&key
),
1764 match_channel_trigger_list
,
1767 node
= cds_lfht_iter_get_node(&iter
);
1769 * There is a severe internal error if we are being asked to remove a
1770 * channel that doesn't exist.
1773 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1777 /* Free the list of triggers associated with this channel. */
1778 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1779 channel_triggers_ht_node
);
1780 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1781 &trigger_list
->list
, node
) {
1782 cds_list_del(&trigger_list_element
->node
);
1783 free(trigger_list_element
);
1785 cds_lfht_del(state
->channel_triggers_ht
, node
);
1786 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1788 /* Free sampled channel state. */
1789 cds_lfht_lookup(state
->channel_state_ht
,
1790 hash_channel_key(&key
),
1791 match_channel_state_sample
,
1794 node
= cds_lfht_iter_get_node(&iter
);
1796 * This is expected to be NULL if the channel is destroyed before we
1797 * received a sample.
1800 struct channel_state_sample
*sample
= caa_container_of(node
,
1801 struct channel_state_sample
,
1802 channel_state_ht_node
);
1804 cds_lfht_del(state
->channel_state_ht
, node
);
1805 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1808 /* Remove the channel from the channels_ht and free it. */
1809 cds_lfht_lookup(state
->channels_ht
,
1810 hash_channel_key(&key
),
1814 node
= cds_lfht_iter_get_node(&iter
);
1816 channel_info
= caa_container_of(node
, struct channel_info
,
1818 cds_lfht_del(state
->channels_ht
, node
);
1819 channel_info_destroy(channel_info
);
1822 *cmd_result
= LTTNG_OK
;
1827 int handle_notification_thread_command_session_rotation(
1828 struct notification_thread_state
*state
,
1829 enum notification_thread_command_type cmd_type
,
1830 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1831 uint64_t trace_archive_chunk_id
,
1832 struct lttng_trace_archive_location
*location
,
1833 enum lttng_error_code
*_cmd_result
)
1836 enum lttng_error_code cmd_result
= LTTNG_OK
;
1837 struct lttng_session_trigger_list
*trigger_list
;
1838 struct lttng_trigger_list_element
*trigger_list_element
;
1839 struct session_info
*session_info
;
1840 const struct lttng_credentials session_creds
= {
1841 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1842 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1847 session_info
= find_or_create_session_info(state
, session_name
,
1848 session_uid
, session_gid
);
1849 if (!session_info
) {
1850 /* Allocation error or an internal error occurred. */
1852 cmd_result
= LTTNG_ERR_NOMEM
;
1856 session_info
->rotation
.ongoing
=
1857 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1858 session_info
->rotation
.id
= trace_archive_chunk_id
;
1859 trigger_list
= get_session_trigger_list(state
, session_name
);
1860 if (!trigger_list
) {
1861 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1866 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1868 const struct lttng_condition
*condition
;
1869 struct lttng_trigger
*trigger
;
1870 struct notification_client_list
*client_list
;
1871 struct lttng_evaluation
*evaluation
= NULL
;
1872 enum lttng_condition_type condition_type
;
1873 enum action_executor_status executor_status
;
1875 trigger
= trigger_list_element
->trigger
;
1876 condition
= lttng_trigger_get_const_condition(trigger
);
1878 condition_type
= lttng_condition_get_type(condition
);
1880 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1881 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1883 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1884 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1888 client_list
= get_client_list_from_condition(state
, condition
);
1889 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1890 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1891 trace_archive_chunk_id
);
1893 evaluation
= lttng_evaluation_session_rotation_completed_create(
1894 trace_archive_chunk_id
, location
);
1898 /* Internal error */
1900 cmd_result
= LTTNG_ERR_UNK
;
1905 * Ownership of `evaluation` transferred to the action executor
1906 * no matter the result.
1908 executor_status
= action_executor_enqueue(state
->executor
,
1909 trigger
, evaluation
, &session_creds
,
1912 switch (executor_status
) {
1913 case ACTION_EXECUTOR_STATUS_OK
:
1915 case ACTION_EXECUTOR_STATUS_ERROR
:
1916 case ACTION_EXECUTOR_STATUS_INVALID
:
1918 * TODO Add trigger identification (name/id) when
1919 * it is added to the API.
1921 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1924 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1926 * TODO Add trigger identification (name/id) when
1927 * it is added to the API.
1929 * Not a fatal error.
1931 WARN("No space left when enqueuing action associated with session rotation trigger");
1939 notification_client_list_put(client_list
);
1940 if (caa_unlikely(ret
)) {
1945 session_info_put(session_info
);
1946 *_cmd_result
= cmd_result
;
1952 int handle_notification_thread_command_add_tracer_event_source(
1953 struct notification_thread_state
*state
,
1954 int tracer_event_source_fd
,
1955 enum lttng_domain_type domain_type
,
1956 enum lttng_error_code
*_cmd_result
)
1959 enum lttng_error_code cmd_result
= LTTNG_OK
;
1960 struct notification_event_tracer_event_source_element
*element
= NULL
;
1962 element
= zmalloc(sizeof(*element
));
1964 cmd_result
= LTTNG_ERR_NOMEM
;
1969 element
->fd
= tracer_event_source_fd
;
1970 element
->domain
= domain_type
;
1972 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
1974 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
1975 tracer_event_source_fd
,
1976 lttng_domain_type_str(domain_type
));
1978 /* Adding the read side pipe to the event poll. */
1979 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
1981 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
1982 tracer_event_source_fd
,
1983 lttng_domain_type_str(element
->domain
));
1984 cds_list_del(&element
->node
);
1989 element
->is_fd_in_poll_set
= true;
1992 *_cmd_result
= cmd_result
;
1997 int handle_notification_thread_command_remove_tracer_event_source(
1998 struct notification_thread_state
*state
,
1999 int tracer_event_source_fd
,
2000 enum lttng_error_code
*_cmd_result
)
2004 enum lttng_error_code cmd_result
= LTTNG_OK
;
2005 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
2007 cds_list_for_each_entry_safe(source_element
, tmp
,
2008 &state
->tracer_event_sources_list
, node
) {
2009 if (source_element
->fd
!= tracer_event_source_fd
) {
2013 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2014 tracer_event_source_fd
,
2015 lttng_domain_type_str(source_element
->domain
));
2016 cds_list_del(&source_element
->node
);
2023 * This is temporarily allowed since the poll activity set is
2024 * not properly cleaned-up for the moment. This is adressed in
2027 source_element
= NULL
;
2031 if (!source_element
->is_fd_in_poll_set
) {
2032 /* Skip the poll set removal. */
2036 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2037 tracer_event_source_fd
,
2038 lttng_domain_type_str(source_element
->domain
));
2040 /* Removing the fd from the event poll set. */
2041 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2043 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2044 tracer_event_source_fd
,
2045 lttng_domain_type_str(source_element
->domain
));
2046 cmd_result
= LTTNG_ERR_FATAL
;
2050 source_element
->is_fd_in_poll_set
= false;
2053 free(source_element
);
2054 *_cmd_result
= cmd_result
;
2058 int handle_notification_thread_remove_tracer_event_source_no_result(
2059 struct notification_thread_state
*state
,
2060 int tracer_event_source_fd
)
2063 enum lttng_error_code cmd_result
;
2065 ret
= handle_notification_thread_command_remove_tracer_event_source(
2066 state
, tracer_event_source_fd
, &cmd_result
);
2071 static int handle_notification_thread_command_list_triggers(
2072 struct notification_thread_handle
*handle
,
2073 struct notification_thread_state
*state
,
2075 struct lttng_triggers
**triggers
,
2076 enum lttng_error_code
*_cmd_result
)
2079 enum lttng_error_code cmd_result
= LTTNG_OK
;
2080 struct cds_lfht_iter iter
;
2081 struct lttng_trigger_ht_element
*trigger_ht_element
;
2082 struct lttng_triggers
*local_triggers
= NULL
;
2083 const struct lttng_credentials
*creds
;
2087 local_triggers
= lttng_triggers_create();
2088 if (!local_triggers
) {
2089 /* Not a fatal error. */
2090 cmd_result
= LTTNG_ERR_NOMEM
;
2094 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2095 trigger_ht_element
, node
) {
2097 * Only return the triggers to which the client has access.
2098 * The root user has visibility over all triggers.
2100 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2101 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2105 ret
= lttng_triggers_add(local_triggers
,
2106 trigger_ht_element
->trigger
);
2108 /* Not a fatal error. */
2110 cmd_result
= LTTNG_ERR_NOMEM
;
2115 /* Transferring ownership to the caller. */
2116 *triggers
= local_triggers
;
2117 local_triggers
= NULL
;
2121 lttng_triggers_destroy(local_triggers
);
2122 *_cmd_result
= cmd_result
;
2127 bool condition_is_supported(struct lttng_condition
*condition
)
2131 switch (lttng_condition_get_type(condition
)) {
2132 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2133 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2136 enum lttng_domain_type domain
;
2138 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2142 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2143 is_supported
= true;
2148 * Older kernel tracers don't expose the API to monitor their
2149 * buffers. Therefore, we reject triggers that require that
2150 * mechanism to be available to be evaluated.
2152 * Assume unsupported on error.
2154 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2157 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
2159 const struct lttng_event_rule
*event_rule
;
2160 enum lttng_domain_type domain
;
2161 const enum lttng_condition_status status
=
2162 lttng_condition_event_rule_get_rule(
2163 condition
, &event_rule
);
2165 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2167 domain
= lttng_event_rule_get_domain_type(event_rule
);
2168 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2169 is_supported
= true;
2174 * Older kernel tracers can't emit notification. Therefore, we
2175 * reject triggers that require that mechanism to be available
2178 * Assume unsupported on error.
2180 is_supported
= kernel_supports_event_notifiers() == 1;
2184 is_supported
= true;
2187 return is_supported
;
2190 /* Must be called with RCU read lock held. */
2192 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2193 struct notification_thread_state
*state
)
2196 const struct lttng_condition
*condition
;
2197 const char *session_name
;
2198 struct lttng_session_trigger_list
*trigger_list
;
2200 condition
= lttng_trigger_get_const_condition(trigger
);
2201 switch (lttng_condition_get_type(condition
)) {
2202 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2203 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2205 enum lttng_condition_status status
;
2207 status
= lttng_condition_session_rotation_get_session_name(
2208 condition
, &session_name
);
2209 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2210 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2221 trigger_list
= get_session_trigger_list(state
, session_name
);
2222 if (!trigger_list
) {
2223 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2229 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2231 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2236 /* Must be called with RCU read lock held. */
2238 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2239 struct notification_thread_state
*state
)
2242 struct cds_lfht_node
*node
;
2243 struct cds_lfht_iter iter
;
2244 struct channel_info
*channel
;
2246 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2248 struct lttng_trigger_list_element
*trigger_list_element
;
2249 struct lttng_channel_trigger_list
*trigger_list
;
2250 struct cds_lfht_iter lookup_iter
;
2252 if (!trigger_applies_to_channel(trigger
, channel
)) {
2256 cds_lfht_lookup(state
->channel_triggers_ht
,
2257 hash_channel_key(&channel
->key
),
2258 match_channel_trigger_list
,
2261 node
= cds_lfht_iter_get_node(&lookup_iter
);
2263 trigger_list
= caa_container_of(node
,
2264 struct lttng_channel_trigger_list
,
2265 channel_triggers_ht_node
);
2267 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2268 if (!trigger_list_element
) {
2272 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2273 trigger_list_element
->trigger
= trigger
;
2274 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2275 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2283 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2285 bool is_notify
= false;
2286 unsigned int i
, count
;
2287 enum lttng_action_status action_status
;
2288 const struct lttng_action
*action
=
2289 lttng_trigger_get_const_action(trigger
);
2290 enum lttng_action_type action_type
;
2293 action_type
= lttng_action_get_type(action
);
2294 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2297 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2301 action_status
= lttng_action_group_get_count(action
, &count
);
2302 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2304 for (i
= 0; i
< count
; i
++) {
2305 const struct lttng_action
*inner_action
=
2306 lttng_action_group_get_at_index(
2309 action_type
= lttng_action_get_type(inner_action
);
2310 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2320 static bool trigger_name_taken(struct notification_thread_state
*state
,
2321 const struct lttng_trigger
*trigger
)
2323 struct cds_lfht_iter iter
;
2326 * No duplicata is allowed in the triggers_by_name_uid_ht.
2327 * The match is done against the trigger name and uid.
2329 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2330 hash_trigger_by_name_uid(trigger
),
2331 match_trigger_by_name_uid
,
2334 return !!cds_lfht_iter_get_node(&iter
);
2338 enum lttng_error_code
generate_trigger_name(
2339 struct notification_thread_state
*state
,
2340 struct lttng_trigger
*trigger
, const char **name
)
2342 enum lttng_error_code ret_code
= LTTNG_OK
;
2344 enum lttng_trigger_status status
;
2347 const int ret
= lttng_trigger_generate_name(trigger
,
2348 state
->trigger_id
.name_offset
++);
2350 /* The only reason this can fail right now. */
2351 ret_code
= LTTNG_ERR_NOMEM
;
2355 status
= lttng_trigger_get_name(trigger
, name
);
2356 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2358 taken
= trigger_name_taken(state
, trigger
);
2359 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2365 * FIXME A client's credentials are not checked when registering a trigger.
2367 * The effects of this are benign since:
2368 * - The client will succeed in registering the trigger, as it is valid,
2369 * - The trigger will, internally, be bound to the channel/session,
2370 * - The notifications will not be sent since the client's credentials
2371 * are checked against the channel at that moment.
2373 * If this function returns a non-zero value, it means something is
2374 * fundamentally broken and the whole subsystem/thread will be torn down.
2376 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2380 int handle_notification_thread_command_register_trigger(
2381 struct notification_thread_state
*state
,
2382 struct lttng_trigger
*trigger
,
2383 enum lttng_error_code
*cmd_result
)
2386 struct lttng_condition
*condition
;
2387 struct notification_client
*client
;
2388 struct notification_client_list
*client_list
= NULL
;
2389 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2390 struct notification_client_list_element
*client_list_element
;
2391 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2392 struct cds_lfht_node
*node
;
2393 struct cds_lfht_iter iter
;
2394 const char* trigger_name
;
2395 bool free_trigger
= true;
2396 struct lttng_evaluation
*evaluation
= NULL
;
2397 struct lttng_credentials object_creds
;
2400 enum action_executor_status executor_status
;
2401 const uint64_t trigger_tracer_token
=
2402 state
->trigger_id
.next_tracer_token
++;
2406 /* Set the trigger's tracer token. */
2407 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2409 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2410 LTTNG_TRIGGER_STATUS_UNSET
) {
2411 const enum lttng_error_code ret_code
= generate_trigger_name(
2412 state
, trigger
, &trigger_name
);
2414 if (ret_code
!= LTTNG_OK
) {
2417 *cmd_result
= ret_code
;
2420 } else if (trigger_name_taken(state
, trigger
)) {
2421 /* Not a fatal error. */
2422 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2427 condition
= lttng_trigger_get_condition(trigger
);
2430 /* Some conditions require tracers to implement a minimal ABI version. */
2431 if (!condition_is_supported(condition
)) {
2432 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2436 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2437 if (!trigger_ht_element
) {
2442 /* Add trigger to the trigger_ht. */
2443 cds_lfht_node_init(&trigger_ht_element
->node
);
2444 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2445 trigger_ht_element
->trigger
= trigger
;
2447 node
= cds_lfht_add_unique(state
->triggers_ht
,
2448 lttng_condition_hash(condition
),
2451 &trigger_ht_element
->node
);
2452 if (node
!= &trigger_ht_element
->node
) {
2453 /* Not a fatal error, simply report it to the client. */
2454 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2455 goto error_free_ht_element
;
2458 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2459 hash_trigger_by_name_uid(trigger
),
2460 match_trigger_by_name_uid
,
2462 &trigger_ht_element
->node_by_name_uid
);
2463 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2464 /* Not a fatal error, simply report it to the client. */
2465 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2466 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2467 goto error_free_ht_element
;
2470 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2471 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2472 if (!trigger_tokens_ht_element
) {
2475 cds_lfht_del(state
->triggers_ht
,
2476 &trigger_ht_element
->node
);
2477 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2478 &trigger_ht_element
->node_by_name_uid
);
2479 goto error_free_ht_element
;
2482 /* Add trigger token to the trigger_tokens_ht. */
2483 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2484 trigger_tokens_ht_element
->token
=
2485 LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2486 trigger_tokens_ht_element
->trigger
= trigger
;
2488 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2489 hash_key_u64(&trigger_tokens_ht_element
->token
,
2491 match_trigger_token
,
2492 &trigger_tokens_ht_element
->token
,
2493 &trigger_tokens_ht_element
->node
);
2494 if (node
!= &trigger_tokens_ht_element
->node
) {
2495 /* Internal corruption, fatal error. */
2497 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2498 cds_lfht_del(state
->triggers_ht
,
2499 &trigger_ht_element
->node
);
2500 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2501 &trigger_ht_element
->node_by_name_uid
);
2502 goto error_free_ht_element
;
2507 * Ownership of the trigger and of its wrapper was transfered to
2508 * the triggers_ht. Same for token ht element if necessary.
2510 trigger_tokens_ht_element
= NULL
;
2511 trigger_ht_element
= NULL
;
2512 free_trigger
= false;
2515 * The rest only applies to triggers that have a "notify" action.
2516 * It is not skipped as this is the only action type currently
2519 if (is_trigger_action_notify(trigger
)) {
2520 client_list
= notification_client_list_create(trigger
);
2523 goto error_free_ht_element
;
2526 /* Build a list of clients to which this new trigger applies. */
2527 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2528 client_socket_ht_node
) {
2529 if (!trigger_applies_to_client(trigger
, client
)) {
2533 client_list_element
=
2534 zmalloc(sizeof(*client_list_element
));
2535 if (!client_list_element
) {
2537 goto error_put_client_list
;
2540 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2541 client_list_element
->client
= client
;
2542 cds_list_add(&client_list_element
->node
,
2543 &client_list
->list
);
2547 * Client list ownership transferred to the
2548 * notification_trigger_clients_ht.
2550 publish_notification_client_list(state
, client_list
);
2553 switch (get_condition_binding_object(condition
)) {
2554 case LTTNG_OBJECT_TYPE_SESSION
:
2555 /* Add the trigger to the list if it matches a known session. */
2556 ret
= bind_trigger_to_matching_session(trigger
, state
);
2558 goto error_put_client_list
;
2561 case LTTNG_OBJECT_TYPE_CHANNEL
:
2563 * Add the trigger to list of triggers bound to the channels
2566 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2568 goto error_put_client_list
;
2571 case LTTNG_OBJECT_TYPE_NONE
:
2574 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2576 goto error_put_client_list
;
2580 * The new trigger's condition must be evaluated against the current
2583 * In the case of `notify` action, nothing preventing clients from
2584 * subscribing to a condition before the corresponding trigger is
2585 * registered, we have to evaluate this new condition right away.
2587 * At some point, we were waiting for the next "evaluation" (e.g. on
2588 * reception of a channel sample) to evaluate this new condition, but
2591 * The reason it was broken is that waiting for the next sample
2592 * does not allow us to properly handle transitions for edge-triggered
2595 * Consider this example: when we handle a new channel sample, we
2596 * evaluate each conditions twice: once with the previous state, and
2597 * again with the newest state. We then use those two results to
2598 * determine whether a state change happened: a condition was false and
2599 * became true. If a state change happened, we have to notify clients.
2601 * Now, if a client subscribes to a given notification and registers
2602 * a trigger *after* that subscription, we have to make sure the
2603 * condition is evaluated at this point while considering only the
2604 * current state. Otherwise, the next evaluation cycle may only see
2605 * that the evaluations remain the same (true for samples n-1 and n) and
2606 * the client will never know that the condition has been met.
2608 switch (get_condition_binding_object(condition
)) {
2609 case LTTNG_OBJECT_TYPE_SESSION
:
2610 ret
= evaluate_session_condition_for_client(condition
, state
,
2611 &evaluation
, &object_uid
,
2613 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2614 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2616 case LTTNG_OBJECT_TYPE_CHANNEL
:
2617 ret
= evaluate_channel_condition_for_client(condition
, state
,
2618 &evaluation
, &object_uid
,
2620 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2621 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2623 case LTTNG_OBJECT_TYPE_NONE
:
2626 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2634 goto error_put_client_list
;
2637 DBG("Newly registered trigger's condition evaluated to %s",
2638 evaluation
? "true" : "false");
2640 /* Evaluation yielded nothing. Normal exit. */
2646 * Ownership of `evaluation` transferred to the action executor
2647 * no matter the result.
2649 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2650 evaluation
, &object_creds
, client_list
);
2652 switch (executor_status
) {
2653 case ACTION_EXECUTOR_STATUS_OK
:
2655 case ACTION_EXECUTOR_STATUS_ERROR
:
2656 case ACTION_EXECUTOR_STATUS_INVALID
:
2658 * TODO Add trigger identification (name/id) when
2659 * it is added to the API.
2661 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2663 goto error_put_client_list
;
2664 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2666 * TODO Add trigger identification (name/id) when
2667 * it is added to the API.
2669 * Not a fatal error.
2671 WARN("No space left when enqueuing action associated to newly registered trigger");
2679 *cmd_result
= LTTNG_OK
;
2680 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2681 trigger_name
, trigger_tracer_token
);
2683 error_put_client_list
:
2684 notification_client_list_put(client_list
);
2686 error_free_ht_element
:
2687 if (trigger_ht_element
) {
2688 /* Delayed removal due to RCU constraint on delete. */
2689 call_rcu(&trigger_ht_element
->rcu_node
,
2690 free_lttng_trigger_ht_element_rcu
);
2693 free(trigger_tokens_ht_element
);
2696 lttng_trigger_destroy(trigger
);
2703 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2705 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2710 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2712 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2717 int handle_notification_thread_command_unregister_trigger(
2718 struct notification_thread_state
*state
,
2719 const struct lttng_trigger
*trigger
,
2720 enum lttng_error_code
*_cmd_reply
)
2722 struct cds_lfht_iter iter
;
2723 struct cds_lfht_node
*triggers_ht_node
;
2724 struct lttng_channel_trigger_list
*trigger_list
;
2725 struct notification_client_list
*client_list
;
2726 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2727 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
2729 enum lttng_error_code cmd_reply
;
2733 cds_lfht_lookup(state
->triggers_ht
,
2734 lttng_condition_hash(condition
),
2738 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2739 if (!triggers_ht_node
) {
2740 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2743 cmd_reply
= LTTNG_OK
;
2746 /* Remove trigger from channel_triggers_ht. */
2747 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2748 channel_triggers_ht_node
) {
2749 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2751 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2752 &trigger_list
->list
, node
) {
2753 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2757 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2758 cds_list_del(&trigger_element
->node
);
2759 /* A trigger can only appear once per channel */
2764 if (lttng_condition_get_type(condition
) ==
2765 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2766 struct notification_trigger_tokens_ht_element
2767 *trigger_tokens_ht_element
;
2769 cds_lfht_for_each_entry (state
->trigger_tokens_ht
, &iter
,
2770 trigger_tokens_ht_element
, node
) {
2771 if (!lttng_trigger_is_equal(trigger
,
2772 trigger_tokens_ht_element
->trigger
)) {
2776 DBG("[notification-thread] Removed trigger from tokens_ht");
2777 cds_lfht_del(state
->trigger_tokens_ht
,
2778 &trigger_tokens_ht_element
->node
);
2779 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2780 free_notification_trigger_tokens_ht_element_rcu
);
2786 if (is_trigger_action_notify(trigger
)) {
2788 * Remove and release the client list from
2789 * notification_trigger_clients_ht.
2791 client_list
= get_client_list_from_condition(state
, condition
);
2792 assert(client_list
);
2794 /* Put new reference and the hashtable's reference. */
2795 notification_client_list_put(client_list
);
2796 notification_client_list_put(client_list
);
2800 /* Remove trigger from triggers_ht. */
2801 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2802 struct lttng_trigger_ht_element
, node
);
2803 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2804 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2806 /* Release the ownership of the trigger. */
2807 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2808 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2812 *_cmd_reply
= cmd_reply
;
2817 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2818 int handle_notification_thread_command(
2819 struct notification_thread_handle
*handle
,
2820 struct notification_thread_state
*state
)
2824 struct notification_thread_command
*cmd
;
2826 /* Read the event pipe to put it back into a quiescent state. */
2827 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2829 if (ret
!= sizeof(counter
)) {
2833 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2834 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2835 struct notification_thread_command
, cmd_list_node
);
2836 cds_list_del(&cmd
->cmd_list_node
);
2837 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2839 DBG("[notification-thread] Received `%s` command",
2840 notification_command_type_str(cmd
->type
));
2841 switch (cmd
->type
) {
2842 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2843 ret
= handle_notification_thread_command_register_trigger(state
,
2844 cmd
->parameters
.register_trigger
.trigger
,
2847 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2848 ret
= handle_notification_thread_command_unregister_trigger(
2850 cmd
->parameters
.unregister_trigger
.trigger
,
2853 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2854 ret
= handle_notification_thread_command_add_channel(
2856 cmd
->parameters
.add_channel
.session
.name
,
2857 cmd
->parameters
.add_channel
.session
.uid
,
2858 cmd
->parameters
.add_channel
.session
.gid
,
2859 cmd
->parameters
.add_channel
.channel
.name
,
2860 cmd
->parameters
.add_channel
.channel
.domain
,
2861 cmd
->parameters
.add_channel
.channel
.key
,
2862 cmd
->parameters
.add_channel
.channel
.capacity
,
2865 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2866 ret
= handle_notification_thread_command_remove_channel(
2867 state
, cmd
->parameters
.remove_channel
.key
,
2868 cmd
->parameters
.remove_channel
.domain
,
2871 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2872 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2873 ret
= handle_notification_thread_command_session_rotation(
2876 cmd
->parameters
.session_rotation
.session_name
,
2877 cmd
->parameters
.session_rotation
.uid
,
2878 cmd
->parameters
.session_rotation
.gid
,
2879 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2880 cmd
->parameters
.session_rotation
.location
,
2883 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
2884 ret
= handle_notification_thread_command_add_tracer_event_source(
2886 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2887 cmd
->parameters
.tracer_event_source
.domain
,
2890 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
2891 ret
= handle_notification_thread_command_remove_tracer_event_source(
2893 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2896 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2898 struct lttng_triggers
*triggers
= NULL
;
2900 ret
= handle_notification_thread_command_list_triggers(
2903 cmd
->parameters
.list_triggers
.uid
,
2906 cmd
->reply
.list_triggers
.triggers
= triggers
;
2910 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2911 cmd
->reply_code
= LTTNG_OK
;
2914 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2916 const enum client_transmission_status client_status
=
2917 cmd
->parameters
.client_communication_update
2919 const notification_client_id client_id
=
2920 cmd
->parameters
.client_communication_update
.id
;
2921 struct notification_client
*client
;
2924 client
= get_client_from_id(client_id
, state
);
2928 * Client error was probably already picked-up by the
2929 * notification thread or it has disconnected
2930 * gracefully while this command was queued.
2932 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2936 ret
= client_handle_transmission_status(
2937 client
, client_status
, state
);
2943 ERR("[notification-thread] Unknown internal command received");
2951 if (cmd
->is_async
) {
2955 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2959 /* Wake-up and return a fatal error to the calling thread. */
2960 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2961 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2963 /* Indicate a fatal error to the caller. */
2968 int socket_set_non_blocking(int socket
)
2972 /* Set the pipe as non-blocking. */
2973 ret
= fcntl(socket
, F_GETFL
, 0);
2975 PERROR("fcntl get socket flags");
2980 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2982 PERROR("fcntl set O_NONBLOCK socket flag");
2985 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2991 int client_reset_inbound_state(struct notification_client
*client
)
2996 lttng_payload_clear(&client
->communication
.inbound
.payload
);
2998 client
->communication
.inbound
.bytes_to_receive
=
2999 sizeof(struct lttng_notification_channel_message
);
3000 client
->communication
.inbound
.msg_type
=
3001 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3002 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3003 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3004 ret
= lttng_dynamic_buffer_set_size(
3005 &client
->communication
.inbound
.payload
.buffer
,
3006 client
->communication
.inbound
.bytes_to_receive
);
3011 int handle_notification_thread_client_connect(
3012 struct notification_thread_state
*state
)
3015 struct notification_client
*client
;
3017 DBG("[notification-thread] Handling new notification channel client connection");
3019 client
= zmalloc(sizeof(*client
));
3026 pthread_mutex_init(&client
->lock
, NULL
);
3027 client
->id
= state
->next_notification_client_id
++;
3028 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3029 lttng_payload_init(&client
->communication
.inbound
.payload
);
3030 lttng_payload_init(&client
->communication
.outbound
.payload
);
3031 client
->communication
.inbound
.expect_creds
= true;
3033 ret
= client_reset_inbound_state(client
);
3035 ERR("[notification-thread] Failed to reset client communication's inbound state");
3040 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3042 ERR("[notification-thread] Failed to accept new notification channel client connection");
3047 client
->socket
= ret
;
3049 ret
= socket_set_non_blocking(client
->socket
);
3051 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3055 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3057 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3062 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3063 LPOLLIN
| LPOLLERR
|
3064 LPOLLHUP
| LPOLLRDHUP
);
3066 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3070 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3074 cds_lfht_add(state
->client_socket_ht
,
3075 hash_client_socket(client
->socket
),
3076 &client
->client_socket_ht_node
);
3077 cds_lfht_add(state
->client_id_ht
,
3078 hash_client_id(client
->id
),
3079 &client
->client_id_ht_node
);
3085 notification_client_destroy(client
, state
);
3090 * RCU read-lock must be held by the caller.
3091 * Client lock must _not_ be held by the caller.
3094 int notification_thread_client_disconnect(
3095 struct notification_client
*client
,
3096 struct notification_thread_state
*state
)
3099 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3101 /* Acquire the client lock to disable its communication atomically. */
3102 pthread_mutex_lock(&client
->lock
);
3103 client
->communication
.active
= false;
3104 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3105 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3106 pthread_mutex_unlock(&client
->lock
);
3108 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3110 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3114 /* Release all conditions to which the client was subscribed. */
3115 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3116 &client
->condition_list
, node
) {
3117 (void) notification_thread_client_unsubscribe(client
,
3118 condition_list_element
->condition
, state
, NULL
);
3122 * Client no longer accessible to other threads (through the
3125 notification_client_destroy(client
, state
);
3129 int handle_notification_thread_client_disconnect(
3130 int client_socket
, struct notification_thread_state
*state
)
3133 struct notification_client
*client
;
3136 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3138 client
= get_client_from_socket(client_socket
, state
);
3140 /* Internal state corruption, fatal error. */
3141 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3147 ret
= notification_thread_client_disconnect(client
, state
);
3153 int handle_notification_thread_client_disconnect_all(
3154 struct notification_thread_state
*state
)
3156 struct cds_lfht_iter iter
;
3157 struct notification_client
*client
;
3158 bool error_encoutered
= false;
3161 DBG("[notification-thread] Closing all client connections");
3162 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3163 client_socket_ht_node
) {
3166 ret
= notification_thread_client_disconnect(
3169 error_encoutered
= true;
3173 return error_encoutered
? 1 : 0;
3176 int handle_notification_thread_trigger_unregister_all(
3177 struct notification_thread_state
*state
)
3179 bool error_occurred
= false;
3180 struct cds_lfht_iter iter
;
3181 struct lttng_trigger_ht_element
*trigger_ht_element
;
3184 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3186 int ret
= handle_notification_thread_command_unregister_trigger(
3187 state
, trigger_ht_element
->trigger
, NULL
);
3189 error_occurred
= true;
3193 return error_occurred
? -1 : 0;
3197 int client_handle_transmission_status(
3198 struct notification_client
*client
,
3199 enum client_transmission_status transmission_status
,
3200 struct notification_thread_state
*state
)
3204 switch (transmission_status
) {
3205 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3206 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3207 CLIENT_POLL_MASK_IN
);
3213 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3215 * We want to be notified whenever there is buffer space
3216 * available to send the rest of the payload.
3218 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3219 CLIENT_POLL_MASK_IN_OUT
);
3224 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3225 ret
= notification_thread_client_disconnect(client
, state
);
3230 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3240 /* Client lock must be acquired by caller. */
3242 enum client_transmission_status
client_flush_outgoing_queue(
3243 struct notification_client
*client
)
3246 size_t to_send_count
;
3247 enum client_transmission_status status
;
3248 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3249 &client
->communication
.outbound
.payload
, 0, -1);
3250 const int fds_to_send_count
=
3251 lttng_payload_view_get_fd_handle_count(&pv
);
3253 ASSERT_LOCKED(client
->lock
);
3255 if (!client
->communication
.active
) {
3256 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3260 if (pv
.buffer
.size
== 0) {
3262 * If both data and fds are equal to zero, we are in an invalid
3265 assert(fds_to_send_count
!= 0);
3270 to_send_count
= pv
.buffer
.size
;
3271 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3274 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3277 if ((ret
>= 0 && ret
< to_send_count
)) {
3278 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3280 to_send_count
-= max(ret
, 0);
3282 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3284 pv
.buffer
.size
- to_send_count
,
3286 ret
= lttng_dynamic_buffer_set_size(
3287 &client
->communication
.outbound
.payload
.buffer
,
3293 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3295 } else if (ret
< 0) {
3296 /* Generic error, disable the client's communication. */
3297 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3299 client
->communication
.active
= false;
3300 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3304 * No error and flushed the queue completely.
3306 * The payload buffer size is used later to
3307 * check if there is notifications queued. So albeit that the
3308 * direct caller knows that the transmission is complete, we
3309 * need to set the buffer size to zero.
3311 ret
= lttng_dynamic_buffer_set_size(
3312 &client
->communication
.outbound
.payload
.buffer
, 0);
3319 /* No fds to send, transmission is complete. */
3320 if (fds_to_send_count
== 0) {
3321 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3325 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3326 client
->socket
, &pv
);
3328 /* Generic error, disable the client's communication. */
3329 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3331 client
->communication
.active
= false;
3332 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3334 } else if (ret
== 0) {
3335 /* Nothing could be sent. */
3336 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3338 /* Fd passing is an all or nothing kind of thing. */
3339 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3341 * The payload _fd_array count is used later to
3342 * check if there is notifications queued. So although the
3343 * direct caller knows that the transmission is complete, we
3344 * need to clear the _fd_array for the queuing check.
3346 lttng_dynamic_pointer_array_clear(
3347 &client
->communication
.outbound
.payload
3352 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3353 client
->communication
.outbound
.queued_command_reply
= false;
3354 client
->communication
.outbound
.dropped_notification
= false;
3355 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3360 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3364 bool client_has_outbound_data_left(
3365 const struct notification_client
*client
)
3367 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3368 &client
->communication
.outbound
.payload
, 0, -1);
3369 const bool has_data
= pv
.buffer
.size
!= 0;
3370 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3372 return has_data
|| has_fds
;
3375 /* Client lock must _not_ be held by the caller. */
3377 int client_send_command_reply(struct notification_client
*client
,
3378 struct notification_thread_state
*state
,
3379 enum lttng_notification_channel_status status
)
3382 struct lttng_notification_channel_command_reply reply
= {
3383 .status
= (int8_t) status
,
3385 struct lttng_notification_channel_message msg
= {
3386 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3387 .size
= sizeof(reply
),
3389 char buffer
[sizeof(msg
) + sizeof(reply
)];
3390 enum client_transmission_status transmission_status
;
3392 memcpy(buffer
, &msg
, sizeof(msg
));
3393 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3394 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3396 pthread_mutex_lock(&client
->lock
);
3397 if (client
->communication
.outbound
.queued_command_reply
) {
3398 /* Protocol error. */
3402 /* Enqueue buffer to outgoing queue and flush it. */
3403 ret
= lttng_dynamic_buffer_append(
3404 &client
->communication
.outbound
.payload
.buffer
,
3405 buffer
, sizeof(buffer
));
3410 transmission_status
= client_flush_outgoing_queue(client
);
3412 if (client_has_outbound_data_left(client
)) {
3413 /* Queue could not be emptied. */
3414 client
->communication
.outbound
.queued_command_reply
= true;
3417 pthread_mutex_unlock(&client
->lock
);
3418 ret
= client_handle_transmission_status(
3419 client
, transmission_status
, state
);
3426 pthread_mutex_unlock(&client
->lock
);
3432 int client_handle_message_unknown(struct notification_client
*client
,
3433 struct notification_thread_state
*state
)
3437 * Receiving message header. The function will be called again
3438 * once the rest of the message as been received and can be
3441 const struct lttng_notification_channel_message
*msg
;
3443 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3444 msg
= (const struct lttng_notification_channel_message
*)
3445 client
->communication
.inbound
.payload
.buffer
.data
;
3447 if (msg
->size
== 0 ||
3448 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3449 ERR("[notification-thread] Invalid notification channel message: length = %u",
3455 switch (msg
->type
) {
3456 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3457 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3458 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3462 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3466 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3467 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3468 client
->communication
.inbound
.msg_type
=
3469 (enum lttng_notification_channel_message_type
) msg
->type
;
3470 ret
= lttng_dynamic_buffer_set_size(
3471 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3473 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3480 int client_handle_message_handshake(struct notification_client
*client
,
3481 struct notification_thread_state
*state
)
3484 struct lttng_notification_channel_command_handshake
*handshake_client
;
3485 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3486 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3487 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3489 const struct lttng_notification_channel_message msg_header
= {
3490 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3491 .size
= sizeof(handshake_reply
),
3493 enum lttng_notification_channel_status status
=
3494 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3495 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3497 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3498 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3499 sizeof(handshake_reply
));
3502 (struct lttng_notification_channel_command_handshake
*)
3503 client
->communication
.inbound
.payload
.buffer
3505 client
->major
= handshake_client
->major
;
3506 client
->minor
= handshake_client
->minor
;
3507 if (!client
->communication
.inbound
.creds_received
) {
3508 ERR("[notification-thread] No credentials received from client");
3513 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3514 &client
->communication
.inbound
.creds
);
3515 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3516 &client
->communication
.inbound
.creds
);
3517 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3518 client
->uid
, client
->gid
, (int) client
->major
,
3519 (int) client
->minor
);
3521 if (handshake_client
->major
!=
3522 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3523 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3526 pthread_mutex_lock(&client
->lock
);
3527 /* Outgoing queue will be flushed when the command reply is sent. */
3528 ret
= lttng_dynamic_buffer_append(
3529 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3530 sizeof(send_buffer
));
3532 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3536 client
->validated
= true;
3537 client
->communication
.active
= true;
3538 pthread_mutex_unlock(&client
->lock
);
3540 /* Set reception state to receive the next message header. */
3541 ret
= client_reset_inbound_state(client
);
3543 ERR("[notification-thread] Failed to reset client communication's inbound state");
3547 /* Flushes the outgoing queue. */
3548 ret
= client_send_command_reply(client
, state
, status
);
3550 ERR("[notification-thread] Failed to send reply to notification channel client");
3556 pthread_mutex_unlock(&client
->lock
);
3562 int client_handle_message_subscription(
3563 struct notification_client
*client
,
3564 enum lttng_notification_channel_message_type msg_type
,
3565 struct notification_thread_state
*state
)
3568 struct lttng_condition
*condition
;
3569 enum lttng_notification_channel_status status
=
3570 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3571 struct lttng_payload_view condition_view
=
3572 lttng_payload_view_from_payload(
3573 &client
->communication
.inbound
.payload
,
3575 size_t expected_condition_size
;
3578 * No need to lock client to sample the inbound state as the only
3579 * other thread accessing clients (action executor) only uses the
3582 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3583 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3584 if (ret
!= expected_condition_size
) {
3585 ERR("[notification-thread] Malformed condition received from client");
3589 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3590 ret
= notification_thread_client_subscribe(
3591 client
, condition
, state
, &status
);
3593 ret
= notification_thread_client_unsubscribe(
3594 client
, condition
, state
, &status
);
3601 /* Set reception state to receive the next message header. */
3602 ret
= client_reset_inbound_state(client
);
3604 ERR("[notification-thread] Failed to reset client communication's inbound state");
3608 ret
= client_send_command_reply(client
, state
, status
);
3610 ERR("[notification-thread] Failed to send reply to notification channel client");
3619 int client_dispatch_message(struct notification_client
*client
,
3620 struct notification_thread_state
*state
)
3624 if (client
->communication
.inbound
.msg_type
!=
3625 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3626 client
->communication
.inbound
.msg_type
!=
3627 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3628 !client
->validated
) {
3629 WARN("[notification-thread] client attempted a command before handshake");
3634 switch (client
->communication
.inbound
.msg_type
) {
3635 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3637 ret
= client_handle_message_unknown(client
, state
);
3640 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3642 ret
= client_handle_message_handshake(client
, state
);
3645 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3646 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3648 ret
= client_handle_message_subscription(client
,
3649 client
->communication
.inbound
.msg_type
, state
);
3659 /* Incoming data from client. */
3660 int handle_notification_thread_client_in(
3661 struct notification_thread_state
*state
, int socket
)
3664 struct notification_client
*client
;
3669 client
= get_client_from_socket(socket
, state
);
3671 /* Internal error, abort. */
3676 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3677 client
->communication
.inbound
.bytes_to_receive
;
3678 if (client
->communication
.inbound
.expect_creds
) {
3679 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3680 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3681 client
->communication
.inbound
.bytes_to_receive
,
3682 &client
->communication
.inbound
.creds
);
3684 client
->communication
.inbound
.expect_creds
= false;
3685 client
->communication
.inbound
.creds_received
= true;
3688 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3689 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3690 client
->communication
.inbound
.bytes_to_receive
);
3692 if (recv_ret
>= 0) {
3693 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3695 goto error_disconnect_client
;
3698 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3699 /* Message incomplete wait for more data. */
3704 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3707 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3708 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3710 client
->communication
.inbound
.fds_to_receive
,
3711 &client
->communication
.inbound
.payload
);
3714 * Fds received. non blocking fds passing is all
3717 ssize_t expected_size
;
3719 expected_size
= sizeof(int) *
3720 client
->communication
.inbound
3722 assert(ret
== expected_size
);
3723 client
->communication
.inbound
.fds_to_receive
= 0;
3724 } else if (ret
== 0) {
3725 /* Received nothing. */
3729 goto error_disconnect_client
;
3733 /* At this point the message is complete.*/
3734 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3735 client
->communication
.inbound
.fds_to_receive
== 0);
3736 ret
= client_dispatch_message(client
, state
);
3739 * Only returns an error if this client must be
3742 goto error_disconnect_client
;
3749 error_disconnect_client
:
3750 ret
= notification_thread_client_disconnect(client
, state
);
3754 /* Client ready to receive outgoing data. */
3755 int handle_notification_thread_client_out(
3756 struct notification_thread_state
*state
, int socket
)
3759 struct notification_client
*client
;
3760 enum client_transmission_status transmission_status
;
3763 client
= get_client_from_socket(socket
, state
);
3765 /* Internal error, abort. */
3770 pthread_mutex_lock(&client
->lock
);
3771 transmission_status
= client_flush_outgoing_queue(client
);
3772 pthread_mutex_unlock(&client
->lock
);
3774 ret
= client_handle_transmission_status(
3775 client
, transmission_status
, state
);
3785 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3786 const struct channel_state_sample
*sample
,
3787 uint64_t buffer_capacity
)
3789 bool result
= false;
3791 enum lttng_condition_type condition_type
;
3792 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3793 condition
, struct lttng_condition_buffer_usage
,
3796 if (use_condition
->threshold_bytes
.set
) {
3797 threshold
= use_condition
->threshold_bytes
.value
;
3800 * Threshold was expressed as a ratio.
3802 * TODO the threshold (in bytes) of conditions expressed
3803 * as a ratio of total buffer size could be cached to
3804 * forego this double-multiplication or it could be performed
3805 * as fixed-point math.
3807 * Note that caching should accommodates the case where the
3808 * condition applies to multiple channels (i.e. don't assume
3809 * that all channels matching my_chann* have the same size...)
3811 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3812 (double) buffer_capacity
);
3815 condition_type
= lttng_condition_get_type(condition
);
3816 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3817 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3818 threshold
, sample
->highest_usage
);
3821 * The low condition should only be triggered once _all_ of the
3822 * streams in a channel have gone below the "low" threshold.
3824 if (sample
->highest_usage
<= threshold
) {
3828 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3829 threshold
, sample
->highest_usage
);
3832 * For high buffer usage scenarios, we want to trigger whenever
3833 * _any_ of the streams has reached the "high" threshold.
3835 if (sample
->highest_usage
>= threshold
) {
3844 bool evaluate_session_consumed_size_condition(
3845 const struct lttng_condition
*condition
,
3846 uint64_t session_consumed_size
)
3849 const struct lttng_condition_session_consumed_size
*size_condition
=
3850 container_of(condition
,
3851 struct lttng_condition_session_consumed_size
,
3854 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3855 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3856 threshold
, session_consumed_size
);
3857 return session_consumed_size
>= threshold
;
3861 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3862 struct lttng_evaluation
**evaluation
,
3863 const struct notification_thread_state
*state
,
3864 const struct channel_state_sample
*previous_sample
,
3865 const struct channel_state_sample
*latest_sample
,
3866 uint64_t previous_session_consumed_total
,
3867 uint64_t latest_session_consumed_total
,
3868 struct channel_info
*channel_info
)
3871 enum lttng_condition_type condition_type
;
3872 const bool previous_sample_available
= !!previous_sample
;
3873 bool previous_sample_result
= false;
3874 bool latest_sample_result
;
3876 condition_type
= lttng_condition_get_type(condition
);
3878 switch (condition_type
) {
3879 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3880 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3881 if (caa_likely(previous_sample_available
)) {
3882 previous_sample_result
=
3883 evaluate_buffer_usage_condition(condition
,
3884 previous_sample
, channel_info
->capacity
);
3886 latest_sample_result
= evaluate_buffer_usage_condition(
3887 condition
, latest_sample
,
3888 channel_info
->capacity
);
3890 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3891 if (caa_likely(previous_sample_available
)) {
3892 previous_sample_result
=
3893 evaluate_session_consumed_size_condition(
3895 previous_session_consumed_total
);
3897 latest_sample_result
=
3898 evaluate_session_consumed_size_condition(
3900 latest_session_consumed_total
);
3903 /* Unknown condition type; internal error. */
3907 if (!latest_sample_result
||
3908 (previous_sample_result
== latest_sample_result
)) {
3910 * Only trigger on a condition evaluation transition.
3912 * NOTE: This edge-triggered logic may not be appropriate for
3913 * future condition types.
3918 if (!evaluation
|| !latest_sample_result
) {
3922 switch (condition_type
) {
3923 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3924 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3925 *evaluation
= lttng_evaluation_buffer_usage_create(
3927 latest_sample
->highest_usage
,
3928 channel_info
->capacity
);
3930 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3931 *evaluation
= lttng_evaluation_session_consumed_size_create(
3932 latest_session_consumed_total
);
3947 int client_notification_overflow(struct notification_client
*client
)
3950 const struct lttng_notification_channel_message msg
= {
3951 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3954 ASSERT_LOCKED(client
->lock
);
3956 DBG("Dropping notification addressed to client (socket fd = %i)",
3958 if (client
->communication
.outbound
.dropped_notification
) {
3960 * The client already has a "notification dropped" message
3961 * in its outgoing queue. Nothing to do since all
3962 * of those messages are coalesced.
3967 client
->communication
.outbound
.dropped_notification
= true;
3968 ret
= lttng_dynamic_buffer_append(
3969 &client
->communication
.outbound
.payload
.buffer
, &msg
,
3972 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3979 static int client_handle_transmission_status_wrapper(
3980 struct notification_client
*client
,
3981 enum client_transmission_status status
,
3984 return client_handle_transmission_status(client
, status
,
3985 (struct notification_thread_state
*) user_data
);
3989 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3990 const struct lttng_evaluation
*evaluation
,
3991 struct notification_client_list
* client_list
,
3992 struct notification_thread_state
*state
,
3993 uid_t object_uid
, gid_t object_gid
)
3995 const struct lttng_credentials creds
= {
3996 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
3997 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4000 return notification_client_list_send_evaluation(client_list
,
4001 lttng_trigger_get_const_condition(trigger
), evaluation
,
4002 lttng_trigger_get_credentials(trigger
),
4004 client_handle_transmission_status_wrapper
, state
);
4008 * Permission checks relative to notification channel clients are performed
4009 * here. Notice how object, client, and trigger credentials are involved in
4012 * The `object` credentials are the credentials associated with the "subject"
4013 * of a condition. For instance, a `rotation completed` condition applies
4014 * to a session. When that condition is met, it will produce an evaluation
4015 * against a session. Hence, in this case, the `object` credentials are the
4016 * credentials of the "subject" session.
4018 * The `trigger` credentials are the credentials of the user that registered the
4021 * The `client` credentials are the credentials of the user that created a given
4022 * notification channel.
4024 * In terms of visibility, it is expected that non-privilieged users can only
4025 * register triggers against "their" objects (their own sessions and
4026 * applications they are allowed to interact with). They can then open a
4027 * notification channel and subscribe to notifications associated with those
4030 * As for privilieged users, they can register triggers against the objects of
4031 * other users. They can then subscribe to the notifications associated to their
4032 * triggers. Privilieged users _can't_ subscribe to the notifications of
4033 * triggers owned by other users; they must create their own triggers.
4035 * This is more a concern of usability than security. It would be difficult for
4036 * a root user reliably subscribe to a specific set of conditions without
4037 * interference from external users (those could, for instance, unregister
4041 int notification_client_list_send_evaluation(
4042 struct notification_client_list
*client_list
,
4043 const struct lttng_condition
*condition
,
4044 const struct lttng_evaluation
*evaluation
,
4045 const struct lttng_credentials
*trigger_creds
,
4046 const struct lttng_credentials
*source_object_creds
,
4047 report_client_transmission_result_cb client_report
,
4051 struct lttng_payload msg_payload
;
4052 struct notification_client_list_element
*client_list_element
, *tmp
;
4053 const struct lttng_notification notification
= {
4054 .condition
= (struct lttng_condition
*) condition
,
4055 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4057 struct lttng_notification_channel_message msg_header
= {
4058 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4061 lttng_payload_init(&msg_payload
);
4063 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4064 sizeof(msg_header
));
4069 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4071 ERR("[notification-thread] Failed to serialize notification");
4076 /* Update payload size. */
4077 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4078 ->size
= (uint32_t)(
4079 msg_payload
.buffer
.size
- sizeof(msg_header
));
4081 /* Update the payload number of fds. */
4083 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4084 &msg_payload
, 0, -1);
4086 ((struct lttng_notification_channel_message
*)
4087 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4088 lttng_payload_view_get_fd_handle_count(&pv
);
4091 pthread_mutex_lock(&client_list
->lock
);
4092 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4093 &client_list
->list
, node
) {
4094 enum client_transmission_status transmission_status
;
4095 struct notification_client
*client
=
4096 client_list_element
->client
;
4099 pthread_mutex_lock(&client
->lock
);
4100 if (!client
->communication
.active
) {
4102 * Skip inactive client (protocol error or
4105 DBG("Skipping client at it is marked as inactive");
4109 if (source_object_creds
) {
4110 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4111 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4114 * Client is not allowed to monitor this
4117 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4122 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
4123 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4127 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4128 client
->socket
, msg_payload
.buffer
.size
);
4130 if (client_has_outbound_data_left(client
)) {
4132 * Outgoing data is already buffered for this client;
4133 * drop the notification and enqueue a "dropped
4134 * notification" message if this is the first dropped
4135 * notification since the socket spilled-over to the
4138 ret
= client_notification_overflow(client
);
4145 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4151 transmission_status
= client_flush_outgoing_queue(client
);
4152 pthread_mutex_unlock(&client
->lock
);
4153 ret
= client_report(client
, transmission_status
, user_data
);
4156 goto end_unlock_list
;
4162 pthread_mutex_unlock(&client
->lock
);
4165 goto end_unlock_list
;
4171 pthread_mutex_unlock(&client_list
->lock
);
4173 lttng_payload_reset(&msg_payload
);
4177 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4178 int notification_pipe_read_fd
,
4179 enum lttng_domain_type domain
)
4182 struct lttng_ust_event_notifier_notification ust_notification
;
4183 struct lttng_kernel_event_notifier_notification kernel_notification
;
4184 struct lttng_evaluation
*evaluation
= NULL
;
4185 struct cds_lfht_node
*node
;
4186 struct cds_lfht_iter iter
;
4187 struct notification_trigger_tokens_ht_element
*element
;
4188 enum lttng_trigger_status status
;
4189 struct lttng_event_notifier_notification notification
;
4190 void *reception_buffer
;
4191 size_t reception_size
;
4192 enum action_executor_status executor_status
;
4193 struct notification_client_list
*client_list
= NULL
;
4194 const char *trigger_name
;
4196 notification
.type
= domain
;
4199 case LTTNG_DOMAIN_UST
:
4200 reception_buffer
= (void *) &ust_notification
;
4201 reception_size
= sizeof(ust_notification
);
4202 notification
.notification
.ust
= &ust_notification
;
4204 case LTTNG_DOMAIN_KERNEL
:
4205 reception_buffer
= (void *) &kernel_notification
;
4206 reception_size
= sizeof(kernel_notification
);
4207 notification
.notification
.kernel
= &kernel_notification
;
4214 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4215 * ensuring that read/write of tracer notifications are atomic.
4217 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4219 if (ret
!= reception_size
) {
4220 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4221 notification_pipe_read_fd
, reception_size
, ret
);
4227 case LTTNG_DOMAIN_UST
:
4228 notification
.token
= ust_notification
.token
;
4230 case LTTNG_DOMAIN_KERNEL
:
4231 notification
.token
= kernel_notification
.token
;
4237 /* Find triggers associated with this token. */
4239 cds_lfht_lookup(state
->trigger_tokens_ht
,
4240 hash_key_u64(¬ification
.token
, lttng_ht_seed
),
4241 match_trigger_token
, ¬ification
.token
, &iter
);
4242 node
= cds_lfht_iter_get_node(&iter
);
4243 if (caa_unlikely(!node
)) {
4245 * This is not an error, slow consumption of the tracer
4246 * notifications can lead to situations where a trigger is
4247 * removed but we still get tracer notifications matching a
4248 * trigger that no longer exists.
4254 element
= caa_container_of(node
,
4255 struct notification_trigger_tokens_ht_element
,
4258 if (!lttng_trigger_should_fire(element
->trigger
)) {
4263 lttng_trigger_fire(element
->trigger
);
4265 status
= lttng_trigger_get_name(element
->trigger
, &trigger_name
);
4266 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
4267 evaluation
= lttng_evaluation_event_rule_create(trigger_name
);
4268 if (evaluation
== NULL
) {
4269 ERR("Failed to create event rule evaluation while creating and enqueuing action executor job");
4274 client_list
= get_client_list_from_condition(state
,
4275 lttng_trigger_get_const_condition(element
->trigger
));
4276 executor_status
= action_executor_enqueue(state
->executor
,
4277 element
->trigger
, evaluation
, NULL
, client_list
);
4278 switch (executor_status
) {
4279 case ACTION_EXECUTOR_STATUS_OK
:
4282 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4284 struct notification_client_list_element
*client_list_element
,
4288 * Not a fatal error; this is expected and simply means the
4289 * executor has too much work queued already.
4293 /* No clients subscribed to notifications for this trigger. */
4298 /* Warn clients that a notification (or more) was dropped. */
4299 pthread_mutex_lock(&client_list
->lock
);
4300 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4301 &client_list
->list
, node
) {
4302 enum client_transmission_status transmission_status
;
4303 struct notification_client
*client
=
4304 client_list_element
->client
;
4306 pthread_mutex_lock(&client
->lock
);
4307 ret
= client_notification_overflow(client
);
4313 transmission_status
=
4314 client_flush_outgoing_queue(client
);
4315 ret
= client_handle_transmission_status(
4316 client
, transmission_status
, state
);
4322 pthread_mutex_unlock(&client
->lock
);
4328 pthread_mutex_unlock(&client_list
->lock
);
4331 case ACTION_EXECUTOR_STATUS_ERROR
:
4332 /* Fatal error, shut down everything. */
4333 ERR("Fatal error encoutered while enqueuing action to the action executor");
4337 /* Unhandled error. */
4342 notification_client_list_put(client_list
);
4348 int handle_notification_thread_channel_sample(
4349 struct notification_thread_state
*state
, int pipe
,
4350 enum lttng_domain_type domain
)
4353 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4354 struct channel_info
*channel_info
;
4355 struct cds_lfht_node
*node
;
4356 struct cds_lfht_iter iter
;
4357 struct lttng_channel_trigger_list
*trigger_list
;
4358 struct lttng_trigger_list_element
*trigger_list_element
;
4359 bool previous_sample_available
= false;
4360 struct channel_state_sample previous_sample
, latest_sample
;
4361 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4362 struct lttng_credentials channel_creds
;
4365 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4366 * ensuring that read/write of sampling messages are atomic.
4368 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4369 if (ret
!= sizeof(sample_msg
)) {
4370 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4377 latest_sample
.key
.key
= sample_msg
.key
;
4378 latest_sample
.key
.domain
= domain
;
4379 latest_sample
.highest_usage
= sample_msg
.highest
;
4380 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4381 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4385 /* Retrieve the channel's informations */
4386 cds_lfht_lookup(state
->channels_ht
,
4387 hash_channel_key(&latest_sample
.key
),
4391 node
= cds_lfht_iter_get_node(&iter
);
4392 if (caa_unlikely(!node
)) {
4394 * Not an error since the consumer can push a sample to the pipe
4395 * and the rest of the session daemon could notify us of the
4396 * channel's destruction before we get a chance to process that
4399 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4400 latest_sample
.key
.key
,
4401 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
4405 channel_info
= caa_container_of(node
, struct channel_info
,
4407 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4409 latest_sample
.key
.key
,
4410 channel_info
->session_info
->name
,
4411 latest_sample
.highest_usage
,
4412 latest_sample
.lowest_usage
,
4413 latest_sample
.channel_total_consumed
);
4415 previous_session_consumed_total
=
4416 channel_info
->session_info
->consumed_data_size
;
4418 /* Retrieve the channel's last sample, if it exists, and update it. */
4419 cds_lfht_lookup(state
->channel_state_ht
,
4420 hash_channel_key(&latest_sample
.key
),
4421 match_channel_state_sample
,
4424 node
= cds_lfht_iter_get_node(&iter
);
4425 if (caa_likely(node
)) {
4426 struct channel_state_sample
*stored_sample
;
4428 /* Update the sample stored. */
4429 stored_sample
= caa_container_of(node
,
4430 struct channel_state_sample
,
4431 channel_state_ht_node
);
4433 memcpy(&previous_sample
, stored_sample
,
4434 sizeof(previous_sample
));
4435 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4436 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4437 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4438 previous_sample_available
= true;
4440 latest_session_consumed_total
=
4441 previous_session_consumed_total
+
4442 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4445 * This is the channel's first sample, allocate space for and
4446 * store the new sample.
4448 struct channel_state_sample
*stored_sample
;
4450 stored_sample
= zmalloc(sizeof(*stored_sample
));
4451 if (!stored_sample
) {
4456 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4457 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4458 cds_lfht_add(state
->channel_state_ht
,
4459 hash_channel_key(&stored_sample
->key
),
4460 &stored_sample
->channel_state_ht_node
);
4462 latest_session_consumed_total
=
4463 previous_session_consumed_total
+
4464 latest_sample
.channel_total_consumed
;
4467 channel_info
->session_info
->consumed_data_size
=
4468 latest_session_consumed_total
;
4470 /* Find triggers associated with this channel. */
4471 cds_lfht_lookup(state
->channel_triggers_ht
,
4472 hash_channel_key(&latest_sample
.key
),
4473 match_channel_trigger_list
,
4476 node
= cds_lfht_iter_get_node(&iter
);
4477 if (caa_likely(!node
)) {
4481 channel_creds
= (typeof(channel_creds
)) {
4482 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4483 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4486 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4487 channel_triggers_ht_node
);
4488 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4490 const struct lttng_condition
*condition
;
4491 struct lttng_trigger
*trigger
;
4492 struct notification_client_list
*client_list
= NULL
;
4493 struct lttng_evaluation
*evaluation
= NULL
;
4494 enum action_executor_status executor_status
;
4497 trigger
= trigger_list_element
->trigger
;
4498 condition
= lttng_trigger_get_const_condition(trigger
);
4502 * Check if any client is subscribed to the result of this
4505 client_list
= get_client_list_from_condition(state
, condition
);
4507 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4508 previous_sample_available
? &previous_sample
: NULL
,
4510 previous_session_consumed_total
,
4511 latest_session_consumed_total
,
4513 if (caa_unlikely(ret
)) {
4517 if (caa_likely(!evaluation
)) {
4521 if (!lttng_trigger_should_fire(trigger
)) {
4525 lttng_trigger_fire(trigger
);
4528 * Ownership of `evaluation` transferred to the action executor
4529 * no matter the result.
4531 executor_status
= action_executor_enqueue(state
->executor
,
4532 trigger
, evaluation
, &channel_creds
,
4535 switch (executor_status
) {
4536 case ACTION_EXECUTOR_STATUS_OK
:
4538 case ACTION_EXECUTOR_STATUS_ERROR
:
4539 case ACTION_EXECUTOR_STATUS_INVALID
:
4541 * TODO Add trigger identification (name/id) when
4542 * it is added to the API.
4544 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4547 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4549 * TODO Add trigger identification (name/id) when
4550 * it is added to the API.
4552 * Not a fatal error.
4554 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4562 notification_client_list_put(client_list
);
4563 if (caa_unlikely(ret
)) {