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/action/group-internal.h>
25 #include <lttng/domain-internal.h>
26 #include <lttng/notification/notification-internal.h>
27 #include <lttng/condition/condition-internal.h>
28 #include <lttng/condition/buffer-usage-internal.h>
29 #include <lttng/condition/session-consumed-size-internal.h>
30 #include <lttng/condition/session-rotation-internal.h>
31 #include <lttng/condition/event-rule-matches-internal.h>
32 #include <lttng/domain-internal.h>
33 #include <lttng/notification/channel-internal.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <lttng/event-rule/event-rule-internal.h>
43 #include "condition-internal.h"
44 #include "event-notifier-error-accounting.h"
45 #include "notification-thread.h"
46 #include "notification-thread-events.h"
47 #include "notification-thread-commands.h"
48 #include "lttng-sessiond.h"
51 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
52 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
54 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
55 #define MAX_CAPTURE_SIZE (PIPE_BUF)
57 enum lttng_object_type
{
58 LTTNG_OBJECT_TYPE_UNKNOWN
,
59 LTTNG_OBJECT_TYPE_NONE
,
60 LTTNG_OBJECT_TYPE_CHANNEL
,
61 LTTNG_OBJECT_TYPE_SESSION
,
64 struct lttng_trigger_list_element
{
65 /* No ownership of the trigger object is assumed. */
66 struct lttng_trigger
*trigger
;
67 struct cds_list_head node
;
70 struct lttng_channel_trigger_list
{
71 struct channel_key channel_key
;
72 /* List of struct lttng_trigger_list_element. */
73 struct cds_list_head list
;
74 /* Node in the channel_triggers_ht */
75 struct cds_lfht_node channel_triggers_ht_node
;
76 /* call_rcu delayed reclaim. */
77 struct rcu_head rcu_node
;
81 * List of triggers applying to a given session.
84 * - lttng_session_trigger_list_create()
85 * - lttng_session_trigger_list_build()
86 * - lttng_session_trigger_list_destroy()
87 * - lttng_session_trigger_list_add()
89 struct lttng_session_trigger_list
{
91 * Not owned by this; points to the session_info structure's
94 const char *session_name
;
95 /* List of struct lttng_trigger_list_element. */
96 struct cds_list_head list
;
97 /* Node in the session_triggers_ht */
98 struct cds_lfht_node session_triggers_ht_node
;
100 * Weak reference to the notification system's session triggers
103 * The session trigger list structure structure is owned by
104 * the session's session_info.
106 * The session_info is kept alive the the channel_infos holding a
107 * reference to it (reference counting). When those channels are
108 * destroyed (at runtime or on teardown), the reference they hold
109 * to the session_info are released. On destruction of session_info,
110 * session_info_destroy() will remove the list of triggers applying
111 * to this session from the notification system's state.
113 * This implies that the session_triggers_ht must be destroyed
114 * after the channels.
116 struct cds_lfht
*session_triggers_ht
;
117 /* Used for delayed RCU reclaim. */
118 struct rcu_head rcu_node
;
121 struct lttng_trigger_ht_element
{
122 struct lttng_trigger
*trigger
;
123 struct cds_lfht_node node
;
124 struct cds_lfht_node node_by_name_uid
;
125 struct cds_list_head client_list_trigger_node
;
126 /* call_rcu delayed reclaim. */
127 struct rcu_head rcu_node
;
130 struct lttng_condition_list_element
{
131 struct lttng_condition
*condition
;
132 struct cds_list_head node
;
135 struct channel_state_sample
{
136 struct channel_key key
;
137 struct cds_lfht_node channel_state_ht_node
;
138 uint64_t highest_usage
;
139 uint64_t lowest_usage
;
140 uint64_t channel_total_consumed
;
141 /* call_rcu delayed reclaim. */
142 struct rcu_head rcu_node
;
145 static unsigned long hash_channel_key(struct channel_key
*key
);
146 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
147 struct lttng_evaluation
**evaluation
,
148 const struct notification_thread_state
*state
,
149 const struct channel_state_sample
*previous_sample
,
150 const struct channel_state_sample
*latest_sample
,
151 uint64_t previous_session_consumed_total
,
152 uint64_t latest_session_consumed_total
,
153 struct channel_info
*channel_info
);
155 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
156 const struct lttng_evaluation
*evaluation
,
157 struct notification_client_list
*client_list
,
158 struct notification_thread_state
*state
,
159 uid_t channel_uid
, gid_t channel_gid
);
162 /* session_info API */
164 void session_info_destroy(void *_data
);
166 void session_info_get(struct session_info
*session_info
);
168 void session_info_put(struct session_info
*session_info
);
170 struct session_info
*session_info_create(const char *name
,
171 uid_t uid
, gid_t gid
,
172 struct lttng_session_trigger_list
*trigger_list
,
173 struct cds_lfht
*sessions_ht
);
175 void session_info_add_channel(struct session_info
*session_info
,
176 struct channel_info
*channel_info
);
178 void session_info_remove_channel(struct session_info
*session_info
,
179 struct channel_info
*channel_info
);
181 /* lttng_session_trigger_list API */
183 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
184 const char *session_name
,
185 struct cds_lfht
*session_triggers_ht
);
187 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
188 const struct notification_thread_state
*state
,
189 const char *session_name
);
191 void lttng_session_trigger_list_destroy(
192 struct lttng_session_trigger_list
*list
);
194 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
195 struct lttng_trigger
*trigger
);
198 int client_handle_transmission_status(
199 struct notification_client
*client
,
200 enum client_transmission_status transmission_status
,
201 struct notification_thread_state
*state
);
204 int handle_one_event_notifier_notification(
205 struct notification_thread_state
*state
,
206 int pipe
, enum lttng_domain_type domain
);
209 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
212 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
214 /* This double-cast is intended to supress pointer-to-cast warning. */
215 const int socket
= (int) (intptr_t) key
;
216 const struct notification_client
*client
= caa_container_of(node
,
217 struct notification_client
, client_socket_ht_node
);
219 return client
->socket
== socket
;
223 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
225 /* This double-cast is intended to supress pointer-to-cast warning. */
226 const notification_client_id id
= *((notification_client_id
*) key
);
227 const struct notification_client
*client
= caa_container_of(
228 node
, struct notification_client
, client_id_ht_node
);
230 return client
->id
== id
;
234 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
236 struct channel_key
*channel_key
= (struct channel_key
*) key
;
237 struct lttng_channel_trigger_list
*trigger_list
;
239 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
240 channel_triggers_ht_node
);
242 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
243 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
247 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
249 const char *session_name
= (const char *) key
;
250 struct lttng_session_trigger_list
*trigger_list
;
252 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
253 session_triggers_ht_node
);
255 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
259 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
261 struct channel_key
*channel_key
= (struct channel_key
*) key
;
262 struct channel_state_sample
*sample
;
264 sample
= caa_container_of(node
, struct channel_state_sample
,
265 channel_state_ht_node
);
267 return !!((channel_key
->key
== sample
->key
.key
) &&
268 (channel_key
->domain
== sample
->key
.domain
));
272 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
274 struct channel_key
*channel_key
= (struct channel_key
*) key
;
275 struct channel_info
*channel_info
;
277 channel_info
= caa_container_of(node
, struct channel_info
,
280 return !!((channel_key
->key
== channel_info
->key
.key
) &&
281 (channel_key
->domain
== channel_info
->key
.domain
));
285 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
287 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
288 struct lttng_trigger_ht_element
*trigger_ht_element
;
290 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
293 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
297 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
299 const uint64_t *_key
= key
;
300 struct notification_trigger_tokens_ht_element
*element
;
302 element
= caa_container_of(node
,
303 struct notification_trigger_tokens_ht_element
, node
);
304 return *_key
== element
->token
;
308 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
310 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
311 struct notification_client_list
*client_list
;
312 const struct lttng_condition
*condition
;
314 assert(condition_key
);
316 client_list
= caa_container_of(node
, struct notification_client_list
,
317 notification_trigger_clients_ht_node
);
318 condition
= client_list
->condition
;
320 return !!lttng_condition_is_equal(condition_key
, condition
);
324 int match_session(struct cds_lfht_node
*node
, const void *key
)
326 const char *name
= key
;
327 struct session_info
*session_info
= caa_container_of(
328 node
, struct session_info
, sessions_ht_node
);
330 return !strcmp(session_info
->name
, name
);
334 const char *notification_command_type_str(
335 enum notification_thread_command_type type
)
338 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
339 return "REGISTER_TRIGGER";
340 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
341 return "UNREGISTER_TRIGGER";
342 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
343 return "ADD_CHANNEL";
344 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
345 return "REMOVE_CHANNEL";
346 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
347 return "SESSION_ROTATION_ONGOING";
348 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
349 return "SESSION_ROTATION_COMPLETED";
350 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
351 return "ADD_TRACER_EVENT_SOURCE";
352 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
353 return "REMOVE_TRACER_EVENT_SOURCE";
354 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
355 return "LIST_TRIGGERS";
356 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
357 return "GET_TRIGGER";
358 case NOTIFICATION_COMMAND_TYPE_QUIT
:
360 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
361 return "CLIENT_COMMUNICATION_UPDATE";
368 * Match trigger based on name and credentials only.
369 * Name duplication is NOT allowed for the same uid.
372 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
377 const char *key_name
;
378 enum lttng_trigger_status status
;
379 const struct lttng_credentials
*key_creds
;
380 const struct lttng_credentials
*node_creds
;
381 const struct lttng_trigger
*trigger_key
=
382 (const struct lttng_trigger
*) key
;
383 const struct lttng_trigger_ht_element
*trigger_ht_element
=
384 caa_container_of(node
,
385 struct lttng_trigger_ht_element
,
388 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
389 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
391 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
392 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
394 /* Compare the names. */
395 if (strcmp(name
, key_name
) != 0) {
399 /* Compare the owners' UIDs. */
400 key_creds
= lttng_trigger_get_credentials(trigger_key
);
401 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
403 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
410 * Hash trigger based on name and credentials only.
413 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
415 unsigned long hash
= 0;
416 const struct lttng_credentials
*trigger_creds
;
417 const char *trigger_name
;
418 enum lttng_trigger_status status
;
420 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
421 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
422 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
425 trigger_creds
= lttng_trigger_get_credentials(trigger
);
426 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
433 unsigned long hash_channel_key(struct channel_key
*key
)
435 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
436 unsigned long domain_hash
= hash_key_ulong(
437 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
439 return key_hash
^ domain_hash
;
443 unsigned long hash_client_socket(int socket
)
445 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
449 unsigned long hash_client_id(notification_client_id id
)
451 return hash_key_u64(&id
, lttng_ht_seed
);
455 * Get the type of object to which a given condition applies. Bindings let
456 * the notification system evaluate a trigger's condition when a given
457 * object's state is updated.
459 * For instance, a condition bound to a channel will be evaluated everytime
460 * the channel's state is changed by a channel monitoring sample.
463 enum lttng_object_type
get_condition_binding_object(
464 const struct lttng_condition
*condition
)
466 switch (lttng_condition_get_type(condition
)) {
467 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
468 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
469 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
470 return LTTNG_OBJECT_TYPE_CHANNEL
;
471 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
472 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
473 return LTTNG_OBJECT_TYPE_SESSION
;
474 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
475 return LTTNG_OBJECT_TYPE_NONE
;
477 return LTTNG_OBJECT_TYPE_UNKNOWN
;
482 void free_channel_info_rcu(struct rcu_head
*node
)
484 free(caa_container_of(node
, struct channel_info
, rcu_node
));
488 void channel_info_destroy(struct channel_info
*channel_info
)
494 if (channel_info
->session_info
) {
495 session_info_remove_channel(channel_info
->session_info
,
497 session_info_put(channel_info
->session_info
);
499 if (channel_info
->name
) {
500 free(channel_info
->name
);
502 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
506 void free_session_info_rcu(struct rcu_head
*node
)
508 free(caa_container_of(node
, struct session_info
, rcu_node
));
511 /* Don't call directly, use the ref-counting mechanism. */
513 void session_info_destroy(void *_data
)
515 struct session_info
*session_info
= _data
;
518 assert(session_info
);
519 if (session_info
->channel_infos_ht
) {
520 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
522 ERR("[notification-thread] Failed to destroy channel information hash table");
525 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
528 cds_lfht_del(session_info
->sessions_ht
,
529 &session_info
->sessions_ht_node
);
531 free(session_info
->name
);
532 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
536 void session_info_get(struct session_info
*session_info
)
541 lttng_ref_get(&session_info
->ref
);
545 void session_info_put(struct session_info
*session_info
)
550 lttng_ref_put(&session_info
->ref
);
554 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
555 struct lttng_session_trigger_list
*trigger_list
,
556 struct cds_lfht
*sessions_ht
)
558 struct session_info
*session_info
;
562 session_info
= zmalloc(sizeof(*session_info
));
566 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
568 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
569 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
570 if (!session_info
->channel_infos_ht
) {
574 cds_lfht_node_init(&session_info
->sessions_ht_node
);
575 session_info
->name
= strdup(name
);
576 if (!session_info
->name
) {
579 session_info
->uid
= uid
;
580 session_info
->gid
= gid
;
581 session_info
->trigger_list
= trigger_list
;
582 session_info
->sessions_ht
= sessions_ht
;
586 session_info_put(session_info
);
591 void session_info_add_channel(struct session_info
*session_info
,
592 struct channel_info
*channel_info
)
595 cds_lfht_add(session_info
->channel_infos_ht
,
596 hash_channel_key(&channel_info
->key
),
597 &channel_info
->session_info_channels_ht_node
);
602 void session_info_remove_channel(struct session_info
*session_info
,
603 struct channel_info
*channel_info
)
606 cds_lfht_del(session_info
->channel_infos_ht
,
607 &channel_info
->session_info_channels_ht_node
);
612 struct channel_info
*channel_info_create(const char *channel_name
,
613 struct channel_key
*channel_key
, uint64_t channel_capacity
,
614 struct session_info
*session_info
)
616 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
622 cds_lfht_node_init(&channel_info
->channels_ht_node
);
623 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
624 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
625 channel_info
->capacity
= channel_capacity
;
627 channel_info
->name
= strdup(channel_name
);
628 if (!channel_info
->name
) {
633 * Set the references between session and channel infos:
634 * - channel_info holds a strong reference to session_info
635 * - session_info holds a weak reference to channel_info
637 session_info_get(session_info
);
638 session_info_add_channel(session_info
, channel_info
);
639 channel_info
->session_info
= session_info
;
643 channel_info_destroy(channel_info
);
648 bool notification_client_list_get(struct notification_client_list
*list
)
650 return urcu_ref_get_unless_zero(&list
->ref
);
654 void free_notification_client_list_rcu(struct rcu_head
*node
)
656 free(caa_container_of(node
, struct notification_client_list
,
661 void notification_client_list_release(struct urcu_ref
*list_ref
)
663 struct notification_client_list
*list
=
664 container_of(list_ref
, typeof(*list
), ref
);
665 struct notification_client_list_element
*client_list_element
, *tmp
;
667 lttng_condition_put(list
->condition
);
669 if (list
->notification_trigger_clients_ht
) {
672 cds_lfht_del(list
->notification_trigger_clients_ht
,
673 &list
->notification_trigger_clients_ht_node
);
675 list
->notification_trigger_clients_ht
= NULL
;
677 cds_list_for_each_entry_safe(client_list_element
, tmp
,
678 &list
->clients_list
, node
) {
679 free(client_list_element
);
682 assert(cds_list_empty(&list
->triggers_list
));
684 pthread_mutex_destroy(&list
->lock
);
685 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
689 bool condition_applies_to_client(const struct lttng_condition
*condition
,
690 struct notification_client
*client
)
692 bool applies
= false;
693 struct lttng_condition_list_element
*condition_list_element
;
695 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
697 applies
= lttng_condition_is_equal(
698 condition_list_element
->condition
,
709 struct notification_client_list
*notification_client_list_create(
710 struct notification_thread_state
*state
,
711 const struct lttng_condition
*condition
)
713 struct notification_client
*client
;
714 struct cds_lfht_iter iter
;
715 struct notification_client_list
*client_list
;
717 client_list
= zmalloc(sizeof(*client_list
));
719 PERROR("Failed to allocate notification client list");
723 pthread_mutex_init(&client_list
->lock
, NULL
);
725 * The trigger that owns the condition has the first reference to this
728 urcu_ref_init(&client_list
->ref
);
729 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
730 CDS_INIT_LIST_HEAD(&client_list
->clients_list
);
731 CDS_INIT_LIST_HEAD(&client_list
->triggers_list
);
734 * Create a copy of the condition so that it's independent of any
735 * trigger. The client list may outlive the trigger object (which owns
736 * the condition) that is used to create it.
738 client_list
->condition
= lttng_condition_copy(condition
);
740 /* Build a list of clients to which this new condition applies. */
741 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
742 client_socket_ht_node
) {
743 struct notification_client_list_element
*client_list_element
;
745 if (!condition_applies_to_client(condition
, client
)) {
749 client_list_element
= zmalloc(sizeof(*client_list_element
));
750 if (!client_list_element
) {
751 goto error_put_client_list
;
754 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
755 client_list_element
->client
= client
;
756 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
759 client_list
->notification_trigger_clients_ht
=
760 state
->notification_trigger_clients_ht
;
764 * Add the client list to the global list of client list.
766 cds_lfht_add_unique(state
->notification_trigger_clients_ht
,
767 lttng_condition_hash(client_list
->condition
),
768 match_client_list_condition
,
769 client_list
->condition
,
770 &client_list
->notification_trigger_clients_ht_node
);
774 error_put_client_list
:
775 notification_client_list_put(client_list
);
782 void notification_client_list_put(struct notification_client_list
*list
)
787 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
790 /* Provides a reference to the returned list. */
792 struct notification_client_list
*get_client_list_from_condition(
793 struct notification_thread_state
*state
,
794 const struct lttng_condition
*condition
)
796 struct cds_lfht_node
*node
;
797 struct cds_lfht_iter iter
;
798 struct notification_client_list
*list
= NULL
;
801 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
802 lttng_condition_hash(condition
),
803 match_client_list_condition
,
806 node
= cds_lfht_iter_get_node(&iter
);
808 list
= container_of(node
, struct notification_client_list
,
809 notification_trigger_clients_ht_node
);
810 list
= notification_client_list_get(list
) ? list
: NULL
;
818 int evaluate_channel_condition_for_client(
819 const struct lttng_condition
*condition
,
820 struct notification_thread_state
*state
,
821 struct lttng_evaluation
**evaluation
,
822 uid_t
*session_uid
, gid_t
*session_gid
)
825 struct cds_lfht_iter iter
;
826 struct cds_lfht_node
*node
;
827 struct channel_info
*channel_info
= NULL
;
828 struct channel_key
*channel_key
= NULL
;
829 struct channel_state_sample
*last_sample
= NULL
;
830 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
834 /* Find the channel associated with the condition. */
835 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
836 channel_trigger_list
, channel_triggers_ht_node
) {
837 struct lttng_trigger_list_element
*element
;
839 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
840 const struct lttng_condition
*current_condition
=
841 lttng_trigger_get_const_condition(
844 assert(current_condition
);
845 if (!lttng_condition_is_equal(condition
,
846 current_condition
)) {
850 /* Found the trigger, save the channel key. */
851 channel_key
= &channel_trigger_list
->channel_key
;
855 /* The channel key was found stop iteration. */
861 /* No channel found; normal exit. */
862 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
867 /* Fetch channel info for the matching channel. */
868 cds_lfht_lookup(state
->channels_ht
,
869 hash_channel_key(channel_key
),
873 node
= cds_lfht_iter_get_node(&iter
);
875 channel_info
= caa_container_of(node
, struct channel_info
,
878 /* Retrieve the channel's last sample, if it exists. */
879 cds_lfht_lookup(state
->channel_state_ht
,
880 hash_channel_key(channel_key
),
881 match_channel_state_sample
,
884 node
= cds_lfht_iter_get_node(&iter
);
886 last_sample
= caa_container_of(node
,
887 struct channel_state_sample
,
888 channel_state_ht_node
);
890 /* Nothing to evaluate, no sample was ever taken. Normal exit */
891 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
896 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
898 0, channel_info
->session_info
->consumed_data_size
,
901 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
905 *session_uid
= channel_info
->session_info
->uid
;
906 *session_gid
= channel_info
->session_info
->gid
;
913 const char *get_condition_session_name(const struct lttng_condition
*condition
)
915 const char *session_name
= NULL
;
916 enum lttng_condition_status status
;
918 switch (lttng_condition_get_type(condition
)) {
919 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
920 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
921 status
= lttng_condition_buffer_usage_get_session_name(
922 condition
, &session_name
);
924 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
925 status
= lttng_condition_session_consumed_size_get_session_name(
926 condition
, &session_name
);
928 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
929 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
930 status
= lttng_condition_session_rotation_get_session_name(
931 condition
, &session_name
);
936 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
937 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
945 int evaluate_session_condition_for_client(
946 const struct lttng_condition
*condition
,
947 struct notification_thread_state
*state
,
948 struct lttng_evaluation
**evaluation
,
949 uid_t
*session_uid
, gid_t
*session_gid
)
952 struct cds_lfht_iter iter
;
953 struct cds_lfht_node
*node
;
954 const char *session_name
;
955 struct session_info
*session_info
= NULL
;
958 session_name
= get_condition_session_name(condition
);
960 /* Find the session associated with the trigger. */
961 cds_lfht_lookup(state
->sessions_ht
,
962 hash_key_str(session_name
, lttng_ht_seed
),
966 node
= cds_lfht_iter_get_node(&iter
);
968 DBG("[notification-thread] No known session matching name \"%s\"",
974 session_info
= caa_container_of(node
, struct session_info
,
976 session_info_get(session_info
);
979 * Evaluation is performed in-line here since only one type of
980 * session-bound condition is handled for the moment.
982 switch (lttng_condition_get_type(condition
)) {
983 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
984 if (!session_info
->rotation
.ongoing
) {
986 goto end_session_put
;
989 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
990 session_info
->rotation
.id
);
993 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
996 goto end_session_put
;
1002 goto end_session_put
;
1005 *session_uid
= session_info
->uid
;
1006 *session_gid
= session_info
->gid
;
1009 session_info_put(session_info
);
1016 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1017 const struct lttng_condition
*condition
,
1018 struct notification_client
*client
,
1019 struct notification_thread_state
*state
)
1022 struct lttng_evaluation
*evaluation
= NULL
;
1023 struct notification_client_list client_list
= {
1024 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1026 struct notification_client_list_element client_list_element
= { 0 };
1027 uid_t object_uid
= 0;
1028 gid_t object_gid
= 0;
1035 switch (get_condition_binding_object(condition
)) {
1036 case LTTNG_OBJECT_TYPE_SESSION
:
1037 ret
= evaluate_session_condition_for_client(condition
, state
,
1038 &evaluation
, &object_uid
, &object_gid
);
1040 case LTTNG_OBJECT_TYPE_CHANNEL
:
1041 ret
= evaluate_channel_condition_for_client(condition
, state
,
1042 &evaluation
, &object_uid
, &object_gid
);
1044 case LTTNG_OBJECT_TYPE_NONE
:
1045 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
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
.clients_list
);
1071 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1072 client_list_element
.client
= client
;
1073 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1075 /* Send evaluation result to the newly-subscribed client. */
1076 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1077 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1078 state
, object_uid
, object_gid
);
1085 int notification_thread_client_subscribe(struct notification_client
*client
,
1086 struct lttng_condition
*condition
,
1087 struct notification_thread_state
*state
,
1088 enum lttng_notification_channel_status
*_status
)
1091 struct notification_client_list
*client_list
= NULL
;
1092 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1093 struct notification_client_list_element
*client_list_element
= NULL
;
1094 struct lttng_trigger_ht_element
*trigger_ht_element
;
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 * Note the iteration on all triggers which share an identical
1145 * `condition` than the one to which the client is registering. This is
1146 * done to ensure that the client receives a distinct notification for
1147 * all triggers that have a `notify` action that have this condition.
1149 pthread_mutex_lock(&client_list
->lock
);
1150 cds_list_for_each_entry(trigger_ht_element
,
1151 &client_list
->triggers_list
, client_list_trigger_node
) {
1152 if (evaluate_condition_for_client(trigger_ht_element
->trigger
, condition
,
1154 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1156 free(client_list_element
);
1157 pthread_mutex_unlock(&client_list
->lock
);
1161 pthread_mutex_unlock(&client_list
->lock
);
1164 * Add the client to the list of clients interested in a given trigger
1165 * if a "notification" trigger with a corresponding condition was
1168 client_list_element
->client
= client
;
1169 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1171 pthread_mutex_lock(&client_list
->lock
);
1172 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1173 pthread_mutex_unlock(&client_list
->lock
);
1179 notification_client_list_put(client_list
);
1183 free(condition_list_element
);
1184 free(client_list_element
);
1189 int notification_thread_client_unsubscribe(
1190 struct notification_client
*client
,
1191 struct lttng_condition
*condition
,
1192 struct notification_thread_state
*state
,
1193 enum lttng_notification_channel_status
*_status
)
1195 struct notification_client_list
*client_list
;
1196 struct lttng_condition_list_element
*condition_list_element
,
1198 struct notification_client_list_element
*client_list_element
,
1200 bool condition_found
= false;
1201 enum lttng_notification_channel_status status
=
1202 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1204 /* Remove the condition from the client's condition list. */
1205 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1206 &client
->condition_list
, node
) {
1207 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1212 cds_list_del(&condition_list_element
->node
);
1214 * The caller may be iterating on the client's conditions to
1215 * tear down a client's connection. In this case, the condition
1216 * will be destroyed at the end.
1218 if (condition
!= condition_list_element
->condition
) {
1219 lttng_condition_destroy(
1220 condition_list_element
->condition
);
1222 free(condition_list_element
);
1223 condition_found
= true;
1227 if (!condition_found
) {
1228 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1233 * Remove the client from the list of clients interested the trigger
1234 * matching the condition.
1236 client_list
= get_client_list_from_condition(state
, condition
);
1241 pthread_mutex_lock(&client_list
->lock
);
1242 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1243 &client_list
->clients_list
, node
) {
1244 if (client_list_element
->client
->id
!= client
->id
) {
1247 cds_list_del(&client_list_element
->node
);
1248 free(client_list_element
);
1251 pthread_mutex_unlock(&client_list
->lock
);
1252 notification_client_list_put(client_list
);
1255 lttng_condition_destroy(condition
);
1263 void free_notification_client_rcu(struct rcu_head
*node
)
1265 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1269 void notification_client_destroy(struct notification_client
*client
,
1270 struct notification_thread_state
*state
)
1277 * The client object is not reachable by other threads, no need to lock
1280 if (client
->socket
>= 0) {
1281 (void) lttcomm_close_unix_sock(client
->socket
);
1282 client
->socket
= -1;
1284 client
->communication
.active
= false;
1285 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1286 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1287 pthread_mutex_destroy(&client
->lock
);
1288 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1292 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1296 struct notification_client
*get_client_from_socket(int socket
,
1297 struct notification_thread_state
*state
)
1299 struct cds_lfht_iter iter
;
1300 struct cds_lfht_node
*node
;
1301 struct notification_client
*client
= NULL
;
1303 cds_lfht_lookup(state
->client_socket_ht
,
1304 hash_client_socket(socket
),
1305 match_client_socket
,
1306 (void *) (unsigned long) socket
,
1308 node
= cds_lfht_iter_get_node(&iter
);
1313 client
= caa_container_of(node
, struct notification_client
,
1314 client_socket_ht_node
);
1320 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1324 struct notification_client
*get_client_from_id(notification_client_id id
,
1325 struct notification_thread_state
*state
)
1327 struct cds_lfht_iter iter
;
1328 struct cds_lfht_node
*node
;
1329 struct notification_client
*client
= NULL
;
1331 cds_lfht_lookup(state
->client_id_ht
,
1336 node
= cds_lfht_iter_get_node(&iter
);
1341 client
= caa_container_of(node
, struct notification_client
,
1348 bool buffer_usage_condition_applies_to_channel(
1349 const struct lttng_condition
*condition
,
1350 const struct channel_info
*channel_info
)
1352 enum lttng_condition_status status
;
1353 enum lttng_domain_type condition_domain
;
1354 const char *condition_session_name
= NULL
;
1355 const char *condition_channel_name
= NULL
;
1357 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1359 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1360 if (channel_info
->key
.domain
!= condition_domain
) {
1364 status
= lttng_condition_buffer_usage_get_session_name(
1365 condition
, &condition_session_name
);
1366 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1368 status
= lttng_condition_buffer_usage_get_channel_name(
1369 condition
, &condition_channel_name
);
1370 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1372 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1375 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1385 bool session_consumed_size_condition_applies_to_channel(
1386 const struct lttng_condition
*condition
,
1387 const struct channel_info
*channel_info
)
1389 enum lttng_condition_status status
;
1390 const char *condition_session_name
= NULL
;
1392 status
= lttng_condition_session_consumed_size_get_session_name(
1393 condition
, &condition_session_name
);
1394 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1396 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1406 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1407 const struct channel_info
*channel_info
)
1409 const struct lttng_condition
*condition
;
1410 bool trigger_applies
;
1412 condition
= lttng_trigger_get_const_condition(trigger
);
1417 switch (lttng_condition_get_type(condition
)) {
1418 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1419 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1420 trigger_applies
= buffer_usage_condition_applies_to_channel(
1421 condition
, channel_info
);
1423 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1424 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1425 condition
, channel_info
);
1431 return trigger_applies
;
1436 /* Must be called with RCU read lock held. */
1438 struct lttng_session_trigger_list
*get_session_trigger_list(
1439 struct notification_thread_state
*state
,
1440 const char *session_name
)
1442 struct lttng_session_trigger_list
*list
= NULL
;
1443 struct cds_lfht_node
*node
;
1444 struct cds_lfht_iter iter
;
1446 cds_lfht_lookup(state
->session_triggers_ht
,
1447 hash_key_str(session_name
, lttng_ht_seed
),
1448 match_session_trigger_list
,
1451 node
= cds_lfht_iter_get_node(&iter
);
1454 * Not an error, the list of triggers applying to that session
1455 * will be initialized when the session is created.
1457 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1462 list
= caa_container_of(node
,
1463 struct lttng_session_trigger_list
,
1464 session_triggers_ht_node
);
1470 * Allocate an empty lttng_session_trigger_list for the session named
1473 * No ownership of 'session_name' is assumed by the session trigger list.
1474 * It is the caller's responsability to ensure the session name is alive
1475 * for as long as this list is.
1478 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1479 const char *session_name
,
1480 struct cds_lfht
*session_triggers_ht
)
1482 struct lttng_session_trigger_list
*list
;
1484 list
= zmalloc(sizeof(*list
));
1488 list
->session_name
= session_name
;
1489 CDS_INIT_LIST_HEAD(&list
->list
);
1490 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1491 list
->session_triggers_ht
= session_triggers_ht
;
1494 /* Publish the list through the session_triggers_ht. */
1495 cds_lfht_add(session_triggers_ht
,
1496 hash_key_str(session_name
, lttng_ht_seed
),
1497 &list
->session_triggers_ht_node
);
1504 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1506 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1511 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1513 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1515 /* Empty the list element by element, and then free the list itself. */
1516 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1517 &list
->list
, node
) {
1518 cds_list_del(&trigger_list_element
->node
);
1519 free(trigger_list_element
);
1522 /* Unpublish the list from the session_triggers_ht. */
1523 cds_lfht_del(list
->session_triggers_ht
,
1524 &list
->session_triggers_ht_node
);
1526 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1530 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1531 struct lttng_trigger
*trigger
)
1534 struct lttng_trigger_list_element
*new_element
=
1535 zmalloc(sizeof(*new_element
));
1541 CDS_INIT_LIST_HEAD(&new_element
->node
);
1542 new_element
->trigger
= trigger
;
1543 cds_list_add(&new_element
->node
, &list
->list
);
1549 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1550 const char *session_name
)
1552 bool applies
= false;
1553 const struct lttng_condition
*condition
;
1555 condition
= lttng_trigger_get_const_condition(trigger
);
1556 switch (lttng_condition_get_type(condition
)) {
1557 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1558 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1560 enum lttng_condition_status condition_status
;
1561 const char *condition_session_name
;
1563 condition_status
= lttng_condition_session_rotation_get_session_name(
1564 condition
, &condition_session_name
);
1565 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1566 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1570 assert(condition_session_name
);
1571 applies
= !strcmp(condition_session_name
, session_name
);
1582 * Allocate and initialize an lttng_session_trigger_list which contains
1583 * all triggers that apply to the session named 'session_name'.
1585 * No ownership of 'session_name' is assumed by the session trigger list.
1586 * It is the caller's responsability to ensure the session name is alive
1587 * for as long as this list is.
1590 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1591 const struct notification_thread_state
*state
,
1592 const char *session_name
)
1594 int trigger_count
= 0;
1595 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1596 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1597 struct cds_lfht_iter iter
;
1599 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1600 state
->session_triggers_ht
);
1602 /* Add all triggers applying to the session named 'session_name'. */
1603 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1607 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1612 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1613 trigger_ht_element
->trigger
);
1621 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1623 return session_trigger_list
;
1625 lttng_session_trigger_list_destroy(session_trigger_list
);
1630 struct session_info
*find_or_create_session_info(
1631 struct notification_thread_state
*state
,
1632 const char *name
, uid_t uid
, gid_t gid
)
1634 struct session_info
*session
= NULL
;
1635 struct cds_lfht_node
*node
;
1636 struct cds_lfht_iter iter
;
1637 struct lttng_session_trigger_list
*trigger_list
;
1640 cds_lfht_lookup(state
->sessions_ht
,
1641 hash_key_str(name
, lttng_ht_seed
),
1645 node
= cds_lfht_iter_get_node(&iter
);
1647 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1649 session
= caa_container_of(node
, struct session_info
,
1651 assert(session
->uid
== uid
);
1652 assert(session
->gid
== gid
);
1653 session_info_get(session
);
1657 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1658 if (!trigger_list
) {
1662 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1663 state
->sessions_ht
);
1665 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1667 lttng_session_trigger_list_destroy(trigger_list
);
1670 trigger_list
= NULL
;
1672 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1673 &session
->sessions_ht_node
);
1679 session_info_put(session
);
1684 int handle_notification_thread_command_add_channel(
1685 struct notification_thread_state
*state
,
1686 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1687 const char *channel_name
, enum lttng_domain_type channel_domain
,
1688 uint64_t channel_key_int
, uint64_t channel_capacity
,
1689 enum lttng_error_code
*cmd_result
)
1691 struct cds_list_head trigger_list
;
1692 struct channel_info
*new_channel_info
= NULL
;
1693 struct channel_key channel_key
= {
1694 .key
= channel_key_int
,
1695 .domain
= channel_domain
,
1697 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1698 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1699 int trigger_count
= 0;
1700 struct cds_lfht_iter iter
;
1701 struct session_info
*session_info
= NULL
;
1703 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1704 channel_name
, session_name
, channel_key_int
,
1705 lttng_domain_type_str(channel_domain
));
1707 CDS_INIT_LIST_HEAD(&trigger_list
);
1709 session_info
= find_or_create_session_info(state
, session_name
,
1710 session_uid
, session_gid
);
1711 if (!session_info
) {
1712 /* Allocation error or an internal error occurred. */
1716 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1717 channel_capacity
, session_info
);
1718 if (!new_channel_info
) {
1723 /* Build a list of all triggers applying to the new channel. */
1724 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1726 struct lttng_trigger_list_element
*new_element
;
1728 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1729 new_channel_info
)) {
1733 new_element
= zmalloc(sizeof(*new_element
));
1738 CDS_INIT_LIST_HEAD(&new_element
->node
);
1739 new_element
->trigger
= trigger_ht_element
->trigger
;
1740 cds_list_add(&new_element
->node
, &trigger_list
);
1745 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1747 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1748 if (!channel_trigger_list
) {
1751 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1752 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1753 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1754 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1757 /* Add channel to the channel_ht which owns the channel_infos. */
1758 cds_lfht_add(state
->channels_ht
,
1759 hash_channel_key(&new_channel_info
->key
),
1760 &new_channel_info
->channels_ht_node
);
1762 * Add the list of triggers associated with this channel to the
1763 * channel_triggers_ht.
1765 cds_lfht_add(state
->channel_triggers_ht
,
1766 hash_channel_key(&new_channel_info
->key
),
1767 &channel_trigger_list
->channel_triggers_ht_node
);
1769 session_info_put(session_info
);
1770 *cmd_result
= LTTNG_OK
;
1773 channel_info_destroy(new_channel_info
);
1774 session_info_put(session_info
);
1779 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1781 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1786 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1788 free(caa_container_of(node
, struct channel_state_sample
,
1793 int handle_notification_thread_command_remove_channel(
1794 struct notification_thread_state
*state
,
1795 uint64_t channel_key
, enum lttng_domain_type domain
,
1796 enum lttng_error_code
*cmd_result
)
1798 struct cds_lfht_node
*node
;
1799 struct cds_lfht_iter iter
;
1800 struct lttng_channel_trigger_list
*trigger_list
;
1801 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1802 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1803 struct channel_info
*channel_info
;
1805 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1806 channel_key
, lttng_domain_type_str(domain
));
1810 cds_lfht_lookup(state
->channel_triggers_ht
,
1811 hash_channel_key(&key
),
1812 match_channel_trigger_list
,
1815 node
= cds_lfht_iter_get_node(&iter
);
1817 * There is a severe internal error if we are being asked to remove a
1818 * channel that doesn't exist.
1821 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1825 /* Free the list of triggers associated with this channel. */
1826 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1827 channel_triggers_ht_node
);
1828 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1829 &trigger_list
->list
, node
) {
1830 cds_list_del(&trigger_list_element
->node
);
1831 free(trigger_list_element
);
1833 cds_lfht_del(state
->channel_triggers_ht
, node
);
1834 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1836 /* Free sampled channel state. */
1837 cds_lfht_lookup(state
->channel_state_ht
,
1838 hash_channel_key(&key
),
1839 match_channel_state_sample
,
1842 node
= cds_lfht_iter_get_node(&iter
);
1844 * This is expected to be NULL if the channel is destroyed before we
1845 * received a sample.
1848 struct channel_state_sample
*sample
= caa_container_of(node
,
1849 struct channel_state_sample
,
1850 channel_state_ht_node
);
1852 cds_lfht_del(state
->channel_state_ht
, node
);
1853 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1856 /* Remove the channel from the channels_ht and free it. */
1857 cds_lfht_lookup(state
->channels_ht
,
1858 hash_channel_key(&key
),
1862 node
= cds_lfht_iter_get_node(&iter
);
1864 channel_info
= caa_container_of(node
, struct channel_info
,
1866 cds_lfht_del(state
->channels_ht
, node
);
1867 channel_info_destroy(channel_info
);
1870 *cmd_result
= LTTNG_OK
;
1875 int handle_notification_thread_command_session_rotation(
1876 struct notification_thread_state
*state
,
1877 enum notification_thread_command_type cmd_type
,
1878 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1879 uint64_t trace_archive_chunk_id
,
1880 struct lttng_trace_archive_location
*location
,
1881 enum lttng_error_code
*_cmd_result
)
1884 enum lttng_error_code cmd_result
= LTTNG_OK
;
1885 struct lttng_session_trigger_list
*trigger_list
;
1886 struct lttng_trigger_list_element
*trigger_list_element
;
1887 struct session_info
*session_info
;
1888 const struct lttng_credentials session_creds
= {
1889 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1890 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1895 session_info
= find_or_create_session_info(state
, session_name
,
1896 session_uid
, session_gid
);
1897 if (!session_info
) {
1898 /* Allocation error or an internal error occurred. */
1900 cmd_result
= LTTNG_ERR_NOMEM
;
1904 session_info
->rotation
.ongoing
=
1905 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1906 session_info
->rotation
.id
= trace_archive_chunk_id
;
1907 trigger_list
= get_session_trigger_list(state
, session_name
);
1908 if (!trigger_list
) {
1909 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1914 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1916 const struct lttng_condition
*condition
;
1917 struct lttng_trigger
*trigger
;
1918 struct notification_client_list
*client_list
;
1919 struct lttng_evaluation
*evaluation
= NULL
;
1920 enum lttng_condition_type condition_type
;
1921 enum action_executor_status executor_status
;
1923 trigger
= trigger_list_element
->trigger
;
1924 condition
= lttng_trigger_get_const_condition(trigger
);
1926 condition_type
= lttng_condition_get_type(condition
);
1928 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1929 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1931 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1932 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1936 client_list
= get_client_list_from_condition(state
, condition
);
1937 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1938 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1939 trace_archive_chunk_id
);
1941 evaluation
= lttng_evaluation_session_rotation_completed_create(
1942 trace_archive_chunk_id
, location
);
1946 /* Internal error */
1948 cmd_result
= LTTNG_ERR_UNK
;
1953 * Ownership of `evaluation` transferred to the action executor
1954 * no matter the result.
1956 executor_status
= action_executor_enqueue_trigger(
1957 state
->executor
, trigger
, evaluation
,
1958 &session_creds
, client_list
);
1960 switch (executor_status
) {
1961 case ACTION_EXECUTOR_STATUS_OK
:
1963 case ACTION_EXECUTOR_STATUS_ERROR
:
1964 case ACTION_EXECUTOR_STATUS_INVALID
:
1966 * TODO Add trigger identification (name/id) when
1967 * it is added to the API.
1969 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1972 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1974 * TODO Add trigger identification (name/id) when
1975 * it is added to the API.
1977 * Not a fatal error.
1979 WARN("No space left when enqueuing action associated with session rotation trigger");
1987 notification_client_list_put(client_list
);
1988 if (caa_unlikely(ret
)) {
1993 session_info_put(session_info
);
1994 *_cmd_result
= cmd_result
;
2000 int handle_notification_thread_command_add_tracer_event_source(
2001 struct notification_thread_state
*state
,
2002 int tracer_event_source_fd
,
2003 enum lttng_domain_type domain_type
,
2004 enum lttng_error_code
*_cmd_result
)
2007 enum lttng_error_code cmd_result
= LTTNG_OK
;
2008 struct notification_event_tracer_event_source_element
*element
= NULL
;
2010 element
= zmalloc(sizeof(*element
));
2012 cmd_result
= LTTNG_ERR_NOMEM
;
2017 element
->fd
= tracer_event_source_fd
;
2018 element
->domain
= domain_type
;
2020 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2022 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2023 tracer_event_source_fd
,
2024 lttng_domain_type_str(domain_type
));
2026 /* Adding the read side pipe to the event poll. */
2027 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
2029 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2030 tracer_event_source_fd
,
2031 lttng_domain_type_str(element
->domain
));
2032 cds_list_del(&element
->node
);
2037 element
->is_fd_in_poll_set
= true;
2040 *_cmd_result
= cmd_result
;
2045 int drain_event_notifier_notification_pipe(
2046 struct notification_thread_state
*state
,
2047 int pipe
, enum lttng_domain_type domain
)
2049 struct lttng_poll_event events
= {0};
2052 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2054 ERR("[notification-thread] Error creating lttng_poll_event");
2058 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2060 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2067 * Continue to consume notifications as long as there are new
2068 * ones coming in. The tracer has been asked to stop producing
2071 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2072 * monitored (on Linux, at least) and will be returned when
2073 * the pipe is closed but empty.
2075 ret
= lttng_poll_wait_interruptible(&events
, 0);
2076 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2077 /* No more notification to be read on this pipe. */
2080 } else if (ret
< 0) {
2081 PERROR("Failed on lttng_poll_wait_interruptible() call");
2086 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2088 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2093 lttng_poll_clean(&events
);
2098 int handle_notification_thread_command_remove_tracer_event_source(
2099 struct notification_thread_state
*state
,
2100 int tracer_event_source_fd
,
2101 enum lttng_error_code
*_cmd_result
)
2105 enum lttng_error_code cmd_result
= LTTNG_OK
;
2106 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
2108 cds_list_for_each_entry_safe(source_element
, tmp
,
2109 &state
->tracer_event_sources_list
, node
) {
2110 if (source_element
->fd
!= tracer_event_source_fd
) {
2114 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2115 tracer_event_source_fd
,
2116 lttng_domain_type_str(source_element
->domain
));
2117 cds_list_del(&source_element
->node
);
2124 * This is temporarily allowed since the poll activity set is
2125 * not properly cleaned-up for the moment. This is adressed in
2128 source_element
= NULL
;
2132 if (!source_element
->is_fd_in_poll_set
) {
2133 /* Skip the poll set removal. */
2137 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2138 tracer_event_source_fd
,
2139 lttng_domain_type_str(source_element
->domain
));
2141 /* Removing the fd from the event poll set. */
2142 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2144 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2145 tracer_event_source_fd
,
2146 lttng_domain_type_str(source_element
->domain
));
2147 cmd_result
= LTTNG_ERR_FATAL
;
2151 source_element
->is_fd_in_poll_set
= false;
2153 ret
= drain_event_notifier_notification_pipe(state
, tracer_event_source_fd
,
2154 source_element
->domain
);
2156 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2157 tracer_event_source_fd
,
2158 lttng_domain_type_str(source_element
->domain
));
2159 cmd_result
= LTTNG_ERR_FATAL
;
2164 * The drain_event_notifier_notification_pipe() call might have read
2165 * data from an fd that we received in event in the latest _poll_wait()
2166 * call. Make sure the thread call poll_wait() again to ensure we have
2169 state
->restart_poll
= true;
2172 free(source_element
);
2173 *_cmd_result
= cmd_result
;
2177 int handle_notification_thread_remove_tracer_event_source_no_result(
2178 struct notification_thread_state
*state
,
2179 int tracer_event_source_fd
)
2182 enum lttng_error_code cmd_result
;
2184 ret
= handle_notification_thread_command_remove_tracer_event_source(
2185 state
, tracer_event_source_fd
, &cmd_result
);
2190 static int handle_notification_thread_command_list_triggers(
2191 struct notification_thread_handle
*handle
,
2192 struct notification_thread_state
*state
,
2194 struct lttng_triggers
**triggers
,
2195 enum lttng_error_code
*_cmd_result
)
2198 enum lttng_error_code cmd_result
= LTTNG_OK
;
2199 struct cds_lfht_iter iter
;
2200 struct lttng_trigger_ht_element
*trigger_ht_element
;
2201 struct lttng_triggers
*local_triggers
= NULL
;
2202 const struct lttng_credentials
*creds
;
2206 local_triggers
= lttng_triggers_create();
2207 if (!local_triggers
) {
2208 /* Not a fatal error. */
2209 cmd_result
= LTTNG_ERR_NOMEM
;
2213 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2214 trigger_ht_element
, node
) {
2216 * Only return the triggers to which the client has access.
2217 * The root user has visibility over all triggers.
2219 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2220 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2224 ret
= lttng_triggers_add(local_triggers
,
2225 trigger_ht_element
->trigger
);
2227 /* Not a fatal error. */
2229 cmd_result
= LTTNG_ERR_NOMEM
;
2234 /* Transferring ownership to the caller. */
2235 *triggers
= local_triggers
;
2236 local_triggers
= NULL
;
2240 lttng_triggers_destroy(local_triggers
);
2241 *_cmd_result
= cmd_result
;
2245 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
2246 const char **trigger_name
,
2247 uid_t
*trigger_owner_uid
)
2249 enum lttng_trigger_status trigger_status
;
2251 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
2252 switch (trigger_status
) {
2253 case LTTNG_TRIGGER_STATUS_OK
:
2255 case LTTNG_TRIGGER_STATUS_UNSET
:
2256 *trigger_name
= "(unset)";
2262 trigger_status
= lttng_trigger_get_owner_uid(trigger
,
2264 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2267 static int handle_notification_thread_command_get_trigger(
2268 struct notification_thread_state
*state
,
2269 const struct lttng_trigger
*trigger
,
2270 struct lttng_trigger
**registered_trigger
,
2271 enum lttng_error_code
*_cmd_result
)
2274 struct cds_lfht_iter iter
;
2275 struct lttng_trigger_ht_element
*trigger_ht_element
;
2276 enum lttng_error_code cmd_result
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2277 const char *trigger_name
;
2278 uid_t trigger_owner_uid
;
2282 cds_lfht_for_each_entry(
2283 state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2284 if (lttng_trigger_is_equal(
2285 trigger
, trigger_ht_element
->trigger
)) {
2286 /* Take one reference on the return trigger. */
2287 *registered_trigger
= trigger_ht_element
->trigger
;
2288 lttng_trigger_get(*registered_trigger
);
2290 cmd_result
= LTTNG_OK
;
2295 /* Not a fatal error if the trigger is not found. */
2296 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
2297 ERR("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d",
2298 trigger_name
, (int) trigger_owner_uid
);
2304 *_cmd_result
= cmd_result
;
2309 bool condition_is_supported(struct lttng_condition
*condition
)
2313 switch (lttng_condition_get_type(condition
)) {
2314 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2315 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2318 enum lttng_domain_type domain
;
2320 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2324 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2325 is_supported
= true;
2330 * Older kernel tracers don't expose the API to monitor their
2331 * buffers. Therefore, we reject triggers that require that
2332 * mechanism to be available to be evaluated.
2334 * Assume unsupported on error.
2336 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2339 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
2341 const struct lttng_event_rule
*event_rule
;
2342 enum lttng_domain_type domain
;
2343 const enum lttng_condition_status status
=
2344 lttng_condition_event_rule_matches_get_rule(
2345 condition
, &event_rule
);
2347 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2349 domain
= lttng_event_rule_get_domain_type(event_rule
);
2350 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2351 is_supported
= true;
2356 * Older kernel tracers can't emit notification. Therefore, we
2357 * reject triggers that require that mechanism to be available
2360 * Assume unsupported on error.
2362 is_supported
= kernel_supports_event_notifiers() == 1;
2366 is_supported
= true;
2369 return is_supported
;
2372 /* Must be called with RCU read lock held. */
2374 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2375 struct notification_thread_state
*state
)
2378 const struct lttng_condition
*condition
;
2379 const char *session_name
;
2380 struct lttng_session_trigger_list
*trigger_list
;
2382 condition
= lttng_trigger_get_const_condition(trigger
);
2383 switch (lttng_condition_get_type(condition
)) {
2384 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2385 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2387 enum lttng_condition_status status
;
2389 status
= lttng_condition_session_rotation_get_session_name(
2390 condition
, &session_name
);
2391 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2392 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2403 trigger_list
= get_session_trigger_list(state
, session_name
);
2404 if (!trigger_list
) {
2405 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2411 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2413 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2418 /* Must be called with RCU read lock held. */
2420 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2421 struct notification_thread_state
*state
)
2424 struct cds_lfht_node
*node
;
2425 struct cds_lfht_iter iter
;
2426 struct channel_info
*channel
;
2428 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2430 struct lttng_trigger_list_element
*trigger_list_element
;
2431 struct lttng_channel_trigger_list
*trigger_list
;
2432 struct cds_lfht_iter lookup_iter
;
2434 if (!trigger_applies_to_channel(trigger
, channel
)) {
2438 cds_lfht_lookup(state
->channel_triggers_ht
,
2439 hash_channel_key(&channel
->key
),
2440 match_channel_trigger_list
,
2443 node
= cds_lfht_iter_get_node(&lookup_iter
);
2445 trigger_list
= caa_container_of(node
,
2446 struct lttng_channel_trigger_list
,
2447 channel_triggers_ht_node
);
2449 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2450 if (!trigger_list_element
) {
2454 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2455 trigger_list_element
->trigger
= trigger
;
2456 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2457 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2465 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2467 bool is_notify
= false;
2468 unsigned int i
, count
;
2469 enum lttng_action_status action_status
;
2470 const struct lttng_action
*action
=
2471 lttng_trigger_get_const_action(trigger
);
2472 enum lttng_action_type action_type
;
2475 action_type
= lttng_action_get_type(action
);
2476 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2479 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2483 action_status
= lttng_action_group_get_count(action
, &count
);
2484 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2486 for (i
= 0; i
< count
; i
++) {
2487 const struct lttng_action
*inner_action
=
2488 lttng_action_group_get_at_index(
2491 action_type
= lttng_action_get_type(inner_action
);
2492 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2502 static bool trigger_name_taken(struct notification_thread_state
*state
,
2503 const struct lttng_trigger
*trigger
)
2505 struct cds_lfht_iter iter
;
2508 * No duplicata is allowed in the triggers_by_name_uid_ht.
2509 * The match is done against the trigger name and uid.
2511 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2512 hash_trigger_by_name_uid(trigger
),
2513 match_trigger_by_name_uid
,
2516 return !!cds_lfht_iter_get_node(&iter
);
2520 enum lttng_error_code
generate_trigger_name(
2521 struct notification_thread_state
*state
,
2522 struct lttng_trigger
*trigger
, const char **name
)
2524 enum lttng_error_code ret_code
= LTTNG_OK
;
2526 enum lttng_trigger_status status
;
2529 const int ret
= lttng_trigger_generate_name(trigger
,
2530 state
->trigger_id
.name_offset
++);
2532 /* The only reason this can fail right now. */
2533 ret_code
= LTTNG_ERR_NOMEM
;
2537 status
= lttng_trigger_get_name(trigger
, name
);
2538 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2540 taken
= trigger_name_taken(state
, trigger
);
2541 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2547 void notif_thread_state_remove_trigger_ht_elem(
2548 struct notification_thread_state
*state
,
2549 struct lttng_trigger_ht_element
*trigger_ht_element
)
2552 assert(trigger_ht_element
);
2554 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2555 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2559 enum lttng_error_code
setup_tracer_notifier(
2560 struct notification_thread_state
*state
,
2561 struct lttng_trigger
*trigger
)
2563 enum lttng_error_code ret
;
2564 enum event_notifier_error_accounting_status error_accounting_status
;
2565 struct cds_lfht_node
*node
;
2566 uint64_t error_counter_index
= 0;
2567 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2568 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2570 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2571 if (!trigger_tokens_ht_element
) {
2572 ret
= LTTNG_ERR_NOMEM
;
2576 /* Add trigger token to the trigger_tokens_ht. */
2577 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2578 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2579 trigger_tokens_ht_element
->trigger
= trigger
;
2581 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2582 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2583 match_trigger_token
,
2584 &trigger_tokens_ht_element
->token
,
2585 &trigger_tokens_ht_element
->node
);
2586 if (node
!= &trigger_tokens_ht_element
->node
) {
2587 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2588 goto error_free_ht_element
;
2591 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2592 trigger
, &error_counter_index
);
2593 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2594 if (error_accounting_status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2595 DBG("Trigger group error accounting counter full.");
2596 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2598 ERR("Error registering trigger for error accounting");
2599 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2602 goto error_remove_ht_element
;
2605 lttng_condition_event_rule_matches_set_error_counter_index(
2606 condition
, error_counter_index
);
2611 error_remove_ht_element
:
2612 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2613 error_free_ht_element
:
2614 free(trigger_tokens_ht_element
);
2620 * FIXME A client's credentials are not checked when registering a trigger.
2622 * The effects of this are benign since:
2623 * - The client will succeed in registering the trigger, as it is valid,
2624 * - The trigger will, internally, be bound to the channel/session,
2625 * - The notifications will not be sent since the client's credentials
2626 * are checked against the channel at that moment.
2628 * If this function returns a non-zero value, it means something is
2629 * fundamentally broken and the whole subsystem/thread will be torn down.
2631 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2635 int handle_notification_thread_command_register_trigger(
2636 struct notification_thread_state
*state
,
2637 struct lttng_trigger
*trigger
,
2638 enum lttng_error_code
*cmd_result
)
2641 struct lttng_condition
*condition
;
2642 struct notification_client_list
*client_list
= NULL
;
2643 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2644 struct cds_lfht_node
*node
;
2645 const char* trigger_name
;
2646 bool free_trigger
= true;
2647 struct lttng_evaluation
*evaluation
= NULL
;
2648 struct lttng_credentials object_creds
;
2651 enum action_executor_status executor_status
;
2652 const uint64_t trigger_tracer_token
=
2653 state
->trigger_id
.next_tracer_token
++;
2657 /* Set the trigger's tracer token. */
2658 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2660 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2661 LTTNG_TRIGGER_STATUS_UNSET
) {
2662 const enum lttng_error_code ret_code
= generate_trigger_name(
2663 state
, trigger
, &trigger_name
);
2665 if (ret_code
!= LTTNG_OK
) {
2668 *cmd_result
= ret_code
;
2671 } else if (trigger_name_taken(state
, trigger
)) {
2672 /* Not a fatal error. */
2673 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2678 condition
= lttng_trigger_get_condition(trigger
);
2681 /* Some conditions require tracers to implement a minimal ABI version. */
2682 if (!condition_is_supported(condition
)) {
2683 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2687 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2688 if (!trigger_ht_element
) {
2693 /* Add trigger to the trigger_ht. */
2694 cds_lfht_node_init(&trigger_ht_element
->node
);
2695 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2696 trigger_ht_element
->trigger
= trigger
;
2698 node
= cds_lfht_add_unique(state
->triggers_ht
,
2699 lttng_condition_hash(condition
),
2702 &trigger_ht_element
->node
);
2703 if (node
!= &trigger_ht_element
->node
) {
2704 /* Not a fatal error, simply report it to the client. */
2705 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2706 goto error_free_ht_element
;
2709 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2710 hash_trigger_by_name_uid(trigger
),
2711 match_trigger_by_name_uid
,
2713 &trigger_ht_element
->node_by_name_uid
);
2714 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2715 /* Not a fatal error, simply report it to the client. */
2716 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2717 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2718 goto error_free_ht_element
;
2721 /* From this point consider the trigger registered. */
2722 lttng_trigger_set_as_registered(trigger
);
2725 * Some triggers might need a tracer notifier depending on its
2726 * condition and actions.
2728 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2729 enum lttng_error_code error_code
;
2731 error_code
= setup_tracer_notifier(state
, trigger
);
2732 if (error_code
!= LTTNG_OK
) {
2733 notif_thread_state_remove_trigger_ht_elem(state
,
2734 trigger_ht_element
);
2735 if (error_code
== LTTNG_ERR_NOMEM
) {
2738 *cmd_result
= error_code
;
2742 goto error_free_ht_element
;
2747 * The rest only applies to triggers that have a "notify" action.
2748 * It is not skipped as this is the only action type currently
2751 if (is_trigger_action_notify(trigger
)) {
2753 * Find or create the client list of this condition. It may
2754 * already be present if another trigger is already registered
2755 * with the same condition.
2757 client_list
= get_client_list_from_condition(state
, condition
);
2760 * No client list for this condition yet. We create new
2761 * one and build it up.
2763 client_list
= notification_client_list_create(state
, condition
);
2765 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2766 goto error_free_ht_element
;
2770 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2772 pthread_mutex_lock(&client_list
->lock
);
2773 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2774 pthread_mutex_unlock(&client_list
->lock
);
2778 * Ownership of the trigger and of its wrapper was transfered to
2779 * the triggers_ht. Same for token ht element if necessary.
2781 trigger_ht_element
= NULL
;
2782 free_trigger
= false;
2784 switch (get_condition_binding_object(condition
)) {
2785 case LTTNG_OBJECT_TYPE_SESSION
:
2786 /* Add the trigger to the list if it matches a known session. */
2787 ret
= bind_trigger_to_matching_session(trigger
, state
);
2789 goto error_free_ht_element
;
2792 case LTTNG_OBJECT_TYPE_CHANNEL
:
2794 * Add the trigger to list of triggers bound to the channels
2797 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2799 goto error_free_ht_element
;
2802 case LTTNG_OBJECT_TYPE_NONE
:
2805 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2807 goto error_free_ht_element
;
2811 * The new trigger's condition must be evaluated against the current
2814 * In the case of `notify` action, nothing preventing clients from
2815 * subscribing to a condition before the corresponding trigger is
2816 * registered, we have to evaluate this new condition right away.
2818 * At some point, we were waiting for the next "evaluation" (e.g. on
2819 * reception of a channel sample) to evaluate this new condition, but
2822 * The reason it was broken is that waiting for the next sample
2823 * does not allow us to properly handle transitions for edge-triggered
2826 * Consider this example: when we handle a new channel sample, we
2827 * evaluate each conditions twice: once with the previous state, and
2828 * again with the newest state. We then use those two results to
2829 * determine whether a state change happened: a condition was false and
2830 * became true. If a state change happened, we have to notify clients.
2832 * Now, if a client subscribes to a given notification and registers
2833 * a trigger *after* that subscription, we have to make sure the
2834 * condition is evaluated at this point while considering only the
2835 * current state. Otherwise, the next evaluation cycle may only see
2836 * that the evaluations remain the same (true for samples n-1 and n) and
2837 * the client will never know that the condition has been met.
2839 switch (get_condition_binding_object(condition
)) {
2840 case LTTNG_OBJECT_TYPE_SESSION
:
2841 ret
= evaluate_session_condition_for_client(condition
, state
,
2842 &evaluation
, &object_uid
,
2844 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2845 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2847 case LTTNG_OBJECT_TYPE_CHANNEL
:
2848 ret
= evaluate_channel_condition_for_client(condition
, state
,
2849 &evaluation
, &object_uid
,
2851 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2852 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2854 case LTTNG_OBJECT_TYPE_NONE
:
2857 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2865 goto error_free_ht_element
;
2868 DBG("Newly registered trigger's condition evaluated to %s",
2869 evaluation
? "true" : "false");
2871 /* Evaluation yielded nothing. Normal exit. */
2877 * Ownership of `evaluation` transferred to the action executor
2878 * no matter the result.
2880 executor_status
= action_executor_enqueue_trigger(state
->executor
,
2881 trigger
, evaluation
, &object_creds
, client_list
);
2883 switch (executor_status
) {
2884 case ACTION_EXECUTOR_STATUS_OK
:
2886 case ACTION_EXECUTOR_STATUS_ERROR
:
2887 case ACTION_EXECUTOR_STATUS_INVALID
:
2889 * TODO Add trigger identification (name/id) when
2890 * it is added to the API.
2892 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2894 goto error_free_ht_element
;
2895 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2897 * TODO Add trigger identification (name/id) when
2898 * it is added to the API.
2900 * Not a fatal error.
2902 WARN("No space left when enqueuing action associated to newly registered trigger");
2910 *cmd_result
= LTTNG_OK
;
2911 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2912 trigger_name
, trigger_tracer_token
);
2915 error_free_ht_element
:
2916 if (trigger_ht_element
) {
2917 /* Delayed removal due to RCU constraint on delete. */
2918 call_rcu(&trigger_ht_element
->rcu_node
,
2919 free_lttng_trigger_ht_element_rcu
);
2924 * Other objects might have a reference to the trigger, mark it
2927 lttng_trigger_set_as_unregistered(trigger
);
2928 lttng_trigger_destroy(trigger
);
2936 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2938 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2943 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2945 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2950 void teardown_tracer_notifier(struct notification_thread_state
*state
,
2951 const struct lttng_trigger
*trigger
)
2953 struct cds_lfht_iter iter
;
2954 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2956 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
2957 trigger_tokens_ht_element
, node
) {
2959 if (!lttng_trigger_is_equal(trigger
,
2960 trigger_tokens_ht_element
->trigger
)) {
2964 event_notifier_error_accounting_unregister_event_notifier(
2965 trigger_tokens_ht_element
->trigger
);
2967 /* TODO talk to all app and remove it */
2968 DBG("[notification-thread] Removed trigger from tokens_ht");
2969 cds_lfht_del(state
->trigger_tokens_ht
,
2970 &trigger_tokens_ht_element
->node
);
2972 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2973 free_notification_trigger_tokens_ht_element_rcu
);
2980 int handle_notification_thread_command_unregister_trigger(
2981 struct notification_thread_state
*state
,
2982 const struct lttng_trigger
*trigger
,
2983 enum lttng_error_code
*_cmd_reply
)
2985 struct cds_lfht_iter iter
;
2986 struct cds_lfht_node
*triggers_ht_node
;
2987 struct lttng_channel_trigger_list
*trigger_list
;
2988 struct notification_client_list
*client_list
;
2989 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2990 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
2992 enum lttng_error_code cmd_reply
;
2996 cds_lfht_lookup(state
->triggers_ht
,
2997 lttng_condition_hash(condition
),
3001 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3002 if (!triggers_ht_node
) {
3003 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3006 cmd_reply
= LTTNG_OK
;
3009 trigger_ht_element
= caa_container_of(triggers_ht_node
,
3010 struct lttng_trigger_ht_element
, node
);
3012 /* Remove trigger from channel_triggers_ht. */
3013 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
3014 channel_triggers_ht_node
) {
3015 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3017 cds_list_for_each_entry_safe(trigger_element
, tmp
,
3018 &trigger_list
->list
, node
) {
3019 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3023 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
3024 cds_list_del(&trigger_element
->node
);
3025 /* A trigger can only appear once per channel */
3030 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3031 teardown_tracer_notifier(state
, trigger
);
3034 if (is_trigger_action_notify(trigger
)) {
3036 * Remove and release the client list from
3037 * notification_trigger_clients_ht.
3039 client_list
= get_client_list_from_condition(state
, condition
);
3040 assert(client_list
);
3042 pthread_mutex_lock(&client_list
->lock
);
3043 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3044 pthread_mutex_unlock(&client_list
->lock
);
3046 /* Put new reference and the hashtable's reference. */
3047 notification_client_list_put(client_list
);
3048 notification_client_list_put(client_list
);
3052 /* Remove trigger from triggers_ht. */
3053 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3055 /* Release the ownership of the trigger. */
3056 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3057 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3061 *_cmd_reply
= cmd_reply
;
3066 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3067 int handle_notification_thread_command(
3068 struct notification_thread_handle
*handle
,
3069 struct notification_thread_state
*state
)
3073 struct notification_thread_command
*cmd
;
3075 /* Read the event pipe to put it back into a quiescent state. */
3076 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
3078 if (ret
!= sizeof(counter
)) {
3082 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3083 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3084 struct notification_thread_command
, cmd_list_node
);
3085 cds_list_del(&cmd
->cmd_list_node
);
3086 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3088 DBG("[notification-thread] Received `%s` command",
3089 notification_command_type_str(cmd
->type
));
3090 switch (cmd
->type
) {
3091 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3092 ret
= handle_notification_thread_command_register_trigger(state
,
3093 cmd
->parameters
.register_trigger
.trigger
,
3096 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3097 ret
= handle_notification_thread_command_unregister_trigger(
3099 cmd
->parameters
.unregister_trigger
.trigger
,
3102 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3103 ret
= handle_notification_thread_command_add_channel(
3105 cmd
->parameters
.add_channel
.session
.name
,
3106 cmd
->parameters
.add_channel
.session
.uid
,
3107 cmd
->parameters
.add_channel
.session
.gid
,
3108 cmd
->parameters
.add_channel
.channel
.name
,
3109 cmd
->parameters
.add_channel
.channel
.domain
,
3110 cmd
->parameters
.add_channel
.channel
.key
,
3111 cmd
->parameters
.add_channel
.channel
.capacity
,
3114 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3115 ret
= handle_notification_thread_command_remove_channel(
3116 state
, cmd
->parameters
.remove_channel
.key
,
3117 cmd
->parameters
.remove_channel
.domain
,
3120 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3121 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3122 ret
= handle_notification_thread_command_session_rotation(
3125 cmd
->parameters
.session_rotation
.session_name
,
3126 cmd
->parameters
.session_rotation
.uid
,
3127 cmd
->parameters
.session_rotation
.gid
,
3128 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3129 cmd
->parameters
.session_rotation
.location
,
3132 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3133 ret
= handle_notification_thread_command_add_tracer_event_source(
3135 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3136 cmd
->parameters
.tracer_event_source
.domain
,
3139 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3140 ret
= handle_notification_thread_command_remove_tracer_event_source(
3142 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3145 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3147 struct lttng_triggers
*triggers
= NULL
;
3149 ret
= handle_notification_thread_command_list_triggers(
3152 cmd
->parameters
.list_triggers
.uid
,
3155 cmd
->reply
.list_triggers
.triggers
= triggers
;
3159 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3160 cmd
->reply_code
= LTTNG_OK
;
3163 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3165 struct lttng_trigger
*trigger
= NULL
;
3167 ret
= handle_notification_thread_command_get_trigger(state
,
3168 cmd
->parameters
.get_trigger
.trigger
, &trigger
,
3170 cmd
->reply
.get_trigger
.trigger
= trigger
;
3173 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3175 const enum client_transmission_status client_status
=
3176 cmd
->parameters
.client_communication_update
3178 const notification_client_id client_id
=
3179 cmd
->parameters
.client_communication_update
.id
;
3180 struct notification_client
*client
;
3183 client
= get_client_from_id(client_id
, state
);
3187 * Client error was probably already picked-up by the
3188 * notification thread or it has disconnected
3189 * gracefully while this command was queued.
3191 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3195 ret
= client_handle_transmission_status(
3196 client
, client_status
, state
);
3202 ERR("[notification-thread] Unknown internal command received");
3210 if (cmd
->is_async
) {
3214 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3218 /* Wake-up and return a fatal error to the calling thread. */
3219 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3220 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3222 /* Indicate a fatal error to the caller. */
3227 int socket_set_non_blocking(int socket
)
3231 /* Set the pipe as non-blocking. */
3232 ret
= fcntl(socket
, F_GETFL
, 0);
3234 PERROR("fcntl get socket flags");
3239 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3241 PERROR("fcntl set O_NONBLOCK socket flag");
3244 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3250 int client_reset_inbound_state(struct notification_client
*client
)
3255 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3257 client
->communication
.inbound
.bytes_to_receive
=
3258 sizeof(struct lttng_notification_channel_message
);
3259 client
->communication
.inbound
.msg_type
=
3260 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3261 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3262 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3263 ret
= lttng_dynamic_buffer_set_size(
3264 &client
->communication
.inbound
.payload
.buffer
,
3265 client
->communication
.inbound
.bytes_to_receive
);
3270 int handle_notification_thread_client_connect(
3271 struct notification_thread_state
*state
)
3274 struct notification_client
*client
;
3276 DBG("[notification-thread] Handling new notification channel client connection");
3278 client
= zmalloc(sizeof(*client
));
3285 pthread_mutex_init(&client
->lock
, NULL
);
3286 client
->id
= state
->next_notification_client_id
++;
3287 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3288 lttng_payload_init(&client
->communication
.inbound
.payload
);
3289 lttng_payload_init(&client
->communication
.outbound
.payload
);
3290 client
->communication
.inbound
.expect_creds
= true;
3292 ret
= client_reset_inbound_state(client
);
3294 ERR("[notification-thread] Failed to reset client communication's inbound state");
3299 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3301 ERR("[notification-thread] Failed to accept new notification channel client connection");
3306 client
->socket
= ret
;
3308 ret
= socket_set_non_blocking(client
->socket
);
3310 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3314 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3316 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3321 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3322 LPOLLIN
| LPOLLERR
|
3323 LPOLLHUP
| LPOLLRDHUP
);
3325 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3329 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3333 cds_lfht_add(state
->client_socket_ht
,
3334 hash_client_socket(client
->socket
),
3335 &client
->client_socket_ht_node
);
3336 cds_lfht_add(state
->client_id_ht
,
3337 hash_client_id(client
->id
),
3338 &client
->client_id_ht_node
);
3344 notification_client_destroy(client
, state
);
3349 * RCU read-lock must be held by the caller.
3350 * Client lock must _not_ be held by the caller.
3353 int notification_thread_client_disconnect(
3354 struct notification_client
*client
,
3355 struct notification_thread_state
*state
)
3358 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3360 /* Acquire the client lock to disable its communication atomically. */
3361 pthread_mutex_lock(&client
->lock
);
3362 client
->communication
.active
= false;
3363 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3364 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3365 pthread_mutex_unlock(&client
->lock
);
3367 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3369 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3373 /* Release all conditions to which the client was subscribed. */
3374 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3375 &client
->condition_list
, node
) {
3376 (void) notification_thread_client_unsubscribe(client
,
3377 condition_list_element
->condition
, state
, NULL
);
3381 * Client no longer accessible to other threads (through the
3384 notification_client_destroy(client
, state
);
3388 int handle_notification_thread_client_disconnect(
3389 int client_socket
, struct notification_thread_state
*state
)
3392 struct notification_client
*client
;
3395 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3397 client
= get_client_from_socket(client_socket
, state
);
3399 /* Internal state corruption, fatal error. */
3400 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3406 ret
= notification_thread_client_disconnect(client
, state
);
3412 int handle_notification_thread_client_disconnect_all(
3413 struct notification_thread_state
*state
)
3415 struct cds_lfht_iter iter
;
3416 struct notification_client
*client
;
3417 bool error_encoutered
= false;
3420 DBG("[notification-thread] Closing all client connections");
3421 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3422 client_socket_ht_node
) {
3425 ret
= notification_thread_client_disconnect(
3428 error_encoutered
= true;
3432 return error_encoutered
? 1 : 0;
3435 int handle_notification_thread_trigger_unregister_all(
3436 struct notification_thread_state
*state
)
3438 bool error_occurred
= false;
3439 struct cds_lfht_iter iter
;
3440 struct lttng_trigger_ht_element
*trigger_ht_element
;
3443 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3445 int ret
= handle_notification_thread_command_unregister_trigger(
3446 state
, trigger_ht_element
->trigger
, NULL
);
3448 error_occurred
= true;
3452 return error_occurred
? -1 : 0;
3456 int client_handle_transmission_status(
3457 struct notification_client
*client
,
3458 enum client_transmission_status transmission_status
,
3459 struct notification_thread_state
*state
)
3463 switch (transmission_status
) {
3464 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3465 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3466 CLIENT_POLL_MASK_IN
);
3472 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3474 * We want to be notified whenever there is buffer space
3475 * available to send the rest of the payload.
3477 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3478 CLIENT_POLL_MASK_IN_OUT
);
3483 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3484 ret
= notification_thread_client_disconnect(client
, state
);
3489 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3499 /* Client lock must be acquired by caller. */
3501 enum client_transmission_status
client_flush_outgoing_queue(
3502 struct notification_client
*client
)
3505 size_t to_send_count
;
3506 enum client_transmission_status status
;
3507 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3508 &client
->communication
.outbound
.payload
, 0, -1);
3509 const int fds_to_send_count
=
3510 lttng_payload_view_get_fd_handle_count(&pv
);
3512 ASSERT_LOCKED(client
->lock
);
3514 if (!client
->communication
.active
) {
3515 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3519 if (pv
.buffer
.size
== 0) {
3521 * If both data and fds are equal to zero, we are in an invalid
3524 assert(fds_to_send_count
!= 0);
3529 to_send_count
= pv
.buffer
.size
;
3530 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3533 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3536 if ((ret
>= 0 && ret
< to_send_count
)) {
3537 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3539 to_send_count
-= max(ret
, 0);
3541 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3543 pv
.buffer
.size
- to_send_count
,
3545 ret
= lttng_dynamic_buffer_set_size(
3546 &client
->communication
.outbound
.payload
.buffer
,
3552 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3554 } else if (ret
< 0) {
3555 /* Generic error, disable the client's communication. */
3556 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3558 client
->communication
.active
= false;
3559 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3563 * No error and flushed the queue completely.
3565 * The payload buffer size is used later to
3566 * check if there is notifications queued. So albeit that the
3567 * direct caller knows that the transmission is complete, we
3568 * need to set the buffer size to zero.
3570 ret
= lttng_dynamic_buffer_set_size(
3571 &client
->communication
.outbound
.payload
.buffer
, 0);
3578 /* No fds to send, transmission is complete. */
3579 if (fds_to_send_count
== 0) {
3580 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3584 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3585 client
->socket
, &pv
);
3587 /* Generic error, disable the client's communication. */
3588 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3590 client
->communication
.active
= false;
3591 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3593 } else if (ret
== 0) {
3594 /* Nothing could be sent. */
3595 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3597 /* Fd passing is an all or nothing kind of thing. */
3598 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3600 * The payload _fd_array count is used later to
3601 * check if there is notifications queued. So although the
3602 * direct caller knows that the transmission is complete, we
3603 * need to clear the _fd_array for the queuing check.
3605 lttng_dynamic_pointer_array_clear(
3606 &client
->communication
.outbound
.payload
3611 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3612 client
->communication
.outbound
.queued_command_reply
= false;
3613 client
->communication
.outbound
.dropped_notification
= false;
3614 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3619 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3623 bool client_has_outbound_data_left(
3624 const struct notification_client
*client
)
3626 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3627 &client
->communication
.outbound
.payload
, 0, -1);
3628 const bool has_data
= pv
.buffer
.size
!= 0;
3629 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3631 return has_data
|| has_fds
;
3634 /* Client lock must _not_ be held by the caller. */
3636 int client_send_command_reply(struct notification_client
*client
,
3637 struct notification_thread_state
*state
,
3638 enum lttng_notification_channel_status status
)
3641 struct lttng_notification_channel_command_reply reply
= {
3642 .status
= (int8_t) status
,
3644 struct lttng_notification_channel_message msg
= {
3645 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3646 .size
= sizeof(reply
),
3648 char buffer
[sizeof(msg
) + sizeof(reply
)];
3649 enum client_transmission_status transmission_status
;
3651 memcpy(buffer
, &msg
, sizeof(msg
));
3652 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3653 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3655 pthread_mutex_lock(&client
->lock
);
3656 if (client
->communication
.outbound
.queued_command_reply
) {
3657 /* Protocol error. */
3661 /* Enqueue buffer to outgoing queue and flush it. */
3662 ret
= lttng_dynamic_buffer_append(
3663 &client
->communication
.outbound
.payload
.buffer
,
3664 buffer
, sizeof(buffer
));
3669 transmission_status
= client_flush_outgoing_queue(client
);
3671 if (client_has_outbound_data_left(client
)) {
3672 /* Queue could not be emptied. */
3673 client
->communication
.outbound
.queued_command_reply
= true;
3676 pthread_mutex_unlock(&client
->lock
);
3677 ret
= client_handle_transmission_status(
3678 client
, transmission_status
, state
);
3685 pthread_mutex_unlock(&client
->lock
);
3691 int client_handle_message_unknown(struct notification_client
*client
,
3692 struct notification_thread_state
*state
)
3696 * Receiving message header. The function will be called again
3697 * once the rest of the message as been received and can be
3700 const struct lttng_notification_channel_message
*msg
;
3702 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3703 msg
= (const struct lttng_notification_channel_message
*)
3704 client
->communication
.inbound
.payload
.buffer
.data
;
3706 if (msg
->size
== 0 ||
3707 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3708 ERR("[notification-thread] Invalid notification channel message: length = %u",
3714 switch (msg
->type
) {
3715 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3716 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3717 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3721 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3725 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3726 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3727 client
->communication
.inbound
.msg_type
=
3728 (enum lttng_notification_channel_message_type
) msg
->type
;
3729 ret
= lttng_dynamic_buffer_set_size(
3730 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3732 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3739 int client_handle_message_handshake(struct notification_client
*client
,
3740 struct notification_thread_state
*state
)
3743 struct lttng_notification_channel_command_handshake
*handshake_client
;
3744 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3745 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3746 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3748 const struct lttng_notification_channel_message msg_header
= {
3749 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3750 .size
= sizeof(handshake_reply
),
3752 enum lttng_notification_channel_status status
=
3753 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3754 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3756 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3757 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3758 sizeof(handshake_reply
));
3761 (struct lttng_notification_channel_command_handshake
*)
3762 client
->communication
.inbound
.payload
.buffer
3764 client
->major
= handshake_client
->major
;
3765 client
->minor
= handshake_client
->minor
;
3766 if (!client
->communication
.inbound
.creds_received
) {
3767 ERR("[notification-thread] No credentials received from client");
3772 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3773 &client
->communication
.inbound
.creds
);
3774 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3775 &client
->communication
.inbound
.creds
);
3776 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3777 client
->uid
, client
->gid
, (int) client
->major
,
3778 (int) client
->minor
);
3780 if (handshake_client
->major
!=
3781 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3782 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3785 pthread_mutex_lock(&client
->lock
);
3786 /* Outgoing queue will be flushed when the command reply is sent. */
3787 ret
= lttng_dynamic_buffer_append(
3788 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3789 sizeof(send_buffer
));
3791 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3795 client
->validated
= true;
3796 client
->communication
.active
= true;
3797 pthread_mutex_unlock(&client
->lock
);
3799 /* Set reception state to receive the next message header. */
3800 ret
= client_reset_inbound_state(client
);
3802 ERR("[notification-thread] Failed to reset client communication's inbound state");
3806 /* Flushes the outgoing queue. */
3807 ret
= client_send_command_reply(client
, state
, status
);
3809 ERR("[notification-thread] Failed to send reply to notification channel client");
3815 pthread_mutex_unlock(&client
->lock
);
3821 int client_handle_message_subscription(
3822 struct notification_client
*client
,
3823 enum lttng_notification_channel_message_type msg_type
,
3824 struct notification_thread_state
*state
)
3827 struct lttng_condition
*condition
;
3828 enum lttng_notification_channel_status status
=
3829 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3830 struct lttng_payload_view condition_view
=
3831 lttng_payload_view_from_payload(
3832 &client
->communication
.inbound
.payload
,
3834 size_t expected_condition_size
;
3837 * No need to lock client to sample the inbound state as the only
3838 * other thread accessing clients (action executor) only uses the
3841 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3842 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3843 if (ret
!= expected_condition_size
) {
3844 ERR("[notification-thread] Malformed condition received from client");
3848 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3849 ret
= notification_thread_client_subscribe(
3850 client
, condition
, state
, &status
);
3852 ret
= notification_thread_client_unsubscribe(
3853 client
, condition
, state
, &status
);
3860 /* Set reception state to receive the next message header. */
3861 ret
= client_reset_inbound_state(client
);
3863 ERR("[notification-thread] Failed to reset client communication's inbound state");
3867 ret
= client_send_command_reply(client
, state
, status
);
3869 ERR("[notification-thread] Failed to send reply to notification channel client");
3878 int client_dispatch_message(struct notification_client
*client
,
3879 struct notification_thread_state
*state
)
3883 if (client
->communication
.inbound
.msg_type
!=
3884 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3885 client
->communication
.inbound
.msg_type
!=
3886 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3887 !client
->validated
) {
3888 WARN("[notification-thread] client attempted a command before handshake");
3893 switch (client
->communication
.inbound
.msg_type
) {
3894 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3896 ret
= client_handle_message_unknown(client
, state
);
3899 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3901 ret
= client_handle_message_handshake(client
, state
);
3904 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3905 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3907 ret
= client_handle_message_subscription(client
,
3908 client
->communication
.inbound
.msg_type
, state
);
3918 /* Incoming data from client. */
3919 int handle_notification_thread_client_in(
3920 struct notification_thread_state
*state
, int socket
)
3923 struct notification_client
*client
;
3928 client
= get_client_from_socket(socket
, state
);
3930 /* Internal error, abort. */
3935 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3936 client
->communication
.inbound
.bytes_to_receive
;
3937 if (client
->communication
.inbound
.expect_creds
) {
3938 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3939 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3940 client
->communication
.inbound
.bytes_to_receive
,
3941 &client
->communication
.inbound
.creds
);
3943 client
->communication
.inbound
.expect_creds
= false;
3944 client
->communication
.inbound
.creds_received
= true;
3947 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3948 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3949 client
->communication
.inbound
.bytes_to_receive
);
3951 if (recv_ret
>= 0) {
3952 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3954 goto error_disconnect_client
;
3957 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3958 /* Message incomplete wait for more data. */
3963 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3966 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3967 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3969 client
->communication
.inbound
.fds_to_receive
,
3970 &client
->communication
.inbound
.payload
);
3973 * Fds received. non blocking fds passing is all
3976 ssize_t expected_size
;
3978 expected_size
= sizeof(int) *
3979 client
->communication
.inbound
3981 assert(ret
== expected_size
);
3982 client
->communication
.inbound
.fds_to_receive
= 0;
3983 } else if (ret
== 0) {
3984 /* Received nothing. */
3988 goto error_disconnect_client
;
3992 /* At this point the message is complete.*/
3993 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3994 client
->communication
.inbound
.fds_to_receive
== 0);
3995 ret
= client_dispatch_message(client
, state
);
3998 * Only returns an error if this client must be
4001 goto error_disconnect_client
;
4008 error_disconnect_client
:
4009 ret
= notification_thread_client_disconnect(client
, state
);
4013 /* Client ready to receive outgoing data. */
4014 int handle_notification_thread_client_out(
4015 struct notification_thread_state
*state
, int socket
)
4018 struct notification_client
*client
;
4019 enum client_transmission_status transmission_status
;
4022 client
= get_client_from_socket(socket
, state
);
4024 /* Internal error, abort. */
4029 pthread_mutex_lock(&client
->lock
);
4030 transmission_status
= client_flush_outgoing_queue(client
);
4031 pthread_mutex_unlock(&client
->lock
);
4033 ret
= client_handle_transmission_status(
4034 client
, transmission_status
, state
);
4044 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4045 const struct channel_state_sample
*sample
,
4046 uint64_t buffer_capacity
)
4048 bool result
= false;
4050 enum lttng_condition_type condition_type
;
4051 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4052 condition
, struct lttng_condition_buffer_usage
,
4055 if (use_condition
->threshold_bytes
.set
) {
4056 threshold
= use_condition
->threshold_bytes
.value
;
4059 * Threshold was expressed as a ratio.
4061 * TODO the threshold (in bytes) of conditions expressed
4062 * as a ratio of total buffer size could be cached to
4063 * forego this double-multiplication or it could be performed
4064 * as fixed-point math.
4066 * Note that caching should accommodates the case where the
4067 * condition applies to multiple channels (i.e. don't assume
4068 * that all channels matching my_chann* have the same size...)
4070 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4071 (double) buffer_capacity
);
4074 condition_type
= lttng_condition_get_type(condition
);
4075 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4076 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4077 threshold
, sample
->highest_usage
);
4080 * The low condition should only be triggered once _all_ of the
4081 * streams in a channel have gone below the "low" threshold.
4083 if (sample
->highest_usage
<= threshold
) {
4087 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4088 threshold
, sample
->highest_usage
);
4091 * For high buffer usage scenarios, we want to trigger whenever
4092 * _any_ of the streams has reached the "high" threshold.
4094 if (sample
->highest_usage
>= threshold
) {
4103 bool evaluate_session_consumed_size_condition(
4104 const struct lttng_condition
*condition
,
4105 uint64_t session_consumed_size
)
4108 const struct lttng_condition_session_consumed_size
*size_condition
=
4109 container_of(condition
,
4110 struct lttng_condition_session_consumed_size
,
4113 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4114 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4115 threshold
, session_consumed_size
);
4116 return session_consumed_size
>= threshold
;
4120 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4121 struct lttng_evaluation
**evaluation
,
4122 const struct notification_thread_state
*state
,
4123 const struct channel_state_sample
*previous_sample
,
4124 const struct channel_state_sample
*latest_sample
,
4125 uint64_t previous_session_consumed_total
,
4126 uint64_t latest_session_consumed_total
,
4127 struct channel_info
*channel_info
)
4130 enum lttng_condition_type condition_type
;
4131 const bool previous_sample_available
= !!previous_sample
;
4132 bool previous_sample_result
= false;
4133 bool latest_sample_result
;
4135 condition_type
= lttng_condition_get_type(condition
);
4137 switch (condition_type
) {
4138 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4139 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4140 if (caa_likely(previous_sample_available
)) {
4141 previous_sample_result
=
4142 evaluate_buffer_usage_condition(condition
,
4143 previous_sample
, channel_info
->capacity
);
4145 latest_sample_result
= evaluate_buffer_usage_condition(
4146 condition
, latest_sample
,
4147 channel_info
->capacity
);
4149 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4150 if (caa_likely(previous_sample_available
)) {
4151 previous_sample_result
=
4152 evaluate_session_consumed_size_condition(
4154 previous_session_consumed_total
);
4156 latest_sample_result
=
4157 evaluate_session_consumed_size_condition(
4159 latest_session_consumed_total
);
4162 /* Unknown condition type; internal error. */
4166 if (!latest_sample_result
||
4167 (previous_sample_result
== latest_sample_result
)) {
4169 * Only trigger on a condition evaluation transition.
4171 * NOTE: This edge-triggered logic may not be appropriate for
4172 * future condition types.
4177 if (!evaluation
|| !latest_sample_result
) {
4181 switch (condition_type
) {
4182 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4183 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4184 *evaluation
= lttng_evaluation_buffer_usage_create(
4186 latest_sample
->highest_usage
,
4187 channel_info
->capacity
);
4189 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4190 *evaluation
= lttng_evaluation_session_consumed_size_create(
4191 latest_session_consumed_total
);
4206 int client_notification_overflow(struct notification_client
*client
)
4209 const struct lttng_notification_channel_message msg
= {
4210 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4213 ASSERT_LOCKED(client
->lock
);
4215 DBG("Dropping notification addressed to client (socket fd = %i)",
4217 if (client
->communication
.outbound
.dropped_notification
) {
4219 * The client already has a "notification dropped" message
4220 * in its outgoing queue. Nothing to do since all
4221 * of those messages are coalesced.
4226 client
->communication
.outbound
.dropped_notification
= true;
4227 ret
= lttng_dynamic_buffer_append(
4228 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4231 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4238 static int client_handle_transmission_status_wrapper(
4239 struct notification_client
*client
,
4240 enum client_transmission_status status
,
4243 return client_handle_transmission_status(client
, status
,
4244 (struct notification_thread_state
*) user_data
);
4248 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4249 const struct lttng_evaluation
*evaluation
,
4250 struct notification_client_list
* client_list
,
4251 struct notification_thread_state
*state
,
4252 uid_t object_uid
, gid_t object_gid
)
4254 const struct lttng_credentials creds
= {
4255 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4256 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4259 return notification_client_list_send_evaluation(client_list
,
4260 trigger
, evaluation
,
4262 client_handle_transmission_status_wrapper
, state
);
4266 * Permission checks relative to notification channel clients are performed
4267 * here. Notice how object, client, and trigger credentials are involved in
4270 * The `object` credentials are the credentials associated with the "subject"
4271 * of a condition. For instance, a `rotation completed` condition applies
4272 * to a session. When that condition is met, it will produce an evaluation
4273 * against a session. Hence, in this case, the `object` credentials are the
4274 * credentials of the "subject" session.
4276 * The `trigger` credentials are the credentials of the user that registered the
4279 * The `client` credentials are the credentials of the user that created a given
4280 * notification channel.
4282 * In terms of visibility, it is expected that non-privilieged users can only
4283 * register triggers against "their" objects (their own sessions and
4284 * applications they are allowed to interact with). They can then open a
4285 * notification channel and subscribe to notifications associated with those
4288 * As for privilieged users, they can register triggers against the objects of
4289 * other users. They can then subscribe to the notifications associated to their
4290 * triggers. Privilieged users _can't_ subscribe to the notifications of
4291 * triggers owned by other users; they must create their own triggers.
4293 * This is more a concern of usability than security. It would be difficult for
4294 * a root user reliably subscribe to a specific set of conditions without
4295 * interference from external users (those could, for instance, unregister
4299 int notification_client_list_send_evaluation(
4300 struct notification_client_list
*client_list
,
4301 const struct lttng_trigger
*trigger
,
4302 const struct lttng_evaluation
*evaluation
,
4303 const struct lttng_credentials
*source_object_creds
,
4304 report_client_transmission_result_cb client_report
,
4308 struct lttng_payload msg_payload
;
4309 struct notification_client_list_element
*client_list_element
, *tmp
;
4310 const struct lttng_notification notification
= {
4311 .trigger
= (struct lttng_trigger
*) trigger
,
4312 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4314 struct lttng_notification_channel_message msg_header
= {
4315 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4317 const struct lttng_credentials
*trigger_creds
=
4318 lttng_trigger_get_credentials(trigger
);
4320 lttng_payload_init(&msg_payload
);
4322 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4323 sizeof(msg_header
));
4328 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4330 ERR("[notification-thread] Failed to serialize notification");
4335 /* Update payload size. */
4336 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4337 ->size
= (uint32_t)(
4338 msg_payload
.buffer
.size
- sizeof(msg_header
));
4340 /* Update the payload number of fds. */
4342 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4343 &msg_payload
, 0, -1);
4345 ((struct lttng_notification_channel_message
*)
4346 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4347 lttng_payload_view_get_fd_handle_count(&pv
);
4350 pthread_mutex_lock(&client_list
->lock
);
4351 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4352 &client_list
->clients_list
, node
) {
4353 enum client_transmission_status transmission_status
;
4354 struct notification_client
*client
=
4355 client_list_element
->client
;
4358 pthread_mutex_lock(&client
->lock
);
4359 if (!client
->communication
.active
) {
4361 * Skip inactive client (protocol error or
4364 DBG("Skipping client at it is marked as inactive");
4368 if (source_object_creds
) {
4369 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4370 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4373 * Client is not allowed to monitor this
4376 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4381 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
)) {
4382 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4386 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4387 client
->socket
, msg_payload
.buffer
.size
);
4389 if (client_has_outbound_data_left(client
)) {
4391 * Outgoing data is already buffered for this client;
4392 * drop the notification and enqueue a "dropped
4393 * notification" message if this is the first dropped
4394 * notification since the socket spilled-over to the
4397 ret
= client_notification_overflow(client
);
4404 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4410 transmission_status
= client_flush_outgoing_queue(client
);
4411 pthread_mutex_unlock(&client
->lock
);
4412 ret
= client_report(client
, transmission_status
, user_data
);
4415 goto end_unlock_list
;
4421 pthread_mutex_unlock(&client
->lock
);
4424 goto end_unlock_list
;
4430 pthread_mutex_unlock(&client_list
->lock
);
4432 lttng_payload_reset(&msg_payload
);
4437 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4438 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4442 struct lttng_event_notifier_notification
*notification
= NULL
;
4443 char *capture_buffer
= NULL
;
4444 size_t capture_buffer_size
;
4445 void *reception_buffer
;
4446 size_t reception_size
;
4448 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4449 struct lttng_kernel_event_notifier_notification kernel_notification
;
4451 /* Init lttng_event_notifier_notification */
4453 case LTTNG_DOMAIN_UST
:
4454 reception_buffer
= (void *) &ust_notification
;
4455 reception_size
= sizeof(ust_notification
);
4457 case LTTNG_DOMAIN_KERNEL
:
4458 reception_buffer
= (void *) &kernel_notification
;
4459 reception_size
= sizeof(kernel_notification
);
4466 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4467 * ensuring that read/write of tracer notifications are atomic.
4469 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4471 if (ret
!= reception_size
) {
4472 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4473 notification_pipe_read_fd
, reception_size
, ret
);
4479 case LTTNG_DOMAIN_UST
:
4480 token
= ust_notification
.token
;
4481 capture_buffer_size
= ust_notification
.capture_buf_size
;
4483 case LTTNG_DOMAIN_KERNEL
:
4484 token
= kernel_notification
.token
;
4485 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4491 if (capture_buffer_size
== 0) {
4492 capture_buffer
= NULL
;
4496 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4497 ERR("[notification-thread] Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4498 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4502 capture_buffer
= zmalloc(capture_buffer_size
);
4503 if (!capture_buffer
) {
4504 ERR("[notification-thread] Failed to allocate capture buffer");
4508 /* Fetch additional payload (capture). */
4509 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4510 if (ret
!= capture_buffer_size
) {
4511 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4512 notification_pipe_read_fd
);
4517 notification
= lttng_event_notifier_notification_create(token
, domain
,
4518 capture_buffer
, capture_buffer_size
);
4519 if (notification
== NULL
) {
4524 * Ownership transfered to the lttng_event_notifier_notification object.
4526 capture_buffer
= NULL
;
4529 free(capture_buffer
);
4530 return notification
;
4534 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4535 struct lttng_event_notifier_notification
*notification
)
4537 struct cds_lfht_node
*node
;
4538 struct cds_lfht_iter iter
;
4539 struct notification_trigger_tokens_ht_element
*element
;
4540 enum lttng_trigger_status trigger_status
;
4541 struct lttng_evaluation
*evaluation
= NULL
;
4542 enum action_executor_status executor_status
;
4543 struct notification_client_list
*client_list
= NULL
;
4544 const char *trigger_name
;
4546 unsigned int capture_count
= 0;
4548 /* Find triggers associated with this token. */
4550 cds_lfht_lookup(state
->trigger_tokens_ht
,
4551 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4552 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4553 node
= cds_lfht_iter_get_node(&iter
);
4554 if (caa_unlikely(!node
)) {
4556 * This is not an error, slow consumption of the tracer
4557 * notifications can lead to situations where a trigger is
4558 * removed but we still get tracer notifications matching a
4559 * trigger that no longer exists.
4565 element
= caa_container_of(node
,
4566 struct notification_trigger_tokens_ht_element
,
4569 trigger_status
= lttng_trigger_get_name(element
->trigger
, &trigger_name
);
4570 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4572 if (lttng_condition_event_rule_matches_get_capture_descriptor_count(
4573 lttng_trigger_get_const_condition(element
->trigger
),
4574 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4575 ERR("Failed to get capture count");
4580 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4581 ERR("Expected capture but capture buffer is null");
4586 evaluation
= lttng_evaluation_event_rule_matches_create(
4587 container_of(lttng_trigger_get_const_condition(
4589 struct lttng_condition_event_rule_matches
,
4591 notification
->capture_buffer
,
4592 notification
->capture_buf_size
, false);
4594 if (evaluation
== NULL
) {
4595 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
4599 client_list
= get_client_list_from_condition(state
,
4600 lttng_trigger_get_const_condition(element
->trigger
));
4601 executor_status
= action_executor_enqueue_trigger(state
->executor
,
4602 element
->trigger
, evaluation
, NULL
, client_list
);
4603 switch (executor_status
) {
4604 case ACTION_EXECUTOR_STATUS_OK
:
4607 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4609 struct notification_client_list_element
*client_list_element
,
4613 * Not a fatal error; this is expected and simply means the
4614 * executor has too much work queued already.
4618 /* No clients subscribed to notifications for this trigger. */
4623 /* Warn clients that a notification (or more) was dropped. */
4624 pthread_mutex_lock(&client_list
->lock
);
4625 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4626 &client_list
->clients_list
, node
) {
4627 enum client_transmission_status transmission_status
;
4628 struct notification_client
*client
=
4629 client_list_element
->client
;
4631 pthread_mutex_lock(&client
->lock
);
4632 ret
= client_notification_overflow(client
);
4638 transmission_status
=
4639 client_flush_outgoing_queue(client
);
4640 ret
= client_handle_transmission_status(
4641 client
, transmission_status
, state
);
4647 pthread_mutex_unlock(&client
->lock
);
4653 pthread_mutex_unlock(&client_list
->lock
);
4656 case ACTION_EXECUTOR_STATUS_INVALID
:
4657 case ACTION_EXECUTOR_STATUS_ERROR
:
4658 /* Fatal error, shut down everything. */
4659 ERR("Fatal error encoutered while enqueuing action to the action executor");
4663 /* Unhandled error. */
4668 notification_client_list_put(client_list
);
4675 int handle_one_event_notifier_notification(
4676 struct notification_thread_state
*state
,
4677 int pipe
, enum lttng_domain_type domain
)
4680 struct lttng_event_notifier_notification
*notification
= NULL
;
4682 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4683 if (notification
== NULL
) {
4684 /* Reception failed, don't consider it fatal. */
4685 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4686 pipe
, lttng_domain_type_str(domain
));
4690 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4692 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4693 pipe
, lttng_domain_type_str(domain
));
4698 lttng_event_notifier_notification_destroy(notification
);
4702 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4703 int pipe
, enum lttng_domain_type domain
)
4705 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4708 int handle_notification_thread_channel_sample(
4709 struct notification_thread_state
*state
, int pipe
,
4710 enum lttng_domain_type domain
)
4713 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4714 struct channel_info
*channel_info
;
4715 struct cds_lfht_node
*node
;
4716 struct cds_lfht_iter iter
;
4717 struct lttng_channel_trigger_list
*trigger_list
;
4718 struct lttng_trigger_list_element
*trigger_list_element
;
4719 bool previous_sample_available
= false;
4720 struct channel_state_sample previous_sample
, latest_sample
;
4721 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4722 struct lttng_credentials channel_creds
;
4725 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4726 * ensuring that read/write of sampling messages are atomic.
4728 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4729 if (ret
!= sizeof(sample_msg
)) {
4730 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4737 latest_sample
.key
.key
= sample_msg
.key
;
4738 latest_sample
.key
.domain
= domain
;
4739 latest_sample
.highest_usage
= sample_msg
.highest
;
4740 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4741 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4745 /* Retrieve the channel's informations */
4746 cds_lfht_lookup(state
->channels_ht
,
4747 hash_channel_key(&latest_sample
.key
),
4751 node
= cds_lfht_iter_get_node(&iter
);
4752 if (caa_unlikely(!node
)) {
4754 * Not an error since the consumer can push a sample to the pipe
4755 * and the rest of the session daemon could notify us of the
4756 * channel's destruction before we get a chance to process that
4759 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4760 latest_sample
.key
.key
,
4761 lttng_domain_type_str(domain
));
4764 channel_info
= caa_container_of(node
, struct channel_info
,
4766 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4768 latest_sample
.key
.key
,
4769 channel_info
->session_info
->name
,
4770 latest_sample
.highest_usage
,
4771 latest_sample
.lowest_usage
,
4772 latest_sample
.channel_total_consumed
);
4774 previous_session_consumed_total
=
4775 channel_info
->session_info
->consumed_data_size
;
4777 /* Retrieve the channel's last sample, if it exists, and update it. */
4778 cds_lfht_lookup(state
->channel_state_ht
,
4779 hash_channel_key(&latest_sample
.key
),
4780 match_channel_state_sample
,
4783 node
= cds_lfht_iter_get_node(&iter
);
4784 if (caa_likely(node
)) {
4785 struct channel_state_sample
*stored_sample
;
4787 /* Update the sample stored. */
4788 stored_sample
= caa_container_of(node
,
4789 struct channel_state_sample
,
4790 channel_state_ht_node
);
4792 memcpy(&previous_sample
, stored_sample
,
4793 sizeof(previous_sample
));
4794 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4795 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4796 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4797 previous_sample_available
= true;
4799 latest_session_consumed_total
=
4800 previous_session_consumed_total
+
4801 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4804 * This is the channel's first sample, allocate space for and
4805 * store the new sample.
4807 struct channel_state_sample
*stored_sample
;
4809 stored_sample
= zmalloc(sizeof(*stored_sample
));
4810 if (!stored_sample
) {
4815 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4816 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4817 cds_lfht_add(state
->channel_state_ht
,
4818 hash_channel_key(&stored_sample
->key
),
4819 &stored_sample
->channel_state_ht_node
);
4821 latest_session_consumed_total
=
4822 previous_session_consumed_total
+
4823 latest_sample
.channel_total_consumed
;
4826 channel_info
->session_info
->consumed_data_size
=
4827 latest_session_consumed_total
;
4829 /* Find triggers associated with this channel. */
4830 cds_lfht_lookup(state
->channel_triggers_ht
,
4831 hash_channel_key(&latest_sample
.key
),
4832 match_channel_trigger_list
,
4835 node
= cds_lfht_iter_get_node(&iter
);
4836 if (caa_likely(!node
)) {
4840 channel_creds
= (typeof(channel_creds
)) {
4841 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4842 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4845 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4846 channel_triggers_ht_node
);
4847 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4849 const struct lttng_condition
*condition
;
4850 struct lttng_trigger
*trigger
;
4851 struct notification_client_list
*client_list
= NULL
;
4852 struct lttng_evaluation
*evaluation
= NULL
;
4853 enum action_executor_status executor_status
;
4856 trigger
= trigger_list_element
->trigger
;
4857 condition
= lttng_trigger_get_const_condition(trigger
);
4861 * Check if any client is subscribed to the result of this
4864 client_list
= get_client_list_from_condition(state
, condition
);
4866 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4867 previous_sample_available
? &previous_sample
: NULL
,
4869 previous_session_consumed_total
,
4870 latest_session_consumed_total
,
4872 if (caa_unlikely(ret
)) {
4876 if (caa_likely(!evaluation
)) {
4881 * Ownership of `evaluation` transferred to the action executor
4882 * no matter the result.
4884 executor_status
= action_executor_enqueue_trigger(
4885 state
->executor
, trigger
, evaluation
,
4886 &channel_creds
, client_list
);
4888 switch (executor_status
) {
4889 case ACTION_EXECUTOR_STATUS_OK
:
4891 case ACTION_EXECUTOR_STATUS_ERROR
:
4892 case ACTION_EXECUTOR_STATUS_INVALID
:
4894 * TODO Add trigger identification (name/id) when
4895 * it is added to the API.
4897 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4900 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4902 * TODO Add trigger identification (name/id) when
4903 * it is added to the API.
4905 * Not a fatal error.
4907 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4915 notification_client_list_put(client_list
);
4916 if (caa_unlikely(ret
)) {