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/list-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
,
376 const char *element_trigger_name
;
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
,
389 &element_trigger_name
);
390 element_trigger_name
= status
== LTTNG_TRIGGER_STATUS_OK
?
391 element_trigger_name
: NULL
;
393 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
394 key_name
= status
== LTTNG_TRIGGER_STATUS_OK
? key_name
: NULL
;
398 * Consider null names as not equal. This is to maintain backwards
399 * compatibility with pre-2.13 anonymous triggers. Multiples anonymous
400 * triggers are allowed for a given user.
402 if (!element_trigger_name
|| !key_name
) {
406 if (strcmp(element_trigger_name
, key_name
) != 0) {
410 /* Compare the owners' UIDs. */
411 key_creds
= lttng_trigger_get_credentials(trigger_key
);
412 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
414 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
421 * Hash trigger based on name and credentials only.
424 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
426 unsigned long hash
= 0;
427 const struct lttng_credentials
*trigger_creds
;
428 const char *trigger_name
;
429 enum lttng_trigger_status status
;
431 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
432 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
433 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
436 trigger_creds
= lttng_trigger_get_credentials(trigger
);
437 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
444 unsigned long hash_channel_key(struct channel_key
*key
)
446 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
447 unsigned long domain_hash
= hash_key_ulong(
448 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
450 return key_hash
^ domain_hash
;
454 unsigned long hash_client_socket(int socket
)
456 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
460 unsigned long hash_client_id(notification_client_id id
)
462 return hash_key_u64(&id
, lttng_ht_seed
);
466 * Get the type of object to which a given condition applies. Bindings let
467 * the notification system evaluate a trigger's condition when a given
468 * object's state is updated.
470 * For instance, a condition bound to a channel will be evaluated everytime
471 * the channel's state is changed by a channel monitoring sample.
474 enum lttng_object_type
get_condition_binding_object(
475 const struct lttng_condition
*condition
)
477 switch (lttng_condition_get_type(condition
)) {
478 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
479 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
480 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
481 return LTTNG_OBJECT_TYPE_CHANNEL
;
482 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
483 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
484 return LTTNG_OBJECT_TYPE_SESSION
;
485 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
486 return LTTNG_OBJECT_TYPE_NONE
;
488 return LTTNG_OBJECT_TYPE_UNKNOWN
;
493 void free_channel_info_rcu(struct rcu_head
*node
)
495 free(caa_container_of(node
, struct channel_info
, rcu_node
));
499 void channel_info_destroy(struct channel_info
*channel_info
)
505 if (channel_info
->session_info
) {
506 session_info_remove_channel(channel_info
->session_info
,
508 session_info_put(channel_info
->session_info
);
510 if (channel_info
->name
) {
511 free(channel_info
->name
);
513 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
517 void free_session_info_rcu(struct rcu_head
*node
)
519 free(caa_container_of(node
, struct session_info
, rcu_node
));
522 /* Don't call directly, use the ref-counting mechanism. */
524 void session_info_destroy(void *_data
)
526 struct session_info
*session_info
= _data
;
529 assert(session_info
);
530 if (session_info
->channel_infos_ht
) {
531 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
533 ERR("[notification-thread] Failed to destroy channel information hash table");
536 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
539 cds_lfht_del(session_info
->sessions_ht
,
540 &session_info
->sessions_ht_node
);
542 free(session_info
->name
);
543 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
547 void session_info_get(struct session_info
*session_info
)
552 lttng_ref_get(&session_info
->ref
);
556 void session_info_put(struct session_info
*session_info
)
561 lttng_ref_put(&session_info
->ref
);
565 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
566 struct lttng_session_trigger_list
*trigger_list
,
567 struct cds_lfht
*sessions_ht
)
569 struct session_info
*session_info
;
573 session_info
= zmalloc(sizeof(*session_info
));
577 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
579 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
580 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
581 if (!session_info
->channel_infos_ht
) {
585 cds_lfht_node_init(&session_info
->sessions_ht_node
);
586 session_info
->name
= strdup(name
);
587 if (!session_info
->name
) {
590 session_info
->uid
= uid
;
591 session_info
->gid
= gid
;
592 session_info
->trigger_list
= trigger_list
;
593 session_info
->sessions_ht
= sessions_ht
;
597 session_info_put(session_info
);
602 void session_info_add_channel(struct session_info
*session_info
,
603 struct channel_info
*channel_info
)
606 cds_lfht_add(session_info
->channel_infos_ht
,
607 hash_channel_key(&channel_info
->key
),
608 &channel_info
->session_info_channels_ht_node
);
613 void session_info_remove_channel(struct session_info
*session_info
,
614 struct channel_info
*channel_info
)
617 cds_lfht_del(session_info
->channel_infos_ht
,
618 &channel_info
->session_info_channels_ht_node
);
623 struct channel_info
*channel_info_create(const char *channel_name
,
624 struct channel_key
*channel_key
, uint64_t channel_capacity
,
625 struct session_info
*session_info
)
627 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
633 cds_lfht_node_init(&channel_info
->channels_ht_node
);
634 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
635 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
636 channel_info
->capacity
= channel_capacity
;
638 channel_info
->name
= strdup(channel_name
);
639 if (!channel_info
->name
) {
644 * Set the references between session and channel infos:
645 * - channel_info holds a strong reference to session_info
646 * - session_info holds a weak reference to channel_info
648 session_info_get(session_info
);
649 session_info_add_channel(session_info
, channel_info
);
650 channel_info
->session_info
= session_info
;
654 channel_info_destroy(channel_info
);
659 bool notification_client_list_get(struct notification_client_list
*list
)
661 return urcu_ref_get_unless_zero(&list
->ref
);
665 void free_notification_client_list_rcu(struct rcu_head
*node
)
667 free(caa_container_of(node
, struct notification_client_list
,
672 void notification_client_list_release(struct urcu_ref
*list_ref
)
674 struct notification_client_list
*list
=
675 container_of(list_ref
, typeof(*list
), ref
);
676 struct notification_client_list_element
*client_list_element
, *tmp
;
678 lttng_condition_put(list
->condition
);
680 if (list
->notification_trigger_clients_ht
) {
683 cds_lfht_del(list
->notification_trigger_clients_ht
,
684 &list
->notification_trigger_clients_ht_node
);
686 list
->notification_trigger_clients_ht
= NULL
;
688 cds_list_for_each_entry_safe(client_list_element
, tmp
,
689 &list
->clients_list
, node
) {
690 free(client_list_element
);
693 assert(cds_list_empty(&list
->triggers_list
));
695 pthread_mutex_destroy(&list
->lock
);
696 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
700 bool condition_applies_to_client(const struct lttng_condition
*condition
,
701 struct notification_client
*client
)
703 bool applies
= false;
704 struct lttng_condition_list_element
*condition_list_element
;
706 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
708 applies
= lttng_condition_is_equal(
709 condition_list_element
->condition
,
720 struct notification_client_list
*notification_client_list_create(
721 struct notification_thread_state
*state
,
722 const struct lttng_condition
*condition
)
724 struct notification_client
*client
;
725 struct cds_lfht_iter iter
;
726 struct notification_client_list
*client_list
;
728 client_list
= zmalloc(sizeof(*client_list
));
730 PERROR("Failed to allocate notification client list");
734 pthread_mutex_init(&client_list
->lock
, NULL
);
736 * The trigger that owns the condition has the first reference to this
739 urcu_ref_init(&client_list
->ref
);
740 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
741 CDS_INIT_LIST_HEAD(&client_list
->clients_list
);
742 CDS_INIT_LIST_HEAD(&client_list
->triggers_list
);
745 * Create a copy of the condition so that it's independent of any
746 * trigger. The client list may outlive the trigger object (which owns
747 * the condition) that is used to create it.
749 client_list
->condition
= lttng_condition_copy(condition
);
751 /* Build a list of clients to which this new condition applies. */
752 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
753 client_socket_ht_node
) {
754 struct notification_client_list_element
*client_list_element
;
756 if (!condition_applies_to_client(condition
, client
)) {
760 client_list_element
= zmalloc(sizeof(*client_list_element
));
761 if (!client_list_element
) {
762 goto error_put_client_list
;
765 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
766 client_list_element
->client
= client
;
767 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
770 client_list
->notification_trigger_clients_ht
=
771 state
->notification_trigger_clients_ht
;
775 * Add the client list to the global list of client list.
777 cds_lfht_add_unique(state
->notification_trigger_clients_ht
,
778 lttng_condition_hash(client_list
->condition
),
779 match_client_list_condition
,
780 client_list
->condition
,
781 &client_list
->notification_trigger_clients_ht_node
);
785 error_put_client_list
:
786 notification_client_list_put(client_list
);
793 void notification_client_list_put(struct notification_client_list
*list
)
798 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
801 /* Provides a reference to the returned list. */
803 struct notification_client_list
*get_client_list_from_condition(
804 struct notification_thread_state
*state
,
805 const struct lttng_condition
*condition
)
807 struct cds_lfht_node
*node
;
808 struct cds_lfht_iter iter
;
809 struct notification_client_list
*list
= NULL
;
812 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
813 lttng_condition_hash(condition
),
814 match_client_list_condition
,
817 node
= cds_lfht_iter_get_node(&iter
);
819 list
= container_of(node
, struct notification_client_list
,
820 notification_trigger_clients_ht_node
);
821 list
= notification_client_list_get(list
) ? list
: NULL
;
829 int evaluate_channel_condition_for_client(
830 const struct lttng_condition
*condition
,
831 struct notification_thread_state
*state
,
832 struct lttng_evaluation
**evaluation
,
833 uid_t
*session_uid
, gid_t
*session_gid
)
836 struct cds_lfht_iter iter
;
837 struct cds_lfht_node
*node
;
838 struct channel_info
*channel_info
= NULL
;
839 struct channel_key
*channel_key
= NULL
;
840 struct channel_state_sample
*last_sample
= NULL
;
841 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
845 /* Find the channel associated with the condition. */
846 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
847 channel_trigger_list
, channel_triggers_ht_node
) {
848 struct lttng_trigger_list_element
*element
;
850 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
851 const struct lttng_condition
*current_condition
=
852 lttng_trigger_get_const_condition(
855 assert(current_condition
);
856 if (!lttng_condition_is_equal(condition
,
857 current_condition
)) {
861 /* Found the trigger, save the channel key. */
862 channel_key
= &channel_trigger_list
->channel_key
;
866 /* The channel key was found stop iteration. */
872 /* No channel found; normal exit. */
873 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
878 /* Fetch channel info for the matching channel. */
879 cds_lfht_lookup(state
->channels_ht
,
880 hash_channel_key(channel_key
),
884 node
= cds_lfht_iter_get_node(&iter
);
886 channel_info
= caa_container_of(node
, struct channel_info
,
889 /* Retrieve the channel's last sample, if it exists. */
890 cds_lfht_lookup(state
->channel_state_ht
,
891 hash_channel_key(channel_key
),
892 match_channel_state_sample
,
895 node
= cds_lfht_iter_get_node(&iter
);
897 last_sample
= caa_container_of(node
,
898 struct channel_state_sample
,
899 channel_state_ht_node
);
901 /* Nothing to evaluate, no sample was ever taken. Normal exit */
902 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
907 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
909 0, channel_info
->session_info
->consumed_data_size
,
912 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
916 *session_uid
= channel_info
->session_info
->uid
;
917 *session_gid
= channel_info
->session_info
->gid
;
924 const char *get_condition_session_name(const struct lttng_condition
*condition
)
926 const char *session_name
= NULL
;
927 enum lttng_condition_status status
;
929 switch (lttng_condition_get_type(condition
)) {
930 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
931 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
932 status
= lttng_condition_buffer_usage_get_session_name(
933 condition
, &session_name
);
935 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
936 status
= lttng_condition_session_consumed_size_get_session_name(
937 condition
, &session_name
);
939 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
940 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
941 status
= lttng_condition_session_rotation_get_session_name(
942 condition
, &session_name
);
947 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
948 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
956 int evaluate_session_condition_for_client(
957 const struct lttng_condition
*condition
,
958 struct notification_thread_state
*state
,
959 struct lttng_evaluation
**evaluation
,
960 uid_t
*session_uid
, gid_t
*session_gid
)
963 struct cds_lfht_iter iter
;
964 struct cds_lfht_node
*node
;
965 const char *session_name
;
966 struct session_info
*session_info
= NULL
;
969 session_name
= get_condition_session_name(condition
);
971 /* Find the session associated with the trigger. */
972 cds_lfht_lookup(state
->sessions_ht
,
973 hash_key_str(session_name
, lttng_ht_seed
),
977 node
= cds_lfht_iter_get_node(&iter
);
979 DBG("[notification-thread] No known session matching name \"%s\"",
985 session_info
= caa_container_of(node
, struct session_info
,
987 session_info_get(session_info
);
990 * Evaluation is performed in-line here since only one type of
991 * session-bound condition is handled for the moment.
993 switch (lttng_condition_get_type(condition
)) {
994 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
995 if (!session_info
->rotation
.ongoing
) {
997 goto end_session_put
;
1000 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1001 session_info
->rotation
.id
);
1004 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
1005 session_info
->name
);
1007 goto end_session_put
;
1013 goto end_session_put
;
1016 *session_uid
= session_info
->uid
;
1017 *session_gid
= session_info
->gid
;
1020 session_info_put(session_info
);
1027 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1028 const struct lttng_condition
*condition
,
1029 struct notification_client
*client
,
1030 struct notification_thread_state
*state
)
1033 struct lttng_evaluation
*evaluation
= NULL
;
1034 struct notification_client_list client_list
= {
1035 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1037 struct notification_client_list_element client_list_element
= { 0 };
1038 uid_t object_uid
= 0;
1039 gid_t object_gid
= 0;
1046 switch (get_condition_binding_object(condition
)) {
1047 case LTTNG_OBJECT_TYPE_SESSION
:
1048 ret
= evaluate_session_condition_for_client(condition
, state
,
1049 &evaluation
, &object_uid
, &object_gid
);
1051 case LTTNG_OBJECT_TYPE_CHANNEL
:
1052 ret
= evaluate_channel_condition_for_client(condition
, state
,
1053 &evaluation
, &object_uid
, &object_gid
);
1055 case LTTNG_OBJECT_TYPE_NONE
:
1056 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
1059 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1069 /* Evaluation yielded nothing. Normal exit. */
1070 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
1076 * Create a temporary client list with the client currently
1079 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1080 CDS_INIT_LIST_HEAD(&client_list
.clients_list
);
1082 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1083 client_list_element
.client
= client
;
1084 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1086 /* Send evaluation result to the newly-subscribed client. */
1087 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1088 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1089 state
, object_uid
, object_gid
);
1096 int notification_thread_client_subscribe(struct notification_client
*client
,
1097 struct lttng_condition
*condition
,
1098 struct notification_thread_state
*state
,
1099 enum lttng_notification_channel_status
*_status
)
1102 struct notification_client_list
*client_list
= NULL
;
1103 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1104 struct notification_client_list_element
*client_list_element
= NULL
;
1105 struct lttng_trigger_ht_element
*trigger_ht_element
;
1106 enum lttng_notification_channel_status status
=
1107 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1110 * Ensure that the client has not already subscribed to this condition
1113 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1114 if (lttng_condition_is_equal(condition_list_element
->condition
,
1116 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1121 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1122 if (!condition_list_element
) {
1126 client_list_element
= zmalloc(sizeof(*client_list_element
));
1127 if (!client_list_element
) {
1133 * Add the newly-subscribed condition to the client's subscription list.
1135 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1136 condition_list_element
->condition
= condition
;
1137 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1139 client_list
= get_client_list_from_condition(state
, condition
);
1142 * No notification-emiting trigger registered with this
1143 * condition. We don't evaluate the condition right away
1144 * since this trigger is not registered yet.
1146 free(client_list_element
);
1151 * The condition to which the client just subscribed is evaluated
1152 * at this point so that conditions that are already TRUE result
1153 * in a notification being sent out.
1155 * Note the iteration on all triggers which share an identical
1156 * `condition` than the one to which the client is registering. This is
1157 * done to ensure that the client receives a distinct notification for
1158 * all triggers that have a `notify` action that have this condition.
1160 pthread_mutex_lock(&client_list
->lock
);
1161 cds_list_for_each_entry(trigger_ht_element
,
1162 &client_list
->triggers_list
, client_list_trigger_node
) {
1163 if (evaluate_condition_for_client(trigger_ht_element
->trigger
, condition
,
1165 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1167 free(client_list_element
);
1168 pthread_mutex_unlock(&client_list
->lock
);
1172 pthread_mutex_unlock(&client_list
->lock
);
1175 * Add the client to the list of clients interested in a given trigger
1176 * if a "notification" trigger with a corresponding condition was
1179 client_list_element
->client
= client
;
1180 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1182 pthread_mutex_lock(&client_list
->lock
);
1183 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1184 pthread_mutex_unlock(&client_list
->lock
);
1190 notification_client_list_put(client_list
);
1194 free(condition_list_element
);
1195 free(client_list_element
);
1200 int notification_thread_client_unsubscribe(
1201 struct notification_client
*client
,
1202 struct lttng_condition
*condition
,
1203 struct notification_thread_state
*state
,
1204 enum lttng_notification_channel_status
*_status
)
1206 struct notification_client_list
*client_list
;
1207 struct lttng_condition_list_element
*condition_list_element
,
1209 struct notification_client_list_element
*client_list_element
,
1211 bool condition_found
= false;
1212 enum lttng_notification_channel_status status
=
1213 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1215 /* Remove the condition from the client's condition list. */
1216 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1217 &client
->condition_list
, node
) {
1218 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1223 cds_list_del(&condition_list_element
->node
);
1225 * The caller may be iterating on the client's conditions to
1226 * tear down a client's connection. In this case, the condition
1227 * will be destroyed at the end.
1229 if (condition
!= condition_list_element
->condition
) {
1230 lttng_condition_destroy(
1231 condition_list_element
->condition
);
1233 free(condition_list_element
);
1234 condition_found
= true;
1238 if (!condition_found
) {
1239 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1244 * Remove the client from the list of clients interested the trigger
1245 * matching the condition.
1247 client_list
= get_client_list_from_condition(state
, condition
);
1252 pthread_mutex_lock(&client_list
->lock
);
1253 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1254 &client_list
->clients_list
, node
) {
1255 if (client_list_element
->client
->id
!= client
->id
) {
1258 cds_list_del(&client_list_element
->node
);
1259 free(client_list_element
);
1262 pthread_mutex_unlock(&client_list
->lock
);
1263 notification_client_list_put(client_list
);
1266 lttng_condition_destroy(condition
);
1274 void free_notification_client_rcu(struct rcu_head
*node
)
1276 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1280 void notification_client_destroy(struct notification_client
*client
,
1281 struct notification_thread_state
*state
)
1288 * The client object is not reachable by other threads, no need to lock
1291 if (client
->socket
>= 0) {
1292 (void) lttcomm_close_unix_sock(client
->socket
);
1293 client
->socket
= -1;
1295 client
->communication
.active
= false;
1296 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1297 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1298 pthread_mutex_destroy(&client
->lock
);
1299 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1303 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1307 struct notification_client
*get_client_from_socket(int socket
,
1308 struct notification_thread_state
*state
)
1310 struct cds_lfht_iter iter
;
1311 struct cds_lfht_node
*node
;
1312 struct notification_client
*client
= NULL
;
1314 cds_lfht_lookup(state
->client_socket_ht
,
1315 hash_client_socket(socket
),
1316 match_client_socket
,
1317 (void *) (unsigned long) socket
,
1319 node
= cds_lfht_iter_get_node(&iter
);
1324 client
= caa_container_of(node
, struct notification_client
,
1325 client_socket_ht_node
);
1331 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1335 struct notification_client
*get_client_from_id(notification_client_id id
,
1336 struct notification_thread_state
*state
)
1338 struct cds_lfht_iter iter
;
1339 struct cds_lfht_node
*node
;
1340 struct notification_client
*client
= NULL
;
1342 cds_lfht_lookup(state
->client_id_ht
,
1347 node
= cds_lfht_iter_get_node(&iter
);
1352 client
= caa_container_of(node
, struct notification_client
,
1359 bool buffer_usage_condition_applies_to_channel(
1360 const struct lttng_condition
*condition
,
1361 const struct channel_info
*channel_info
)
1363 enum lttng_condition_status status
;
1364 enum lttng_domain_type condition_domain
;
1365 const char *condition_session_name
= NULL
;
1366 const char *condition_channel_name
= NULL
;
1368 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1370 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1371 if (channel_info
->key
.domain
!= condition_domain
) {
1375 status
= lttng_condition_buffer_usage_get_session_name(
1376 condition
, &condition_session_name
);
1377 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1379 status
= lttng_condition_buffer_usage_get_channel_name(
1380 condition
, &condition_channel_name
);
1381 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1383 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1386 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1396 bool session_consumed_size_condition_applies_to_channel(
1397 const struct lttng_condition
*condition
,
1398 const struct channel_info
*channel_info
)
1400 enum lttng_condition_status status
;
1401 const char *condition_session_name
= NULL
;
1403 status
= lttng_condition_session_consumed_size_get_session_name(
1404 condition
, &condition_session_name
);
1405 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1407 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1417 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1418 const struct channel_info
*channel_info
)
1420 const struct lttng_condition
*condition
;
1421 bool trigger_applies
;
1423 condition
= lttng_trigger_get_const_condition(trigger
);
1428 switch (lttng_condition_get_type(condition
)) {
1429 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1430 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1431 trigger_applies
= buffer_usage_condition_applies_to_channel(
1432 condition
, channel_info
);
1434 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1435 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1436 condition
, channel_info
);
1442 return trigger_applies
;
1447 /* Must be called with RCU read lock held. */
1449 struct lttng_session_trigger_list
*get_session_trigger_list(
1450 struct notification_thread_state
*state
,
1451 const char *session_name
)
1453 struct lttng_session_trigger_list
*list
= NULL
;
1454 struct cds_lfht_node
*node
;
1455 struct cds_lfht_iter iter
;
1457 cds_lfht_lookup(state
->session_triggers_ht
,
1458 hash_key_str(session_name
, lttng_ht_seed
),
1459 match_session_trigger_list
,
1462 node
= cds_lfht_iter_get_node(&iter
);
1465 * Not an error, the list of triggers applying to that session
1466 * will be initialized when the session is created.
1468 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1473 list
= caa_container_of(node
,
1474 struct lttng_session_trigger_list
,
1475 session_triggers_ht_node
);
1481 * Allocate an empty lttng_session_trigger_list for the session named
1484 * No ownership of 'session_name' is assumed by the session trigger list.
1485 * It is the caller's responsability to ensure the session name is alive
1486 * for as long as this list is.
1489 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1490 const char *session_name
,
1491 struct cds_lfht
*session_triggers_ht
)
1493 struct lttng_session_trigger_list
*list
;
1495 list
= zmalloc(sizeof(*list
));
1499 list
->session_name
= session_name
;
1500 CDS_INIT_LIST_HEAD(&list
->list
);
1501 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1502 list
->session_triggers_ht
= session_triggers_ht
;
1505 /* Publish the list through the session_triggers_ht. */
1506 cds_lfht_add(session_triggers_ht
,
1507 hash_key_str(session_name
, lttng_ht_seed
),
1508 &list
->session_triggers_ht_node
);
1515 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1517 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1522 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1524 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1526 /* Empty the list element by element, and then free the list itself. */
1527 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1528 &list
->list
, node
) {
1529 cds_list_del(&trigger_list_element
->node
);
1530 free(trigger_list_element
);
1533 /* Unpublish the list from the session_triggers_ht. */
1534 cds_lfht_del(list
->session_triggers_ht
,
1535 &list
->session_triggers_ht_node
);
1537 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1541 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1542 struct lttng_trigger
*trigger
)
1545 struct lttng_trigger_list_element
*new_element
=
1546 zmalloc(sizeof(*new_element
));
1552 CDS_INIT_LIST_HEAD(&new_element
->node
);
1553 new_element
->trigger
= trigger
;
1554 cds_list_add(&new_element
->node
, &list
->list
);
1560 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1561 const char *session_name
)
1563 bool applies
= false;
1564 const struct lttng_condition
*condition
;
1566 condition
= lttng_trigger_get_const_condition(trigger
);
1567 switch (lttng_condition_get_type(condition
)) {
1568 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1569 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1571 enum lttng_condition_status condition_status
;
1572 const char *condition_session_name
;
1574 condition_status
= lttng_condition_session_rotation_get_session_name(
1575 condition
, &condition_session_name
);
1576 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1577 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1581 assert(condition_session_name
);
1582 applies
= !strcmp(condition_session_name
, session_name
);
1593 * Allocate and initialize an lttng_session_trigger_list which contains
1594 * all triggers that apply to the session named 'session_name'.
1596 * No ownership of 'session_name' is assumed by the session trigger list.
1597 * It is the caller's responsability to ensure the session name is alive
1598 * for as long as this list is.
1601 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1602 const struct notification_thread_state
*state
,
1603 const char *session_name
)
1605 int trigger_count
= 0;
1606 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1607 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1608 struct cds_lfht_iter iter
;
1610 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1611 state
->session_triggers_ht
);
1613 /* Add all triggers applying to the session named 'session_name'. */
1614 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1618 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1623 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1624 trigger_ht_element
->trigger
);
1632 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1634 return session_trigger_list
;
1636 lttng_session_trigger_list_destroy(session_trigger_list
);
1641 struct session_info
*find_or_create_session_info(
1642 struct notification_thread_state
*state
,
1643 const char *name
, uid_t uid
, gid_t gid
)
1645 struct session_info
*session
= NULL
;
1646 struct cds_lfht_node
*node
;
1647 struct cds_lfht_iter iter
;
1648 struct lttng_session_trigger_list
*trigger_list
;
1651 cds_lfht_lookup(state
->sessions_ht
,
1652 hash_key_str(name
, lttng_ht_seed
),
1656 node
= cds_lfht_iter_get_node(&iter
);
1658 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1660 session
= caa_container_of(node
, struct session_info
,
1662 assert(session
->uid
== uid
);
1663 assert(session
->gid
== gid
);
1664 session_info_get(session
);
1668 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1669 if (!trigger_list
) {
1673 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1674 state
->sessions_ht
);
1676 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1678 lttng_session_trigger_list_destroy(trigger_list
);
1681 trigger_list
= NULL
;
1683 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1684 &session
->sessions_ht_node
);
1690 session_info_put(session
);
1695 int handle_notification_thread_command_add_channel(
1696 struct notification_thread_state
*state
,
1697 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1698 const char *channel_name
, enum lttng_domain_type channel_domain
,
1699 uint64_t channel_key_int
, uint64_t channel_capacity
,
1700 enum lttng_error_code
*cmd_result
)
1702 struct cds_list_head trigger_list
;
1703 struct channel_info
*new_channel_info
= NULL
;
1704 struct channel_key channel_key
= {
1705 .key
= channel_key_int
,
1706 .domain
= channel_domain
,
1708 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1709 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1710 int trigger_count
= 0;
1711 struct cds_lfht_iter iter
;
1712 struct session_info
*session_info
= NULL
;
1714 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1715 channel_name
, session_name
, channel_key_int
,
1716 lttng_domain_type_str(channel_domain
));
1718 CDS_INIT_LIST_HEAD(&trigger_list
);
1720 session_info
= find_or_create_session_info(state
, session_name
,
1721 session_uid
, session_gid
);
1722 if (!session_info
) {
1723 /* Allocation error or an internal error occurred. */
1727 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1728 channel_capacity
, session_info
);
1729 if (!new_channel_info
) {
1734 /* Build a list of all triggers applying to the new channel. */
1735 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1737 struct lttng_trigger_list_element
*new_element
;
1739 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1740 new_channel_info
)) {
1744 new_element
= zmalloc(sizeof(*new_element
));
1749 CDS_INIT_LIST_HEAD(&new_element
->node
);
1750 new_element
->trigger
= trigger_ht_element
->trigger
;
1751 cds_list_add(&new_element
->node
, &trigger_list
);
1756 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1758 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1759 if (!channel_trigger_list
) {
1762 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1763 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1764 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1765 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1768 /* Add channel to the channel_ht which owns the channel_infos. */
1769 cds_lfht_add(state
->channels_ht
,
1770 hash_channel_key(&new_channel_info
->key
),
1771 &new_channel_info
->channels_ht_node
);
1773 * Add the list of triggers associated with this channel to the
1774 * channel_triggers_ht.
1776 cds_lfht_add(state
->channel_triggers_ht
,
1777 hash_channel_key(&new_channel_info
->key
),
1778 &channel_trigger_list
->channel_triggers_ht_node
);
1780 session_info_put(session_info
);
1781 *cmd_result
= LTTNG_OK
;
1784 channel_info_destroy(new_channel_info
);
1785 session_info_put(session_info
);
1790 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1792 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1797 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1799 free(caa_container_of(node
, struct channel_state_sample
,
1804 int handle_notification_thread_command_remove_channel(
1805 struct notification_thread_state
*state
,
1806 uint64_t channel_key
, enum lttng_domain_type domain
,
1807 enum lttng_error_code
*cmd_result
)
1809 struct cds_lfht_node
*node
;
1810 struct cds_lfht_iter iter
;
1811 struct lttng_channel_trigger_list
*trigger_list
;
1812 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1813 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1814 struct channel_info
*channel_info
;
1816 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1817 channel_key
, lttng_domain_type_str(domain
));
1821 cds_lfht_lookup(state
->channel_triggers_ht
,
1822 hash_channel_key(&key
),
1823 match_channel_trigger_list
,
1826 node
= cds_lfht_iter_get_node(&iter
);
1828 * There is a severe internal error if we are being asked to remove a
1829 * channel that doesn't exist.
1832 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1836 /* Free the list of triggers associated with this channel. */
1837 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1838 channel_triggers_ht_node
);
1839 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1840 &trigger_list
->list
, node
) {
1841 cds_list_del(&trigger_list_element
->node
);
1842 free(trigger_list_element
);
1844 cds_lfht_del(state
->channel_triggers_ht
, node
);
1845 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1847 /* Free sampled channel state. */
1848 cds_lfht_lookup(state
->channel_state_ht
,
1849 hash_channel_key(&key
),
1850 match_channel_state_sample
,
1853 node
= cds_lfht_iter_get_node(&iter
);
1855 * This is expected to be NULL if the channel is destroyed before we
1856 * received a sample.
1859 struct channel_state_sample
*sample
= caa_container_of(node
,
1860 struct channel_state_sample
,
1861 channel_state_ht_node
);
1863 cds_lfht_del(state
->channel_state_ht
, node
);
1864 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1867 /* Remove the channel from the channels_ht and free it. */
1868 cds_lfht_lookup(state
->channels_ht
,
1869 hash_channel_key(&key
),
1873 node
= cds_lfht_iter_get_node(&iter
);
1875 channel_info
= caa_container_of(node
, struct channel_info
,
1877 cds_lfht_del(state
->channels_ht
, node
);
1878 channel_info_destroy(channel_info
);
1881 *cmd_result
= LTTNG_OK
;
1886 int handle_notification_thread_command_session_rotation(
1887 struct notification_thread_state
*state
,
1888 enum notification_thread_command_type cmd_type
,
1889 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1890 uint64_t trace_archive_chunk_id
,
1891 struct lttng_trace_archive_location
*location
,
1892 enum lttng_error_code
*_cmd_result
)
1895 enum lttng_error_code cmd_result
= LTTNG_OK
;
1896 struct lttng_session_trigger_list
*trigger_list
;
1897 struct lttng_trigger_list_element
*trigger_list_element
;
1898 struct session_info
*session_info
;
1899 const struct lttng_credentials session_creds
= {
1900 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1901 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1906 session_info
= find_or_create_session_info(state
, session_name
,
1907 session_uid
, session_gid
);
1908 if (!session_info
) {
1909 /* Allocation error or an internal error occurred. */
1911 cmd_result
= LTTNG_ERR_NOMEM
;
1915 session_info
->rotation
.ongoing
=
1916 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1917 session_info
->rotation
.id
= trace_archive_chunk_id
;
1918 trigger_list
= get_session_trigger_list(state
, session_name
);
1919 if (!trigger_list
) {
1920 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1925 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1927 const struct lttng_condition
*condition
;
1928 struct lttng_trigger
*trigger
;
1929 struct notification_client_list
*client_list
;
1930 struct lttng_evaluation
*evaluation
= NULL
;
1931 enum lttng_condition_type condition_type
;
1932 enum action_executor_status executor_status
;
1934 trigger
= trigger_list_element
->trigger
;
1935 condition
= lttng_trigger_get_const_condition(trigger
);
1937 condition_type
= lttng_condition_get_type(condition
);
1939 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1940 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1942 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1943 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1947 client_list
= get_client_list_from_condition(state
, condition
);
1948 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1949 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1950 trace_archive_chunk_id
);
1952 evaluation
= lttng_evaluation_session_rotation_completed_create(
1953 trace_archive_chunk_id
, location
);
1957 /* Internal error */
1959 cmd_result
= LTTNG_ERR_UNK
;
1964 * Ownership of `evaluation` transferred to the action executor
1965 * no matter the result.
1967 executor_status
= action_executor_enqueue_trigger(
1968 state
->executor
, trigger
, evaluation
,
1969 &session_creds
, client_list
);
1971 switch (executor_status
) {
1972 case ACTION_EXECUTOR_STATUS_OK
:
1974 case ACTION_EXECUTOR_STATUS_ERROR
:
1975 case ACTION_EXECUTOR_STATUS_INVALID
:
1977 * TODO Add trigger identification (name/id) when
1978 * it is added to the API.
1980 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1983 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1985 * TODO Add trigger identification (name/id) when
1986 * it is added to the API.
1988 * Not a fatal error.
1990 WARN("No space left when enqueuing action associated with session rotation trigger");
1998 notification_client_list_put(client_list
);
1999 if (caa_unlikely(ret
)) {
2004 session_info_put(session_info
);
2005 *_cmd_result
= cmd_result
;
2011 int handle_notification_thread_command_add_tracer_event_source(
2012 struct notification_thread_state
*state
,
2013 int tracer_event_source_fd
,
2014 enum lttng_domain_type domain_type
,
2015 enum lttng_error_code
*_cmd_result
)
2018 enum lttng_error_code cmd_result
= LTTNG_OK
;
2019 struct notification_event_tracer_event_source_element
*element
= NULL
;
2021 element
= zmalloc(sizeof(*element
));
2023 cmd_result
= LTTNG_ERR_NOMEM
;
2028 element
->fd
= tracer_event_source_fd
;
2029 element
->domain
= domain_type
;
2031 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2033 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2034 tracer_event_source_fd
,
2035 lttng_domain_type_str(domain_type
));
2037 /* Adding the read side pipe to the event poll. */
2038 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
2040 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2041 tracer_event_source_fd
,
2042 lttng_domain_type_str(element
->domain
));
2043 cds_list_del(&element
->node
);
2048 element
->is_fd_in_poll_set
= true;
2051 *_cmd_result
= cmd_result
;
2056 int drain_event_notifier_notification_pipe(
2057 struct notification_thread_state
*state
,
2058 int pipe
, enum lttng_domain_type domain
)
2060 struct lttng_poll_event events
= {0};
2063 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2065 ERR("[notification-thread] Error creating lttng_poll_event");
2069 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2071 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2078 * Continue to consume notifications as long as there are new
2079 * ones coming in. The tracer has been asked to stop producing
2082 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2083 * monitored (on Linux, at least) and will be returned when
2084 * the pipe is closed but empty.
2086 ret
= lttng_poll_wait_interruptible(&events
, 0);
2087 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2088 /* No more notification to be read on this pipe. */
2091 } else if (ret
< 0) {
2092 PERROR("Failed on lttng_poll_wait_interruptible() call");
2097 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2099 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2104 lttng_poll_clean(&events
);
2109 int handle_notification_thread_command_remove_tracer_event_source(
2110 struct notification_thread_state
*state
,
2111 int tracer_event_source_fd
,
2112 enum lttng_error_code
*_cmd_result
)
2116 enum lttng_error_code cmd_result
= LTTNG_OK
;
2117 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
2119 cds_list_for_each_entry_safe(source_element
, tmp
,
2120 &state
->tracer_event_sources_list
, node
) {
2121 if (source_element
->fd
!= tracer_event_source_fd
) {
2125 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2126 tracer_event_source_fd
,
2127 lttng_domain_type_str(source_element
->domain
));
2128 cds_list_del(&source_element
->node
);
2135 * This is temporarily allowed since the poll activity set is
2136 * not properly cleaned-up for the moment. This is adressed in
2139 source_element
= NULL
;
2143 if (!source_element
->is_fd_in_poll_set
) {
2144 /* Skip the poll set removal. */
2148 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2149 tracer_event_source_fd
,
2150 lttng_domain_type_str(source_element
->domain
));
2152 /* Removing the fd from the event poll set. */
2153 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2155 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2156 tracer_event_source_fd
,
2157 lttng_domain_type_str(source_element
->domain
));
2158 cmd_result
= LTTNG_ERR_FATAL
;
2162 source_element
->is_fd_in_poll_set
= false;
2164 ret
= drain_event_notifier_notification_pipe(state
, tracer_event_source_fd
,
2165 source_element
->domain
);
2167 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2168 tracer_event_source_fd
,
2169 lttng_domain_type_str(source_element
->domain
));
2170 cmd_result
= LTTNG_ERR_FATAL
;
2175 * The drain_event_notifier_notification_pipe() call might have read
2176 * data from an fd that we received in event in the latest _poll_wait()
2177 * call. Make sure the thread call poll_wait() again to ensure we have
2180 state
->restart_poll
= true;
2183 free(source_element
);
2184 *_cmd_result
= cmd_result
;
2188 int handle_notification_thread_remove_tracer_event_source_no_result(
2189 struct notification_thread_state
*state
,
2190 int tracer_event_source_fd
)
2193 enum lttng_error_code cmd_result
;
2195 ret
= handle_notification_thread_command_remove_tracer_event_source(
2196 state
, tracer_event_source_fd
, &cmd_result
);
2201 static int handle_notification_thread_command_list_triggers(
2202 struct notification_thread_handle
*handle
,
2203 struct notification_thread_state
*state
,
2205 struct lttng_triggers
**triggers
,
2206 enum lttng_error_code
*_cmd_result
)
2209 enum lttng_error_code cmd_result
= LTTNG_OK
;
2210 struct cds_lfht_iter iter
;
2211 struct lttng_trigger_ht_element
*trigger_ht_element
;
2212 struct lttng_triggers
*local_triggers
= NULL
;
2213 const struct lttng_credentials
*creds
;
2217 local_triggers
= lttng_triggers_create();
2218 if (!local_triggers
) {
2219 /* Not a fatal error. */
2220 cmd_result
= LTTNG_ERR_NOMEM
;
2224 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2225 trigger_ht_element
, node
) {
2227 * Only return the triggers to which the client has access.
2228 * The root user has visibility over all triggers.
2230 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2231 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2235 ret
= lttng_triggers_add(local_triggers
,
2236 trigger_ht_element
->trigger
);
2238 /* Not a fatal error. */
2240 cmd_result
= LTTNG_ERR_NOMEM
;
2245 /* Transferring ownership to the caller. */
2246 *triggers
= local_triggers
;
2247 local_triggers
= NULL
;
2251 lttng_triggers_destroy(local_triggers
);
2252 *_cmd_result
= cmd_result
;
2256 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
2257 const char **trigger_name
,
2258 uid_t
*trigger_owner_uid
)
2260 enum lttng_trigger_status trigger_status
;
2262 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
2263 switch (trigger_status
) {
2264 case LTTNG_TRIGGER_STATUS_OK
:
2266 case LTTNG_TRIGGER_STATUS_UNSET
:
2267 *trigger_name
= "(anonymous)";
2273 trigger_status
= lttng_trigger_get_owner_uid(trigger
,
2275 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2278 static int handle_notification_thread_command_get_trigger(
2279 struct notification_thread_state
*state
,
2280 const struct lttng_trigger
*trigger
,
2281 struct lttng_trigger
**registered_trigger
,
2282 enum lttng_error_code
*_cmd_result
)
2285 struct cds_lfht_iter iter
;
2286 struct lttng_trigger_ht_element
*trigger_ht_element
;
2287 enum lttng_error_code cmd_result
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2288 const char *trigger_name
;
2289 uid_t trigger_owner_uid
;
2293 cds_lfht_for_each_entry(
2294 state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2295 if (lttng_trigger_is_equal(
2296 trigger
, trigger_ht_element
->trigger
)) {
2297 /* Take one reference on the return trigger. */
2298 *registered_trigger
= trigger_ht_element
->trigger
;
2299 lttng_trigger_get(*registered_trigger
);
2301 cmd_result
= LTTNG_OK
;
2306 /* Not a fatal error if the trigger is not found. */
2307 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
2308 DBG("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d",
2309 trigger_name
, (int) trigger_owner_uid
);
2315 *_cmd_result
= cmd_result
;
2320 bool condition_is_supported(struct lttng_condition
*condition
)
2324 switch (lttng_condition_get_type(condition
)) {
2325 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2326 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2329 enum lttng_domain_type domain
;
2331 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2335 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2336 is_supported
= true;
2341 * Older kernel tracers don't expose the API to monitor their
2342 * buffers. Therefore, we reject triggers that require that
2343 * mechanism to be available to be evaluated.
2345 * Assume unsupported on error.
2347 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2350 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
2352 const struct lttng_event_rule
*event_rule
;
2353 enum lttng_domain_type domain
;
2354 const enum lttng_condition_status status
=
2355 lttng_condition_event_rule_matches_get_rule(
2356 condition
, &event_rule
);
2358 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2360 domain
= lttng_event_rule_get_domain_type(event_rule
);
2361 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2362 is_supported
= true;
2367 * Older kernel tracers can't emit notification. Therefore, we
2368 * reject triggers that require that mechanism to be available
2371 * Assume unsupported on error.
2373 is_supported
= kernel_supports_event_notifiers() == 1;
2377 is_supported
= true;
2380 return is_supported
;
2383 /* Must be called with RCU read lock held. */
2385 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2386 struct notification_thread_state
*state
)
2389 const struct lttng_condition
*condition
;
2390 const char *session_name
;
2391 struct lttng_session_trigger_list
*trigger_list
;
2393 condition
= lttng_trigger_get_const_condition(trigger
);
2394 switch (lttng_condition_get_type(condition
)) {
2395 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2396 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2398 enum lttng_condition_status status
;
2400 status
= lttng_condition_session_rotation_get_session_name(
2401 condition
, &session_name
);
2402 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2403 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2414 trigger_list
= get_session_trigger_list(state
, session_name
);
2415 if (!trigger_list
) {
2416 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2422 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2424 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2429 /* Must be called with RCU read lock held. */
2431 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2432 struct notification_thread_state
*state
)
2435 struct cds_lfht_node
*node
;
2436 struct cds_lfht_iter iter
;
2437 struct channel_info
*channel
;
2439 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2441 struct lttng_trigger_list_element
*trigger_list_element
;
2442 struct lttng_channel_trigger_list
*trigger_list
;
2443 struct cds_lfht_iter lookup_iter
;
2445 if (!trigger_applies_to_channel(trigger
, channel
)) {
2449 cds_lfht_lookup(state
->channel_triggers_ht
,
2450 hash_channel_key(&channel
->key
),
2451 match_channel_trigger_list
,
2454 node
= cds_lfht_iter_get_node(&lookup_iter
);
2456 trigger_list
= caa_container_of(node
,
2457 struct lttng_channel_trigger_list
,
2458 channel_triggers_ht_node
);
2460 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2461 if (!trigger_list_element
) {
2465 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2466 trigger_list_element
->trigger
= trigger
;
2467 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2468 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2476 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2478 bool is_notify
= false;
2479 unsigned int i
, count
;
2480 enum lttng_action_status action_status
;
2481 const struct lttng_action
*action
=
2482 lttng_trigger_get_const_action(trigger
);
2483 enum lttng_action_type action_type
;
2486 action_type
= lttng_action_get_type(action
);
2487 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2490 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2494 action_status
= lttng_action_list_get_count(action
, &count
);
2495 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2497 for (i
= 0; i
< count
; i
++) {
2498 const struct lttng_action
*inner_action
=
2499 lttng_action_list_get_at_index(
2502 action_type
= lttng_action_get_type(inner_action
);
2503 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2513 static bool trigger_name_taken(struct notification_thread_state
*state
,
2514 const struct lttng_trigger
*trigger
)
2516 struct cds_lfht_iter iter
;
2519 * No duplicata is allowed in the triggers_by_name_uid_ht.
2520 * The match is done against the trigger name and uid.
2522 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2523 hash_trigger_by_name_uid(trigger
),
2524 match_trigger_by_name_uid
,
2527 return !!cds_lfht_iter_get_node(&iter
);
2531 enum lttng_error_code
generate_trigger_name(
2532 struct notification_thread_state
*state
,
2533 struct lttng_trigger
*trigger
, const char **name
)
2535 enum lttng_error_code ret_code
= LTTNG_OK
;
2537 enum lttng_trigger_status status
;
2540 const int ret
= lttng_trigger_generate_name(trigger
,
2541 state
->trigger_id
.name_offset
++);
2543 /* The only reason this can fail right now. */
2544 ret_code
= LTTNG_ERR_NOMEM
;
2548 status
= lttng_trigger_get_name(trigger
, name
);
2549 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2551 taken
= trigger_name_taken(state
, trigger
);
2552 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2558 void notif_thread_state_remove_trigger_ht_elem(
2559 struct notification_thread_state
*state
,
2560 struct lttng_trigger_ht_element
*trigger_ht_element
)
2563 assert(trigger_ht_element
);
2565 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2566 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2570 enum lttng_error_code
setup_tracer_notifier(
2571 struct notification_thread_state
*state
,
2572 struct lttng_trigger
*trigger
)
2574 enum lttng_error_code ret
;
2575 enum event_notifier_error_accounting_status error_accounting_status
;
2576 struct cds_lfht_node
*node
;
2577 uint64_t error_counter_index
= 0;
2578 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2579 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2581 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2582 if (!trigger_tokens_ht_element
) {
2583 ret
= LTTNG_ERR_NOMEM
;
2587 /* Add trigger token to the trigger_tokens_ht. */
2588 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2589 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2590 trigger_tokens_ht_element
->trigger
= trigger
;
2592 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2593 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2594 match_trigger_token
,
2595 &trigger_tokens_ht_element
->token
,
2596 &trigger_tokens_ht_element
->node
);
2597 if (node
!= &trigger_tokens_ht_element
->node
) {
2598 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2599 goto error_free_ht_element
;
2602 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2603 trigger
, &error_counter_index
);
2604 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2605 if (error_accounting_status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2606 DBG("Trigger group error accounting counter full.");
2607 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2609 ERR("Error registering trigger for error accounting");
2610 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2613 goto error_remove_ht_element
;
2616 lttng_condition_event_rule_matches_set_error_counter_index(
2617 condition
, error_counter_index
);
2622 error_remove_ht_element
:
2623 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2624 error_free_ht_element
:
2625 free(trigger_tokens_ht_element
);
2631 * FIXME A client's credentials are not checked when registering a trigger.
2633 * The effects of this are benign since:
2634 * - The client will succeed in registering the trigger, as it is valid,
2635 * - The trigger will, internally, be bound to the channel/session,
2636 * - The notifications will not be sent since the client's credentials
2637 * are checked against the channel at that moment.
2639 * If this function returns a non-zero value, it means something is
2640 * fundamentally broken and the whole subsystem/thread will be torn down.
2642 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2646 int handle_notification_thread_command_register_trigger(
2647 struct notification_thread_state
*state
,
2648 struct lttng_trigger
*trigger
,
2649 bool is_trigger_anonymous
,
2650 enum lttng_error_code
*cmd_result
)
2653 struct lttng_condition
*condition
;
2654 struct notification_client_list
*client_list
= NULL
;
2655 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2656 struct cds_lfht_node
*node
;
2657 const char* trigger_name
;
2658 bool free_trigger
= true;
2659 struct lttng_evaluation
*evaluation
= NULL
;
2660 struct lttng_credentials object_creds
;
2663 enum action_executor_status executor_status
;
2664 const uint64_t trigger_tracer_token
=
2665 state
->trigger_id
.next_tracer_token
++;
2669 /* Set the trigger's tracer token. */
2670 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2672 if (!is_trigger_anonymous
) {
2673 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2674 LTTNG_TRIGGER_STATUS_UNSET
) {
2675 const enum lttng_error_code ret_code
=
2676 generate_trigger_name(state
, trigger
,
2679 if (ret_code
!= LTTNG_OK
) {
2682 *cmd_result
= ret_code
;
2685 } else if (trigger_name_taken(state
, trigger
)) {
2686 /* Not a fatal error. */
2687 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2692 trigger_name
= "(anonymous)";
2695 condition
= lttng_trigger_get_condition(trigger
);
2698 /* Some conditions require tracers to implement a minimal ABI version. */
2699 if (!condition_is_supported(condition
)) {
2700 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2704 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2705 if (!trigger_ht_element
) {
2710 /* Add trigger to the trigger_ht. */
2711 cds_lfht_node_init(&trigger_ht_element
->node
);
2712 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2713 trigger_ht_element
->trigger
= trigger
;
2715 node
= cds_lfht_add_unique(state
->triggers_ht
,
2716 lttng_condition_hash(condition
),
2719 &trigger_ht_element
->node
);
2720 if (node
!= &trigger_ht_element
->node
) {
2721 /* Not a fatal error, simply report it to the client. */
2722 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2723 goto error_free_ht_element
;
2726 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2727 hash_trigger_by_name_uid(trigger
),
2728 match_trigger_by_name_uid
,
2730 &trigger_ht_element
->node_by_name_uid
);
2731 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2732 /* Not a fatal error, simply report it to the client. */
2733 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2734 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2735 goto error_free_ht_element
;
2738 /* From this point consider the trigger registered. */
2739 lttng_trigger_set_as_registered(trigger
);
2742 * Some triggers might need a tracer notifier depending on its
2743 * condition and actions.
2745 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2746 enum lttng_error_code error_code
;
2748 error_code
= setup_tracer_notifier(state
, trigger
);
2749 if (error_code
!= LTTNG_OK
) {
2750 notif_thread_state_remove_trigger_ht_elem(state
,
2751 trigger_ht_element
);
2752 if (error_code
== LTTNG_ERR_NOMEM
) {
2755 *cmd_result
= error_code
;
2759 goto error_free_ht_element
;
2764 * The rest only applies to triggers that have a "notify" action.
2765 * It is not skipped as this is the only action type currently
2768 if (is_trigger_action_notify(trigger
)) {
2770 * Find or create the client list of this condition. It may
2771 * already be present if another trigger is already registered
2772 * with the same condition.
2774 client_list
= get_client_list_from_condition(state
, condition
);
2777 * No client list for this condition yet. We create new
2778 * one and build it up.
2780 client_list
= notification_client_list_create(state
, condition
);
2782 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2783 goto error_free_ht_element
;
2787 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2789 pthread_mutex_lock(&client_list
->lock
);
2790 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2791 pthread_mutex_unlock(&client_list
->lock
);
2795 * Ownership of the trigger and of its wrapper was transfered to
2796 * the triggers_ht. Same for token ht element if necessary.
2798 trigger_ht_element
= NULL
;
2799 free_trigger
= false;
2801 switch (get_condition_binding_object(condition
)) {
2802 case LTTNG_OBJECT_TYPE_SESSION
:
2803 /* Add the trigger to the list if it matches a known session. */
2804 ret
= bind_trigger_to_matching_session(trigger
, state
);
2806 goto error_free_ht_element
;
2809 case LTTNG_OBJECT_TYPE_CHANNEL
:
2811 * Add the trigger to list of triggers bound to the channels
2814 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2816 goto error_free_ht_element
;
2819 case LTTNG_OBJECT_TYPE_NONE
:
2822 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2824 goto error_free_ht_element
;
2828 * The new trigger's condition must be evaluated against the current
2831 * In the case of `notify` action, nothing preventing clients from
2832 * subscribing to a condition before the corresponding trigger is
2833 * registered, we have to evaluate this new condition right away.
2835 * At some point, we were waiting for the next "evaluation" (e.g. on
2836 * reception of a channel sample) to evaluate this new condition, but
2839 * The reason it was broken is that waiting for the next sample
2840 * does not allow us to properly handle transitions for edge-triggered
2843 * Consider this example: when we handle a new channel sample, we
2844 * evaluate each conditions twice: once with the previous state, and
2845 * again with the newest state. We then use those two results to
2846 * determine whether a state change happened: a condition was false and
2847 * became true. If a state change happened, we have to notify clients.
2849 * Now, if a client subscribes to a given notification and registers
2850 * a trigger *after* that subscription, we have to make sure the
2851 * condition is evaluated at this point while considering only the
2852 * current state. Otherwise, the next evaluation cycle may only see
2853 * that the evaluations remain the same (true for samples n-1 and n) and
2854 * the client will never know that the condition has been met.
2856 switch (get_condition_binding_object(condition
)) {
2857 case LTTNG_OBJECT_TYPE_SESSION
:
2858 ret
= evaluate_session_condition_for_client(condition
, state
,
2859 &evaluation
, &object_uid
,
2861 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2862 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2864 case LTTNG_OBJECT_TYPE_CHANNEL
:
2865 ret
= evaluate_channel_condition_for_client(condition
, state
,
2866 &evaluation
, &object_uid
,
2868 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2869 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2871 case LTTNG_OBJECT_TYPE_NONE
:
2874 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2882 goto error_free_ht_element
;
2885 DBG("Newly registered trigger's condition evaluated to %s",
2886 evaluation
? "true" : "false");
2888 /* Evaluation yielded nothing. Normal exit. */
2894 * Ownership of `evaluation` transferred to the action executor
2895 * no matter the result.
2897 executor_status
= action_executor_enqueue_trigger(state
->executor
,
2898 trigger
, evaluation
, &object_creds
, client_list
);
2900 switch (executor_status
) {
2901 case ACTION_EXECUTOR_STATUS_OK
:
2903 case ACTION_EXECUTOR_STATUS_ERROR
:
2904 case ACTION_EXECUTOR_STATUS_INVALID
:
2906 * TODO Add trigger identification (name/id) when
2907 * it is added to the API.
2909 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2911 goto error_free_ht_element
;
2912 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2914 * TODO Add trigger identification (name/id) when
2915 * it is added to the API.
2917 * Not a fatal error.
2919 WARN("No space left when enqueuing action associated to newly registered trigger");
2927 *cmd_result
= LTTNG_OK
;
2928 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2929 trigger_name
, trigger_tracer_token
);
2932 error_free_ht_element
:
2933 if (trigger_ht_element
) {
2934 /* Delayed removal due to RCU constraint on delete. */
2935 call_rcu(&trigger_ht_element
->rcu_node
,
2936 free_lttng_trigger_ht_element_rcu
);
2941 * Other objects might have a reference to the trigger, mark it
2944 lttng_trigger_set_as_unregistered(trigger
);
2945 lttng_trigger_destroy(trigger
);
2953 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2955 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2960 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2962 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2967 void teardown_tracer_notifier(struct notification_thread_state
*state
,
2968 const struct lttng_trigger
*trigger
)
2970 struct cds_lfht_iter iter
;
2971 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2973 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
2974 trigger_tokens_ht_element
, node
) {
2976 if (!lttng_trigger_is_equal(trigger
,
2977 trigger_tokens_ht_element
->trigger
)) {
2981 event_notifier_error_accounting_unregister_event_notifier(
2982 trigger_tokens_ht_element
->trigger
);
2984 /* TODO talk to all app and remove it */
2985 DBG("[notification-thread] Removed trigger from tokens_ht");
2986 cds_lfht_del(state
->trigger_tokens_ht
,
2987 &trigger_tokens_ht_element
->node
);
2989 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2990 free_notification_trigger_tokens_ht_element_rcu
);
2997 int handle_notification_thread_command_unregister_trigger(
2998 struct notification_thread_state
*state
,
2999 const struct lttng_trigger
*trigger
,
3000 enum lttng_error_code
*_cmd_reply
)
3002 struct cds_lfht_iter iter
;
3003 struct cds_lfht_node
*triggers_ht_node
;
3004 struct lttng_channel_trigger_list
*trigger_list
;
3005 struct notification_client_list
*client_list
;
3006 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
3007 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
3009 enum lttng_error_code cmd_reply
;
3013 cds_lfht_lookup(state
->triggers_ht
,
3014 lttng_condition_hash(condition
),
3018 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3019 if (!triggers_ht_node
) {
3020 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3023 cmd_reply
= LTTNG_OK
;
3026 trigger_ht_element
= caa_container_of(triggers_ht_node
,
3027 struct lttng_trigger_ht_element
, node
);
3029 /* Remove trigger from channel_triggers_ht. */
3030 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
3031 channel_triggers_ht_node
) {
3032 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3034 cds_list_for_each_entry_safe(trigger_element
, tmp
,
3035 &trigger_list
->list
, node
) {
3036 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3040 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
3041 cds_list_del(&trigger_element
->node
);
3042 /* A trigger can only appear once per channel */
3047 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3048 teardown_tracer_notifier(state
, trigger
);
3051 if (is_trigger_action_notify(trigger
)) {
3053 * Remove and release the client list from
3054 * notification_trigger_clients_ht.
3056 client_list
= get_client_list_from_condition(state
, condition
);
3057 assert(client_list
);
3059 pthread_mutex_lock(&client_list
->lock
);
3060 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3061 pthread_mutex_unlock(&client_list
->lock
);
3063 /* Put new reference and the hashtable's reference. */
3064 notification_client_list_put(client_list
);
3065 notification_client_list_put(client_list
);
3069 /* Remove trigger from triggers_ht. */
3070 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3072 /* Release the ownership of the trigger. */
3073 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3074 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3078 *_cmd_reply
= cmd_reply
;
3083 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3084 int handle_notification_thread_command(
3085 struct notification_thread_handle
*handle
,
3086 struct notification_thread_state
*state
)
3090 struct notification_thread_command
*cmd
;
3092 /* Read the event pipe to put it back into a quiescent state. */
3093 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
3095 if (ret
!= sizeof(counter
)) {
3099 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3100 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3101 struct notification_thread_command
, cmd_list_node
);
3102 cds_list_del(&cmd
->cmd_list_node
);
3103 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3105 DBG("[notification-thread] Received `%s` command",
3106 notification_command_type_str(cmd
->type
));
3107 switch (cmd
->type
) {
3108 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3109 ret
= handle_notification_thread_command_register_trigger(state
,
3110 cmd
->parameters
.register_trigger
.trigger
,
3111 cmd
->parameters
.register_trigger
.is_trigger_anonymous
,
3114 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3115 ret
= handle_notification_thread_command_unregister_trigger(
3117 cmd
->parameters
.unregister_trigger
.trigger
,
3120 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3121 ret
= handle_notification_thread_command_add_channel(
3123 cmd
->parameters
.add_channel
.session
.name
,
3124 cmd
->parameters
.add_channel
.session
.uid
,
3125 cmd
->parameters
.add_channel
.session
.gid
,
3126 cmd
->parameters
.add_channel
.channel
.name
,
3127 cmd
->parameters
.add_channel
.channel
.domain
,
3128 cmd
->parameters
.add_channel
.channel
.key
,
3129 cmd
->parameters
.add_channel
.channel
.capacity
,
3132 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3133 ret
= handle_notification_thread_command_remove_channel(
3134 state
, cmd
->parameters
.remove_channel
.key
,
3135 cmd
->parameters
.remove_channel
.domain
,
3138 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3139 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3140 ret
= handle_notification_thread_command_session_rotation(
3143 cmd
->parameters
.session_rotation
.session_name
,
3144 cmd
->parameters
.session_rotation
.uid
,
3145 cmd
->parameters
.session_rotation
.gid
,
3146 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3147 cmd
->parameters
.session_rotation
.location
,
3150 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3151 ret
= handle_notification_thread_command_add_tracer_event_source(
3153 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3154 cmd
->parameters
.tracer_event_source
.domain
,
3157 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3158 ret
= handle_notification_thread_command_remove_tracer_event_source(
3160 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3163 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3165 struct lttng_triggers
*triggers
= NULL
;
3167 ret
= handle_notification_thread_command_list_triggers(
3170 cmd
->parameters
.list_triggers
.uid
,
3173 cmd
->reply
.list_triggers
.triggers
= triggers
;
3177 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3178 cmd
->reply_code
= LTTNG_OK
;
3181 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3183 struct lttng_trigger
*trigger
= NULL
;
3185 ret
= handle_notification_thread_command_get_trigger(state
,
3186 cmd
->parameters
.get_trigger
.trigger
, &trigger
,
3188 cmd
->reply
.get_trigger
.trigger
= trigger
;
3191 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3193 const enum client_transmission_status client_status
=
3194 cmd
->parameters
.client_communication_update
3196 const notification_client_id client_id
=
3197 cmd
->parameters
.client_communication_update
.id
;
3198 struct notification_client
*client
;
3201 client
= get_client_from_id(client_id
, state
);
3205 * Client error was probably already picked-up by the
3206 * notification thread or it has disconnected
3207 * gracefully while this command was queued.
3209 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3213 ret
= client_handle_transmission_status(
3214 client
, client_status
, state
);
3220 ERR("[notification-thread] Unknown internal command received");
3228 if (cmd
->is_async
) {
3232 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3236 /* Wake-up and return a fatal error to the calling thread. */
3237 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3238 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3240 /* Indicate a fatal error to the caller. */
3245 int socket_set_non_blocking(int socket
)
3249 /* Set the pipe as non-blocking. */
3250 ret
= fcntl(socket
, F_GETFL
, 0);
3252 PERROR("fcntl get socket flags");
3257 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3259 PERROR("fcntl set O_NONBLOCK socket flag");
3262 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3268 int client_reset_inbound_state(struct notification_client
*client
)
3273 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3275 client
->communication
.inbound
.bytes_to_receive
=
3276 sizeof(struct lttng_notification_channel_message
);
3277 client
->communication
.inbound
.msg_type
=
3278 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3279 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3280 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3281 ret
= lttng_dynamic_buffer_set_size(
3282 &client
->communication
.inbound
.payload
.buffer
,
3283 client
->communication
.inbound
.bytes_to_receive
);
3288 int handle_notification_thread_client_connect(
3289 struct notification_thread_state
*state
)
3292 struct notification_client
*client
;
3294 DBG("[notification-thread] Handling new notification channel client connection");
3296 client
= zmalloc(sizeof(*client
));
3303 pthread_mutex_init(&client
->lock
, NULL
);
3304 client
->id
= state
->next_notification_client_id
++;
3305 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3306 lttng_payload_init(&client
->communication
.inbound
.payload
);
3307 lttng_payload_init(&client
->communication
.outbound
.payload
);
3308 client
->communication
.inbound
.expect_creds
= true;
3310 ret
= client_reset_inbound_state(client
);
3312 ERR("[notification-thread] Failed to reset client communication's inbound state");
3317 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3319 ERR("[notification-thread] Failed to accept new notification channel client connection");
3324 client
->socket
= ret
;
3326 ret
= socket_set_non_blocking(client
->socket
);
3328 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3332 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3334 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3339 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3340 LPOLLIN
| LPOLLERR
|
3341 LPOLLHUP
| LPOLLRDHUP
);
3343 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3347 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3351 cds_lfht_add(state
->client_socket_ht
,
3352 hash_client_socket(client
->socket
),
3353 &client
->client_socket_ht_node
);
3354 cds_lfht_add(state
->client_id_ht
,
3355 hash_client_id(client
->id
),
3356 &client
->client_id_ht_node
);
3362 notification_client_destroy(client
, state
);
3367 * RCU read-lock must be held by the caller.
3368 * Client lock must _not_ be held by the caller.
3371 int notification_thread_client_disconnect(
3372 struct notification_client
*client
,
3373 struct notification_thread_state
*state
)
3376 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3378 /* Acquire the client lock to disable its communication atomically. */
3379 pthread_mutex_lock(&client
->lock
);
3380 client
->communication
.active
= false;
3381 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3382 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3383 pthread_mutex_unlock(&client
->lock
);
3385 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3387 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3391 /* Release all conditions to which the client was subscribed. */
3392 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3393 &client
->condition_list
, node
) {
3394 (void) notification_thread_client_unsubscribe(client
,
3395 condition_list_element
->condition
, state
, NULL
);
3399 * Client no longer accessible to other threads (through the
3402 notification_client_destroy(client
, state
);
3406 int handle_notification_thread_client_disconnect(
3407 int client_socket
, struct notification_thread_state
*state
)
3410 struct notification_client
*client
;
3413 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3415 client
= get_client_from_socket(client_socket
, state
);
3417 /* Internal state corruption, fatal error. */
3418 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3424 ret
= notification_thread_client_disconnect(client
, state
);
3430 int handle_notification_thread_client_disconnect_all(
3431 struct notification_thread_state
*state
)
3433 struct cds_lfht_iter iter
;
3434 struct notification_client
*client
;
3435 bool error_encoutered
= false;
3438 DBG("[notification-thread] Closing all client connections");
3439 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3440 client_socket_ht_node
) {
3443 ret
= notification_thread_client_disconnect(
3446 error_encoutered
= true;
3450 return error_encoutered
? 1 : 0;
3453 int handle_notification_thread_trigger_unregister_all(
3454 struct notification_thread_state
*state
)
3456 bool error_occurred
= false;
3457 struct cds_lfht_iter iter
;
3458 struct lttng_trigger_ht_element
*trigger_ht_element
;
3461 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3463 int ret
= handle_notification_thread_command_unregister_trigger(
3464 state
, trigger_ht_element
->trigger
, NULL
);
3466 error_occurred
= true;
3470 return error_occurred
? -1 : 0;
3474 int client_handle_transmission_status(
3475 struct notification_client
*client
,
3476 enum client_transmission_status transmission_status
,
3477 struct notification_thread_state
*state
)
3481 switch (transmission_status
) {
3482 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3483 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3484 CLIENT_POLL_MASK_IN
);
3490 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3492 * We want to be notified whenever there is buffer space
3493 * available to send the rest of the payload.
3495 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3496 CLIENT_POLL_MASK_IN_OUT
);
3501 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3502 ret
= notification_thread_client_disconnect(client
, state
);
3507 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3517 /* Client lock must be acquired by caller. */
3519 enum client_transmission_status
client_flush_outgoing_queue(
3520 struct notification_client
*client
)
3523 size_t to_send_count
;
3524 enum client_transmission_status status
;
3525 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3526 &client
->communication
.outbound
.payload
, 0, -1);
3527 const int fds_to_send_count
=
3528 lttng_payload_view_get_fd_handle_count(&pv
);
3530 ASSERT_LOCKED(client
->lock
);
3532 if (!client
->communication
.active
) {
3533 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3537 if (pv
.buffer
.size
== 0) {
3539 * If both data and fds are equal to zero, we are in an invalid
3542 assert(fds_to_send_count
!= 0);
3547 to_send_count
= pv
.buffer
.size
;
3548 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3551 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3554 if ((ret
>= 0 && ret
< to_send_count
)) {
3555 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3557 to_send_count
-= max(ret
, 0);
3559 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3561 pv
.buffer
.size
- to_send_count
,
3563 ret
= lttng_dynamic_buffer_set_size(
3564 &client
->communication
.outbound
.payload
.buffer
,
3570 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3572 } else if (ret
< 0) {
3573 /* Generic error, disable the client's communication. */
3574 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3576 client
->communication
.active
= false;
3577 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3581 * No error and flushed the queue completely.
3583 * The payload buffer size is used later to
3584 * check if there is notifications queued. So albeit that the
3585 * direct caller knows that the transmission is complete, we
3586 * need to set the buffer size to zero.
3588 ret
= lttng_dynamic_buffer_set_size(
3589 &client
->communication
.outbound
.payload
.buffer
, 0);
3596 /* No fds to send, transmission is complete. */
3597 if (fds_to_send_count
== 0) {
3598 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3602 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3603 client
->socket
, &pv
);
3605 /* Generic error, disable the client's communication. */
3606 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3608 client
->communication
.active
= false;
3609 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3611 } else if (ret
== 0) {
3612 /* Nothing could be sent. */
3613 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3615 /* Fd passing is an all or nothing kind of thing. */
3616 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3618 * The payload _fd_array count is used later to
3619 * check if there is notifications queued. So although the
3620 * direct caller knows that the transmission is complete, we
3621 * need to clear the _fd_array for the queuing check.
3623 lttng_dynamic_pointer_array_clear(
3624 &client
->communication
.outbound
.payload
3629 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3630 client
->communication
.outbound
.queued_command_reply
= false;
3631 client
->communication
.outbound
.dropped_notification
= false;
3632 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3637 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3641 bool client_has_outbound_data_left(
3642 const struct notification_client
*client
)
3644 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3645 &client
->communication
.outbound
.payload
, 0, -1);
3646 const bool has_data
= pv
.buffer
.size
!= 0;
3647 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3649 return has_data
|| has_fds
;
3652 /* Client lock must _not_ be held by the caller. */
3654 int client_send_command_reply(struct notification_client
*client
,
3655 struct notification_thread_state
*state
,
3656 enum lttng_notification_channel_status status
)
3659 struct lttng_notification_channel_command_reply reply
= {
3660 .status
= (int8_t) status
,
3662 struct lttng_notification_channel_message msg
= {
3663 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3664 .size
= sizeof(reply
),
3666 char buffer
[sizeof(msg
) + sizeof(reply
)];
3667 enum client_transmission_status transmission_status
;
3669 memcpy(buffer
, &msg
, sizeof(msg
));
3670 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3671 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3673 pthread_mutex_lock(&client
->lock
);
3674 if (client
->communication
.outbound
.queued_command_reply
) {
3675 /* Protocol error. */
3679 /* Enqueue buffer to outgoing queue and flush it. */
3680 ret
= lttng_dynamic_buffer_append(
3681 &client
->communication
.outbound
.payload
.buffer
,
3682 buffer
, sizeof(buffer
));
3687 transmission_status
= client_flush_outgoing_queue(client
);
3689 if (client_has_outbound_data_left(client
)) {
3690 /* Queue could not be emptied. */
3691 client
->communication
.outbound
.queued_command_reply
= true;
3694 pthread_mutex_unlock(&client
->lock
);
3695 ret
= client_handle_transmission_status(
3696 client
, transmission_status
, state
);
3703 pthread_mutex_unlock(&client
->lock
);
3709 int client_handle_message_unknown(struct notification_client
*client
,
3710 struct notification_thread_state
*state
)
3714 * Receiving message header. The function will be called again
3715 * once the rest of the message as been received and can be
3718 const struct lttng_notification_channel_message
*msg
;
3720 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3721 msg
= (const struct lttng_notification_channel_message
*)
3722 client
->communication
.inbound
.payload
.buffer
.data
;
3724 if (msg
->size
== 0 ||
3725 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3726 ERR("[notification-thread] Invalid notification channel message: length = %u",
3732 switch (msg
->type
) {
3733 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3734 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3735 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3739 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3743 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3744 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3745 client
->communication
.inbound
.msg_type
=
3746 (enum lttng_notification_channel_message_type
) msg
->type
;
3747 ret
= lttng_dynamic_buffer_set_size(
3748 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3750 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3757 int client_handle_message_handshake(struct notification_client
*client
,
3758 struct notification_thread_state
*state
)
3761 struct lttng_notification_channel_command_handshake
*handshake_client
;
3762 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3763 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3764 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3766 const struct lttng_notification_channel_message msg_header
= {
3767 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3768 .size
= sizeof(handshake_reply
),
3770 enum lttng_notification_channel_status status
=
3771 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3772 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3774 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3775 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3776 sizeof(handshake_reply
));
3779 (struct lttng_notification_channel_command_handshake
*)
3780 client
->communication
.inbound
.payload
.buffer
3782 client
->major
= handshake_client
->major
;
3783 client
->minor
= handshake_client
->minor
;
3784 if (!client
->communication
.inbound
.creds_received
) {
3785 ERR("[notification-thread] No credentials received from client");
3790 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3791 &client
->communication
.inbound
.creds
);
3792 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3793 &client
->communication
.inbound
.creds
);
3794 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3795 client
->uid
, client
->gid
, (int) client
->major
,
3796 (int) client
->minor
);
3798 if (handshake_client
->major
!=
3799 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3800 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3803 pthread_mutex_lock(&client
->lock
);
3804 /* Outgoing queue will be flushed when the command reply is sent. */
3805 ret
= lttng_dynamic_buffer_append(
3806 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3807 sizeof(send_buffer
));
3809 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3813 client
->validated
= true;
3814 client
->communication
.active
= true;
3815 pthread_mutex_unlock(&client
->lock
);
3817 /* Set reception state to receive the next message header. */
3818 ret
= client_reset_inbound_state(client
);
3820 ERR("[notification-thread] Failed to reset client communication's inbound state");
3824 /* Flushes the outgoing queue. */
3825 ret
= client_send_command_reply(client
, state
, status
);
3827 ERR("[notification-thread] Failed to send reply to notification channel client");
3833 pthread_mutex_unlock(&client
->lock
);
3839 int client_handle_message_subscription(
3840 struct notification_client
*client
,
3841 enum lttng_notification_channel_message_type msg_type
,
3842 struct notification_thread_state
*state
)
3845 struct lttng_condition
*condition
;
3846 enum lttng_notification_channel_status status
=
3847 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3848 struct lttng_payload_view condition_view
=
3849 lttng_payload_view_from_payload(
3850 &client
->communication
.inbound
.payload
,
3852 size_t expected_condition_size
;
3855 * No need to lock client to sample the inbound state as the only
3856 * other thread accessing clients (action executor) only uses the
3859 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3860 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3861 if (ret
!= expected_condition_size
) {
3862 ERR("[notification-thread] Malformed condition received from client");
3866 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3867 ret
= notification_thread_client_subscribe(
3868 client
, condition
, state
, &status
);
3870 ret
= notification_thread_client_unsubscribe(
3871 client
, condition
, state
, &status
);
3878 /* Set reception state to receive the next message header. */
3879 ret
= client_reset_inbound_state(client
);
3881 ERR("[notification-thread] Failed to reset client communication's inbound state");
3885 ret
= client_send_command_reply(client
, state
, status
);
3887 ERR("[notification-thread] Failed to send reply to notification channel client");
3896 int client_dispatch_message(struct notification_client
*client
,
3897 struct notification_thread_state
*state
)
3901 if (client
->communication
.inbound
.msg_type
!=
3902 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3903 client
->communication
.inbound
.msg_type
!=
3904 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3905 !client
->validated
) {
3906 WARN("[notification-thread] client attempted a command before handshake");
3911 switch (client
->communication
.inbound
.msg_type
) {
3912 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3914 ret
= client_handle_message_unknown(client
, state
);
3917 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3919 ret
= client_handle_message_handshake(client
, state
);
3922 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3923 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3925 ret
= client_handle_message_subscription(client
,
3926 client
->communication
.inbound
.msg_type
, state
);
3936 /* Incoming data from client. */
3937 int handle_notification_thread_client_in(
3938 struct notification_thread_state
*state
, int socket
)
3941 struct notification_client
*client
;
3946 client
= get_client_from_socket(socket
, state
);
3948 /* Internal error, abort. */
3953 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3954 client
->communication
.inbound
.bytes_to_receive
;
3955 if (client
->communication
.inbound
.expect_creds
) {
3956 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3957 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3958 client
->communication
.inbound
.bytes_to_receive
,
3959 &client
->communication
.inbound
.creds
);
3961 client
->communication
.inbound
.expect_creds
= false;
3962 client
->communication
.inbound
.creds_received
= true;
3965 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3966 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3967 client
->communication
.inbound
.bytes_to_receive
);
3969 if (recv_ret
>= 0) {
3970 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3972 goto error_disconnect_client
;
3975 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3976 /* Message incomplete wait for more data. */
3981 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3984 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3985 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3987 client
->communication
.inbound
.fds_to_receive
,
3988 &client
->communication
.inbound
.payload
);
3991 * Fds received. non blocking fds passing is all
3994 ssize_t expected_size
;
3996 expected_size
= sizeof(int) *
3997 client
->communication
.inbound
3999 assert(ret
== expected_size
);
4000 client
->communication
.inbound
.fds_to_receive
= 0;
4001 } else if (ret
== 0) {
4002 /* Received nothing. */
4006 goto error_disconnect_client
;
4010 /* At this point the message is complete.*/
4011 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4012 client
->communication
.inbound
.fds_to_receive
== 0);
4013 ret
= client_dispatch_message(client
, state
);
4016 * Only returns an error if this client must be
4019 goto error_disconnect_client
;
4026 error_disconnect_client
:
4027 ret
= notification_thread_client_disconnect(client
, state
);
4031 /* Client ready to receive outgoing data. */
4032 int handle_notification_thread_client_out(
4033 struct notification_thread_state
*state
, int socket
)
4036 struct notification_client
*client
;
4037 enum client_transmission_status transmission_status
;
4040 client
= get_client_from_socket(socket
, state
);
4042 /* Internal error, abort. */
4047 pthread_mutex_lock(&client
->lock
);
4048 transmission_status
= client_flush_outgoing_queue(client
);
4049 pthread_mutex_unlock(&client
->lock
);
4051 ret
= client_handle_transmission_status(
4052 client
, transmission_status
, state
);
4062 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4063 const struct channel_state_sample
*sample
,
4064 uint64_t buffer_capacity
)
4066 bool result
= false;
4068 enum lttng_condition_type condition_type
;
4069 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4070 condition
, struct lttng_condition_buffer_usage
,
4073 if (use_condition
->threshold_bytes
.set
) {
4074 threshold
= use_condition
->threshold_bytes
.value
;
4077 * Threshold was expressed as a ratio.
4079 * TODO the threshold (in bytes) of conditions expressed
4080 * as a ratio of total buffer size could be cached to
4081 * forego this double-multiplication or it could be performed
4082 * as fixed-point math.
4084 * Note that caching should accommodates the case where the
4085 * condition applies to multiple channels (i.e. don't assume
4086 * that all channels matching my_chann* have the same size...)
4088 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4089 (double) buffer_capacity
);
4092 condition_type
= lttng_condition_get_type(condition
);
4093 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4094 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4095 threshold
, sample
->highest_usage
);
4098 * The low condition should only be triggered once _all_ of the
4099 * streams in a channel have gone below the "low" threshold.
4101 if (sample
->highest_usage
<= threshold
) {
4105 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4106 threshold
, sample
->highest_usage
);
4109 * For high buffer usage scenarios, we want to trigger whenever
4110 * _any_ of the streams has reached the "high" threshold.
4112 if (sample
->highest_usage
>= threshold
) {
4121 bool evaluate_session_consumed_size_condition(
4122 const struct lttng_condition
*condition
,
4123 uint64_t session_consumed_size
)
4126 const struct lttng_condition_session_consumed_size
*size_condition
=
4127 container_of(condition
,
4128 struct lttng_condition_session_consumed_size
,
4131 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4132 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4133 threshold
, session_consumed_size
);
4134 return session_consumed_size
>= threshold
;
4138 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4139 struct lttng_evaluation
**evaluation
,
4140 const struct notification_thread_state
*state
,
4141 const struct channel_state_sample
*previous_sample
,
4142 const struct channel_state_sample
*latest_sample
,
4143 uint64_t previous_session_consumed_total
,
4144 uint64_t latest_session_consumed_total
,
4145 struct channel_info
*channel_info
)
4148 enum lttng_condition_type condition_type
;
4149 const bool previous_sample_available
= !!previous_sample
;
4150 bool previous_sample_result
= false;
4151 bool latest_sample_result
;
4153 condition_type
= lttng_condition_get_type(condition
);
4155 switch (condition_type
) {
4156 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4157 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4158 if (caa_likely(previous_sample_available
)) {
4159 previous_sample_result
=
4160 evaluate_buffer_usage_condition(condition
,
4161 previous_sample
, channel_info
->capacity
);
4163 latest_sample_result
= evaluate_buffer_usage_condition(
4164 condition
, latest_sample
,
4165 channel_info
->capacity
);
4167 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4168 if (caa_likely(previous_sample_available
)) {
4169 previous_sample_result
=
4170 evaluate_session_consumed_size_condition(
4172 previous_session_consumed_total
);
4174 latest_sample_result
=
4175 evaluate_session_consumed_size_condition(
4177 latest_session_consumed_total
);
4180 /* Unknown condition type; internal error. */
4184 if (!latest_sample_result
||
4185 (previous_sample_result
== latest_sample_result
)) {
4187 * Only trigger on a condition evaluation transition.
4189 * NOTE: This edge-triggered logic may not be appropriate for
4190 * future condition types.
4195 if (!evaluation
|| !latest_sample_result
) {
4199 switch (condition_type
) {
4200 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4201 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4202 *evaluation
= lttng_evaluation_buffer_usage_create(
4204 latest_sample
->highest_usage
,
4205 channel_info
->capacity
);
4207 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4208 *evaluation
= lttng_evaluation_session_consumed_size_create(
4209 latest_session_consumed_total
);
4224 int client_notification_overflow(struct notification_client
*client
)
4227 const struct lttng_notification_channel_message msg
= {
4228 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4231 ASSERT_LOCKED(client
->lock
);
4233 DBG("Dropping notification addressed to client (socket fd = %i)",
4235 if (client
->communication
.outbound
.dropped_notification
) {
4237 * The client already has a "notification dropped" message
4238 * in its outgoing queue. Nothing to do since all
4239 * of those messages are coalesced.
4244 client
->communication
.outbound
.dropped_notification
= true;
4245 ret
= lttng_dynamic_buffer_append(
4246 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4249 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4256 static int client_handle_transmission_status_wrapper(
4257 struct notification_client
*client
,
4258 enum client_transmission_status status
,
4261 return client_handle_transmission_status(client
, status
,
4262 (struct notification_thread_state
*) user_data
);
4266 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4267 const struct lttng_evaluation
*evaluation
,
4268 struct notification_client_list
* client_list
,
4269 struct notification_thread_state
*state
,
4270 uid_t object_uid
, gid_t object_gid
)
4272 const struct lttng_credentials creds
= {
4273 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4274 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4277 return notification_client_list_send_evaluation(client_list
,
4278 trigger
, evaluation
,
4280 client_handle_transmission_status_wrapper
, state
);
4284 * Permission checks relative to notification channel clients are performed
4285 * here. Notice how object, client, and trigger credentials are involved in
4288 * The `object` credentials are the credentials associated with the "subject"
4289 * of a condition. For instance, a `rotation completed` condition applies
4290 * to a session. When that condition is met, it will produce an evaluation
4291 * against a session. Hence, in this case, the `object` credentials are the
4292 * credentials of the "subject" session.
4294 * The `trigger` credentials are the credentials of the user that registered the
4297 * The `client` credentials are the credentials of the user that created a given
4298 * notification channel.
4300 * In terms of visibility, it is expected that non-privilieged users can only
4301 * register triggers against "their" objects (their own sessions and
4302 * applications they are allowed to interact with). They can then open a
4303 * notification channel and subscribe to notifications associated with those
4306 * As for privilieged users, they can register triggers against the objects of
4307 * other users. They can then subscribe to the notifications associated to their
4308 * triggers. Privilieged users _can't_ subscribe to the notifications of
4309 * triggers owned by other users; they must create their own triggers.
4311 * This is more a concern of usability than security. It would be difficult for
4312 * a root user reliably subscribe to a specific set of conditions without
4313 * interference from external users (those could, for instance, unregister
4317 int notification_client_list_send_evaluation(
4318 struct notification_client_list
*client_list
,
4319 const struct lttng_trigger
*trigger
,
4320 const struct lttng_evaluation
*evaluation
,
4321 const struct lttng_credentials
*source_object_creds
,
4322 report_client_transmission_result_cb client_report
,
4326 struct lttng_payload msg_payload
;
4327 struct notification_client_list_element
*client_list_element
, *tmp
;
4328 const struct lttng_notification notification
= {
4329 .trigger
= (struct lttng_trigger
*) trigger
,
4330 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4332 struct lttng_notification_channel_message msg_header
= {
4333 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4335 const struct lttng_credentials
*trigger_creds
=
4336 lttng_trigger_get_credentials(trigger
);
4338 lttng_payload_init(&msg_payload
);
4340 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4341 sizeof(msg_header
));
4346 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4348 ERR("[notification-thread] Failed to serialize notification");
4353 /* Update payload size. */
4354 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4355 ->size
= (uint32_t)(
4356 msg_payload
.buffer
.size
- sizeof(msg_header
));
4358 /* Update the payload number of fds. */
4360 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4361 &msg_payload
, 0, -1);
4363 ((struct lttng_notification_channel_message
*)
4364 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4365 lttng_payload_view_get_fd_handle_count(&pv
);
4368 pthread_mutex_lock(&client_list
->lock
);
4369 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4370 &client_list
->clients_list
, node
) {
4371 enum client_transmission_status transmission_status
;
4372 struct notification_client
*client
=
4373 client_list_element
->client
;
4376 pthread_mutex_lock(&client
->lock
);
4377 if (!client
->communication
.active
) {
4379 * Skip inactive client (protocol error or
4382 DBG("Skipping client at it is marked as inactive");
4386 if (source_object_creds
) {
4387 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4388 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4391 * Client is not allowed to monitor this
4394 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4399 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
)) {
4400 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4404 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4405 client
->socket
, msg_payload
.buffer
.size
);
4407 if (client_has_outbound_data_left(client
)) {
4409 * Outgoing data is already buffered for this client;
4410 * drop the notification and enqueue a "dropped
4411 * notification" message if this is the first dropped
4412 * notification since the socket spilled-over to the
4415 ret
= client_notification_overflow(client
);
4422 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4428 transmission_status
= client_flush_outgoing_queue(client
);
4429 pthread_mutex_unlock(&client
->lock
);
4430 ret
= client_report(client
, transmission_status
, user_data
);
4433 goto end_unlock_list
;
4439 pthread_mutex_unlock(&client
->lock
);
4442 goto end_unlock_list
;
4448 pthread_mutex_unlock(&client_list
->lock
);
4450 lttng_payload_reset(&msg_payload
);
4455 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4456 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4460 struct lttng_event_notifier_notification
*notification
= NULL
;
4461 char *capture_buffer
= NULL
;
4462 size_t capture_buffer_size
;
4463 void *reception_buffer
;
4464 size_t reception_size
;
4466 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4467 struct lttng_kernel_event_notifier_notification kernel_notification
;
4469 /* Init lttng_event_notifier_notification */
4471 case LTTNG_DOMAIN_UST
:
4472 reception_buffer
= (void *) &ust_notification
;
4473 reception_size
= sizeof(ust_notification
);
4475 case LTTNG_DOMAIN_KERNEL
:
4476 reception_buffer
= (void *) &kernel_notification
;
4477 reception_size
= sizeof(kernel_notification
);
4484 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4485 * ensuring that read/write of tracer notifications are atomic.
4487 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4489 if (ret
!= reception_size
) {
4490 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4491 notification_pipe_read_fd
, reception_size
, ret
);
4497 case LTTNG_DOMAIN_UST
:
4498 token
= ust_notification
.token
;
4499 capture_buffer_size
= ust_notification
.capture_buf_size
;
4501 case LTTNG_DOMAIN_KERNEL
:
4502 token
= kernel_notification
.token
;
4503 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4509 if (capture_buffer_size
== 0) {
4510 capture_buffer
= NULL
;
4514 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4515 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",
4516 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4520 capture_buffer
= zmalloc(capture_buffer_size
);
4521 if (!capture_buffer
) {
4522 ERR("[notification-thread] Failed to allocate capture buffer");
4526 /* Fetch additional payload (capture). */
4527 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4528 if (ret
!= capture_buffer_size
) {
4529 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4530 notification_pipe_read_fd
);
4535 notification
= lttng_event_notifier_notification_create(token
, domain
,
4536 capture_buffer
, capture_buffer_size
);
4537 if (notification
== NULL
) {
4542 * Ownership transfered to the lttng_event_notifier_notification object.
4544 capture_buffer
= NULL
;
4547 free(capture_buffer
);
4548 return notification
;
4552 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4553 struct lttng_event_notifier_notification
*notification
)
4555 struct cds_lfht_node
*node
;
4556 struct cds_lfht_iter iter
;
4557 struct notification_trigger_tokens_ht_element
*element
;
4558 struct lttng_evaluation
*evaluation
= NULL
;
4559 enum action_executor_status executor_status
;
4560 struct notification_client_list
*client_list
= NULL
;
4562 unsigned int capture_count
= 0;
4564 /* Find triggers associated with this token. */
4566 cds_lfht_lookup(state
->trigger_tokens_ht
,
4567 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4568 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4569 node
= cds_lfht_iter_get_node(&iter
);
4570 if (caa_unlikely(!node
)) {
4572 * This is not an error, slow consumption of the tracer
4573 * notifications can lead to situations where a trigger is
4574 * removed but we still get tracer notifications matching a
4575 * trigger that no longer exists.
4581 element
= caa_container_of(node
,
4582 struct notification_trigger_tokens_ht_element
,
4585 if (lttng_condition_event_rule_matches_get_capture_descriptor_count(
4586 lttng_trigger_get_const_condition(element
->trigger
),
4587 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4588 ERR("Failed to get capture count");
4593 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4594 ERR("Expected capture but capture buffer is null");
4599 evaluation
= lttng_evaluation_event_rule_matches_create(
4600 container_of(lttng_trigger_get_const_condition(
4602 struct lttng_condition_event_rule_matches
,
4604 notification
->capture_buffer
,
4605 notification
->capture_buf_size
, false);
4607 if (evaluation
== NULL
) {
4608 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
4612 client_list
= get_client_list_from_condition(state
,
4613 lttng_trigger_get_const_condition(element
->trigger
));
4614 executor_status
= action_executor_enqueue_trigger(state
->executor
,
4615 element
->trigger
, evaluation
, NULL
, client_list
);
4616 switch (executor_status
) {
4617 case ACTION_EXECUTOR_STATUS_OK
:
4620 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4622 struct notification_client_list_element
*client_list_element
,
4626 * Not a fatal error; this is expected and simply means the
4627 * executor has too much work queued already.
4631 /* No clients subscribed to notifications for this trigger. */
4636 /* Warn clients that a notification (or more) was dropped. */
4637 pthread_mutex_lock(&client_list
->lock
);
4638 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4639 &client_list
->clients_list
, node
) {
4640 enum client_transmission_status transmission_status
;
4641 struct notification_client
*client
=
4642 client_list_element
->client
;
4644 pthread_mutex_lock(&client
->lock
);
4645 ret
= client_notification_overflow(client
);
4651 transmission_status
=
4652 client_flush_outgoing_queue(client
);
4653 ret
= client_handle_transmission_status(
4654 client
, transmission_status
, state
);
4660 pthread_mutex_unlock(&client
->lock
);
4666 pthread_mutex_unlock(&client_list
->lock
);
4669 case ACTION_EXECUTOR_STATUS_INVALID
:
4670 case ACTION_EXECUTOR_STATUS_ERROR
:
4671 /* Fatal error, shut down everything. */
4672 ERR("Fatal error encoutered while enqueuing action to the action executor");
4676 /* Unhandled error. */
4681 notification_client_list_put(client_list
);
4688 int handle_one_event_notifier_notification(
4689 struct notification_thread_state
*state
,
4690 int pipe
, enum lttng_domain_type domain
)
4693 struct lttng_event_notifier_notification
*notification
= NULL
;
4695 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4696 if (notification
== NULL
) {
4697 /* Reception failed, don't consider it fatal. */
4698 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4699 pipe
, lttng_domain_type_str(domain
));
4703 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4705 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4706 pipe
, lttng_domain_type_str(domain
));
4711 lttng_event_notifier_notification_destroy(notification
);
4715 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4716 int pipe
, enum lttng_domain_type domain
)
4718 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4721 int handle_notification_thread_channel_sample(
4722 struct notification_thread_state
*state
, int pipe
,
4723 enum lttng_domain_type domain
)
4726 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4727 struct channel_info
*channel_info
;
4728 struct cds_lfht_node
*node
;
4729 struct cds_lfht_iter iter
;
4730 struct lttng_channel_trigger_list
*trigger_list
;
4731 struct lttng_trigger_list_element
*trigger_list_element
;
4732 bool previous_sample_available
= false;
4733 struct channel_state_sample previous_sample
, latest_sample
;
4734 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4735 struct lttng_credentials channel_creds
;
4738 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4739 * ensuring that read/write of sampling messages are atomic.
4741 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4742 if (ret
!= sizeof(sample_msg
)) {
4743 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4750 latest_sample
.key
.key
= sample_msg
.key
;
4751 latest_sample
.key
.domain
= domain
;
4752 latest_sample
.highest_usage
= sample_msg
.highest
;
4753 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4754 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4758 /* Retrieve the channel's informations */
4759 cds_lfht_lookup(state
->channels_ht
,
4760 hash_channel_key(&latest_sample
.key
),
4764 node
= cds_lfht_iter_get_node(&iter
);
4765 if (caa_unlikely(!node
)) {
4767 * Not an error since the consumer can push a sample to the pipe
4768 * and the rest of the session daemon could notify us of the
4769 * channel's destruction before we get a chance to process that
4772 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4773 latest_sample
.key
.key
,
4774 lttng_domain_type_str(domain
));
4777 channel_info
= caa_container_of(node
, struct channel_info
,
4779 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4781 latest_sample
.key
.key
,
4782 channel_info
->session_info
->name
,
4783 latest_sample
.highest_usage
,
4784 latest_sample
.lowest_usage
,
4785 latest_sample
.channel_total_consumed
);
4787 previous_session_consumed_total
=
4788 channel_info
->session_info
->consumed_data_size
;
4790 /* Retrieve the channel's last sample, if it exists, and update it. */
4791 cds_lfht_lookup(state
->channel_state_ht
,
4792 hash_channel_key(&latest_sample
.key
),
4793 match_channel_state_sample
,
4796 node
= cds_lfht_iter_get_node(&iter
);
4797 if (caa_likely(node
)) {
4798 struct channel_state_sample
*stored_sample
;
4800 /* Update the sample stored. */
4801 stored_sample
= caa_container_of(node
,
4802 struct channel_state_sample
,
4803 channel_state_ht_node
);
4805 memcpy(&previous_sample
, stored_sample
,
4806 sizeof(previous_sample
));
4807 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4808 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4809 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4810 previous_sample_available
= true;
4812 latest_session_consumed_total
=
4813 previous_session_consumed_total
+
4814 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4817 * This is the channel's first sample, allocate space for and
4818 * store the new sample.
4820 struct channel_state_sample
*stored_sample
;
4822 stored_sample
= zmalloc(sizeof(*stored_sample
));
4823 if (!stored_sample
) {
4828 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4829 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4830 cds_lfht_add(state
->channel_state_ht
,
4831 hash_channel_key(&stored_sample
->key
),
4832 &stored_sample
->channel_state_ht_node
);
4834 latest_session_consumed_total
=
4835 previous_session_consumed_total
+
4836 latest_sample
.channel_total_consumed
;
4839 channel_info
->session_info
->consumed_data_size
=
4840 latest_session_consumed_total
;
4842 /* Find triggers associated with this channel. */
4843 cds_lfht_lookup(state
->channel_triggers_ht
,
4844 hash_channel_key(&latest_sample
.key
),
4845 match_channel_trigger_list
,
4848 node
= cds_lfht_iter_get_node(&iter
);
4849 if (caa_likely(!node
)) {
4853 channel_creds
= (typeof(channel_creds
)) {
4854 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4855 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4858 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4859 channel_triggers_ht_node
);
4860 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4862 const struct lttng_condition
*condition
;
4863 struct lttng_trigger
*trigger
;
4864 struct notification_client_list
*client_list
= NULL
;
4865 struct lttng_evaluation
*evaluation
= NULL
;
4866 enum action_executor_status executor_status
;
4869 trigger
= trigger_list_element
->trigger
;
4870 condition
= lttng_trigger_get_const_condition(trigger
);
4874 * Check if any client is subscribed to the result of this
4877 client_list
= get_client_list_from_condition(state
, condition
);
4879 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4880 previous_sample_available
? &previous_sample
: NULL
,
4882 previous_session_consumed_total
,
4883 latest_session_consumed_total
,
4885 if (caa_unlikely(ret
)) {
4889 if (caa_likely(!evaluation
)) {
4894 * Ownership of `evaluation` transferred to the action executor
4895 * no matter the result.
4897 executor_status
= action_executor_enqueue_trigger(
4898 state
->executor
, trigger
, evaluation
,
4899 &channel_creds
, client_list
);
4901 switch (executor_status
) {
4902 case ACTION_EXECUTOR_STATUS_OK
:
4904 case ACTION_EXECUTOR_STATUS_ERROR
:
4905 case ACTION_EXECUTOR_STATUS_INVALID
:
4907 * TODO Add trigger identification (name/id) when
4908 * it is added to the API.
4910 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4913 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4915 * TODO Add trigger identification (name/id) when
4916 * it is added to the API.
4918 * Not a fatal error.
4920 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4928 notification_client_list_put(client_list
);
4929 if (caa_unlikely(ret
)) {