2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.h"
12 #include <urcu/rculfhash.h>
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/futex.h>
17 #include <common/unix.h>
18 #include <common/dynamic-buffer.h>
19 #include <common/hashtable/utils.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/macros.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.h>
24 #include <lttng/action/group-internal.h>
25 #include <lttng/action/incr-value-internal.h>
26 #include <lttng/domain-internal.h>
27 #include <lttng/notification/notification-internal.h>
28 #include <lttng/condition/condition-internal.h>
29 #include <lttng/condition/buffer-usage-internal.h>
30 #include <lttng/condition/session-consumed-size-internal.h>
31 #include <lttng/condition/session-rotation-internal.h>
32 #include <lttng/condition/on-event-internal.h>
33 #include <lttng/domain-internal.h>
34 #include <lttng/notification/channel-internal.h>
35 #include <lttng/trigger/trigger-internal.h>
36 #include <lttng/event-rule/event-rule-internal.h>
44 #include "condition-internal.h"
45 #include "event-notifier-error-accounting.h"
46 #include "notification-thread.h"
47 #include "notification-thread-events.h"
48 #include "notification-thread-commands.h"
49 #include "lttng-sessiond.h"
53 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
54 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
56 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
57 #define MAX_CAPTURE_SIZE (PIPE_BUF)
59 enum lttng_object_type
{
60 LTTNG_OBJECT_TYPE_UNKNOWN
,
61 LTTNG_OBJECT_TYPE_NONE
,
62 LTTNG_OBJECT_TYPE_CHANNEL
,
63 LTTNG_OBJECT_TYPE_SESSION
,
66 struct lttng_trigger_list_element
{
67 /* No ownership of the trigger object is assumed. */
68 struct lttng_trigger
*trigger
;
69 struct cds_list_head node
;
72 struct lttng_channel_trigger_list
{
73 struct channel_key channel_key
;
74 /* List of struct lttng_trigger_list_element. */
75 struct cds_list_head list
;
76 /* Node in the channel_triggers_ht */
77 struct cds_lfht_node channel_triggers_ht_node
;
78 /* call_rcu delayed reclaim. */
79 struct rcu_head rcu_node
;
83 * List of triggers applying to a given session.
86 * - lttng_session_trigger_list_create()
87 * - lttng_session_trigger_list_build()
88 * - lttng_session_trigger_list_destroy()
89 * - lttng_session_trigger_list_add()
91 struct lttng_session_trigger_list
{
93 * Not owned by this; points to the session_info structure's
96 const char *session_name
;
97 /* List of struct lttng_trigger_list_element. */
98 struct cds_list_head list
;
99 /* Node in the session_triggers_ht */
100 struct cds_lfht_node session_triggers_ht_node
;
102 * Weak reference to the notification system's session triggers
105 * The session trigger list structure structure is owned by
106 * the session's session_info.
108 * The session_info is kept alive the the channel_infos holding a
109 * reference to it (reference counting). When those channels are
110 * destroyed (at runtime or on teardown), the reference they hold
111 * to the session_info are released. On destruction of session_info,
112 * session_info_destroy() will remove the list of triggers applying
113 * to this session from the notification system's state.
115 * This implies that the session_triggers_ht must be destroyed
116 * after the channels.
118 struct cds_lfht
*session_triggers_ht
;
119 /* Used for delayed RCU reclaim. */
120 struct rcu_head rcu_node
;
123 struct lttng_trigger_ht_element
{
124 struct lttng_trigger
*trigger
;
125 struct cds_lfht_node node
;
126 struct cds_lfht_node node_by_name_uid
;
127 struct cds_list_head client_list_trigger_node
;
128 /* call_rcu delayed reclaim. */
129 struct rcu_head rcu_node
;
132 struct lttng_condition_list_element
{
133 struct lttng_condition
*condition
;
134 struct cds_list_head node
;
137 struct channel_state_sample
{
138 struct channel_key key
;
139 struct cds_lfht_node channel_state_ht_node
;
140 uint64_t highest_usage
;
141 uint64_t lowest_usage
;
142 uint64_t channel_total_consumed
;
143 /* call_rcu delayed reclaim. */
144 struct rcu_head rcu_node
;
147 static unsigned long hash_channel_key(struct channel_key
*key
);
148 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
149 struct lttng_evaluation
**evaluation
,
150 const struct notification_thread_state
*state
,
151 const struct channel_state_sample
*previous_sample
,
152 const struct channel_state_sample
*latest_sample
,
153 uint64_t previous_session_consumed_total
,
154 uint64_t latest_session_consumed_total
,
155 struct channel_info
*channel_info
);
157 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
158 const struct lttng_evaluation
*evaluation
,
159 struct notification_client_list
*client_list
,
160 struct notification_thread_state
*state
,
161 uid_t channel_uid
, gid_t channel_gid
);
164 /* session_info API */
166 void session_info_destroy(void *_data
);
168 void session_info_get(struct session_info
*session_info
);
170 void session_info_put(struct session_info
*session_info
);
172 struct session_info
*session_info_create(const char *name
,
173 uid_t uid
, gid_t gid
,
174 struct lttng_session_trigger_list
*trigger_list
,
175 struct cds_lfht
*sessions_ht
);
177 void session_info_add_channel(struct session_info
*session_info
,
178 struct channel_info
*channel_info
);
180 void session_info_remove_channel(struct session_info
*session_info
,
181 struct channel_info
*channel_info
);
183 /* lttng_session_trigger_list API */
185 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
186 const char *session_name
,
187 struct cds_lfht
*session_triggers_ht
);
189 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
190 const struct notification_thread_state
*state
,
191 const char *session_name
);
193 void lttng_session_trigger_list_destroy(
194 struct lttng_session_trigger_list
*list
);
196 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
197 struct lttng_trigger
*trigger
);
200 int client_handle_transmission_status(
201 struct notification_client
*client
,
202 enum client_transmission_status transmission_status
,
203 struct notification_thread_state
*state
);
206 int handle_one_event_notifier_notification(
207 struct notification_thread_state
*state
,
208 int pipe
, enum lttng_domain_type domain
);
211 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
214 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
216 /* This double-cast is intended to supress pointer-to-cast warning. */
217 const int socket
= (int) (intptr_t) key
;
218 const struct notification_client
*client
= caa_container_of(node
,
219 struct notification_client
, client_socket_ht_node
);
221 return client
->socket
== socket
;
225 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
227 /* This double-cast is intended to supress pointer-to-cast warning. */
228 const notification_client_id id
= *((notification_client_id
*) key
);
229 const struct notification_client
*client
= caa_container_of(
230 node
, struct notification_client
, client_id_ht_node
);
232 return client
->id
== id
;
236 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
238 struct channel_key
*channel_key
= (struct channel_key
*) key
;
239 struct lttng_channel_trigger_list
*trigger_list
;
241 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
242 channel_triggers_ht_node
);
244 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
245 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
249 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
251 const char *session_name
= (const char *) key
;
252 struct lttng_session_trigger_list
*trigger_list
;
254 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
255 session_triggers_ht_node
);
257 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
261 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
263 struct channel_key
*channel_key
= (struct channel_key
*) key
;
264 struct channel_state_sample
*sample
;
266 sample
= caa_container_of(node
, struct channel_state_sample
,
267 channel_state_ht_node
);
269 return !!((channel_key
->key
== sample
->key
.key
) &&
270 (channel_key
->domain
== sample
->key
.domain
));
274 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
276 struct channel_key
*channel_key
= (struct channel_key
*) key
;
277 struct channel_info
*channel_info
;
279 channel_info
= caa_container_of(node
, struct channel_info
,
282 return !!((channel_key
->key
== channel_info
->key
.key
) &&
283 (channel_key
->domain
== channel_info
->key
.domain
));
287 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
289 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
290 struct lttng_trigger_ht_element
*trigger_ht_element
;
292 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
295 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
299 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
301 const uint64_t *_key
= key
;
302 struct notification_trigger_tokens_ht_element
*element
;
304 element
= caa_container_of(node
,
305 struct notification_trigger_tokens_ht_element
, node
);
306 return *_key
== element
->token
;
310 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
312 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
313 struct notification_client_list
*client_list
;
314 const struct lttng_condition
*condition
;
316 assert(condition_key
);
318 client_list
= caa_container_of(node
, struct notification_client_list
,
319 notification_trigger_clients_ht_node
);
320 condition
= client_list
->condition
;
322 return !!lttng_condition_is_equal(condition_key
, condition
);
326 int match_session(struct cds_lfht_node
*node
, const void *key
)
328 const char *name
= key
;
329 struct session_info
*session_info
= caa_container_of(
330 node
, struct session_info
, sessions_ht_node
);
332 return !strcmp(session_info
->name
, name
);
336 const char *notification_command_type_str(
337 enum notification_thread_command_type type
)
340 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
341 return "REGISTER_TRIGGER";
342 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
343 return "UNREGISTER_TRIGGER";
344 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
345 return "ADD_CHANNEL";
346 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
347 return "REMOVE_CHANNEL";
348 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
349 return "SESSION_ROTATION_ONGOING";
350 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
351 return "SESSION_ROTATION_COMPLETED";
352 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
353 return "ADD_TRACER_EVENT_SOURCE";
354 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
355 return "REMOVE_TRACER_EVENT_SOURCE";
356 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
357 return "LIST_TRIGGERS";
358 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
359 return "GET_TRIGGER";
360 case NOTIFICATION_COMMAND_TYPE_QUIT
:
362 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
363 return "CLIENT_COMMUNICATION_UPDATE";
370 * Match trigger based on name and credentials only.
371 * Name duplication is NOT allowed for the same uid.
374 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
379 const char *key_name
;
380 enum lttng_trigger_status status
;
381 const struct lttng_credentials
*key_creds
;
382 const struct lttng_credentials
*node_creds
;
383 const struct lttng_trigger
*trigger_key
=
384 (const struct lttng_trigger
*) key
;
385 const struct lttng_trigger_ht_element
*trigger_ht_element
=
386 caa_container_of(node
,
387 struct lttng_trigger_ht_element
,
390 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
391 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
393 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
394 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
396 /* Compare the names. */
397 if (strcmp(name
, key_name
) != 0) {
401 /* Compare the owners' UIDs. */
402 key_creds
= lttng_trigger_get_credentials(trigger_key
);
403 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
405 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
412 * Hash trigger based on name and credentials only.
415 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
417 unsigned long hash
= 0;
418 const struct lttng_credentials
*trigger_creds
;
419 const char *trigger_name
;
420 enum lttng_trigger_status status
;
422 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
423 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
424 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
427 trigger_creds
= lttng_trigger_get_credentials(trigger
);
428 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
435 unsigned long hash_channel_key(struct channel_key
*key
)
437 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
438 unsigned long domain_hash
= hash_key_ulong(
439 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
441 return key_hash
^ domain_hash
;
445 unsigned long hash_client_socket(int socket
)
447 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
451 unsigned long hash_client_id(notification_client_id id
)
453 return hash_key_u64(&id
, lttng_ht_seed
);
457 * Get the type of object to which a given condition applies. Bindings let
458 * the notification system evaluate a trigger's condition when a given
459 * object's state is updated.
461 * For instance, a condition bound to a channel will be evaluated everytime
462 * the channel's state is changed by a channel monitoring sample.
465 enum lttng_object_type
get_condition_binding_object(
466 const struct lttng_condition
*condition
)
468 switch (lttng_condition_get_type(condition
)) {
469 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
470 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
471 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
472 return LTTNG_OBJECT_TYPE_CHANNEL
;
473 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
474 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
475 return LTTNG_OBJECT_TYPE_SESSION
;
476 case LTTNG_CONDITION_TYPE_ON_EVENT
:
477 return LTTNG_OBJECT_TYPE_NONE
;
479 return LTTNG_OBJECT_TYPE_UNKNOWN
;
484 void free_channel_info_rcu(struct rcu_head
*node
)
486 free(caa_container_of(node
, struct channel_info
, rcu_node
));
490 void channel_info_destroy(struct channel_info
*channel_info
)
496 if (channel_info
->session_info
) {
497 session_info_remove_channel(channel_info
->session_info
,
499 session_info_put(channel_info
->session_info
);
501 if (channel_info
->name
) {
502 free(channel_info
->name
);
504 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
508 void free_session_info_rcu(struct rcu_head
*node
)
510 free(caa_container_of(node
, struct session_info
, rcu_node
));
513 /* Don't call directly, use the ref-counting mechanism. */
515 void session_info_destroy(void *_data
)
517 struct session_info
*session_info
= _data
;
520 assert(session_info
);
521 if (session_info
->channel_infos_ht
) {
522 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
524 ERR("[notification-thread] Failed to destroy channel information hash table");
527 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
530 cds_lfht_del(session_info
->sessions_ht
,
531 &session_info
->sessions_ht_node
);
533 free(session_info
->name
);
534 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
538 void session_info_get(struct session_info
*session_info
)
543 lttng_ref_get(&session_info
->ref
);
547 void session_info_put(struct session_info
*session_info
)
552 lttng_ref_put(&session_info
->ref
);
556 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
557 struct lttng_session_trigger_list
*trigger_list
,
558 struct cds_lfht
*sessions_ht
)
560 struct session_info
*session_info
;
564 session_info
= zmalloc(sizeof(*session_info
));
568 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
570 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
571 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
572 if (!session_info
->channel_infos_ht
) {
576 cds_lfht_node_init(&session_info
->sessions_ht_node
);
577 session_info
->name
= strdup(name
);
578 if (!session_info
->name
) {
581 session_info
->uid
= uid
;
582 session_info
->gid
= gid
;
583 session_info
->trigger_list
= trigger_list
;
584 session_info
->sessions_ht
= sessions_ht
;
588 session_info_put(session_info
);
593 void session_info_add_channel(struct session_info
*session_info
,
594 struct channel_info
*channel_info
)
597 cds_lfht_add(session_info
->channel_infos_ht
,
598 hash_channel_key(&channel_info
->key
),
599 &channel_info
->session_info_channels_ht_node
);
604 void session_info_remove_channel(struct session_info
*session_info
,
605 struct channel_info
*channel_info
)
608 cds_lfht_del(session_info
->channel_infos_ht
,
609 &channel_info
->session_info_channels_ht_node
);
614 struct channel_info
*channel_info_create(const char *channel_name
,
615 struct channel_key
*channel_key
, uint64_t channel_capacity
,
616 struct session_info
*session_info
)
618 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
624 cds_lfht_node_init(&channel_info
->channels_ht_node
);
625 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
626 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
627 channel_info
->capacity
= channel_capacity
;
629 channel_info
->name
= strdup(channel_name
);
630 if (!channel_info
->name
) {
635 * Set the references between session and channel infos:
636 * - channel_info holds a strong reference to session_info
637 * - session_info holds a weak reference to channel_info
639 session_info_get(session_info
);
640 session_info_add_channel(session_info
, channel_info
);
641 channel_info
->session_info
= session_info
;
645 channel_info_destroy(channel_info
);
650 bool notification_client_list_get(struct notification_client_list
*list
)
652 return urcu_ref_get_unless_zero(&list
->ref
);
656 void free_notification_client_list_rcu(struct rcu_head
*node
)
658 free(caa_container_of(node
, struct notification_client_list
,
663 void notification_client_list_release(struct urcu_ref
*list_ref
)
665 struct notification_client_list
*list
=
666 container_of(list_ref
, typeof(*list
), ref
);
667 struct notification_client_list_element
*client_list_element
, *tmp
;
669 lttng_condition_put(list
->condition
);
671 if (list
->notification_trigger_clients_ht
) {
674 cds_lfht_del(list
->notification_trigger_clients_ht
,
675 &list
->notification_trigger_clients_ht_node
);
677 list
->notification_trigger_clients_ht
= NULL
;
679 cds_list_for_each_entry_safe(client_list_element
, tmp
,
680 &list
->clients_list
, node
) {
681 free(client_list_element
);
684 assert(cds_list_empty(&list
->triggers_list
));
686 pthread_mutex_destroy(&list
->lock
);
687 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
691 bool condition_applies_to_client(const struct lttng_condition
*condition
,
692 struct notification_client
*client
)
694 bool applies
= false;
695 struct lttng_condition_list_element
*condition_list_element
;
697 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
699 applies
= lttng_condition_is_equal(
700 condition_list_element
->condition
,
710 struct notification_client_list
*notification_client_list_create(
711 struct notification_thread_state
*state
,
712 const struct lttng_condition
*condition
)
714 struct notification_client
*client
;
715 struct cds_lfht_iter iter
;
716 struct notification_client_list
*client_list
;
718 client_list
= zmalloc(sizeof(*client_list
));
723 pthread_mutex_init(&client_list
->lock
, NULL
);
725 * The trigger that owns the condition has the first reference to this
728 urcu_ref_init(&client_list
->ref
);
729 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
730 CDS_INIT_LIST_HEAD(&client_list
->clients_list
);
731 CDS_INIT_LIST_HEAD(&client_list
->triggers_list
);
734 * Create a copy of the condition so that it's independent of any
735 * trigger. The client list may outlive the trigger object (which owns
736 * the condition) that is used to create it.
738 client_list
->condition
= lttng_condition_copy(condition
);
740 /* Build a list of clients to which this new condition applies. */
741 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
742 client_socket_ht_node
) {
743 struct notification_client_list_element
*client_list_element
;
745 if (!condition_applies_to_client(condition
, client
)) {
749 client_list_element
= zmalloc(sizeof(*client_list_element
));
750 if (!client_list_element
) {
751 goto error_put_client_list
;
754 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
755 client_list_element
->client
= client
;
756 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
759 client_list
->notification_trigger_clients_ht
=
760 state
->notification_trigger_clients_ht
;
764 * Add the client list to the global list of client list.
766 cds_lfht_add_unique(state
->notification_trigger_clients_ht
,
767 lttng_condition_hash(client_list
->condition
),
768 match_client_list_condition
,
769 client_list
->condition
,
770 &client_list
->notification_trigger_clients_ht_node
);
774 error_put_client_list
:
775 notification_client_list_put(client_list
);
782 void notification_client_list_put(struct notification_client_list
*list
)
787 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
790 /* Provides a reference to the returned list. */
792 struct notification_client_list
*get_client_list_from_condition(
793 struct notification_thread_state
*state
,
794 const struct lttng_condition
*condition
)
796 struct cds_lfht_node
*node
;
797 struct cds_lfht_iter iter
;
798 struct notification_client_list
*list
= NULL
;
801 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
802 lttng_condition_hash(condition
),
803 match_client_list_condition
,
806 node
= cds_lfht_iter_get_node(&iter
);
808 list
= container_of(node
, struct notification_client_list
,
809 notification_trigger_clients_ht_node
);
810 list
= notification_client_list_get(list
) ? list
: NULL
;
818 int evaluate_channel_condition_for_client(
819 const struct lttng_condition
*condition
,
820 struct notification_thread_state
*state
,
821 struct lttng_evaluation
**evaluation
,
822 uid_t
*session_uid
, gid_t
*session_gid
)
825 struct cds_lfht_iter iter
;
826 struct cds_lfht_node
*node
;
827 struct channel_info
*channel_info
= NULL
;
828 struct channel_key
*channel_key
= NULL
;
829 struct channel_state_sample
*last_sample
= NULL
;
830 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
834 /* Find the channel associated with the condition. */
835 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
836 channel_trigger_list
, channel_triggers_ht_node
) {
837 struct lttng_trigger_list_element
*element
;
839 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
840 const struct lttng_condition
*current_condition
=
841 lttng_trigger_get_const_condition(
844 assert(current_condition
);
845 if (!lttng_condition_is_equal(condition
,
846 current_condition
)) {
850 /* Found the trigger, save the channel key. */
851 channel_key
= &channel_trigger_list
->channel_key
;
855 /* The channel key was found stop iteration. */
861 /* No channel found; normal exit. */
862 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
867 /* Fetch channel info for the matching channel. */
868 cds_lfht_lookup(state
->channels_ht
,
869 hash_channel_key(channel_key
),
873 node
= cds_lfht_iter_get_node(&iter
);
875 channel_info
= caa_container_of(node
, struct channel_info
,
878 /* Retrieve the channel's last sample, if it exists. */
879 cds_lfht_lookup(state
->channel_state_ht
,
880 hash_channel_key(channel_key
),
881 match_channel_state_sample
,
884 node
= cds_lfht_iter_get_node(&iter
);
886 last_sample
= caa_container_of(node
,
887 struct channel_state_sample
,
888 channel_state_ht_node
);
890 /* Nothing to evaluate, no sample was ever taken. Normal exit */
891 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
896 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
898 0, channel_info
->session_info
->consumed_data_size
,
901 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
905 *session_uid
= channel_info
->session_info
->uid
;
906 *session_gid
= channel_info
->session_info
->gid
;
913 const char *get_condition_session_name(const struct lttng_condition
*condition
)
915 const char *session_name
= NULL
;
916 enum lttng_condition_status status
;
918 switch (lttng_condition_get_type(condition
)) {
919 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
920 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
921 status
= lttng_condition_buffer_usage_get_session_name(
922 condition
, &session_name
);
924 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
925 status
= lttng_condition_session_consumed_size_get_session_name(
926 condition
, &session_name
);
928 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
929 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
930 status
= lttng_condition_session_rotation_get_session_name(
931 condition
, &session_name
);
936 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
937 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
945 int evaluate_session_condition_for_client(
946 const struct lttng_condition
*condition
,
947 struct notification_thread_state
*state
,
948 struct lttng_evaluation
**evaluation
,
949 uid_t
*session_uid
, gid_t
*session_gid
)
952 struct cds_lfht_iter iter
;
953 struct cds_lfht_node
*node
;
954 const char *session_name
;
955 struct session_info
*session_info
= NULL
;
958 session_name
= get_condition_session_name(condition
);
960 /* Find the session associated with the trigger. */
961 cds_lfht_lookup(state
->sessions_ht
,
962 hash_key_str(session_name
, lttng_ht_seed
),
966 node
= cds_lfht_iter_get_node(&iter
);
968 DBG("[notification-thread] No known session matching name \"%s\"",
974 session_info
= caa_container_of(node
, struct session_info
,
976 session_info_get(session_info
);
979 * Evaluation is performed in-line here since only one type of
980 * session-bound condition is handled for the moment.
982 switch (lttng_condition_get_type(condition
)) {
983 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
984 if (!session_info
->rotation
.ongoing
) {
986 goto end_session_put
;
989 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
990 session_info
->rotation
.id
);
993 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
996 goto end_session_put
;
1002 goto end_session_put
;
1005 *session_uid
= session_info
->uid
;
1006 *session_gid
= session_info
->gid
;
1009 session_info_put(session_info
);
1016 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1017 const struct lttng_condition
*condition
,
1018 struct notification_client
*client
,
1019 struct notification_thread_state
*state
)
1022 struct lttng_evaluation
*evaluation
= NULL
;
1023 struct notification_client_list client_list
= {
1024 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1026 struct notification_client_list_element client_list_element
= { 0 };
1027 uid_t object_uid
= 0;
1028 gid_t object_gid
= 0;
1035 switch (get_condition_binding_object(condition
)) {
1036 case LTTNG_OBJECT_TYPE_SESSION
:
1037 ret
= evaluate_session_condition_for_client(condition
, state
,
1038 &evaluation
, &object_uid
, &object_gid
);
1040 case LTTNG_OBJECT_TYPE_CHANNEL
:
1041 ret
= evaluate_channel_condition_for_client(condition
, state
,
1042 &evaluation
, &object_uid
, &object_gid
);
1044 case LTTNG_OBJECT_TYPE_NONE
:
1045 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
1048 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1058 /* Evaluation yielded nothing. Normal exit. */
1059 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
1065 * Create a temporary client list with the client currently
1068 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1069 CDS_INIT_LIST_HEAD(&client_list
.clients_list
);
1071 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1072 client_list_element
.client
= client
;
1073 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1075 /* Send evaluation result to the newly-subscribed client. */
1076 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1077 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1078 state
, object_uid
, object_gid
);
1085 int notification_thread_client_subscribe(struct notification_client
*client
,
1086 struct lttng_condition
*condition
,
1087 struct notification_thread_state
*state
,
1088 enum lttng_notification_channel_status
*_status
)
1091 struct notification_client_list
*client_list
= NULL
;
1092 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1093 struct notification_client_list_element
*client_list_element
= NULL
;
1094 enum lttng_notification_channel_status status
=
1095 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1098 * Ensure that the client has not already subscribed to this condition
1101 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1102 if (lttng_condition_is_equal(condition_list_element
->condition
,
1104 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1109 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1110 if (!condition_list_element
) {
1114 client_list_element
= zmalloc(sizeof(*client_list_element
));
1115 if (!client_list_element
) {
1121 * Add the newly-subscribed condition to the client's subscription list.
1123 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1124 condition_list_element
->condition
= condition
;
1125 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1127 client_list
= get_client_list_from_condition(state
, condition
);
1130 * No notification-emiting trigger registered with this
1131 * condition. We don't evaluate the condition right away
1132 * since this trigger is not registered yet.
1134 free(client_list_element
);
1139 * The condition to which the client just subscribed is evaluated
1140 * at this point so that conditions that are already TRUE result
1141 * in a notification being sent out.
1143 * The client_list's trigger is used without locking the list itself.
1144 * This is correct since the list doesn't own the trigger and the
1145 * object is immutable.
1147 struct lttng_trigger_ht_element
*trigger_ht_element
;
1148 pthread_mutex_lock(&client_list
->lock
);
1149 cds_list_for_each_entry(trigger_ht_element
,
1150 &client_list
->triggers_list
, client_list_trigger_node
) {
1151 if (evaluate_condition_for_client(trigger_ht_element
->trigger
, condition
,
1153 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1155 free(client_list_element
);
1159 pthread_mutex_unlock(&client_list
->lock
);
1162 * Add the client to the list of clients interested in a given trigger
1163 * if a "notification" trigger with a corresponding condition was
1166 client_list_element
->client
= client
;
1167 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1169 pthread_mutex_lock(&client_list
->lock
);
1170 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1171 pthread_mutex_unlock(&client_list
->lock
);
1177 notification_client_list_put(client_list
);
1181 free(condition_list_element
);
1182 free(client_list_element
);
1187 int notification_thread_client_unsubscribe(
1188 struct notification_client
*client
,
1189 struct lttng_condition
*condition
,
1190 struct notification_thread_state
*state
,
1191 enum lttng_notification_channel_status
*_status
)
1193 struct notification_client_list
*client_list
;
1194 struct lttng_condition_list_element
*condition_list_element
,
1196 struct notification_client_list_element
*client_list_element
,
1198 bool condition_found
= false;
1199 enum lttng_notification_channel_status status
=
1200 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1202 /* Remove the condition from the client's condition list. */
1203 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1204 &client
->condition_list
, node
) {
1205 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1210 cds_list_del(&condition_list_element
->node
);
1212 * The caller may be iterating on the client's conditions to
1213 * tear down a client's connection. In this case, the condition
1214 * will be destroyed at the end.
1216 if (condition
!= condition_list_element
->condition
) {
1217 lttng_condition_destroy(
1218 condition_list_element
->condition
);
1220 free(condition_list_element
);
1221 condition_found
= true;
1225 if (!condition_found
) {
1226 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1231 * Remove the client from the list of clients interested the trigger
1232 * matching the condition.
1234 client_list
= get_client_list_from_condition(state
, condition
);
1239 pthread_mutex_lock(&client_list
->lock
);
1240 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1241 &client_list
->clients_list
, node
) {
1242 if (client_list_element
->client
->id
!= client
->id
) {
1245 cds_list_del(&client_list_element
->node
);
1246 free(client_list_element
);
1249 pthread_mutex_unlock(&client_list
->lock
);
1250 notification_client_list_put(client_list
);
1253 lttng_condition_destroy(condition
);
1261 void free_notification_client_rcu(struct rcu_head
*node
)
1263 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1267 void notification_client_destroy(struct notification_client
*client
,
1268 struct notification_thread_state
*state
)
1275 * The client object is not reachable by other threads, no need to lock
1278 if (client
->socket
>= 0) {
1279 (void) lttcomm_close_unix_sock(client
->socket
);
1280 client
->socket
= -1;
1282 client
->communication
.active
= false;
1283 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1284 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1285 pthread_mutex_destroy(&client
->lock
);
1286 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1290 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1294 struct notification_client
*get_client_from_socket(int socket
,
1295 struct notification_thread_state
*state
)
1297 struct cds_lfht_iter iter
;
1298 struct cds_lfht_node
*node
;
1299 struct notification_client
*client
= NULL
;
1301 cds_lfht_lookup(state
->client_socket_ht
,
1302 hash_client_socket(socket
),
1303 match_client_socket
,
1304 (void *) (unsigned long) socket
,
1306 node
= cds_lfht_iter_get_node(&iter
);
1311 client
= caa_container_of(node
, struct notification_client
,
1312 client_socket_ht_node
);
1318 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1322 struct notification_client
*get_client_from_id(notification_client_id id
,
1323 struct notification_thread_state
*state
)
1325 struct cds_lfht_iter iter
;
1326 struct cds_lfht_node
*node
;
1327 struct notification_client
*client
= NULL
;
1329 cds_lfht_lookup(state
->client_id_ht
,
1334 node
= cds_lfht_iter_get_node(&iter
);
1339 client
= caa_container_of(node
, struct notification_client
,
1346 bool buffer_usage_condition_applies_to_channel(
1347 const struct lttng_condition
*condition
,
1348 const struct channel_info
*channel_info
)
1350 enum lttng_condition_status status
;
1351 enum lttng_domain_type condition_domain
;
1352 const char *condition_session_name
= NULL
;
1353 const char *condition_channel_name
= NULL
;
1355 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1357 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1358 if (channel_info
->key
.domain
!= condition_domain
) {
1362 status
= lttng_condition_buffer_usage_get_session_name(
1363 condition
, &condition_session_name
);
1364 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1366 status
= lttng_condition_buffer_usage_get_channel_name(
1367 condition
, &condition_channel_name
);
1368 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1370 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1373 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1383 bool session_consumed_size_condition_applies_to_channel(
1384 const struct lttng_condition
*condition
,
1385 const struct channel_info
*channel_info
)
1387 enum lttng_condition_status status
;
1388 const char *condition_session_name
= NULL
;
1390 status
= lttng_condition_session_consumed_size_get_session_name(
1391 condition
, &condition_session_name
);
1392 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1394 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1404 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1405 const struct channel_info
*channel_info
)
1407 const struct lttng_condition
*condition
;
1408 bool trigger_applies
;
1410 condition
= lttng_trigger_get_const_condition(trigger
);
1415 switch (lttng_condition_get_type(condition
)) {
1416 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1417 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1418 trigger_applies
= buffer_usage_condition_applies_to_channel(
1419 condition
, channel_info
);
1421 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1422 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1423 condition
, channel_info
);
1429 return trigger_applies
;
1434 /* Must be called with RCU read lock held. */
1436 struct lttng_session_trigger_list
*get_session_trigger_list(
1437 struct notification_thread_state
*state
,
1438 const char *session_name
)
1440 struct lttng_session_trigger_list
*list
= NULL
;
1441 struct cds_lfht_node
*node
;
1442 struct cds_lfht_iter iter
;
1444 cds_lfht_lookup(state
->session_triggers_ht
,
1445 hash_key_str(session_name
, lttng_ht_seed
),
1446 match_session_trigger_list
,
1449 node
= cds_lfht_iter_get_node(&iter
);
1452 * Not an error, the list of triggers applying to that session
1453 * will be initialized when the session is created.
1455 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1460 list
= caa_container_of(node
,
1461 struct lttng_session_trigger_list
,
1462 session_triggers_ht_node
);
1468 * Allocate an empty lttng_session_trigger_list for the session named
1471 * No ownership of 'session_name' is assumed by the session trigger list.
1472 * It is the caller's responsability to ensure the session name is alive
1473 * for as long as this list is.
1476 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1477 const char *session_name
,
1478 struct cds_lfht
*session_triggers_ht
)
1480 struct lttng_session_trigger_list
*list
;
1482 list
= zmalloc(sizeof(*list
));
1486 list
->session_name
= session_name
;
1487 CDS_INIT_LIST_HEAD(&list
->list
);
1488 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1489 list
->session_triggers_ht
= session_triggers_ht
;
1492 /* Publish the list through the session_triggers_ht. */
1493 cds_lfht_add(session_triggers_ht
,
1494 hash_key_str(session_name
, lttng_ht_seed
),
1495 &list
->session_triggers_ht_node
);
1502 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1504 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1509 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1511 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1513 /* Empty the list element by element, and then free the list itself. */
1514 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1515 &list
->list
, node
) {
1516 cds_list_del(&trigger_list_element
->node
);
1517 free(trigger_list_element
);
1520 /* Unpublish the list from the session_triggers_ht. */
1521 cds_lfht_del(list
->session_triggers_ht
,
1522 &list
->session_triggers_ht_node
);
1524 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1528 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1529 struct lttng_trigger
*trigger
)
1532 struct lttng_trigger_list_element
*new_element
=
1533 zmalloc(sizeof(*new_element
));
1539 CDS_INIT_LIST_HEAD(&new_element
->node
);
1540 new_element
->trigger
= trigger
;
1541 cds_list_add(&new_element
->node
, &list
->list
);
1547 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1548 const char *session_name
)
1550 bool applies
= false;
1551 const struct lttng_condition
*condition
;
1553 condition
= lttng_trigger_get_const_condition(trigger
);
1554 switch (lttng_condition_get_type(condition
)) {
1555 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1556 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1558 enum lttng_condition_status condition_status
;
1559 const char *condition_session_name
;
1561 condition_status
= lttng_condition_session_rotation_get_session_name(
1562 condition
, &condition_session_name
);
1563 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1564 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1568 assert(condition_session_name
);
1569 applies
= !strcmp(condition_session_name
, session_name
);
1580 * Allocate and initialize an lttng_session_trigger_list which contains
1581 * all triggers that apply to the session named 'session_name'.
1583 * No ownership of 'session_name' is assumed by the session trigger list.
1584 * It is the caller's responsability to ensure the session name is alive
1585 * for as long as this list is.
1588 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1589 const struct notification_thread_state
*state
,
1590 const char *session_name
)
1592 int trigger_count
= 0;
1593 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1594 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1595 struct cds_lfht_iter iter
;
1597 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1598 state
->session_triggers_ht
);
1600 /* Add all triggers applying to the session named 'session_name'. */
1601 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1605 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1610 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1611 trigger_ht_element
->trigger
);
1619 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1621 return session_trigger_list
;
1623 lttng_session_trigger_list_destroy(session_trigger_list
);
1628 struct session_info
*find_or_create_session_info(
1629 struct notification_thread_state
*state
,
1630 const char *name
, uid_t uid
, gid_t gid
)
1632 struct session_info
*session
= NULL
;
1633 struct cds_lfht_node
*node
;
1634 struct cds_lfht_iter iter
;
1635 struct lttng_session_trigger_list
*trigger_list
;
1638 cds_lfht_lookup(state
->sessions_ht
,
1639 hash_key_str(name
, lttng_ht_seed
),
1643 node
= cds_lfht_iter_get_node(&iter
);
1645 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1647 session
= caa_container_of(node
, struct session_info
,
1649 assert(session
->uid
== uid
);
1650 assert(session
->gid
== gid
);
1651 session_info_get(session
);
1655 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1656 if (!trigger_list
) {
1660 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1661 state
->sessions_ht
);
1663 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1665 lttng_session_trigger_list_destroy(trigger_list
);
1668 trigger_list
= NULL
;
1670 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1671 &session
->sessions_ht_node
);
1677 session_info_put(session
);
1682 int handle_notification_thread_command_add_channel(
1683 struct notification_thread_state
*state
,
1684 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1685 const char *channel_name
, enum lttng_domain_type channel_domain
,
1686 uint64_t channel_key_int
, uint64_t channel_capacity
,
1687 enum lttng_error_code
*cmd_result
)
1689 struct cds_list_head trigger_list
;
1690 struct channel_info
*new_channel_info
= NULL
;
1691 struct channel_key channel_key
= {
1692 .key
= channel_key_int
,
1693 .domain
= channel_domain
,
1695 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1696 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1697 int trigger_count
= 0;
1698 struct cds_lfht_iter iter
;
1699 struct session_info
*session_info
= NULL
;
1701 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1702 channel_name
, session_name
, channel_key_int
,
1703 lttng_domain_type_str(channel_domain
));
1705 CDS_INIT_LIST_HEAD(&trigger_list
);
1707 session_info
= find_or_create_session_info(state
, session_name
,
1708 session_uid
, session_gid
);
1709 if (!session_info
) {
1710 /* Allocation error or an internal error occurred. */
1714 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1715 channel_capacity
, session_info
);
1716 if (!new_channel_info
) {
1721 /* Build a list of all triggers applying to the new channel. */
1722 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1724 struct lttng_trigger_list_element
*new_element
;
1726 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1727 new_channel_info
)) {
1731 new_element
= zmalloc(sizeof(*new_element
));
1736 CDS_INIT_LIST_HEAD(&new_element
->node
);
1737 new_element
->trigger
= trigger_ht_element
->trigger
;
1738 cds_list_add(&new_element
->node
, &trigger_list
);
1743 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1745 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1746 if (!channel_trigger_list
) {
1749 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1750 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1751 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1752 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1755 /* Add channel to the channel_ht which owns the channel_infos. */
1756 cds_lfht_add(state
->channels_ht
,
1757 hash_channel_key(&new_channel_info
->key
),
1758 &new_channel_info
->channels_ht_node
);
1760 * Add the list of triggers associated with this channel to the
1761 * channel_triggers_ht.
1763 cds_lfht_add(state
->channel_triggers_ht
,
1764 hash_channel_key(&new_channel_info
->key
),
1765 &channel_trigger_list
->channel_triggers_ht_node
);
1767 session_info_put(session_info
);
1768 *cmd_result
= LTTNG_OK
;
1771 channel_info_destroy(new_channel_info
);
1772 session_info_put(session_info
);
1776 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1778 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1783 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1785 free(caa_container_of(node
, struct channel_state_sample
,
1790 int handle_notification_thread_command_remove_channel(
1791 struct notification_thread_state
*state
,
1792 uint64_t channel_key
, enum lttng_domain_type domain
,
1793 enum lttng_error_code
*cmd_result
)
1795 struct cds_lfht_node
*node
;
1796 struct cds_lfht_iter iter
;
1797 struct lttng_channel_trigger_list
*trigger_list
;
1798 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1799 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1800 struct channel_info
*channel_info
;
1802 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1803 channel_key
, lttng_domain_type_str(domain
));
1807 cds_lfht_lookup(state
->channel_triggers_ht
,
1808 hash_channel_key(&key
),
1809 match_channel_trigger_list
,
1812 node
= cds_lfht_iter_get_node(&iter
);
1814 * There is a severe internal error if we are being asked to remove a
1815 * channel that doesn't exist.
1818 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1822 /* Free the list of triggers associated with this channel. */
1823 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1824 channel_triggers_ht_node
);
1825 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1826 &trigger_list
->list
, node
) {
1827 cds_list_del(&trigger_list_element
->node
);
1828 free(trigger_list_element
);
1830 cds_lfht_del(state
->channel_triggers_ht
, node
);
1831 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1833 /* Free sampled channel state. */
1834 cds_lfht_lookup(state
->channel_state_ht
,
1835 hash_channel_key(&key
),
1836 match_channel_state_sample
,
1839 node
= cds_lfht_iter_get_node(&iter
);
1841 * This is expected to be NULL if the channel is destroyed before we
1842 * received a sample.
1845 struct channel_state_sample
*sample
= caa_container_of(node
,
1846 struct channel_state_sample
,
1847 channel_state_ht_node
);
1849 cds_lfht_del(state
->channel_state_ht
, node
);
1850 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1853 /* Remove the channel from the channels_ht and free it. */
1854 cds_lfht_lookup(state
->channels_ht
,
1855 hash_channel_key(&key
),
1859 node
= cds_lfht_iter_get_node(&iter
);
1861 channel_info
= caa_container_of(node
, struct channel_info
,
1863 cds_lfht_del(state
->channels_ht
, node
);
1864 channel_info_destroy(channel_info
);
1867 *cmd_result
= LTTNG_OK
;
1872 int handle_notification_thread_command_session_rotation(
1873 struct notification_thread_state
*state
,
1874 enum notification_thread_command_type cmd_type
,
1875 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1876 uint64_t trace_archive_chunk_id
,
1877 struct lttng_trace_archive_location
*location
,
1878 enum lttng_error_code
*_cmd_result
)
1881 enum lttng_error_code cmd_result
= LTTNG_OK
;
1882 struct lttng_session_trigger_list
*trigger_list
;
1883 struct lttng_trigger_list_element
*trigger_list_element
;
1884 struct session_info
*session_info
;
1885 const struct lttng_credentials session_creds
= {
1886 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1887 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1892 session_info
= find_or_create_session_info(state
, session_name
,
1893 session_uid
, session_gid
);
1894 if (!session_info
) {
1895 /* Allocation error or an internal error occurred. */
1897 cmd_result
= LTTNG_ERR_NOMEM
;
1901 session_info
->rotation
.ongoing
=
1902 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1903 session_info
->rotation
.id
= trace_archive_chunk_id
;
1904 trigger_list
= get_session_trigger_list(state
, session_name
);
1905 if (!trigger_list
) {
1906 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1911 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1913 const struct lttng_condition
*condition
;
1914 struct lttng_trigger
*trigger
;
1915 struct notification_client_list
*client_list
;
1916 struct lttng_evaluation
*evaluation
= NULL
;
1917 enum lttng_condition_type condition_type
;
1918 enum action_executor_status executor_status
;
1920 trigger
= trigger_list_element
->trigger
;
1921 condition
= lttng_trigger_get_const_condition(trigger
);
1923 condition_type
= lttng_condition_get_type(condition
);
1925 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1926 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1928 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1929 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1933 client_list
= get_client_list_from_condition(state
, condition
);
1934 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1935 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1936 trace_archive_chunk_id
);
1938 evaluation
= lttng_evaluation_session_rotation_completed_create(
1939 trace_archive_chunk_id
, location
);
1943 /* Internal error */
1945 cmd_result
= LTTNG_ERR_UNK
;
1950 * Ownership of `evaluation` transferred to the action executor
1951 * no matter the result.
1953 executor_status
= action_executor_enqueue(state
->executor
,
1954 trigger
, evaluation
, &session_creds
,
1957 switch (executor_status
) {
1958 case ACTION_EXECUTOR_STATUS_OK
:
1960 case ACTION_EXECUTOR_STATUS_ERROR
:
1961 case ACTION_EXECUTOR_STATUS_INVALID
:
1963 * TODO Add trigger identification (name/id) when
1964 * it is added to the API.
1966 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1969 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1971 * TODO Add trigger identification (name/id) when
1972 * it is added to the API.
1974 * Not a fatal error.
1976 WARN("No space left when enqueuing action associated with session rotation trigger");
1984 notification_client_list_put(client_list
);
1985 if (caa_unlikely(ret
)) {
1990 session_info_put(session_info
);
1991 *_cmd_result
= cmd_result
;
1997 int handle_notification_thread_command_add_tracer_event_source(
1998 struct notification_thread_state
*state
,
1999 int tracer_event_source_fd
,
2000 enum lttng_domain_type domain_type
,
2001 enum lttng_error_code
*_cmd_result
)
2004 enum lttng_error_code cmd_result
= LTTNG_OK
;
2005 struct notification_event_tracer_event_source_element
*element
= NULL
;
2007 element
= zmalloc(sizeof(*element
));
2009 cmd_result
= LTTNG_ERR_NOMEM
;
2014 element
->fd
= tracer_event_source_fd
;
2015 element
->domain
= domain_type
;
2017 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2019 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2020 tracer_event_source_fd
,
2021 lttng_domain_type_str(domain_type
));
2023 /* Adding the read side pipe to the event poll. */
2024 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
2026 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2027 tracer_event_source_fd
,
2028 lttng_domain_type_str(element
->domain
));
2029 cds_list_del(&element
->node
);
2034 element
->is_fd_in_poll_set
= true;
2037 *_cmd_result
= cmd_result
;
2042 int drain_event_notifier_notification_pipe(
2043 struct notification_thread_state
*state
,
2044 int pipe
, enum lttng_domain_type domain
)
2046 struct lttng_poll_event events
= {0};
2049 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2051 ERR("[notification-thread] Error creating lttng_poll_event");
2055 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2057 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2064 * Continue to consume notifications as long as there are new
2065 * ones coming in. The tracer has been asked to stop producing
2068 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2069 * monitored (on Linux, at least) and will be returned when
2070 * the pipe is closed but empty.
2072 ret
= lttng_poll_wait_interruptible(&events
, 0);
2073 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2074 /* No more notification to be read on this pipe. */
2077 } else if (ret
< 0) {
2078 PERROR("Failed on lttng_poll_wait_interruptible() call");
2083 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2085 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2090 lttng_poll_clean(&events
);
2095 int handle_notification_thread_command_remove_tracer_event_source(
2096 struct notification_thread_state
*state
,
2097 int tracer_event_source_fd
,
2098 enum lttng_error_code
*_cmd_result
)
2102 enum lttng_error_code cmd_result
= LTTNG_OK
;
2103 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
2105 cds_list_for_each_entry_safe(source_element
, tmp
,
2106 &state
->tracer_event_sources_list
, node
) {
2107 if (source_element
->fd
!= tracer_event_source_fd
) {
2111 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2112 tracer_event_source_fd
,
2113 lttng_domain_type_str(source_element
->domain
));
2114 cds_list_del(&source_element
->node
);
2121 * This is temporarily allowed since the poll activity set is
2122 * not properly cleaned-up for the moment. This is adressed in
2125 source_element
= NULL
;
2129 if (!source_element
->is_fd_in_poll_set
) {
2130 /* Skip the poll set removal. */
2134 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2135 tracer_event_source_fd
,
2136 lttng_domain_type_str(source_element
->domain
));
2138 /* Removing the fd from the event poll set. */
2139 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2141 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2142 tracer_event_source_fd
,
2143 lttng_domain_type_str(source_element
->domain
));
2144 cmd_result
= LTTNG_ERR_FATAL
;
2148 source_element
->is_fd_in_poll_set
= false;
2150 ret
= drain_event_notifier_notification_pipe(state
, tracer_event_source_fd
,
2151 source_element
->domain
);
2153 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2154 tracer_event_source_fd
,
2155 lttng_domain_type_str(source_element
->domain
));
2156 cmd_result
= LTTNG_ERR_FATAL
;
2161 * The drain_event_notifier_notification_pipe() call might have read
2162 * data from an fd that we received in event in the latest _poll_wait()
2163 * call. Make sure the thread call poll_wait() again to ensure we have
2166 state
->restart_poll
= true;
2169 free(source_element
);
2170 *_cmd_result
= cmd_result
;
2175 int condition_on_event_update_error_count(struct lttng_trigger
*trigger
)
2178 uint64_t error_count
= 0;
2179 struct lttng_condition
*condition
;
2180 enum event_notifier_error_accounting_status status
;
2182 condition
= lttng_trigger_get_condition(trigger
);
2184 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
2186 status
= event_notifier_error_accounting_get_count(trigger
, &error_count
);
2187 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2188 ERR("Error getting event notifier error count.");
2192 lttng_condition_on_event_set_error_count(condition
, error_count
);
2197 int handle_notification_thread_remove_tracer_event_source_no_result(
2198 struct notification_thread_state
*state
,
2199 int tracer_event_source_fd
)
2202 enum lttng_error_code cmd_result
;
2204 ret
= handle_notification_thread_command_remove_tracer_event_source(
2205 state
, tracer_event_source_fd
, &cmd_result
);
2210 static int handle_notification_thread_command_list_triggers(
2211 struct notification_thread_handle
*handle
,
2212 struct notification_thread_state
*state
,
2214 struct lttng_triggers
**triggers
,
2215 enum lttng_error_code
*_cmd_result
)
2218 enum lttng_error_code cmd_result
= LTTNG_OK
;
2219 struct cds_lfht_iter iter
;
2220 struct lttng_trigger_ht_element
*trigger_ht_element
;
2221 struct lttng_triggers
*local_triggers
= NULL
;
2222 const struct lttng_credentials
*creds
;
2226 local_triggers
= lttng_triggers_create();
2227 if (!local_triggers
) {
2228 /* Not a fatal error. */
2229 cmd_result
= LTTNG_ERR_NOMEM
;
2233 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2234 trigger_ht_element
, node
) {
2236 * Only return the triggers to which the client has access.
2237 * The root user has visibility over all triggers.
2239 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2240 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2244 if (lttng_trigger_needs_tracer_notifier(trigger_ht_element
->trigger
)) {
2245 ret
= condition_on_event_update_error_count(
2246 trigger_ht_element
->trigger
);
2250 ret
= lttng_triggers_add(local_triggers
,
2251 trigger_ht_element
->trigger
);
2253 /* Not a fatal error. */
2255 cmd_result
= LTTNG_ERR_NOMEM
;
2260 /* Transferring ownership to the caller. */
2261 *triggers
= local_triggers
;
2262 local_triggers
= NULL
;
2266 lttng_triggers_destroy(local_triggers
);
2267 *_cmd_result
= cmd_result
;
2272 int handle_notification_thread_command_get_trigger(
2273 struct notification_thread_state
*state
,
2274 const struct lttng_trigger
*trigger
,
2275 struct lttng_trigger
**real_trigger
,
2276 enum lttng_error_code
*_cmd_result
)
2279 struct cds_lfht_iter iter
;
2280 struct lttng_trigger_ht_element
*trigger_ht_element
;
2281 enum lttng_error_code cmd_result
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2285 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2286 trigger_ht_element
, node
) {
2288 if (lttng_trigger_is_equal(trigger
, trigger_ht_element
->trigger
)) {
2290 * Take one reference on the return trigger.
2292 *real_trigger
= trigger_ht_element
->trigger
;
2293 lttng_trigger_get(*real_trigger
);
2295 cmd_result
= LTTNG_OK
;
2303 *_cmd_result
= cmd_result
;
2308 bool condition_is_supported(struct lttng_condition
*condition
)
2312 switch (lttng_condition_get_type(condition
)) {
2313 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2314 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2317 enum lttng_domain_type domain
;
2319 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2323 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2324 is_supported
= true;
2329 * Older kernel tracers don't expose the API to monitor their
2330 * buffers. Therefore, we reject triggers that require that
2331 * mechanism to be available to be evaluated.
2333 * Assume unsupported on error.
2335 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2338 case LTTNG_CONDITION_TYPE_ON_EVENT
:
2340 const struct lttng_event_rule
*event_rule
;
2341 enum lttng_domain_type domain
;
2342 const enum lttng_condition_status status
=
2343 lttng_condition_on_event_get_rule(
2344 condition
, &event_rule
);
2346 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2348 domain
= lttng_event_rule_get_domain_type(event_rule
);
2349 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2350 is_supported
= true;
2355 * Older kernel tracers can't emit notification. Therefore, we
2356 * reject triggers that require that mechanism to be available
2359 * Assume unsupported on error.
2361 is_supported
= kernel_supports_event_notifiers() == 1;
2365 is_supported
= true;
2368 return is_supported
;
2371 /* Must be called with RCU read lock held. */
2373 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2374 struct notification_thread_state
*state
)
2377 const struct lttng_condition
*condition
;
2378 const char *session_name
;
2379 struct lttng_session_trigger_list
*trigger_list
;
2381 condition
= lttng_trigger_get_const_condition(trigger
);
2382 switch (lttng_condition_get_type(condition
)) {
2383 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2384 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2386 enum lttng_condition_status status
;
2388 status
= lttng_condition_session_rotation_get_session_name(
2389 condition
, &session_name
);
2390 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2391 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2402 trigger_list
= get_session_trigger_list(state
, session_name
);
2403 if (!trigger_list
) {
2404 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2410 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2412 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2417 /* Must be called with RCU read lock held. */
2419 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2420 struct notification_thread_state
*state
)
2423 struct cds_lfht_node
*node
;
2424 struct cds_lfht_iter iter
;
2425 struct channel_info
*channel
;
2427 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2429 struct lttng_trigger_list_element
*trigger_list_element
;
2430 struct lttng_channel_trigger_list
*trigger_list
;
2431 struct cds_lfht_iter lookup_iter
;
2433 if (!trigger_applies_to_channel(trigger
, channel
)) {
2437 cds_lfht_lookup(state
->channel_triggers_ht
,
2438 hash_channel_key(&channel
->key
),
2439 match_channel_trigger_list
,
2442 node
= cds_lfht_iter_get_node(&lookup_iter
);
2444 trigger_list
= caa_container_of(node
,
2445 struct lttng_channel_trigger_list
,
2446 channel_triggers_ht_node
);
2448 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2449 if (!trigger_list_element
) {
2453 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2454 trigger_list_element
->trigger
= trigger
;
2455 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2456 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2464 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2466 bool is_notify
= false;
2467 unsigned int i
, count
;
2468 enum lttng_action_status action_status
;
2469 const struct lttng_action
*action
=
2470 lttng_trigger_get_const_action(trigger
);
2471 enum lttng_action_type action_type
;
2474 action_type
= lttng_action_get_type(action
);
2475 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2478 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2482 action_status
= lttng_action_group_get_count(action
, &count
);
2483 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2485 for (i
= 0; i
< count
; i
++) {
2486 const struct lttng_action
*inner_action
=
2487 lttng_action_group_get_at_index(
2490 action_type
= lttng_action_get_type(inner_action
);
2491 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2501 static bool trigger_name_taken(struct notification_thread_state
*state
,
2502 const struct lttng_trigger
*trigger
)
2504 struct cds_lfht_iter iter
;
2507 * No duplicata is allowed in the triggers_by_name_uid_ht.
2508 * The match is done against the trigger name and uid.
2510 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2511 hash_trigger_by_name_uid(trigger
),
2512 match_trigger_by_name_uid
,
2515 return !!cds_lfht_iter_get_node(&iter
);
2519 enum lttng_error_code
generate_trigger_name(
2520 struct notification_thread_state
*state
,
2521 struct lttng_trigger
*trigger
, const char **name
)
2523 enum lttng_error_code ret_code
= LTTNG_OK
;
2525 enum lttng_trigger_status status
;
2528 const int ret
= lttng_trigger_generate_name(trigger
,
2529 state
->trigger_id
.name_offset
++);
2531 /* The only reason this can fail right now. */
2532 ret_code
= LTTNG_ERR_NOMEM
;
2536 status
= lttng_trigger_get_name(trigger
, name
);
2537 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2539 taken
= trigger_name_taken(state
, trigger
);
2540 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2546 void notif_thread_state_remove_trigger_ht_elem(
2547 struct notification_thread_state
*state
,
2548 struct lttng_trigger_ht_element
*trigger_ht_element
)
2551 assert(trigger_ht_element
);
2553 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2554 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2558 enum lttng_error_code
setup_tracer_notifier(
2559 struct notification_thread_state
*state
,
2560 struct lttng_trigger
*trigger
)
2562 enum lttng_error_code ret
;
2563 enum event_notifier_error_accounting_status error_accounting_status
;
2564 struct cds_lfht_node
*node
;
2565 uint64_t error_counter_index
= 0;
2566 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2567 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2569 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2570 if (!trigger_tokens_ht_element
) {
2571 ret
= LTTNG_ERR_NOMEM
;
2575 /* Add trigger token to the trigger_tokens_ht. */
2576 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2577 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2578 trigger_tokens_ht_element
->trigger
= trigger
;
2580 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2581 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2582 match_trigger_token
,
2583 &trigger_tokens_ht_element
->token
,
2584 &trigger_tokens_ht_element
->node
);
2585 if (node
!= &trigger_tokens_ht_element
->node
) {
2586 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2587 goto error_free_ht_element
;
2590 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2591 trigger
, &error_counter_index
);
2592 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2593 if (error_accounting_status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2594 DBG("Trigger group error accounting counter full.");
2595 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2597 ERR("Error registering trigger for error accounting");
2598 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2601 goto error_remove_ht_element
;
2604 lttng_condition_on_event_set_error_counter_index(
2605 condition
, error_counter_index
);
2610 error_remove_ht_element
:
2611 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2612 error_free_ht_element
:
2613 free(trigger_tokens_ht_element
);
2619 void set_action_incr_value_tracer_token(struct notification_thread_state
*state
,
2620 struct lttng_action
*action
)
2622 lttng_action_incr_value_set_tracer_token(action
,
2623 state
->trigger_id
.next_tracer_token
++);
2627 void set_all_action_incr_value_tracer_token(struct notification_thread_state
*state
,
2628 struct lttng_action
*action
)
2630 unsigned int i
, count
;
2631 enum lttng_action_status action_status
;
2632 enum lttng_action_type action_type
;
2633 action_type
= lttng_action_get_type(action
);
2635 if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2636 if (action_type
== LTTNG_ACTION_TYPE_INCREMENT_VALUE
) {
2637 set_action_incr_value_tracer_token(state
, action
);
2640 action_status
= lttng_action_group_get_count(action
, &count
);
2641 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2643 for (i
= 0; i
< count
; i
++) {
2644 struct lttng_action
*inner_action
=
2645 lttng_action_group_get_mutable_at_index(
2647 set_all_action_incr_value_tracer_token(state
,
2654 * FIXME A client's credentials are not checked when registering a trigger.
2656 * The effects of this are benign since:
2657 * - The client will succeed in registering the trigger, as it is valid,
2658 * - The trigger will, internally, be bound to the channel/session,
2659 * - The notifications will not be sent since the client's credentials
2660 * are checked against the channel at that moment.
2662 * If this function returns a non-zero value, it means something is
2663 * fundamentally broken and the whole subsystem/thread will be torn down.
2665 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2669 int handle_notification_thread_command_register_trigger(
2670 struct notification_thread_state
*state
,
2671 struct lttng_trigger
*trigger
,
2672 enum lttng_error_code
*cmd_result
)
2675 struct lttng_condition
*condition
;
2676 struct notification_client_list
*client_list
= NULL
;
2677 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2678 struct cds_lfht_node
*node
;
2679 const char* trigger_name
;
2680 bool free_trigger
= true;
2681 struct lttng_evaluation
*evaluation
= NULL
;
2682 struct lttng_credentials object_creds
;
2685 enum action_executor_status executor_status
;
2686 const uint64_t trigger_tracer_token
=
2687 state
->trigger_id
.next_tracer_token
++;
2691 /* Set the trigger's tracer token. */
2692 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2694 set_all_action_incr_value_tracer_token(state
, lttng_trigger_get_action(trigger
));
2696 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2697 LTTNG_TRIGGER_STATUS_UNSET
) {
2698 const enum lttng_error_code ret_code
= generate_trigger_name(
2699 state
, trigger
, &trigger_name
);
2701 if (ret_code
!= LTTNG_OK
) {
2704 *cmd_result
= ret_code
;
2707 } else if (trigger_name_taken(state
, trigger
)) {
2708 /* Not a fatal error. */
2709 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2714 condition
= lttng_trigger_get_condition(trigger
);
2717 /* Some conditions require tracers to implement a minimal ABI version. */
2718 if (!condition_is_supported(condition
)) {
2719 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2723 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2724 if (!trigger_ht_element
) {
2729 /* Add trigger to the trigger_ht. */
2730 cds_lfht_node_init(&trigger_ht_element
->node
);
2731 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2732 trigger_ht_element
->trigger
= trigger
;
2734 node
= cds_lfht_add_unique(state
->triggers_ht
,
2735 lttng_condition_hash(condition
),
2738 &trigger_ht_element
->node
);
2739 if (node
!= &trigger_ht_element
->node
) {
2740 /* Not a fatal error, simply report it to the client. */
2741 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2742 goto error_free_ht_element
;
2745 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2746 hash_trigger_by_name_uid(trigger
),
2747 match_trigger_by_name_uid
,
2749 &trigger_ht_element
->node_by_name_uid
);
2750 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2751 /* Not a fatal error, simply report it to the client. */
2752 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2753 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2754 goto error_free_ht_element
;
2758 * Some triggers might need a tracer notifier depending on its
2759 * condition and actions.
2761 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2762 enum lttng_error_code error_code
;
2764 error_code
= setup_tracer_notifier(state
, trigger
);
2765 if (error_code
!= LTTNG_OK
) {
2766 notif_thread_state_remove_trigger_ht_elem(state
,
2767 trigger_ht_element
);
2768 if (error_code
== LTTNG_ERR_NOMEM
) {
2771 *cmd_result
= error_code
;
2775 goto error_free_ht_element
;
2780 * The rest only applies to triggers that have a "notify" action.
2781 * It is not skipped as this is the only action type currently
2784 if (is_trigger_action_notify(trigger
)) {
2786 * Find or create the client list of this condition. It may
2787 * already be present if another trigger is already registered
2788 * with the same condition.
2790 client_list
= get_client_list_from_condition(state
, condition
);
2793 * No client list for this condition yet. We create new
2794 * one and build it up.
2796 client_list
= notification_client_list_create(state
, condition
);
2798 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2799 goto error_free_ht_element
;
2803 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2805 pthread_mutex_lock(&client_list
->lock
);
2806 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2807 pthread_mutex_unlock(&client_list
->lock
);
2811 * Ownership of the trigger and of its wrapper was transfered to
2812 * the triggers_ht. Same for token ht element if necessary.
2814 trigger_ht_element
= NULL
;
2815 free_trigger
= false;
2817 switch (get_condition_binding_object(condition
)) {
2818 case LTTNG_OBJECT_TYPE_SESSION
:
2819 /* Add the trigger to the list if it matches a known session. */
2820 ret
= bind_trigger_to_matching_session(trigger
, state
);
2822 goto error_free_ht_element
;
2825 case LTTNG_OBJECT_TYPE_CHANNEL
:
2827 * Add the trigger to list of triggers bound to the channels
2830 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2832 goto error_free_ht_element
;
2835 case LTTNG_OBJECT_TYPE_NONE
:
2838 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2840 goto error_free_ht_element
;
2844 * The new trigger's condition must be evaluated against the current
2847 * In the case of `notify` action, nothing preventing clients from
2848 * subscribing to a condition before the corresponding trigger is
2849 * registered, we have to evaluate this new condition right away.
2851 * At some point, we were waiting for the next "evaluation" (e.g. on
2852 * reception of a channel sample) to evaluate this new condition, but
2855 * The reason it was broken is that waiting for the next sample
2856 * does not allow us to properly handle transitions for edge-triggered
2859 * Consider this example: when we handle a new channel sample, we
2860 * evaluate each conditions twice: once with the previous state, and
2861 * again with the newest state. We then use those two results to
2862 * determine whether a state change happened: a condition was false and
2863 * became true. If a state change happened, we have to notify clients.
2865 * Now, if a client subscribes to a given notification and registers
2866 * a trigger *after* that subscription, we have to make sure the
2867 * condition is evaluated at this point while considering only the
2868 * current state. Otherwise, the next evaluation cycle may only see
2869 * that the evaluations remain the same (true for samples n-1 and n) and
2870 * the client will never know that the condition has been met.
2872 switch (get_condition_binding_object(condition
)) {
2873 case LTTNG_OBJECT_TYPE_SESSION
:
2874 ret
= evaluate_session_condition_for_client(condition
, state
,
2875 &evaluation
, &object_uid
,
2877 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2878 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2880 case LTTNG_OBJECT_TYPE_CHANNEL
:
2881 ret
= evaluate_channel_condition_for_client(condition
, state
,
2882 &evaluation
, &object_uid
,
2884 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2885 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2887 case LTTNG_OBJECT_TYPE_NONE
:
2890 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2898 goto error_free_ht_element
;
2901 DBG("Newly registered trigger's condition evaluated to %s",
2902 evaluation
? "true" : "false");
2904 /* Evaluation yielded nothing. Normal exit. */
2910 * Ownership of `evaluation` transferred to the action executor
2911 * no matter the result.
2913 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2914 evaluation
, &object_creds
, client_list
);
2916 switch (executor_status
) {
2917 case ACTION_EXECUTOR_STATUS_OK
:
2919 case ACTION_EXECUTOR_STATUS_ERROR
:
2920 case ACTION_EXECUTOR_STATUS_INVALID
:
2922 * TODO Add trigger identification (name/id) when
2923 * it is added to the API.
2925 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2927 goto error_free_ht_element
;
2928 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2930 * TODO Add trigger identification (name/id) when
2931 * it is added to the API.
2933 * Not a fatal error.
2935 WARN("No space left when enqueuing action associated to newly registered trigger");
2943 *cmd_result
= LTTNG_OK
;
2944 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2945 trigger_name
, trigger_tracer_token
);
2948 error_free_ht_element
:
2949 if (trigger_ht_element
) {
2950 /* Delayed removal due to RCU constraint on delete. */
2951 call_rcu(&trigger_ht_element
->rcu_node
,
2952 free_lttng_trigger_ht_element_rcu
);
2956 lttng_trigger_destroy(trigger
);
2964 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2966 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2971 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2973 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2978 void teardown_tracer_notifier(struct notification_thread_state
*state
,
2979 const struct lttng_trigger
*trigger
)
2981 struct cds_lfht_iter iter
;
2982 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2984 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
2985 trigger_tokens_ht_element
, node
) {
2987 if (!lttng_trigger_is_equal(trigger
,
2988 trigger_tokens_ht_element
->trigger
)) {
2992 event_notifier_error_accounting_unregister_event_notifier(
2993 trigger_tokens_ht_element
->trigger
);
2995 /* TODO talk to all app and remove it */
2996 DBG("[notification-thread] Removed trigger from tokens_ht");
2997 cds_lfht_del(state
->trigger_tokens_ht
,
2998 &trigger_tokens_ht_element
->node
);
3000 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
3001 free_notification_trigger_tokens_ht_element_rcu
);
3008 int handle_notification_thread_command_unregister_trigger(
3009 struct notification_thread_state
*state
,
3010 const struct lttng_trigger
*trigger
,
3011 enum lttng_error_code
*_cmd_reply
)
3013 struct cds_lfht_iter iter
;
3014 struct cds_lfht_node
*triggers_ht_node
;
3015 struct lttng_channel_trigger_list
*trigger_list
;
3016 struct notification_client_list
*client_list
;
3017 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
3018 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
3020 enum lttng_error_code cmd_reply
;
3024 cds_lfht_lookup(state
->triggers_ht
,
3025 lttng_condition_hash(condition
),
3029 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3030 if (!triggers_ht_node
) {
3031 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3034 cmd_reply
= LTTNG_OK
;
3037 /* Remove trigger from channel_triggers_ht. */
3038 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
3039 channel_triggers_ht_node
) {
3040 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3042 cds_list_for_each_entry_safe(trigger_element
, tmp
,
3043 &trigger_list
->list
, node
) {
3044 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3048 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
3049 cds_list_del(&trigger_element
->node
);
3050 /* A trigger can only appear once per channel */
3055 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3056 teardown_tracer_notifier(state
, trigger
);
3059 trigger_ht_element
= caa_container_of(triggers_ht_node
,
3060 struct lttng_trigger_ht_element
, node
);
3062 if (is_trigger_action_notify(trigger
)) {
3064 * Remove and release the client list from
3065 * notification_trigger_clients_ht.
3067 client_list
= get_client_list_from_condition(state
, condition
);
3068 assert(client_list
);
3070 pthread_mutex_lock(&client_list
->lock
);
3071 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3072 pthread_mutex_unlock(&client_list
->lock
);
3074 /* Put new reference and the hashtable's reference. */
3075 notification_client_list_put(client_list
);
3076 notification_client_list_put(client_list
);
3080 /* Remove trigger from triggers_ht. */
3081 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3083 /* Release the ownership of the trigger. */
3084 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3085 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3089 *_cmd_reply
= cmd_reply
;
3094 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3095 int handle_notification_thread_command(
3096 struct notification_thread_handle
*handle
,
3097 struct notification_thread_state
*state
)
3101 struct notification_thread_command
*cmd
;
3103 /* Read the event pipe to put it back into a quiescent state. */
3104 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
3106 if (ret
!= sizeof(counter
)) {
3110 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3111 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3112 struct notification_thread_command
, cmd_list_node
);
3113 cds_list_del(&cmd
->cmd_list_node
);
3114 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3116 DBG("[notification-thread] Received `%s` command",
3117 notification_command_type_str(cmd
->type
));
3118 switch (cmd
->type
) {
3119 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3120 ret
= handle_notification_thread_command_register_trigger(state
,
3121 cmd
->parameters
.register_trigger
.trigger
,
3124 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3125 ret
= handle_notification_thread_command_unregister_trigger(
3127 cmd
->parameters
.unregister_trigger
.trigger
,
3130 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3131 ret
= handle_notification_thread_command_add_channel(
3133 cmd
->parameters
.add_channel
.session
.name
,
3134 cmd
->parameters
.add_channel
.session
.uid
,
3135 cmd
->parameters
.add_channel
.session
.gid
,
3136 cmd
->parameters
.add_channel
.channel
.name
,
3137 cmd
->parameters
.add_channel
.channel
.domain
,
3138 cmd
->parameters
.add_channel
.channel
.key
,
3139 cmd
->parameters
.add_channel
.channel
.capacity
,
3142 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3143 ret
= handle_notification_thread_command_remove_channel(
3144 state
, cmd
->parameters
.remove_channel
.key
,
3145 cmd
->parameters
.remove_channel
.domain
,
3148 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3149 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3150 ret
= handle_notification_thread_command_session_rotation(
3153 cmd
->parameters
.session_rotation
.session_name
,
3154 cmd
->parameters
.session_rotation
.uid
,
3155 cmd
->parameters
.session_rotation
.gid
,
3156 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3157 cmd
->parameters
.session_rotation
.location
,
3160 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3161 ret
= handle_notification_thread_command_add_tracer_event_source(
3163 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3164 cmd
->parameters
.tracer_event_source
.domain
,
3167 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3168 ret
= handle_notification_thread_command_remove_tracer_event_source(
3170 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3173 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3175 struct lttng_triggers
*triggers
= NULL
;
3177 ret
= handle_notification_thread_command_list_triggers(
3180 cmd
->parameters
.list_triggers
.uid
,
3183 cmd
->reply
.list_triggers
.triggers
= triggers
;
3187 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3188 cmd
->reply_code
= LTTNG_OK
;
3191 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3193 struct lttng_trigger
*trigger
= NULL
;
3195 ret
= handle_notification_thread_command_get_trigger(
3197 cmd
->parameters
.get_trigger
.trigger
,
3200 cmd
->reply
.get_trigger
.trigger
= trigger
;
3204 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3206 const enum client_transmission_status client_status
=
3207 cmd
->parameters
.client_communication_update
3209 const notification_client_id client_id
=
3210 cmd
->parameters
.client_communication_update
.id
;
3211 struct notification_client
*client
;
3214 client
= get_client_from_id(client_id
, state
);
3218 * Client error was probably already picked-up by the
3219 * notification thread or it has disconnected
3220 * gracefully while this command was queued.
3222 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3226 ret
= client_handle_transmission_status(
3227 client
, client_status
, state
);
3233 ERR("[notification-thread] Unknown internal command received");
3241 if (cmd
->is_async
) {
3245 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3249 /* Wake-up and return a fatal error to the calling thread. */
3250 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3251 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3253 /* Indicate a fatal error to the caller. */
3258 int socket_set_non_blocking(int socket
)
3262 /* Set the pipe as non-blocking. */
3263 ret
= fcntl(socket
, F_GETFL
, 0);
3265 PERROR("fcntl get socket flags");
3270 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3272 PERROR("fcntl set O_NONBLOCK socket flag");
3275 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3281 int client_reset_inbound_state(struct notification_client
*client
)
3286 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3288 client
->communication
.inbound
.bytes_to_receive
=
3289 sizeof(struct lttng_notification_channel_message
);
3290 client
->communication
.inbound
.msg_type
=
3291 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3292 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3293 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3294 ret
= lttng_dynamic_buffer_set_size(
3295 &client
->communication
.inbound
.payload
.buffer
,
3296 client
->communication
.inbound
.bytes_to_receive
);
3301 int handle_notification_thread_client_connect(
3302 struct notification_thread_state
*state
)
3305 struct notification_client
*client
;
3307 DBG("[notification-thread] Handling new notification channel client connection");
3309 client
= zmalloc(sizeof(*client
));
3316 pthread_mutex_init(&client
->lock
, NULL
);
3317 client
->id
= state
->next_notification_client_id
++;
3318 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3319 lttng_payload_init(&client
->communication
.inbound
.payload
);
3320 lttng_payload_init(&client
->communication
.outbound
.payload
);
3321 client
->communication
.inbound
.expect_creds
= true;
3323 ret
= client_reset_inbound_state(client
);
3325 ERR("[notification-thread] Failed to reset client communication's inbound state");
3330 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3332 ERR("[notification-thread] Failed to accept new notification channel client connection");
3337 client
->socket
= ret
;
3339 ret
= socket_set_non_blocking(client
->socket
);
3341 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3345 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3347 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3352 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3353 LPOLLIN
| LPOLLERR
|
3354 LPOLLHUP
| LPOLLRDHUP
);
3356 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3360 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3364 cds_lfht_add(state
->client_socket_ht
,
3365 hash_client_socket(client
->socket
),
3366 &client
->client_socket_ht_node
);
3367 cds_lfht_add(state
->client_id_ht
,
3368 hash_client_id(client
->id
),
3369 &client
->client_id_ht_node
);
3375 notification_client_destroy(client
, state
);
3380 * RCU read-lock must be held by the caller.
3381 * Client lock must _not_ be held by the caller.
3384 int notification_thread_client_disconnect(
3385 struct notification_client
*client
,
3386 struct notification_thread_state
*state
)
3389 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3391 /* Acquire the client lock to disable its communication atomically. */
3392 pthread_mutex_lock(&client
->lock
);
3393 client
->communication
.active
= false;
3394 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3395 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3396 pthread_mutex_unlock(&client
->lock
);
3398 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3400 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3404 /* Release all conditions to which the client was subscribed. */
3405 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3406 &client
->condition_list
, node
) {
3407 (void) notification_thread_client_unsubscribe(client
,
3408 condition_list_element
->condition
, state
, NULL
);
3412 * Client no longer accessible to other threads (through the
3415 notification_client_destroy(client
, state
);
3419 int handle_notification_thread_client_disconnect(
3420 int client_socket
, struct notification_thread_state
*state
)
3423 struct notification_client
*client
;
3426 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3428 client
= get_client_from_socket(client_socket
, state
);
3430 /* Internal state corruption, fatal error. */
3431 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3437 ret
= notification_thread_client_disconnect(client
, state
);
3443 int handle_notification_thread_client_disconnect_all(
3444 struct notification_thread_state
*state
)
3446 struct cds_lfht_iter iter
;
3447 struct notification_client
*client
;
3448 bool error_encoutered
= false;
3451 DBG("[notification-thread] Closing all client connections");
3452 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3453 client_socket_ht_node
) {
3456 ret
= notification_thread_client_disconnect(
3459 error_encoutered
= true;
3463 return error_encoutered
? 1 : 0;
3466 int handle_notification_thread_trigger_unregister_all(
3467 struct notification_thread_state
*state
)
3469 bool error_occurred
= false;
3470 struct cds_lfht_iter iter
;
3471 struct lttng_trigger_ht_element
*trigger_ht_element
;
3474 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3476 int ret
= handle_notification_thread_command_unregister_trigger(
3477 state
, trigger_ht_element
->trigger
, NULL
);
3479 error_occurred
= true;
3483 return error_occurred
? -1 : 0;
3487 int client_handle_transmission_status(
3488 struct notification_client
*client
,
3489 enum client_transmission_status transmission_status
,
3490 struct notification_thread_state
*state
)
3494 switch (transmission_status
) {
3495 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3496 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3497 CLIENT_POLL_MASK_IN
);
3503 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3505 * We want to be notified whenever there is buffer space
3506 * available to send the rest of the payload.
3508 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3509 CLIENT_POLL_MASK_IN_OUT
);
3514 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3515 ret
= notification_thread_client_disconnect(client
, state
);
3520 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3530 /* Client lock must be acquired by caller. */
3532 enum client_transmission_status
client_flush_outgoing_queue(
3533 struct notification_client
*client
)
3536 size_t to_send_count
;
3537 enum client_transmission_status status
;
3538 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3539 &client
->communication
.outbound
.payload
, 0, -1);
3540 const int fds_to_send_count
=
3541 lttng_payload_view_get_fd_handle_count(&pv
);
3543 ASSERT_LOCKED(client
->lock
);
3545 if (!client
->communication
.active
) {
3546 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3550 if (pv
.buffer
.size
== 0) {
3552 * If both data and fds are equal to zero, we are in an invalid
3555 assert(fds_to_send_count
!= 0);
3560 to_send_count
= pv
.buffer
.size
;
3561 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3564 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3567 if ((ret
>= 0 && ret
< to_send_count
)) {
3568 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3570 to_send_count
-= max(ret
, 0);
3572 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3574 pv
.buffer
.size
- to_send_count
,
3576 ret
= lttng_dynamic_buffer_set_size(
3577 &client
->communication
.outbound
.payload
.buffer
,
3583 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3585 } else if (ret
< 0) {
3586 /* Generic error, disable the client's communication. */
3587 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3589 client
->communication
.active
= false;
3590 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3594 * No error and flushed the queue completely.
3596 * The payload buffer size is used later to
3597 * check if there is notifications queued. So albeit that the
3598 * direct caller knows that the transmission is complete, we
3599 * need to set the buffer size to zero.
3601 ret
= lttng_dynamic_buffer_set_size(
3602 &client
->communication
.outbound
.payload
.buffer
, 0);
3609 /* No fds to send, transmission is complete. */
3610 if (fds_to_send_count
== 0) {
3611 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3615 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3616 client
->socket
, &pv
);
3618 /* Generic error, disable the client's communication. */
3619 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3621 client
->communication
.active
= false;
3622 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3624 } else if (ret
== 0) {
3625 /* Nothing could be sent. */
3626 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3628 /* Fd passing is an all or nothing kind of thing. */
3629 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3631 * The payload _fd_array count is used later to
3632 * check if there is notifications queued. So although the
3633 * direct caller knows that the transmission is complete, we
3634 * need to clear the _fd_array for the queuing check.
3636 lttng_dynamic_pointer_array_clear(
3637 &client
->communication
.outbound
.payload
3642 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3643 client
->communication
.outbound
.queued_command_reply
= false;
3644 client
->communication
.outbound
.dropped_notification
= false;
3645 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3650 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3654 bool client_has_outbound_data_left(
3655 const struct notification_client
*client
)
3657 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3658 &client
->communication
.outbound
.payload
, 0, -1);
3659 const bool has_data
= pv
.buffer
.size
!= 0;
3660 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3662 return has_data
|| has_fds
;
3665 /* Client lock must _not_ be held by the caller. */
3667 int client_send_command_reply(struct notification_client
*client
,
3668 struct notification_thread_state
*state
,
3669 enum lttng_notification_channel_status status
)
3672 struct lttng_notification_channel_command_reply reply
= {
3673 .status
= (int8_t) status
,
3675 struct lttng_notification_channel_message msg
= {
3676 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3677 .size
= sizeof(reply
),
3679 char buffer
[sizeof(msg
) + sizeof(reply
)];
3680 enum client_transmission_status transmission_status
;
3682 memcpy(buffer
, &msg
, sizeof(msg
));
3683 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3684 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3686 pthread_mutex_lock(&client
->lock
);
3687 if (client
->communication
.outbound
.queued_command_reply
) {
3688 /* Protocol error. */
3692 /* Enqueue buffer to outgoing queue and flush it. */
3693 ret
= lttng_dynamic_buffer_append(
3694 &client
->communication
.outbound
.payload
.buffer
,
3695 buffer
, sizeof(buffer
));
3700 transmission_status
= client_flush_outgoing_queue(client
);
3702 if (client_has_outbound_data_left(client
)) {
3703 /* Queue could not be emptied. */
3704 client
->communication
.outbound
.queued_command_reply
= true;
3707 pthread_mutex_unlock(&client
->lock
);
3708 ret
= client_handle_transmission_status(
3709 client
, transmission_status
, state
);
3716 pthread_mutex_unlock(&client
->lock
);
3722 int client_handle_message_unknown(struct notification_client
*client
,
3723 struct notification_thread_state
*state
)
3727 * Receiving message header. The function will be called again
3728 * once the rest of the message as been received and can be
3731 const struct lttng_notification_channel_message
*msg
;
3733 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3734 msg
= (const struct lttng_notification_channel_message
*)
3735 client
->communication
.inbound
.payload
.buffer
.data
;
3737 if (msg
->size
== 0 ||
3738 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3739 ERR("[notification-thread] Invalid notification channel message: length = %u",
3745 switch (msg
->type
) {
3746 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3747 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3748 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3752 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3756 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3757 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3758 client
->communication
.inbound
.msg_type
=
3759 (enum lttng_notification_channel_message_type
) msg
->type
;
3760 ret
= lttng_dynamic_buffer_set_size(
3761 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3763 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3770 int client_handle_message_handshake(struct notification_client
*client
,
3771 struct notification_thread_state
*state
)
3774 struct lttng_notification_channel_command_handshake
*handshake_client
;
3775 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3776 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3777 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3779 const struct lttng_notification_channel_message msg_header
= {
3780 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3781 .size
= sizeof(handshake_reply
),
3783 enum lttng_notification_channel_status status
=
3784 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3785 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3787 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3788 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3789 sizeof(handshake_reply
));
3792 (struct lttng_notification_channel_command_handshake
*)
3793 client
->communication
.inbound
.payload
.buffer
3795 client
->major
= handshake_client
->major
;
3796 client
->minor
= handshake_client
->minor
;
3797 if (!client
->communication
.inbound
.creds_received
) {
3798 ERR("[notification-thread] No credentials received from client");
3803 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3804 &client
->communication
.inbound
.creds
);
3805 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3806 &client
->communication
.inbound
.creds
);
3807 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3808 client
->uid
, client
->gid
, (int) client
->major
,
3809 (int) client
->minor
);
3811 if (handshake_client
->major
!=
3812 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3813 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3816 pthread_mutex_lock(&client
->lock
);
3817 /* Outgoing queue will be flushed when the command reply is sent. */
3818 ret
= lttng_dynamic_buffer_append(
3819 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3820 sizeof(send_buffer
));
3822 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3826 client
->validated
= true;
3827 client
->communication
.active
= true;
3828 pthread_mutex_unlock(&client
->lock
);
3830 /* Set reception state to receive the next message header. */
3831 ret
= client_reset_inbound_state(client
);
3833 ERR("[notification-thread] Failed to reset client communication's inbound state");
3837 /* Flushes the outgoing queue. */
3838 ret
= client_send_command_reply(client
, state
, status
);
3840 ERR("[notification-thread] Failed to send reply to notification channel client");
3846 pthread_mutex_unlock(&client
->lock
);
3852 int client_handle_message_subscription(
3853 struct notification_client
*client
,
3854 enum lttng_notification_channel_message_type msg_type
,
3855 struct notification_thread_state
*state
)
3858 struct lttng_condition
*condition
;
3859 enum lttng_notification_channel_status status
=
3860 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3861 struct lttng_payload_view condition_view
=
3862 lttng_payload_view_from_payload(
3863 &client
->communication
.inbound
.payload
,
3865 size_t expected_condition_size
;
3868 * No need to lock client to sample the inbound state as the only
3869 * other thread accessing clients (action executor) only uses the
3872 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3873 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3874 if (ret
!= expected_condition_size
) {
3875 ERR("[notification-thread] Malformed condition received from client");
3879 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3880 ret
= notification_thread_client_subscribe(
3881 client
, condition
, state
, &status
);
3883 ret
= notification_thread_client_unsubscribe(
3884 client
, condition
, state
, &status
);
3891 /* Set reception state to receive the next message header. */
3892 ret
= client_reset_inbound_state(client
);
3894 ERR("[notification-thread] Failed to reset client communication's inbound state");
3898 ret
= client_send_command_reply(client
, state
, status
);
3900 ERR("[notification-thread] Failed to send reply to notification channel client");
3909 int client_dispatch_message(struct notification_client
*client
,
3910 struct notification_thread_state
*state
)
3914 if (client
->communication
.inbound
.msg_type
!=
3915 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3916 client
->communication
.inbound
.msg_type
!=
3917 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3918 !client
->validated
) {
3919 WARN("[notification-thread] client attempted a command before handshake");
3924 switch (client
->communication
.inbound
.msg_type
) {
3925 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3927 ret
= client_handle_message_unknown(client
, state
);
3930 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3932 ret
= client_handle_message_handshake(client
, state
);
3935 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3936 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3938 ret
= client_handle_message_subscription(client
,
3939 client
->communication
.inbound
.msg_type
, state
);
3949 /* Incoming data from client. */
3950 int handle_notification_thread_client_in(
3951 struct notification_thread_state
*state
, int socket
)
3954 struct notification_client
*client
;
3959 client
= get_client_from_socket(socket
, state
);
3961 /* Internal error, abort. */
3966 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3967 client
->communication
.inbound
.bytes_to_receive
;
3968 if (client
->communication
.inbound
.expect_creds
) {
3969 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3970 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3971 client
->communication
.inbound
.bytes_to_receive
,
3972 &client
->communication
.inbound
.creds
);
3974 client
->communication
.inbound
.expect_creds
= false;
3975 client
->communication
.inbound
.creds_received
= true;
3978 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3979 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3980 client
->communication
.inbound
.bytes_to_receive
);
3982 if (recv_ret
>= 0) {
3983 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3985 goto error_disconnect_client
;
3988 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3989 /* Message incomplete wait for more data. */
3994 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3997 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3998 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
4000 client
->communication
.inbound
.fds_to_receive
,
4001 &client
->communication
.inbound
.payload
);
4004 * Fds received. non blocking fds passing is all
4007 ssize_t expected_size
;
4009 expected_size
= sizeof(int) *
4010 client
->communication
.inbound
4012 assert(ret
== expected_size
);
4013 client
->communication
.inbound
.fds_to_receive
= 0;
4014 } else if (ret
== 0) {
4015 /* Received nothing. */
4019 goto error_disconnect_client
;
4023 /* At this point the message is complete.*/
4024 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4025 client
->communication
.inbound
.fds_to_receive
== 0);
4026 ret
= client_dispatch_message(client
, state
);
4029 * Only returns an error if this client must be
4032 goto error_disconnect_client
;
4039 error_disconnect_client
:
4040 ret
= notification_thread_client_disconnect(client
, state
);
4044 /* Client ready to receive outgoing data. */
4045 int handle_notification_thread_client_out(
4046 struct notification_thread_state
*state
, int socket
)
4049 struct notification_client
*client
;
4050 enum client_transmission_status transmission_status
;
4053 client
= get_client_from_socket(socket
, state
);
4055 /* Internal error, abort. */
4060 pthread_mutex_lock(&client
->lock
);
4061 transmission_status
= client_flush_outgoing_queue(client
);
4062 pthread_mutex_unlock(&client
->lock
);
4064 ret
= client_handle_transmission_status(
4065 client
, transmission_status
, state
);
4075 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4076 const struct channel_state_sample
*sample
,
4077 uint64_t buffer_capacity
)
4079 bool result
= false;
4081 enum lttng_condition_type condition_type
;
4082 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4083 condition
, struct lttng_condition_buffer_usage
,
4086 if (use_condition
->threshold_bytes
.set
) {
4087 threshold
= use_condition
->threshold_bytes
.value
;
4090 * Threshold was expressed as a ratio.
4092 * TODO the threshold (in bytes) of conditions expressed
4093 * as a ratio of total buffer size could be cached to
4094 * forego this double-multiplication or it could be performed
4095 * as fixed-point math.
4097 * Note that caching should accommodates the case where the
4098 * condition applies to multiple channels (i.e. don't assume
4099 * that all channels matching my_chann* have the same size...)
4101 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4102 (double) buffer_capacity
);
4105 condition_type
= lttng_condition_get_type(condition
);
4106 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4107 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4108 threshold
, sample
->highest_usage
);
4111 * The low condition should only be triggered once _all_ of the
4112 * streams in a channel have gone below the "low" threshold.
4114 if (sample
->highest_usage
<= threshold
) {
4118 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4119 threshold
, sample
->highest_usage
);
4122 * For high buffer usage scenarios, we want to trigger whenever
4123 * _any_ of the streams has reached the "high" threshold.
4125 if (sample
->highest_usage
>= threshold
) {
4134 bool evaluate_session_consumed_size_condition(
4135 const struct lttng_condition
*condition
,
4136 uint64_t session_consumed_size
)
4139 const struct lttng_condition_session_consumed_size
*size_condition
=
4140 container_of(condition
,
4141 struct lttng_condition_session_consumed_size
,
4144 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4145 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4146 threshold
, session_consumed_size
);
4147 return session_consumed_size
>= threshold
;
4151 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4152 struct lttng_evaluation
**evaluation
,
4153 const struct notification_thread_state
*state
,
4154 const struct channel_state_sample
*previous_sample
,
4155 const struct channel_state_sample
*latest_sample
,
4156 uint64_t previous_session_consumed_total
,
4157 uint64_t latest_session_consumed_total
,
4158 struct channel_info
*channel_info
)
4161 enum lttng_condition_type condition_type
;
4162 const bool previous_sample_available
= !!previous_sample
;
4163 bool previous_sample_result
= false;
4164 bool latest_sample_result
;
4166 condition_type
= lttng_condition_get_type(condition
);
4168 switch (condition_type
) {
4169 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4170 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4171 if (caa_likely(previous_sample_available
)) {
4172 previous_sample_result
=
4173 evaluate_buffer_usage_condition(condition
,
4174 previous_sample
, channel_info
->capacity
);
4176 latest_sample_result
= evaluate_buffer_usage_condition(
4177 condition
, latest_sample
,
4178 channel_info
->capacity
);
4180 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4181 if (caa_likely(previous_sample_available
)) {
4182 previous_sample_result
=
4183 evaluate_session_consumed_size_condition(
4185 previous_session_consumed_total
);
4187 latest_sample_result
=
4188 evaluate_session_consumed_size_condition(
4190 latest_session_consumed_total
);
4193 /* Unknown condition type; internal error. */
4197 if (!latest_sample_result
||
4198 (previous_sample_result
== latest_sample_result
)) {
4200 * Only trigger on a condition evaluation transition.
4202 * NOTE: This edge-triggered logic may not be appropriate for
4203 * future condition types.
4208 if (!evaluation
|| !latest_sample_result
) {
4212 switch (condition_type
) {
4213 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4214 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4215 *evaluation
= lttng_evaluation_buffer_usage_create(
4217 latest_sample
->highest_usage
,
4218 channel_info
->capacity
);
4220 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4221 *evaluation
= lttng_evaluation_session_consumed_size_create(
4222 latest_session_consumed_total
);
4237 int client_notification_overflow(struct notification_client
*client
)
4240 const struct lttng_notification_channel_message msg
= {
4241 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4244 ASSERT_LOCKED(client
->lock
);
4246 DBG("Dropping notification addressed to client (socket fd = %i)",
4248 if (client
->communication
.outbound
.dropped_notification
) {
4250 * The client already has a "notification dropped" message
4251 * in its outgoing queue. Nothing to do since all
4252 * of those messages are coalesced.
4257 client
->communication
.outbound
.dropped_notification
= true;
4258 ret
= lttng_dynamic_buffer_append(
4259 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4262 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4269 static int client_handle_transmission_status_wrapper(
4270 struct notification_client
*client
,
4271 enum client_transmission_status status
,
4274 return client_handle_transmission_status(client
, status
,
4275 (struct notification_thread_state
*) user_data
);
4279 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4280 const struct lttng_evaluation
*evaluation
,
4281 struct notification_client_list
* client_list
,
4282 struct notification_thread_state
*state
,
4283 uid_t object_uid
, gid_t object_gid
)
4285 const struct lttng_credentials creds
= {
4286 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4287 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4290 return notification_client_list_send_evaluation(client_list
,
4291 lttng_trigger_get_const_condition(trigger
), evaluation
,
4292 lttng_trigger_get_credentials(trigger
),
4294 client_handle_transmission_status_wrapper
, state
);
4298 * Permission checks relative to notification channel clients are performed
4299 * here. Notice how object, client, and trigger credentials are involved in
4302 * The `object` credentials are the credentials associated with the "subject"
4303 * of a condition. For instance, a `rotation completed` condition applies
4304 * to a session. When that condition is met, it will produce an evaluation
4305 * against a session. Hence, in this case, the `object` credentials are the
4306 * credentials of the "subject" session.
4308 * The `trigger` credentials are the credentials of the user that registered the
4311 * The `client` credentials are the credentials of the user that created a given
4312 * notification channel.
4314 * In terms of visibility, it is expected that non-privilieged users can only
4315 * register triggers against "their" objects (their own sessions and
4316 * applications they are allowed to interact with). They can then open a
4317 * notification channel and subscribe to notifications associated with those
4320 * As for privilieged users, they can register triggers against the objects of
4321 * other users. They can then subscribe to the notifications associated to their
4322 * triggers. Privilieged users _can't_ subscribe to the notifications of
4323 * triggers owned by other users; they must create their own triggers.
4325 * This is more a concern of usability than security. It would be difficult for
4326 * a root user reliably subscribe to a specific set of conditions without
4327 * interference from external users (those could, for instance, unregister
4331 int notification_client_list_send_evaluation(
4332 struct notification_client_list
*client_list
,
4333 const struct lttng_condition
*condition
,
4334 const struct lttng_evaluation
*evaluation
,
4335 const struct lttng_credentials
*trigger_creds
,
4336 const struct lttng_credentials
*source_object_creds
,
4337 report_client_transmission_result_cb client_report
,
4341 struct lttng_payload msg_payload
;
4342 struct notification_client_list_element
*client_list_element
, *tmp
;
4343 const struct lttng_notification notification
= {
4344 .condition
= (struct lttng_condition
*) condition
,
4345 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4347 struct lttng_notification_channel_message msg_header
= {
4348 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4351 lttng_payload_init(&msg_payload
);
4353 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4354 sizeof(msg_header
));
4359 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4361 ERR("[notification-thread] Failed to serialize notification");
4366 /* Update payload size. */
4367 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4368 ->size
= (uint32_t)(
4369 msg_payload
.buffer
.size
- sizeof(msg_header
));
4371 /* Update the payload number of fds. */
4373 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4374 &msg_payload
, 0, -1);
4376 ((struct lttng_notification_channel_message
*)
4377 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4378 lttng_payload_view_get_fd_handle_count(&pv
);
4381 pthread_mutex_lock(&client_list
->lock
);
4382 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4383 &client_list
->clients_list
, node
) {
4384 enum client_transmission_status transmission_status
;
4385 struct notification_client
*client
=
4386 client_list_element
->client
;
4389 pthread_mutex_lock(&client
->lock
);
4390 if (!client
->communication
.active
) {
4392 * Skip inactive client (protocol error or
4395 DBG("Skipping client at it is marked as inactive");
4399 if (source_object_creds
) {
4400 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4401 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4404 * Client is not allowed to monitor this
4407 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4412 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
4413 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4417 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4418 client
->socket
, msg_payload
.buffer
.size
);
4420 if (client_has_outbound_data_left(client
)) {
4422 * Outgoing data is already buffered for this client;
4423 * drop the notification and enqueue a "dropped
4424 * notification" message if this is the first dropped
4425 * notification since the socket spilled-over to the
4428 ret
= client_notification_overflow(client
);
4435 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4441 transmission_status
= client_flush_outgoing_queue(client
);
4442 pthread_mutex_unlock(&client
->lock
);
4443 ret
= client_report(client
, transmission_status
, user_data
);
4446 goto end_unlock_list
;
4452 pthread_mutex_unlock(&client
->lock
);
4455 goto end_unlock_list
;
4461 pthread_mutex_unlock(&client_list
->lock
);
4463 lttng_payload_reset(&msg_payload
);
4468 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4469 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4473 struct lttng_event_notifier_notification
*notification
= NULL
;
4474 char *capture_buffer
= NULL
;
4475 size_t capture_buffer_size
;
4476 void *reception_buffer
;
4477 size_t reception_size
;
4479 struct lttng_ust_event_notifier_notification ust_notification
;
4480 struct lttng_kernel_event_notifier_notification kernel_notification
;
4482 /* Init lttng_event_notifier_notification */
4484 case LTTNG_DOMAIN_UST
:
4485 reception_buffer
= (void *) &ust_notification
;
4486 reception_size
= sizeof(ust_notification
);
4488 case LTTNG_DOMAIN_KERNEL
:
4489 reception_buffer
= (void *) &kernel_notification
;
4490 reception_size
= sizeof(kernel_notification
);
4497 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4498 * ensuring that read/write of tracer notifications are atomic.
4500 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4502 if (ret
!= reception_size
) {
4503 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4504 notification_pipe_read_fd
, reception_size
, ret
);
4510 case LTTNG_DOMAIN_UST
:
4511 token
= ust_notification
.token
;
4512 capture_buffer_size
= ust_notification
.capture_buf_size
;
4514 case LTTNG_DOMAIN_KERNEL
:
4515 token
= kernel_notification
.token
;
4516 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4522 if (capture_buffer_size
== 0) {
4523 capture_buffer
= NULL
;
4527 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4528 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",
4529 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4533 capture_buffer
= zmalloc(capture_buffer_size
);
4534 if (!capture_buffer
) {
4535 ERR("[notification-thread] Failed to allocate capture buffer");
4539 /* Fetch additional payload (capture). */
4540 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4541 if (ret
!= capture_buffer_size
) {
4542 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4543 notification_pipe_read_fd
);
4548 notification
= lttng_event_notifier_notification_create(token
, domain
,
4549 capture_buffer
, capture_buffer_size
);
4550 if (notification
== NULL
) {
4555 * Ownership transfered to the lttng_event_notifier_notification object.
4557 capture_buffer
= NULL
;
4560 free(capture_buffer
);
4561 return notification
;
4565 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4566 struct lttng_event_notifier_notification
*notification
)
4568 struct cds_lfht_node
*node
;
4569 struct cds_lfht_iter iter
;
4570 struct notification_trigger_tokens_ht_element
*element
;
4571 enum lttng_trigger_status trigger_status
;
4572 struct lttng_evaluation
*evaluation
= NULL
;
4573 enum action_executor_status executor_status
;
4574 struct notification_client_list
*client_list
= NULL
;
4575 const char *trigger_name
;
4577 unsigned int capture_count
= 0;
4579 /* Find triggers associated with this token. */
4581 cds_lfht_lookup(state
->trigger_tokens_ht
,
4582 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4583 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4584 node
= cds_lfht_iter_get_node(&iter
);
4585 if (caa_unlikely(!node
)) {
4587 * This is not an error, slow consumption of the tracer
4588 * notifications can lead to situations where a trigger is
4589 * removed but we still get tracer notifications matching a
4590 * trigger that no longer exists.
4596 element
= caa_container_of(node
,
4597 struct notification_trigger_tokens_ht_element
,
4600 if (!lttng_trigger_should_fire(element
->trigger
)) {
4605 lttng_trigger_fire(element
->trigger
);
4607 trigger_status
= lttng_trigger_get_name(element
->trigger
, &trigger_name
);
4608 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4610 if (lttng_condition_on_event_get_capture_descriptor_count(
4611 lttng_trigger_get_const_condition(element
->trigger
),
4612 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4613 ERR("Failed to get capture count");
4618 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4619 ERR("Expected capture but capture buffer is null");
4624 evaluation
= lttng_evaluation_event_rule_create(
4625 container_of(lttng_trigger_get_const_condition(
4627 struct lttng_condition_on_event
,
4630 notification
->capture_buffer
,
4631 notification
->capture_buf_size
, false);
4633 if (evaluation
== NULL
) {
4634 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
4638 client_list
= get_client_list_from_condition(state
,
4639 lttng_trigger_get_const_condition(element
->trigger
));
4640 executor_status
= action_executor_enqueue(state
->executor
,
4641 element
->trigger
, evaluation
, NULL
, client_list
);
4642 switch (executor_status
) {
4643 case ACTION_EXECUTOR_STATUS_OK
:
4646 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4648 struct notification_client_list_element
*client_list_element
,
4652 * Not a fatal error; this is expected and simply means the
4653 * executor has too much work queued already.
4657 /* No clients subscribed to notifications for this trigger. */
4662 /* Warn clients that a notification (or more) was dropped. */
4663 pthread_mutex_lock(&client_list
->lock
);
4664 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4665 &client_list
->clients_list
, node
) {
4666 enum client_transmission_status transmission_status
;
4667 struct notification_client
*client
=
4668 client_list_element
->client
;
4670 pthread_mutex_lock(&client
->lock
);
4671 ret
= client_notification_overflow(client
);
4677 transmission_status
=
4678 client_flush_outgoing_queue(client
);
4679 ret
= client_handle_transmission_status(
4680 client
, transmission_status
, state
);
4686 pthread_mutex_unlock(&client
->lock
);
4692 pthread_mutex_unlock(&client_list
->lock
);
4695 case ACTION_EXECUTOR_STATUS_INVALID
:
4696 case ACTION_EXECUTOR_STATUS_ERROR
:
4697 /* Fatal error, shut down everything. */
4698 ERR("Fatal error encoutered while enqueuing action to the action executor");
4702 /* Unhandled error. */
4707 notification_client_list_put(client_list
);
4714 int handle_one_event_notifier_notification(
4715 struct notification_thread_state
*state
,
4716 int pipe
, enum lttng_domain_type domain
)
4719 struct lttng_event_notifier_notification
*notification
= NULL
;
4721 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4722 if (notification
== NULL
) {
4723 /* Reception failed, don't consider it fatal. */
4724 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4725 pipe
, lttng_domain_type_str(domain
));
4729 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4731 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4732 pipe
, lttng_domain_type_str(domain
));
4737 lttng_event_notifier_notification_destroy(notification
);
4741 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4742 int pipe
, enum lttng_domain_type domain
)
4744 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4747 int handle_notification_thread_channel_sample(
4748 struct notification_thread_state
*state
, int pipe
,
4749 enum lttng_domain_type domain
)
4752 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4753 struct channel_info
*channel_info
;
4754 struct cds_lfht_node
*node
;
4755 struct cds_lfht_iter iter
;
4756 struct lttng_channel_trigger_list
*trigger_list
;
4757 struct lttng_trigger_list_element
*trigger_list_element
;
4758 bool previous_sample_available
= false;
4759 struct channel_state_sample previous_sample
, latest_sample
;
4760 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4761 struct lttng_credentials channel_creds
;
4764 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4765 * ensuring that read/write of sampling messages are atomic.
4767 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4768 if (ret
!= sizeof(sample_msg
)) {
4769 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4776 latest_sample
.key
.key
= sample_msg
.key
;
4777 latest_sample
.key
.domain
= domain
;
4778 latest_sample
.highest_usage
= sample_msg
.highest
;
4779 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4780 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4784 /* Retrieve the channel's informations */
4785 cds_lfht_lookup(state
->channels_ht
,
4786 hash_channel_key(&latest_sample
.key
),
4790 node
= cds_lfht_iter_get_node(&iter
);
4791 if (caa_unlikely(!node
)) {
4793 * Not an error since the consumer can push a sample to the pipe
4794 * and the rest of the session daemon could notify us of the
4795 * channel's destruction before we get a chance to process that
4798 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4799 latest_sample
.key
.key
,
4800 lttng_domain_type_str(domain
));
4803 channel_info
= caa_container_of(node
, struct channel_info
,
4805 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4807 latest_sample
.key
.key
,
4808 channel_info
->session_info
->name
,
4809 latest_sample
.highest_usage
,
4810 latest_sample
.lowest_usage
,
4811 latest_sample
.channel_total_consumed
);
4813 previous_session_consumed_total
=
4814 channel_info
->session_info
->consumed_data_size
;
4816 /* Retrieve the channel's last sample, if it exists, and update it. */
4817 cds_lfht_lookup(state
->channel_state_ht
,
4818 hash_channel_key(&latest_sample
.key
),
4819 match_channel_state_sample
,
4822 node
= cds_lfht_iter_get_node(&iter
);
4823 if (caa_likely(node
)) {
4824 struct channel_state_sample
*stored_sample
;
4826 /* Update the sample stored. */
4827 stored_sample
= caa_container_of(node
,
4828 struct channel_state_sample
,
4829 channel_state_ht_node
);
4831 memcpy(&previous_sample
, stored_sample
,
4832 sizeof(previous_sample
));
4833 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4834 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4835 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4836 previous_sample_available
= true;
4838 latest_session_consumed_total
=
4839 previous_session_consumed_total
+
4840 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4843 * This is the channel's first sample, allocate space for and
4844 * store the new sample.
4846 struct channel_state_sample
*stored_sample
;
4848 stored_sample
= zmalloc(sizeof(*stored_sample
));
4849 if (!stored_sample
) {
4854 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4855 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4856 cds_lfht_add(state
->channel_state_ht
,
4857 hash_channel_key(&stored_sample
->key
),
4858 &stored_sample
->channel_state_ht_node
);
4860 latest_session_consumed_total
=
4861 previous_session_consumed_total
+
4862 latest_sample
.channel_total_consumed
;
4865 channel_info
->session_info
->consumed_data_size
=
4866 latest_session_consumed_total
;
4868 /* Find triggers associated with this channel. */
4869 cds_lfht_lookup(state
->channel_triggers_ht
,
4870 hash_channel_key(&latest_sample
.key
),
4871 match_channel_trigger_list
,
4874 node
= cds_lfht_iter_get_node(&iter
);
4875 if (caa_likely(!node
)) {
4879 channel_creds
= (typeof(channel_creds
)) {
4880 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4881 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4884 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4885 channel_triggers_ht_node
);
4886 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4888 const struct lttng_condition
*condition
;
4889 struct lttng_trigger
*trigger
;
4890 struct notification_client_list
*client_list
= NULL
;
4891 struct lttng_evaluation
*evaluation
= NULL
;
4892 enum action_executor_status executor_status
;
4895 trigger
= trigger_list_element
->trigger
;
4896 condition
= lttng_trigger_get_const_condition(trigger
);
4900 * Check if any client is subscribed to the result of this
4903 client_list
= get_client_list_from_condition(state
, condition
);
4905 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4906 previous_sample_available
? &previous_sample
: NULL
,
4908 previous_session_consumed_total
,
4909 latest_session_consumed_total
,
4911 if (caa_unlikely(ret
)) {
4915 if (caa_likely(!evaluation
)) {
4919 if (!lttng_trigger_should_fire(trigger
)) {
4923 lttng_trigger_fire(trigger
);
4926 * Ownership of `evaluation` transferred to the action executor
4927 * no matter the result.
4929 executor_status
= action_executor_enqueue(state
->executor
,
4930 trigger
, evaluation
, &channel_creds
,
4933 switch (executor_status
) {
4934 case ACTION_EXECUTOR_STATUS_OK
:
4936 case ACTION_EXECUTOR_STATUS_ERROR
:
4937 case ACTION_EXECUTOR_STATUS_INVALID
:
4939 * TODO Add trigger identification (name/id) when
4940 * it is added to the API.
4942 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4945 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4947 * TODO Add trigger identification (name/id) when
4948 * it is added to the API.
4950 * Not a fatal error.
4952 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4960 notification_client_list_put(client_list
);
4961 if (caa_unlikely(ret
)) {