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/notification/channel-internal.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <lttng/event-rule/event-rule-internal.h>
40 #include "condition-internal.h"
41 #include "notification-thread.h"
42 #include "notification-thread-events.h"
43 #include "notification-thread-commands.h"
44 #include "lttng-sessiond.h"
47 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
48 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
50 enum lttng_object_type
{
51 LTTNG_OBJECT_TYPE_UNKNOWN
,
52 LTTNG_OBJECT_TYPE_NONE
,
53 LTTNG_OBJECT_TYPE_CHANNEL
,
54 LTTNG_OBJECT_TYPE_SESSION
,
57 struct lttng_trigger_list_element
{
58 /* No ownership of the trigger object is assumed. */
59 struct lttng_trigger
*trigger
;
60 struct cds_list_head node
;
63 struct lttng_channel_trigger_list
{
64 struct channel_key channel_key
;
65 /* List of struct lttng_trigger_list_element. */
66 struct cds_list_head list
;
67 /* Node in the channel_triggers_ht */
68 struct cds_lfht_node channel_triggers_ht_node
;
69 /* call_rcu delayed reclaim. */
70 struct rcu_head rcu_node
;
74 * List of triggers applying to a given session.
77 * - lttng_session_trigger_list_create()
78 * - lttng_session_trigger_list_build()
79 * - lttng_session_trigger_list_destroy()
80 * - lttng_session_trigger_list_add()
82 struct lttng_session_trigger_list
{
84 * Not owned by this; points to the session_info structure's
87 const char *session_name
;
88 /* List of struct lttng_trigger_list_element. */
89 struct cds_list_head list
;
90 /* Node in the session_triggers_ht */
91 struct cds_lfht_node session_triggers_ht_node
;
93 * Weak reference to the notification system's session triggers
96 * The session trigger list structure structure is owned by
97 * the session's session_info.
99 * The session_info is kept alive the the channel_infos holding a
100 * reference to it (reference counting). When those channels are
101 * destroyed (at runtime or on teardown), the reference they hold
102 * to the session_info are released. On destruction of session_info,
103 * session_info_destroy() will remove the list of triggers applying
104 * to this session from the notification system's state.
106 * This implies that the session_triggers_ht must be destroyed
107 * after the channels.
109 struct cds_lfht
*session_triggers_ht
;
110 /* Used for delayed RCU reclaim. */
111 struct rcu_head rcu_node
;
114 struct lttng_trigger_ht_element
{
115 struct lttng_trigger
*trigger
;
116 struct cds_lfht_node node
;
117 struct cds_lfht_node node_by_name_uid
;
118 /* call_rcu delayed reclaim. */
119 struct rcu_head rcu_node
;
122 struct lttng_condition_list_element
{
123 struct lttng_condition
*condition
;
124 struct cds_list_head node
;
127 struct channel_state_sample
{
128 struct channel_key key
;
129 struct cds_lfht_node channel_state_ht_node
;
130 uint64_t highest_usage
;
131 uint64_t lowest_usage
;
132 uint64_t channel_total_consumed
;
133 /* call_rcu delayed reclaim. */
134 struct rcu_head rcu_node
;
137 static unsigned long hash_channel_key(struct channel_key
*key
);
138 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
139 struct lttng_evaluation
**evaluation
,
140 const struct notification_thread_state
*state
,
141 const struct channel_state_sample
*previous_sample
,
142 const struct channel_state_sample
*latest_sample
,
143 uint64_t previous_session_consumed_total
,
144 uint64_t latest_session_consumed_total
,
145 struct channel_info
*channel_info
);
147 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
148 const struct lttng_evaluation
*evaluation
,
149 struct notification_client_list
*client_list
,
150 struct notification_thread_state
*state
,
151 uid_t channel_uid
, gid_t channel_gid
);
154 /* session_info API */
156 void session_info_destroy(void *_data
);
158 void session_info_get(struct session_info
*session_info
);
160 void session_info_put(struct session_info
*session_info
);
162 struct session_info
*session_info_create(const char *name
,
163 uid_t uid
, gid_t gid
,
164 struct lttng_session_trigger_list
*trigger_list
,
165 struct cds_lfht
*sessions_ht
);
167 void session_info_add_channel(struct session_info
*session_info
,
168 struct channel_info
*channel_info
);
170 void session_info_remove_channel(struct session_info
*session_info
,
171 struct channel_info
*channel_info
);
173 /* lttng_session_trigger_list API */
175 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
176 const char *session_name
,
177 struct cds_lfht
*session_triggers_ht
);
179 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
180 const struct notification_thread_state
*state
,
181 const char *session_name
);
183 void lttng_session_trigger_list_destroy(
184 struct lttng_session_trigger_list
*list
);
186 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
187 struct lttng_trigger
*trigger
);
190 int client_handle_transmission_status(
191 struct notification_client
*client
,
192 enum client_transmission_status transmission_status
,
193 struct notification_thread_state
*state
);
196 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
199 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
201 /* This double-cast is intended to supress pointer-to-cast warning. */
202 const int socket
= (int) (intptr_t) key
;
203 const struct notification_client
*client
= caa_container_of(node
,
204 struct notification_client
, client_socket_ht_node
);
206 return client
->socket
== socket
;
210 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
212 /* This double-cast is intended to supress pointer-to-cast warning. */
213 const notification_client_id id
= *((notification_client_id
*) key
);
214 const struct notification_client
*client
= caa_container_of(
215 node
, struct notification_client
, client_id_ht_node
);
217 return client
->id
== id
;
221 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
223 struct channel_key
*channel_key
= (struct channel_key
*) key
;
224 struct lttng_channel_trigger_list
*trigger_list
;
226 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
227 channel_triggers_ht_node
);
229 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
230 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
234 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
236 const char *session_name
= (const char *) key
;
237 struct lttng_session_trigger_list
*trigger_list
;
239 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
240 session_triggers_ht_node
);
242 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
246 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
248 struct channel_key
*channel_key
= (struct channel_key
*) key
;
249 struct channel_state_sample
*sample
;
251 sample
= caa_container_of(node
, struct channel_state_sample
,
252 channel_state_ht_node
);
254 return !!((channel_key
->key
== sample
->key
.key
) &&
255 (channel_key
->domain
== sample
->key
.domain
));
259 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
261 struct channel_key
*channel_key
= (struct channel_key
*) key
;
262 struct channel_info
*channel_info
;
264 channel_info
= caa_container_of(node
, struct channel_info
,
267 return !!((channel_key
->key
== channel_info
->key
.key
) &&
268 (channel_key
->domain
== channel_info
->key
.domain
));
272 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
274 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
275 struct lttng_trigger_ht_element
*trigger_ht_element
;
277 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
280 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
284 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
286 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
287 struct notification_client_list
*client_list
;
288 const struct lttng_condition
*condition
;
290 assert(condition_key
);
292 client_list
= caa_container_of(node
, struct notification_client_list
,
293 notification_trigger_clients_ht_node
);
294 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
296 return !!lttng_condition_is_equal(condition_key
, condition
);
300 int match_session(struct cds_lfht_node
*node
, const void *key
)
302 const char *name
= key
;
303 struct session_info
*session_info
= caa_container_of(
304 node
, struct session_info
, sessions_ht_node
);
306 return !strcmp(session_info
->name
, name
);
310 * Match trigger based on name and credentials only.
311 * Name duplication is NOT allowed for the same uid.
314 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
319 const char *key_name
;
320 enum lttng_trigger_status status
;
321 const struct lttng_credentials
*key_creds
;
322 const struct lttng_credentials
*node_creds
;
323 const struct lttng_trigger
*trigger_key
=
324 (const struct lttng_trigger
*) key
;
325 const struct lttng_trigger_ht_element
*trigger_ht_element
=
326 caa_container_of(node
,
327 struct lttng_trigger_ht_element
,
330 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
331 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
333 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
334 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
336 /* Compare the names. */
337 if (strcmp(name
, key_name
) != 0) {
341 /* Compare the owners' UIDs. */
342 key_creds
= lttng_trigger_get_credentials(trigger_key
);
343 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
345 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
352 * Hash trigger based on name and credentials only.
355 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
357 unsigned long hash
= 0;
358 const struct lttng_credentials
*trigger_creds
;
359 const char *trigger_name
;
360 enum lttng_trigger_status status
;
362 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
363 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
364 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
367 trigger_creds
= lttng_trigger_get_credentials(trigger
);
368 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
375 unsigned long hash_channel_key(struct channel_key
*key
)
377 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
378 unsigned long domain_hash
= hash_key_ulong(
379 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
381 return key_hash
^ domain_hash
;
385 unsigned long hash_client_socket(int socket
)
387 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
391 unsigned long hash_client_id(notification_client_id id
)
393 return hash_key_u64(&id
, lttng_ht_seed
);
397 * Get the type of object to which a given condition applies. Bindings let
398 * the notification system evaluate a trigger's condition when a given
399 * object's state is updated.
401 * For instance, a condition bound to a channel will be evaluated everytime
402 * the channel's state is changed by a channel monitoring sample.
405 enum lttng_object_type
get_condition_binding_object(
406 const struct lttng_condition
*condition
)
408 switch (lttng_condition_get_type(condition
)) {
409 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
410 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
411 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
412 return LTTNG_OBJECT_TYPE_CHANNEL
;
413 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
414 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
415 return LTTNG_OBJECT_TYPE_SESSION
;
416 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
417 return LTTNG_OBJECT_TYPE_NONE
;
419 return LTTNG_OBJECT_TYPE_UNKNOWN
;
424 void free_channel_info_rcu(struct rcu_head
*node
)
426 free(caa_container_of(node
, struct channel_info
, rcu_node
));
430 void channel_info_destroy(struct channel_info
*channel_info
)
436 if (channel_info
->session_info
) {
437 session_info_remove_channel(channel_info
->session_info
,
439 session_info_put(channel_info
->session_info
);
441 if (channel_info
->name
) {
442 free(channel_info
->name
);
444 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
448 void free_session_info_rcu(struct rcu_head
*node
)
450 free(caa_container_of(node
, struct session_info
, rcu_node
));
453 /* Don't call directly, use the ref-counting mechanism. */
455 void session_info_destroy(void *_data
)
457 struct session_info
*session_info
= _data
;
460 assert(session_info
);
461 if (session_info
->channel_infos_ht
) {
462 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
464 ERR("[notification-thread] Failed to destroy channel information hash table");
467 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
470 cds_lfht_del(session_info
->sessions_ht
,
471 &session_info
->sessions_ht_node
);
473 free(session_info
->name
);
474 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
478 void session_info_get(struct session_info
*session_info
)
483 lttng_ref_get(&session_info
->ref
);
487 void session_info_put(struct session_info
*session_info
)
492 lttng_ref_put(&session_info
->ref
);
496 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
497 struct lttng_session_trigger_list
*trigger_list
,
498 struct cds_lfht
*sessions_ht
)
500 struct session_info
*session_info
;
504 session_info
= zmalloc(sizeof(*session_info
));
508 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
510 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
511 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
512 if (!session_info
->channel_infos_ht
) {
516 cds_lfht_node_init(&session_info
->sessions_ht_node
);
517 session_info
->name
= strdup(name
);
518 if (!session_info
->name
) {
521 session_info
->uid
= uid
;
522 session_info
->gid
= gid
;
523 session_info
->trigger_list
= trigger_list
;
524 session_info
->sessions_ht
= sessions_ht
;
528 session_info_put(session_info
);
533 void session_info_add_channel(struct session_info
*session_info
,
534 struct channel_info
*channel_info
)
537 cds_lfht_add(session_info
->channel_infos_ht
,
538 hash_channel_key(&channel_info
->key
),
539 &channel_info
->session_info_channels_ht_node
);
544 void session_info_remove_channel(struct session_info
*session_info
,
545 struct channel_info
*channel_info
)
548 cds_lfht_del(session_info
->channel_infos_ht
,
549 &channel_info
->session_info_channels_ht_node
);
554 struct channel_info
*channel_info_create(const char *channel_name
,
555 struct channel_key
*channel_key
, uint64_t channel_capacity
,
556 struct session_info
*session_info
)
558 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
564 cds_lfht_node_init(&channel_info
->channels_ht_node
);
565 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
566 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
567 channel_info
->capacity
= channel_capacity
;
569 channel_info
->name
= strdup(channel_name
);
570 if (!channel_info
->name
) {
575 * Set the references between session and channel infos:
576 * - channel_info holds a strong reference to session_info
577 * - session_info holds a weak reference to channel_info
579 session_info_get(session_info
);
580 session_info_add_channel(session_info
, channel_info
);
581 channel_info
->session_info
= session_info
;
585 channel_info_destroy(channel_info
);
590 bool notification_client_list_get(struct notification_client_list
*list
)
592 return urcu_ref_get_unless_zero(&list
->ref
);
596 void free_notification_client_list_rcu(struct rcu_head
*node
)
598 free(caa_container_of(node
, struct notification_client_list
,
603 void notification_client_list_release(struct urcu_ref
*list_ref
)
605 struct notification_client_list
*list
=
606 container_of(list_ref
, typeof(*list
), ref
);
607 struct notification_client_list_element
*client_list_element
, *tmp
;
609 if (list
->notification_trigger_clients_ht
) {
611 cds_lfht_del(list
->notification_trigger_clients_ht
,
612 &list
->notification_trigger_clients_ht_node
);
614 list
->notification_trigger_clients_ht
= NULL
;
616 cds_list_for_each_entry_safe(client_list_element
, tmp
,
618 free(client_list_element
);
620 pthread_mutex_destroy(&list
->lock
);
621 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
625 struct notification_client_list
*notification_client_list_create(
626 const struct lttng_trigger
*trigger
)
628 struct notification_client_list
*client_list
=
629 zmalloc(sizeof(*client_list
));
634 pthread_mutex_init(&client_list
->lock
, NULL
);
635 urcu_ref_init(&client_list
->ref
);
636 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
637 CDS_INIT_LIST_HEAD(&client_list
->list
);
638 client_list
->trigger
= trigger
;
644 void publish_notification_client_list(
645 struct notification_thread_state
*state
,
646 struct notification_client_list
*list
)
648 const struct lttng_condition
*condition
=
649 lttng_trigger_get_const_condition(list
->trigger
);
651 assert(!list
->notification_trigger_clients_ht
);
652 notification_client_list_get(list
);
654 list
->notification_trigger_clients_ht
=
655 state
->notification_trigger_clients_ht
;
658 cds_lfht_add(state
->notification_trigger_clients_ht
,
659 lttng_condition_hash(condition
),
660 &list
->notification_trigger_clients_ht_node
);
665 void notification_client_list_put(struct notification_client_list
*list
)
670 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
673 /* Provides a reference to the returned list. */
675 struct notification_client_list
*get_client_list_from_condition(
676 struct notification_thread_state
*state
,
677 const struct lttng_condition
*condition
)
679 struct cds_lfht_node
*node
;
680 struct cds_lfht_iter iter
;
681 struct notification_client_list
*list
= NULL
;
684 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
685 lttng_condition_hash(condition
),
686 match_client_list_condition
,
689 node
= cds_lfht_iter_get_node(&iter
);
691 list
= container_of(node
, struct notification_client_list
,
692 notification_trigger_clients_ht_node
);
693 list
= notification_client_list_get(list
) ? list
: NULL
;
701 int evaluate_channel_condition_for_client(
702 const struct lttng_condition
*condition
,
703 struct notification_thread_state
*state
,
704 struct lttng_evaluation
**evaluation
,
705 uid_t
*session_uid
, gid_t
*session_gid
)
708 struct cds_lfht_iter iter
;
709 struct cds_lfht_node
*node
;
710 struct channel_info
*channel_info
= NULL
;
711 struct channel_key
*channel_key
= NULL
;
712 struct channel_state_sample
*last_sample
= NULL
;
713 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
717 /* Find the channel associated with the condition. */
718 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
719 channel_trigger_list
, channel_triggers_ht_node
) {
720 struct lttng_trigger_list_element
*element
;
722 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
723 const struct lttng_condition
*current_condition
=
724 lttng_trigger_get_const_condition(
727 assert(current_condition
);
728 if (!lttng_condition_is_equal(condition
,
729 current_condition
)) {
733 /* Found the trigger, save the channel key. */
734 channel_key
= &channel_trigger_list
->channel_key
;
738 /* The channel key was found stop iteration. */
744 /* No channel found; normal exit. */
745 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
750 /* Fetch channel info for the matching channel. */
751 cds_lfht_lookup(state
->channels_ht
,
752 hash_channel_key(channel_key
),
756 node
= cds_lfht_iter_get_node(&iter
);
758 channel_info
= caa_container_of(node
, struct channel_info
,
761 /* Retrieve the channel's last sample, if it exists. */
762 cds_lfht_lookup(state
->channel_state_ht
,
763 hash_channel_key(channel_key
),
764 match_channel_state_sample
,
767 node
= cds_lfht_iter_get_node(&iter
);
769 last_sample
= caa_container_of(node
,
770 struct channel_state_sample
,
771 channel_state_ht_node
);
773 /* Nothing to evaluate, no sample was ever taken. Normal exit */
774 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
779 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
781 0, channel_info
->session_info
->consumed_data_size
,
784 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
788 *session_uid
= channel_info
->session_info
->uid
;
789 *session_gid
= channel_info
->session_info
->gid
;
796 const char *get_condition_session_name(const struct lttng_condition
*condition
)
798 const char *session_name
= NULL
;
799 enum lttng_condition_status status
;
801 switch (lttng_condition_get_type(condition
)) {
802 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
803 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
804 status
= lttng_condition_buffer_usage_get_session_name(
805 condition
, &session_name
);
807 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
808 status
= lttng_condition_session_consumed_size_get_session_name(
809 condition
, &session_name
);
811 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
812 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
813 status
= lttng_condition_session_rotation_get_session_name(
814 condition
, &session_name
);
819 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
820 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
828 int evaluate_session_condition_for_client(
829 const struct lttng_condition
*condition
,
830 struct notification_thread_state
*state
,
831 struct lttng_evaluation
**evaluation
,
832 uid_t
*session_uid
, gid_t
*session_gid
)
835 struct cds_lfht_iter iter
;
836 struct cds_lfht_node
*node
;
837 const char *session_name
;
838 struct session_info
*session_info
= NULL
;
841 session_name
= get_condition_session_name(condition
);
843 /* Find the session associated with the trigger. */
844 cds_lfht_lookup(state
->sessions_ht
,
845 hash_key_str(session_name
, lttng_ht_seed
),
849 node
= cds_lfht_iter_get_node(&iter
);
851 DBG("[notification-thread] No known session matching name \"%s\"",
857 session_info
= caa_container_of(node
, struct session_info
,
859 session_info_get(session_info
);
862 * Evaluation is performed in-line here since only one type of
863 * session-bound condition is handled for the moment.
865 switch (lttng_condition_get_type(condition
)) {
866 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
867 if (!session_info
->rotation
.ongoing
) {
869 goto end_session_put
;
872 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
873 session_info
->rotation
.id
);
876 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
879 goto end_session_put
;
885 goto end_session_put
;
888 *session_uid
= session_info
->uid
;
889 *session_gid
= session_info
->gid
;
892 session_info_put(session_info
);
899 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
900 const struct lttng_condition
*condition
,
901 struct notification_client
*client
,
902 struct notification_thread_state
*state
)
905 struct lttng_evaluation
*evaluation
= NULL
;
906 struct notification_client_list client_list
= {
907 .lock
= PTHREAD_MUTEX_INITIALIZER
,
909 struct notification_client_list_element client_list_element
= { 0 };
910 uid_t object_uid
= 0;
911 gid_t object_gid
= 0;
918 switch (get_condition_binding_object(condition
)) {
919 case LTTNG_OBJECT_TYPE_SESSION
:
920 ret
= evaluate_session_condition_for_client(condition
, state
,
921 &evaluation
, &object_uid
, &object_gid
);
923 case LTTNG_OBJECT_TYPE_CHANNEL
:
924 ret
= evaluate_channel_condition_for_client(condition
, state
,
925 &evaluation
, &object_uid
, &object_gid
);
927 case LTTNG_OBJECT_TYPE_NONE
:
928 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
931 case LTTNG_OBJECT_TYPE_UNKNOWN
:
941 /* Evaluation yielded nothing. Normal exit. */
942 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
948 * Create a temporary client list with the client currently
951 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
952 CDS_INIT_LIST_HEAD(&client_list
.list
);
953 client_list
.trigger
= trigger
;
955 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
956 client_list_element
.client
= client
;
957 cds_list_add(&client_list_element
.node
, &client_list
.list
);
959 /* Send evaluation result to the newly-subscribed client. */
960 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
961 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
962 state
, object_uid
, object_gid
);
969 int notification_thread_client_subscribe(struct notification_client
*client
,
970 struct lttng_condition
*condition
,
971 struct notification_thread_state
*state
,
972 enum lttng_notification_channel_status
*_status
)
975 struct notification_client_list
*client_list
= NULL
;
976 struct lttng_condition_list_element
*condition_list_element
= NULL
;
977 struct notification_client_list_element
*client_list_element
= NULL
;
978 enum lttng_notification_channel_status status
=
979 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
982 * Ensure that the client has not already subscribed to this condition
985 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
986 if (lttng_condition_is_equal(condition_list_element
->condition
,
988 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
993 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
994 if (!condition_list_element
) {
998 client_list_element
= zmalloc(sizeof(*client_list_element
));
999 if (!client_list_element
) {
1005 * Add the newly-subscribed condition to the client's subscription list.
1007 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1008 condition_list_element
->condition
= condition
;
1009 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1011 client_list
= get_client_list_from_condition(state
, condition
);
1014 * No notification-emiting trigger registered with this
1015 * condition. We don't evaluate the condition right away
1016 * since this trigger is not registered yet.
1018 free(client_list_element
);
1023 * The condition to which the client just subscribed is evaluated
1024 * at this point so that conditions that are already TRUE result
1025 * in a notification being sent out.
1027 * The client_list's trigger is used without locking the list itself.
1028 * This is correct since the list doesn't own the trigger and the
1029 * object is immutable.
1031 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1033 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1035 free(client_list_element
);
1040 * Add the client to the list of clients interested in a given trigger
1041 * if a "notification" trigger with a corresponding condition was
1044 client_list_element
->client
= client
;
1045 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1047 pthread_mutex_lock(&client_list
->lock
);
1048 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1049 pthread_mutex_unlock(&client_list
->lock
);
1055 notification_client_list_put(client_list
);
1059 free(condition_list_element
);
1060 free(client_list_element
);
1065 int notification_thread_client_unsubscribe(
1066 struct notification_client
*client
,
1067 struct lttng_condition
*condition
,
1068 struct notification_thread_state
*state
,
1069 enum lttng_notification_channel_status
*_status
)
1071 struct notification_client_list
*client_list
;
1072 struct lttng_condition_list_element
*condition_list_element
,
1074 struct notification_client_list_element
*client_list_element
,
1076 bool condition_found
= false;
1077 enum lttng_notification_channel_status status
=
1078 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1080 /* Remove the condition from the client's condition list. */
1081 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1082 &client
->condition_list
, node
) {
1083 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1088 cds_list_del(&condition_list_element
->node
);
1090 * The caller may be iterating on the client's conditions to
1091 * tear down a client's connection. In this case, the condition
1092 * will be destroyed at the end.
1094 if (condition
!= condition_list_element
->condition
) {
1095 lttng_condition_destroy(
1096 condition_list_element
->condition
);
1098 free(condition_list_element
);
1099 condition_found
= true;
1103 if (!condition_found
) {
1104 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1109 * Remove the client from the list of clients interested the trigger
1110 * matching the condition.
1112 client_list
= get_client_list_from_condition(state
, condition
);
1117 pthread_mutex_lock(&client_list
->lock
);
1118 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1119 &client_list
->list
, node
) {
1120 if (client_list_element
->client
->id
!= client
->id
) {
1123 cds_list_del(&client_list_element
->node
);
1124 free(client_list_element
);
1127 pthread_mutex_unlock(&client_list
->lock
);
1128 notification_client_list_put(client_list
);
1131 lttng_condition_destroy(condition
);
1139 void free_notification_client_rcu(struct rcu_head
*node
)
1141 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1145 void notification_client_destroy(struct notification_client
*client
,
1146 struct notification_thread_state
*state
)
1153 * The client object is not reachable by other threads, no need to lock
1156 if (client
->socket
>= 0) {
1157 (void) lttcomm_close_unix_sock(client
->socket
);
1158 client
->socket
= -1;
1160 client
->communication
.active
= false;
1161 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1162 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1163 pthread_mutex_destroy(&client
->lock
);
1164 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1168 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1172 struct notification_client
*get_client_from_socket(int socket
,
1173 struct notification_thread_state
*state
)
1175 struct cds_lfht_iter iter
;
1176 struct cds_lfht_node
*node
;
1177 struct notification_client
*client
= NULL
;
1179 cds_lfht_lookup(state
->client_socket_ht
,
1180 hash_client_socket(socket
),
1181 match_client_socket
,
1182 (void *) (unsigned long) socket
,
1184 node
= cds_lfht_iter_get_node(&iter
);
1189 client
= caa_container_of(node
, struct notification_client
,
1190 client_socket_ht_node
);
1196 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1200 struct notification_client
*get_client_from_id(notification_client_id id
,
1201 struct notification_thread_state
*state
)
1203 struct cds_lfht_iter iter
;
1204 struct cds_lfht_node
*node
;
1205 struct notification_client
*client
= NULL
;
1207 cds_lfht_lookup(state
->client_id_ht
,
1212 node
= cds_lfht_iter_get_node(&iter
);
1217 client
= caa_container_of(node
, struct notification_client
,
1224 bool buffer_usage_condition_applies_to_channel(
1225 const struct lttng_condition
*condition
,
1226 const struct channel_info
*channel_info
)
1228 enum lttng_condition_status status
;
1229 enum lttng_domain_type condition_domain
;
1230 const char *condition_session_name
= NULL
;
1231 const char *condition_channel_name
= NULL
;
1233 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1235 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1236 if (channel_info
->key
.domain
!= condition_domain
) {
1240 status
= lttng_condition_buffer_usage_get_session_name(
1241 condition
, &condition_session_name
);
1242 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1244 status
= lttng_condition_buffer_usage_get_channel_name(
1245 condition
, &condition_channel_name
);
1246 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1248 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1251 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1261 bool session_consumed_size_condition_applies_to_channel(
1262 const struct lttng_condition
*condition
,
1263 const struct channel_info
*channel_info
)
1265 enum lttng_condition_status status
;
1266 const char *condition_session_name
= NULL
;
1268 status
= lttng_condition_session_consumed_size_get_session_name(
1269 condition
, &condition_session_name
);
1270 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1272 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1282 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1283 const struct channel_info
*channel_info
)
1285 const struct lttng_condition
*condition
;
1286 bool trigger_applies
;
1288 condition
= lttng_trigger_get_const_condition(trigger
);
1293 switch (lttng_condition_get_type(condition
)) {
1294 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1295 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1296 trigger_applies
= buffer_usage_condition_applies_to_channel(
1297 condition
, channel_info
);
1299 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1300 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1301 condition
, channel_info
);
1307 return trigger_applies
;
1313 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1314 struct notification_client
*client
)
1316 bool applies
= false;
1317 struct lttng_condition_list_element
*condition_list_element
;
1319 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1321 applies
= lttng_condition_is_equal(
1322 condition_list_element
->condition
,
1323 lttng_trigger_get_condition(trigger
));
1331 /* Must be called with RCU read lock held. */
1333 struct lttng_session_trigger_list
*get_session_trigger_list(
1334 struct notification_thread_state
*state
,
1335 const char *session_name
)
1337 struct lttng_session_trigger_list
*list
= NULL
;
1338 struct cds_lfht_node
*node
;
1339 struct cds_lfht_iter iter
;
1341 cds_lfht_lookup(state
->session_triggers_ht
,
1342 hash_key_str(session_name
, lttng_ht_seed
),
1343 match_session_trigger_list
,
1346 node
= cds_lfht_iter_get_node(&iter
);
1349 * Not an error, the list of triggers applying to that session
1350 * will be initialized when the session is created.
1352 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1357 list
= caa_container_of(node
,
1358 struct lttng_session_trigger_list
,
1359 session_triggers_ht_node
);
1365 * Allocate an empty lttng_session_trigger_list for the session named
1368 * No ownership of 'session_name' is assumed by the session trigger list.
1369 * It is the caller's responsability to ensure the session name is alive
1370 * for as long as this list is.
1373 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1374 const char *session_name
,
1375 struct cds_lfht
*session_triggers_ht
)
1377 struct lttng_session_trigger_list
*list
;
1379 list
= zmalloc(sizeof(*list
));
1383 list
->session_name
= session_name
;
1384 CDS_INIT_LIST_HEAD(&list
->list
);
1385 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1386 list
->session_triggers_ht
= session_triggers_ht
;
1389 /* Publish the list through the session_triggers_ht. */
1390 cds_lfht_add(session_triggers_ht
,
1391 hash_key_str(session_name
, lttng_ht_seed
),
1392 &list
->session_triggers_ht_node
);
1399 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1401 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1406 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1408 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1410 /* Empty the list element by element, and then free the list itself. */
1411 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1412 &list
->list
, node
) {
1413 cds_list_del(&trigger_list_element
->node
);
1414 free(trigger_list_element
);
1417 /* Unpublish the list from the session_triggers_ht. */
1418 cds_lfht_del(list
->session_triggers_ht
,
1419 &list
->session_triggers_ht_node
);
1421 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1425 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1426 struct lttng_trigger
*trigger
)
1429 struct lttng_trigger_list_element
*new_element
=
1430 zmalloc(sizeof(*new_element
));
1436 CDS_INIT_LIST_HEAD(&new_element
->node
);
1437 new_element
->trigger
= trigger
;
1438 cds_list_add(&new_element
->node
, &list
->list
);
1444 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1445 const char *session_name
)
1447 bool applies
= false;
1448 const struct lttng_condition
*condition
;
1450 condition
= lttng_trigger_get_const_condition(trigger
);
1451 switch (lttng_condition_get_type(condition
)) {
1452 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1453 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1455 enum lttng_condition_status condition_status
;
1456 const char *condition_session_name
;
1458 condition_status
= lttng_condition_session_rotation_get_session_name(
1459 condition
, &condition_session_name
);
1460 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1461 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1465 assert(condition_session_name
);
1466 applies
= !strcmp(condition_session_name
, session_name
);
1477 * Allocate and initialize an lttng_session_trigger_list which contains
1478 * all triggers that apply to the session named 'session_name'.
1480 * No ownership of 'session_name' is assumed by the session trigger list.
1481 * It is the caller's responsability to ensure the session name is alive
1482 * for as long as this list is.
1485 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1486 const struct notification_thread_state
*state
,
1487 const char *session_name
)
1489 int trigger_count
= 0;
1490 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1491 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1492 struct cds_lfht_iter iter
;
1494 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1495 state
->session_triggers_ht
);
1497 /* Add all triggers applying to the session named 'session_name'. */
1498 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1502 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1507 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1508 trigger_ht_element
->trigger
);
1516 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1518 return session_trigger_list
;
1520 lttng_session_trigger_list_destroy(session_trigger_list
);
1525 struct session_info
*find_or_create_session_info(
1526 struct notification_thread_state
*state
,
1527 const char *name
, uid_t uid
, gid_t gid
)
1529 struct session_info
*session
= NULL
;
1530 struct cds_lfht_node
*node
;
1531 struct cds_lfht_iter iter
;
1532 struct lttng_session_trigger_list
*trigger_list
;
1535 cds_lfht_lookup(state
->sessions_ht
,
1536 hash_key_str(name
, lttng_ht_seed
),
1540 node
= cds_lfht_iter_get_node(&iter
);
1542 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1544 session
= caa_container_of(node
, struct session_info
,
1546 assert(session
->uid
== uid
);
1547 assert(session
->gid
== gid
);
1548 session_info_get(session
);
1552 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1553 if (!trigger_list
) {
1557 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1558 state
->sessions_ht
);
1560 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1562 lttng_session_trigger_list_destroy(trigger_list
);
1565 trigger_list
= NULL
;
1567 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1568 &session
->sessions_ht_node
);
1574 session_info_put(session
);
1579 int handle_notification_thread_command_add_channel(
1580 struct notification_thread_state
*state
,
1581 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1582 const char *channel_name
, enum lttng_domain_type channel_domain
,
1583 uint64_t channel_key_int
, uint64_t channel_capacity
,
1584 enum lttng_error_code
*cmd_result
)
1586 struct cds_list_head trigger_list
;
1587 struct channel_info
*new_channel_info
= NULL
;
1588 struct channel_key channel_key
= {
1589 .key
= channel_key_int
,
1590 .domain
= channel_domain
,
1592 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1593 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1594 int trigger_count
= 0;
1595 struct cds_lfht_iter iter
;
1596 struct session_info
*session_info
= NULL
;
1598 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1599 channel_name
, session_name
, channel_key_int
,
1600 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1602 CDS_INIT_LIST_HEAD(&trigger_list
);
1604 session_info
= find_or_create_session_info(state
, session_name
,
1605 session_uid
, session_gid
);
1606 if (!session_info
) {
1607 /* Allocation error or an internal error occurred. */
1611 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1612 channel_capacity
, session_info
);
1613 if (!new_channel_info
) {
1618 /* Build a list of all triggers applying to the new channel. */
1619 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1621 struct lttng_trigger_list_element
*new_element
;
1623 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1624 new_channel_info
)) {
1628 new_element
= zmalloc(sizeof(*new_element
));
1633 CDS_INIT_LIST_HEAD(&new_element
->node
);
1634 new_element
->trigger
= trigger_ht_element
->trigger
;
1635 cds_list_add(&new_element
->node
, &trigger_list
);
1640 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1642 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1643 if (!channel_trigger_list
) {
1646 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1647 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1648 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1649 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1652 /* Add channel to the channel_ht which owns the channel_infos. */
1653 cds_lfht_add(state
->channels_ht
,
1654 hash_channel_key(&new_channel_info
->key
),
1655 &new_channel_info
->channels_ht_node
);
1657 * Add the list of triggers associated with this channel to the
1658 * channel_triggers_ht.
1660 cds_lfht_add(state
->channel_triggers_ht
,
1661 hash_channel_key(&new_channel_info
->key
),
1662 &channel_trigger_list
->channel_triggers_ht_node
);
1664 session_info_put(session_info
);
1665 *cmd_result
= LTTNG_OK
;
1668 channel_info_destroy(new_channel_info
);
1669 session_info_put(session_info
);
1674 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1676 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1681 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1683 free(caa_container_of(node
, struct channel_state_sample
,
1688 int handle_notification_thread_command_remove_channel(
1689 struct notification_thread_state
*state
,
1690 uint64_t channel_key
, enum lttng_domain_type domain
,
1691 enum lttng_error_code
*cmd_result
)
1693 struct cds_lfht_node
*node
;
1694 struct cds_lfht_iter iter
;
1695 struct lttng_channel_trigger_list
*trigger_list
;
1696 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1697 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1698 struct channel_info
*channel_info
;
1700 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1701 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1705 cds_lfht_lookup(state
->channel_triggers_ht
,
1706 hash_channel_key(&key
),
1707 match_channel_trigger_list
,
1710 node
= cds_lfht_iter_get_node(&iter
);
1712 * There is a severe internal error if we are being asked to remove a
1713 * channel that doesn't exist.
1716 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1720 /* Free the list of triggers associated with this channel. */
1721 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1722 channel_triggers_ht_node
);
1723 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1724 &trigger_list
->list
, node
) {
1725 cds_list_del(&trigger_list_element
->node
);
1726 free(trigger_list_element
);
1728 cds_lfht_del(state
->channel_triggers_ht
, node
);
1729 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1731 /* Free sampled channel state. */
1732 cds_lfht_lookup(state
->channel_state_ht
,
1733 hash_channel_key(&key
),
1734 match_channel_state_sample
,
1737 node
= cds_lfht_iter_get_node(&iter
);
1739 * This is expected to be NULL if the channel is destroyed before we
1740 * received a sample.
1743 struct channel_state_sample
*sample
= caa_container_of(node
,
1744 struct channel_state_sample
,
1745 channel_state_ht_node
);
1747 cds_lfht_del(state
->channel_state_ht
, node
);
1748 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1751 /* Remove the channel from the channels_ht and free it. */
1752 cds_lfht_lookup(state
->channels_ht
,
1753 hash_channel_key(&key
),
1757 node
= cds_lfht_iter_get_node(&iter
);
1759 channel_info
= caa_container_of(node
, struct channel_info
,
1761 cds_lfht_del(state
->channels_ht
, node
);
1762 channel_info_destroy(channel_info
);
1765 *cmd_result
= LTTNG_OK
;
1770 int handle_notification_thread_command_session_rotation(
1771 struct notification_thread_state
*state
,
1772 enum notification_thread_command_type cmd_type
,
1773 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1774 uint64_t trace_archive_chunk_id
,
1775 struct lttng_trace_archive_location
*location
,
1776 enum lttng_error_code
*_cmd_result
)
1779 enum lttng_error_code cmd_result
= LTTNG_OK
;
1780 struct lttng_session_trigger_list
*trigger_list
;
1781 struct lttng_trigger_list_element
*trigger_list_element
;
1782 struct session_info
*session_info
;
1783 const struct lttng_credentials session_creds
= {
1784 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1785 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1790 session_info
= find_or_create_session_info(state
, session_name
,
1791 session_uid
, session_gid
);
1792 if (!session_info
) {
1793 /* Allocation error or an internal error occurred. */
1795 cmd_result
= LTTNG_ERR_NOMEM
;
1799 session_info
->rotation
.ongoing
=
1800 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1801 session_info
->rotation
.id
= trace_archive_chunk_id
;
1802 trigger_list
= get_session_trigger_list(state
, session_name
);
1803 if (!trigger_list
) {
1804 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1809 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1811 const struct lttng_condition
*condition
;
1812 struct lttng_trigger
*trigger
;
1813 struct notification_client_list
*client_list
;
1814 struct lttng_evaluation
*evaluation
= NULL
;
1815 enum lttng_condition_type condition_type
;
1816 enum action_executor_status executor_status
;
1818 trigger
= trigger_list_element
->trigger
;
1819 condition
= lttng_trigger_get_const_condition(trigger
);
1821 condition_type
= lttng_condition_get_type(condition
);
1823 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1824 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1826 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1827 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1831 client_list
= get_client_list_from_condition(state
, condition
);
1832 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1833 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1834 trace_archive_chunk_id
);
1836 evaluation
= lttng_evaluation_session_rotation_completed_create(
1837 trace_archive_chunk_id
, location
);
1841 /* Internal error */
1843 cmd_result
= LTTNG_ERR_UNK
;
1848 * Ownership of `evaluation` transferred to the action executor
1849 * no matter the result.
1851 executor_status
= action_executor_enqueue(state
->executor
,
1852 trigger
, evaluation
, &session_creds
,
1855 switch (executor_status
) {
1856 case ACTION_EXECUTOR_STATUS_OK
:
1858 case ACTION_EXECUTOR_STATUS_ERROR
:
1859 case ACTION_EXECUTOR_STATUS_INVALID
:
1861 * TODO Add trigger identification (name/id) when
1862 * it is added to the API.
1864 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1867 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1869 * TODO Add trigger identification (name/id) when
1870 * it is added to the API.
1872 * Not a fatal error.
1874 WARN("No space left when enqueuing action associated with session rotation trigger");
1882 notification_client_list_put(client_list
);
1883 if (caa_unlikely(ret
)) {
1888 session_info_put(session_info
);
1889 *_cmd_result
= cmd_result
;
1894 static int handle_notification_thread_command_list_triggers(
1895 struct notification_thread_handle
*handle
,
1896 struct notification_thread_state
*state
,
1898 struct lttng_triggers
**triggers
,
1899 enum lttng_error_code
*_cmd_result
)
1902 enum lttng_error_code cmd_result
= LTTNG_OK
;
1903 struct cds_lfht_iter iter
;
1904 struct lttng_trigger_ht_element
*trigger_ht_element
;
1905 struct lttng_triggers
*local_triggers
= NULL
;
1906 const struct lttng_credentials
*creds
;
1910 local_triggers
= lttng_triggers_create();
1911 if (!local_triggers
) {
1912 /* Not a fatal error. */
1913 cmd_result
= LTTNG_ERR_NOMEM
;
1917 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
1918 trigger_ht_element
, node
) {
1920 * Only return the triggers to which the client has access.
1921 * The root user has visibility over all triggers.
1923 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
1924 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
1928 ret
= lttng_triggers_add(local_triggers
,
1929 trigger_ht_element
->trigger
);
1931 /* Not a fatal error. */
1933 cmd_result
= LTTNG_ERR_NOMEM
;
1938 /* Transferring ownership to the caller. */
1939 *triggers
= local_triggers
;
1940 local_triggers
= NULL
;
1944 lttng_triggers_destroy(local_triggers
);
1945 *_cmd_result
= cmd_result
;
1950 bool condition_is_supported(struct lttng_condition
*condition
)
1954 switch (lttng_condition_get_type(condition
)) {
1955 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1956 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1959 enum lttng_domain_type domain
;
1961 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1965 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1966 is_supported
= true;
1971 * Older kernel tracers don't expose the API to monitor their
1972 * buffers. Therefore, we reject triggers that require that
1973 * mechanism to be available to be evaluated.
1975 * Assume unsupported on error.
1977 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
1980 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
1982 const struct lttng_event_rule
*event_rule
;
1983 enum lttng_domain_type domain
;
1984 const enum lttng_condition_status status
=
1985 lttng_condition_event_rule_get_rule(
1986 condition
, &event_rule
);
1988 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1990 domain
= lttng_event_rule_get_domain_type(event_rule
);
1991 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1992 is_supported
= true;
1997 * Older kernel tracers can't emit notification. Therefore, we
1998 * reject triggers that require that mechanism to be available
2001 * Assume unsupported on error.
2003 is_supported
= kernel_supports_event_notifiers() == 1;
2007 is_supported
= true;
2010 return is_supported
;
2013 /* Must be called with RCU read lock held. */
2015 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2016 struct notification_thread_state
*state
)
2019 const struct lttng_condition
*condition
;
2020 const char *session_name
;
2021 struct lttng_session_trigger_list
*trigger_list
;
2023 condition
= lttng_trigger_get_const_condition(trigger
);
2024 switch (lttng_condition_get_type(condition
)) {
2025 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2026 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2028 enum lttng_condition_status status
;
2030 status
= lttng_condition_session_rotation_get_session_name(
2031 condition
, &session_name
);
2032 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2033 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2044 trigger_list
= get_session_trigger_list(state
, session_name
);
2045 if (!trigger_list
) {
2046 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2052 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2054 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2059 /* Must be called with RCU read lock held. */
2061 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2062 struct notification_thread_state
*state
)
2065 struct cds_lfht_node
*node
;
2066 struct cds_lfht_iter iter
;
2067 struct channel_info
*channel
;
2069 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2071 struct lttng_trigger_list_element
*trigger_list_element
;
2072 struct lttng_channel_trigger_list
*trigger_list
;
2073 struct cds_lfht_iter lookup_iter
;
2075 if (!trigger_applies_to_channel(trigger
, channel
)) {
2079 cds_lfht_lookup(state
->channel_triggers_ht
,
2080 hash_channel_key(&channel
->key
),
2081 match_channel_trigger_list
,
2084 node
= cds_lfht_iter_get_node(&lookup_iter
);
2086 trigger_list
= caa_container_of(node
,
2087 struct lttng_channel_trigger_list
,
2088 channel_triggers_ht_node
);
2090 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2091 if (!trigger_list_element
) {
2095 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2096 trigger_list_element
->trigger
= trigger
;
2097 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2098 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2106 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2108 bool is_notify
= false;
2109 unsigned int i
, count
;
2110 enum lttng_action_status action_status
;
2111 const struct lttng_action
*action
=
2112 lttng_trigger_get_const_action(trigger
);
2113 enum lttng_action_type action_type
;
2116 action_type
= lttng_action_get_type(action
);
2117 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2120 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2124 action_status
= lttng_action_group_get_count(action
, &count
);
2125 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2127 for (i
= 0; i
< count
; i
++) {
2128 const struct lttng_action
*inner_action
=
2129 lttng_action_group_get_at_index(
2132 action_type
= lttng_action_get_type(inner_action
);
2133 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2143 static bool trigger_name_taken(struct notification_thread_state
*state
,
2144 const struct lttng_trigger
*trigger
)
2146 struct cds_lfht_iter iter
;
2149 * No duplicata is allowed in the triggers_by_name_uid_ht.
2150 * The match is done against the trigger name and uid.
2152 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2153 hash_trigger_by_name_uid(trigger
),
2154 match_trigger_by_name_uid
,
2157 return !!cds_lfht_iter_get_node(&iter
);
2161 enum lttng_error_code
generate_trigger_name(
2162 struct notification_thread_state
*state
,
2163 struct lttng_trigger
*trigger
, const char **name
)
2165 enum lttng_error_code ret_code
= LTTNG_OK
;
2167 enum lttng_trigger_status status
;
2170 const int ret
= lttng_trigger_generate_name(trigger
,
2171 state
->trigger_id
.name_offset
++);
2173 /* The only reason this can fail right now. */
2174 ret_code
= LTTNG_ERR_NOMEM
;
2178 status
= lttng_trigger_get_name(trigger
, name
);
2179 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2181 taken
= trigger_name_taken(state
, trigger
);
2182 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2188 * FIXME A client's credentials are not checked when registering a trigger.
2190 * The effects of this are benign since:
2191 * - The client will succeed in registering the trigger, as it is valid,
2192 * - The trigger will, internally, be bound to the channel/session,
2193 * - The notifications will not be sent since the client's credentials
2194 * are checked against the channel at that moment.
2196 * If this function returns a non-zero value, it means something is
2197 * fundamentally broken and the whole subsystem/thread will be torn down.
2199 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2203 int handle_notification_thread_command_register_trigger(
2204 struct notification_thread_state
*state
,
2205 struct lttng_trigger
*trigger
,
2206 enum lttng_error_code
*cmd_result
)
2209 struct lttng_condition
*condition
;
2210 struct notification_client
*client
;
2211 struct notification_client_list
*client_list
= NULL
;
2212 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2213 struct notification_client_list_element
*client_list_element
;
2214 struct cds_lfht_node
*node
;
2215 struct cds_lfht_iter iter
;
2216 const char* trigger_name
;
2217 bool free_trigger
= true;
2218 struct lttng_evaluation
*evaluation
= NULL
;
2219 struct lttng_credentials object_creds
;
2222 enum action_executor_status executor_status
;
2223 const uint64_t trigger_tracer_token
=
2224 state
->trigger_id
.next_tracer_token
++;
2228 /* Set the trigger's tracer token. */
2229 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2231 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2232 LTTNG_TRIGGER_STATUS_UNSET
) {
2233 const enum lttng_error_code ret_code
= generate_trigger_name(
2234 state
, trigger
, &trigger_name
);
2236 if (ret_code
!= LTTNG_OK
) {
2239 *cmd_result
= ret_code
;
2242 } else if (trigger_name_taken(state
, trigger
)) {
2243 /* Not a fatal error. */
2244 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2249 condition
= lttng_trigger_get_condition(trigger
);
2252 /* Some conditions require tracers to implement a minimal ABI version. */
2253 if (!condition_is_supported(condition
)) {
2254 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2258 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2259 if (!trigger_ht_element
) {
2264 /* Add trigger to the trigger_ht. */
2265 cds_lfht_node_init(&trigger_ht_element
->node
);
2266 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2267 trigger_ht_element
->trigger
= trigger
;
2269 node
= cds_lfht_add_unique(state
->triggers_ht
,
2270 lttng_condition_hash(condition
),
2273 &trigger_ht_element
->node
);
2274 if (node
!= &trigger_ht_element
->node
) {
2275 /* Not a fatal error, simply report it to the client. */
2276 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2277 goto error_free_ht_element
;
2280 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2281 hash_trigger_by_name_uid(trigger
),
2282 match_trigger_by_name_uid
,
2284 &trigger_ht_element
->node_by_name_uid
);
2285 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2286 /* Not a fatal error, simply report it to the client. */
2287 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2288 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2289 goto error_free_ht_element
;
2293 * Ownership of the trigger and of its wrapper was transfered to
2296 trigger_ht_element
= NULL
;
2297 free_trigger
= false;
2300 * The rest only applies to triggers that have a "notify" action.
2301 * It is not skipped as this is the only action type currently
2304 if (is_trigger_action_notify(trigger
)) {
2305 client_list
= notification_client_list_create(trigger
);
2308 goto error_free_ht_element
;
2311 /* Build a list of clients to which this new trigger applies. */
2312 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2313 client_socket_ht_node
) {
2314 if (!trigger_applies_to_client(trigger
, client
)) {
2318 client_list_element
=
2319 zmalloc(sizeof(*client_list_element
));
2320 if (!client_list_element
) {
2322 goto error_put_client_list
;
2325 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2326 client_list_element
->client
= client
;
2327 cds_list_add(&client_list_element
->node
,
2328 &client_list
->list
);
2332 * Client list ownership transferred to the
2333 * notification_trigger_clients_ht.
2335 publish_notification_client_list(state
, client_list
);
2338 switch (get_condition_binding_object(condition
)) {
2339 case LTTNG_OBJECT_TYPE_SESSION
:
2340 /* Add the trigger to the list if it matches a known session. */
2341 ret
= bind_trigger_to_matching_session(trigger
, state
);
2343 goto error_put_client_list
;
2346 case LTTNG_OBJECT_TYPE_CHANNEL
:
2348 * Add the trigger to list of triggers bound to the channels
2351 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2353 goto error_put_client_list
;
2356 case LTTNG_OBJECT_TYPE_NONE
:
2359 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2361 goto error_put_client_list
;
2365 * The new trigger's condition must be evaluated against the current
2368 * In the case of `notify` action, nothing preventing clients from
2369 * subscribing to a condition before the corresponding trigger is
2370 * registered, we have to evaluate this new condition right away.
2372 * At some point, we were waiting for the next "evaluation" (e.g. on
2373 * reception of a channel sample) to evaluate this new condition, but
2376 * The reason it was broken is that waiting for the next sample
2377 * does not allow us to properly handle transitions for edge-triggered
2380 * Consider this example: when we handle a new channel sample, we
2381 * evaluate each conditions twice: once with the previous state, and
2382 * again with the newest state. We then use those two results to
2383 * determine whether a state change happened: a condition was false and
2384 * became true. If a state change happened, we have to notify clients.
2386 * Now, if a client subscribes to a given notification and registers
2387 * a trigger *after* that subscription, we have to make sure the
2388 * condition is evaluated at this point while considering only the
2389 * current state. Otherwise, the next evaluation cycle may only see
2390 * that the evaluations remain the same (true for samples n-1 and n) and
2391 * the client will never know that the condition has been met.
2393 switch (get_condition_binding_object(condition
)) {
2394 case LTTNG_OBJECT_TYPE_SESSION
:
2395 ret
= evaluate_session_condition_for_client(condition
, state
,
2396 &evaluation
, &object_uid
,
2399 case LTTNG_OBJECT_TYPE_CHANNEL
:
2400 ret
= evaluate_channel_condition_for_client(condition
, state
,
2401 &evaluation
, &object_uid
,
2404 case LTTNG_OBJECT_TYPE_NONE
:
2407 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2415 goto error_put_client_list
;
2418 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2419 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2421 DBG("Newly registered trigger's condition evaluated to %s",
2422 evaluation
? "true" : "false");
2424 /* Evaluation yielded nothing. Normal exit. */
2430 * Ownership of `evaluation` transferred to the action executor
2431 * no matter the result.
2433 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2434 evaluation
, &object_creds
, client_list
);
2436 switch (executor_status
) {
2437 case ACTION_EXECUTOR_STATUS_OK
:
2439 case ACTION_EXECUTOR_STATUS_ERROR
:
2440 case ACTION_EXECUTOR_STATUS_INVALID
:
2442 * TODO Add trigger identification (name/id) when
2443 * it is added to the API.
2445 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2447 goto error_put_client_list
;
2448 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2450 * TODO Add trigger identification (name/id) when
2451 * it is added to the API.
2453 * Not a fatal error.
2455 WARN("No space left when enqueuing action associated to newly registered trigger");
2463 *cmd_result
= LTTNG_OK
;
2464 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2465 trigger_name
, trigger_tracer_token
);
2467 error_put_client_list
:
2468 notification_client_list_put(client_list
);
2470 error_free_ht_element
:
2471 if (trigger_ht_element
) {
2472 /* Delayed removal due to RCU constraint on delete. */
2473 call_rcu(&trigger_ht_element
->rcu_node
,
2474 free_lttng_trigger_ht_element_rcu
);
2479 lttng_trigger_destroy(trigger
);
2486 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2488 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2493 int handle_notification_thread_command_unregister_trigger(
2494 struct notification_thread_state
*state
,
2495 struct lttng_trigger
*trigger
,
2496 enum lttng_error_code
*_cmd_reply
)
2498 struct cds_lfht_iter iter
;
2499 struct cds_lfht_node
*triggers_ht_node
;
2500 struct lttng_channel_trigger_list
*trigger_list
;
2501 struct notification_client_list
*client_list
;
2502 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2503 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2505 enum lttng_error_code cmd_reply
;
2509 cds_lfht_lookup(state
->triggers_ht
,
2510 lttng_condition_hash(condition
),
2514 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2515 if (!triggers_ht_node
) {
2516 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2519 cmd_reply
= LTTNG_OK
;
2522 /* Remove trigger from channel_triggers_ht. */
2523 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2524 channel_triggers_ht_node
) {
2525 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2527 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2528 &trigger_list
->list
, node
) {
2529 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2533 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2534 cds_list_del(&trigger_element
->node
);
2535 /* A trigger can only appear once per channel */
2540 if (is_trigger_action_notify(trigger
)) {
2542 * Remove and release the client list from
2543 * notification_trigger_clients_ht.
2545 client_list
= get_client_list_from_condition(state
, condition
);
2546 assert(client_list
);
2548 /* Put new reference and the hashtable's reference. */
2549 notification_client_list_put(client_list
);
2550 notification_client_list_put(client_list
);
2554 /* Remove trigger from triggers_ht. */
2555 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2556 struct lttng_trigger_ht_element
, node
);
2557 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2558 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2560 /* Release the ownership of the trigger. */
2561 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2562 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2566 *_cmd_reply
= cmd_reply
;
2571 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2572 int handle_notification_thread_command(
2573 struct notification_thread_handle
*handle
,
2574 struct notification_thread_state
*state
)
2578 struct notification_thread_command
*cmd
;
2580 /* Read the event pipe to put it back into a quiescent state. */
2581 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2583 if (ret
!= sizeof(counter
)) {
2587 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2588 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2589 struct notification_thread_command
, cmd_list_node
);
2590 cds_list_del(&cmd
->cmd_list_node
);
2591 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2592 switch (cmd
->type
) {
2593 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2594 DBG("[notification-thread] Received register trigger command");
2595 ret
= handle_notification_thread_command_register_trigger(
2596 state
, cmd
->parameters
.trigger
,
2599 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2600 DBG("[notification-thread] Received unregister trigger command");
2601 ret
= handle_notification_thread_command_unregister_trigger(
2602 state
, cmd
->parameters
.trigger
,
2605 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2606 DBG("[notification-thread] Received add channel command");
2607 ret
= handle_notification_thread_command_add_channel(
2609 cmd
->parameters
.add_channel
.session
.name
,
2610 cmd
->parameters
.add_channel
.session
.uid
,
2611 cmd
->parameters
.add_channel
.session
.gid
,
2612 cmd
->parameters
.add_channel
.channel
.name
,
2613 cmd
->parameters
.add_channel
.channel
.domain
,
2614 cmd
->parameters
.add_channel
.channel
.key
,
2615 cmd
->parameters
.add_channel
.channel
.capacity
,
2618 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2619 DBG("[notification-thread] Received remove channel command");
2620 ret
= handle_notification_thread_command_remove_channel(
2621 state
, cmd
->parameters
.remove_channel
.key
,
2622 cmd
->parameters
.remove_channel
.domain
,
2625 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2626 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2627 DBG("[notification-thread] Received session rotation %s command",
2628 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2629 "ongoing" : "completed");
2630 ret
= handle_notification_thread_command_session_rotation(
2633 cmd
->parameters
.session_rotation
.session_name
,
2634 cmd
->parameters
.session_rotation
.uid
,
2635 cmd
->parameters
.session_rotation
.gid
,
2636 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2637 cmd
->parameters
.session_rotation
.location
,
2640 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2642 struct lttng_triggers
*triggers
= NULL
;
2644 ret
= handle_notification_thread_command_list_triggers(
2647 cmd
->parameters
.list_triggers
.uid
,
2650 cmd
->reply
.list_triggers
.triggers
= triggers
;
2654 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2655 DBG("[notification-thread] Received quit command");
2656 cmd
->reply_code
= LTTNG_OK
;
2659 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2661 const enum client_transmission_status client_status
=
2662 cmd
->parameters
.client_communication_update
2664 const notification_client_id client_id
=
2665 cmd
->parameters
.client_communication_update
.id
;
2666 struct notification_client
*client
;
2669 client
= get_client_from_id(client_id
, state
);
2673 * Client error was probably already picked-up by the
2674 * notification thread or it has disconnected
2675 * gracefully while this command was queued.
2677 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2681 ret
= client_handle_transmission_status(
2682 client
, client_status
, state
);
2688 ERR("[notification-thread] Unknown internal command received");
2696 if (cmd
->is_async
) {
2700 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2704 /* Wake-up and return a fatal error to the calling thread. */
2705 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2706 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2708 /* Indicate a fatal error to the caller. */
2713 int socket_set_non_blocking(int socket
)
2717 /* Set the pipe as non-blocking. */
2718 ret
= fcntl(socket
, F_GETFL
, 0);
2720 PERROR("fcntl get socket flags");
2725 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2727 PERROR("fcntl set O_NONBLOCK socket flag");
2730 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2736 int client_reset_inbound_state(struct notification_client
*client
)
2741 lttng_payload_clear(&client
->communication
.inbound
.payload
);
2743 client
->communication
.inbound
.bytes_to_receive
=
2744 sizeof(struct lttng_notification_channel_message
);
2745 client
->communication
.inbound
.msg_type
=
2746 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2747 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2748 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2749 ret
= lttng_dynamic_buffer_set_size(
2750 &client
->communication
.inbound
.payload
.buffer
,
2751 client
->communication
.inbound
.bytes_to_receive
);
2756 int handle_notification_thread_client_connect(
2757 struct notification_thread_state
*state
)
2760 struct notification_client
*client
;
2762 DBG("[notification-thread] Handling new notification channel client connection");
2764 client
= zmalloc(sizeof(*client
));
2771 pthread_mutex_init(&client
->lock
, NULL
);
2772 client
->id
= state
->next_notification_client_id
++;
2773 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2774 lttng_payload_init(&client
->communication
.inbound
.payload
);
2775 lttng_payload_init(&client
->communication
.outbound
.payload
);
2776 client
->communication
.inbound
.expect_creds
= true;
2778 ret
= client_reset_inbound_state(client
);
2780 ERR("[notification-thread] Failed to reset client communication's inbound state");
2785 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2787 ERR("[notification-thread] Failed to accept new notification channel client connection");
2792 client
->socket
= ret
;
2794 ret
= socket_set_non_blocking(client
->socket
);
2796 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2800 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2802 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2807 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2808 LPOLLIN
| LPOLLERR
|
2809 LPOLLHUP
| LPOLLRDHUP
);
2811 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2815 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2819 cds_lfht_add(state
->client_socket_ht
,
2820 hash_client_socket(client
->socket
),
2821 &client
->client_socket_ht_node
);
2822 cds_lfht_add(state
->client_id_ht
,
2823 hash_client_id(client
->id
),
2824 &client
->client_id_ht_node
);
2830 notification_client_destroy(client
, state
);
2835 * RCU read-lock must be held by the caller.
2836 * Client lock must _not_ be held by the caller.
2839 int notification_thread_client_disconnect(
2840 struct notification_client
*client
,
2841 struct notification_thread_state
*state
)
2844 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
2846 /* Acquire the client lock to disable its communication atomically. */
2847 pthread_mutex_lock(&client
->lock
);
2848 client
->communication
.active
= false;
2849 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
2850 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
2851 pthread_mutex_unlock(&client
->lock
);
2853 ret
= lttng_poll_del(&state
->events
, client
->socket
);
2855 ERR("[notification-thread] Failed to remove client socket %d from poll set",
2859 /* Release all conditions to which the client was subscribed. */
2860 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
2861 &client
->condition_list
, node
) {
2862 (void) notification_thread_client_unsubscribe(client
,
2863 condition_list_element
->condition
, state
, NULL
);
2867 * Client no longer accessible to other threads (through the
2870 notification_client_destroy(client
, state
);
2874 int handle_notification_thread_client_disconnect(
2875 int client_socket
, struct notification_thread_state
*state
)
2878 struct notification_client
*client
;
2881 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2883 client
= get_client_from_socket(client_socket
, state
);
2885 /* Internal state corruption, fatal error. */
2886 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2892 ret
= notification_thread_client_disconnect(client
, state
);
2898 int handle_notification_thread_client_disconnect_all(
2899 struct notification_thread_state
*state
)
2901 struct cds_lfht_iter iter
;
2902 struct notification_client
*client
;
2903 bool error_encoutered
= false;
2906 DBG("[notification-thread] Closing all client connections");
2907 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2908 client_socket_ht_node
) {
2911 ret
= notification_thread_client_disconnect(
2914 error_encoutered
= true;
2918 return error_encoutered
? 1 : 0;
2921 int handle_notification_thread_trigger_unregister_all(
2922 struct notification_thread_state
*state
)
2924 bool error_occurred
= false;
2925 struct cds_lfht_iter iter
;
2926 struct lttng_trigger_ht_element
*trigger_ht_element
;
2929 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2931 int ret
= handle_notification_thread_command_unregister_trigger(
2932 state
, trigger_ht_element
->trigger
, NULL
);
2934 error_occurred
= true;
2938 return error_occurred
? -1 : 0;
2942 int client_handle_transmission_status(
2943 struct notification_client
*client
,
2944 enum client_transmission_status transmission_status
,
2945 struct notification_thread_state
*state
)
2949 switch (transmission_status
) {
2950 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
2951 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2952 CLIENT_POLL_MASK_IN
);
2958 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
2960 * We want to be notified whenever there is buffer space
2961 * available to send the rest of the payload.
2963 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2964 CLIENT_POLL_MASK_IN_OUT
);
2969 case CLIENT_TRANSMISSION_STATUS_FAIL
:
2970 ret
= notification_thread_client_disconnect(client
, state
);
2975 case CLIENT_TRANSMISSION_STATUS_ERROR
:
2985 /* Client lock must be acquired by caller. */
2987 enum client_transmission_status
client_flush_outgoing_queue(
2988 struct notification_client
*client
)
2991 size_t to_send_count
;
2992 enum client_transmission_status status
;
2993 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
2994 &client
->communication
.outbound
.payload
, 0, -1);
2995 const int fds_to_send_count
=
2996 lttng_payload_view_get_fd_handle_count(&pv
);
2998 ASSERT_LOCKED(client
->lock
);
3000 if (!client
->communication
.active
) {
3001 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3005 if (pv
.buffer
.size
== 0) {
3007 * If both data and fds are equal to zero, we are in an invalid
3010 assert(fds_to_send_count
!= 0);
3015 to_send_count
= pv
.buffer
.size
;
3016 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3019 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3022 if ((ret
>= 0 && ret
< to_send_count
)) {
3023 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3025 to_send_count
-= max(ret
, 0);
3027 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3029 pv
.buffer
.size
- to_send_count
,
3031 ret
= lttng_dynamic_buffer_set_size(
3032 &client
->communication
.outbound
.payload
.buffer
,
3038 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3040 } else if (ret
< 0) {
3041 /* Generic error, disable the client's communication. */
3042 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3044 client
->communication
.active
= false;
3045 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3049 * No error and flushed the queue completely.
3051 * The payload buffer size is used later to
3052 * check if there is notifications queued. So albeit that the
3053 * direct caller knows that the transmission is complete, we
3054 * need to set the buffer size to zero.
3056 ret
= lttng_dynamic_buffer_set_size(
3057 &client
->communication
.outbound
.payload
.buffer
, 0);
3064 /* No fds to send, transmission is complete. */
3065 if (fds_to_send_count
== 0) {
3066 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3070 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3071 client
->socket
, &pv
);
3073 /* Generic error, disable the client's communication. */
3074 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3076 client
->communication
.active
= false;
3077 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3079 } else if (ret
== 0) {
3080 /* Nothing could be sent. */
3081 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3083 /* Fd passing is an all or nothing kind of thing. */
3084 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3086 * The payload _fd_array count is used later to
3087 * check if there is notifications queued. So although the
3088 * direct caller knows that the transmission is complete, we
3089 * need to clear the _fd_array for the queuing check.
3091 lttng_dynamic_pointer_array_clear(
3092 &client
->communication
.outbound
.payload
3097 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3098 client
->communication
.outbound
.queued_command_reply
= false;
3099 client
->communication
.outbound
.dropped_notification
= false;
3100 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3105 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3109 bool client_has_outbound_data_left(
3110 const struct notification_client
*client
)
3112 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3113 &client
->communication
.outbound
.payload
, 0, -1);
3114 const bool has_data
= pv
.buffer
.size
!= 0;
3115 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3117 return has_data
|| has_fds
;
3120 /* Client lock must _not_ be held by the caller. */
3122 int client_send_command_reply(struct notification_client
*client
,
3123 struct notification_thread_state
*state
,
3124 enum lttng_notification_channel_status status
)
3127 struct lttng_notification_channel_command_reply reply
= {
3128 .status
= (int8_t) status
,
3130 struct lttng_notification_channel_message msg
= {
3131 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3132 .size
= sizeof(reply
),
3134 char buffer
[sizeof(msg
) + sizeof(reply
)];
3135 enum client_transmission_status transmission_status
;
3137 memcpy(buffer
, &msg
, sizeof(msg
));
3138 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3139 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3141 pthread_mutex_lock(&client
->lock
);
3142 if (client
->communication
.outbound
.queued_command_reply
) {
3143 /* Protocol error. */
3147 /* Enqueue buffer to outgoing queue and flush it. */
3148 ret
= lttng_dynamic_buffer_append(
3149 &client
->communication
.outbound
.payload
.buffer
,
3150 buffer
, sizeof(buffer
));
3155 transmission_status
= client_flush_outgoing_queue(client
);
3157 if (client_has_outbound_data_left(client
)) {
3158 /* Queue could not be emptied. */
3159 client
->communication
.outbound
.queued_command_reply
= true;
3162 pthread_mutex_unlock(&client
->lock
);
3163 ret
= client_handle_transmission_status(
3164 client
, transmission_status
, state
);
3171 pthread_mutex_unlock(&client
->lock
);
3177 int client_handle_message_unknown(struct notification_client
*client
,
3178 struct notification_thread_state
*state
)
3182 * Receiving message header. The function will be called again
3183 * once the rest of the message as been received and can be
3186 const struct lttng_notification_channel_message
*msg
;
3188 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3189 msg
= (const struct lttng_notification_channel_message
*)
3190 client
->communication
.inbound
.payload
.buffer
.data
;
3192 if (msg
->size
== 0 ||
3193 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3194 ERR("[notification-thread] Invalid notification channel message: length = %u",
3200 switch (msg
->type
) {
3201 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3202 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3203 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3207 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3211 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3212 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3213 client
->communication
.inbound
.msg_type
=
3214 (enum lttng_notification_channel_message_type
) msg
->type
;
3215 ret
= lttng_dynamic_buffer_set_size(
3216 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3218 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3225 int client_handle_message_handshake(struct notification_client
*client
,
3226 struct notification_thread_state
*state
)
3229 struct lttng_notification_channel_command_handshake
*handshake_client
;
3230 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3231 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3232 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3234 const struct lttng_notification_channel_message msg_header
= {
3235 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3236 .size
= sizeof(handshake_reply
),
3238 enum lttng_notification_channel_status status
=
3239 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3240 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3242 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3243 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3244 sizeof(handshake_reply
));
3247 (struct lttng_notification_channel_command_handshake
*)
3248 client
->communication
.inbound
.payload
.buffer
3250 client
->major
= handshake_client
->major
;
3251 client
->minor
= handshake_client
->minor
;
3252 if (!client
->communication
.inbound
.creds_received
) {
3253 ERR("[notification-thread] No credentials received from client");
3258 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3259 &client
->communication
.inbound
.creds
);
3260 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3261 &client
->communication
.inbound
.creds
);
3262 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3263 client
->uid
, client
->gid
, (int) client
->major
,
3264 (int) client
->minor
);
3266 if (handshake_client
->major
!=
3267 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3268 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3271 pthread_mutex_lock(&client
->lock
);
3272 /* Outgoing queue will be flushed when the command reply is sent. */
3273 ret
= lttng_dynamic_buffer_append(
3274 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3275 sizeof(send_buffer
));
3277 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3281 client
->validated
= true;
3282 client
->communication
.active
= true;
3283 pthread_mutex_unlock(&client
->lock
);
3285 /* Set reception state to receive the next message header. */
3286 ret
= client_reset_inbound_state(client
);
3288 ERR("[notification-thread] Failed to reset client communication's inbound state");
3292 /* Flushes the outgoing queue. */
3293 ret
= client_send_command_reply(client
, state
, status
);
3295 ERR("[notification-thread] Failed to send reply to notification channel client");
3301 pthread_mutex_unlock(&client
->lock
);
3307 int client_handle_message_subscription(
3308 struct notification_client
*client
,
3309 enum lttng_notification_channel_message_type msg_type
,
3310 struct notification_thread_state
*state
)
3313 struct lttng_condition
*condition
;
3314 enum lttng_notification_channel_status status
=
3315 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3316 struct lttng_payload_view condition_view
=
3317 lttng_payload_view_from_payload(
3318 &client
->communication
.inbound
.payload
,
3320 size_t expected_condition_size
;
3323 * No need to lock client to sample the inbound state as the only
3324 * other thread accessing clients (action executor) only uses the
3327 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3328 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3329 if (ret
!= expected_condition_size
) {
3330 ERR("[notification-thread] Malformed condition received from client");
3334 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3335 ret
= notification_thread_client_subscribe(
3336 client
, condition
, state
, &status
);
3338 ret
= notification_thread_client_unsubscribe(
3339 client
, condition
, state
, &status
);
3346 /* Set reception state to receive the next message header. */
3347 ret
= client_reset_inbound_state(client
);
3349 ERR("[notification-thread] Failed to reset client communication's inbound state");
3353 ret
= client_send_command_reply(client
, state
, status
);
3355 ERR("[notification-thread] Failed to send reply to notification channel client");
3364 int client_dispatch_message(struct notification_client
*client
,
3365 struct notification_thread_state
*state
)
3369 if (client
->communication
.inbound
.msg_type
!=
3370 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3371 client
->communication
.inbound
.msg_type
!=
3372 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3373 !client
->validated
) {
3374 WARN("[notification-thread] client attempted a command before handshake");
3379 switch (client
->communication
.inbound
.msg_type
) {
3380 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3382 ret
= client_handle_message_unknown(client
, state
);
3385 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3387 ret
= client_handle_message_handshake(client
, state
);
3390 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3391 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3393 ret
= client_handle_message_subscription(client
,
3394 client
->communication
.inbound
.msg_type
, state
);
3404 /* Incoming data from client. */
3405 int handle_notification_thread_client_in(
3406 struct notification_thread_state
*state
, int socket
)
3409 struct notification_client
*client
;
3414 client
= get_client_from_socket(socket
, state
);
3416 /* Internal error, abort. */
3421 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3422 client
->communication
.inbound
.bytes_to_receive
;
3423 if (client
->communication
.inbound
.expect_creds
) {
3424 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3425 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3426 client
->communication
.inbound
.bytes_to_receive
,
3427 &client
->communication
.inbound
.creds
);
3429 client
->communication
.inbound
.expect_creds
= false;
3430 client
->communication
.inbound
.creds_received
= true;
3433 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3434 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3435 client
->communication
.inbound
.bytes_to_receive
);
3437 if (recv_ret
>= 0) {
3438 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3440 goto error_disconnect_client
;
3443 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3444 /* Message incomplete wait for more data. */
3449 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3452 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3453 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3455 client
->communication
.inbound
.fds_to_receive
,
3456 &client
->communication
.inbound
.payload
);
3459 * Fds received. non blocking fds passing is all
3462 ssize_t expected_size
;
3464 expected_size
= sizeof(int) *
3465 client
->communication
.inbound
3467 assert(ret
== expected_size
);
3468 client
->communication
.inbound
.fds_to_receive
= 0;
3469 } else if (ret
== 0) {
3470 /* Received nothing. */
3474 goto error_disconnect_client
;
3478 /* At this point the message is complete.*/
3479 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3480 client
->communication
.inbound
.fds_to_receive
== 0);
3481 ret
= client_dispatch_message(client
, state
);
3484 * Only returns an error if this client must be
3487 goto error_disconnect_client
;
3494 error_disconnect_client
:
3495 ret
= notification_thread_client_disconnect(client
, state
);
3499 /* Client ready to receive outgoing data. */
3500 int handle_notification_thread_client_out(
3501 struct notification_thread_state
*state
, int socket
)
3504 struct notification_client
*client
;
3505 enum client_transmission_status transmission_status
;
3508 client
= get_client_from_socket(socket
, state
);
3510 /* Internal error, abort. */
3515 pthread_mutex_lock(&client
->lock
);
3516 transmission_status
= client_flush_outgoing_queue(client
);
3517 pthread_mutex_unlock(&client
->lock
);
3519 ret
= client_handle_transmission_status(
3520 client
, transmission_status
, state
);
3530 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3531 const struct channel_state_sample
*sample
,
3532 uint64_t buffer_capacity
)
3534 bool result
= false;
3536 enum lttng_condition_type condition_type
;
3537 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3538 condition
, struct lttng_condition_buffer_usage
,
3541 if (use_condition
->threshold_bytes
.set
) {
3542 threshold
= use_condition
->threshold_bytes
.value
;
3545 * Threshold was expressed as a ratio.
3547 * TODO the threshold (in bytes) of conditions expressed
3548 * as a ratio of total buffer size could be cached to
3549 * forego this double-multiplication or it could be performed
3550 * as fixed-point math.
3552 * Note that caching should accommodates the case where the
3553 * condition applies to multiple channels (i.e. don't assume
3554 * that all channels matching my_chann* have the same size...)
3556 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3557 (double) buffer_capacity
);
3560 condition_type
= lttng_condition_get_type(condition
);
3561 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3562 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3563 threshold
, sample
->highest_usage
);
3566 * The low condition should only be triggered once _all_ of the
3567 * streams in a channel have gone below the "low" threshold.
3569 if (sample
->highest_usage
<= threshold
) {
3573 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3574 threshold
, sample
->highest_usage
);
3577 * For high buffer usage scenarios, we want to trigger whenever
3578 * _any_ of the streams has reached the "high" threshold.
3580 if (sample
->highest_usage
>= threshold
) {
3589 bool evaluate_session_consumed_size_condition(
3590 const struct lttng_condition
*condition
,
3591 uint64_t session_consumed_size
)
3594 const struct lttng_condition_session_consumed_size
*size_condition
=
3595 container_of(condition
,
3596 struct lttng_condition_session_consumed_size
,
3599 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3600 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3601 threshold
, session_consumed_size
);
3602 return session_consumed_size
>= threshold
;
3606 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3607 struct lttng_evaluation
**evaluation
,
3608 const struct notification_thread_state
*state
,
3609 const struct channel_state_sample
*previous_sample
,
3610 const struct channel_state_sample
*latest_sample
,
3611 uint64_t previous_session_consumed_total
,
3612 uint64_t latest_session_consumed_total
,
3613 struct channel_info
*channel_info
)
3616 enum lttng_condition_type condition_type
;
3617 const bool previous_sample_available
= !!previous_sample
;
3618 bool previous_sample_result
= false;
3619 bool latest_sample_result
;
3621 condition_type
= lttng_condition_get_type(condition
);
3623 switch (condition_type
) {
3624 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3625 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3626 if (caa_likely(previous_sample_available
)) {
3627 previous_sample_result
=
3628 evaluate_buffer_usage_condition(condition
,
3629 previous_sample
, channel_info
->capacity
);
3631 latest_sample_result
= evaluate_buffer_usage_condition(
3632 condition
, latest_sample
,
3633 channel_info
->capacity
);
3635 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3636 if (caa_likely(previous_sample_available
)) {
3637 previous_sample_result
=
3638 evaluate_session_consumed_size_condition(
3640 previous_session_consumed_total
);
3642 latest_sample_result
=
3643 evaluate_session_consumed_size_condition(
3645 latest_session_consumed_total
);
3648 /* Unknown condition type; internal error. */
3652 if (!latest_sample_result
||
3653 (previous_sample_result
== latest_sample_result
)) {
3655 * Only trigger on a condition evaluation transition.
3657 * NOTE: This edge-triggered logic may not be appropriate for
3658 * future condition types.
3663 if (!evaluation
|| !latest_sample_result
) {
3667 switch (condition_type
) {
3668 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3669 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3670 *evaluation
= lttng_evaluation_buffer_usage_create(
3672 latest_sample
->highest_usage
,
3673 channel_info
->capacity
);
3675 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3676 *evaluation
= lttng_evaluation_session_consumed_size_create(
3677 latest_session_consumed_total
);
3692 int client_notification_overflow(struct notification_client
*client
)
3695 const struct lttng_notification_channel_message msg
= {
3696 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3699 ASSERT_LOCKED(client
->lock
);
3701 DBG("Dropping notification addressed to client (socket fd = %i)",
3703 if (client
->communication
.outbound
.dropped_notification
) {
3705 * The client already has a "notification dropped" message
3706 * in its outgoing queue. Nothing to do since all
3707 * of those messages are coalesced.
3712 client
->communication
.outbound
.dropped_notification
= true;
3713 ret
= lttng_dynamic_buffer_append(
3714 &client
->communication
.outbound
.payload
.buffer
, &msg
,
3717 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3724 static int client_handle_transmission_status_wrapper(
3725 struct notification_client
*client
,
3726 enum client_transmission_status status
,
3729 return client_handle_transmission_status(client
, status
,
3730 (struct notification_thread_state
*) user_data
);
3734 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3735 const struct lttng_evaluation
*evaluation
,
3736 struct notification_client_list
* client_list
,
3737 struct notification_thread_state
*state
,
3738 uid_t object_uid
, gid_t object_gid
)
3740 const struct lttng_credentials creds
= {
3741 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
3742 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
3745 return notification_client_list_send_evaluation(client_list
,
3746 lttng_trigger_get_const_condition(trigger
), evaluation
,
3747 lttng_trigger_get_credentials(trigger
),
3749 client_handle_transmission_status_wrapper
, state
);
3753 * Permission checks relative to notification channel clients are performed
3754 * here. Notice how object, client, and trigger credentials are involved in
3757 * The `object` credentials are the credentials associated with the "subject"
3758 * of a condition. For instance, a `rotation completed` condition applies
3759 * to a session. When that condition is met, it will produce an evaluation
3760 * against a session. Hence, in this case, the `object` credentials are the
3761 * credentials of the "subject" session.
3763 * The `trigger` credentials are the credentials of the user that registered the
3766 * The `client` credentials are the credentials of the user that created a given
3767 * notification channel.
3769 * In terms of visibility, it is expected that non-privilieged users can only
3770 * register triggers against "their" objects (their own sessions and
3771 * applications they are allowed to interact with). They can then open a
3772 * notification channel and subscribe to notifications associated with those
3775 * As for privilieged users, they can register triggers against the objects of
3776 * other users. They can then subscribe to the notifications associated to their
3777 * triggers. Privilieged users _can't_ subscribe to the notifications of
3778 * triggers owned by other users; they must create their own triggers.
3780 * This is more a concern of usability than security. It would be difficult for
3781 * a root user reliably subscribe to a specific set of conditions without
3782 * interference from external users (those could, for instance, unregister
3786 int notification_client_list_send_evaluation(
3787 struct notification_client_list
*client_list
,
3788 const struct lttng_condition
*condition
,
3789 const struct lttng_evaluation
*evaluation
,
3790 const struct lttng_credentials
*trigger_creds
,
3791 const struct lttng_credentials
*source_object_creds
,
3792 report_client_transmission_result_cb client_report
,
3796 struct lttng_payload msg_payload
;
3797 struct notification_client_list_element
*client_list_element
, *tmp
;
3798 const struct lttng_notification notification
= {
3799 .condition
= (struct lttng_condition
*) condition
,
3800 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3802 struct lttng_notification_channel_message msg_header
= {
3803 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3806 lttng_payload_init(&msg_payload
);
3808 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
3809 sizeof(msg_header
));
3814 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
3816 ERR("[notification-thread] Failed to serialize notification");
3821 /* Update payload size. */
3822 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
3823 ->size
= (uint32_t)(
3824 msg_payload
.buffer
.size
- sizeof(msg_header
));
3826 /* Update the payload number of fds. */
3828 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3829 &msg_payload
, 0, -1);
3831 ((struct lttng_notification_channel_message
*)
3832 msg_payload
.buffer
.data
)->fds
= (uint32_t)
3833 lttng_payload_view_get_fd_handle_count(&pv
);
3836 pthread_mutex_lock(&client_list
->lock
);
3837 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3838 &client_list
->list
, node
) {
3839 enum client_transmission_status transmission_status
;
3840 struct notification_client
*client
=
3841 client_list_element
->client
;
3844 pthread_mutex_lock(&client
->lock
);
3845 if (!client
->communication
.active
) {
3847 * Skip inactive client (protocol error or
3850 DBG("Skipping client at it is marked as inactive");
3854 if (source_object_creds
) {
3855 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
3856 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
3859 * Client is not allowed to monitor this
3862 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
3867 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
3868 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
3872 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3873 client
->socket
, msg_payload
.buffer
.size
);
3875 if (client_has_outbound_data_left(client
)) {
3877 * Outgoing data is already buffered for this client;
3878 * drop the notification and enqueue a "dropped
3879 * notification" message if this is the first dropped
3880 * notification since the socket spilled-over to the
3883 ret
= client_notification_overflow(client
);
3890 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
3896 transmission_status
= client_flush_outgoing_queue(client
);
3897 pthread_mutex_unlock(&client
->lock
);
3898 ret
= client_report(client
, transmission_status
, user_data
);
3901 goto end_unlock_list
;
3907 pthread_mutex_unlock(&client
->lock
);
3910 goto end_unlock_list
;
3916 pthread_mutex_unlock(&client_list
->lock
);
3918 lttng_payload_reset(&msg_payload
);
3922 int handle_notification_thread_channel_sample(
3923 struct notification_thread_state
*state
, int pipe
,
3924 enum lttng_domain_type domain
)
3927 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
3928 struct channel_info
*channel_info
;
3929 struct cds_lfht_node
*node
;
3930 struct cds_lfht_iter iter
;
3931 struct lttng_channel_trigger_list
*trigger_list
;
3932 struct lttng_trigger_list_element
*trigger_list_element
;
3933 bool previous_sample_available
= false;
3934 struct channel_state_sample previous_sample
, latest_sample
;
3935 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
3936 struct lttng_credentials channel_creds
;
3939 * The monitoring pipe only holds messages smaller than PIPE_BUF,
3940 * ensuring that read/write of sampling messages are atomic.
3942 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
3943 if (ret
!= sizeof(sample_msg
)) {
3944 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
3951 latest_sample
.key
.key
= sample_msg
.key
;
3952 latest_sample
.key
.domain
= domain
;
3953 latest_sample
.highest_usage
= sample_msg
.highest
;
3954 latest_sample
.lowest_usage
= sample_msg
.lowest
;
3955 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
3959 /* Retrieve the channel's informations */
3960 cds_lfht_lookup(state
->channels_ht
,
3961 hash_channel_key(&latest_sample
.key
),
3965 node
= cds_lfht_iter_get_node(&iter
);
3966 if (caa_unlikely(!node
)) {
3968 * Not an error since the consumer can push a sample to the pipe
3969 * and the rest of the session daemon could notify us of the
3970 * channel's destruction before we get a chance to process that
3973 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
3974 latest_sample
.key
.key
,
3975 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
3979 channel_info
= caa_container_of(node
, struct channel_info
,
3981 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
3983 latest_sample
.key
.key
,
3984 channel_info
->session_info
->name
,
3985 latest_sample
.highest_usage
,
3986 latest_sample
.lowest_usage
,
3987 latest_sample
.channel_total_consumed
);
3989 previous_session_consumed_total
=
3990 channel_info
->session_info
->consumed_data_size
;
3992 /* Retrieve the channel's last sample, if it exists, and update it. */
3993 cds_lfht_lookup(state
->channel_state_ht
,
3994 hash_channel_key(&latest_sample
.key
),
3995 match_channel_state_sample
,
3998 node
= cds_lfht_iter_get_node(&iter
);
3999 if (caa_likely(node
)) {
4000 struct channel_state_sample
*stored_sample
;
4002 /* Update the sample stored. */
4003 stored_sample
= caa_container_of(node
,
4004 struct channel_state_sample
,
4005 channel_state_ht_node
);
4007 memcpy(&previous_sample
, stored_sample
,
4008 sizeof(previous_sample
));
4009 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4010 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4011 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4012 previous_sample_available
= true;
4014 latest_session_consumed_total
=
4015 previous_session_consumed_total
+
4016 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4019 * This is the channel's first sample, allocate space for and
4020 * store the new sample.
4022 struct channel_state_sample
*stored_sample
;
4024 stored_sample
= zmalloc(sizeof(*stored_sample
));
4025 if (!stored_sample
) {
4030 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4031 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4032 cds_lfht_add(state
->channel_state_ht
,
4033 hash_channel_key(&stored_sample
->key
),
4034 &stored_sample
->channel_state_ht_node
);
4036 latest_session_consumed_total
=
4037 previous_session_consumed_total
+
4038 latest_sample
.channel_total_consumed
;
4041 channel_info
->session_info
->consumed_data_size
=
4042 latest_session_consumed_total
;
4044 /* Find triggers associated with this channel. */
4045 cds_lfht_lookup(state
->channel_triggers_ht
,
4046 hash_channel_key(&latest_sample
.key
),
4047 match_channel_trigger_list
,
4050 node
= cds_lfht_iter_get_node(&iter
);
4051 if (caa_likely(!node
)) {
4055 channel_creds
= (typeof(channel_creds
)) {
4056 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4057 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4060 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4061 channel_triggers_ht_node
);
4062 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4064 const struct lttng_condition
*condition
;
4065 struct lttng_trigger
*trigger
;
4066 struct notification_client_list
*client_list
= NULL
;
4067 struct lttng_evaluation
*evaluation
= NULL
;
4068 enum action_executor_status executor_status
;
4071 trigger
= trigger_list_element
->trigger
;
4072 condition
= lttng_trigger_get_const_condition(trigger
);
4076 * Check if any client is subscribed to the result of this
4079 client_list
= get_client_list_from_condition(state
, condition
);
4081 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4082 previous_sample_available
? &previous_sample
: NULL
,
4084 previous_session_consumed_total
,
4085 latest_session_consumed_total
,
4087 if (caa_unlikely(ret
)) {
4091 if (caa_likely(!evaluation
)) {
4095 if (!lttng_trigger_should_fire(trigger
)) {
4099 lttng_trigger_fire(trigger
);
4102 * Ownership of `evaluation` transferred to the action executor
4103 * no matter the result.
4105 executor_status
= action_executor_enqueue(state
->executor
,
4106 trigger
, evaluation
, &channel_creds
,
4109 switch (executor_status
) {
4110 case ACTION_EXECUTOR_STATUS_OK
:
4112 case ACTION_EXECUTOR_STATUS_ERROR
:
4113 case ACTION_EXECUTOR_STATUS_INVALID
:
4115 * TODO Add trigger identification (name/id) when
4116 * it is added to the API.
4118 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4121 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4123 * TODO Add trigger identification (name/id) when
4124 * it is added to the API.
4126 * Not a fatal error.
4128 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4136 notification_client_list_put(client_list
);
4137 if (caa_unlikely(ret
)) {