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 /* Internal error: add to triggers_ht should have failed. */
2734 goto error_free_ht_element
;
2737 /* From this point consider the trigger registered. */
2738 lttng_trigger_set_as_registered(trigger
);
2741 * Some triggers might need a tracer notifier depending on its
2742 * condition and actions.
2744 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2745 enum lttng_error_code error_code
;
2747 error_code
= setup_tracer_notifier(state
, trigger
);
2748 if (error_code
!= LTTNG_OK
) {
2749 notif_thread_state_remove_trigger_ht_elem(state
,
2750 trigger_ht_element
);
2751 if (error_code
== LTTNG_ERR_NOMEM
) {
2754 *cmd_result
= error_code
;
2758 goto error_free_ht_element
;
2763 * The rest only applies to triggers that have a "notify" action.
2764 * It is not skipped as this is the only action type currently
2767 if (is_trigger_action_notify(trigger
)) {
2769 * Find or create the client list of this condition. It may
2770 * already be present if another trigger is already registered
2771 * with the same condition.
2773 client_list
= get_client_list_from_condition(state
, condition
);
2776 * No client list for this condition yet. We create new
2777 * one and build it up.
2779 client_list
= notification_client_list_create(state
, condition
);
2781 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2782 goto error_free_ht_element
;
2786 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2788 pthread_mutex_lock(&client_list
->lock
);
2789 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2790 pthread_mutex_unlock(&client_list
->lock
);
2794 * Ownership of the trigger and of its wrapper was transfered to
2795 * the triggers_ht. Same for token ht element if necessary.
2797 trigger_ht_element
= NULL
;
2798 free_trigger
= false;
2800 switch (get_condition_binding_object(condition
)) {
2801 case LTTNG_OBJECT_TYPE_SESSION
:
2802 /* Add the trigger to the list if it matches a known session. */
2803 ret
= bind_trigger_to_matching_session(trigger
, state
);
2805 goto error_free_ht_element
;
2808 case LTTNG_OBJECT_TYPE_CHANNEL
:
2810 * Add the trigger to list of triggers bound to the channels
2813 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2815 goto error_free_ht_element
;
2818 case LTTNG_OBJECT_TYPE_NONE
:
2821 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2823 goto error_free_ht_element
;
2827 * The new trigger's condition must be evaluated against the current
2830 * In the case of `notify` action, nothing preventing clients from
2831 * subscribing to a condition before the corresponding trigger is
2832 * registered, we have to evaluate this new condition right away.
2834 * At some point, we were waiting for the next "evaluation" (e.g. on
2835 * reception of a channel sample) to evaluate this new condition, but
2838 * The reason it was broken is that waiting for the next sample
2839 * does not allow us to properly handle transitions for edge-triggered
2842 * Consider this example: when we handle a new channel sample, we
2843 * evaluate each conditions twice: once with the previous state, and
2844 * again with the newest state. We then use those two results to
2845 * determine whether a state change happened: a condition was false and
2846 * became true. If a state change happened, we have to notify clients.
2848 * Now, if a client subscribes to a given notification and registers
2849 * a trigger *after* that subscription, we have to make sure the
2850 * condition is evaluated at this point while considering only the
2851 * current state. Otherwise, the next evaluation cycle may only see
2852 * that the evaluations remain the same (true for samples n-1 and n) and
2853 * the client will never know that the condition has been met.
2855 switch (get_condition_binding_object(condition
)) {
2856 case LTTNG_OBJECT_TYPE_SESSION
:
2857 ret
= evaluate_session_condition_for_client(condition
, state
,
2858 &evaluation
, &object_uid
,
2860 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2861 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2863 case LTTNG_OBJECT_TYPE_CHANNEL
:
2864 ret
= evaluate_channel_condition_for_client(condition
, state
,
2865 &evaluation
, &object_uid
,
2867 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2868 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2870 case LTTNG_OBJECT_TYPE_NONE
:
2873 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2881 goto error_free_ht_element
;
2884 DBG("Newly registered trigger's condition evaluated to %s",
2885 evaluation
? "true" : "false");
2887 /* Evaluation yielded nothing. Normal exit. */
2893 * Ownership of `evaluation` transferred to the action executor
2894 * no matter the result.
2896 executor_status
= action_executor_enqueue_trigger(state
->executor
,
2897 trigger
, evaluation
, &object_creds
, client_list
);
2899 switch (executor_status
) {
2900 case ACTION_EXECUTOR_STATUS_OK
:
2902 case ACTION_EXECUTOR_STATUS_ERROR
:
2903 case ACTION_EXECUTOR_STATUS_INVALID
:
2905 * TODO Add trigger identification (name/id) when
2906 * it is added to the API.
2908 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2910 goto error_free_ht_element
;
2911 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2913 * TODO Add trigger identification (name/id) when
2914 * it is added to the API.
2916 * Not a fatal error.
2918 WARN("No space left when enqueuing action associated to newly registered trigger");
2926 *cmd_result
= LTTNG_OK
;
2927 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2928 trigger_name
, trigger_tracer_token
);
2931 error_free_ht_element
:
2932 if (trigger_ht_element
) {
2933 /* Delayed removal due to RCU constraint on delete. */
2934 call_rcu(&trigger_ht_element
->rcu_node
,
2935 free_lttng_trigger_ht_element_rcu
);
2940 * Other objects might have a reference to the trigger, mark it
2943 lttng_trigger_set_as_unregistered(trigger
);
2944 lttng_trigger_destroy(trigger
);
2952 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2954 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2959 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2961 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2966 void teardown_tracer_notifier(struct notification_thread_state
*state
,
2967 const struct lttng_trigger
*trigger
)
2969 struct cds_lfht_iter iter
;
2970 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2972 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
2973 trigger_tokens_ht_element
, node
) {
2975 if (!lttng_trigger_is_equal(trigger
,
2976 trigger_tokens_ht_element
->trigger
)) {
2980 event_notifier_error_accounting_unregister_event_notifier(
2981 trigger_tokens_ht_element
->trigger
);
2983 /* TODO talk to all app and remove it */
2984 DBG("[notification-thread] Removed trigger from tokens_ht");
2985 cds_lfht_del(state
->trigger_tokens_ht
,
2986 &trigger_tokens_ht_element
->node
);
2988 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2989 free_notification_trigger_tokens_ht_element_rcu
);
2996 int handle_notification_thread_command_unregister_trigger(
2997 struct notification_thread_state
*state
,
2998 const struct lttng_trigger
*trigger
,
2999 enum lttng_error_code
*_cmd_reply
)
3001 struct cds_lfht_iter iter
;
3002 struct cds_lfht_node
*triggers_ht_node
;
3003 struct lttng_channel_trigger_list
*trigger_list
;
3004 struct notification_client_list
*client_list
;
3005 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
3006 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
3008 enum lttng_error_code cmd_reply
;
3012 cds_lfht_lookup(state
->triggers_ht
,
3013 lttng_condition_hash(condition
),
3017 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3018 if (!triggers_ht_node
) {
3019 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3022 cmd_reply
= LTTNG_OK
;
3025 trigger_ht_element
= caa_container_of(triggers_ht_node
,
3026 struct lttng_trigger_ht_element
, node
);
3028 /* Remove trigger from channel_triggers_ht. */
3029 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
3030 channel_triggers_ht_node
) {
3031 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3033 cds_list_for_each_entry_safe(trigger_element
, tmp
,
3034 &trigger_list
->list
, node
) {
3035 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3039 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
3040 cds_list_del(&trigger_element
->node
);
3041 /* A trigger can only appear once per channel */
3046 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3047 teardown_tracer_notifier(state
, trigger
);
3050 if (is_trigger_action_notify(trigger
)) {
3052 * Remove and release the client list from
3053 * notification_trigger_clients_ht.
3055 client_list
= get_client_list_from_condition(state
, condition
);
3056 assert(client_list
);
3058 pthread_mutex_lock(&client_list
->lock
);
3059 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3060 pthread_mutex_unlock(&client_list
->lock
);
3062 /* Put new reference and the hashtable's reference. */
3063 notification_client_list_put(client_list
);
3064 notification_client_list_put(client_list
);
3068 /* Remove trigger from triggers_ht. */
3069 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3071 /* Release the ownership of the trigger. */
3072 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3073 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3077 *_cmd_reply
= cmd_reply
;
3082 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3083 int handle_notification_thread_command(
3084 struct notification_thread_handle
*handle
,
3085 struct notification_thread_state
*state
)
3089 struct notification_thread_command
*cmd
;
3091 /* Read the event pipe to put it back into a quiescent state. */
3092 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
3094 if (ret
!= sizeof(counter
)) {
3098 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3099 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3100 struct notification_thread_command
, cmd_list_node
);
3101 cds_list_del(&cmd
->cmd_list_node
);
3102 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3104 DBG("[notification-thread] Received `%s` command",
3105 notification_command_type_str(cmd
->type
));
3106 switch (cmd
->type
) {
3107 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3108 ret
= handle_notification_thread_command_register_trigger(state
,
3109 cmd
->parameters
.register_trigger
.trigger
,
3110 cmd
->parameters
.register_trigger
.is_trigger_anonymous
,
3113 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3114 ret
= handle_notification_thread_command_unregister_trigger(
3116 cmd
->parameters
.unregister_trigger
.trigger
,
3119 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3120 ret
= handle_notification_thread_command_add_channel(
3122 cmd
->parameters
.add_channel
.session
.name
,
3123 cmd
->parameters
.add_channel
.session
.uid
,
3124 cmd
->parameters
.add_channel
.session
.gid
,
3125 cmd
->parameters
.add_channel
.channel
.name
,
3126 cmd
->parameters
.add_channel
.channel
.domain
,
3127 cmd
->parameters
.add_channel
.channel
.key
,
3128 cmd
->parameters
.add_channel
.channel
.capacity
,
3131 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3132 ret
= handle_notification_thread_command_remove_channel(
3133 state
, cmd
->parameters
.remove_channel
.key
,
3134 cmd
->parameters
.remove_channel
.domain
,
3137 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3138 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3139 ret
= handle_notification_thread_command_session_rotation(
3142 cmd
->parameters
.session_rotation
.session_name
,
3143 cmd
->parameters
.session_rotation
.uid
,
3144 cmd
->parameters
.session_rotation
.gid
,
3145 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3146 cmd
->parameters
.session_rotation
.location
,
3149 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3150 ret
= handle_notification_thread_command_add_tracer_event_source(
3152 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3153 cmd
->parameters
.tracer_event_source
.domain
,
3156 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3157 ret
= handle_notification_thread_command_remove_tracer_event_source(
3159 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3162 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3164 struct lttng_triggers
*triggers
= NULL
;
3166 ret
= handle_notification_thread_command_list_triggers(
3169 cmd
->parameters
.list_triggers
.uid
,
3172 cmd
->reply
.list_triggers
.triggers
= triggers
;
3176 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3177 cmd
->reply_code
= LTTNG_OK
;
3180 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3182 struct lttng_trigger
*trigger
= NULL
;
3184 ret
= handle_notification_thread_command_get_trigger(state
,
3185 cmd
->parameters
.get_trigger
.trigger
, &trigger
,
3187 cmd
->reply
.get_trigger
.trigger
= trigger
;
3190 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3192 const enum client_transmission_status client_status
=
3193 cmd
->parameters
.client_communication_update
3195 const notification_client_id client_id
=
3196 cmd
->parameters
.client_communication_update
.id
;
3197 struct notification_client
*client
;
3200 client
= get_client_from_id(client_id
, state
);
3204 * Client error was probably already picked-up by the
3205 * notification thread or it has disconnected
3206 * gracefully while this command was queued.
3208 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3212 ret
= client_handle_transmission_status(
3213 client
, client_status
, state
);
3219 ERR("[notification-thread] Unknown internal command received");
3227 if (cmd
->is_async
) {
3231 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3235 /* Wake-up and return a fatal error to the calling thread. */
3236 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3237 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3239 /* Indicate a fatal error to the caller. */
3244 int socket_set_non_blocking(int socket
)
3248 /* Set the pipe as non-blocking. */
3249 ret
= fcntl(socket
, F_GETFL
, 0);
3251 PERROR("fcntl get socket flags");
3256 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3258 PERROR("fcntl set O_NONBLOCK socket flag");
3261 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3267 int client_reset_inbound_state(struct notification_client
*client
)
3272 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3274 client
->communication
.inbound
.bytes_to_receive
=
3275 sizeof(struct lttng_notification_channel_message
);
3276 client
->communication
.inbound
.msg_type
=
3277 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3278 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3279 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3280 ret
= lttng_dynamic_buffer_set_size(
3281 &client
->communication
.inbound
.payload
.buffer
,
3282 client
->communication
.inbound
.bytes_to_receive
);
3287 int handle_notification_thread_client_connect(
3288 struct notification_thread_state
*state
)
3291 struct notification_client
*client
;
3293 DBG("[notification-thread] Handling new notification channel client connection");
3295 client
= zmalloc(sizeof(*client
));
3302 pthread_mutex_init(&client
->lock
, NULL
);
3303 client
->id
= state
->next_notification_client_id
++;
3304 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3305 lttng_payload_init(&client
->communication
.inbound
.payload
);
3306 lttng_payload_init(&client
->communication
.outbound
.payload
);
3307 client
->communication
.inbound
.expect_creds
= true;
3309 ret
= client_reset_inbound_state(client
);
3311 ERR("[notification-thread] Failed to reset client communication's inbound state");
3316 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3318 ERR("[notification-thread] Failed to accept new notification channel client connection");
3323 client
->socket
= ret
;
3325 ret
= socket_set_non_blocking(client
->socket
);
3327 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3331 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3333 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3338 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3339 LPOLLIN
| LPOLLERR
|
3340 LPOLLHUP
| LPOLLRDHUP
);
3342 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3346 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3350 cds_lfht_add(state
->client_socket_ht
,
3351 hash_client_socket(client
->socket
),
3352 &client
->client_socket_ht_node
);
3353 cds_lfht_add(state
->client_id_ht
,
3354 hash_client_id(client
->id
),
3355 &client
->client_id_ht_node
);
3361 notification_client_destroy(client
, state
);
3366 * RCU read-lock must be held by the caller.
3367 * Client lock must _not_ be held by the caller.
3370 int notification_thread_client_disconnect(
3371 struct notification_client
*client
,
3372 struct notification_thread_state
*state
)
3375 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3377 /* Acquire the client lock to disable its communication atomically. */
3378 pthread_mutex_lock(&client
->lock
);
3379 client
->communication
.active
= false;
3380 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3381 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3382 pthread_mutex_unlock(&client
->lock
);
3384 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3386 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3390 /* Release all conditions to which the client was subscribed. */
3391 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3392 &client
->condition_list
, node
) {
3393 (void) notification_thread_client_unsubscribe(client
,
3394 condition_list_element
->condition
, state
, NULL
);
3398 * Client no longer accessible to other threads (through the
3401 notification_client_destroy(client
, state
);
3405 int handle_notification_thread_client_disconnect(
3406 int client_socket
, struct notification_thread_state
*state
)
3409 struct notification_client
*client
;
3412 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3414 client
= get_client_from_socket(client_socket
, state
);
3416 /* Internal state corruption, fatal error. */
3417 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3423 ret
= notification_thread_client_disconnect(client
, state
);
3429 int handle_notification_thread_client_disconnect_all(
3430 struct notification_thread_state
*state
)
3432 struct cds_lfht_iter iter
;
3433 struct notification_client
*client
;
3434 bool error_encoutered
= false;
3437 DBG("[notification-thread] Closing all client connections");
3438 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3439 client_socket_ht_node
) {
3442 ret
= notification_thread_client_disconnect(
3445 error_encoutered
= true;
3449 return error_encoutered
? 1 : 0;
3452 int handle_notification_thread_trigger_unregister_all(
3453 struct notification_thread_state
*state
)
3455 bool error_occurred
= false;
3456 struct cds_lfht_iter iter
;
3457 struct lttng_trigger_ht_element
*trigger_ht_element
;
3460 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3462 int ret
= handle_notification_thread_command_unregister_trigger(
3463 state
, trigger_ht_element
->trigger
, NULL
);
3465 error_occurred
= true;
3469 return error_occurred
? -1 : 0;
3473 int client_handle_transmission_status(
3474 struct notification_client
*client
,
3475 enum client_transmission_status transmission_status
,
3476 struct notification_thread_state
*state
)
3480 switch (transmission_status
) {
3481 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3482 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3483 CLIENT_POLL_MASK_IN
);
3489 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3491 * We want to be notified whenever there is buffer space
3492 * available to send the rest of the payload.
3494 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3495 CLIENT_POLL_MASK_IN_OUT
);
3500 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3501 ret
= notification_thread_client_disconnect(client
, state
);
3506 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3516 /* Client lock must be acquired by caller. */
3518 enum client_transmission_status
client_flush_outgoing_queue(
3519 struct notification_client
*client
)
3522 size_t to_send_count
;
3523 enum client_transmission_status status
;
3524 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3525 &client
->communication
.outbound
.payload
, 0, -1);
3526 const int fds_to_send_count
=
3527 lttng_payload_view_get_fd_handle_count(&pv
);
3529 ASSERT_LOCKED(client
->lock
);
3531 if (!client
->communication
.active
) {
3532 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3536 if (pv
.buffer
.size
== 0) {
3538 * If both data and fds are equal to zero, we are in an invalid
3541 assert(fds_to_send_count
!= 0);
3546 to_send_count
= pv
.buffer
.size
;
3547 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3550 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3553 if ((ret
>= 0 && ret
< to_send_count
)) {
3554 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3556 to_send_count
-= max(ret
, 0);
3558 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3560 pv
.buffer
.size
- to_send_count
,
3562 ret
= lttng_dynamic_buffer_set_size(
3563 &client
->communication
.outbound
.payload
.buffer
,
3569 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3571 } else if (ret
< 0) {
3572 /* Generic error, disable the client's communication. */
3573 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3575 client
->communication
.active
= false;
3576 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3580 * No error and flushed the queue completely.
3582 * The payload buffer size is used later to
3583 * check if there is notifications queued. So albeit that the
3584 * direct caller knows that the transmission is complete, we
3585 * need to set the buffer size to zero.
3587 ret
= lttng_dynamic_buffer_set_size(
3588 &client
->communication
.outbound
.payload
.buffer
, 0);
3595 /* No fds to send, transmission is complete. */
3596 if (fds_to_send_count
== 0) {
3597 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3601 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3602 client
->socket
, &pv
);
3604 /* Generic error, disable the client's communication. */
3605 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3607 client
->communication
.active
= false;
3608 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3610 } else if (ret
== 0) {
3611 /* Nothing could be sent. */
3612 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3614 /* Fd passing is an all or nothing kind of thing. */
3615 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3617 * The payload _fd_array count is used later to
3618 * check if there is notifications queued. So although the
3619 * direct caller knows that the transmission is complete, we
3620 * need to clear the _fd_array for the queuing check.
3622 lttng_dynamic_pointer_array_clear(
3623 &client
->communication
.outbound
.payload
3628 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3629 client
->communication
.outbound
.queued_command_reply
= false;
3630 client
->communication
.outbound
.dropped_notification
= false;
3631 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3636 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3640 bool client_has_outbound_data_left(
3641 const struct notification_client
*client
)
3643 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3644 &client
->communication
.outbound
.payload
, 0, -1);
3645 const bool has_data
= pv
.buffer
.size
!= 0;
3646 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3648 return has_data
|| has_fds
;
3651 /* Client lock must _not_ be held by the caller. */
3653 int client_send_command_reply(struct notification_client
*client
,
3654 struct notification_thread_state
*state
,
3655 enum lttng_notification_channel_status status
)
3658 struct lttng_notification_channel_command_reply reply
= {
3659 .status
= (int8_t) status
,
3661 struct lttng_notification_channel_message msg
= {
3662 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3663 .size
= sizeof(reply
),
3665 char buffer
[sizeof(msg
) + sizeof(reply
)];
3666 enum client_transmission_status transmission_status
;
3668 memcpy(buffer
, &msg
, sizeof(msg
));
3669 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3670 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3672 pthread_mutex_lock(&client
->lock
);
3673 if (client
->communication
.outbound
.queued_command_reply
) {
3674 /* Protocol error. */
3678 /* Enqueue buffer to outgoing queue and flush it. */
3679 ret
= lttng_dynamic_buffer_append(
3680 &client
->communication
.outbound
.payload
.buffer
,
3681 buffer
, sizeof(buffer
));
3686 transmission_status
= client_flush_outgoing_queue(client
);
3688 if (client_has_outbound_data_left(client
)) {
3689 /* Queue could not be emptied. */
3690 client
->communication
.outbound
.queued_command_reply
= true;
3693 pthread_mutex_unlock(&client
->lock
);
3694 ret
= client_handle_transmission_status(
3695 client
, transmission_status
, state
);
3702 pthread_mutex_unlock(&client
->lock
);
3708 int client_handle_message_unknown(struct notification_client
*client
,
3709 struct notification_thread_state
*state
)
3713 * Receiving message header. The function will be called again
3714 * once the rest of the message as been received and can be
3717 const struct lttng_notification_channel_message
*msg
;
3719 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3720 msg
= (const struct lttng_notification_channel_message
*)
3721 client
->communication
.inbound
.payload
.buffer
.data
;
3723 if (msg
->size
== 0 ||
3724 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3725 ERR("[notification-thread] Invalid notification channel message: length = %u",
3731 switch (msg
->type
) {
3732 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3733 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3734 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3738 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3742 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3743 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3744 client
->communication
.inbound
.msg_type
=
3745 (enum lttng_notification_channel_message_type
) msg
->type
;
3746 ret
= lttng_dynamic_buffer_set_size(
3747 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3749 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3756 int client_handle_message_handshake(struct notification_client
*client
,
3757 struct notification_thread_state
*state
)
3760 struct lttng_notification_channel_command_handshake
*handshake_client
;
3761 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3762 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3763 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3765 const struct lttng_notification_channel_message msg_header
= {
3766 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3767 .size
= sizeof(handshake_reply
),
3769 enum lttng_notification_channel_status status
=
3770 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3771 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3773 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3774 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3775 sizeof(handshake_reply
));
3778 (struct lttng_notification_channel_command_handshake
*)
3779 client
->communication
.inbound
.payload
.buffer
3781 client
->major
= handshake_client
->major
;
3782 client
->minor
= handshake_client
->minor
;
3783 if (!client
->communication
.inbound
.creds_received
) {
3784 ERR("[notification-thread] No credentials received from client");
3789 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3790 &client
->communication
.inbound
.creds
);
3791 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3792 &client
->communication
.inbound
.creds
);
3793 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3794 client
->uid
, client
->gid
, (int) client
->major
,
3795 (int) client
->minor
);
3797 if (handshake_client
->major
!=
3798 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3799 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3802 pthread_mutex_lock(&client
->lock
);
3803 /* Outgoing queue will be flushed when the command reply is sent. */
3804 ret
= lttng_dynamic_buffer_append(
3805 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3806 sizeof(send_buffer
));
3808 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3812 client
->validated
= true;
3813 client
->communication
.active
= true;
3814 pthread_mutex_unlock(&client
->lock
);
3816 /* Set reception state to receive the next message header. */
3817 ret
= client_reset_inbound_state(client
);
3819 ERR("[notification-thread] Failed to reset client communication's inbound state");
3823 /* Flushes the outgoing queue. */
3824 ret
= client_send_command_reply(client
, state
, status
);
3826 ERR("[notification-thread] Failed to send reply to notification channel client");
3832 pthread_mutex_unlock(&client
->lock
);
3838 int client_handle_message_subscription(
3839 struct notification_client
*client
,
3840 enum lttng_notification_channel_message_type msg_type
,
3841 struct notification_thread_state
*state
)
3844 struct lttng_condition
*condition
;
3845 enum lttng_notification_channel_status status
=
3846 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3847 struct lttng_payload_view condition_view
=
3848 lttng_payload_view_from_payload(
3849 &client
->communication
.inbound
.payload
,
3851 size_t expected_condition_size
;
3854 * No need to lock client to sample the inbound state as the only
3855 * other thread accessing clients (action executor) only uses the
3858 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3859 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3860 if (ret
!= expected_condition_size
) {
3861 ERR("[notification-thread] Malformed condition received from client");
3865 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3866 ret
= notification_thread_client_subscribe(
3867 client
, condition
, state
, &status
);
3869 ret
= notification_thread_client_unsubscribe(
3870 client
, condition
, state
, &status
);
3877 /* Set reception state to receive the next message header. */
3878 ret
= client_reset_inbound_state(client
);
3880 ERR("[notification-thread] Failed to reset client communication's inbound state");
3884 ret
= client_send_command_reply(client
, state
, status
);
3886 ERR("[notification-thread] Failed to send reply to notification channel client");
3895 int client_dispatch_message(struct notification_client
*client
,
3896 struct notification_thread_state
*state
)
3900 if (client
->communication
.inbound
.msg_type
!=
3901 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3902 client
->communication
.inbound
.msg_type
!=
3903 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3904 !client
->validated
) {
3905 WARN("[notification-thread] client attempted a command before handshake");
3910 switch (client
->communication
.inbound
.msg_type
) {
3911 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3913 ret
= client_handle_message_unknown(client
, state
);
3916 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3918 ret
= client_handle_message_handshake(client
, state
);
3921 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3922 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3924 ret
= client_handle_message_subscription(client
,
3925 client
->communication
.inbound
.msg_type
, state
);
3935 /* Incoming data from client. */
3936 int handle_notification_thread_client_in(
3937 struct notification_thread_state
*state
, int socket
)
3940 struct notification_client
*client
;
3945 client
= get_client_from_socket(socket
, state
);
3947 /* Internal error, abort. */
3952 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3953 client
->communication
.inbound
.bytes_to_receive
;
3954 if (client
->communication
.inbound
.expect_creds
) {
3955 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3956 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3957 client
->communication
.inbound
.bytes_to_receive
,
3958 &client
->communication
.inbound
.creds
);
3960 client
->communication
.inbound
.expect_creds
= false;
3961 client
->communication
.inbound
.creds_received
= true;
3964 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3965 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3966 client
->communication
.inbound
.bytes_to_receive
);
3968 if (recv_ret
>= 0) {
3969 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3971 goto error_disconnect_client
;
3974 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3975 /* Message incomplete wait for more data. */
3980 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3983 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3984 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3986 client
->communication
.inbound
.fds_to_receive
,
3987 &client
->communication
.inbound
.payload
);
3990 * Fds received. non blocking fds passing is all
3993 ssize_t expected_size
;
3995 expected_size
= sizeof(int) *
3996 client
->communication
.inbound
3998 assert(ret
== expected_size
);
3999 client
->communication
.inbound
.fds_to_receive
= 0;
4000 } else if (ret
== 0) {
4001 /* Received nothing. */
4005 goto error_disconnect_client
;
4009 /* At this point the message is complete.*/
4010 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4011 client
->communication
.inbound
.fds_to_receive
== 0);
4012 ret
= client_dispatch_message(client
, state
);
4015 * Only returns an error if this client must be
4018 goto error_disconnect_client
;
4025 error_disconnect_client
:
4026 ret
= notification_thread_client_disconnect(client
, state
);
4030 /* Client ready to receive outgoing data. */
4031 int handle_notification_thread_client_out(
4032 struct notification_thread_state
*state
, int socket
)
4035 struct notification_client
*client
;
4036 enum client_transmission_status transmission_status
;
4039 client
= get_client_from_socket(socket
, state
);
4041 /* Internal error, abort. */
4046 pthread_mutex_lock(&client
->lock
);
4047 transmission_status
= client_flush_outgoing_queue(client
);
4048 pthread_mutex_unlock(&client
->lock
);
4050 ret
= client_handle_transmission_status(
4051 client
, transmission_status
, state
);
4061 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4062 const struct channel_state_sample
*sample
,
4063 uint64_t buffer_capacity
)
4065 bool result
= false;
4067 enum lttng_condition_type condition_type
;
4068 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4069 condition
, struct lttng_condition_buffer_usage
,
4072 if (use_condition
->threshold_bytes
.set
) {
4073 threshold
= use_condition
->threshold_bytes
.value
;
4076 * Threshold was expressed as a ratio.
4078 * TODO the threshold (in bytes) of conditions expressed
4079 * as a ratio of total buffer size could be cached to
4080 * forego this double-multiplication or it could be performed
4081 * as fixed-point math.
4083 * Note that caching should accommodates the case where the
4084 * condition applies to multiple channels (i.e. don't assume
4085 * that all channels matching my_chann* have the same size...)
4087 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4088 (double) buffer_capacity
);
4091 condition_type
= lttng_condition_get_type(condition
);
4092 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4093 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4094 threshold
, sample
->highest_usage
);
4097 * The low condition should only be triggered once _all_ of the
4098 * streams in a channel have gone below the "low" threshold.
4100 if (sample
->highest_usage
<= threshold
) {
4104 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4105 threshold
, sample
->highest_usage
);
4108 * For high buffer usage scenarios, we want to trigger whenever
4109 * _any_ of the streams has reached the "high" threshold.
4111 if (sample
->highest_usage
>= threshold
) {
4120 bool evaluate_session_consumed_size_condition(
4121 const struct lttng_condition
*condition
,
4122 uint64_t session_consumed_size
)
4125 const struct lttng_condition_session_consumed_size
*size_condition
=
4126 container_of(condition
,
4127 struct lttng_condition_session_consumed_size
,
4130 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4131 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4132 threshold
, session_consumed_size
);
4133 return session_consumed_size
>= threshold
;
4137 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4138 struct lttng_evaluation
**evaluation
,
4139 const struct notification_thread_state
*state
,
4140 const struct channel_state_sample
*previous_sample
,
4141 const struct channel_state_sample
*latest_sample
,
4142 uint64_t previous_session_consumed_total
,
4143 uint64_t latest_session_consumed_total
,
4144 struct channel_info
*channel_info
)
4147 enum lttng_condition_type condition_type
;
4148 const bool previous_sample_available
= !!previous_sample
;
4149 bool previous_sample_result
= false;
4150 bool latest_sample_result
;
4152 condition_type
= lttng_condition_get_type(condition
);
4154 switch (condition_type
) {
4155 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4156 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4157 if (caa_likely(previous_sample_available
)) {
4158 previous_sample_result
=
4159 evaluate_buffer_usage_condition(condition
,
4160 previous_sample
, channel_info
->capacity
);
4162 latest_sample_result
= evaluate_buffer_usage_condition(
4163 condition
, latest_sample
,
4164 channel_info
->capacity
);
4166 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4167 if (caa_likely(previous_sample_available
)) {
4168 previous_sample_result
=
4169 evaluate_session_consumed_size_condition(
4171 previous_session_consumed_total
);
4173 latest_sample_result
=
4174 evaluate_session_consumed_size_condition(
4176 latest_session_consumed_total
);
4179 /* Unknown condition type; internal error. */
4183 if (!latest_sample_result
||
4184 (previous_sample_result
== latest_sample_result
)) {
4186 * Only trigger on a condition evaluation transition.
4188 * NOTE: This edge-triggered logic may not be appropriate for
4189 * future condition types.
4194 if (!evaluation
|| !latest_sample_result
) {
4198 switch (condition_type
) {
4199 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4200 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4201 *evaluation
= lttng_evaluation_buffer_usage_create(
4203 latest_sample
->highest_usage
,
4204 channel_info
->capacity
);
4206 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4207 *evaluation
= lttng_evaluation_session_consumed_size_create(
4208 latest_session_consumed_total
);
4223 int client_notification_overflow(struct notification_client
*client
)
4226 const struct lttng_notification_channel_message msg
= {
4227 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4230 ASSERT_LOCKED(client
->lock
);
4232 DBG("Dropping notification addressed to client (socket fd = %i)",
4234 if (client
->communication
.outbound
.dropped_notification
) {
4236 * The client already has a "notification dropped" message
4237 * in its outgoing queue. Nothing to do since all
4238 * of those messages are coalesced.
4243 client
->communication
.outbound
.dropped_notification
= true;
4244 ret
= lttng_dynamic_buffer_append(
4245 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4248 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4255 static int client_handle_transmission_status_wrapper(
4256 struct notification_client
*client
,
4257 enum client_transmission_status status
,
4260 return client_handle_transmission_status(client
, status
,
4261 (struct notification_thread_state
*) user_data
);
4265 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4266 const struct lttng_evaluation
*evaluation
,
4267 struct notification_client_list
* client_list
,
4268 struct notification_thread_state
*state
,
4269 uid_t object_uid
, gid_t object_gid
)
4271 const struct lttng_credentials creds
= {
4272 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4273 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4276 return notification_client_list_send_evaluation(client_list
,
4277 trigger
, evaluation
,
4279 client_handle_transmission_status_wrapper
, state
);
4283 * Permission checks relative to notification channel clients are performed
4284 * here. Notice how object, client, and trigger credentials are involved in
4287 * The `object` credentials are the credentials associated with the "subject"
4288 * of a condition. For instance, a `rotation completed` condition applies
4289 * to a session. When that condition is met, it will produce an evaluation
4290 * against a session. Hence, in this case, the `object` credentials are the
4291 * credentials of the "subject" session.
4293 * The `trigger` credentials are the credentials of the user that registered the
4296 * The `client` credentials are the credentials of the user that created a given
4297 * notification channel.
4299 * In terms of visibility, it is expected that non-privilieged users can only
4300 * register triggers against "their" objects (their own sessions and
4301 * applications they are allowed to interact with). They can then open a
4302 * notification channel and subscribe to notifications associated with those
4305 * As for privilieged users, they can register triggers against the objects of
4306 * other users. They can then subscribe to the notifications associated to their
4307 * triggers. Privilieged users _can't_ subscribe to the notifications of
4308 * triggers owned by other users; they must create their own triggers.
4310 * This is more a concern of usability than security. It would be difficult for
4311 * a root user reliably subscribe to a specific set of conditions without
4312 * interference from external users (those could, for instance, unregister
4316 int notification_client_list_send_evaluation(
4317 struct notification_client_list
*client_list
,
4318 const struct lttng_trigger
*trigger
,
4319 const struct lttng_evaluation
*evaluation
,
4320 const struct lttng_credentials
*source_object_creds
,
4321 report_client_transmission_result_cb client_report
,
4325 struct lttng_payload msg_payload
;
4326 struct notification_client_list_element
*client_list_element
, *tmp
;
4327 const struct lttng_notification notification
= {
4328 .trigger
= (struct lttng_trigger
*) trigger
,
4329 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4331 struct lttng_notification_channel_message msg_header
= {
4332 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4334 const struct lttng_credentials
*trigger_creds
=
4335 lttng_trigger_get_credentials(trigger
);
4337 lttng_payload_init(&msg_payload
);
4339 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4340 sizeof(msg_header
));
4345 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4347 ERR("[notification-thread] Failed to serialize notification");
4352 /* Update payload size. */
4353 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4354 ->size
= (uint32_t)(
4355 msg_payload
.buffer
.size
- sizeof(msg_header
));
4357 /* Update the payload number of fds. */
4359 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4360 &msg_payload
, 0, -1);
4362 ((struct lttng_notification_channel_message
*)
4363 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4364 lttng_payload_view_get_fd_handle_count(&pv
);
4367 pthread_mutex_lock(&client_list
->lock
);
4368 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4369 &client_list
->clients_list
, node
) {
4370 enum client_transmission_status transmission_status
;
4371 struct notification_client
*client
=
4372 client_list_element
->client
;
4375 pthread_mutex_lock(&client
->lock
);
4376 if (!client
->communication
.active
) {
4378 * Skip inactive client (protocol error or
4381 DBG("Skipping client at it is marked as inactive");
4385 if (source_object_creds
) {
4386 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4387 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4390 * Client is not allowed to monitor this
4393 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4398 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
)) {
4399 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4403 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4404 client
->socket
, msg_payload
.buffer
.size
);
4406 if (client_has_outbound_data_left(client
)) {
4408 * Outgoing data is already buffered for this client;
4409 * drop the notification and enqueue a "dropped
4410 * notification" message if this is the first dropped
4411 * notification since the socket spilled-over to the
4414 ret
= client_notification_overflow(client
);
4421 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4427 transmission_status
= client_flush_outgoing_queue(client
);
4428 pthread_mutex_unlock(&client
->lock
);
4429 ret
= client_report(client
, transmission_status
, user_data
);
4432 goto end_unlock_list
;
4438 pthread_mutex_unlock(&client
->lock
);
4441 goto end_unlock_list
;
4447 pthread_mutex_unlock(&client_list
->lock
);
4449 lttng_payload_reset(&msg_payload
);
4454 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4455 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4459 struct lttng_event_notifier_notification
*notification
= NULL
;
4460 char *capture_buffer
= NULL
;
4461 size_t capture_buffer_size
;
4462 void *reception_buffer
;
4463 size_t reception_size
;
4465 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4466 struct lttng_kernel_event_notifier_notification kernel_notification
;
4468 /* Init lttng_event_notifier_notification */
4470 case LTTNG_DOMAIN_UST
:
4471 reception_buffer
= (void *) &ust_notification
;
4472 reception_size
= sizeof(ust_notification
);
4474 case LTTNG_DOMAIN_KERNEL
:
4475 reception_buffer
= (void *) &kernel_notification
;
4476 reception_size
= sizeof(kernel_notification
);
4483 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4484 * ensuring that read/write of tracer notifications are atomic.
4486 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4488 if (ret
!= reception_size
) {
4489 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4490 notification_pipe_read_fd
, reception_size
, ret
);
4496 case LTTNG_DOMAIN_UST
:
4497 token
= ust_notification
.token
;
4498 capture_buffer_size
= ust_notification
.capture_buf_size
;
4500 case LTTNG_DOMAIN_KERNEL
:
4501 token
= kernel_notification
.token
;
4502 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4508 if (capture_buffer_size
== 0) {
4509 capture_buffer
= NULL
;
4513 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4514 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",
4515 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4519 capture_buffer
= zmalloc(capture_buffer_size
);
4520 if (!capture_buffer
) {
4521 ERR("[notification-thread] Failed to allocate capture buffer");
4525 /* Fetch additional payload (capture). */
4526 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4527 if (ret
!= capture_buffer_size
) {
4528 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4529 notification_pipe_read_fd
);
4534 notification
= lttng_event_notifier_notification_create(token
, domain
,
4535 capture_buffer
, capture_buffer_size
);
4536 if (notification
== NULL
) {
4541 * Ownership transfered to the lttng_event_notifier_notification object.
4543 capture_buffer
= NULL
;
4546 free(capture_buffer
);
4547 return notification
;
4551 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4552 struct lttng_event_notifier_notification
*notification
)
4554 struct cds_lfht_node
*node
;
4555 struct cds_lfht_iter iter
;
4556 struct notification_trigger_tokens_ht_element
*element
;
4557 struct lttng_evaluation
*evaluation
= NULL
;
4558 enum action_executor_status executor_status
;
4559 struct notification_client_list
*client_list
= NULL
;
4561 unsigned int capture_count
= 0;
4563 /* Find triggers associated with this token. */
4565 cds_lfht_lookup(state
->trigger_tokens_ht
,
4566 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4567 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4568 node
= cds_lfht_iter_get_node(&iter
);
4569 if (caa_unlikely(!node
)) {
4571 * This is not an error, slow consumption of the tracer
4572 * notifications can lead to situations where a trigger is
4573 * removed but we still get tracer notifications matching a
4574 * trigger that no longer exists.
4580 element
= caa_container_of(node
,
4581 struct notification_trigger_tokens_ht_element
,
4584 if (lttng_condition_event_rule_matches_get_capture_descriptor_count(
4585 lttng_trigger_get_const_condition(element
->trigger
),
4586 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4587 ERR("Failed to get capture count");
4592 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4593 ERR("Expected capture but capture buffer is null");
4598 evaluation
= lttng_evaluation_event_rule_matches_create(
4599 container_of(lttng_trigger_get_const_condition(
4601 struct lttng_condition_event_rule_matches
,
4603 notification
->capture_buffer
,
4604 notification
->capture_buf_size
, false);
4606 if (evaluation
== NULL
) {
4607 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
4611 client_list
= get_client_list_from_condition(state
,
4612 lttng_trigger_get_const_condition(element
->trigger
));
4613 executor_status
= action_executor_enqueue_trigger(state
->executor
,
4614 element
->trigger
, evaluation
, NULL
, client_list
);
4615 switch (executor_status
) {
4616 case ACTION_EXECUTOR_STATUS_OK
:
4619 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4621 struct notification_client_list_element
*client_list_element
,
4625 * Not a fatal error; this is expected and simply means the
4626 * executor has too much work queued already.
4630 /* No clients subscribed to notifications for this trigger. */
4635 /* Warn clients that a notification (or more) was dropped. */
4636 pthread_mutex_lock(&client_list
->lock
);
4637 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4638 &client_list
->clients_list
, node
) {
4639 enum client_transmission_status transmission_status
;
4640 struct notification_client
*client
=
4641 client_list_element
->client
;
4643 pthread_mutex_lock(&client
->lock
);
4644 ret
= client_notification_overflow(client
);
4650 transmission_status
=
4651 client_flush_outgoing_queue(client
);
4652 ret
= client_handle_transmission_status(
4653 client
, transmission_status
, state
);
4659 pthread_mutex_unlock(&client
->lock
);
4665 pthread_mutex_unlock(&client_list
->lock
);
4668 case ACTION_EXECUTOR_STATUS_INVALID
:
4669 case ACTION_EXECUTOR_STATUS_ERROR
:
4670 /* Fatal error, shut down everything. */
4671 ERR("Fatal error encoutered while enqueuing action to the action executor");
4675 /* Unhandled error. */
4680 notification_client_list_put(client_list
);
4687 int handle_one_event_notifier_notification(
4688 struct notification_thread_state
*state
,
4689 int pipe
, enum lttng_domain_type domain
)
4692 struct lttng_event_notifier_notification
*notification
= NULL
;
4694 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4695 if (notification
== NULL
) {
4696 /* Reception failed, don't consider it fatal. */
4697 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4698 pipe
, lttng_domain_type_str(domain
));
4702 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4704 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4705 pipe
, lttng_domain_type_str(domain
));
4710 lttng_event_notifier_notification_destroy(notification
);
4714 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4715 int pipe
, enum lttng_domain_type domain
)
4717 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4720 int handle_notification_thread_channel_sample(
4721 struct notification_thread_state
*state
, int pipe
,
4722 enum lttng_domain_type domain
)
4725 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4726 struct channel_info
*channel_info
;
4727 struct cds_lfht_node
*node
;
4728 struct cds_lfht_iter iter
;
4729 struct lttng_channel_trigger_list
*trigger_list
;
4730 struct lttng_trigger_list_element
*trigger_list_element
;
4731 bool previous_sample_available
= false;
4732 struct channel_state_sample previous_sample
, latest_sample
;
4733 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4734 struct lttng_credentials channel_creds
;
4737 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4738 * ensuring that read/write of sampling messages are atomic.
4740 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4741 if (ret
!= sizeof(sample_msg
)) {
4742 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4749 latest_sample
.key
.key
= sample_msg
.key
;
4750 latest_sample
.key
.domain
= domain
;
4751 latest_sample
.highest_usage
= sample_msg
.highest
;
4752 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4753 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4757 /* Retrieve the channel's informations */
4758 cds_lfht_lookup(state
->channels_ht
,
4759 hash_channel_key(&latest_sample
.key
),
4763 node
= cds_lfht_iter_get_node(&iter
);
4764 if (caa_unlikely(!node
)) {
4766 * Not an error since the consumer can push a sample to the pipe
4767 * and the rest of the session daemon could notify us of the
4768 * channel's destruction before we get a chance to process that
4771 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4772 latest_sample
.key
.key
,
4773 lttng_domain_type_str(domain
));
4776 channel_info
= caa_container_of(node
, struct channel_info
,
4778 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4780 latest_sample
.key
.key
,
4781 channel_info
->session_info
->name
,
4782 latest_sample
.highest_usage
,
4783 latest_sample
.lowest_usage
,
4784 latest_sample
.channel_total_consumed
);
4786 previous_session_consumed_total
=
4787 channel_info
->session_info
->consumed_data_size
;
4789 /* Retrieve the channel's last sample, if it exists, and update it. */
4790 cds_lfht_lookup(state
->channel_state_ht
,
4791 hash_channel_key(&latest_sample
.key
),
4792 match_channel_state_sample
,
4795 node
= cds_lfht_iter_get_node(&iter
);
4796 if (caa_likely(node
)) {
4797 struct channel_state_sample
*stored_sample
;
4799 /* Update the sample stored. */
4800 stored_sample
= caa_container_of(node
,
4801 struct channel_state_sample
,
4802 channel_state_ht_node
);
4804 memcpy(&previous_sample
, stored_sample
,
4805 sizeof(previous_sample
));
4806 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4807 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4808 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4809 previous_sample_available
= true;
4811 latest_session_consumed_total
=
4812 previous_session_consumed_total
+
4813 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4816 * This is the channel's first sample, allocate space for and
4817 * store the new sample.
4819 struct channel_state_sample
*stored_sample
;
4821 stored_sample
= zmalloc(sizeof(*stored_sample
));
4822 if (!stored_sample
) {
4827 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4828 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4829 cds_lfht_add(state
->channel_state_ht
,
4830 hash_channel_key(&stored_sample
->key
),
4831 &stored_sample
->channel_state_ht_node
);
4833 latest_session_consumed_total
=
4834 previous_session_consumed_total
+
4835 latest_sample
.channel_total_consumed
;
4838 channel_info
->session_info
->consumed_data_size
=
4839 latest_session_consumed_total
;
4841 /* Find triggers associated with this channel. */
4842 cds_lfht_lookup(state
->channel_triggers_ht
,
4843 hash_channel_key(&latest_sample
.key
),
4844 match_channel_trigger_list
,
4847 node
= cds_lfht_iter_get_node(&iter
);
4848 if (caa_likely(!node
)) {
4852 channel_creds
= (typeof(channel_creds
)) {
4853 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4854 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4857 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4858 channel_triggers_ht_node
);
4859 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4861 const struct lttng_condition
*condition
;
4862 struct lttng_trigger
*trigger
;
4863 struct notification_client_list
*client_list
= NULL
;
4864 struct lttng_evaluation
*evaluation
= NULL
;
4865 enum action_executor_status executor_status
;
4868 trigger
= trigger_list_element
->trigger
;
4869 condition
= lttng_trigger_get_const_condition(trigger
);
4873 * Check if any client is subscribed to the result of this
4876 client_list
= get_client_list_from_condition(state
, condition
);
4878 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4879 previous_sample_available
? &previous_sample
: NULL
,
4881 previous_session_consumed_total
,
4882 latest_session_consumed_total
,
4884 if (caa_unlikely(ret
)) {
4888 if (caa_likely(!evaluation
)) {
4893 * Ownership of `evaluation` transferred to the action executor
4894 * no matter the result.
4896 executor_status
= action_executor_enqueue_trigger(
4897 state
->executor
, trigger
, evaluation
,
4898 &channel_creds
, client_list
);
4900 switch (executor_status
) {
4901 case ACTION_EXECUTOR_STATUS_OK
:
4903 case ACTION_EXECUTOR_STATUS_ERROR
:
4904 case ACTION_EXECUTOR_STATUS_INVALID
:
4906 * TODO Add trigger identification (name/id) when
4907 * it is added to the API.
4909 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4912 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4914 * TODO Add trigger identification (name/id) when
4915 * it is added to the API.
4917 * Not a fatal error.
4919 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4927 notification_client_list_put(client_list
);
4928 if (caa_unlikely(ret
)) {