2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.h"
12 #include <urcu/rculfhash.h>
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/futex.h>
17 #include <common/unix.h>
18 #include <common/dynamic-buffer.h>
19 #include <common/hashtable/utils.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/macros.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.h>
24 #include <lttng/action/list-internal.h>
25 #include <lttng/domain-internal.h>
26 #include <lttng/notification/notification-internal.h>
27 #include <lttng/condition/condition-internal.h>
28 #include <lttng/condition/buffer-usage-internal.h>
29 #include <lttng/condition/session-consumed-size-internal.h>
30 #include <lttng/condition/session-rotation-internal.h>
31 #include <lttng/condition/event-rule-matches-internal.h>
32 #include <lttng/domain-internal.h>
33 #include <lttng/notification/channel-internal.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <lttng/event-rule/event-rule-internal.h>
42 #include "condition-internal.h"
43 #include "event-notifier-error-accounting.h"
44 #include "notification-thread.h"
45 #include "notification-thread-events.h"
46 #include "notification-thread-commands.h"
47 #include "lttng-sessiond.h"
50 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
51 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
53 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
54 #define MAX_CAPTURE_SIZE (PIPE_BUF)
56 enum lttng_object_type
{
57 LTTNG_OBJECT_TYPE_UNKNOWN
,
58 LTTNG_OBJECT_TYPE_NONE
,
59 LTTNG_OBJECT_TYPE_CHANNEL
,
60 LTTNG_OBJECT_TYPE_SESSION
,
63 struct lttng_trigger_list_element
{
64 /* No ownership of the trigger object is assumed. */
65 struct lttng_trigger
*trigger
;
66 struct cds_list_head node
;
69 struct lttng_channel_trigger_list
{
70 struct channel_key channel_key
;
71 /* List of struct lttng_trigger_list_element. */
72 struct cds_list_head list
;
73 /* Node in the channel_triggers_ht */
74 struct cds_lfht_node channel_triggers_ht_node
;
75 /* call_rcu delayed reclaim. */
76 struct rcu_head rcu_node
;
80 * List of triggers applying to a given session.
83 * - lttng_session_trigger_list_create()
84 * - lttng_session_trigger_list_build()
85 * - lttng_session_trigger_list_destroy()
86 * - lttng_session_trigger_list_add()
88 struct lttng_session_trigger_list
{
90 * Not owned by this; points to the session_info structure's
93 const char *session_name
;
94 /* List of struct lttng_trigger_list_element. */
95 struct cds_list_head list
;
96 /* Node in the session_triggers_ht */
97 struct cds_lfht_node session_triggers_ht_node
;
99 * Weak reference to the notification system's session triggers
102 * The session trigger list structure structure is owned by
103 * the session's session_info.
105 * The session_info is kept alive the the channel_infos holding a
106 * reference to it (reference counting). When those channels are
107 * destroyed (at runtime or on teardown), the reference they hold
108 * to the session_info are released. On destruction of session_info,
109 * session_info_destroy() will remove the list of triggers applying
110 * to this session from the notification system's state.
112 * This implies that the session_triggers_ht must be destroyed
113 * after the channels.
115 struct cds_lfht
*session_triggers_ht
;
116 /* Used for delayed RCU reclaim. */
117 struct rcu_head rcu_node
;
120 struct lttng_trigger_ht_element
{
121 struct lttng_trigger
*trigger
;
122 struct cds_lfht_node node
;
123 struct cds_lfht_node node_by_name_uid
;
124 struct cds_list_head client_list_trigger_node
;
125 /* call_rcu delayed reclaim. */
126 struct rcu_head rcu_node
;
129 struct lttng_condition_list_element
{
130 struct lttng_condition
*condition
;
131 struct cds_list_head node
;
134 struct channel_state_sample
{
135 struct channel_key key
;
136 struct cds_lfht_node channel_state_ht_node
;
137 uint64_t highest_usage
;
138 uint64_t lowest_usage
;
139 uint64_t channel_total_consumed
;
140 /* call_rcu delayed reclaim. */
141 struct rcu_head rcu_node
;
144 static unsigned long hash_channel_key(struct channel_key
*key
);
145 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
146 struct lttng_evaluation
**evaluation
,
147 const struct notification_thread_state
*state
,
148 const struct channel_state_sample
*previous_sample
,
149 const struct channel_state_sample
*latest_sample
,
150 uint64_t previous_session_consumed_total
,
151 uint64_t latest_session_consumed_total
,
152 struct channel_info
*channel_info
);
154 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
155 const struct lttng_evaluation
*evaluation
,
156 struct notification_client_list
*client_list
,
157 struct notification_thread_state
*state
,
158 uid_t channel_uid
, gid_t channel_gid
);
161 /* session_info API */
163 void session_info_destroy(void *_data
);
165 void session_info_get(struct session_info
*session_info
);
167 void session_info_put(struct session_info
*session_info
);
169 struct session_info
*session_info_create(const char *name
,
170 uid_t uid
, gid_t gid
,
171 struct lttng_session_trigger_list
*trigger_list
,
172 struct cds_lfht
*sessions_ht
);
174 void session_info_add_channel(struct session_info
*session_info
,
175 struct channel_info
*channel_info
);
177 void session_info_remove_channel(struct session_info
*session_info
,
178 struct channel_info
*channel_info
);
180 /* lttng_session_trigger_list API */
182 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
183 const char *session_name
,
184 struct cds_lfht
*session_triggers_ht
);
186 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
187 const struct notification_thread_state
*state
,
188 const char *session_name
);
190 void lttng_session_trigger_list_destroy(
191 struct lttng_session_trigger_list
*list
);
193 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
194 struct lttng_trigger
*trigger
);
197 int client_handle_transmission_status(
198 struct notification_client
*client
,
199 enum client_transmission_status transmission_status
,
200 struct notification_thread_state
*state
);
203 int handle_one_event_notifier_notification(
204 struct notification_thread_state
*state
,
205 int pipe
, enum lttng_domain_type domain
);
208 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
211 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
213 /* This double-cast is intended to supress pointer-to-cast warning. */
214 const int socket
= (int) (intptr_t) key
;
215 const struct notification_client
*client
= caa_container_of(node
,
216 struct notification_client
, client_socket_ht_node
);
218 return client
->socket
== socket
;
222 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
224 /* This double-cast is intended to supress pointer-to-cast warning. */
225 const notification_client_id id
= *((notification_client_id
*) key
);
226 const struct notification_client
*client
= caa_container_of(
227 node
, struct notification_client
, client_id_ht_node
);
229 return client
->id
== id
;
233 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
235 struct channel_key
*channel_key
= (struct channel_key
*) key
;
236 struct lttng_channel_trigger_list
*trigger_list
;
238 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
239 channel_triggers_ht_node
);
241 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
242 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
246 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
248 const char *session_name
= (const char *) key
;
249 struct lttng_session_trigger_list
*trigger_list
;
251 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
252 session_triggers_ht_node
);
254 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
258 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
260 struct channel_key
*channel_key
= (struct channel_key
*) key
;
261 struct channel_state_sample
*sample
;
263 sample
= caa_container_of(node
, struct channel_state_sample
,
264 channel_state_ht_node
);
266 return !!((channel_key
->key
== sample
->key
.key
) &&
267 (channel_key
->domain
== sample
->key
.domain
));
271 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
273 struct channel_key
*channel_key
= (struct channel_key
*) key
;
274 struct channel_info
*channel_info
;
276 channel_info
= caa_container_of(node
, struct channel_info
,
279 return !!((channel_key
->key
== channel_info
->key
.key
) &&
280 (channel_key
->domain
== channel_info
->key
.domain
));
284 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
286 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
287 struct lttng_trigger_ht_element
*trigger_ht_element
;
289 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
292 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
296 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
298 const uint64_t *_key
= key
;
299 struct notification_trigger_tokens_ht_element
*element
;
301 element
= caa_container_of(node
,
302 struct notification_trigger_tokens_ht_element
, node
);
303 return *_key
== element
->token
;
307 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
309 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
310 struct notification_client_list
*client_list
;
311 const struct lttng_condition
*condition
;
313 LTTNG_ASSERT(condition_key
);
315 client_list
= caa_container_of(node
, struct notification_client_list
,
316 notification_trigger_clients_ht_node
);
317 condition
= client_list
->condition
;
319 return !!lttng_condition_is_equal(condition_key
, condition
);
323 int match_session(struct cds_lfht_node
*node
, const void *key
)
325 const char *name
= key
;
326 struct session_info
*session_info
= caa_container_of(
327 node
, struct session_info
, sessions_ht_node
);
329 return !strcmp(session_info
->name
, name
);
333 const char *notification_command_type_str(
334 enum notification_thread_command_type type
)
337 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
338 return "REGISTER_TRIGGER";
339 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
340 return "UNREGISTER_TRIGGER";
341 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
342 return "ADD_CHANNEL";
343 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
344 return "REMOVE_CHANNEL";
345 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
346 return "SESSION_ROTATION_ONGOING";
347 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
348 return "SESSION_ROTATION_COMPLETED";
349 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
350 return "ADD_TRACER_EVENT_SOURCE";
351 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
352 return "REMOVE_TRACER_EVENT_SOURCE";
353 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
354 return "LIST_TRIGGERS";
355 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
356 return "GET_TRIGGER";
357 case NOTIFICATION_COMMAND_TYPE_QUIT
:
359 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
360 return "CLIENT_COMMUNICATION_UPDATE";
367 * Match trigger based on name and credentials only.
368 * Name duplication is NOT allowed for the same uid.
371 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
375 const char *element_trigger_name
;
376 const char *key_name
;
377 enum lttng_trigger_status status
;
378 const struct lttng_credentials
*key_creds
;
379 const struct lttng_credentials
*node_creds
;
380 const struct lttng_trigger
*trigger_key
=
381 (const struct lttng_trigger
*) key
;
382 const struct lttng_trigger_ht_element
*trigger_ht_element
=
383 caa_container_of(node
,
384 struct lttng_trigger_ht_element
,
387 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
,
388 &element_trigger_name
);
389 element_trigger_name
= status
== LTTNG_TRIGGER_STATUS_OK
?
390 element_trigger_name
: NULL
;
392 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
393 key_name
= status
== LTTNG_TRIGGER_STATUS_OK
? key_name
: NULL
;
397 * Consider null names as not equal. This is to maintain backwards
398 * compatibility with pre-2.13 anonymous triggers. Multiples anonymous
399 * triggers are allowed for a given user.
401 if (!element_trigger_name
|| !key_name
) {
405 if (strcmp(element_trigger_name
, key_name
) != 0) {
409 /* Compare the owners' UIDs. */
410 key_creds
= lttng_trigger_get_credentials(trigger_key
);
411 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
413 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
420 * Hash trigger based on name and credentials only.
423 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
425 unsigned long hash
= 0;
426 const struct lttng_credentials
*trigger_creds
;
427 const char *trigger_name
;
428 enum lttng_trigger_status status
;
430 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
431 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
432 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
435 trigger_creds
= lttng_trigger_get_credentials(trigger
);
436 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
443 unsigned long hash_channel_key(struct channel_key
*key
)
445 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
446 unsigned long domain_hash
= hash_key_ulong(
447 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
449 return key_hash
^ domain_hash
;
453 unsigned long hash_client_socket(int socket
)
455 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
459 unsigned long hash_client_id(notification_client_id id
)
461 return hash_key_u64(&id
, lttng_ht_seed
);
465 * Get the type of object to which a given condition applies. Bindings let
466 * the notification system evaluate a trigger's condition when a given
467 * object's state is updated.
469 * For instance, a condition bound to a channel will be evaluated everytime
470 * the channel's state is changed by a channel monitoring sample.
473 enum lttng_object_type
get_condition_binding_object(
474 const struct lttng_condition
*condition
)
476 switch (lttng_condition_get_type(condition
)) {
477 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
478 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
479 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
480 return LTTNG_OBJECT_TYPE_CHANNEL
;
481 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
482 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
483 return LTTNG_OBJECT_TYPE_SESSION
;
484 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
485 return LTTNG_OBJECT_TYPE_NONE
;
487 return LTTNG_OBJECT_TYPE_UNKNOWN
;
492 void free_channel_info_rcu(struct rcu_head
*node
)
494 free(caa_container_of(node
, struct channel_info
, rcu_node
));
498 void channel_info_destroy(struct channel_info
*channel_info
)
504 if (channel_info
->session_info
) {
505 session_info_remove_channel(channel_info
->session_info
,
507 session_info_put(channel_info
->session_info
);
509 if (channel_info
->name
) {
510 free(channel_info
->name
);
512 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
516 void free_session_info_rcu(struct rcu_head
*node
)
518 free(caa_container_of(node
, struct session_info
, rcu_node
));
521 /* Don't call directly, use the ref-counting mechanism. */
523 void session_info_destroy(void *_data
)
525 struct session_info
*session_info
= _data
;
528 LTTNG_ASSERT(session_info
);
529 if (session_info
->channel_infos_ht
) {
530 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
532 ERR("Failed to destroy channel information hash table");
535 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
538 cds_lfht_del(session_info
->sessions_ht
,
539 &session_info
->sessions_ht_node
);
541 free(session_info
->name
);
542 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
546 void session_info_get(struct session_info
*session_info
)
551 lttng_ref_get(&session_info
->ref
);
555 void session_info_put(struct session_info
*session_info
)
560 lttng_ref_put(&session_info
->ref
);
564 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
565 struct lttng_session_trigger_list
*trigger_list
,
566 struct cds_lfht
*sessions_ht
)
568 struct session_info
*session_info
;
572 session_info
= zmalloc(sizeof(*session_info
));
576 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
578 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
579 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
580 if (!session_info
->channel_infos_ht
) {
584 cds_lfht_node_init(&session_info
->sessions_ht_node
);
585 session_info
->name
= strdup(name
);
586 if (!session_info
->name
) {
589 session_info
->uid
= uid
;
590 session_info
->gid
= gid
;
591 session_info
->trigger_list
= trigger_list
;
592 session_info
->sessions_ht
= sessions_ht
;
596 session_info_put(session_info
);
601 void session_info_add_channel(struct session_info
*session_info
,
602 struct channel_info
*channel_info
)
605 cds_lfht_add(session_info
->channel_infos_ht
,
606 hash_channel_key(&channel_info
->key
),
607 &channel_info
->session_info_channels_ht_node
);
612 void session_info_remove_channel(struct session_info
*session_info
,
613 struct channel_info
*channel_info
)
616 cds_lfht_del(session_info
->channel_infos_ht
,
617 &channel_info
->session_info_channels_ht_node
);
622 struct channel_info
*channel_info_create(const char *channel_name
,
623 struct channel_key
*channel_key
, uint64_t channel_capacity
,
624 struct session_info
*session_info
)
626 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
632 cds_lfht_node_init(&channel_info
->channels_ht_node
);
633 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
634 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
635 channel_info
->capacity
= channel_capacity
;
637 channel_info
->name
= strdup(channel_name
);
638 if (!channel_info
->name
) {
643 * Set the references between session and channel infos:
644 * - channel_info holds a strong reference to session_info
645 * - session_info holds a weak reference to channel_info
647 session_info_get(session_info
);
648 session_info_add_channel(session_info
, channel_info
);
649 channel_info
->session_info
= session_info
;
653 channel_info_destroy(channel_info
);
658 bool notification_client_list_get(struct notification_client_list
*list
)
660 return urcu_ref_get_unless_zero(&list
->ref
);
664 void free_notification_client_list_rcu(struct rcu_head
*node
)
666 free(caa_container_of(node
, struct notification_client_list
,
671 void notification_client_list_release(struct urcu_ref
*list_ref
)
673 struct notification_client_list
*list
=
674 container_of(list_ref
, typeof(*list
), ref
);
675 struct notification_client_list_element
*client_list_element
, *tmp
;
677 lttng_condition_put(list
->condition
);
679 if (list
->notification_trigger_clients_ht
) {
682 cds_lfht_del(list
->notification_trigger_clients_ht
,
683 &list
->notification_trigger_clients_ht_node
);
685 list
->notification_trigger_clients_ht
= NULL
;
687 cds_list_for_each_entry_safe(client_list_element
, tmp
,
688 &list
->clients_list
, node
) {
689 free(client_list_element
);
692 LTTNG_ASSERT(cds_list_empty(&list
->triggers_list
));
694 pthread_mutex_destroy(&list
->lock
);
695 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
699 bool condition_applies_to_client(const struct lttng_condition
*condition
,
700 struct notification_client
*client
)
702 bool applies
= false;
703 struct lttng_condition_list_element
*condition_list_element
;
705 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
707 applies
= lttng_condition_is_equal(
708 condition_list_element
->condition
,
719 struct notification_client_list
*notification_client_list_create(
720 struct notification_thread_state
*state
,
721 const struct lttng_condition
*condition
)
723 struct notification_client
*client
;
724 struct cds_lfht_iter iter
;
725 struct notification_client_list
*client_list
;
727 client_list
= zmalloc(sizeof(*client_list
));
729 PERROR("Failed to allocate notification client list");
733 pthread_mutex_init(&client_list
->lock
, NULL
);
735 * The trigger that owns the condition has the first reference to this
738 urcu_ref_init(&client_list
->ref
);
739 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
740 CDS_INIT_LIST_HEAD(&client_list
->clients_list
);
741 CDS_INIT_LIST_HEAD(&client_list
->triggers_list
);
744 * Create a copy of the condition so that it's independent of any
745 * trigger. The client list may outlive the trigger object (which owns
746 * the condition) that is used to create it.
748 client_list
->condition
= lttng_condition_copy(condition
);
750 /* Build a list of clients to which this new condition applies. */
751 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
752 client_socket_ht_node
) {
753 struct notification_client_list_element
*client_list_element
;
755 if (!condition_applies_to_client(condition
, client
)) {
759 client_list_element
= zmalloc(sizeof(*client_list_element
));
760 if (!client_list_element
) {
761 goto error_put_client_list
;
764 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
765 client_list_element
->client
= client
;
766 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
769 client_list
->notification_trigger_clients_ht
=
770 state
->notification_trigger_clients_ht
;
774 * Add the client list to the global list of client list.
776 cds_lfht_add_unique(state
->notification_trigger_clients_ht
,
777 lttng_condition_hash(client_list
->condition
),
778 match_client_list_condition
,
779 client_list
->condition
,
780 &client_list
->notification_trigger_clients_ht_node
);
784 error_put_client_list
:
785 notification_client_list_put(client_list
);
792 void notification_client_list_put(struct notification_client_list
*list
)
797 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
800 /* Provides a reference to the returned list. */
802 struct notification_client_list
*get_client_list_from_condition(
803 struct notification_thread_state
*state
,
804 const struct lttng_condition
*condition
)
806 struct cds_lfht_node
*node
;
807 struct cds_lfht_iter iter
;
808 struct notification_client_list
*list
= NULL
;
811 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
812 lttng_condition_hash(condition
),
813 match_client_list_condition
,
816 node
= cds_lfht_iter_get_node(&iter
);
818 list
= container_of(node
, struct notification_client_list
,
819 notification_trigger_clients_ht_node
);
820 list
= notification_client_list_get(list
) ? list
: NULL
;
828 int evaluate_channel_condition_for_client(
829 const struct lttng_condition
*condition
,
830 struct notification_thread_state
*state
,
831 struct lttng_evaluation
**evaluation
,
832 uid_t
*session_uid
, gid_t
*session_gid
)
835 struct cds_lfht_iter iter
;
836 struct cds_lfht_node
*node
;
837 struct channel_info
*channel_info
= NULL
;
838 struct channel_key
*channel_key
= NULL
;
839 struct channel_state_sample
*last_sample
= NULL
;
840 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
844 /* Find the channel associated with the condition. */
845 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
846 channel_trigger_list
, channel_triggers_ht_node
) {
847 struct lttng_trigger_list_element
*element
;
849 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
850 const struct lttng_condition
*current_condition
=
851 lttng_trigger_get_const_condition(
854 LTTNG_ASSERT(current_condition
);
855 if (!lttng_condition_is_equal(condition
,
856 current_condition
)) {
860 /* Found the trigger, save the channel key. */
861 channel_key
= &channel_trigger_list
->channel_key
;
865 /* The channel key was found stop iteration. */
871 /* No channel found; normal exit. */
872 DBG("No known channel associated with newly subscribed-to condition");
877 /* Fetch channel info for the matching channel. */
878 cds_lfht_lookup(state
->channels_ht
,
879 hash_channel_key(channel_key
),
883 node
= cds_lfht_iter_get_node(&iter
);
885 channel_info
= caa_container_of(node
, struct channel_info
,
888 /* Retrieve the channel's last sample, if it exists. */
889 cds_lfht_lookup(state
->channel_state_ht
,
890 hash_channel_key(channel_key
),
891 match_channel_state_sample
,
894 node
= cds_lfht_iter_get_node(&iter
);
896 last_sample
= caa_container_of(node
,
897 struct channel_state_sample
,
898 channel_state_ht_node
);
900 /* Nothing to evaluate, no sample was ever taken. Normal exit */
901 DBG("No channel sample associated with newly subscribed-to condition");
906 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
908 0, channel_info
->session_info
->consumed_data_size
,
911 WARN("Fatal error occurred while evaluating a newly subscribed-to condition");
915 *session_uid
= channel_info
->session_info
->uid
;
916 *session_gid
= channel_info
->session_info
->gid
;
923 const char *get_condition_session_name(const struct lttng_condition
*condition
)
925 const char *session_name
= NULL
;
926 enum lttng_condition_status status
;
928 switch (lttng_condition_get_type(condition
)) {
929 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
930 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
931 status
= lttng_condition_buffer_usage_get_session_name(
932 condition
, &session_name
);
934 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
935 status
= lttng_condition_session_consumed_size_get_session_name(
936 condition
, &session_name
);
938 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
939 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
940 status
= lttng_condition_session_rotation_get_session_name(
941 condition
, &session_name
);
946 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
947 ERR("Failed to retrieve session rotation condition's session name");
955 int evaluate_session_condition_for_client(
956 const struct lttng_condition
*condition
,
957 struct notification_thread_state
*state
,
958 struct lttng_evaluation
**evaluation
,
959 uid_t
*session_uid
, gid_t
*session_gid
)
962 struct cds_lfht_iter iter
;
963 struct cds_lfht_node
*node
;
964 const char *session_name
;
965 struct session_info
*session_info
= NULL
;
968 session_name
= get_condition_session_name(condition
);
970 /* Find the session associated with the trigger. */
971 cds_lfht_lookup(state
->sessions_ht
,
972 hash_key_str(session_name
, lttng_ht_seed
),
976 node
= cds_lfht_iter_get_node(&iter
);
978 DBG("No known session matching name \"%s\"",
984 session_info
= caa_container_of(node
, struct session_info
,
986 session_info_get(session_info
);
989 * Evaluation is performed in-line here since only one type of
990 * session-bound condition is handled for the moment.
992 switch (lttng_condition_get_type(condition
)) {
993 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
994 if (!session_info
->rotation
.ongoing
) {
996 goto end_session_put
;
999 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1000 session_info
->rotation
.id
);
1003 ERR("Failed to create session rotation ongoing evaluation for session \"%s\"",
1004 session_info
->name
);
1006 goto end_session_put
;
1012 goto end_session_put
;
1015 *session_uid
= session_info
->uid
;
1016 *session_gid
= session_info
->gid
;
1019 session_info_put(session_info
);
1026 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1027 const struct lttng_condition
*condition
,
1028 struct notification_client
*client
,
1029 struct notification_thread_state
*state
)
1032 struct lttng_evaluation
*evaluation
= NULL
;
1033 struct notification_client_list client_list
= {
1034 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1036 struct notification_client_list_element client_list_element
= { 0 };
1037 uid_t object_uid
= 0;
1038 gid_t object_gid
= 0;
1040 LTTNG_ASSERT(trigger
);
1041 LTTNG_ASSERT(condition
);
1042 LTTNG_ASSERT(client
);
1043 LTTNG_ASSERT(state
);
1045 switch (get_condition_binding_object(condition
)) {
1046 case LTTNG_OBJECT_TYPE_SESSION
:
1047 ret
= evaluate_session_condition_for_client(condition
, state
,
1048 &evaluation
, &object_uid
, &object_gid
);
1050 case LTTNG_OBJECT_TYPE_CHANNEL
:
1051 ret
= evaluate_channel_condition_for_client(condition
, state
,
1052 &evaluation
, &object_uid
, &object_gid
);
1054 case LTTNG_OBJECT_TYPE_NONE
:
1055 DBG("Newly subscribed-to condition not bound to object, nothing to evaluate");
1058 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1068 /* Evaluation yielded nothing. Normal exit. */
1069 DBG("Newly subscribed-to condition evaluated to false, nothing to report to client");
1075 * Create a temporary client list with the client currently
1078 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1079 CDS_INIT_LIST_HEAD(&client_list
.clients_list
);
1081 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1082 client_list_element
.client
= client
;
1083 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1085 /* Send evaluation result to the newly-subscribed client. */
1086 DBG("Newly subscribed-to condition evaluated to true, notifying client");
1087 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1088 state
, object_uid
, object_gid
);
1095 int notification_thread_client_subscribe(struct notification_client
*client
,
1096 struct lttng_condition
*condition
,
1097 struct notification_thread_state
*state
,
1098 enum lttng_notification_channel_status
*_status
)
1101 struct notification_client_list
*client_list
= NULL
;
1102 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1103 struct notification_client_list_element
*client_list_element
= NULL
;
1104 struct lttng_trigger_ht_element
*trigger_ht_element
;
1105 enum lttng_notification_channel_status status
=
1106 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1109 * Ensure that the client has not already subscribed to this condition
1112 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1113 if (lttng_condition_is_equal(condition_list_element
->condition
,
1115 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1120 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1121 if (!condition_list_element
) {
1125 client_list_element
= zmalloc(sizeof(*client_list_element
));
1126 if (!client_list_element
) {
1132 * Add the newly-subscribed condition to the client's subscription list.
1134 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1135 condition_list_element
->condition
= condition
;
1137 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1139 client_list
= get_client_list_from_condition(
1140 state
, condition_list_element
->condition
);
1143 * No notification-emiting trigger registered with this
1144 * condition. We don't evaluate the condition right away
1145 * since this trigger is not registered yet.
1147 free(client_list_element
);
1152 * The condition to which the client just subscribed is evaluated
1153 * at this point so that conditions that are already TRUE result
1154 * in a notification being sent out.
1156 * Note the iteration on all triggers which share an identical
1157 * `condition` than the one to which the client is registering. This is
1158 * done to ensure that the client receives a distinct notification for
1159 * all triggers that have a `notify` action that have this condition.
1161 pthread_mutex_lock(&client_list
->lock
);
1162 cds_list_for_each_entry(trigger_ht_element
,
1163 &client_list
->triggers_list
, client_list_trigger_node
) {
1164 if (evaluate_condition_for_client(trigger_ht_element
->trigger
, condition_list_element
->condition
,
1166 WARN("Evaluation of a condition on client subscription failed, aborting.");
1168 free(client_list_element
);
1169 pthread_mutex_unlock(&client_list
->lock
);
1173 pthread_mutex_unlock(&client_list
->lock
);
1176 * Add the client to the list of clients interested in a given trigger
1177 * if a "notification" trigger with a corresponding condition was
1180 client_list_element
->client
= client
;
1181 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1183 pthread_mutex_lock(&client_list
->lock
);
1184 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1185 pthread_mutex_unlock(&client_list
->lock
);
1191 notification_client_list_put(client_list
);
1193 lttng_condition_destroy(condition
);
1196 free(condition_list_element
);
1197 free(client_list_element
);
1198 lttng_condition_destroy(condition
);
1203 int notification_thread_client_unsubscribe(
1204 struct notification_client
*client
,
1205 struct lttng_condition
*condition
,
1206 struct notification_thread_state
*state
,
1207 enum lttng_notification_channel_status
*_status
)
1209 struct notification_client_list
*client_list
;
1210 struct lttng_condition_list_element
*condition_list_element
,
1212 struct notification_client_list_element
*client_list_element
,
1214 bool condition_found
= false;
1215 enum lttng_notification_channel_status status
=
1216 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1218 /* Remove the condition from the client's condition list. */
1219 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1220 &client
->condition_list
, node
) {
1221 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1226 cds_list_del(&condition_list_element
->node
);
1228 * The caller may be iterating on the client's conditions to
1229 * tear down a client's connection. In this case, the condition
1230 * will be destroyed at the end.
1232 if (condition
!= condition_list_element
->condition
) {
1233 lttng_condition_destroy(
1234 condition_list_element
->condition
);
1236 free(condition_list_element
);
1237 condition_found
= true;
1241 if (!condition_found
) {
1242 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1247 * Remove the client from the list of clients interested the trigger
1248 * matching the condition.
1250 client_list
= get_client_list_from_condition(state
, condition
);
1255 pthread_mutex_lock(&client_list
->lock
);
1256 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1257 &client_list
->clients_list
, node
) {
1258 if (client_list_element
->client
->id
!= client
->id
) {
1261 cds_list_del(&client_list_element
->node
);
1262 free(client_list_element
);
1265 pthread_mutex_unlock(&client_list
->lock
);
1266 notification_client_list_put(client_list
);
1269 lttng_condition_destroy(condition
);
1277 void free_notification_client_rcu(struct rcu_head
*node
)
1279 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1283 void notification_client_destroy(struct notification_client
*client
,
1284 struct notification_thread_state
*state
)
1291 * The client object is not reachable by other threads, no need to lock
1294 if (client
->socket
>= 0) {
1295 (void) lttcomm_close_unix_sock(client
->socket
);
1296 client
->socket
= -1;
1298 client
->communication
.active
= false;
1299 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1300 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1301 pthread_mutex_destroy(&client
->lock
);
1302 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1306 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1310 struct notification_client
*get_client_from_socket(int socket
,
1311 struct notification_thread_state
*state
)
1313 struct cds_lfht_iter iter
;
1314 struct cds_lfht_node
*node
;
1315 struct notification_client
*client
= NULL
;
1317 cds_lfht_lookup(state
->client_socket_ht
,
1318 hash_client_socket(socket
),
1319 match_client_socket
,
1320 (void *) (unsigned long) socket
,
1322 node
= cds_lfht_iter_get_node(&iter
);
1327 client
= caa_container_of(node
, struct notification_client
,
1328 client_socket_ht_node
);
1334 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1338 struct notification_client
*get_client_from_id(notification_client_id id
,
1339 struct notification_thread_state
*state
)
1341 struct cds_lfht_iter iter
;
1342 struct cds_lfht_node
*node
;
1343 struct notification_client
*client
= NULL
;
1345 cds_lfht_lookup(state
->client_id_ht
,
1350 node
= cds_lfht_iter_get_node(&iter
);
1355 client
= caa_container_of(node
, struct notification_client
,
1362 bool buffer_usage_condition_applies_to_channel(
1363 const struct lttng_condition
*condition
,
1364 const struct channel_info
*channel_info
)
1366 enum lttng_condition_status status
;
1367 enum lttng_domain_type condition_domain
;
1368 const char *condition_session_name
= NULL
;
1369 const char *condition_channel_name
= NULL
;
1371 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1373 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
1374 if (channel_info
->key
.domain
!= condition_domain
) {
1378 status
= lttng_condition_buffer_usage_get_session_name(
1379 condition
, &condition_session_name
);
1380 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1382 status
= lttng_condition_buffer_usage_get_channel_name(
1383 condition
, &condition_channel_name
);
1384 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1386 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1389 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1399 bool session_consumed_size_condition_applies_to_channel(
1400 const struct lttng_condition
*condition
,
1401 const struct channel_info
*channel_info
)
1403 enum lttng_condition_status status
;
1404 const char *condition_session_name
= NULL
;
1406 status
= lttng_condition_session_consumed_size_get_session_name(
1407 condition
, &condition_session_name
);
1408 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1410 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1420 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1421 const struct channel_info
*channel_info
)
1423 const struct lttng_condition
*condition
;
1424 bool trigger_applies
;
1426 condition
= lttng_trigger_get_const_condition(trigger
);
1431 switch (lttng_condition_get_type(condition
)) {
1432 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1433 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1434 trigger_applies
= buffer_usage_condition_applies_to_channel(
1435 condition
, channel_info
);
1437 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1438 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1439 condition
, channel_info
);
1445 return trigger_applies
;
1450 /* Must be called with RCU read lock held. */
1452 struct lttng_session_trigger_list
*get_session_trigger_list(
1453 struct notification_thread_state
*state
,
1454 const char *session_name
)
1456 struct lttng_session_trigger_list
*list
= NULL
;
1457 struct cds_lfht_node
*node
;
1458 struct cds_lfht_iter iter
;
1460 cds_lfht_lookup(state
->session_triggers_ht
,
1461 hash_key_str(session_name
, lttng_ht_seed
),
1462 match_session_trigger_list
,
1465 node
= cds_lfht_iter_get_node(&iter
);
1468 * Not an error, the list of triggers applying to that session
1469 * will be initialized when the session is created.
1471 DBG("No trigger list found for session \"%s\" as it is not yet known to the notification system",
1476 list
= caa_container_of(node
,
1477 struct lttng_session_trigger_list
,
1478 session_triggers_ht_node
);
1484 * Allocate an empty lttng_session_trigger_list for the session named
1487 * No ownership of 'session_name' is assumed by the session trigger list.
1488 * It is the caller's responsability to ensure the session name is alive
1489 * for as long as this list is.
1492 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1493 const char *session_name
,
1494 struct cds_lfht
*session_triggers_ht
)
1496 struct lttng_session_trigger_list
*list
;
1498 list
= zmalloc(sizeof(*list
));
1502 list
->session_name
= session_name
;
1503 CDS_INIT_LIST_HEAD(&list
->list
);
1504 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1505 list
->session_triggers_ht
= session_triggers_ht
;
1508 /* Publish the list through the session_triggers_ht. */
1509 cds_lfht_add(session_triggers_ht
,
1510 hash_key_str(session_name
, lttng_ht_seed
),
1511 &list
->session_triggers_ht_node
);
1518 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1520 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1525 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1527 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1529 /* Empty the list element by element, and then free the list itself. */
1530 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1531 &list
->list
, node
) {
1532 cds_list_del(&trigger_list_element
->node
);
1533 free(trigger_list_element
);
1536 /* Unpublish the list from the session_triggers_ht. */
1537 cds_lfht_del(list
->session_triggers_ht
,
1538 &list
->session_triggers_ht_node
);
1540 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1544 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1545 struct lttng_trigger
*trigger
)
1548 struct lttng_trigger_list_element
*new_element
=
1549 zmalloc(sizeof(*new_element
));
1555 CDS_INIT_LIST_HEAD(&new_element
->node
);
1556 new_element
->trigger
= trigger
;
1557 cds_list_add(&new_element
->node
, &list
->list
);
1563 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1564 const char *session_name
)
1566 bool applies
= false;
1567 const struct lttng_condition
*condition
;
1569 condition
= lttng_trigger_get_const_condition(trigger
);
1570 switch (lttng_condition_get_type(condition
)) {
1571 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1572 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1574 enum lttng_condition_status condition_status
;
1575 const char *condition_session_name
;
1577 condition_status
= lttng_condition_session_rotation_get_session_name(
1578 condition
, &condition_session_name
);
1579 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1580 ERR("Failed to retrieve session rotation condition's session name");
1584 LTTNG_ASSERT(condition_session_name
);
1585 applies
= !strcmp(condition_session_name
, session_name
);
1596 * Allocate and initialize an lttng_session_trigger_list which contains
1597 * all triggers that apply to the session named 'session_name'.
1599 * No ownership of 'session_name' is assumed by the session trigger list.
1600 * It is the caller's responsability to ensure the session name is alive
1601 * for as long as this list is.
1604 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1605 const struct notification_thread_state
*state
,
1606 const char *session_name
)
1608 int trigger_count
= 0;
1609 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1610 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1611 struct cds_lfht_iter iter
;
1613 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1614 state
->session_triggers_ht
);
1616 /* Add all triggers applying to the session named 'session_name'. */
1617 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1621 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1626 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1627 trigger_ht_element
->trigger
);
1635 DBG("Found %i triggers that apply to newly created session",
1637 return session_trigger_list
;
1639 lttng_session_trigger_list_destroy(session_trigger_list
);
1644 struct session_info
*find_or_create_session_info(
1645 struct notification_thread_state
*state
,
1646 const char *name
, uid_t uid
, gid_t gid
)
1648 struct session_info
*session
= NULL
;
1649 struct cds_lfht_node
*node
;
1650 struct cds_lfht_iter iter
;
1651 struct lttng_session_trigger_list
*trigger_list
;
1654 cds_lfht_lookup(state
->sessions_ht
,
1655 hash_key_str(name
, lttng_ht_seed
),
1659 node
= cds_lfht_iter_get_node(&iter
);
1661 DBG("Found session info of session \"%s\" (uid = %i, gid = %i)",
1663 session
= caa_container_of(node
, struct session_info
,
1665 LTTNG_ASSERT(session
->uid
== uid
);
1666 LTTNG_ASSERT(session
->gid
== gid
);
1667 session_info_get(session
);
1671 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1672 if (!trigger_list
) {
1676 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1677 state
->sessions_ht
);
1679 ERR("Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1681 lttng_session_trigger_list_destroy(trigger_list
);
1684 trigger_list
= NULL
;
1686 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1687 &session
->sessions_ht_node
);
1693 session_info_put(session
);
1698 int handle_notification_thread_command_add_channel(
1699 struct notification_thread_state
*state
,
1700 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1701 const char *channel_name
, enum lttng_domain_type channel_domain
,
1702 uint64_t channel_key_int
, uint64_t channel_capacity
,
1703 enum lttng_error_code
*cmd_result
)
1705 struct cds_list_head trigger_list
;
1706 struct channel_info
*new_channel_info
= NULL
;
1707 struct channel_key channel_key
= {
1708 .key
= channel_key_int
,
1709 .domain
= channel_domain
,
1711 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1712 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1713 int trigger_count
= 0;
1714 struct cds_lfht_iter iter
;
1715 struct session_info
*session_info
= NULL
;
1717 DBG("Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1718 channel_name
, session_name
, channel_key_int
,
1719 lttng_domain_type_str(channel_domain
));
1721 CDS_INIT_LIST_HEAD(&trigger_list
);
1723 session_info
= find_or_create_session_info(state
, session_name
,
1724 session_uid
, session_gid
);
1725 if (!session_info
) {
1726 /* Allocation error or an internal error occurred. */
1730 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1731 channel_capacity
, session_info
);
1732 if (!new_channel_info
) {
1737 /* Build a list of all triggers applying to the new channel. */
1738 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1740 struct lttng_trigger_list_element
*new_element
;
1742 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1743 new_channel_info
)) {
1747 new_element
= zmalloc(sizeof(*new_element
));
1752 CDS_INIT_LIST_HEAD(&new_element
->node
);
1753 new_element
->trigger
= trigger_ht_element
->trigger
;
1754 cds_list_add(&new_element
->node
, &trigger_list
);
1759 DBG("Found %i triggers that apply to newly added channel",
1761 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1762 if (!channel_trigger_list
) {
1765 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1766 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1767 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1768 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1771 /* Add channel to the channel_ht which owns the channel_infos. */
1772 cds_lfht_add(state
->channels_ht
,
1773 hash_channel_key(&new_channel_info
->key
),
1774 &new_channel_info
->channels_ht_node
);
1776 * Add the list of triggers associated with this channel to the
1777 * channel_triggers_ht.
1779 cds_lfht_add(state
->channel_triggers_ht
,
1780 hash_channel_key(&new_channel_info
->key
),
1781 &channel_trigger_list
->channel_triggers_ht_node
);
1783 session_info_put(session_info
);
1784 *cmd_result
= LTTNG_OK
;
1787 channel_info_destroy(new_channel_info
);
1788 session_info_put(session_info
);
1793 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1795 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1800 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1802 free(caa_container_of(node
, struct channel_state_sample
,
1807 int handle_notification_thread_command_remove_channel(
1808 struct notification_thread_state
*state
,
1809 uint64_t channel_key
, enum lttng_domain_type domain
,
1810 enum lttng_error_code
*cmd_result
)
1812 struct cds_lfht_node
*node
;
1813 struct cds_lfht_iter iter
;
1814 struct lttng_channel_trigger_list
*trigger_list
;
1815 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1816 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1817 struct channel_info
*channel_info
;
1819 DBG("Removing channel key = %" PRIu64
" in %s domain",
1820 channel_key
, lttng_domain_type_str(domain
));
1824 cds_lfht_lookup(state
->channel_triggers_ht
,
1825 hash_channel_key(&key
),
1826 match_channel_trigger_list
,
1829 node
= cds_lfht_iter_get_node(&iter
);
1831 * There is a severe internal error if we are being asked to remove a
1832 * channel that doesn't exist.
1835 ERR("Channel being removed is unknown to the notification thread");
1839 /* Free the list of triggers associated with this channel. */
1840 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1841 channel_triggers_ht_node
);
1842 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1843 &trigger_list
->list
, node
) {
1844 cds_list_del(&trigger_list_element
->node
);
1845 free(trigger_list_element
);
1847 cds_lfht_del(state
->channel_triggers_ht
, node
);
1848 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1850 /* Free sampled channel state. */
1851 cds_lfht_lookup(state
->channel_state_ht
,
1852 hash_channel_key(&key
),
1853 match_channel_state_sample
,
1856 node
= cds_lfht_iter_get_node(&iter
);
1858 * This is expected to be NULL if the channel is destroyed before we
1859 * received a sample.
1862 struct channel_state_sample
*sample
= caa_container_of(node
,
1863 struct channel_state_sample
,
1864 channel_state_ht_node
);
1866 cds_lfht_del(state
->channel_state_ht
, node
);
1867 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1870 /* Remove the channel from the channels_ht and free it. */
1871 cds_lfht_lookup(state
->channels_ht
,
1872 hash_channel_key(&key
),
1876 node
= cds_lfht_iter_get_node(&iter
);
1878 channel_info
= caa_container_of(node
, struct channel_info
,
1880 cds_lfht_del(state
->channels_ht
, node
);
1881 channel_info_destroy(channel_info
);
1884 *cmd_result
= LTTNG_OK
;
1889 int handle_notification_thread_command_session_rotation(
1890 struct notification_thread_state
*state
,
1891 enum notification_thread_command_type cmd_type
,
1892 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1893 uint64_t trace_archive_chunk_id
,
1894 struct lttng_trace_archive_location
*location
,
1895 enum lttng_error_code
*_cmd_result
)
1898 enum lttng_error_code cmd_result
= LTTNG_OK
;
1899 struct lttng_session_trigger_list
*trigger_list
;
1900 struct lttng_trigger_list_element
*trigger_list_element
;
1901 struct session_info
*session_info
;
1902 const struct lttng_credentials session_creds
= {
1903 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1904 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1909 session_info
= find_or_create_session_info(state
, session_name
,
1910 session_uid
, session_gid
);
1911 if (!session_info
) {
1912 /* Allocation error or an internal error occurred. */
1914 cmd_result
= LTTNG_ERR_NOMEM
;
1918 session_info
->rotation
.ongoing
=
1919 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1920 session_info
->rotation
.id
= trace_archive_chunk_id
;
1921 trigger_list
= get_session_trigger_list(state
, session_name
);
1922 if (!trigger_list
) {
1923 DBG("No triggers applying to session \"%s\" found",
1928 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1930 const struct lttng_condition
*condition
;
1931 struct lttng_trigger
*trigger
;
1932 struct notification_client_list
*client_list
;
1933 struct lttng_evaluation
*evaluation
= NULL
;
1934 enum lttng_condition_type condition_type
;
1935 enum action_executor_status executor_status
;
1937 trigger
= trigger_list_element
->trigger
;
1938 condition
= lttng_trigger_get_const_condition(trigger
);
1939 LTTNG_ASSERT(condition
);
1940 condition_type
= lttng_condition_get_type(condition
);
1942 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1943 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1945 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1946 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1950 client_list
= get_client_list_from_condition(state
, condition
);
1951 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1952 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1953 trace_archive_chunk_id
);
1955 evaluation
= lttng_evaluation_session_rotation_completed_create(
1956 trace_archive_chunk_id
, location
);
1960 /* Internal error */
1962 cmd_result
= LTTNG_ERR_UNK
;
1967 * Ownership of `evaluation` transferred to the action executor
1968 * no matter the result.
1970 executor_status
= action_executor_enqueue_trigger(
1971 state
->executor
, trigger
, evaluation
,
1972 &session_creds
, client_list
);
1974 switch (executor_status
) {
1975 case ACTION_EXECUTOR_STATUS_OK
:
1977 case ACTION_EXECUTOR_STATUS_ERROR
:
1978 case ACTION_EXECUTOR_STATUS_INVALID
:
1980 * TODO Add trigger identification (name/id) when
1981 * it is added to the API.
1983 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1986 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1988 * TODO Add trigger identification (name/id) when
1989 * it is added to the API.
1991 * Not a fatal error.
1993 WARN("No space left when enqueuing action associated with session rotation trigger");
2001 notification_client_list_put(client_list
);
2002 if (caa_unlikely(ret
)) {
2007 session_info_put(session_info
);
2008 *_cmd_result
= cmd_result
;
2014 int handle_notification_thread_command_add_tracer_event_source(
2015 struct notification_thread_state
*state
,
2016 int tracer_event_source_fd
,
2017 enum lttng_domain_type domain_type
,
2018 enum lttng_error_code
*_cmd_result
)
2021 enum lttng_error_code cmd_result
= LTTNG_OK
;
2022 struct notification_event_tracer_event_source_element
*element
= NULL
;
2024 element
= zmalloc(sizeof(*element
));
2026 cmd_result
= LTTNG_ERR_NOMEM
;
2031 element
->fd
= tracer_event_source_fd
;
2032 element
->domain
= domain_type
;
2034 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2036 DBG3("Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2037 tracer_event_source_fd
,
2038 lttng_domain_type_str(domain_type
));
2040 /* Adding the read side pipe to the event poll. */
2041 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
2043 ERR("Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2044 tracer_event_source_fd
,
2045 lttng_domain_type_str(element
->domain
));
2046 cds_list_del(&element
->node
);
2051 element
->is_fd_in_poll_set
= true;
2054 *_cmd_result
= cmd_result
;
2059 int drain_event_notifier_notification_pipe(
2060 struct notification_thread_state
*state
,
2061 int pipe
, enum lttng_domain_type domain
)
2063 struct lttng_poll_event events
= {0};
2066 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2068 ERR("Error creating lttng_poll_event");
2072 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2074 ERR("Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2081 * Continue to consume notifications as long as there are new
2082 * ones coming in. The tracer has been asked to stop producing
2085 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2086 * monitored (on Linux, at least) and will be returned when
2087 * the pipe is closed but empty.
2089 ret
= lttng_poll_wait_interruptible(&events
, 0);
2090 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2091 /* No more notification to be read on this pipe. */
2094 } else if (ret
< 0) {
2095 PERROR("Failed on lttng_poll_wait_interruptible() call");
2100 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2102 ERR("Error consuming an event notifier notification from pipe: fd = %d",
2107 lttng_poll_clean(&events
);
2112 struct notification_event_tracer_event_source_element
*
2113 find_tracer_event_source_element(struct notification_thread_state
*state
,
2114 int tracer_event_source_fd
)
2116 struct notification_event_tracer_event_source_element
*source_element
;
2118 cds_list_for_each_entry(source_element
,
2119 &state
->tracer_event_sources_list
, node
) {
2120 if (source_element
->fd
== tracer_event_source_fd
) {
2125 source_element
= NULL
;
2127 return source_element
;
2131 int remove_tracer_event_source_from_pollset(
2132 struct notification_thread_state
*state
,
2133 struct notification_event_tracer_event_source_element
*source_element
)
2137 LTTNG_ASSERT(source_element
->is_fd_in_poll_set
);
2139 DBG3("Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2141 lttng_domain_type_str(source_element
->domain
));
2143 /* Removing the fd from the event poll set. */
2144 ret
= lttng_poll_del(&state
->events
, source_element
->fd
);
2146 ERR("Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2148 lttng_domain_type_str(source_element
->domain
));
2153 source_element
->is_fd_in_poll_set
= false;
2155 ret
= drain_event_notifier_notification_pipe(state
, source_element
->fd
,
2156 source_element
->domain
);
2158 ERR("Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2160 lttng_domain_type_str(source_element
->domain
));
2169 int handle_notification_thread_tracer_event_source_died(
2170 struct notification_thread_state
*state
,
2171 int tracer_event_source_fd
)
2174 struct notification_event_tracer_event_source_element
*source_element
;
2176 source_element
= find_tracer_event_source_element(state
,
2177 tracer_event_source_fd
);
2179 LTTNG_ASSERT(source_element
);
2181 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2183 ERR("Failed to remove dead tracer event source from poll set");
2190 int handle_notification_thread_command_remove_tracer_event_source(
2191 struct notification_thread_state
*state
,
2192 int tracer_event_source_fd
,
2193 enum lttng_error_code
*_cmd_result
)
2196 enum lttng_error_code cmd_result
= LTTNG_OK
;
2197 struct notification_event_tracer_event_source_element
*source_element
= NULL
;
2199 source_element
= find_tracer_event_source_element(state
,
2200 tracer_event_source_fd
);
2202 LTTNG_ASSERT(source_element
);
2204 /* Remove the tracer source from the list. */
2205 cds_list_del(&source_element
->node
);
2207 if (!source_element
->is_fd_in_poll_set
) {
2208 /* Skip the poll set removal. */
2212 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2214 ERR("Failed to remove tracer event source from poll set");
2215 cmd_result
= LTTNG_ERR_FATAL
;
2219 free(source_element
);
2220 *_cmd_result
= cmd_result
;
2224 static int handle_notification_thread_command_list_triggers(
2225 struct notification_thread_handle
*handle
,
2226 struct notification_thread_state
*state
,
2228 struct lttng_triggers
**triggers
,
2229 enum lttng_error_code
*_cmd_result
)
2232 enum lttng_error_code cmd_result
= LTTNG_OK
;
2233 struct cds_lfht_iter iter
;
2234 struct lttng_trigger_ht_element
*trigger_ht_element
;
2235 struct lttng_triggers
*local_triggers
= NULL
;
2236 const struct lttng_credentials
*creds
;
2240 local_triggers
= lttng_triggers_create();
2241 if (!local_triggers
) {
2242 /* Not a fatal error. */
2243 cmd_result
= LTTNG_ERR_NOMEM
;
2247 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2248 trigger_ht_element
, node
) {
2250 * Only return the triggers to which the client has access.
2251 * The root user has visibility over all triggers.
2253 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2254 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2258 ret
= lttng_triggers_add(local_triggers
,
2259 trigger_ht_element
->trigger
);
2261 /* Not a fatal error. */
2263 cmd_result
= LTTNG_ERR_NOMEM
;
2268 /* Transferring ownership to the caller. */
2269 *triggers
= local_triggers
;
2270 local_triggers
= NULL
;
2274 lttng_triggers_destroy(local_triggers
);
2275 *_cmd_result
= cmd_result
;
2279 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
2280 const char **trigger_name
,
2281 uid_t
*trigger_owner_uid
)
2283 enum lttng_trigger_status trigger_status
;
2285 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
2286 switch (trigger_status
) {
2287 case LTTNG_TRIGGER_STATUS_OK
:
2289 case LTTNG_TRIGGER_STATUS_UNSET
:
2290 *trigger_name
= "(anonymous)";
2296 trigger_status
= lttng_trigger_get_owner_uid(trigger
,
2298 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2301 static int handle_notification_thread_command_get_trigger(
2302 struct notification_thread_state
*state
,
2303 const struct lttng_trigger
*trigger
,
2304 struct lttng_trigger
**registered_trigger
,
2305 enum lttng_error_code
*_cmd_result
)
2308 struct cds_lfht_iter iter
;
2309 struct lttng_trigger_ht_element
*trigger_ht_element
;
2310 enum lttng_error_code cmd_result
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2311 const char *trigger_name
;
2312 uid_t trigger_owner_uid
;
2316 cds_lfht_for_each_entry(
2317 state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2318 if (lttng_trigger_is_equal(
2319 trigger
, trigger_ht_element
->trigger
)) {
2320 /* Take one reference on the return trigger. */
2321 *registered_trigger
= trigger_ht_element
->trigger
;
2322 lttng_trigger_get(*registered_trigger
);
2324 cmd_result
= LTTNG_OK
;
2329 /* Not a fatal error if the trigger is not found. */
2330 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
2331 DBG("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d",
2332 trigger_name
, (int) trigger_owner_uid
);
2338 *_cmd_result
= cmd_result
;
2343 bool condition_is_supported(struct lttng_condition
*condition
)
2347 switch (lttng_condition_get_type(condition
)) {
2348 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2349 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2352 enum lttng_domain_type domain
;
2354 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2356 LTTNG_ASSERT(ret
== 0);
2358 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2359 is_supported
= true;
2364 * Older kernel tracers don't expose the API to monitor their
2365 * buffers. Therefore, we reject triggers that require that
2366 * mechanism to be available to be evaluated.
2368 * Assume unsupported on error.
2370 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2373 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
2375 const struct lttng_event_rule
*event_rule
;
2376 enum lttng_domain_type domain
;
2377 const enum lttng_condition_status status
=
2378 lttng_condition_event_rule_matches_get_rule(
2379 condition
, &event_rule
);
2381 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
2383 domain
= lttng_event_rule_get_domain_type(event_rule
);
2384 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2385 is_supported
= true;
2390 * Older kernel tracers can't emit notification. Therefore, we
2391 * reject triggers that require that mechanism to be available
2394 * Assume unsupported on error.
2396 is_supported
= kernel_supports_event_notifiers() == 1;
2400 is_supported
= true;
2403 return is_supported
;
2406 /* Must be called with RCU read lock held. */
2408 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2409 struct notification_thread_state
*state
)
2412 const struct lttng_condition
*condition
;
2413 const char *session_name
;
2414 struct lttng_session_trigger_list
*trigger_list
;
2416 condition
= lttng_trigger_get_const_condition(trigger
);
2417 switch (lttng_condition_get_type(condition
)) {
2418 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2419 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2421 enum lttng_condition_status status
;
2423 status
= lttng_condition_session_rotation_get_session_name(
2424 condition
, &session_name
);
2425 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2426 ERR("Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2437 trigger_list
= get_session_trigger_list(state
, session_name
);
2438 if (!trigger_list
) {
2439 DBG("Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2445 DBG("Newly registered trigger bound to session \"%s\"",
2447 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2452 /* Must be called with RCU read lock held. */
2454 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2455 struct notification_thread_state
*state
)
2458 struct cds_lfht_node
*node
;
2459 struct cds_lfht_iter iter
;
2460 struct channel_info
*channel
;
2462 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2464 struct lttng_trigger_list_element
*trigger_list_element
;
2465 struct lttng_channel_trigger_list
*trigger_list
;
2466 struct cds_lfht_iter lookup_iter
;
2468 if (!trigger_applies_to_channel(trigger
, channel
)) {
2472 cds_lfht_lookup(state
->channel_triggers_ht
,
2473 hash_channel_key(&channel
->key
),
2474 match_channel_trigger_list
,
2477 node
= cds_lfht_iter_get_node(&lookup_iter
);
2479 trigger_list
= caa_container_of(node
,
2480 struct lttng_channel_trigger_list
,
2481 channel_triggers_ht_node
);
2483 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2484 if (!trigger_list_element
) {
2488 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2489 trigger_list_element
->trigger
= trigger
;
2490 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2491 DBG("Newly registered trigger bound to channel \"%s\"",
2499 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2501 bool is_notify
= false;
2502 unsigned int i
, count
;
2503 enum lttng_action_status action_status
;
2504 const struct lttng_action
*action
=
2505 lttng_trigger_get_const_action(trigger
);
2506 enum lttng_action_type action_type
;
2508 LTTNG_ASSERT(action
);
2509 action_type
= lttng_action_get_type(action
);
2510 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2513 } else if (action_type
!= LTTNG_ACTION_TYPE_LIST
) {
2517 action_status
= lttng_action_list_get_count(action
, &count
);
2518 LTTNG_ASSERT(action_status
== LTTNG_ACTION_STATUS_OK
);
2520 for (i
= 0; i
< count
; i
++) {
2521 const struct lttng_action
*inner_action
=
2522 lttng_action_list_get_at_index(
2525 action_type
= lttng_action_get_type(inner_action
);
2526 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2536 static bool trigger_name_taken(struct notification_thread_state
*state
,
2537 const struct lttng_trigger
*trigger
)
2539 struct cds_lfht_iter iter
;
2542 * No duplicata is allowed in the triggers_by_name_uid_ht.
2543 * The match is done against the trigger name and uid.
2545 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2546 hash_trigger_by_name_uid(trigger
),
2547 match_trigger_by_name_uid
,
2550 return !!cds_lfht_iter_get_node(&iter
);
2554 enum lttng_error_code
generate_trigger_name(
2555 struct notification_thread_state
*state
,
2556 struct lttng_trigger
*trigger
, const char **name
)
2558 enum lttng_error_code ret_code
= LTTNG_OK
;
2560 enum lttng_trigger_status status
;
2563 const int ret
= lttng_trigger_generate_name(trigger
,
2564 state
->trigger_id
.name_offset
++);
2566 /* The only reason this can fail right now. */
2567 ret_code
= LTTNG_ERR_NOMEM
;
2571 status
= lttng_trigger_get_name(trigger
, name
);
2572 LTTNG_ASSERT(status
== LTTNG_TRIGGER_STATUS_OK
);
2574 taken
= trigger_name_taken(state
, trigger
);
2575 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2581 void notif_thread_state_remove_trigger_ht_elem(
2582 struct notification_thread_state
*state
,
2583 struct lttng_trigger_ht_element
*trigger_ht_element
)
2585 LTTNG_ASSERT(state
);
2586 LTTNG_ASSERT(trigger_ht_element
);
2588 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2589 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2593 enum lttng_error_code
setup_tracer_notifier(
2594 struct notification_thread_state
*state
,
2595 struct lttng_trigger
*trigger
)
2597 enum lttng_error_code ret
;
2598 enum event_notifier_error_accounting_status error_accounting_status
;
2599 struct cds_lfht_node
*node
;
2600 uint64_t error_counter_index
= 0;
2601 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2602 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2604 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2605 if (!trigger_tokens_ht_element
) {
2606 ret
= LTTNG_ERR_NOMEM
;
2610 /* Add trigger token to the trigger_tokens_ht. */
2611 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2612 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2613 trigger_tokens_ht_element
->trigger
= trigger
;
2615 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2616 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2617 match_trigger_token
,
2618 &trigger_tokens_ht_element
->token
,
2619 &trigger_tokens_ht_element
->node
);
2620 if (node
!= &trigger_tokens_ht_element
->node
) {
2621 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2622 goto error_free_ht_element
;
2625 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2626 trigger
, &error_counter_index
);
2627 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2628 if (error_accounting_status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2629 DBG("Trigger list error accounting counter full.");
2630 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2632 ERR("Error registering trigger for error accounting");
2633 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2636 goto error_remove_ht_element
;
2639 lttng_condition_event_rule_matches_set_error_counter_index(
2640 condition
, error_counter_index
);
2645 error_remove_ht_element
:
2646 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2647 error_free_ht_element
:
2648 free(trigger_tokens_ht_element
);
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 bool is_trigger_anonymous
,
2673 enum lttng_error_code
*cmd_result
)
2676 struct lttng_condition
*condition
;
2677 struct notification_client_list
*client_list
= NULL
;
2678 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2679 struct cds_lfht_node
*node
;
2680 const char* trigger_name
;
2681 bool free_trigger
= true;
2682 struct lttng_evaluation
*evaluation
= NULL
;
2683 struct lttng_credentials object_creds
;
2686 enum action_executor_status executor_status
;
2687 const uint64_t trigger_tracer_token
=
2688 state
->trigger_id
.next_tracer_token
++;
2692 /* Set the trigger's tracer token. */
2693 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2695 if (!is_trigger_anonymous
) {
2696 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2697 LTTNG_TRIGGER_STATUS_UNSET
) {
2698 const enum lttng_error_code ret_code
=
2699 generate_trigger_name(state
, trigger
,
2702 if (ret_code
!= LTTNG_OK
) {
2705 *cmd_result
= ret_code
;
2708 } else if (trigger_name_taken(state
, trigger
)) {
2709 /* Not a fatal error. */
2710 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2715 trigger_name
= "(anonymous)";
2718 condition
= lttng_trigger_get_condition(trigger
);
2719 LTTNG_ASSERT(condition
);
2721 /* Some conditions require tracers to implement a minimal ABI version. */
2722 if (!condition_is_supported(condition
)) {
2723 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2727 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2728 if (!trigger_ht_element
) {
2733 /* Add trigger to the trigger_ht. */
2734 cds_lfht_node_init(&trigger_ht_element
->node
);
2735 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2736 trigger_ht_element
->trigger
= trigger
;
2738 node
= cds_lfht_add_unique(state
->triggers_ht
,
2739 lttng_condition_hash(condition
),
2742 &trigger_ht_element
->node
);
2743 if (node
!= &trigger_ht_element
->node
) {
2744 /* Not a fatal error, simply report it to the client. */
2745 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2746 goto error_free_ht_element
;
2749 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2750 hash_trigger_by_name_uid(trigger
),
2751 match_trigger_by_name_uid
,
2753 &trigger_ht_element
->node_by_name_uid
);
2754 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2755 /* Internal error: add to triggers_ht should have failed. */
2757 goto error_free_ht_element
;
2760 /* From this point consider the trigger registered. */
2761 lttng_trigger_set_as_registered(trigger
);
2764 * Some triggers might need a tracer notifier depending on its
2765 * condition and actions.
2767 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2768 enum lttng_error_code error_code
;
2770 error_code
= setup_tracer_notifier(state
, trigger
);
2771 if (error_code
!= LTTNG_OK
) {
2772 notif_thread_state_remove_trigger_ht_elem(state
,
2773 trigger_ht_element
);
2774 if (error_code
== LTTNG_ERR_NOMEM
) {
2777 *cmd_result
= error_code
;
2781 goto error_free_ht_element
;
2786 * The rest only applies to triggers that have a "notify" action.
2787 * It is not skipped as this is the only action type currently
2790 if (is_trigger_action_notify(trigger
)) {
2792 * Find or create the client list of this condition. It may
2793 * already be present if another trigger is already registered
2794 * with the same condition.
2796 client_list
= get_client_list_from_condition(state
, condition
);
2799 * No client list for this condition yet. We create new
2800 * one and build it up.
2802 client_list
= notification_client_list_create(state
, condition
);
2804 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2805 goto error_free_ht_element
;
2809 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2811 pthread_mutex_lock(&client_list
->lock
);
2812 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2813 pthread_mutex_unlock(&client_list
->lock
);
2817 * Ownership of the trigger and of its wrapper was transfered to
2818 * the triggers_ht. Same for token ht element if necessary.
2820 trigger_ht_element
= NULL
;
2821 free_trigger
= false;
2823 switch (get_condition_binding_object(condition
)) {
2824 case LTTNG_OBJECT_TYPE_SESSION
:
2825 /* Add the trigger to the list if it matches a known session. */
2826 ret
= bind_trigger_to_matching_session(trigger
, state
);
2828 goto error_free_ht_element
;
2831 case LTTNG_OBJECT_TYPE_CHANNEL
:
2833 * Add the trigger to list of triggers bound to the channels
2836 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2838 goto error_free_ht_element
;
2841 case LTTNG_OBJECT_TYPE_NONE
:
2844 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2846 goto error_free_ht_element
;
2850 * The new trigger's condition must be evaluated against the current
2853 * In the case of `notify` action, nothing preventing clients from
2854 * subscribing to a condition before the corresponding trigger is
2855 * registered, we have to evaluate this new condition right away.
2857 * At some point, we were waiting for the next "evaluation" (e.g. on
2858 * reception of a channel sample) to evaluate this new condition, but
2861 * The reason it was broken is that waiting for the next sample
2862 * does not allow us to properly handle transitions for edge-triggered
2865 * Consider this example: when we handle a new channel sample, we
2866 * evaluate each conditions twice: once with the previous state, and
2867 * again with the newest state. We then use those two results to
2868 * determine whether a state change happened: a condition was false and
2869 * became true. If a state change happened, we have to notify clients.
2871 * Now, if a client subscribes to a given notification and registers
2872 * a trigger *after* that subscription, we have to make sure the
2873 * condition is evaluated at this point while considering only the
2874 * current state. Otherwise, the next evaluation cycle may only see
2875 * that the evaluations remain the same (true for samples n-1 and n) and
2876 * the client will never know that the condition has been met.
2878 switch (get_condition_binding_object(condition
)) {
2879 case LTTNG_OBJECT_TYPE_SESSION
:
2880 ret
= evaluate_session_condition_for_client(condition
, state
,
2881 &evaluation
, &object_uid
,
2883 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2884 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2886 case LTTNG_OBJECT_TYPE_CHANNEL
:
2887 ret
= evaluate_channel_condition_for_client(condition
, state
,
2888 &evaluation
, &object_uid
,
2890 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2891 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2893 case LTTNG_OBJECT_TYPE_NONE
:
2896 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2904 goto error_free_ht_element
;
2907 DBG("Newly registered trigger's condition evaluated to %s",
2908 evaluation
? "true" : "false");
2910 /* Evaluation yielded nothing. Normal exit. */
2916 * Ownership of `evaluation` transferred to the action executor
2917 * no matter the result.
2919 executor_status
= action_executor_enqueue_trigger(state
->executor
,
2920 trigger
, evaluation
, &object_creds
, client_list
);
2922 switch (executor_status
) {
2923 case ACTION_EXECUTOR_STATUS_OK
:
2925 case ACTION_EXECUTOR_STATUS_ERROR
:
2926 case ACTION_EXECUTOR_STATUS_INVALID
:
2928 * TODO Add trigger identification (name/id) when
2929 * it is added to the API.
2931 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2933 goto error_free_ht_element
;
2934 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2936 * TODO Add trigger identification (name/id) when
2937 * it is added to the API.
2939 * Not a fatal error.
2941 WARN("No space left when enqueuing action associated to newly registered trigger");
2949 *cmd_result
= LTTNG_OK
;
2950 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2951 trigger_name
, trigger_tracer_token
);
2954 error_free_ht_element
:
2955 if (trigger_ht_element
) {
2956 /* Delayed removal due to RCU constraint on delete. */
2957 call_rcu(&trigger_ht_element
->rcu_node
,
2958 free_lttng_trigger_ht_element_rcu
);
2963 * Other objects might have a reference to the trigger, mark it
2966 lttng_trigger_set_as_unregistered(trigger
);
2967 lttng_trigger_destroy(trigger
);
2975 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2977 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2982 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2984 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2989 void teardown_tracer_notifier(struct notification_thread_state
*state
,
2990 const struct lttng_trigger
*trigger
)
2992 struct cds_lfht_iter iter
;
2993 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2995 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
2996 trigger_tokens_ht_element
, node
) {
2998 if (!lttng_trigger_is_equal(trigger
,
2999 trigger_tokens_ht_element
->trigger
)) {
3003 event_notifier_error_accounting_unregister_event_notifier(
3004 trigger_tokens_ht_element
->trigger
);
3006 /* TODO talk to all app and remove it */
3007 DBG("Removed trigger from tokens_ht");
3008 cds_lfht_del(state
->trigger_tokens_ht
,
3009 &trigger_tokens_ht_element
->node
);
3011 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
3012 free_notification_trigger_tokens_ht_element_rcu
);
3019 int handle_notification_thread_command_unregister_trigger(
3020 struct notification_thread_state
*state
,
3021 const struct lttng_trigger
*trigger
,
3022 enum lttng_error_code
*_cmd_reply
)
3024 struct cds_lfht_iter iter
;
3025 struct cds_lfht_node
*triggers_ht_node
;
3026 struct lttng_channel_trigger_list
*trigger_list
;
3027 struct notification_client_list
*client_list
;
3028 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
3029 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
3031 enum lttng_error_code cmd_reply
;
3035 cds_lfht_lookup(state
->triggers_ht
,
3036 lttng_condition_hash(condition
),
3040 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3041 if (!triggers_ht_node
) {
3042 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3045 cmd_reply
= LTTNG_OK
;
3048 trigger_ht_element
= caa_container_of(triggers_ht_node
,
3049 struct lttng_trigger_ht_element
, node
);
3051 /* Remove trigger from channel_triggers_ht. */
3052 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
3053 channel_triggers_ht_node
) {
3054 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3056 cds_list_for_each_entry_safe(trigger_element
, tmp
,
3057 &trigger_list
->list
, node
) {
3058 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3062 DBG("Removed trigger from channel_triggers_ht");
3063 cds_list_del(&trigger_element
->node
);
3064 /* A trigger can only appear once per channel */
3069 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3070 teardown_tracer_notifier(state
, trigger
);
3073 if (is_trigger_action_notify(trigger
)) {
3075 * Remove and release the client list from
3076 * notification_trigger_clients_ht.
3078 client_list
= get_client_list_from_condition(state
, condition
);
3079 LTTNG_ASSERT(client_list
);
3081 pthread_mutex_lock(&client_list
->lock
);
3082 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3083 pthread_mutex_unlock(&client_list
->lock
);
3085 /* Put new reference and the hashtable's reference. */
3086 notification_client_list_put(client_list
);
3087 notification_client_list_put(client_list
);
3091 /* Remove trigger from triggers_ht. */
3092 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3094 /* Release the ownership of the trigger. */
3095 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3096 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3100 *_cmd_reply
= cmd_reply
;
3105 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3106 int handle_notification_thread_command(
3107 struct notification_thread_handle
*handle
,
3108 struct notification_thread_state
*state
)
3112 struct notification_thread_command
*cmd
;
3114 /* Read the event pipe to put it back into a quiescent state. */
3115 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
3117 if (ret
!= sizeof(counter
)) {
3121 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3122 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3123 struct notification_thread_command
, cmd_list_node
);
3124 cds_list_del(&cmd
->cmd_list_node
);
3125 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3127 DBG("Received `%s` command",
3128 notification_command_type_str(cmd
->type
));
3129 switch (cmd
->type
) {
3130 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3131 ret
= handle_notification_thread_command_register_trigger(state
,
3132 cmd
->parameters
.register_trigger
.trigger
,
3133 cmd
->parameters
.register_trigger
.is_trigger_anonymous
,
3136 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3137 ret
= handle_notification_thread_command_unregister_trigger(
3139 cmd
->parameters
.unregister_trigger
.trigger
,
3142 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3143 ret
= handle_notification_thread_command_add_channel(
3145 cmd
->parameters
.add_channel
.session
.name
,
3146 cmd
->parameters
.add_channel
.session
.uid
,
3147 cmd
->parameters
.add_channel
.session
.gid
,
3148 cmd
->parameters
.add_channel
.channel
.name
,
3149 cmd
->parameters
.add_channel
.channel
.domain
,
3150 cmd
->parameters
.add_channel
.channel
.key
,
3151 cmd
->parameters
.add_channel
.channel
.capacity
,
3154 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3155 ret
= handle_notification_thread_command_remove_channel(
3156 state
, cmd
->parameters
.remove_channel
.key
,
3157 cmd
->parameters
.remove_channel
.domain
,
3160 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3161 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3162 ret
= handle_notification_thread_command_session_rotation(
3165 cmd
->parameters
.session_rotation
.session_name
,
3166 cmd
->parameters
.session_rotation
.uid
,
3167 cmd
->parameters
.session_rotation
.gid
,
3168 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3169 cmd
->parameters
.session_rotation
.location
,
3172 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3173 ret
= handle_notification_thread_command_add_tracer_event_source(
3175 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3176 cmd
->parameters
.tracer_event_source
.domain
,
3179 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3180 ret
= handle_notification_thread_command_remove_tracer_event_source(
3182 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3185 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3187 struct lttng_triggers
*triggers
= NULL
;
3189 ret
= handle_notification_thread_command_list_triggers(
3192 cmd
->parameters
.list_triggers
.uid
,
3195 cmd
->reply
.list_triggers
.triggers
= triggers
;
3199 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3200 cmd
->reply_code
= LTTNG_OK
;
3203 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3205 struct lttng_trigger
*trigger
= NULL
;
3207 ret
= handle_notification_thread_command_get_trigger(state
,
3208 cmd
->parameters
.get_trigger
.trigger
, &trigger
,
3210 cmd
->reply
.get_trigger
.trigger
= trigger
;
3213 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3215 const enum client_transmission_status client_status
=
3216 cmd
->parameters
.client_communication_update
3218 const notification_client_id client_id
=
3219 cmd
->parameters
.client_communication_update
.id
;
3220 struct notification_client
*client
;
3223 client
= get_client_from_id(client_id
, state
);
3227 * Client error was probably already picked-up by the
3228 * notification thread or it has disconnected
3229 * gracefully while this command was queued.
3231 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3235 ret
= client_handle_transmission_status(
3236 client
, client_status
, state
);
3242 ERR("Unknown internal command received");
3250 if (cmd
->is_async
) {
3254 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3258 /* Wake-up and return a fatal error to the calling thread. */
3259 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3260 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3262 /* Indicate a fatal error to the caller. */
3267 int socket_set_non_blocking(int socket
)
3271 /* Set the pipe as non-blocking. */
3272 ret
= fcntl(socket
, F_GETFL
, 0);
3274 PERROR("fcntl get socket flags");
3279 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3281 PERROR("fcntl set O_NONBLOCK socket flag");
3284 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3290 int client_reset_inbound_state(struct notification_client
*client
)
3295 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3297 client
->communication
.inbound
.bytes_to_receive
=
3298 sizeof(struct lttng_notification_channel_message
);
3299 client
->communication
.inbound
.msg_type
=
3300 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3301 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3302 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3303 ret
= lttng_dynamic_buffer_set_size(
3304 &client
->communication
.inbound
.payload
.buffer
,
3305 client
->communication
.inbound
.bytes_to_receive
);
3310 int handle_notification_thread_client_connect(
3311 struct notification_thread_state
*state
)
3314 struct notification_client
*client
;
3316 DBG("Handling new notification channel client connection");
3318 client
= zmalloc(sizeof(*client
));
3325 pthread_mutex_init(&client
->lock
, NULL
);
3326 client
->id
= state
->next_notification_client_id
++;
3327 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3328 lttng_payload_init(&client
->communication
.inbound
.payload
);
3329 lttng_payload_init(&client
->communication
.outbound
.payload
);
3330 client
->communication
.inbound
.expect_creds
= true;
3332 ret
= client_reset_inbound_state(client
);
3334 ERR("Failed to reset client communication's inbound state");
3339 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3341 ERR("Failed to accept new notification channel client connection");
3346 client
->socket
= ret
;
3348 ret
= socket_set_non_blocking(client
->socket
);
3350 ERR("Failed to set new notification channel client connection socket as non-blocking");
3354 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3356 ERR("Failed to set socket options on new notification channel client socket");
3361 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3362 LPOLLIN
| LPOLLERR
|
3363 LPOLLHUP
| LPOLLRDHUP
);
3365 ERR("Failed to add notification channel client socket to poll set");
3369 DBG("Added new notification channel client socket (%i) to poll set",
3373 cds_lfht_add(state
->client_socket_ht
,
3374 hash_client_socket(client
->socket
),
3375 &client
->client_socket_ht_node
);
3376 cds_lfht_add(state
->client_id_ht
,
3377 hash_client_id(client
->id
),
3378 &client
->client_id_ht_node
);
3384 notification_client_destroy(client
, state
);
3389 * RCU read-lock must be held by the caller.
3390 * Client lock must _not_ be held by the caller.
3393 int notification_thread_client_disconnect(
3394 struct notification_client
*client
,
3395 struct notification_thread_state
*state
)
3398 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3400 /* Acquire the client lock to disable its communication atomically. */
3401 pthread_mutex_lock(&client
->lock
);
3402 client
->communication
.active
= false;
3403 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3404 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3405 pthread_mutex_unlock(&client
->lock
);
3407 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3409 ERR("Failed to remove client socket %d from poll set",
3413 /* Release all conditions to which the client was subscribed. */
3414 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3415 &client
->condition_list
, node
) {
3416 (void) notification_thread_client_unsubscribe(client
,
3417 condition_list_element
->condition
, state
, NULL
);
3421 * Client no longer accessible to other threads (through the
3424 notification_client_destroy(client
, state
);
3428 int handle_notification_thread_client_disconnect(
3429 int client_socket
, struct notification_thread_state
*state
)
3432 struct notification_client
*client
;
3435 DBG("Closing client connection (socket fd = %i)",
3437 client
= get_client_from_socket(client_socket
, state
);
3439 /* Internal state corruption, fatal error. */
3440 ERR("Unable to find client (socket fd = %i)",
3446 ret
= notification_thread_client_disconnect(client
, state
);
3452 int handle_notification_thread_client_disconnect_all(
3453 struct notification_thread_state
*state
)
3455 struct cds_lfht_iter iter
;
3456 struct notification_client
*client
;
3457 bool error_encoutered
= false;
3460 DBG("Closing all client connections");
3461 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3462 client_socket_ht_node
) {
3465 ret
= notification_thread_client_disconnect(
3468 error_encoutered
= true;
3472 return error_encoutered
? 1 : 0;
3475 int handle_notification_thread_trigger_unregister_all(
3476 struct notification_thread_state
*state
)
3478 bool error_occurred
= false;
3479 struct cds_lfht_iter iter
;
3480 struct lttng_trigger_ht_element
*trigger_ht_element
;
3483 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3485 int ret
= handle_notification_thread_command_unregister_trigger(
3486 state
, trigger_ht_element
->trigger
, NULL
);
3488 error_occurred
= true;
3492 return error_occurred
? -1 : 0;
3496 int client_handle_transmission_status(
3497 struct notification_client
*client
,
3498 enum client_transmission_status transmission_status
,
3499 struct notification_thread_state
*state
)
3503 switch (transmission_status
) {
3504 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3505 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3506 CLIENT_POLL_MASK_IN
);
3512 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3514 * We want to be notified whenever there is buffer space
3515 * available to send the rest of the payload.
3517 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3518 CLIENT_POLL_MASK_IN_OUT
);
3523 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3524 ret
= notification_thread_client_disconnect(client
, state
);
3529 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3539 /* Client lock must be acquired by caller. */
3541 enum client_transmission_status
client_flush_outgoing_queue(
3542 struct notification_client
*client
)
3545 size_t to_send_count
;
3546 enum client_transmission_status status
;
3547 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3548 &client
->communication
.outbound
.payload
, 0, -1);
3549 const int fds_to_send_count
=
3550 lttng_payload_view_get_fd_handle_count(&pv
);
3552 ASSERT_LOCKED(client
->lock
);
3554 if (!client
->communication
.active
) {
3555 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3559 if (pv
.buffer
.size
== 0) {
3561 * If both data and fds are equal to zero, we are in an invalid
3564 LTTNG_ASSERT(fds_to_send_count
!= 0);
3569 to_send_count
= pv
.buffer
.size
;
3570 DBG("Flushing client (socket fd = %i) outgoing queue",
3573 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3576 if ((ret
>= 0 && ret
< to_send_count
)) {
3577 DBG("Client (socket fd = %i) outgoing queue could not be completely flushed",
3579 to_send_count
-= max(ret
, 0);
3581 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3583 pv
.buffer
.size
- to_send_count
,
3585 ret
= lttng_dynamic_buffer_set_size(
3586 &client
->communication
.outbound
.payload
.buffer
,
3592 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3594 } else if (ret
< 0) {
3595 /* Generic error, disable the client's communication. */
3596 ERR("Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3598 client
->communication
.active
= false;
3599 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3603 * No error and flushed the queue completely.
3605 * The payload buffer size is used later to
3606 * check if there is notifications queued. So albeit that the
3607 * direct caller knows that the transmission is complete, we
3608 * need to set the buffer size to zero.
3610 ret
= lttng_dynamic_buffer_set_size(
3611 &client
->communication
.outbound
.payload
.buffer
, 0);
3618 /* No fds to send, transmission is complete. */
3619 if (fds_to_send_count
== 0) {
3620 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3624 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3625 client
->socket
, &pv
);
3627 /* Generic error, disable the client's communication. */
3628 ERR("Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3630 client
->communication
.active
= false;
3631 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3633 } else if (ret
== 0) {
3634 /* Nothing could be sent. */
3635 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3637 /* Fd passing is an all or nothing kind of thing. */
3638 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3640 * The payload _fd_array count is used later to
3641 * check if there is notifications queued. So although the
3642 * direct caller knows that the transmission is complete, we
3643 * need to clear the _fd_array for the queuing check.
3645 lttng_dynamic_pointer_array_clear(
3646 &client
->communication
.outbound
.payload
3651 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3652 client
->communication
.outbound
.queued_command_reply
= false;
3653 client
->communication
.outbound
.dropped_notification
= false;
3654 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3659 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3663 bool client_has_outbound_data_left(
3664 const struct notification_client
*client
)
3666 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3667 &client
->communication
.outbound
.payload
, 0, -1);
3668 const bool has_data
= pv
.buffer
.size
!= 0;
3669 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3671 return has_data
|| has_fds
;
3674 /* Client lock must _not_ be held by the caller. */
3676 int client_send_command_reply(struct notification_client
*client
,
3677 struct notification_thread_state
*state
,
3678 enum lttng_notification_channel_status status
)
3681 struct lttng_notification_channel_command_reply reply
= {
3682 .status
= (int8_t) status
,
3684 struct lttng_notification_channel_message msg
= {
3685 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3686 .size
= sizeof(reply
),
3688 char buffer
[sizeof(msg
) + sizeof(reply
)];
3689 enum client_transmission_status transmission_status
;
3691 memcpy(buffer
, &msg
, sizeof(msg
));
3692 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3693 DBG("Send command reply (%i)", (int) status
);
3695 pthread_mutex_lock(&client
->lock
);
3696 if (client
->communication
.outbound
.queued_command_reply
) {
3697 /* Protocol error. */
3701 /* Enqueue buffer to outgoing queue and flush it. */
3702 ret
= lttng_dynamic_buffer_append(
3703 &client
->communication
.outbound
.payload
.buffer
,
3704 buffer
, sizeof(buffer
));
3709 transmission_status
= client_flush_outgoing_queue(client
);
3711 if (client_has_outbound_data_left(client
)) {
3712 /* Queue could not be emptied. */
3713 client
->communication
.outbound
.queued_command_reply
= true;
3716 pthread_mutex_unlock(&client
->lock
);
3717 ret
= client_handle_transmission_status(
3718 client
, transmission_status
, state
);
3725 pthread_mutex_unlock(&client
->lock
);
3731 int client_handle_message_unknown(struct notification_client
*client
,
3732 struct notification_thread_state
*state
)
3736 * Receiving message header. The function will be called again
3737 * once the rest of the message as been received and can be
3740 const struct lttng_notification_channel_message
*msg
;
3742 LTTNG_ASSERT(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3743 msg
= (const struct lttng_notification_channel_message
*)
3744 client
->communication
.inbound
.payload
.buffer
.data
;
3746 if (msg
->size
== 0 ||
3747 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3748 ERR("Invalid notification channel message: length = %u",
3754 switch (msg
->type
) {
3755 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3756 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3757 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3761 ERR("Invalid notification channel message: unexpected message type");
3765 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3766 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3767 client
->communication
.inbound
.msg_type
=
3768 (enum lttng_notification_channel_message_type
) msg
->type
;
3769 ret
= lttng_dynamic_buffer_set_size(
3770 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3772 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3779 int client_handle_message_handshake(struct notification_client
*client
,
3780 struct notification_thread_state
*state
)
3783 struct lttng_notification_channel_command_handshake
*handshake_client
;
3784 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3785 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3786 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3788 const struct lttng_notification_channel_message msg_header
= {
3789 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3790 .size
= sizeof(handshake_reply
),
3792 enum lttng_notification_channel_status status
=
3793 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3794 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3796 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3797 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3798 sizeof(handshake_reply
));
3801 (struct lttng_notification_channel_command_handshake
*)
3802 client
->communication
.inbound
.payload
.buffer
3804 client
->major
= handshake_client
->major
;
3805 client
->minor
= handshake_client
->minor
;
3806 if (!client
->communication
.inbound
.creds_received
) {
3807 ERR("No credentials received from client");
3812 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3813 &client
->communication
.inbound
.creds
);
3814 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3815 &client
->communication
.inbound
.creds
);
3816 client
->is_sessiond
= LTTNG_SOCK_GET_PID_CRED(&client
->communication
.inbound
.creds
) == getpid();
3817 DBG("Received handshake from client: uid = %u, gid = %u, protocol version = %i.%i, client is sessiond = %s",
3818 client
->uid
, client
->gid
, (int) client
->major
,
3819 (int) client
->minor
,
3820 client
->is_sessiond
? "true" : "false");
3822 if (handshake_client
->major
!=
3823 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3824 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3827 pthread_mutex_lock(&client
->lock
);
3828 /* Outgoing queue will be flushed when the command reply is sent. */
3829 ret
= lttng_dynamic_buffer_append(
3830 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3831 sizeof(send_buffer
));
3833 ERR("Failed to send protocol version to notification channel client");
3837 client
->validated
= true;
3838 client
->communication
.active
= true;
3839 pthread_mutex_unlock(&client
->lock
);
3841 /* Set reception state to receive the next message header. */
3842 ret
= client_reset_inbound_state(client
);
3844 ERR("Failed to reset client communication's inbound state");
3848 /* Flushes the outgoing queue. */
3849 ret
= client_send_command_reply(client
, state
, status
);
3851 ERR("Failed to send reply to notification channel client");
3857 pthread_mutex_unlock(&client
->lock
);
3863 int client_handle_message_subscription(
3864 struct notification_client
*client
,
3865 enum lttng_notification_channel_message_type msg_type
,
3866 struct notification_thread_state
*state
)
3869 struct lttng_condition
*condition
;
3870 enum lttng_notification_channel_status status
=
3871 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3872 struct lttng_payload_view condition_view
=
3873 lttng_payload_view_from_payload(
3874 &client
->communication
.inbound
.payload
,
3876 size_t expected_condition_size
;
3879 * No need to lock client to sample the inbound state as the only
3880 * other thread accessing clients (action executor) only uses the
3883 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3884 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3885 if (ret
!= expected_condition_size
) {
3886 ERR("Malformed condition received from client");
3890 /* Ownership of condition is always transferred. */
3891 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3892 ret
= notification_thread_client_subscribe(
3893 client
, condition
, state
, &status
);
3895 ret
= notification_thread_client_unsubscribe(
3896 client
, condition
, state
, &status
);
3903 /* Set reception state to receive the next message header. */
3904 ret
= client_reset_inbound_state(client
);
3906 ERR("Failed to reset client communication's inbound state");
3910 ret
= client_send_command_reply(client
, state
, status
);
3912 ERR("Failed to send reply to notification channel client");
3921 int client_dispatch_message(struct notification_client
*client
,
3922 struct notification_thread_state
*state
)
3926 if (client
->communication
.inbound
.msg_type
!=
3927 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3928 client
->communication
.inbound
.msg_type
!=
3929 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3930 !client
->validated
) {
3931 WARN("client attempted a command before handshake");
3936 switch (client
->communication
.inbound
.msg_type
) {
3937 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3939 ret
= client_handle_message_unknown(client
, state
);
3942 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3944 ret
= client_handle_message_handshake(client
, state
);
3947 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3948 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3950 ret
= client_handle_message_subscription(client
,
3951 client
->communication
.inbound
.msg_type
, state
);
3961 /* Incoming data from client. */
3962 int handle_notification_thread_client_in(
3963 struct notification_thread_state
*state
, int socket
)
3966 struct notification_client
*client
;
3971 client
= get_client_from_socket(socket
, state
);
3973 /* Internal error, abort. */
3978 if (client
->communication
.inbound
.bytes_to_receive
== 0 &&
3979 client
->communication
.inbound
.fds_to_receive
!= 0) {
3980 /* Only FDs left to receive. */
3984 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3985 client
->communication
.inbound
.bytes_to_receive
;
3986 if (client
->communication
.inbound
.expect_creds
) {
3987 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3988 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3989 client
->communication
.inbound
.bytes_to_receive
,
3990 &client
->communication
.inbound
.creds
);
3992 client
->communication
.inbound
.expect_creds
= false;
3993 client
->communication
.inbound
.creds_received
= true;
3996 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3997 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3998 client
->communication
.inbound
.bytes_to_receive
);
4000 if (recv_ret
>= 0) {
4001 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
4003 goto error_disconnect_client
;
4006 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
4007 /* Message incomplete wait for more data. */
4013 LTTNG_ASSERT(client
->communication
.inbound
.bytes_to_receive
== 0);
4016 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
4017 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
4019 client
->communication
.inbound
.fds_to_receive
,
4020 &client
->communication
.inbound
.payload
);
4023 * Fds received. non blocking fds passing is all
4026 ssize_t expected_size
;
4028 expected_size
= sizeof(int) *
4029 client
->communication
.inbound
4031 LTTNG_ASSERT(ret
== expected_size
);
4032 client
->communication
.inbound
.fds_to_receive
= 0;
4033 } else if (ret
== 0) {
4034 /* Received nothing. */
4038 goto error_disconnect_client
;
4042 /* At this point the message is complete.*/
4043 LTTNG_ASSERT(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4044 client
->communication
.inbound
.fds_to_receive
== 0);
4045 ret
= client_dispatch_message(client
, state
);
4048 * Only returns an error if this client must be
4051 goto error_disconnect_client
;
4058 error_disconnect_client
:
4059 ret
= notification_thread_client_disconnect(client
, state
);
4063 /* Client ready to receive outgoing data. */
4064 int handle_notification_thread_client_out(
4065 struct notification_thread_state
*state
, int socket
)
4068 struct notification_client
*client
;
4069 enum client_transmission_status transmission_status
;
4072 client
= get_client_from_socket(socket
, state
);
4074 /* Internal error, abort. */
4079 pthread_mutex_lock(&client
->lock
);
4080 transmission_status
= client_flush_outgoing_queue(client
);
4081 pthread_mutex_unlock(&client
->lock
);
4083 ret
= client_handle_transmission_status(
4084 client
, transmission_status
, state
);
4094 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4095 const struct channel_state_sample
*sample
,
4096 uint64_t buffer_capacity
)
4098 bool result
= false;
4100 enum lttng_condition_type condition_type
;
4101 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4102 condition
, struct lttng_condition_buffer_usage
,
4105 if (use_condition
->threshold_bytes
.set
) {
4106 threshold
= use_condition
->threshold_bytes
.value
;
4109 * Threshold was expressed as a ratio.
4111 * TODO the threshold (in bytes) of conditions expressed
4112 * as a ratio of total buffer size could be cached to
4113 * forego this double-multiplication or it could be performed
4114 * as fixed-point math.
4116 * Note that caching should accommodates the case where the
4117 * condition applies to multiple channels (i.e. don't assume
4118 * that all channels matching my_chann* have the same size...)
4120 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4121 (double) buffer_capacity
);
4124 condition_type
= lttng_condition_get_type(condition
);
4125 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4126 DBG("Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4127 threshold
, sample
->highest_usage
);
4130 * The low condition should only be triggered once _all_ of the
4131 * streams in a channel have gone below the "low" threshold.
4133 if (sample
->highest_usage
<= threshold
) {
4137 DBG("High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4138 threshold
, sample
->highest_usage
);
4141 * For high buffer usage scenarios, we want to trigger whenever
4142 * _any_ of the streams has reached the "high" threshold.
4144 if (sample
->highest_usage
>= threshold
) {
4153 bool evaluate_session_consumed_size_condition(
4154 const struct lttng_condition
*condition
,
4155 uint64_t session_consumed_size
)
4158 const struct lttng_condition_session_consumed_size
*size_condition
=
4159 container_of(condition
,
4160 struct lttng_condition_session_consumed_size
,
4163 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4164 DBG("Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4165 threshold
, session_consumed_size
);
4166 return session_consumed_size
>= threshold
;
4170 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4171 struct lttng_evaluation
**evaluation
,
4172 const struct notification_thread_state
*state
,
4173 const struct channel_state_sample
*previous_sample
,
4174 const struct channel_state_sample
*latest_sample
,
4175 uint64_t previous_session_consumed_total
,
4176 uint64_t latest_session_consumed_total
,
4177 struct channel_info
*channel_info
)
4180 enum lttng_condition_type condition_type
;
4181 const bool previous_sample_available
= !!previous_sample
;
4182 bool previous_sample_result
= false;
4183 bool latest_sample_result
;
4185 condition_type
= lttng_condition_get_type(condition
);
4187 switch (condition_type
) {
4188 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4189 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4190 if (caa_likely(previous_sample_available
)) {
4191 previous_sample_result
=
4192 evaluate_buffer_usage_condition(condition
,
4193 previous_sample
, channel_info
->capacity
);
4195 latest_sample_result
= evaluate_buffer_usage_condition(
4196 condition
, latest_sample
,
4197 channel_info
->capacity
);
4199 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4200 if (caa_likely(previous_sample_available
)) {
4201 previous_sample_result
=
4202 evaluate_session_consumed_size_condition(
4204 previous_session_consumed_total
);
4206 latest_sample_result
=
4207 evaluate_session_consumed_size_condition(
4209 latest_session_consumed_total
);
4212 /* Unknown condition type; internal error. */
4216 if (!latest_sample_result
||
4217 (previous_sample_result
== latest_sample_result
)) {
4219 * Only trigger on a condition evaluation transition.
4221 * NOTE: This edge-triggered logic may not be appropriate for
4222 * future condition types.
4227 if (!evaluation
|| !latest_sample_result
) {
4231 switch (condition_type
) {
4232 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4233 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4234 *evaluation
= lttng_evaluation_buffer_usage_create(
4236 latest_sample
->highest_usage
,
4237 channel_info
->capacity
);
4239 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4240 *evaluation
= lttng_evaluation_session_consumed_size_create(
4241 latest_session_consumed_total
);
4256 int client_notification_overflow(struct notification_client
*client
)
4259 const struct lttng_notification_channel_message msg
= {
4260 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4263 ASSERT_LOCKED(client
->lock
);
4265 DBG("Dropping notification addressed to client (socket fd = %i)",
4267 if (client
->communication
.outbound
.dropped_notification
) {
4269 * The client already has a "notification dropped" message
4270 * in its outgoing queue. Nothing to do since all
4271 * of those messages are coalesced.
4276 client
->communication
.outbound
.dropped_notification
= true;
4277 ret
= lttng_dynamic_buffer_append(
4278 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4281 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4288 static int client_handle_transmission_status_wrapper(
4289 struct notification_client
*client
,
4290 enum client_transmission_status status
,
4293 return client_handle_transmission_status(client
, status
,
4294 (struct notification_thread_state
*) user_data
);
4298 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4299 const struct lttng_evaluation
*evaluation
,
4300 struct notification_client_list
* client_list
,
4301 struct notification_thread_state
*state
,
4302 uid_t object_uid
, gid_t object_gid
)
4304 const struct lttng_credentials creds
= {
4305 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4306 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4309 return notification_client_list_send_evaluation(client_list
,
4310 trigger
, evaluation
,
4312 client_handle_transmission_status_wrapper
, state
);
4316 * Permission checks relative to notification channel clients are performed
4317 * here. Notice how object, client, and trigger credentials are involved in
4320 * The `object` credentials are the credentials associated with the "subject"
4321 * of a condition. For instance, a `rotation completed` condition applies
4322 * to a session. When that condition is met, it will produce an evaluation
4323 * against a session. Hence, in this case, the `object` credentials are the
4324 * credentials of the "subject" session.
4326 * The `trigger` credentials are the credentials of the user that registered the
4329 * The `client` credentials are the credentials of the user that created a given
4330 * notification channel.
4332 * In terms of visibility, it is expected that non-privilieged users can only
4333 * register triggers against "their" objects (their own sessions and
4334 * applications they are allowed to interact with). They can then open a
4335 * notification channel and subscribe to notifications associated with those
4338 * As for privilieged users, they can register triggers against the objects of
4339 * other users. They can then subscribe to the notifications associated to their
4340 * triggers. Privilieged users _can't_ subscribe to the notifications of
4341 * triggers owned by other users; they must create their own triggers.
4343 * This is more a concern of usability than security. It would be difficult for
4344 * a root user reliably subscribe to a specific set of conditions without
4345 * interference from external users (those could, for instance, unregister
4349 int notification_client_list_send_evaluation(
4350 struct notification_client_list
*client_list
,
4351 const struct lttng_trigger
*trigger
,
4352 const struct lttng_evaluation
*evaluation
,
4353 const struct lttng_credentials
*source_object_creds
,
4354 report_client_transmission_result_cb client_report
,
4358 struct lttng_payload msg_payload
;
4359 struct notification_client_list_element
*client_list_element
, *tmp
;
4360 const struct lttng_notification notification
= {
4361 .trigger
= (struct lttng_trigger
*) trigger
,
4362 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4364 struct lttng_notification_channel_message msg_header
= {
4365 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4367 const struct lttng_credentials
*trigger_creds
=
4368 lttng_trigger_get_credentials(trigger
);
4370 lttng_payload_init(&msg_payload
);
4372 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4373 sizeof(msg_header
));
4378 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4380 ERR("Failed to serialize notification");
4385 /* Update payload size. */
4386 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4387 ->size
= (uint32_t)(
4388 msg_payload
.buffer
.size
- sizeof(msg_header
));
4390 /* Update the payload number of fds. */
4392 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4393 &msg_payload
, 0, -1);
4395 ((struct lttng_notification_channel_message
*)
4396 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4397 lttng_payload_view_get_fd_handle_count(&pv
);
4400 pthread_mutex_lock(&client_list
->lock
);
4401 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4402 &client_list
->clients_list
, node
) {
4403 enum client_transmission_status transmission_status
;
4404 struct notification_client
*client
=
4405 client_list_element
->client
;
4408 pthread_mutex_lock(&client
->lock
);
4409 if (!client
->communication
.active
) {
4411 * Skip inactive client (protocol error or
4414 DBG("Skipping client at it is marked as inactive");
4418 if (lttng_trigger_is_hidden(trigger
) && !client
->is_sessiond
) {
4420 * Notifications resulting from an hidden trigger are
4421 * only sent to the session daemon.
4423 DBG("Skipping client as the trigger is hidden and the client is not the session daemon");
4427 if (source_object_creds
) {
4428 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4429 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4432 * Client is not allowed to monitor this
4435 DBG("Skipping client at it does not have the object permission to receive notification for this trigger");
4440 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
)) {
4441 DBG("Skipping client at it does not have the permission to receive notification for this trigger");
4445 DBG("Sending notification to client (fd = %i, %zu bytes)",
4446 client
->socket
, msg_payload
.buffer
.size
);
4448 if (client_has_outbound_data_left(client
)) {
4450 * Outgoing data is already buffered for this client;
4451 * drop the notification and enqueue a "dropped
4452 * notification" message if this is the first dropped
4453 * notification since the socket spilled-over to the
4456 ret
= client_notification_overflow(client
);
4463 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4469 transmission_status
= client_flush_outgoing_queue(client
);
4470 pthread_mutex_unlock(&client
->lock
);
4471 ret
= client_report(client
, transmission_status
, user_data
);
4474 goto end_unlock_list
;
4480 pthread_mutex_unlock(&client
->lock
);
4483 goto end_unlock_list
;
4489 pthread_mutex_unlock(&client_list
->lock
);
4491 lttng_payload_reset(&msg_payload
);
4496 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4497 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4501 struct lttng_event_notifier_notification
*notification
= NULL
;
4502 char *capture_buffer
= NULL
;
4503 size_t capture_buffer_size
;
4504 void *reception_buffer
;
4505 size_t reception_size
;
4507 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4508 struct lttng_kernel_abi_event_notifier_notification kernel_notification
;
4510 /* Init lttng_event_notifier_notification */
4512 case LTTNG_DOMAIN_UST
:
4513 reception_buffer
= (void *) &ust_notification
;
4514 reception_size
= sizeof(ust_notification
);
4516 case LTTNG_DOMAIN_KERNEL
:
4517 reception_buffer
= (void *) &kernel_notification
;
4518 reception_size
= sizeof(kernel_notification
);
4525 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4526 * ensuring that read/write of tracer notifications are atomic.
4528 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4530 if (ret
!= reception_size
) {
4531 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4532 notification_pipe_read_fd
, reception_size
, ret
);
4538 case LTTNG_DOMAIN_UST
:
4539 token
= ust_notification
.token
;
4540 capture_buffer_size
= ust_notification
.capture_buf_size
;
4542 case LTTNG_DOMAIN_KERNEL
:
4543 token
= kernel_notification
.token
;
4544 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4550 if (capture_buffer_size
== 0) {
4551 capture_buffer
= NULL
;
4555 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4556 ERR("Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4557 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4561 capture_buffer
= zmalloc(capture_buffer_size
);
4562 if (!capture_buffer
) {
4563 ERR("Failed to allocate capture buffer");
4567 /* Fetch additional payload (capture). */
4568 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4569 if (ret
!= capture_buffer_size
) {
4570 ERR("Failed to read from event source pipe (fd = %i)",
4571 notification_pipe_read_fd
);
4576 notification
= lttng_event_notifier_notification_create(token
, domain
,
4577 capture_buffer
, capture_buffer_size
);
4578 if (notification
== NULL
) {
4583 * Ownership transfered to the lttng_event_notifier_notification object.
4585 capture_buffer
= NULL
;
4588 free(capture_buffer
);
4589 return notification
;
4593 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4594 struct lttng_event_notifier_notification
*notification
)
4596 struct cds_lfht_node
*node
;
4597 struct cds_lfht_iter iter
;
4598 struct notification_trigger_tokens_ht_element
*element
;
4599 struct lttng_evaluation
*evaluation
= NULL
;
4600 enum action_executor_status executor_status
;
4601 struct notification_client_list
*client_list
= NULL
;
4603 unsigned int capture_count
= 0;
4605 /* Find triggers associated with this token. */
4607 cds_lfht_lookup(state
->trigger_tokens_ht
,
4608 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4609 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4610 node
= cds_lfht_iter_get_node(&iter
);
4611 if (caa_unlikely(!node
)) {
4613 * This is not an error, slow consumption of the tracer
4614 * notifications can lead to situations where a trigger is
4615 * removed but we still get tracer notifications matching a
4616 * trigger that no longer exists.
4622 element
= caa_container_of(node
,
4623 struct notification_trigger_tokens_ht_element
,
4626 if (lttng_condition_event_rule_matches_get_capture_descriptor_count(
4627 lttng_trigger_get_const_condition(element
->trigger
),
4628 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4629 ERR("Failed to get capture count");
4634 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4635 ERR("Expected capture but capture buffer is null");
4640 evaluation
= lttng_evaluation_event_rule_matches_create(
4641 container_of(lttng_trigger_get_const_condition(
4643 struct lttng_condition_event_rule_matches
,
4645 notification
->capture_buffer
,
4646 notification
->capture_buf_size
, false);
4648 if (evaluation
== NULL
) {
4649 ERR("Failed to create event rule matches evaluation while creating and enqueuing action executor job");
4654 client_list
= get_client_list_from_condition(state
,
4655 lttng_trigger_get_const_condition(element
->trigger
));
4656 executor_status
= action_executor_enqueue_trigger(state
->executor
,
4657 element
->trigger
, evaluation
, NULL
, client_list
);
4658 switch (executor_status
) {
4659 case ACTION_EXECUTOR_STATUS_OK
:
4662 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4664 struct notification_client_list_element
*client_list_element
,
4668 * Not a fatal error; this is expected and simply means the
4669 * executor has too much work queued already.
4673 /* No clients subscribed to notifications for this trigger. */
4678 /* Warn clients that a notification (or more) was dropped. */
4679 pthread_mutex_lock(&client_list
->lock
);
4680 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4681 &client_list
->clients_list
, node
) {
4682 enum client_transmission_status transmission_status
;
4683 struct notification_client
*client
=
4684 client_list_element
->client
;
4686 pthread_mutex_lock(&client
->lock
);
4687 ret
= client_notification_overflow(client
);
4693 transmission_status
=
4694 client_flush_outgoing_queue(client
);
4695 ret
= client_handle_transmission_status(
4696 client
, transmission_status
, state
);
4702 pthread_mutex_unlock(&client
->lock
);
4708 pthread_mutex_unlock(&client_list
->lock
);
4711 case ACTION_EXECUTOR_STATUS_INVALID
:
4712 case ACTION_EXECUTOR_STATUS_ERROR
:
4713 /* Fatal error, shut down everything. */
4714 ERR("Fatal error encoutered while enqueuing action to the action executor");
4718 /* Unhandled error. */
4723 notification_client_list_put(client_list
);
4730 int handle_one_event_notifier_notification(
4731 struct notification_thread_state
*state
,
4732 int pipe
, enum lttng_domain_type domain
)
4735 struct lttng_event_notifier_notification
*notification
= NULL
;
4737 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4738 if (notification
== NULL
) {
4739 /* Reception failed, don't consider it fatal. */
4740 ERR("Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4741 pipe
, lttng_domain_type_str(domain
));
4745 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4747 ERR("Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4748 pipe
, lttng_domain_type_str(domain
));
4753 lttng_event_notifier_notification_destroy(notification
);
4757 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4758 int pipe
, enum lttng_domain_type domain
)
4760 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4763 int handle_notification_thread_channel_sample(
4764 struct notification_thread_state
*state
, int pipe
,
4765 enum lttng_domain_type domain
)
4768 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4769 struct channel_info
*channel_info
;
4770 struct cds_lfht_node
*node
;
4771 struct cds_lfht_iter iter
;
4772 struct lttng_channel_trigger_list
*trigger_list
;
4773 struct lttng_trigger_list_element
*trigger_list_element
;
4774 bool previous_sample_available
= false;
4775 struct channel_state_sample previous_sample
, latest_sample
;
4776 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4777 struct lttng_credentials channel_creds
;
4780 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4781 * ensuring that read/write of sampling messages are atomic.
4783 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4784 if (ret
!= sizeof(sample_msg
)) {
4785 ERR("Failed to read from monitoring pipe (fd = %i)",
4792 latest_sample
.key
.key
= sample_msg
.key
;
4793 latest_sample
.key
.domain
= domain
;
4794 latest_sample
.highest_usage
= sample_msg
.highest
;
4795 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4796 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4800 /* Retrieve the channel's informations */
4801 cds_lfht_lookup(state
->channels_ht
,
4802 hash_channel_key(&latest_sample
.key
),
4806 node
= cds_lfht_iter_get_node(&iter
);
4807 if (caa_unlikely(!node
)) {
4809 * Not an error since the consumer can push a sample to the pipe
4810 * and the rest of the session daemon could notify us of the
4811 * channel's destruction before we get a chance to process that
4814 DBG("Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4815 latest_sample
.key
.key
,
4816 lttng_domain_type_str(domain
));
4819 channel_info
= caa_container_of(node
, struct channel_info
,
4821 DBG("Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4823 latest_sample
.key
.key
,
4824 channel_info
->session_info
->name
,
4825 latest_sample
.highest_usage
,
4826 latest_sample
.lowest_usage
,
4827 latest_sample
.channel_total_consumed
);
4829 previous_session_consumed_total
=
4830 channel_info
->session_info
->consumed_data_size
;
4832 /* Retrieve the channel's last sample, if it exists, and update it. */
4833 cds_lfht_lookup(state
->channel_state_ht
,
4834 hash_channel_key(&latest_sample
.key
),
4835 match_channel_state_sample
,
4838 node
= cds_lfht_iter_get_node(&iter
);
4839 if (caa_likely(node
)) {
4840 struct channel_state_sample
*stored_sample
;
4842 /* Update the sample stored. */
4843 stored_sample
= caa_container_of(node
,
4844 struct channel_state_sample
,
4845 channel_state_ht_node
);
4847 memcpy(&previous_sample
, stored_sample
,
4848 sizeof(previous_sample
));
4849 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4850 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4851 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4852 previous_sample_available
= true;
4854 latest_session_consumed_total
=
4855 previous_session_consumed_total
+
4856 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4859 * This is the channel's first sample, allocate space for and
4860 * store the new sample.
4862 struct channel_state_sample
*stored_sample
;
4864 stored_sample
= zmalloc(sizeof(*stored_sample
));
4865 if (!stored_sample
) {
4870 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4871 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4872 cds_lfht_add(state
->channel_state_ht
,
4873 hash_channel_key(&stored_sample
->key
),
4874 &stored_sample
->channel_state_ht_node
);
4876 latest_session_consumed_total
=
4877 previous_session_consumed_total
+
4878 latest_sample
.channel_total_consumed
;
4881 channel_info
->session_info
->consumed_data_size
=
4882 latest_session_consumed_total
;
4884 /* Find triggers associated with this channel. */
4885 cds_lfht_lookup(state
->channel_triggers_ht
,
4886 hash_channel_key(&latest_sample
.key
),
4887 match_channel_trigger_list
,
4890 node
= cds_lfht_iter_get_node(&iter
);
4891 if (caa_likely(!node
)) {
4895 channel_creds
= (typeof(channel_creds
)) {
4896 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4897 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4900 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4901 channel_triggers_ht_node
);
4902 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4904 const struct lttng_condition
*condition
;
4905 struct lttng_trigger
*trigger
;
4906 struct notification_client_list
*client_list
= NULL
;
4907 struct lttng_evaluation
*evaluation
= NULL
;
4908 enum action_executor_status executor_status
;
4911 trigger
= trigger_list_element
->trigger
;
4912 condition
= lttng_trigger_get_const_condition(trigger
);
4913 LTTNG_ASSERT(condition
);
4916 * Check if any client is subscribed to the result of this
4919 client_list
= get_client_list_from_condition(state
, condition
);
4921 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4922 previous_sample_available
? &previous_sample
: NULL
,
4924 previous_session_consumed_total
,
4925 latest_session_consumed_total
,
4927 if (caa_unlikely(ret
)) {
4931 if (caa_likely(!evaluation
)) {
4936 * Ownership of `evaluation` transferred to the action executor
4937 * no matter the result.
4939 executor_status
= action_executor_enqueue_trigger(
4940 state
->executor
, trigger
, evaluation
,
4941 &channel_creds
, client_list
);
4943 switch (executor_status
) {
4944 case ACTION_EXECUTOR_STATUS_OK
:
4946 case ACTION_EXECUTOR_STATUS_ERROR
:
4947 case ACTION_EXECUTOR_STATUS_INVALID
:
4949 * TODO Add trigger identification (name/id) when
4950 * it is added to the API.
4952 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4955 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4957 * TODO Add trigger identification (name/id) when
4958 * it is added to the API.
4960 * Not a fatal error.
4962 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4970 notification_client_list_put(client_list
);
4971 if (caa_unlikely(ret
)) {