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 "notification-thread.h"
41 #include "notification-thread-events.h"
42 #include "notification-thread-commands.h"
43 #include "lttng-sessiond.h"
46 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
47 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
49 enum lttng_object_type
{
50 LTTNG_OBJECT_TYPE_UNKNOWN
,
51 LTTNG_OBJECT_TYPE_NONE
,
52 LTTNG_OBJECT_TYPE_CHANNEL
,
53 LTTNG_OBJECT_TYPE_SESSION
,
56 struct lttng_trigger_list_element
{
57 /* No ownership of the trigger object is assumed. */
58 struct lttng_trigger
*trigger
;
59 struct cds_list_head node
;
62 struct lttng_channel_trigger_list
{
63 struct channel_key channel_key
;
64 /* List of struct lttng_trigger_list_element. */
65 struct cds_list_head list
;
66 /* Node in the channel_triggers_ht */
67 struct cds_lfht_node channel_triggers_ht_node
;
68 /* call_rcu delayed reclaim. */
69 struct rcu_head rcu_node
;
73 * List of triggers applying to a given session.
76 * - lttng_session_trigger_list_create()
77 * - lttng_session_trigger_list_build()
78 * - lttng_session_trigger_list_destroy()
79 * - lttng_session_trigger_list_add()
81 struct lttng_session_trigger_list
{
83 * Not owned by this; points to the session_info structure's
86 const char *session_name
;
87 /* List of struct lttng_trigger_list_element. */
88 struct cds_list_head list
;
89 /* Node in the session_triggers_ht */
90 struct cds_lfht_node session_triggers_ht_node
;
92 * Weak reference to the notification system's session triggers
95 * The session trigger list structure structure is owned by
96 * the session's session_info.
98 * The session_info is kept alive the the channel_infos holding a
99 * reference to it (reference counting). When those channels are
100 * destroyed (at runtime or on teardown), the reference they hold
101 * to the session_info are released. On destruction of session_info,
102 * session_info_destroy() will remove the list of triggers applying
103 * to this session from the notification system's state.
105 * This implies that the session_triggers_ht must be destroyed
106 * after the channels.
108 struct cds_lfht
*session_triggers_ht
;
109 /* Used for delayed RCU reclaim. */
110 struct rcu_head rcu_node
;
113 struct lttng_trigger_ht_element
{
114 struct lttng_trigger
*trigger
;
115 struct cds_lfht_node node
;
116 struct cds_lfht_node node_by_name_uid
;
117 /* call_rcu delayed reclaim. */
118 struct rcu_head rcu_node
;
121 struct lttng_condition_list_element
{
122 struct lttng_condition
*condition
;
123 struct cds_list_head node
;
126 struct channel_state_sample
{
127 struct channel_key key
;
128 struct cds_lfht_node channel_state_ht_node
;
129 uint64_t highest_usage
;
130 uint64_t lowest_usage
;
131 uint64_t channel_total_consumed
;
132 /* call_rcu delayed reclaim. */
133 struct rcu_head rcu_node
;
136 static unsigned long hash_channel_key(struct channel_key
*key
);
137 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
138 struct lttng_evaluation
**evaluation
,
139 const struct notification_thread_state
*state
,
140 const struct channel_state_sample
*previous_sample
,
141 const struct channel_state_sample
*latest_sample
,
142 uint64_t previous_session_consumed_total
,
143 uint64_t latest_session_consumed_total
,
144 struct channel_info
*channel_info
);
146 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
147 const struct lttng_evaluation
*evaluation
,
148 struct notification_client_list
*client_list
,
149 struct notification_thread_state
*state
,
150 uid_t channel_uid
, gid_t channel_gid
);
153 /* session_info API */
155 void session_info_destroy(void *_data
);
157 void session_info_get(struct session_info
*session_info
);
159 void session_info_put(struct session_info
*session_info
);
161 struct session_info
*session_info_create(const char *name
,
162 uid_t uid
, gid_t gid
,
163 struct lttng_session_trigger_list
*trigger_list
,
164 struct cds_lfht
*sessions_ht
);
166 void session_info_add_channel(struct session_info
*session_info
,
167 struct channel_info
*channel_info
);
169 void session_info_remove_channel(struct session_info
*session_info
,
170 struct channel_info
*channel_info
);
172 /* lttng_session_trigger_list API */
174 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
175 const char *session_name
,
176 struct cds_lfht
*session_triggers_ht
);
178 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
179 const struct notification_thread_state
*state
,
180 const char *session_name
);
182 void lttng_session_trigger_list_destroy(
183 struct lttng_session_trigger_list
*list
);
185 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
186 struct lttng_trigger
*trigger
);
189 int client_handle_transmission_status(
190 struct notification_client
*client
,
191 enum client_transmission_status transmission_status
,
192 struct notification_thread_state
*state
);
195 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
198 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
200 /* This double-cast is intended to supress pointer-to-cast warning. */
201 const int socket
= (int) (intptr_t) key
;
202 const struct notification_client
*client
= caa_container_of(node
,
203 struct notification_client
, client_socket_ht_node
);
205 return client
->socket
== socket
;
209 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
211 /* This double-cast is intended to supress pointer-to-cast warning. */
212 const notification_client_id id
= *((notification_client_id
*) key
);
213 const struct notification_client
*client
= caa_container_of(
214 node
, struct notification_client
, client_id_ht_node
);
216 return client
->id
== id
;
220 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
222 struct channel_key
*channel_key
= (struct channel_key
*) key
;
223 struct lttng_channel_trigger_list
*trigger_list
;
225 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
226 channel_triggers_ht_node
);
228 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
229 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
233 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
235 const char *session_name
= (const char *) key
;
236 struct lttng_session_trigger_list
*trigger_list
;
238 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
239 session_triggers_ht_node
);
241 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
245 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
247 struct channel_key
*channel_key
= (struct channel_key
*) key
;
248 struct channel_state_sample
*sample
;
250 sample
= caa_container_of(node
, struct channel_state_sample
,
251 channel_state_ht_node
);
253 return !!((channel_key
->key
== sample
->key
.key
) &&
254 (channel_key
->domain
== sample
->key
.domain
));
258 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
260 struct channel_key
*channel_key
= (struct channel_key
*) key
;
261 struct channel_info
*channel_info
;
263 channel_info
= caa_container_of(node
, struct channel_info
,
266 return !!((channel_key
->key
== channel_info
->key
.key
) &&
267 (channel_key
->domain
== channel_info
->key
.domain
));
271 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
273 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
274 struct lttng_trigger_ht_element
*trigger_ht_element
;
276 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
279 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
283 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
285 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
286 struct notification_client_list
*client_list
;
287 const struct lttng_condition
*condition
;
289 assert(condition_key
);
291 client_list
= caa_container_of(node
, struct notification_client_list
,
292 notification_trigger_clients_ht_node
);
293 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
295 return !!lttng_condition_is_equal(condition_key
, condition
);
299 int match_session(struct cds_lfht_node
*node
, const void *key
)
301 const char *name
= key
;
302 struct session_info
*session_info
= caa_container_of(
303 node
, struct session_info
, sessions_ht_node
);
305 return !strcmp(session_info
->name
, name
);
309 * Match trigger based on name and credentials only.
310 * Name duplication is NOT allowed for the same uid.
313 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
318 const char *key_name
;
319 enum lttng_trigger_status status
;
320 const struct lttng_credentials
*key_creds
;
321 const struct lttng_credentials
*node_creds
;
322 const struct lttng_trigger
*trigger_key
=
323 (const struct lttng_trigger
*) key
;
324 const struct lttng_trigger_ht_element
*trigger_ht_element
=
325 caa_container_of(node
,
326 struct lttng_trigger_ht_element
,
329 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
330 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
332 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
333 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
335 /* Compare the names. */
336 if (strcmp(name
, key_name
) != 0) {
340 /* Compare the owners' UIDs. */
341 key_creds
= lttng_trigger_get_credentials(trigger_key
);
342 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
344 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
351 * Hash trigger based on name and credentials only.
354 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
356 unsigned long hash
= 0;
357 const struct lttng_credentials
*trigger_creds
;
358 const char *trigger_name
;
359 enum lttng_trigger_status status
;
361 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
362 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
363 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
366 trigger_creds
= lttng_trigger_get_credentials(trigger
);
367 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
374 unsigned long lttng_condition_buffer_usage_hash(
375 const struct lttng_condition
*_condition
)
378 unsigned long condition_type
;
379 struct lttng_condition_buffer_usage
*condition
;
381 condition
= container_of(_condition
,
382 struct lttng_condition_buffer_usage
, parent
);
384 condition_type
= (unsigned long) condition
->parent
.type
;
385 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
386 if (condition
->session_name
) {
387 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
389 if (condition
->channel_name
) {
390 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
392 if (condition
->domain
.set
) {
393 hash
^= hash_key_ulong(
394 (void *) condition
->domain
.type
,
397 if (condition
->threshold_ratio
.set
) {
400 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
401 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
402 } else if (condition
->threshold_bytes
.set
) {
405 val
= condition
->threshold_bytes
.value
;
406 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
412 unsigned long lttng_condition_session_consumed_size_hash(
413 const struct lttng_condition
*_condition
)
416 unsigned long condition_type
=
417 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
418 struct lttng_condition_session_consumed_size
*condition
;
421 condition
= container_of(_condition
,
422 struct lttng_condition_session_consumed_size
, parent
);
424 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
425 if (condition
->session_name
) {
426 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
428 val
= condition
->consumed_threshold_bytes
.value
;
429 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
434 unsigned long lttng_condition_session_rotation_hash(
435 const struct lttng_condition
*_condition
)
437 unsigned long hash
, condition_type
;
438 struct lttng_condition_session_rotation
*condition
;
440 condition
= container_of(_condition
,
441 struct lttng_condition_session_rotation
, parent
);
442 condition_type
= (unsigned long) condition
->parent
.type
;
443 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
444 assert(condition
->session_name
);
445 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
450 unsigned long lttng_condition_event_rule_hash(
451 const struct lttng_condition
*condition
)
453 unsigned long hash
, condition_type
;
454 enum lttng_condition_status condition_status
;
455 const struct lttng_event_rule
*event_rule
;
457 condition_type
= (unsigned long) condition
->type
;
459 condition_status
= lttng_condition_event_rule_get_rule(condition
,
461 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
463 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
464 return hash
^ lttng_event_rule_hash(event_rule
);
468 * The lttng_condition hashing code is kept in this file (rather than
469 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
470 * don't want to link in liblttng-ctl.
473 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
475 switch (condition
->type
) {
476 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
477 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
478 return lttng_condition_buffer_usage_hash(condition
);
479 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
480 return lttng_condition_session_consumed_size_hash(condition
);
481 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
482 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
483 return lttng_condition_session_rotation_hash(condition
);
484 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
485 return lttng_condition_event_rule_hash(condition
);
487 ERR("[notification-thread] Unexpected condition type caught");
493 unsigned long hash_channel_key(struct channel_key
*key
)
495 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
496 unsigned long domain_hash
= hash_key_ulong(
497 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
499 return key_hash
^ domain_hash
;
503 unsigned long hash_client_socket(int socket
)
505 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
509 unsigned long hash_client_id(notification_client_id id
)
511 return hash_key_u64(&id
, lttng_ht_seed
);
515 * Get the type of object to which a given condition applies. Bindings let
516 * the notification system evaluate a trigger's condition when a given
517 * object's state is updated.
519 * For instance, a condition bound to a channel will be evaluated everytime
520 * the channel's state is changed by a channel monitoring sample.
523 enum lttng_object_type
get_condition_binding_object(
524 const struct lttng_condition
*condition
)
526 switch (lttng_condition_get_type(condition
)) {
527 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
528 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
529 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
530 return LTTNG_OBJECT_TYPE_CHANNEL
;
531 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
532 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
533 return LTTNG_OBJECT_TYPE_SESSION
;
534 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
535 return LTTNG_OBJECT_TYPE_NONE
;
537 return LTTNG_OBJECT_TYPE_UNKNOWN
;
542 void free_channel_info_rcu(struct rcu_head
*node
)
544 free(caa_container_of(node
, struct channel_info
, rcu_node
));
548 void channel_info_destroy(struct channel_info
*channel_info
)
554 if (channel_info
->session_info
) {
555 session_info_remove_channel(channel_info
->session_info
,
557 session_info_put(channel_info
->session_info
);
559 if (channel_info
->name
) {
560 free(channel_info
->name
);
562 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
566 void free_session_info_rcu(struct rcu_head
*node
)
568 free(caa_container_of(node
, struct session_info
, rcu_node
));
571 /* Don't call directly, use the ref-counting mechanism. */
573 void session_info_destroy(void *_data
)
575 struct session_info
*session_info
= _data
;
578 assert(session_info
);
579 if (session_info
->channel_infos_ht
) {
580 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
582 ERR("[notification-thread] Failed to destroy channel information hash table");
585 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
588 cds_lfht_del(session_info
->sessions_ht
,
589 &session_info
->sessions_ht_node
);
591 free(session_info
->name
);
592 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
596 void session_info_get(struct session_info
*session_info
)
601 lttng_ref_get(&session_info
->ref
);
605 void session_info_put(struct session_info
*session_info
)
610 lttng_ref_put(&session_info
->ref
);
614 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
615 struct lttng_session_trigger_list
*trigger_list
,
616 struct cds_lfht
*sessions_ht
)
618 struct session_info
*session_info
;
622 session_info
= zmalloc(sizeof(*session_info
));
626 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
628 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
629 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
630 if (!session_info
->channel_infos_ht
) {
634 cds_lfht_node_init(&session_info
->sessions_ht_node
);
635 session_info
->name
= strdup(name
);
636 if (!session_info
->name
) {
639 session_info
->uid
= uid
;
640 session_info
->gid
= gid
;
641 session_info
->trigger_list
= trigger_list
;
642 session_info
->sessions_ht
= sessions_ht
;
646 session_info_put(session_info
);
651 void session_info_add_channel(struct session_info
*session_info
,
652 struct channel_info
*channel_info
)
655 cds_lfht_add(session_info
->channel_infos_ht
,
656 hash_channel_key(&channel_info
->key
),
657 &channel_info
->session_info_channels_ht_node
);
662 void session_info_remove_channel(struct session_info
*session_info
,
663 struct channel_info
*channel_info
)
666 cds_lfht_del(session_info
->channel_infos_ht
,
667 &channel_info
->session_info_channels_ht_node
);
672 struct channel_info
*channel_info_create(const char *channel_name
,
673 struct channel_key
*channel_key
, uint64_t channel_capacity
,
674 struct session_info
*session_info
)
676 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
682 cds_lfht_node_init(&channel_info
->channels_ht_node
);
683 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
684 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
685 channel_info
->capacity
= channel_capacity
;
687 channel_info
->name
= strdup(channel_name
);
688 if (!channel_info
->name
) {
693 * Set the references between session and channel infos:
694 * - channel_info holds a strong reference to session_info
695 * - session_info holds a weak reference to channel_info
697 session_info_get(session_info
);
698 session_info_add_channel(session_info
, channel_info
);
699 channel_info
->session_info
= session_info
;
703 channel_info_destroy(channel_info
);
708 bool notification_client_list_get(struct notification_client_list
*list
)
710 return urcu_ref_get_unless_zero(&list
->ref
);
714 void free_notification_client_list_rcu(struct rcu_head
*node
)
716 free(caa_container_of(node
, struct notification_client_list
,
721 void notification_client_list_release(struct urcu_ref
*list_ref
)
723 struct notification_client_list
*list
=
724 container_of(list_ref
, typeof(*list
), ref
);
725 struct notification_client_list_element
*client_list_element
, *tmp
;
727 if (list
->notification_trigger_clients_ht
) {
729 cds_lfht_del(list
->notification_trigger_clients_ht
,
730 &list
->notification_trigger_clients_ht_node
);
732 list
->notification_trigger_clients_ht
= NULL
;
734 cds_list_for_each_entry_safe(client_list_element
, tmp
,
736 free(client_list_element
);
738 pthread_mutex_destroy(&list
->lock
);
739 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
743 struct notification_client_list
*notification_client_list_create(
744 const struct lttng_trigger
*trigger
)
746 struct notification_client_list
*client_list
=
747 zmalloc(sizeof(*client_list
));
752 pthread_mutex_init(&client_list
->lock
, NULL
);
753 urcu_ref_init(&client_list
->ref
);
754 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
755 CDS_INIT_LIST_HEAD(&client_list
->list
);
756 client_list
->trigger
= trigger
;
762 void publish_notification_client_list(
763 struct notification_thread_state
*state
,
764 struct notification_client_list
*list
)
766 const struct lttng_condition
*condition
=
767 lttng_trigger_get_const_condition(list
->trigger
);
769 assert(!list
->notification_trigger_clients_ht
);
770 notification_client_list_get(list
);
772 list
->notification_trigger_clients_ht
=
773 state
->notification_trigger_clients_ht
;
776 cds_lfht_add(state
->notification_trigger_clients_ht
,
777 lttng_condition_hash(condition
),
778 &list
->notification_trigger_clients_ht_node
);
783 void notification_client_list_put(struct notification_client_list
*list
)
788 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
791 /* Provides a reference to the returned list. */
793 struct notification_client_list
*get_client_list_from_condition(
794 struct notification_thread_state
*state
,
795 const struct lttng_condition
*condition
)
797 struct cds_lfht_node
*node
;
798 struct cds_lfht_iter iter
;
799 struct notification_client_list
*list
= NULL
;
802 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
803 lttng_condition_hash(condition
),
804 match_client_list_condition
,
807 node
= cds_lfht_iter_get_node(&iter
);
809 list
= container_of(node
, struct notification_client_list
,
810 notification_trigger_clients_ht_node
);
811 list
= notification_client_list_get(list
) ? list
: NULL
;
819 int evaluate_channel_condition_for_client(
820 const struct lttng_condition
*condition
,
821 struct notification_thread_state
*state
,
822 struct lttng_evaluation
**evaluation
,
823 uid_t
*session_uid
, gid_t
*session_gid
)
826 struct cds_lfht_iter iter
;
827 struct cds_lfht_node
*node
;
828 struct channel_info
*channel_info
= NULL
;
829 struct channel_key
*channel_key
= NULL
;
830 struct channel_state_sample
*last_sample
= NULL
;
831 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
835 /* Find the channel associated with the condition. */
836 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
837 channel_trigger_list
, channel_triggers_ht_node
) {
838 struct lttng_trigger_list_element
*element
;
840 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
841 const struct lttng_condition
*current_condition
=
842 lttng_trigger_get_const_condition(
845 assert(current_condition
);
846 if (!lttng_condition_is_equal(condition
,
847 current_condition
)) {
851 /* Found the trigger, save the channel key. */
852 channel_key
= &channel_trigger_list
->channel_key
;
856 /* The channel key was found stop iteration. */
862 /* No channel found; normal exit. */
863 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
868 /* Fetch channel info for the matching channel. */
869 cds_lfht_lookup(state
->channels_ht
,
870 hash_channel_key(channel_key
),
874 node
= cds_lfht_iter_get_node(&iter
);
876 channel_info
= caa_container_of(node
, struct channel_info
,
879 /* Retrieve the channel's last sample, if it exists. */
880 cds_lfht_lookup(state
->channel_state_ht
,
881 hash_channel_key(channel_key
),
882 match_channel_state_sample
,
885 node
= cds_lfht_iter_get_node(&iter
);
887 last_sample
= caa_container_of(node
,
888 struct channel_state_sample
,
889 channel_state_ht_node
);
891 /* Nothing to evaluate, no sample was ever taken. Normal exit */
892 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
897 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
899 0, channel_info
->session_info
->consumed_data_size
,
902 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
906 *session_uid
= channel_info
->session_info
->uid
;
907 *session_gid
= channel_info
->session_info
->gid
;
914 const char *get_condition_session_name(const struct lttng_condition
*condition
)
916 const char *session_name
= NULL
;
917 enum lttng_condition_status status
;
919 switch (lttng_condition_get_type(condition
)) {
920 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
921 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
922 status
= lttng_condition_buffer_usage_get_session_name(
923 condition
, &session_name
);
925 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
926 status
= lttng_condition_session_consumed_size_get_session_name(
927 condition
, &session_name
);
929 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
930 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
931 status
= lttng_condition_session_rotation_get_session_name(
932 condition
, &session_name
);
937 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
938 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
946 int evaluate_session_condition_for_client(
947 const struct lttng_condition
*condition
,
948 struct notification_thread_state
*state
,
949 struct lttng_evaluation
**evaluation
,
950 uid_t
*session_uid
, gid_t
*session_gid
)
953 struct cds_lfht_iter iter
;
954 struct cds_lfht_node
*node
;
955 const char *session_name
;
956 struct session_info
*session_info
= NULL
;
959 session_name
= get_condition_session_name(condition
);
961 /* Find the session associated with the trigger. */
962 cds_lfht_lookup(state
->sessions_ht
,
963 hash_key_str(session_name
, lttng_ht_seed
),
967 node
= cds_lfht_iter_get_node(&iter
);
969 DBG("[notification-thread] No known session matching name \"%s\"",
975 session_info
= caa_container_of(node
, struct session_info
,
977 session_info_get(session_info
);
980 * Evaluation is performed in-line here since only one type of
981 * session-bound condition is handled for the moment.
983 switch (lttng_condition_get_type(condition
)) {
984 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
985 if (!session_info
->rotation
.ongoing
) {
987 goto end_session_put
;
990 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
991 session_info
->rotation
.id
);
994 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
997 goto end_session_put
;
1003 goto end_session_put
;
1006 *session_uid
= session_info
->uid
;
1007 *session_gid
= session_info
->gid
;
1010 session_info_put(session_info
);
1017 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1018 const struct lttng_condition
*condition
,
1019 struct notification_client
*client
,
1020 struct notification_thread_state
*state
)
1023 struct lttng_evaluation
*evaluation
= NULL
;
1024 struct notification_client_list client_list
= {
1025 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1027 struct notification_client_list_element client_list_element
= { 0 };
1028 uid_t object_uid
= 0;
1029 gid_t object_gid
= 0;
1036 switch (get_condition_binding_object(condition
)) {
1037 case LTTNG_OBJECT_TYPE_SESSION
:
1038 ret
= evaluate_session_condition_for_client(condition
, state
,
1039 &evaluation
, &object_uid
, &object_gid
);
1041 case LTTNG_OBJECT_TYPE_CHANNEL
:
1042 ret
= evaluate_channel_condition_for_client(condition
, state
,
1043 &evaluation
, &object_uid
, &object_gid
);
1045 case LTTNG_OBJECT_TYPE_NONE
:
1048 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1058 /* Evaluation yielded nothing. Normal exit. */
1059 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
1065 * Create a temporary client list with the client currently
1068 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1069 CDS_INIT_LIST_HEAD(&client_list
.list
);
1070 client_list
.trigger
= trigger
;
1072 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1073 client_list_element
.client
= client
;
1074 cds_list_add(&client_list_element
.node
, &client_list
.list
);
1076 /* Send evaluation result to the newly-subscribed client. */
1077 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1078 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1079 state
, object_uid
, object_gid
);
1086 int notification_thread_client_subscribe(struct notification_client
*client
,
1087 struct lttng_condition
*condition
,
1088 struct notification_thread_state
*state
,
1089 enum lttng_notification_channel_status
*_status
)
1092 struct notification_client_list
*client_list
= NULL
;
1093 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1094 struct notification_client_list_element
*client_list_element
= NULL
;
1095 enum lttng_notification_channel_status status
=
1096 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1099 * Ensure that the client has not already subscribed to this condition
1102 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1103 if (lttng_condition_is_equal(condition_list_element
->condition
,
1105 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1110 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1111 if (!condition_list_element
) {
1115 client_list_element
= zmalloc(sizeof(*client_list_element
));
1116 if (!client_list_element
) {
1122 * Add the newly-subscribed condition to the client's subscription list.
1124 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1125 condition_list_element
->condition
= condition
;
1126 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1128 client_list
= get_client_list_from_condition(state
, condition
);
1131 * No notification-emiting trigger registered with this
1132 * condition. We don't evaluate the condition right away
1133 * since this trigger is not registered yet.
1135 free(client_list_element
);
1140 * The condition to which the client just subscribed is evaluated
1141 * at this point so that conditions that are already TRUE result
1142 * in a notification being sent out.
1144 * The client_list's trigger is used without locking the list itself.
1145 * This is correct since the list doesn't own the trigger and the
1146 * object is immutable.
1148 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1150 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1152 free(client_list_element
);
1157 * Add the client to the list of clients interested in a given trigger
1158 * if a "notification" trigger with a corresponding condition was
1161 client_list_element
->client
= client
;
1162 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1164 pthread_mutex_lock(&client_list
->lock
);
1165 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1166 pthread_mutex_unlock(&client_list
->lock
);
1172 notification_client_list_put(client_list
);
1176 free(condition_list_element
);
1177 free(client_list_element
);
1182 int notification_thread_client_unsubscribe(
1183 struct notification_client
*client
,
1184 struct lttng_condition
*condition
,
1185 struct notification_thread_state
*state
,
1186 enum lttng_notification_channel_status
*_status
)
1188 struct notification_client_list
*client_list
;
1189 struct lttng_condition_list_element
*condition_list_element
,
1191 struct notification_client_list_element
*client_list_element
,
1193 bool condition_found
= false;
1194 enum lttng_notification_channel_status status
=
1195 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1197 /* Remove the condition from the client's condition list. */
1198 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1199 &client
->condition_list
, node
) {
1200 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1205 cds_list_del(&condition_list_element
->node
);
1207 * The caller may be iterating on the client's conditions to
1208 * tear down a client's connection. In this case, the condition
1209 * will be destroyed at the end.
1211 if (condition
!= condition_list_element
->condition
) {
1212 lttng_condition_destroy(
1213 condition_list_element
->condition
);
1215 free(condition_list_element
);
1216 condition_found
= true;
1220 if (!condition_found
) {
1221 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1226 * Remove the client from the list of clients interested the trigger
1227 * matching the condition.
1229 client_list
= get_client_list_from_condition(state
, condition
);
1234 pthread_mutex_lock(&client_list
->lock
);
1235 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1236 &client_list
->list
, node
) {
1237 if (client_list_element
->client
->id
!= client
->id
) {
1240 cds_list_del(&client_list_element
->node
);
1241 free(client_list_element
);
1244 pthread_mutex_unlock(&client_list
->lock
);
1245 notification_client_list_put(client_list
);
1248 lttng_condition_destroy(condition
);
1256 void free_notification_client_rcu(struct rcu_head
*node
)
1258 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1262 void notification_client_destroy(struct notification_client
*client
,
1263 struct notification_thread_state
*state
)
1270 * The client object is not reachable by other threads, no need to lock
1273 if (client
->socket
>= 0) {
1274 (void) lttcomm_close_unix_sock(client
->socket
);
1275 client
->socket
= -1;
1277 client
->communication
.active
= false;
1278 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1279 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1280 pthread_mutex_destroy(&client
->lock
);
1281 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1285 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1289 struct notification_client
*get_client_from_socket(int socket
,
1290 struct notification_thread_state
*state
)
1292 struct cds_lfht_iter iter
;
1293 struct cds_lfht_node
*node
;
1294 struct notification_client
*client
= NULL
;
1296 cds_lfht_lookup(state
->client_socket_ht
,
1297 hash_client_socket(socket
),
1298 match_client_socket
,
1299 (void *) (unsigned long) socket
,
1301 node
= cds_lfht_iter_get_node(&iter
);
1306 client
= caa_container_of(node
, struct notification_client
,
1307 client_socket_ht_node
);
1313 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1317 struct notification_client
*get_client_from_id(notification_client_id id
,
1318 struct notification_thread_state
*state
)
1320 struct cds_lfht_iter iter
;
1321 struct cds_lfht_node
*node
;
1322 struct notification_client
*client
= NULL
;
1324 cds_lfht_lookup(state
->client_id_ht
,
1329 node
= cds_lfht_iter_get_node(&iter
);
1334 client
= caa_container_of(node
, struct notification_client
,
1341 bool buffer_usage_condition_applies_to_channel(
1342 const struct lttng_condition
*condition
,
1343 const struct channel_info
*channel_info
)
1345 enum lttng_condition_status status
;
1346 enum lttng_domain_type condition_domain
;
1347 const char *condition_session_name
= NULL
;
1348 const char *condition_channel_name
= NULL
;
1350 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1352 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1353 if (channel_info
->key
.domain
!= condition_domain
) {
1357 status
= lttng_condition_buffer_usage_get_session_name(
1358 condition
, &condition_session_name
);
1359 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1361 status
= lttng_condition_buffer_usage_get_channel_name(
1362 condition
, &condition_channel_name
);
1363 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1365 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1368 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1378 bool session_consumed_size_condition_applies_to_channel(
1379 const struct lttng_condition
*condition
,
1380 const struct channel_info
*channel_info
)
1382 enum lttng_condition_status status
;
1383 const char *condition_session_name
= NULL
;
1385 status
= lttng_condition_session_consumed_size_get_session_name(
1386 condition
, &condition_session_name
);
1387 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1389 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1399 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1400 const struct channel_info
*channel_info
)
1402 const struct lttng_condition
*condition
;
1403 bool trigger_applies
;
1405 condition
= lttng_trigger_get_const_condition(trigger
);
1410 switch (lttng_condition_get_type(condition
)) {
1411 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1412 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1413 trigger_applies
= buffer_usage_condition_applies_to_channel(
1414 condition
, channel_info
);
1416 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1417 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1418 condition
, channel_info
);
1424 return trigger_applies
;
1430 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1431 struct notification_client
*client
)
1433 bool applies
= false;
1434 struct lttng_condition_list_element
*condition_list_element
;
1436 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1438 applies
= lttng_condition_is_equal(
1439 condition_list_element
->condition
,
1440 lttng_trigger_get_condition(trigger
));
1448 /* Must be called with RCU read lock held. */
1450 struct lttng_session_trigger_list
*get_session_trigger_list(
1451 struct notification_thread_state
*state
,
1452 const char *session_name
)
1454 struct lttng_session_trigger_list
*list
= NULL
;
1455 struct cds_lfht_node
*node
;
1456 struct cds_lfht_iter iter
;
1458 cds_lfht_lookup(state
->session_triggers_ht
,
1459 hash_key_str(session_name
, lttng_ht_seed
),
1460 match_session_trigger_list
,
1463 node
= cds_lfht_iter_get_node(&iter
);
1466 * Not an error, the list of triggers applying to that session
1467 * will be initialized when the session is created.
1469 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1474 list
= caa_container_of(node
,
1475 struct lttng_session_trigger_list
,
1476 session_triggers_ht_node
);
1482 * Allocate an empty lttng_session_trigger_list for the session named
1485 * No ownership of 'session_name' is assumed by the session trigger list.
1486 * It is the caller's responsability to ensure the session name is alive
1487 * for as long as this list is.
1490 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1491 const char *session_name
,
1492 struct cds_lfht
*session_triggers_ht
)
1494 struct lttng_session_trigger_list
*list
;
1496 list
= zmalloc(sizeof(*list
));
1500 list
->session_name
= session_name
;
1501 CDS_INIT_LIST_HEAD(&list
->list
);
1502 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1503 list
->session_triggers_ht
= session_triggers_ht
;
1506 /* Publish the list through the session_triggers_ht. */
1507 cds_lfht_add(session_triggers_ht
,
1508 hash_key_str(session_name
, lttng_ht_seed
),
1509 &list
->session_triggers_ht_node
);
1516 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1518 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1523 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1525 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1527 /* Empty the list element by element, and then free the list itself. */
1528 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1529 &list
->list
, node
) {
1530 cds_list_del(&trigger_list_element
->node
);
1531 free(trigger_list_element
);
1534 /* Unpublish the list from the session_triggers_ht. */
1535 cds_lfht_del(list
->session_triggers_ht
,
1536 &list
->session_triggers_ht_node
);
1538 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1542 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1543 struct lttng_trigger
*trigger
)
1546 struct lttng_trigger_list_element
*new_element
=
1547 zmalloc(sizeof(*new_element
));
1553 CDS_INIT_LIST_HEAD(&new_element
->node
);
1554 new_element
->trigger
= trigger
;
1555 cds_list_add(&new_element
->node
, &list
->list
);
1561 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1562 const char *session_name
)
1564 bool applies
= false;
1565 const struct lttng_condition
*condition
;
1567 condition
= lttng_trigger_get_const_condition(trigger
);
1568 switch (lttng_condition_get_type(condition
)) {
1569 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1570 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1572 enum lttng_condition_status condition_status
;
1573 const char *condition_session_name
;
1575 condition_status
= lttng_condition_session_rotation_get_session_name(
1576 condition
, &condition_session_name
);
1577 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1578 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1582 assert(condition_session_name
);
1583 applies
= !strcmp(condition_session_name
, session_name
);
1594 * Allocate and initialize an lttng_session_trigger_list which contains
1595 * all triggers that apply to the session named 'session_name'.
1597 * No ownership of 'session_name' is assumed by the session trigger list.
1598 * It is the caller's responsability to ensure the session name is alive
1599 * for as long as this list is.
1602 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1603 const struct notification_thread_state
*state
,
1604 const char *session_name
)
1606 int trigger_count
= 0;
1607 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1608 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1609 struct cds_lfht_iter iter
;
1611 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1612 state
->session_triggers_ht
);
1614 /* Add all triggers applying to the session named 'session_name'. */
1615 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1619 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1624 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1625 trigger_ht_element
->trigger
);
1633 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1635 return session_trigger_list
;
1637 lttng_session_trigger_list_destroy(session_trigger_list
);
1642 struct session_info
*find_or_create_session_info(
1643 struct notification_thread_state
*state
,
1644 const char *name
, uid_t uid
, gid_t gid
)
1646 struct session_info
*session
= NULL
;
1647 struct cds_lfht_node
*node
;
1648 struct cds_lfht_iter iter
;
1649 struct lttng_session_trigger_list
*trigger_list
;
1652 cds_lfht_lookup(state
->sessions_ht
,
1653 hash_key_str(name
, lttng_ht_seed
),
1657 node
= cds_lfht_iter_get_node(&iter
);
1659 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1661 session
= caa_container_of(node
, struct session_info
,
1663 assert(session
->uid
== uid
);
1664 assert(session
->gid
== gid
);
1665 session_info_get(session
);
1669 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1670 if (!trigger_list
) {
1674 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1675 state
->sessions_ht
);
1677 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1679 lttng_session_trigger_list_destroy(trigger_list
);
1682 trigger_list
= NULL
;
1684 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1685 &session
->sessions_ht_node
);
1691 session_info_put(session
);
1696 int handle_notification_thread_command_add_channel(
1697 struct notification_thread_state
*state
,
1698 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1699 const char *channel_name
, enum lttng_domain_type channel_domain
,
1700 uint64_t channel_key_int
, uint64_t channel_capacity
,
1701 enum lttng_error_code
*cmd_result
)
1703 struct cds_list_head trigger_list
;
1704 struct channel_info
*new_channel_info
= NULL
;
1705 struct channel_key channel_key
= {
1706 .key
= channel_key_int
,
1707 .domain
= channel_domain
,
1709 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1710 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1711 int trigger_count
= 0;
1712 struct cds_lfht_iter iter
;
1713 struct session_info
*session_info
= NULL
;
1715 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1716 channel_name
, session_name
, channel_key_int
,
1717 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1719 CDS_INIT_LIST_HEAD(&trigger_list
);
1721 session_info
= find_or_create_session_info(state
, session_name
,
1722 session_uid
, session_gid
);
1723 if (!session_info
) {
1724 /* Allocation error or an internal error occurred. */
1728 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1729 channel_capacity
, session_info
);
1730 if (!new_channel_info
) {
1735 /* Build a list of all triggers applying to the new channel. */
1736 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1738 struct lttng_trigger_list_element
*new_element
;
1740 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1741 new_channel_info
)) {
1745 new_element
= zmalloc(sizeof(*new_element
));
1750 CDS_INIT_LIST_HEAD(&new_element
->node
);
1751 new_element
->trigger
= trigger_ht_element
->trigger
;
1752 cds_list_add(&new_element
->node
, &trigger_list
);
1757 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1759 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1760 if (!channel_trigger_list
) {
1763 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1764 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1765 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1766 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1769 /* Add channel to the channel_ht which owns the channel_infos. */
1770 cds_lfht_add(state
->channels_ht
,
1771 hash_channel_key(&new_channel_info
->key
),
1772 &new_channel_info
->channels_ht_node
);
1774 * Add the list of triggers associated with this channel to the
1775 * channel_triggers_ht.
1777 cds_lfht_add(state
->channel_triggers_ht
,
1778 hash_channel_key(&new_channel_info
->key
),
1779 &channel_trigger_list
->channel_triggers_ht_node
);
1781 session_info_put(session_info
);
1782 *cmd_result
= LTTNG_OK
;
1785 channel_info_destroy(new_channel_info
);
1786 session_info_put(session_info
);
1791 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1793 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1798 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1800 free(caa_container_of(node
, struct channel_state_sample
,
1805 int handle_notification_thread_command_remove_channel(
1806 struct notification_thread_state
*state
,
1807 uint64_t channel_key
, enum lttng_domain_type domain
,
1808 enum lttng_error_code
*cmd_result
)
1810 struct cds_lfht_node
*node
;
1811 struct cds_lfht_iter iter
;
1812 struct lttng_channel_trigger_list
*trigger_list
;
1813 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1814 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1815 struct channel_info
*channel_info
;
1817 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1818 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1822 cds_lfht_lookup(state
->channel_triggers_ht
,
1823 hash_channel_key(&key
),
1824 match_channel_trigger_list
,
1827 node
= cds_lfht_iter_get_node(&iter
);
1829 * There is a severe internal error if we are being asked to remove a
1830 * channel that doesn't exist.
1833 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1837 /* Free the list of triggers associated with this channel. */
1838 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1839 channel_triggers_ht_node
);
1840 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1841 &trigger_list
->list
, node
) {
1842 cds_list_del(&trigger_list_element
->node
);
1843 free(trigger_list_element
);
1845 cds_lfht_del(state
->channel_triggers_ht
, node
);
1846 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1848 /* Free sampled channel state. */
1849 cds_lfht_lookup(state
->channel_state_ht
,
1850 hash_channel_key(&key
),
1851 match_channel_state_sample
,
1854 node
= cds_lfht_iter_get_node(&iter
);
1856 * This is expected to be NULL if the channel is destroyed before we
1857 * received a sample.
1860 struct channel_state_sample
*sample
= caa_container_of(node
,
1861 struct channel_state_sample
,
1862 channel_state_ht_node
);
1864 cds_lfht_del(state
->channel_state_ht
, node
);
1865 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1868 /* Remove the channel from the channels_ht and free it. */
1869 cds_lfht_lookup(state
->channels_ht
,
1870 hash_channel_key(&key
),
1874 node
= cds_lfht_iter_get_node(&iter
);
1876 channel_info
= caa_container_of(node
, struct channel_info
,
1878 cds_lfht_del(state
->channels_ht
, node
);
1879 channel_info_destroy(channel_info
);
1882 *cmd_result
= LTTNG_OK
;
1887 int handle_notification_thread_command_session_rotation(
1888 struct notification_thread_state
*state
,
1889 enum notification_thread_command_type cmd_type
,
1890 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1891 uint64_t trace_archive_chunk_id
,
1892 struct lttng_trace_archive_location
*location
,
1893 enum lttng_error_code
*_cmd_result
)
1896 enum lttng_error_code cmd_result
= LTTNG_OK
;
1897 struct lttng_session_trigger_list
*trigger_list
;
1898 struct lttng_trigger_list_element
*trigger_list_element
;
1899 struct session_info
*session_info
;
1900 const struct lttng_credentials session_creds
= {
1901 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1902 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1907 session_info
= find_or_create_session_info(state
, session_name
,
1908 session_uid
, session_gid
);
1909 if (!session_info
) {
1910 /* Allocation error or an internal error occurred. */
1912 cmd_result
= LTTNG_ERR_NOMEM
;
1916 session_info
->rotation
.ongoing
=
1917 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1918 session_info
->rotation
.id
= trace_archive_chunk_id
;
1919 trigger_list
= get_session_trigger_list(state
, session_name
);
1920 if (!trigger_list
) {
1921 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1926 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1928 const struct lttng_condition
*condition
;
1929 struct lttng_trigger
*trigger
;
1930 struct notification_client_list
*client_list
;
1931 struct lttng_evaluation
*evaluation
= NULL
;
1932 enum lttng_condition_type condition_type
;
1933 enum action_executor_status executor_status
;
1935 trigger
= trigger_list_element
->trigger
;
1936 condition
= lttng_trigger_get_const_condition(trigger
);
1938 condition_type
= lttng_condition_get_type(condition
);
1940 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1941 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1943 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1944 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1948 client_list
= get_client_list_from_condition(state
, condition
);
1949 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1950 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1951 trace_archive_chunk_id
);
1953 evaluation
= lttng_evaluation_session_rotation_completed_create(
1954 trace_archive_chunk_id
, location
);
1958 /* Internal error */
1960 cmd_result
= LTTNG_ERR_UNK
;
1965 * Ownership of `evaluation` transferred to the action executor
1966 * no matter the result.
1968 executor_status
= action_executor_enqueue(state
->executor
,
1969 trigger
, evaluation
, &session_creds
,
1972 switch (executor_status
) {
1973 case ACTION_EXECUTOR_STATUS_OK
:
1975 case ACTION_EXECUTOR_STATUS_ERROR
:
1976 case ACTION_EXECUTOR_STATUS_INVALID
:
1978 * TODO Add trigger identification (name/id) when
1979 * it is added to the API.
1981 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1984 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1986 * TODO Add trigger identification (name/id) when
1987 * it is added to the API.
1989 * Not a fatal error.
1991 WARN("No space left when enqueuing action associated with session rotation trigger");
1999 notification_client_list_put(client_list
);
2000 if (caa_unlikely(ret
)) {
2005 session_info_put(session_info
);
2006 *_cmd_result
= cmd_result
;
2011 static int handle_notification_thread_command_list_triggers(
2012 struct notification_thread_handle
*handle
,
2013 struct notification_thread_state
*state
,
2015 struct lttng_triggers
**triggers
,
2016 enum lttng_error_code
*_cmd_result
)
2019 enum lttng_error_code cmd_result
= LTTNG_OK
;
2020 struct cds_lfht_iter iter
;
2021 struct lttng_trigger_ht_element
*trigger_ht_element
;
2022 struct lttng_triggers
*local_triggers
= NULL
;
2023 const struct lttng_credentials
*creds
;
2027 local_triggers
= lttng_triggers_create();
2028 if (!local_triggers
) {
2029 /* Not a fatal error. */
2030 cmd_result
= LTTNG_ERR_NOMEM
;
2034 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2035 trigger_ht_element
, node
) {
2037 * Only return the triggers to which the client has access.
2038 * The root user has visibility over all triggers.
2040 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2041 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2045 ret
= lttng_triggers_add(local_triggers
,
2046 trigger_ht_element
->trigger
);
2048 /* Not a fatal error. */
2050 cmd_result
= LTTNG_ERR_NOMEM
;
2055 /* Transferring ownership to the caller. */
2056 *triggers
= local_triggers
;
2057 local_triggers
= NULL
;
2061 lttng_triggers_destroy(local_triggers
);
2062 *_cmd_result
= cmd_result
;
2067 bool condition_is_supported(struct lttng_condition
*condition
)
2071 switch (lttng_condition_get_type(condition
)) {
2072 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2073 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2076 enum lttng_domain_type domain
;
2078 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2082 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2083 is_supported
= true;
2088 * Older kernel tracers don't expose the API to monitor their
2089 * buffers. Therefore, we reject triggers that require that
2090 * mechanism to be available to be evaluated.
2092 * Assume unsupported on error.
2094 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2097 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
2099 const struct lttng_event_rule
*event_rule
;
2100 enum lttng_domain_type domain
;
2101 const enum lttng_condition_status status
=
2102 lttng_condition_event_rule_get_rule(
2103 condition
, &event_rule
);
2105 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2107 domain
= lttng_event_rule_get_domain_type(event_rule
);
2108 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2109 is_supported
= true;
2114 * Older kernel tracers can't emit notification. Therefore, we
2115 * reject triggers that require that mechanism to be available
2118 * Assume unsupported on error.
2120 is_supported
= kernel_supports_event_notifiers() == 1;
2124 is_supported
= true;
2127 return is_supported
;
2130 /* Must be called with RCU read lock held. */
2132 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2133 struct notification_thread_state
*state
)
2136 const struct lttng_condition
*condition
;
2137 const char *session_name
;
2138 struct lttng_session_trigger_list
*trigger_list
;
2140 condition
= lttng_trigger_get_const_condition(trigger
);
2141 switch (lttng_condition_get_type(condition
)) {
2142 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2143 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2145 enum lttng_condition_status status
;
2147 status
= lttng_condition_session_rotation_get_session_name(
2148 condition
, &session_name
);
2149 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2150 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2161 trigger_list
= get_session_trigger_list(state
, session_name
);
2162 if (!trigger_list
) {
2163 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2169 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2171 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2176 /* Must be called with RCU read lock held. */
2178 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2179 struct notification_thread_state
*state
)
2182 struct cds_lfht_node
*node
;
2183 struct cds_lfht_iter iter
;
2184 struct channel_info
*channel
;
2186 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2188 struct lttng_trigger_list_element
*trigger_list_element
;
2189 struct lttng_channel_trigger_list
*trigger_list
;
2190 struct cds_lfht_iter lookup_iter
;
2192 if (!trigger_applies_to_channel(trigger
, channel
)) {
2196 cds_lfht_lookup(state
->channel_triggers_ht
,
2197 hash_channel_key(&channel
->key
),
2198 match_channel_trigger_list
,
2201 node
= cds_lfht_iter_get_node(&lookup_iter
);
2203 trigger_list
= caa_container_of(node
,
2204 struct lttng_channel_trigger_list
,
2205 channel_triggers_ht_node
);
2207 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2208 if (!trigger_list_element
) {
2212 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2213 trigger_list_element
->trigger
= trigger
;
2214 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2215 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2223 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2225 bool is_notify
= false;
2226 unsigned int i
, count
;
2227 enum lttng_action_status action_status
;
2228 const struct lttng_action
*action
=
2229 lttng_trigger_get_const_action(trigger
);
2230 enum lttng_action_type action_type
;
2233 action_type
= lttng_action_get_type(action
);
2234 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2237 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2241 action_status
= lttng_action_group_get_count(action
, &count
);
2242 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2244 for (i
= 0; i
< count
; i
++) {
2245 const struct lttng_action
*inner_action
=
2246 lttng_action_group_get_at_index(
2249 action_type
= lttng_action_get_type(inner_action
);
2250 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2260 static bool trigger_name_taken(struct notification_thread_state
*state
,
2261 const struct lttng_trigger
*trigger
)
2263 struct cds_lfht_iter iter
;
2266 * No duplicata is allowed in the triggers_by_name_uid_ht.
2267 * The match is done against the trigger name and uid.
2269 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2270 hash_trigger_by_name_uid(trigger
),
2271 match_trigger_by_name_uid
,
2274 return !!cds_lfht_iter_get_node(&iter
);
2278 enum lttng_error_code
generate_trigger_name(
2279 struct notification_thread_state
*state
,
2280 struct lttng_trigger
*trigger
, const char **name
)
2282 enum lttng_error_code ret_code
= LTTNG_OK
;
2284 enum lttng_trigger_status status
;
2287 const int ret
= lttng_trigger_generate_name(trigger
,
2288 state
->trigger_id
.name_offset
++);
2290 /* The only reason this can fail right now. */
2291 ret_code
= LTTNG_ERR_NOMEM
;
2295 status
= lttng_trigger_get_name(trigger
, name
);
2296 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2298 taken
= trigger_name_taken(state
, trigger
);
2299 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2305 * FIXME A client's credentials are not checked when registering a trigger.
2307 * The effects of this are benign since:
2308 * - The client will succeed in registering the trigger, as it is valid,
2309 * - The trigger will, internally, be bound to the channel/session,
2310 * - The notifications will not be sent since the client's credentials
2311 * are checked against the channel at that moment.
2313 * If this function returns a non-zero value, it means something is
2314 * fundamentally broken and the whole subsystem/thread will be torn down.
2316 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2320 int handle_notification_thread_command_register_trigger(
2321 struct notification_thread_state
*state
,
2322 struct lttng_trigger
*trigger
,
2323 enum lttng_error_code
*cmd_result
)
2326 struct lttng_condition
*condition
;
2327 struct notification_client
*client
;
2328 struct notification_client_list
*client_list
= NULL
;
2329 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2330 struct notification_client_list_element
*client_list_element
;
2331 struct cds_lfht_node
*node
;
2332 struct cds_lfht_iter iter
;
2333 const char* trigger_name
;
2334 bool free_trigger
= true;
2335 struct lttng_evaluation
*evaluation
= NULL
;
2336 struct lttng_credentials object_creds
;
2339 enum action_executor_status executor_status
;
2340 const uint64_t trigger_tracer_token
=
2341 state
->trigger_id
.next_tracer_token
++;
2345 /* Set the trigger's tracer token. */
2346 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2348 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2349 LTTNG_TRIGGER_STATUS_UNSET
) {
2350 const enum lttng_error_code ret_code
= generate_trigger_name(
2351 state
, trigger
, &trigger_name
);
2353 if (ret_code
!= LTTNG_OK
) {
2356 *cmd_result
= ret_code
;
2359 } else if (trigger_name_taken(state
, trigger
)) {
2360 /* Not a fatal error. */
2361 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2366 condition
= lttng_trigger_get_condition(trigger
);
2369 /* Some conditions require tracers to implement a minimal ABI version. */
2370 if (!condition_is_supported(condition
)) {
2371 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2375 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2376 if (!trigger_ht_element
) {
2381 /* Add trigger to the trigger_ht. */
2382 cds_lfht_node_init(&trigger_ht_element
->node
);
2383 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2384 trigger_ht_element
->trigger
= trigger
;
2386 node
= cds_lfht_add_unique(state
->triggers_ht
,
2387 lttng_condition_hash(condition
),
2390 &trigger_ht_element
->node
);
2391 if (node
!= &trigger_ht_element
->node
) {
2392 /* Not a fatal error, simply report it to the client. */
2393 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2394 goto error_free_ht_element
;
2397 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2398 hash_trigger_by_name_uid(trigger
),
2399 match_trigger_by_name_uid
,
2401 &trigger_ht_element
->node_by_name_uid
);
2402 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2403 /* Not a fatal error, simply report it to the client. */
2404 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2405 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2406 goto error_free_ht_element
;
2410 * Ownership of the trigger and of its wrapper was transfered to
2413 trigger_ht_element
= NULL
;
2414 free_trigger
= false;
2417 * The rest only applies to triggers that have a "notify" action.
2418 * It is not skipped as this is the only action type currently
2421 if (is_trigger_action_notify(trigger
)) {
2422 client_list
= notification_client_list_create(trigger
);
2425 goto error_free_ht_element
;
2428 /* Build a list of clients to which this new trigger applies. */
2429 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2430 client_socket_ht_node
) {
2431 if (!trigger_applies_to_client(trigger
, client
)) {
2435 client_list_element
=
2436 zmalloc(sizeof(*client_list_element
));
2437 if (!client_list_element
) {
2439 goto error_put_client_list
;
2442 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2443 client_list_element
->client
= client
;
2444 cds_list_add(&client_list_element
->node
,
2445 &client_list
->list
);
2449 * Client list ownership transferred to the
2450 * notification_trigger_clients_ht.
2452 publish_notification_client_list(state
, client_list
);
2455 switch (get_condition_binding_object(condition
)) {
2456 case LTTNG_OBJECT_TYPE_SESSION
:
2457 /* Add the trigger to the list if it matches a known session. */
2458 ret
= bind_trigger_to_matching_session(trigger
, state
);
2460 goto error_put_client_list
;
2463 case LTTNG_OBJECT_TYPE_CHANNEL
:
2465 * Add the trigger to list of triggers bound to the channels
2468 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2470 goto error_put_client_list
;
2473 case LTTNG_OBJECT_TYPE_NONE
:
2476 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2478 goto error_put_client_list
;
2482 * The new trigger's condition must be evaluated against the current
2485 * In the case of `notify` action, nothing preventing clients from
2486 * subscribing to a condition before the corresponding trigger is
2487 * registered, we have to evaluate this new condition right away.
2489 * At some point, we were waiting for the next "evaluation" (e.g. on
2490 * reception of a channel sample) to evaluate this new condition, but
2493 * The reason it was broken is that waiting for the next sample
2494 * does not allow us to properly handle transitions for edge-triggered
2497 * Consider this example: when we handle a new channel sample, we
2498 * evaluate each conditions twice: once with the previous state, and
2499 * again with the newest state. We then use those two results to
2500 * determine whether a state change happened: a condition was false and
2501 * became true. If a state change happened, we have to notify clients.
2503 * Now, if a client subscribes to a given notification and registers
2504 * a trigger *after* that subscription, we have to make sure the
2505 * condition is evaluated at this point while considering only the
2506 * current state. Otherwise, the next evaluation cycle may only see
2507 * that the evaluations remain the same (true for samples n-1 and n) and
2508 * the client will never know that the condition has been met.
2510 switch (get_condition_binding_object(condition
)) {
2511 case LTTNG_OBJECT_TYPE_SESSION
:
2512 ret
= evaluate_session_condition_for_client(condition
, state
,
2513 &evaluation
, &object_uid
,
2516 case LTTNG_OBJECT_TYPE_CHANNEL
:
2517 ret
= evaluate_channel_condition_for_client(condition
, state
,
2518 &evaluation
, &object_uid
,
2521 case LTTNG_OBJECT_TYPE_NONE
:
2524 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2532 goto error_put_client_list
;
2535 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2536 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2538 DBG("Newly registered trigger's condition evaluated to %s",
2539 evaluation
? "true" : "false");
2541 /* Evaluation yielded nothing. Normal exit. */
2547 * Ownership of `evaluation` transferred to the action executor
2548 * no matter the result.
2550 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2551 evaluation
, &object_creds
, client_list
);
2553 switch (executor_status
) {
2554 case ACTION_EXECUTOR_STATUS_OK
:
2556 case ACTION_EXECUTOR_STATUS_ERROR
:
2557 case ACTION_EXECUTOR_STATUS_INVALID
:
2559 * TODO Add trigger identification (name/id) when
2560 * it is added to the API.
2562 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2564 goto error_put_client_list
;
2565 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2567 * TODO Add trigger identification (name/id) when
2568 * it is added to the API.
2570 * Not a fatal error.
2572 WARN("No space left when enqueuing action associated to newly registered trigger");
2580 *cmd_result
= LTTNG_OK
;
2581 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2582 trigger_name
, trigger_tracer_token
);
2584 error_put_client_list
:
2585 notification_client_list_put(client_list
);
2587 error_free_ht_element
:
2588 if (trigger_ht_element
) {
2589 /* Delayed removal due to RCU constraint on delete. */
2590 call_rcu(&trigger_ht_element
->rcu_node
,
2591 free_lttng_trigger_ht_element_rcu
);
2596 lttng_trigger_destroy(trigger
);
2603 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2605 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2610 int handle_notification_thread_command_unregister_trigger(
2611 struct notification_thread_state
*state
,
2612 struct lttng_trigger
*trigger
,
2613 enum lttng_error_code
*_cmd_reply
)
2615 struct cds_lfht_iter iter
;
2616 struct cds_lfht_node
*triggers_ht_node
;
2617 struct lttng_channel_trigger_list
*trigger_list
;
2618 struct notification_client_list
*client_list
;
2619 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2620 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2622 enum lttng_error_code cmd_reply
;
2626 cds_lfht_lookup(state
->triggers_ht
,
2627 lttng_condition_hash(condition
),
2631 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2632 if (!triggers_ht_node
) {
2633 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2636 cmd_reply
= LTTNG_OK
;
2639 /* Remove trigger from channel_triggers_ht. */
2640 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2641 channel_triggers_ht_node
) {
2642 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2644 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2645 &trigger_list
->list
, node
) {
2646 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2650 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2651 cds_list_del(&trigger_element
->node
);
2652 /* A trigger can only appear once per channel */
2657 if (is_trigger_action_notify(trigger
)) {
2659 * Remove and release the client list from
2660 * notification_trigger_clients_ht.
2662 client_list
= get_client_list_from_condition(state
, condition
);
2663 assert(client_list
);
2665 /* Put new reference and the hashtable's reference. */
2666 notification_client_list_put(client_list
);
2667 notification_client_list_put(client_list
);
2671 /* Remove trigger from triggers_ht. */
2672 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2673 struct lttng_trigger_ht_element
, node
);
2674 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2675 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2677 /* Release the ownership of the trigger. */
2678 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2679 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2683 *_cmd_reply
= cmd_reply
;
2688 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2689 int handle_notification_thread_command(
2690 struct notification_thread_handle
*handle
,
2691 struct notification_thread_state
*state
)
2695 struct notification_thread_command
*cmd
;
2697 /* Read the event pipe to put it back into a quiescent state. */
2698 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2700 if (ret
!= sizeof(counter
)) {
2704 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2705 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2706 struct notification_thread_command
, cmd_list_node
);
2707 cds_list_del(&cmd
->cmd_list_node
);
2708 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2709 switch (cmd
->type
) {
2710 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2711 DBG("[notification-thread] Received register trigger command");
2712 ret
= handle_notification_thread_command_register_trigger(
2713 state
, cmd
->parameters
.trigger
,
2716 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2717 DBG("[notification-thread] Received unregister trigger command");
2718 ret
= handle_notification_thread_command_unregister_trigger(
2719 state
, cmd
->parameters
.trigger
,
2722 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2723 DBG("[notification-thread] Received add channel command");
2724 ret
= handle_notification_thread_command_add_channel(
2726 cmd
->parameters
.add_channel
.session
.name
,
2727 cmd
->parameters
.add_channel
.session
.uid
,
2728 cmd
->parameters
.add_channel
.session
.gid
,
2729 cmd
->parameters
.add_channel
.channel
.name
,
2730 cmd
->parameters
.add_channel
.channel
.domain
,
2731 cmd
->parameters
.add_channel
.channel
.key
,
2732 cmd
->parameters
.add_channel
.channel
.capacity
,
2735 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2736 DBG("[notification-thread] Received remove channel command");
2737 ret
= handle_notification_thread_command_remove_channel(
2738 state
, cmd
->parameters
.remove_channel
.key
,
2739 cmd
->parameters
.remove_channel
.domain
,
2742 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2743 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2744 DBG("[notification-thread] Received session rotation %s command",
2745 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2746 "ongoing" : "completed");
2747 ret
= handle_notification_thread_command_session_rotation(
2750 cmd
->parameters
.session_rotation
.session_name
,
2751 cmd
->parameters
.session_rotation
.uid
,
2752 cmd
->parameters
.session_rotation
.gid
,
2753 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2754 cmd
->parameters
.session_rotation
.location
,
2757 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2759 struct lttng_triggers
*triggers
= NULL
;
2761 ret
= handle_notification_thread_command_list_triggers(
2764 cmd
->parameters
.list_triggers
.uid
,
2767 cmd
->reply
.list_triggers
.triggers
= triggers
;
2771 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2772 DBG("[notification-thread] Received quit command");
2773 cmd
->reply_code
= LTTNG_OK
;
2776 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2778 const enum client_transmission_status client_status
=
2779 cmd
->parameters
.client_communication_update
2781 const notification_client_id client_id
=
2782 cmd
->parameters
.client_communication_update
.id
;
2783 struct notification_client
*client
;
2786 client
= get_client_from_id(client_id
, state
);
2790 * Client error was probably already picked-up by the
2791 * notification thread or it has disconnected
2792 * gracefully while this command was queued.
2794 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2798 ret
= client_handle_transmission_status(
2799 client
, client_status
, state
);
2805 ERR("[notification-thread] Unknown internal command received");
2813 if (cmd
->is_async
) {
2817 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2821 /* Wake-up and return a fatal error to the calling thread. */
2822 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2823 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2825 /* Indicate a fatal error to the caller. */
2830 int socket_set_non_blocking(int socket
)
2834 /* Set the pipe as non-blocking. */
2835 ret
= fcntl(socket
, F_GETFL
, 0);
2837 PERROR("fcntl get socket flags");
2842 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2844 PERROR("fcntl set O_NONBLOCK socket flag");
2847 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2853 int client_reset_inbound_state(struct notification_client
*client
)
2858 lttng_payload_clear(&client
->communication
.inbound
.payload
);
2860 client
->communication
.inbound
.bytes_to_receive
=
2861 sizeof(struct lttng_notification_channel_message
);
2862 client
->communication
.inbound
.msg_type
=
2863 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2864 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2865 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2866 ret
= lttng_dynamic_buffer_set_size(
2867 &client
->communication
.inbound
.payload
.buffer
,
2868 client
->communication
.inbound
.bytes_to_receive
);
2873 int handle_notification_thread_client_connect(
2874 struct notification_thread_state
*state
)
2877 struct notification_client
*client
;
2879 DBG("[notification-thread] Handling new notification channel client connection");
2881 client
= zmalloc(sizeof(*client
));
2888 pthread_mutex_init(&client
->lock
, NULL
);
2889 client
->id
= state
->next_notification_client_id
++;
2890 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2891 lttng_payload_init(&client
->communication
.inbound
.payload
);
2892 lttng_payload_init(&client
->communication
.outbound
.payload
);
2893 client
->communication
.inbound
.expect_creds
= true;
2895 ret
= client_reset_inbound_state(client
);
2897 ERR("[notification-thread] Failed to reset client communication's inbound state");
2902 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2904 ERR("[notification-thread] Failed to accept new notification channel client connection");
2909 client
->socket
= ret
;
2911 ret
= socket_set_non_blocking(client
->socket
);
2913 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2917 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2919 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2924 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2925 LPOLLIN
| LPOLLERR
|
2926 LPOLLHUP
| LPOLLRDHUP
);
2928 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2932 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2936 cds_lfht_add(state
->client_socket_ht
,
2937 hash_client_socket(client
->socket
),
2938 &client
->client_socket_ht_node
);
2939 cds_lfht_add(state
->client_id_ht
,
2940 hash_client_id(client
->id
),
2941 &client
->client_id_ht_node
);
2947 notification_client_destroy(client
, state
);
2952 * RCU read-lock must be held by the caller.
2953 * Client lock must _not_ be held by the caller.
2956 int notification_thread_client_disconnect(
2957 struct notification_client
*client
,
2958 struct notification_thread_state
*state
)
2961 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
2963 /* Acquire the client lock to disable its communication atomically. */
2964 pthread_mutex_lock(&client
->lock
);
2965 client
->communication
.active
= false;
2966 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
2967 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
2968 pthread_mutex_unlock(&client
->lock
);
2970 ret
= lttng_poll_del(&state
->events
, client
->socket
);
2972 ERR("[notification-thread] Failed to remove client socket %d from poll set",
2976 /* Release all conditions to which the client was subscribed. */
2977 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
2978 &client
->condition_list
, node
) {
2979 (void) notification_thread_client_unsubscribe(client
,
2980 condition_list_element
->condition
, state
, NULL
);
2984 * Client no longer accessible to other threads (through the
2987 notification_client_destroy(client
, state
);
2991 int handle_notification_thread_client_disconnect(
2992 int client_socket
, struct notification_thread_state
*state
)
2995 struct notification_client
*client
;
2998 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3000 client
= get_client_from_socket(client_socket
, state
);
3002 /* Internal state corruption, fatal error. */
3003 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3009 ret
= notification_thread_client_disconnect(client
, state
);
3015 int handle_notification_thread_client_disconnect_all(
3016 struct notification_thread_state
*state
)
3018 struct cds_lfht_iter iter
;
3019 struct notification_client
*client
;
3020 bool error_encoutered
= false;
3023 DBG("[notification-thread] Closing all client connections");
3024 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3025 client_socket_ht_node
) {
3028 ret
= notification_thread_client_disconnect(
3031 error_encoutered
= true;
3035 return error_encoutered
? 1 : 0;
3038 int handle_notification_thread_trigger_unregister_all(
3039 struct notification_thread_state
*state
)
3041 bool error_occurred
= false;
3042 struct cds_lfht_iter iter
;
3043 struct lttng_trigger_ht_element
*trigger_ht_element
;
3046 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3048 int ret
= handle_notification_thread_command_unregister_trigger(
3049 state
, trigger_ht_element
->trigger
, NULL
);
3051 error_occurred
= true;
3055 return error_occurred
? -1 : 0;
3059 int client_handle_transmission_status(
3060 struct notification_client
*client
,
3061 enum client_transmission_status transmission_status
,
3062 struct notification_thread_state
*state
)
3066 switch (transmission_status
) {
3067 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3068 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3069 CLIENT_POLL_MASK_IN
);
3075 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3077 * We want to be notified whenever there is buffer space
3078 * available to send the rest of the payload.
3080 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3081 CLIENT_POLL_MASK_IN_OUT
);
3086 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3087 ret
= notification_thread_client_disconnect(client
, state
);
3092 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3102 /* Client lock must be acquired by caller. */
3104 enum client_transmission_status
client_flush_outgoing_queue(
3105 struct notification_client
*client
)
3108 size_t to_send_count
;
3109 enum client_transmission_status status
;
3110 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3111 &client
->communication
.outbound
.payload
, 0, -1);
3112 const int fds_to_send_count
=
3113 lttng_payload_view_get_fd_handle_count(&pv
);
3115 ASSERT_LOCKED(client
->lock
);
3117 if (!client
->communication
.active
) {
3118 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3122 if (pv
.buffer
.size
== 0) {
3124 * If both data and fds are equal to zero, we are in an invalid
3127 assert(fds_to_send_count
!= 0);
3132 to_send_count
= pv
.buffer
.size
;
3133 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3136 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3139 if ((ret
>= 0 && ret
< to_send_count
)) {
3140 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3142 to_send_count
-= max(ret
, 0);
3144 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3146 pv
.buffer
.size
- to_send_count
,
3148 ret
= lttng_dynamic_buffer_set_size(
3149 &client
->communication
.outbound
.payload
.buffer
,
3155 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3157 } else if (ret
< 0) {
3158 /* Generic error, disable the client's communication. */
3159 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3161 client
->communication
.active
= false;
3162 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3166 * No error and flushed the queue completely.
3168 * The payload buffer size is used later to
3169 * check if there is notifications queued. So albeit that the
3170 * direct caller knows that the transmission is complete, we
3171 * need to set the buffer size to zero.
3173 ret
= lttng_dynamic_buffer_set_size(
3174 &client
->communication
.outbound
.payload
.buffer
, 0);
3181 /* No fds to send, transmission is complete. */
3182 if (fds_to_send_count
== 0) {
3183 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3187 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3188 client
->socket
, &pv
);
3190 /* Generic error, disable the client's communication. */
3191 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3193 client
->communication
.active
= false;
3194 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3196 } else if (ret
== 0) {
3197 /* Nothing could be sent. */
3198 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3200 /* Fd passing is an all or nothing kind of thing. */
3201 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3203 * The payload _fd_array count is used later to
3204 * check if there is notifications queued. So although the
3205 * direct caller knows that the transmission is complete, we
3206 * need to clear the _fd_array for the queuing check.
3208 lttng_dynamic_pointer_array_clear(
3209 &client
->communication
.outbound
.payload
3214 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3215 client
->communication
.outbound
.queued_command_reply
= false;
3216 client
->communication
.outbound
.dropped_notification
= false;
3217 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3222 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3226 bool client_has_outbound_data_left(
3227 const struct notification_client
*client
)
3229 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3230 &client
->communication
.outbound
.payload
, 0, -1);
3231 const bool has_data
= pv
.buffer
.size
!= 0;
3232 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3234 return has_data
|| has_fds
;
3237 /* Client lock must _not_ be held by the caller. */
3239 int client_send_command_reply(struct notification_client
*client
,
3240 struct notification_thread_state
*state
,
3241 enum lttng_notification_channel_status status
)
3244 struct lttng_notification_channel_command_reply reply
= {
3245 .status
= (int8_t) status
,
3247 struct lttng_notification_channel_message msg
= {
3248 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3249 .size
= sizeof(reply
),
3251 char buffer
[sizeof(msg
) + sizeof(reply
)];
3252 enum client_transmission_status transmission_status
;
3254 memcpy(buffer
, &msg
, sizeof(msg
));
3255 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3256 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3258 pthread_mutex_lock(&client
->lock
);
3259 if (client
->communication
.outbound
.queued_command_reply
) {
3260 /* Protocol error. */
3264 /* Enqueue buffer to outgoing queue and flush it. */
3265 ret
= lttng_dynamic_buffer_append(
3266 &client
->communication
.outbound
.payload
.buffer
,
3267 buffer
, sizeof(buffer
));
3272 transmission_status
= client_flush_outgoing_queue(client
);
3274 if (client_has_outbound_data_left(client
)) {
3275 /* Queue could not be emptied. */
3276 client
->communication
.outbound
.queued_command_reply
= true;
3279 pthread_mutex_unlock(&client
->lock
);
3280 ret
= client_handle_transmission_status(
3281 client
, transmission_status
, state
);
3288 pthread_mutex_unlock(&client
->lock
);
3294 int client_handle_message_unknown(struct notification_client
*client
,
3295 struct notification_thread_state
*state
)
3299 * Receiving message header. The function will be called again
3300 * once the rest of the message as been received and can be
3303 const struct lttng_notification_channel_message
*msg
;
3305 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3306 msg
= (const struct lttng_notification_channel_message
*)
3307 client
->communication
.inbound
.payload
.buffer
.data
;
3309 if (msg
->size
== 0 ||
3310 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3311 ERR("[notification-thread] Invalid notification channel message: length = %u",
3317 switch (msg
->type
) {
3318 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3319 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3320 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3324 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3328 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3329 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3330 client
->communication
.inbound
.msg_type
=
3331 (enum lttng_notification_channel_message_type
) msg
->type
;
3332 ret
= lttng_dynamic_buffer_set_size(
3333 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3335 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3342 int client_handle_message_handshake(struct notification_client
*client
,
3343 struct notification_thread_state
*state
)
3346 struct lttng_notification_channel_command_handshake
*handshake_client
;
3347 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3348 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3349 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3351 const struct lttng_notification_channel_message msg_header
= {
3352 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3353 .size
= sizeof(handshake_reply
),
3355 enum lttng_notification_channel_status status
=
3356 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3357 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3359 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3360 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3361 sizeof(handshake_reply
));
3364 (struct lttng_notification_channel_command_handshake
*)
3365 client
->communication
.inbound
.payload
.buffer
3367 client
->major
= handshake_client
->major
;
3368 client
->minor
= handshake_client
->minor
;
3369 if (!client
->communication
.inbound
.creds_received
) {
3370 ERR("[notification-thread] No credentials received from client");
3375 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3376 &client
->communication
.inbound
.creds
);
3377 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3378 &client
->communication
.inbound
.creds
);
3379 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3380 client
->uid
, client
->gid
, (int) client
->major
,
3381 (int) client
->minor
);
3383 if (handshake_client
->major
!=
3384 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3385 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3388 pthread_mutex_lock(&client
->lock
);
3389 /* Outgoing queue will be flushed when the command reply is sent. */
3390 ret
= lttng_dynamic_buffer_append(
3391 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3392 sizeof(send_buffer
));
3394 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3398 client
->validated
= true;
3399 client
->communication
.active
= true;
3400 pthread_mutex_unlock(&client
->lock
);
3402 /* Set reception state to receive the next message header. */
3403 ret
= client_reset_inbound_state(client
);
3405 ERR("[notification-thread] Failed to reset client communication's inbound state");
3409 /* Flushes the outgoing queue. */
3410 ret
= client_send_command_reply(client
, state
, status
);
3412 ERR("[notification-thread] Failed to send reply to notification channel client");
3418 pthread_mutex_unlock(&client
->lock
);
3424 int client_handle_message_subscription(
3425 struct notification_client
*client
,
3426 enum lttng_notification_channel_message_type msg_type
,
3427 struct notification_thread_state
*state
)
3430 struct lttng_condition
*condition
;
3431 enum lttng_notification_channel_status status
=
3432 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3433 struct lttng_payload_view condition_view
=
3434 lttng_payload_view_from_payload(
3435 &client
->communication
.inbound
.payload
,
3437 size_t expected_condition_size
;
3440 * No need to lock client to sample the inbound state as the only
3441 * other thread accessing clients (action executor) only uses the
3444 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3445 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3446 if (ret
!= expected_condition_size
) {
3447 ERR("[notification-thread] Malformed condition received from client");
3451 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3452 ret
= notification_thread_client_subscribe(
3453 client
, condition
, state
, &status
);
3455 ret
= notification_thread_client_unsubscribe(
3456 client
, condition
, state
, &status
);
3463 /* Set reception state to receive the next message header. */
3464 ret
= client_reset_inbound_state(client
);
3466 ERR("[notification-thread] Failed to reset client communication's inbound state");
3470 ret
= client_send_command_reply(client
, state
, status
);
3472 ERR("[notification-thread] Failed to send reply to notification channel client");
3481 int client_dispatch_message(struct notification_client
*client
,
3482 struct notification_thread_state
*state
)
3486 if (client
->communication
.inbound
.msg_type
!=
3487 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3488 client
->communication
.inbound
.msg_type
!=
3489 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3490 !client
->validated
) {
3491 WARN("[notification-thread] client attempted a command before handshake");
3496 switch (client
->communication
.inbound
.msg_type
) {
3497 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3499 ret
= client_handle_message_unknown(client
, state
);
3502 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3504 ret
= client_handle_message_handshake(client
, state
);
3507 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3508 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3510 ret
= client_handle_message_subscription(client
,
3511 client
->communication
.inbound
.msg_type
, state
);
3521 /* Incoming data from client. */
3522 int handle_notification_thread_client_in(
3523 struct notification_thread_state
*state
, int socket
)
3526 struct notification_client
*client
;
3531 client
= get_client_from_socket(socket
, state
);
3533 /* Internal error, abort. */
3538 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3539 client
->communication
.inbound
.bytes_to_receive
;
3540 if (client
->communication
.inbound
.expect_creds
) {
3541 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3542 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3543 client
->communication
.inbound
.bytes_to_receive
,
3544 &client
->communication
.inbound
.creds
);
3546 client
->communication
.inbound
.expect_creds
= false;
3547 client
->communication
.inbound
.creds_received
= true;
3550 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3551 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3552 client
->communication
.inbound
.bytes_to_receive
);
3554 if (recv_ret
>= 0) {
3555 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3557 goto error_disconnect_client
;
3560 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3561 /* Message incomplete wait for more data. */
3566 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3569 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3570 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3572 client
->communication
.inbound
.fds_to_receive
,
3573 &client
->communication
.inbound
.payload
);
3576 * Fds received. non blocking fds passing is all
3579 ssize_t expected_size
;
3581 expected_size
= sizeof(int) *
3582 client
->communication
.inbound
3584 assert(ret
== expected_size
);
3585 client
->communication
.inbound
.fds_to_receive
= 0;
3586 } else if (ret
== 0) {
3587 /* Received nothing. */
3591 goto error_disconnect_client
;
3595 /* At this point the message is complete.*/
3596 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3597 client
->communication
.inbound
.fds_to_receive
== 0);
3598 ret
= client_dispatch_message(client
, state
);
3601 * Only returns an error if this client must be
3604 goto error_disconnect_client
;
3611 error_disconnect_client
:
3612 ret
= notification_thread_client_disconnect(client
, state
);
3616 /* Client ready to receive outgoing data. */
3617 int handle_notification_thread_client_out(
3618 struct notification_thread_state
*state
, int socket
)
3621 struct notification_client
*client
;
3622 enum client_transmission_status transmission_status
;
3625 client
= get_client_from_socket(socket
, state
);
3627 /* Internal error, abort. */
3632 pthread_mutex_lock(&client
->lock
);
3633 transmission_status
= client_flush_outgoing_queue(client
);
3634 pthread_mutex_unlock(&client
->lock
);
3636 ret
= client_handle_transmission_status(
3637 client
, transmission_status
, state
);
3647 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3648 const struct channel_state_sample
*sample
,
3649 uint64_t buffer_capacity
)
3651 bool result
= false;
3653 enum lttng_condition_type condition_type
;
3654 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3655 condition
, struct lttng_condition_buffer_usage
,
3658 if (use_condition
->threshold_bytes
.set
) {
3659 threshold
= use_condition
->threshold_bytes
.value
;
3662 * Threshold was expressed as a ratio.
3664 * TODO the threshold (in bytes) of conditions expressed
3665 * as a ratio of total buffer size could be cached to
3666 * forego this double-multiplication or it could be performed
3667 * as fixed-point math.
3669 * Note that caching should accommodates the case where the
3670 * condition applies to multiple channels (i.e. don't assume
3671 * that all channels matching my_chann* have the same size...)
3673 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3674 (double) buffer_capacity
);
3677 condition_type
= lttng_condition_get_type(condition
);
3678 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3679 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3680 threshold
, sample
->highest_usage
);
3683 * The low condition should only be triggered once _all_ of the
3684 * streams in a channel have gone below the "low" threshold.
3686 if (sample
->highest_usage
<= threshold
) {
3690 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3691 threshold
, sample
->highest_usage
);
3694 * For high buffer usage scenarios, we want to trigger whenever
3695 * _any_ of the streams has reached the "high" threshold.
3697 if (sample
->highest_usage
>= threshold
) {
3706 bool evaluate_session_consumed_size_condition(
3707 const struct lttng_condition
*condition
,
3708 uint64_t session_consumed_size
)
3711 const struct lttng_condition_session_consumed_size
*size_condition
=
3712 container_of(condition
,
3713 struct lttng_condition_session_consumed_size
,
3716 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3717 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3718 threshold
, session_consumed_size
);
3719 return session_consumed_size
>= threshold
;
3723 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3724 struct lttng_evaluation
**evaluation
,
3725 const struct notification_thread_state
*state
,
3726 const struct channel_state_sample
*previous_sample
,
3727 const struct channel_state_sample
*latest_sample
,
3728 uint64_t previous_session_consumed_total
,
3729 uint64_t latest_session_consumed_total
,
3730 struct channel_info
*channel_info
)
3733 enum lttng_condition_type condition_type
;
3734 const bool previous_sample_available
= !!previous_sample
;
3735 bool previous_sample_result
= false;
3736 bool latest_sample_result
;
3738 condition_type
= lttng_condition_get_type(condition
);
3740 switch (condition_type
) {
3741 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3742 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3743 if (caa_likely(previous_sample_available
)) {
3744 previous_sample_result
=
3745 evaluate_buffer_usage_condition(condition
,
3746 previous_sample
, channel_info
->capacity
);
3748 latest_sample_result
= evaluate_buffer_usage_condition(
3749 condition
, latest_sample
,
3750 channel_info
->capacity
);
3752 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3753 if (caa_likely(previous_sample_available
)) {
3754 previous_sample_result
=
3755 evaluate_session_consumed_size_condition(
3757 previous_session_consumed_total
);
3759 latest_sample_result
=
3760 evaluate_session_consumed_size_condition(
3762 latest_session_consumed_total
);
3765 /* Unknown condition type; internal error. */
3769 if (!latest_sample_result
||
3770 (previous_sample_result
== latest_sample_result
)) {
3772 * Only trigger on a condition evaluation transition.
3774 * NOTE: This edge-triggered logic may not be appropriate for
3775 * future condition types.
3780 if (!evaluation
|| !latest_sample_result
) {
3784 switch (condition_type
) {
3785 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3786 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3787 *evaluation
= lttng_evaluation_buffer_usage_create(
3789 latest_sample
->highest_usage
,
3790 channel_info
->capacity
);
3792 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3793 *evaluation
= lttng_evaluation_session_consumed_size_create(
3794 latest_session_consumed_total
);
3809 int client_notification_overflow(struct notification_client
*client
)
3812 const struct lttng_notification_channel_message msg
= {
3813 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3816 ASSERT_LOCKED(client
->lock
);
3818 DBG("Dropping notification addressed to client (socket fd = %i)",
3820 if (client
->communication
.outbound
.dropped_notification
) {
3822 * The client already has a "notification dropped" message
3823 * in its outgoing queue. Nothing to do since all
3824 * of those messages are coalesced.
3829 client
->communication
.outbound
.dropped_notification
= true;
3830 ret
= lttng_dynamic_buffer_append(
3831 &client
->communication
.outbound
.payload
.buffer
, &msg
,
3834 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3841 static int client_handle_transmission_status_wrapper(
3842 struct notification_client
*client
,
3843 enum client_transmission_status status
,
3846 return client_handle_transmission_status(client
, status
,
3847 (struct notification_thread_state
*) user_data
);
3851 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3852 const struct lttng_evaluation
*evaluation
,
3853 struct notification_client_list
* client_list
,
3854 struct notification_thread_state
*state
,
3855 uid_t object_uid
, gid_t object_gid
)
3857 const struct lttng_credentials creds
= {
3858 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
3859 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
3862 return notification_client_list_send_evaluation(client_list
,
3863 lttng_trigger_get_const_condition(trigger
), evaluation
,
3864 lttng_trigger_get_credentials(trigger
),
3866 client_handle_transmission_status_wrapper
, state
);
3870 * Permission checks relative to notification channel clients are performed
3871 * here. Notice how object, client, and trigger credentials are involved in
3874 * The `object` credentials are the credentials associated with the "subject"
3875 * of a condition. For instance, a `rotation completed` condition applies
3876 * to a session. When that condition is met, it will produce an evaluation
3877 * against a session. Hence, in this case, the `object` credentials are the
3878 * credentials of the "subject" session.
3880 * The `trigger` credentials are the credentials of the user that registered the
3883 * The `client` credentials are the credentials of the user that created a given
3884 * notification channel.
3886 * In terms of visibility, it is expected that non-privilieged users can only
3887 * register triggers against "their" objects (their own sessions and
3888 * applications they are allowed to interact with). They can then open a
3889 * notification channel and subscribe to notifications associated with those
3892 * As for privilieged users, they can register triggers against the objects of
3893 * other users. They can then subscribe to the notifications associated to their
3894 * triggers. Privilieged users _can't_ subscribe to the notifications of
3895 * triggers owned by other users; they must create their own triggers.
3897 * This is more a concern of usability than security. It would be difficult for
3898 * a root user reliably subscribe to a specific set of conditions without
3899 * interference from external users (those could, for instance, unregister
3903 int notification_client_list_send_evaluation(
3904 struct notification_client_list
*client_list
,
3905 const struct lttng_condition
*condition
,
3906 const struct lttng_evaluation
*evaluation
,
3907 const struct lttng_credentials
*trigger_creds
,
3908 const struct lttng_credentials
*source_object_creds
,
3909 report_client_transmission_result_cb client_report
,
3913 struct lttng_payload msg_payload
;
3914 struct notification_client_list_element
*client_list_element
, *tmp
;
3915 const struct lttng_notification notification
= {
3916 .condition
= (struct lttng_condition
*) condition
,
3917 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3919 struct lttng_notification_channel_message msg_header
= {
3920 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3923 lttng_payload_init(&msg_payload
);
3925 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
3926 sizeof(msg_header
));
3931 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
3933 ERR("[notification-thread] Failed to serialize notification");
3938 /* Update payload size. */
3939 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
3940 ->size
= (uint32_t)(
3941 msg_payload
.buffer
.size
- sizeof(msg_header
));
3943 /* Update the payload number of fds. */
3945 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3946 &msg_payload
, 0, -1);
3948 ((struct lttng_notification_channel_message
*)
3949 msg_payload
.buffer
.data
)->fds
= (uint32_t)
3950 lttng_payload_view_get_fd_handle_count(&pv
);
3953 pthread_mutex_lock(&client_list
->lock
);
3954 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3955 &client_list
->list
, node
) {
3956 enum client_transmission_status transmission_status
;
3957 struct notification_client
*client
=
3958 client_list_element
->client
;
3961 pthread_mutex_lock(&client
->lock
);
3962 if (!client
->communication
.active
) {
3964 * Skip inactive client (protocol error or
3967 DBG("Skipping client at it is marked as inactive");
3971 if (source_object_creds
) {
3972 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
3973 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
3976 * Client is not allowed to monitor this
3979 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
3984 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
3985 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
3989 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3990 client
->socket
, msg_payload
.buffer
.size
);
3992 if (client_has_outbound_data_left(client
)) {
3994 * Outgoing data is already buffered for this client;
3995 * drop the notification and enqueue a "dropped
3996 * notification" message if this is the first dropped
3997 * notification since the socket spilled-over to the
4000 ret
= client_notification_overflow(client
);
4007 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4013 transmission_status
= client_flush_outgoing_queue(client
);
4014 pthread_mutex_unlock(&client
->lock
);
4015 ret
= client_report(client
, transmission_status
, user_data
);
4018 goto end_unlock_list
;
4024 pthread_mutex_unlock(&client
->lock
);
4027 goto end_unlock_list
;
4033 pthread_mutex_unlock(&client_list
->lock
);
4035 lttng_payload_reset(&msg_payload
);
4039 int handle_notification_thread_channel_sample(
4040 struct notification_thread_state
*state
, int pipe
,
4041 enum lttng_domain_type domain
)
4044 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4045 struct channel_info
*channel_info
;
4046 struct cds_lfht_node
*node
;
4047 struct cds_lfht_iter iter
;
4048 struct lttng_channel_trigger_list
*trigger_list
;
4049 struct lttng_trigger_list_element
*trigger_list_element
;
4050 bool previous_sample_available
= false;
4051 struct channel_state_sample previous_sample
, latest_sample
;
4052 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4053 struct lttng_credentials channel_creds
;
4056 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4057 * ensuring that read/write of sampling messages are atomic.
4059 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4060 if (ret
!= sizeof(sample_msg
)) {
4061 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4068 latest_sample
.key
.key
= sample_msg
.key
;
4069 latest_sample
.key
.domain
= domain
;
4070 latest_sample
.highest_usage
= sample_msg
.highest
;
4071 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4072 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4076 /* Retrieve the channel's informations */
4077 cds_lfht_lookup(state
->channels_ht
,
4078 hash_channel_key(&latest_sample
.key
),
4082 node
= cds_lfht_iter_get_node(&iter
);
4083 if (caa_unlikely(!node
)) {
4085 * Not an error since the consumer can push a sample to the pipe
4086 * and the rest of the session daemon could notify us of the
4087 * channel's destruction before we get a chance to process that
4090 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4091 latest_sample
.key
.key
,
4092 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
4096 channel_info
= caa_container_of(node
, struct channel_info
,
4098 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4100 latest_sample
.key
.key
,
4101 channel_info
->session_info
->name
,
4102 latest_sample
.highest_usage
,
4103 latest_sample
.lowest_usage
,
4104 latest_sample
.channel_total_consumed
);
4106 previous_session_consumed_total
=
4107 channel_info
->session_info
->consumed_data_size
;
4109 /* Retrieve the channel's last sample, if it exists, and update it. */
4110 cds_lfht_lookup(state
->channel_state_ht
,
4111 hash_channel_key(&latest_sample
.key
),
4112 match_channel_state_sample
,
4115 node
= cds_lfht_iter_get_node(&iter
);
4116 if (caa_likely(node
)) {
4117 struct channel_state_sample
*stored_sample
;
4119 /* Update the sample stored. */
4120 stored_sample
= caa_container_of(node
,
4121 struct channel_state_sample
,
4122 channel_state_ht_node
);
4124 memcpy(&previous_sample
, stored_sample
,
4125 sizeof(previous_sample
));
4126 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4127 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4128 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4129 previous_sample_available
= true;
4131 latest_session_consumed_total
=
4132 previous_session_consumed_total
+
4133 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4136 * This is the channel's first sample, allocate space for and
4137 * store the new sample.
4139 struct channel_state_sample
*stored_sample
;
4141 stored_sample
= zmalloc(sizeof(*stored_sample
));
4142 if (!stored_sample
) {
4147 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4148 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4149 cds_lfht_add(state
->channel_state_ht
,
4150 hash_channel_key(&stored_sample
->key
),
4151 &stored_sample
->channel_state_ht_node
);
4153 latest_session_consumed_total
=
4154 previous_session_consumed_total
+
4155 latest_sample
.channel_total_consumed
;
4158 channel_info
->session_info
->consumed_data_size
=
4159 latest_session_consumed_total
;
4161 /* Find triggers associated with this channel. */
4162 cds_lfht_lookup(state
->channel_triggers_ht
,
4163 hash_channel_key(&latest_sample
.key
),
4164 match_channel_trigger_list
,
4167 node
= cds_lfht_iter_get_node(&iter
);
4168 if (caa_likely(!node
)) {
4172 channel_creds
= (typeof(channel_creds
)) {
4173 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4174 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4177 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4178 channel_triggers_ht_node
);
4179 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4181 const struct lttng_condition
*condition
;
4182 struct lttng_trigger
*trigger
;
4183 struct notification_client_list
*client_list
= NULL
;
4184 struct lttng_evaluation
*evaluation
= NULL
;
4185 enum action_executor_status executor_status
;
4188 trigger
= trigger_list_element
->trigger
;
4189 condition
= lttng_trigger_get_const_condition(trigger
);
4193 * Check if any client is subscribed to the result of this
4196 client_list
= get_client_list_from_condition(state
, condition
);
4198 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4199 previous_sample_available
? &previous_sample
: NULL
,
4201 previous_session_consumed_total
,
4202 latest_session_consumed_total
,
4204 if (caa_unlikely(ret
)) {
4208 if (caa_likely(!evaluation
)) {
4212 if (!lttng_trigger_should_fire(trigger
)) {
4216 lttng_trigger_fire(trigger
);
4219 * Ownership of `evaluation` transferred to the action executor
4220 * no matter the result.
4222 executor_status
= action_executor_enqueue(state
->executor
,
4223 trigger
, evaluation
, &channel_creds
,
4226 switch (executor_status
) {
4227 case ACTION_EXECUTOR_STATUS_OK
:
4229 case ACTION_EXECUTOR_STATUS_ERROR
:
4230 case ACTION_EXECUTOR_STATUS_INVALID
:
4232 * TODO Add trigger identification (name/id) when
4233 * it is added to the API.
4235 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4238 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4240 * TODO Add trigger identification (name/id) when
4241 * it is added to the API.
4243 * Not a fatal error.
4245 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4253 notification_client_list_put(client_list
);
4254 if (caa_unlikely(ret
)) {