2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.h"
12 #include <urcu/rculfhash.h>
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/futex.h>
17 #include <common/unix.h>
18 #include <common/dynamic-buffer.h>
19 #include <common/hashtable/utils.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/macros.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.h>
24 #include <lttng/action/group-internal.h>
25 #include <lttng/domain-internal.h>
26 #include <lttng/notification/notification-internal.h>
27 #include <lttng/condition/condition-internal.h>
28 #include <lttng/condition/buffer-usage-internal.h>
29 #include <lttng/condition/session-consumed-size-internal.h>
30 #include <lttng/condition/session-rotation-internal.h>
31 #include <lttng/condition/on-event-internal.h>
32 #include <lttng/domain-internal.h>
33 #include <lttng/notification/channel-internal.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <lttng/event-rule/event-rule-internal.h>
43 #include "condition-internal.h"
44 #include "event-notifier-error-accounting.h"
45 #include "notification-thread.h"
46 #include "notification-thread-events.h"
47 #include "notification-thread-commands.h"
48 #include "lttng-sessiond.h"
51 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
52 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
54 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
55 #define MAX_CAPTURE_SIZE (PIPE_BUF)
57 enum lttng_object_type
{
58 LTTNG_OBJECT_TYPE_UNKNOWN
,
59 LTTNG_OBJECT_TYPE_NONE
,
60 LTTNG_OBJECT_TYPE_CHANNEL
,
61 LTTNG_OBJECT_TYPE_SESSION
,
64 struct lttng_trigger_list_element
{
65 /* No ownership of the trigger object is assumed. */
66 struct lttng_trigger
*trigger
;
67 struct cds_list_head node
;
70 struct lttng_channel_trigger_list
{
71 struct channel_key channel_key
;
72 /* List of struct lttng_trigger_list_element. */
73 struct cds_list_head list
;
74 /* Node in the channel_triggers_ht */
75 struct cds_lfht_node channel_triggers_ht_node
;
76 /* call_rcu delayed reclaim. */
77 struct rcu_head rcu_node
;
81 * List of triggers applying to a given session.
84 * - lttng_session_trigger_list_create()
85 * - lttng_session_trigger_list_build()
86 * - lttng_session_trigger_list_destroy()
87 * - lttng_session_trigger_list_add()
89 struct lttng_session_trigger_list
{
91 * Not owned by this; points to the session_info structure's
94 const char *session_name
;
95 /* List of struct lttng_trigger_list_element. */
96 struct cds_list_head list
;
97 /* Node in the session_triggers_ht */
98 struct cds_lfht_node session_triggers_ht_node
;
100 * Weak reference to the notification system's session triggers
103 * The session trigger list structure structure is owned by
104 * the session's session_info.
106 * The session_info is kept alive the the channel_infos holding a
107 * reference to it (reference counting). When those channels are
108 * destroyed (at runtime or on teardown), the reference they hold
109 * to the session_info are released. On destruction of session_info,
110 * session_info_destroy() will remove the list of triggers applying
111 * to this session from the notification system's state.
113 * This implies that the session_triggers_ht must be destroyed
114 * after the channels.
116 struct cds_lfht
*session_triggers_ht
;
117 /* Used for delayed RCU reclaim. */
118 struct rcu_head rcu_node
;
121 struct lttng_trigger_ht_element
{
122 struct lttng_trigger
*trigger
;
123 struct cds_lfht_node node
;
124 struct cds_lfht_node node_by_name_uid
;
125 struct cds_list_head client_list_trigger_node
;
126 /* call_rcu delayed reclaim. */
127 struct rcu_head rcu_node
;
130 struct lttng_condition_list_element
{
131 struct lttng_condition
*condition
;
132 struct cds_list_head node
;
135 struct channel_state_sample
{
136 struct channel_key key
;
137 struct cds_lfht_node channel_state_ht_node
;
138 uint64_t highest_usage
;
139 uint64_t lowest_usage
;
140 uint64_t channel_total_consumed
;
141 /* call_rcu delayed reclaim. */
142 struct rcu_head rcu_node
;
145 static unsigned long hash_channel_key(struct channel_key
*key
);
146 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
147 struct lttng_evaluation
**evaluation
,
148 const struct notification_thread_state
*state
,
149 const struct channel_state_sample
*previous_sample
,
150 const struct channel_state_sample
*latest_sample
,
151 uint64_t previous_session_consumed_total
,
152 uint64_t latest_session_consumed_total
,
153 struct channel_info
*channel_info
);
155 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
156 const struct lttng_evaluation
*evaluation
,
157 struct notification_client_list
*client_list
,
158 struct notification_thread_state
*state
,
159 uid_t channel_uid
, gid_t channel_gid
);
162 /* session_info API */
164 void session_info_destroy(void *_data
);
166 void session_info_get(struct session_info
*session_info
);
168 void session_info_put(struct session_info
*session_info
);
170 struct session_info
*session_info_create(const char *name
,
171 uid_t uid
, gid_t gid
,
172 struct lttng_session_trigger_list
*trigger_list
,
173 struct cds_lfht
*sessions_ht
);
175 void session_info_add_channel(struct session_info
*session_info
,
176 struct channel_info
*channel_info
);
178 void session_info_remove_channel(struct session_info
*session_info
,
179 struct channel_info
*channel_info
);
181 /* lttng_session_trigger_list API */
183 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
184 const char *session_name
,
185 struct cds_lfht
*session_triggers_ht
);
187 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
188 const struct notification_thread_state
*state
,
189 const char *session_name
);
191 void lttng_session_trigger_list_destroy(
192 struct lttng_session_trigger_list
*list
);
194 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
195 struct lttng_trigger
*trigger
);
198 int client_handle_transmission_status(
199 struct notification_client
*client
,
200 enum client_transmission_status transmission_status
,
201 struct notification_thread_state
*state
);
204 int handle_one_event_notifier_notification(
205 struct notification_thread_state
*state
,
206 int pipe
, enum lttng_domain_type domain
);
209 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
212 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
214 /* This double-cast is intended to supress pointer-to-cast warning. */
215 const int socket
= (int) (intptr_t) key
;
216 const struct notification_client
*client
= caa_container_of(node
,
217 struct notification_client
, client_socket_ht_node
);
219 return client
->socket
== socket
;
223 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
225 /* This double-cast is intended to supress pointer-to-cast warning. */
226 const notification_client_id id
= *((notification_client_id
*) key
);
227 const struct notification_client
*client
= caa_container_of(
228 node
, struct notification_client
, client_id_ht_node
);
230 return client
->id
== id
;
234 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
236 struct channel_key
*channel_key
= (struct channel_key
*) key
;
237 struct lttng_channel_trigger_list
*trigger_list
;
239 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
240 channel_triggers_ht_node
);
242 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
243 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
247 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
249 const char *session_name
= (const char *) key
;
250 struct lttng_session_trigger_list
*trigger_list
;
252 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
253 session_triggers_ht_node
);
255 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
259 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
261 struct channel_key
*channel_key
= (struct channel_key
*) key
;
262 struct channel_state_sample
*sample
;
264 sample
= caa_container_of(node
, struct channel_state_sample
,
265 channel_state_ht_node
);
267 return !!((channel_key
->key
== sample
->key
.key
) &&
268 (channel_key
->domain
== sample
->key
.domain
));
272 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
274 struct channel_key
*channel_key
= (struct channel_key
*) key
;
275 struct channel_info
*channel_info
;
277 channel_info
= caa_container_of(node
, struct channel_info
,
280 return !!((channel_key
->key
== channel_info
->key
.key
) &&
281 (channel_key
->domain
== channel_info
->key
.domain
));
285 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
287 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
288 struct lttng_trigger_ht_element
*trigger_ht_element
;
290 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
293 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
297 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
299 const uint64_t *_key
= key
;
300 struct notification_trigger_tokens_ht_element
*element
;
302 element
= caa_container_of(node
,
303 struct notification_trigger_tokens_ht_element
, node
);
304 return *_key
== element
->token
;
308 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
310 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
311 struct notification_client_list
*client_list
;
312 const struct lttng_condition
*condition
;
314 assert(condition_key
);
316 client_list
= caa_container_of(node
, struct notification_client_list
,
317 notification_trigger_clients_ht_node
);
318 condition
= client_list
->condition
;
320 return !!lttng_condition_is_equal(condition_key
, condition
);
324 int match_session(struct cds_lfht_node
*node
, const void *key
)
326 const char *name
= key
;
327 struct session_info
*session_info
= caa_container_of(
328 node
, struct session_info
, sessions_ht_node
);
330 return !strcmp(session_info
->name
, name
);
334 const char *notification_command_type_str(
335 enum notification_thread_command_type type
)
338 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
339 return "REGISTER_TRIGGER";
340 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
341 return "UNREGISTER_TRIGGER";
342 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
343 return "ADD_CHANNEL";
344 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
345 return "REMOVE_CHANNEL";
346 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
347 return "SESSION_ROTATION_ONGOING";
348 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
349 return "SESSION_ROTATION_COMPLETED";
350 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
351 return "ADD_TRACER_EVENT_SOURCE";
352 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
353 return "REMOVE_TRACER_EVENT_SOURCE";
354 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
355 return "LIST_TRIGGERS";
356 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
357 return "GET_TRIGGER";
358 case NOTIFICATION_COMMAND_TYPE_QUIT
:
360 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
361 return "CLIENT_COMMUNICATION_UPDATE";
368 * Match trigger based on name and credentials only.
369 * Name duplication is NOT allowed for the same uid.
372 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
377 const char *key_name
;
378 enum lttng_trigger_status status
;
379 const struct lttng_credentials
*key_creds
;
380 const struct lttng_credentials
*node_creds
;
381 const struct lttng_trigger
*trigger_key
=
382 (const struct lttng_trigger
*) key
;
383 const struct lttng_trigger_ht_element
*trigger_ht_element
=
384 caa_container_of(node
,
385 struct lttng_trigger_ht_element
,
388 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
389 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
391 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
392 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
394 /* Compare the names. */
395 if (strcmp(name
, key_name
) != 0) {
399 /* Compare the owners' UIDs. */
400 key_creds
= lttng_trigger_get_credentials(trigger_key
);
401 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
403 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
410 * Hash trigger based on name and credentials only.
413 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
415 unsigned long hash
= 0;
416 const struct lttng_credentials
*trigger_creds
;
417 const char *trigger_name
;
418 enum lttng_trigger_status status
;
420 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
421 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
422 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
425 trigger_creds
= lttng_trigger_get_credentials(trigger
);
426 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
433 unsigned long hash_channel_key(struct channel_key
*key
)
435 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
436 unsigned long domain_hash
= hash_key_ulong(
437 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
439 return key_hash
^ domain_hash
;
443 unsigned long hash_client_socket(int socket
)
445 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
449 unsigned long hash_client_id(notification_client_id id
)
451 return hash_key_u64(&id
, lttng_ht_seed
);
455 * Get the type of object to which a given condition applies. Bindings let
456 * the notification system evaluate a trigger's condition when a given
457 * object's state is updated.
459 * For instance, a condition bound to a channel will be evaluated everytime
460 * the channel's state is changed by a channel monitoring sample.
463 enum lttng_object_type
get_condition_binding_object(
464 const struct lttng_condition
*condition
)
466 switch (lttng_condition_get_type(condition
)) {
467 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
468 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
469 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
470 return LTTNG_OBJECT_TYPE_CHANNEL
;
471 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
472 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
473 return LTTNG_OBJECT_TYPE_SESSION
;
474 case LTTNG_CONDITION_TYPE_ON_EVENT
:
475 return LTTNG_OBJECT_TYPE_NONE
;
477 return LTTNG_OBJECT_TYPE_UNKNOWN
;
482 void free_channel_info_rcu(struct rcu_head
*node
)
484 free(caa_container_of(node
, struct channel_info
, rcu_node
));
488 void channel_info_destroy(struct channel_info
*channel_info
)
494 if (channel_info
->session_info
) {
495 session_info_remove_channel(channel_info
->session_info
,
497 session_info_put(channel_info
->session_info
);
499 if (channel_info
->name
) {
500 free(channel_info
->name
);
502 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
506 void free_session_info_rcu(struct rcu_head
*node
)
508 free(caa_container_of(node
, struct session_info
, rcu_node
));
511 /* Don't call directly, use the ref-counting mechanism. */
513 void session_info_destroy(void *_data
)
515 struct session_info
*session_info
= _data
;
518 assert(session_info
);
519 if (session_info
->channel_infos_ht
) {
520 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
522 ERR("[notification-thread] Failed to destroy channel information hash table");
525 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
528 cds_lfht_del(session_info
->sessions_ht
,
529 &session_info
->sessions_ht_node
);
531 free(session_info
->name
);
532 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
536 void session_info_get(struct session_info
*session_info
)
541 lttng_ref_get(&session_info
->ref
);
545 void session_info_put(struct session_info
*session_info
)
550 lttng_ref_put(&session_info
->ref
);
554 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
555 struct lttng_session_trigger_list
*trigger_list
,
556 struct cds_lfht
*sessions_ht
)
558 struct session_info
*session_info
;
562 session_info
= zmalloc(sizeof(*session_info
));
566 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
568 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
569 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
570 if (!session_info
->channel_infos_ht
) {
574 cds_lfht_node_init(&session_info
->sessions_ht_node
);
575 session_info
->name
= strdup(name
);
576 if (!session_info
->name
) {
579 session_info
->uid
= uid
;
580 session_info
->gid
= gid
;
581 session_info
->trigger_list
= trigger_list
;
582 session_info
->sessions_ht
= sessions_ht
;
586 session_info_put(session_info
);
591 void session_info_add_channel(struct session_info
*session_info
,
592 struct channel_info
*channel_info
)
595 cds_lfht_add(session_info
->channel_infos_ht
,
596 hash_channel_key(&channel_info
->key
),
597 &channel_info
->session_info_channels_ht_node
);
602 void session_info_remove_channel(struct session_info
*session_info
,
603 struct channel_info
*channel_info
)
606 cds_lfht_del(session_info
->channel_infos_ht
,
607 &channel_info
->session_info_channels_ht_node
);
612 struct channel_info
*channel_info_create(const char *channel_name
,
613 struct channel_key
*channel_key
, uint64_t channel_capacity
,
614 struct session_info
*session_info
)
616 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
622 cds_lfht_node_init(&channel_info
->channels_ht_node
);
623 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
624 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
625 channel_info
->capacity
= channel_capacity
;
627 channel_info
->name
= strdup(channel_name
);
628 if (!channel_info
->name
) {
633 * Set the references between session and channel infos:
634 * - channel_info holds a strong reference to session_info
635 * - session_info holds a weak reference to channel_info
637 session_info_get(session_info
);
638 session_info_add_channel(session_info
, channel_info
);
639 channel_info
->session_info
= session_info
;
643 channel_info_destroy(channel_info
);
648 bool notification_client_list_get(struct notification_client_list
*list
)
650 return urcu_ref_get_unless_zero(&list
->ref
);
654 void free_notification_client_list_rcu(struct rcu_head
*node
)
656 free(caa_container_of(node
, struct notification_client_list
,
661 void notification_client_list_release(struct urcu_ref
*list_ref
)
663 struct notification_client_list
*list
=
664 container_of(list_ref
, typeof(*list
), ref
);
665 struct notification_client_list_element
*client_list_element
, *tmp
;
667 lttng_condition_put(list
->condition
);
669 if (list
->notification_trigger_clients_ht
) {
672 cds_lfht_del(list
->notification_trigger_clients_ht
,
673 &list
->notification_trigger_clients_ht_node
);
675 list
->notification_trigger_clients_ht
= NULL
;
677 cds_list_for_each_entry_safe(client_list_element
, tmp
,
678 &list
->clients_list
, node
) {
679 free(client_list_element
);
682 assert(cds_list_empty(&list
->triggers_list
));
684 pthread_mutex_destroy(&list
->lock
);
685 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
689 bool condition_applies_to_client(const struct lttng_condition
*condition
,
690 struct notification_client
*client
)
692 bool applies
= false;
693 struct lttng_condition_list_element
*condition_list_element
;
695 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
697 applies
= lttng_condition_is_equal(
698 condition_list_element
->condition
,
709 struct notification_client_list
*notification_client_list_create(
710 struct notification_thread_state
*state
,
711 const struct lttng_condition
*condition
)
713 struct notification_client
*client
;
714 struct cds_lfht_iter iter
;
715 struct notification_client_list
*client_list
;
717 client_list
= zmalloc(sizeof(*client_list
));
719 PERROR("Failed to allocate notification client list");
723 pthread_mutex_init(&client_list
->lock
, NULL
);
725 * The trigger that owns the condition has the first reference to this
728 urcu_ref_init(&client_list
->ref
);
729 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
730 CDS_INIT_LIST_HEAD(&client_list
->clients_list
);
731 CDS_INIT_LIST_HEAD(&client_list
->triggers_list
);
734 * Create a copy of the condition so that it's independent of any
735 * trigger. The client list may outlive the trigger object (which owns
736 * the condition) that is used to create it.
738 client_list
->condition
= lttng_condition_copy(condition
);
740 /* Build a list of clients to which this new condition applies. */
741 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
742 client_socket_ht_node
) {
743 struct notification_client_list_element
*client_list_element
;
745 if (!condition_applies_to_client(condition
, client
)) {
749 client_list_element
= zmalloc(sizeof(*client_list_element
));
750 if (!client_list_element
) {
751 goto error_put_client_list
;
754 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
755 client_list_element
->client
= client
;
756 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
759 client_list
->notification_trigger_clients_ht
=
760 state
->notification_trigger_clients_ht
;
764 * Add the client list to the global list of client list.
766 cds_lfht_add_unique(state
->notification_trigger_clients_ht
,
767 lttng_condition_hash(client_list
->condition
),
768 match_client_list_condition
,
769 client_list
->condition
,
770 &client_list
->notification_trigger_clients_ht_node
);
774 error_put_client_list
:
775 notification_client_list_put(client_list
);
782 void notification_client_list_put(struct notification_client_list
*list
)
787 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
790 /* Provides a reference to the returned list. */
792 struct notification_client_list
*get_client_list_from_condition(
793 struct notification_thread_state
*state
,
794 const struct lttng_condition
*condition
)
796 struct cds_lfht_node
*node
;
797 struct cds_lfht_iter iter
;
798 struct notification_client_list
*list
= NULL
;
801 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
802 lttng_condition_hash(condition
),
803 match_client_list_condition
,
806 node
= cds_lfht_iter_get_node(&iter
);
808 list
= container_of(node
, struct notification_client_list
,
809 notification_trigger_clients_ht_node
);
810 list
= notification_client_list_get(list
) ? list
: NULL
;
818 int evaluate_channel_condition_for_client(
819 const struct lttng_condition
*condition
,
820 struct notification_thread_state
*state
,
821 struct lttng_evaluation
**evaluation
,
822 uid_t
*session_uid
, gid_t
*session_gid
)
825 struct cds_lfht_iter iter
;
826 struct cds_lfht_node
*node
;
827 struct channel_info
*channel_info
= NULL
;
828 struct channel_key
*channel_key
= NULL
;
829 struct channel_state_sample
*last_sample
= NULL
;
830 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
834 /* Find the channel associated with the condition. */
835 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
836 channel_trigger_list
, channel_triggers_ht_node
) {
837 struct lttng_trigger_list_element
*element
;
839 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
840 const struct lttng_condition
*current_condition
=
841 lttng_trigger_get_const_condition(
844 assert(current_condition
);
845 if (!lttng_condition_is_equal(condition
,
846 current_condition
)) {
850 /* Found the trigger, save the channel key. */
851 channel_key
= &channel_trigger_list
->channel_key
;
855 /* The channel key was found stop iteration. */
861 /* No channel found; normal exit. */
862 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
867 /* Fetch channel info for the matching channel. */
868 cds_lfht_lookup(state
->channels_ht
,
869 hash_channel_key(channel_key
),
873 node
= cds_lfht_iter_get_node(&iter
);
875 channel_info
= caa_container_of(node
, struct channel_info
,
878 /* Retrieve the channel's last sample, if it exists. */
879 cds_lfht_lookup(state
->channel_state_ht
,
880 hash_channel_key(channel_key
),
881 match_channel_state_sample
,
884 node
= cds_lfht_iter_get_node(&iter
);
886 last_sample
= caa_container_of(node
,
887 struct channel_state_sample
,
888 channel_state_ht_node
);
890 /* Nothing to evaluate, no sample was ever taken. Normal exit */
891 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
896 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
898 0, channel_info
->session_info
->consumed_data_size
,
901 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
905 *session_uid
= channel_info
->session_info
->uid
;
906 *session_gid
= channel_info
->session_info
->gid
;
913 const char *get_condition_session_name(const struct lttng_condition
*condition
)
915 const char *session_name
= NULL
;
916 enum lttng_condition_status status
;
918 switch (lttng_condition_get_type(condition
)) {
919 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
920 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
921 status
= lttng_condition_buffer_usage_get_session_name(
922 condition
, &session_name
);
924 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
925 status
= lttng_condition_session_consumed_size_get_session_name(
926 condition
, &session_name
);
928 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
929 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
930 status
= lttng_condition_session_rotation_get_session_name(
931 condition
, &session_name
);
936 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
937 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
945 int evaluate_session_condition_for_client(
946 const struct lttng_condition
*condition
,
947 struct notification_thread_state
*state
,
948 struct lttng_evaluation
**evaluation
,
949 uid_t
*session_uid
, gid_t
*session_gid
)
952 struct cds_lfht_iter iter
;
953 struct cds_lfht_node
*node
;
954 const char *session_name
;
955 struct session_info
*session_info
= NULL
;
958 session_name
= get_condition_session_name(condition
);
960 /* Find the session associated with the trigger. */
961 cds_lfht_lookup(state
->sessions_ht
,
962 hash_key_str(session_name
, lttng_ht_seed
),
966 node
= cds_lfht_iter_get_node(&iter
);
968 DBG("[notification-thread] No known session matching name \"%s\"",
974 session_info
= caa_container_of(node
, struct session_info
,
976 session_info_get(session_info
);
979 * Evaluation is performed in-line here since only one type of
980 * session-bound condition is handled for the moment.
982 switch (lttng_condition_get_type(condition
)) {
983 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
984 if (!session_info
->rotation
.ongoing
) {
986 goto end_session_put
;
989 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
990 session_info
->rotation
.id
);
993 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
996 goto end_session_put
;
1002 goto end_session_put
;
1005 *session_uid
= session_info
->uid
;
1006 *session_gid
= session_info
->gid
;
1009 session_info_put(session_info
);
1016 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1017 const struct lttng_condition
*condition
,
1018 struct notification_client
*client
,
1019 struct notification_thread_state
*state
)
1022 struct lttng_evaluation
*evaluation
= NULL
;
1023 struct notification_client_list client_list
= {
1024 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1026 struct notification_client_list_element client_list_element
= { 0 };
1027 uid_t object_uid
= 0;
1028 gid_t object_gid
= 0;
1035 switch (get_condition_binding_object(condition
)) {
1036 case LTTNG_OBJECT_TYPE_SESSION
:
1037 ret
= evaluate_session_condition_for_client(condition
, state
,
1038 &evaluation
, &object_uid
, &object_gid
);
1040 case LTTNG_OBJECT_TYPE_CHANNEL
:
1041 ret
= evaluate_channel_condition_for_client(condition
, state
,
1042 &evaluation
, &object_uid
, &object_gid
);
1044 case LTTNG_OBJECT_TYPE_NONE
:
1045 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
1048 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1058 /* Evaluation yielded nothing. Normal exit. */
1059 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
1065 * Create a temporary client list with the client currently
1068 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1069 CDS_INIT_LIST_HEAD(&client_list
.clients_list
);
1071 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1072 client_list_element
.client
= client
;
1073 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1075 /* Send evaluation result to the newly-subscribed client. */
1076 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1077 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1078 state
, object_uid
, object_gid
);
1085 int notification_thread_client_subscribe(struct notification_client
*client
,
1086 struct lttng_condition
*condition
,
1087 struct notification_thread_state
*state
,
1088 enum lttng_notification_channel_status
*_status
)
1091 struct notification_client_list
*client_list
= NULL
;
1092 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1093 struct notification_client_list_element
*client_list_element
= NULL
;
1094 struct lttng_trigger_ht_element
*trigger_ht_element
;
1095 enum lttng_notification_channel_status status
=
1096 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1099 * Ensure that the client has not already subscribed to this condition
1102 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1103 if (lttng_condition_is_equal(condition_list_element
->condition
,
1105 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1110 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1111 if (!condition_list_element
) {
1115 client_list_element
= zmalloc(sizeof(*client_list_element
));
1116 if (!client_list_element
) {
1122 * Add the newly-subscribed condition to the client's subscription list.
1124 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1125 condition_list_element
->condition
= condition
;
1126 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1128 client_list
= get_client_list_from_condition(state
, condition
);
1131 * No notification-emiting trigger registered with this
1132 * condition. We don't evaluate the condition right away
1133 * since this trigger is not registered yet.
1135 free(client_list_element
);
1140 * The condition to which the client just subscribed is evaluated
1141 * at this point so that conditions that are already TRUE result
1142 * in a notification being sent out.
1144 * Note the iteration on all triggers which share an identical
1145 * `condition` than the one to which the client is registering. This is
1146 * done to ensure that the client receives a distinct notification for
1147 * all triggers that have a `notify` action that have this condition.
1149 pthread_mutex_lock(&client_list
->lock
);
1150 cds_list_for_each_entry(trigger_ht_element
,
1151 &client_list
->triggers_list
, client_list_trigger_node
) {
1152 if (evaluate_condition_for_client(trigger_ht_element
->trigger
, condition
,
1154 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1156 free(client_list_element
);
1157 pthread_mutex_unlock(&client_list
->lock
);
1161 pthread_mutex_unlock(&client_list
->lock
);
1164 * Add the client to the list of clients interested in a given trigger
1165 * if a "notification" trigger with a corresponding condition was
1168 client_list_element
->client
= client
;
1169 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1171 pthread_mutex_lock(&client_list
->lock
);
1172 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1173 pthread_mutex_unlock(&client_list
->lock
);
1179 notification_client_list_put(client_list
);
1183 free(condition_list_element
);
1184 free(client_list_element
);
1189 int notification_thread_client_unsubscribe(
1190 struct notification_client
*client
,
1191 struct lttng_condition
*condition
,
1192 struct notification_thread_state
*state
,
1193 enum lttng_notification_channel_status
*_status
)
1195 struct notification_client_list
*client_list
;
1196 struct lttng_condition_list_element
*condition_list_element
,
1198 struct notification_client_list_element
*client_list_element
,
1200 bool condition_found
= false;
1201 enum lttng_notification_channel_status status
=
1202 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1204 /* Remove the condition from the client's condition list. */
1205 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1206 &client
->condition_list
, node
) {
1207 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1212 cds_list_del(&condition_list_element
->node
);
1214 * The caller may be iterating on the client's conditions to
1215 * tear down a client's connection. In this case, the condition
1216 * will be destroyed at the end.
1218 if (condition
!= condition_list_element
->condition
) {
1219 lttng_condition_destroy(
1220 condition_list_element
->condition
);
1222 free(condition_list_element
);
1223 condition_found
= true;
1227 if (!condition_found
) {
1228 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1233 * Remove the client from the list of clients interested the trigger
1234 * matching the condition.
1236 client_list
= get_client_list_from_condition(state
, condition
);
1241 pthread_mutex_lock(&client_list
->lock
);
1242 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1243 &client_list
->clients_list
, node
) {
1244 if (client_list_element
->client
->id
!= client
->id
) {
1247 cds_list_del(&client_list_element
->node
);
1248 free(client_list_element
);
1251 pthread_mutex_unlock(&client_list
->lock
);
1252 notification_client_list_put(client_list
);
1255 lttng_condition_destroy(condition
);
1263 void free_notification_client_rcu(struct rcu_head
*node
)
1265 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1269 void notification_client_destroy(struct notification_client
*client
,
1270 struct notification_thread_state
*state
)
1277 * The client object is not reachable by other threads, no need to lock
1280 if (client
->socket
>= 0) {
1281 (void) lttcomm_close_unix_sock(client
->socket
);
1282 client
->socket
= -1;
1284 client
->communication
.active
= false;
1285 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1286 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1287 pthread_mutex_destroy(&client
->lock
);
1288 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1292 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1296 struct notification_client
*get_client_from_socket(int socket
,
1297 struct notification_thread_state
*state
)
1299 struct cds_lfht_iter iter
;
1300 struct cds_lfht_node
*node
;
1301 struct notification_client
*client
= NULL
;
1303 cds_lfht_lookup(state
->client_socket_ht
,
1304 hash_client_socket(socket
),
1305 match_client_socket
,
1306 (void *) (unsigned long) socket
,
1308 node
= cds_lfht_iter_get_node(&iter
);
1313 client
= caa_container_of(node
, struct notification_client
,
1314 client_socket_ht_node
);
1320 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1324 struct notification_client
*get_client_from_id(notification_client_id id
,
1325 struct notification_thread_state
*state
)
1327 struct cds_lfht_iter iter
;
1328 struct cds_lfht_node
*node
;
1329 struct notification_client
*client
= NULL
;
1331 cds_lfht_lookup(state
->client_id_ht
,
1336 node
= cds_lfht_iter_get_node(&iter
);
1341 client
= caa_container_of(node
, struct notification_client
,
1348 bool buffer_usage_condition_applies_to_channel(
1349 const struct lttng_condition
*condition
,
1350 const struct channel_info
*channel_info
)
1352 enum lttng_condition_status status
;
1353 enum lttng_domain_type condition_domain
;
1354 const char *condition_session_name
= NULL
;
1355 const char *condition_channel_name
= NULL
;
1357 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1359 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1360 if (channel_info
->key
.domain
!= condition_domain
) {
1364 status
= lttng_condition_buffer_usage_get_session_name(
1365 condition
, &condition_session_name
);
1366 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1368 status
= lttng_condition_buffer_usage_get_channel_name(
1369 condition
, &condition_channel_name
);
1370 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1372 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1375 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1385 bool session_consumed_size_condition_applies_to_channel(
1386 const struct lttng_condition
*condition
,
1387 const struct channel_info
*channel_info
)
1389 enum lttng_condition_status status
;
1390 const char *condition_session_name
= NULL
;
1392 status
= lttng_condition_session_consumed_size_get_session_name(
1393 condition
, &condition_session_name
);
1394 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1396 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1406 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1407 const struct channel_info
*channel_info
)
1409 const struct lttng_condition
*condition
;
1410 bool trigger_applies
;
1412 condition
= lttng_trigger_get_const_condition(trigger
);
1417 switch (lttng_condition_get_type(condition
)) {
1418 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1419 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1420 trigger_applies
= buffer_usage_condition_applies_to_channel(
1421 condition
, channel_info
);
1423 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1424 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1425 condition
, channel_info
);
1431 return trigger_applies
;
1436 /* Must be called with RCU read lock held. */
1438 struct lttng_session_trigger_list
*get_session_trigger_list(
1439 struct notification_thread_state
*state
,
1440 const char *session_name
)
1442 struct lttng_session_trigger_list
*list
= NULL
;
1443 struct cds_lfht_node
*node
;
1444 struct cds_lfht_iter iter
;
1446 cds_lfht_lookup(state
->session_triggers_ht
,
1447 hash_key_str(session_name
, lttng_ht_seed
),
1448 match_session_trigger_list
,
1451 node
= cds_lfht_iter_get_node(&iter
);
1454 * Not an error, the list of triggers applying to that session
1455 * will be initialized when the session is created.
1457 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1462 list
= caa_container_of(node
,
1463 struct lttng_session_trigger_list
,
1464 session_triggers_ht_node
);
1470 * Allocate an empty lttng_session_trigger_list for the session named
1473 * No ownership of 'session_name' is assumed by the session trigger list.
1474 * It is the caller's responsability to ensure the session name is alive
1475 * for as long as this list is.
1478 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1479 const char *session_name
,
1480 struct cds_lfht
*session_triggers_ht
)
1482 struct lttng_session_trigger_list
*list
;
1484 list
= zmalloc(sizeof(*list
));
1488 list
->session_name
= session_name
;
1489 CDS_INIT_LIST_HEAD(&list
->list
);
1490 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1491 list
->session_triggers_ht
= session_triggers_ht
;
1494 /* Publish the list through the session_triggers_ht. */
1495 cds_lfht_add(session_triggers_ht
,
1496 hash_key_str(session_name
, lttng_ht_seed
),
1497 &list
->session_triggers_ht_node
);
1504 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1506 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1511 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1513 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1515 /* Empty the list element by element, and then free the list itself. */
1516 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1517 &list
->list
, node
) {
1518 cds_list_del(&trigger_list_element
->node
);
1519 free(trigger_list_element
);
1522 /* Unpublish the list from the session_triggers_ht. */
1523 cds_lfht_del(list
->session_triggers_ht
,
1524 &list
->session_triggers_ht_node
);
1526 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1530 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1531 struct lttng_trigger
*trigger
)
1534 struct lttng_trigger_list_element
*new_element
=
1535 zmalloc(sizeof(*new_element
));
1541 CDS_INIT_LIST_HEAD(&new_element
->node
);
1542 new_element
->trigger
= trigger
;
1543 cds_list_add(&new_element
->node
, &list
->list
);
1549 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1550 const char *session_name
)
1552 bool applies
= false;
1553 const struct lttng_condition
*condition
;
1555 condition
= lttng_trigger_get_const_condition(trigger
);
1556 switch (lttng_condition_get_type(condition
)) {
1557 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1558 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1560 enum lttng_condition_status condition_status
;
1561 const char *condition_session_name
;
1563 condition_status
= lttng_condition_session_rotation_get_session_name(
1564 condition
, &condition_session_name
);
1565 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1566 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1570 assert(condition_session_name
);
1571 applies
= !strcmp(condition_session_name
, session_name
);
1582 * Allocate and initialize an lttng_session_trigger_list which contains
1583 * all triggers that apply to the session named 'session_name'.
1585 * No ownership of 'session_name' is assumed by the session trigger list.
1586 * It is the caller's responsability to ensure the session name is alive
1587 * for as long as this list is.
1590 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1591 const struct notification_thread_state
*state
,
1592 const char *session_name
)
1594 int trigger_count
= 0;
1595 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1596 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1597 struct cds_lfht_iter iter
;
1599 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1600 state
->session_triggers_ht
);
1602 /* Add all triggers applying to the session named 'session_name'. */
1603 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1607 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1612 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1613 trigger_ht_element
->trigger
);
1621 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1623 return session_trigger_list
;
1625 lttng_session_trigger_list_destroy(session_trigger_list
);
1630 struct session_info
*find_or_create_session_info(
1631 struct notification_thread_state
*state
,
1632 const char *name
, uid_t uid
, gid_t gid
)
1634 struct session_info
*session
= NULL
;
1635 struct cds_lfht_node
*node
;
1636 struct cds_lfht_iter iter
;
1637 struct lttng_session_trigger_list
*trigger_list
;
1640 cds_lfht_lookup(state
->sessions_ht
,
1641 hash_key_str(name
, lttng_ht_seed
),
1645 node
= cds_lfht_iter_get_node(&iter
);
1647 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1649 session
= caa_container_of(node
, struct session_info
,
1651 assert(session
->uid
== uid
);
1652 assert(session
->gid
== gid
);
1653 session_info_get(session
);
1657 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1658 if (!trigger_list
) {
1662 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1663 state
->sessions_ht
);
1665 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1667 lttng_session_trigger_list_destroy(trigger_list
);
1670 trigger_list
= NULL
;
1672 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1673 &session
->sessions_ht_node
);
1679 session_info_put(session
);
1684 int handle_notification_thread_command_add_channel(
1685 struct notification_thread_state
*state
,
1686 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1687 const char *channel_name
, enum lttng_domain_type channel_domain
,
1688 uint64_t channel_key_int
, uint64_t channel_capacity
,
1689 enum lttng_error_code
*cmd_result
)
1691 struct cds_list_head trigger_list
;
1692 struct channel_info
*new_channel_info
= NULL
;
1693 struct channel_key channel_key
= {
1694 .key
= channel_key_int
,
1695 .domain
= channel_domain
,
1697 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1698 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1699 int trigger_count
= 0;
1700 struct cds_lfht_iter iter
;
1701 struct session_info
*session_info
= NULL
;
1703 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1704 channel_name
, session_name
, channel_key_int
,
1705 lttng_domain_type_str(channel_domain
));
1707 CDS_INIT_LIST_HEAD(&trigger_list
);
1709 session_info
= find_or_create_session_info(state
, session_name
,
1710 session_uid
, session_gid
);
1711 if (!session_info
) {
1712 /* Allocation error or an internal error occurred. */
1716 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1717 channel_capacity
, session_info
);
1718 if (!new_channel_info
) {
1723 /* Build a list of all triggers applying to the new channel. */
1724 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1726 struct lttng_trigger_list_element
*new_element
;
1728 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1729 new_channel_info
)) {
1733 new_element
= zmalloc(sizeof(*new_element
));
1738 CDS_INIT_LIST_HEAD(&new_element
->node
);
1739 new_element
->trigger
= trigger_ht_element
->trigger
;
1740 cds_list_add(&new_element
->node
, &trigger_list
);
1745 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1747 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1748 if (!channel_trigger_list
) {
1751 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1752 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1753 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1754 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1757 /* Add channel to the channel_ht which owns the channel_infos. */
1758 cds_lfht_add(state
->channels_ht
,
1759 hash_channel_key(&new_channel_info
->key
),
1760 &new_channel_info
->channels_ht_node
);
1762 * Add the list of triggers associated with this channel to the
1763 * channel_triggers_ht.
1765 cds_lfht_add(state
->channel_triggers_ht
,
1766 hash_channel_key(&new_channel_info
->key
),
1767 &channel_trigger_list
->channel_triggers_ht_node
);
1769 session_info_put(session_info
);
1770 *cmd_result
= LTTNG_OK
;
1773 channel_info_destroy(new_channel_info
);
1774 session_info_put(session_info
);
1779 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1781 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1786 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1788 free(caa_container_of(node
, struct channel_state_sample
,
1793 int handle_notification_thread_command_remove_channel(
1794 struct notification_thread_state
*state
,
1795 uint64_t channel_key
, enum lttng_domain_type domain
,
1796 enum lttng_error_code
*cmd_result
)
1798 struct cds_lfht_node
*node
;
1799 struct cds_lfht_iter iter
;
1800 struct lttng_channel_trigger_list
*trigger_list
;
1801 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1802 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1803 struct channel_info
*channel_info
;
1805 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1806 channel_key
, lttng_domain_type_str(domain
));
1810 cds_lfht_lookup(state
->channel_triggers_ht
,
1811 hash_channel_key(&key
),
1812 match_channel_trigger_list
,
1815 node
= cds_lfht_iter_get_node(&iter
);
1817 * There is a severe internal error if we are being asked to remove a
1818 * channel that doesn't exist.
1821 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1825 /* Free the list of triggers associated with this channel. */
1826 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1827 channel_triggers_ht_node
);
1828 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1829 &trigger_list
->list
, node
) {
1830 cds_list_del(&trigger_list_element
->node
);
1831 free(trigger_list_element
);
1833 cds_lfht_del(state
->channel_triggers_ht
, node
);
1834 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1836 /* Free sampled channel state. */
1837 cds_lfht_lookup(state
->channel_state_ht
,
1838 hash_channel_key(&key
),
1839 match_channel_state_sample
,
1842 node
= cds_lfht_iter_get_node(&iter
);
1844 * This is expected to be NULL if the channel is destroyed before we
1845 * received a sample.
1848 struct channel_state_sample
*sample
= caa_container_of(node
,
1849 struct channel_state_sample
,
1850 channel_state_ht_node
);
1852 cds_lfht_del(state
->channel_state_ht
, node
);
1853 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1856 /* Remove the channel from the channels_ht and free it. */
1857 cds_lfht_lookup(state
->channels_ht
,
1858 hash_channel_key(&key
),
1862 node
= cds_lfht_iter_get_node(&iter
);
1864 channel_info
= caa_container_of(node
, struct channel_info
,
1866 cds_lfht_del(state
->channels_ht
, node
);
1867 channel_info_destroy(channel_info
);
1870 *cmd_result
= LTTNG_OK
;
1875 int handle_notification_thread_command_session_rotation(
1876 struct notification_thread_state
*state
,
1877 enum notification_thread_command_type cmd_type
,
1878 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1879 uint64_t trace_archive_chunk_id
,
1880 struct lttng_trace_archive_location
*location
,
1881 enum lttng_error_code
*_cmd_result
)
1884 enum lttng_error_code cmd_result
= LTTNG_OK
;
1885 struct lttng_session_trigger_list
*trigger_list
;
1886 struct lttng_trigger_list_element
*trigger_list_element
;
1887 struct session_info
*session_info
;
1888 const struct lttng_credentials session_creds
= {
1889 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1890 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1895 session_info
= find_or_create_session_info(state
, session_name
,
1896 session_uid
, session_gid
);
1897 if (!session_info
) {
1898 /* Allocation error or an internal error occurred. */
1900 cmd_result
= LTTNG_ERR_NOMEM
;
1904 session_info
->rotation
.ongoing
=
1905 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1906 session_info
->rotation
.id
= trace_archive_chunk_id
;
1907 trigger_list
= get_session_trigger_list(state
, session_name
);
1908 if (!trigger_list
) {
1909 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1914 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1916 const struct lttng_condition
*condition
;
1917 struct lttng_trigger
*trigger
;
1918 struct notification_client_list
*client_list
;
1919 struct lttng_evaluation
*evaluation
= NULL
;
1920 enum lttng_condition_type condition_type
;
1921 enum action_executor_status executor_status
;
1923 trigger
= trigger_list_element
->trigger
;
1924 condition
= lttng_trigger_get_const_condition(trigger
);
1926 condition_type
= lttng_condition_get_type(condition
);
1928 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1929 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1931 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1932 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1936 client_list
= get_client_list_from_condition(state
, condition
);
1937 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1938 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1939 trace_archive_chunk_id
);
1941 evaluation
= lttng_evaluation_session_rotation_completed_create(
1942 trace_archive_chunk_id
, location
);
1946 /* Internal error */
1948 cmd_result
= LTTNG_ERR_UNK
;
1953 * Ownership of `evaluation` transferred to the action executor
1954 * no matter the result.
1956 executor_status
= action_executor_enqueue(state
->executor
,
1957 trigger
, evaluation
, &session_creds
,
1960 switch (executor_status
) {
1961 case ACTION_EXECUTOR_STATUS_OK
:
1963 case ACTION_EXECUTOR_STATUS_ERROR
:
1964 case ACTION_EXECUTOR_STATUS_INVALID
:
1966 * TODO Add trigger identification (name/id) when
1967 * it is added to the API.
1969 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1972 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1974 * TODO Add trigger identification (name/id) when
1975 * it is added to the API.
1977 * Not a fatal error.
1979 WARN("No space left when enqueuing action associated with session rotation trigger");
1987 notification_client_list_put(client_list
);
1988 if (caa_unlikely(ret
)) {
1993 session_info_put(session_info
);
1994 *_cmd_result
= cmd_result
;
2000 int handle_notification_thread_command_add_tracer_event_source(
2001 struct notification_thread_state
*state
,
2002 int tracer_event_source_fd
,
2003 enum lttng_domain_type domain_type
,
2004 enum lttng_error_code
*_cmd_result
)
2007 enum lttng_error_code cmd_result
= LTTNG_OK
;
2008 struct notification_event_tracer_event_source_element
*element
= NULL
;
2010 element
= zmalloc(sizeof(*element
));
2012 cmd_result
= LTTNG_ERR_NOMEM
;
2017 element
->fd
= tracer_event_source_fd
;
2018 element
->domain
= domain_type
;
2020 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2022 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2023 tracer_event_source_fd
,
2024 lttng_domain_type_str(domain_type
));
2026 /* Adding the read side pipe to the event poll. */
2027 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
2029 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2030 tracer_event_source_fd
,
2031 lttng_domain_type_str(element
->domain
));
2032 cds_list_del(&element
->node
);
2037 element
->is_fd_in_poll_set
= true;
2040 *_cmd_result
= cmd_result
;
2045 int drain_event_notifier_notification_pipe(
2046 struct notification_thread_state
*state
,
2047 int pipe
, enum lttng_domain_type domain
)
2049 struct lttng_poll_event events
= {0};
2052 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2054 ERR("[notification-thread] Error creating lttng_poll_event");
2058 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2060 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2067 * Continue to consume notifications as long as there are new
2068 * ones coming in. The tracer has been asked to stop producing
2071 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2072 * monitored (on Linux, at least) and will be returned when
2073 * the pipe is closed but empty.
2075 ret
= lttng_poll_wait_interruptible(&events
, 0);
2076 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2077 /* No more notification to be read on this pipe. */
2080 } else if (ret
< 0) {
2081 PERROR("Failed on lttng_poll_wait_interruptible() call");
2086 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2088 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2093 lttng_poll_clean(&events
);
2098 int handle_notification_thread_command_remove_tracer_event_source(
2099 struct notification_thread_state
*state
,
2100 int tracer_event_source_fd
,
2101 enum lttng_error_code
*_cmd_result
)
2105 enum lttng_error_code cmd_result
= LTTNG_OK
;
2106 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
2108 cds_list_for_each_entry_safe(source_element
, tmp
,
2109 &state
->tracer_event_sources_list
, node
) {
2110 if (source_element
->fd
!= tracer_event_source_fd
) {
2114 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2115 tracer_event_source_fd
,
2116 lttng_domain_type_str(source_element
->domain
));
2117 cds_list_del(&source_element
->node
);
2124 * This is temporarily allowed since the poll activity set is
2125 * not properly cleaned-up for the moment. This is adressed in
2128 source_element
= NULL
;
2132 if (!source_element
->is_fd_in_poll_set
) {
2133 /* Skip the poll set removal. */
2137 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2138 tracer_event_source_fd
,
2139 lttng_domain_type_str(source_element
->domain
));
2141 /* Removing the fd from the event poll set. */
2142 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2144 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2145 tracer_event_source_fd
,
2146 lttng_domain_type_str(source_element
->domain
));
2147 cmd_result
= LTTNG_ERR_FATAL
;
2151 source_element
->is_fd_in_poll_set
= false;
2153 ret
= drain_event_notifier_notification_pipe(state
, tracer_event_source_fd
,
2154 source_element
->domain
);
2156 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2157 tracer_event_source_fd
,
2158 lttng_domain_type_str(source_element
->domain
));
2159 cmd_result
= LTTNG_ERR_FATAL
;
2164 * The drain_event_notifier_notification_pipe() call might have read
2165 * data from an fd that we received in event in the latest _poll_wait()
2166 * call. Make sure the thread call poll_wait() again to ensure we have
2169 state
->restart_poll
= true;
2172 free(source_element
);
2173 *_cmd_result
= cmd_result
;
2178 int condition_on_event_update_error_count(struct lttng_trigger
*trigger
)
2181 uint64_t error_count
= 0;
2182 struct lttng_condition
*condition
;
2183 enum event_notifier_error_accounting_status status
;
2185 condition
= lttng_trigger_get_condition(trigger
);
2186 assert(lttng_condition_get_type(condition
) ==
2187 LTTNG_CONDITION_TYPE_ON_EVENT
);
2189 status
= event_notifier_error_accounting_get_count(trigger
, &error_count
);
2190 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2191 uid_t trigger_owner_uid
;
2192 const char *trigger_name
;
2193 const enum lttng_trigger_status trigger_status
=
2194 lttng_trigger_get_owner_uid(
2195 trigger
, &trigger_owner_uid
);
2197 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2198 if (lttng_trigger_get_name(trigger
, &trigger_name
) != LTTNG_TRIGGER_STATUS_OK
) {
2199 trigger_name
= "(unnamed)";
2202 ERR("Failed to get event notifier error count of trigger for update: trigger owner = %d, trigger name = '%s'",
2203 trigger_owner_uid
, trigger_name
);
2207 lttng_condition_on_event_set_error_count(condition
, error_count
);
2211 int handle_notification_thread_remove_tracer_event_source_no_result(
2212 struct notification_thread_state
*state
,
2213 int tracer_event_source_fd
)
2216 enum lttng_error_code cmd_result
;
2218 ret
= handle_notification_thread_command_remove_tracer_event_source(
2219 state
, tracer_event_source_fd
, &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 if (lttng_trigger_needs_tracer_notifier(trigger_ht_element
->trigger
)) {
2259 ret
= condition_on_event_update_error_count(
2260 trigger_ht_element
->trigger
);
2264 ret
= lttng_triggers_add(local_triggers
,
2265 trigger_ht_element
->trigger
);
2267 /* Not a fatal error. */
2269 cmd_result
= LTTNG_ERR_NOMEM
;
2274 /* Transferring ownership to the caller. */
2275 *triggers
= local_triggers
;
2276 local_triggers
= NULL
;
2280 lttng_triggers_destroy(local_triggers
);
2281 *_cmd_result
= cmd_result
;
2285 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
2286 const char **trigger_name
,
2287 uid_t
*trigger_owner_uid
)
2289 enum lttng_trigger_status trigger_status
;
2291 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
2292 switch (trigger_status
) {
2293 case LTTNG_TRIGGER_STATUS_OK
:
2295 case LTTNG_TRIGGER_STATUS_UNSET
:
2296 *trigger_name
= "(unset)";
2302 trigger_status
= lttng_trigger_get_owner_uid(trigger
,
2304 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2307 static int handle_notification_thread_command_get_trigger(
2308 struct notification_thread_state
*state
,
2309 const struct lttng_trigger
*trigger
,
2310 struct lttng_trigger
**registered_trigger
,
2311 enum lttng_error_code
*_cmd_result
)
2314 struct cds_lfht_iter iter
;
2315 struct lttng_trigger_ht_element
*trigger_ht_element
;
2316 enum lttng_error_code cmd_result
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2317 const char *trigger_name
;
2318 uid_t trigger_owner_uid
;
2322 cds_lfht_for_each_entry(
2323 state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2324 if (lttng_trigger_is_equal(
2325 trigger
, trigger_ht_element
->trigger
)) {
2326 /* Take one reference on the return trigger. */
2327 *registered_trigger
= trigger_ht_element
->trigger
;
2328 lttng_trigger_get(*registered_trigger
);
2330 cmd_result
= LTTNG_OK
;
2335 /* Not a fatal error if the trigger is not found. */
2336 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
2337 ERR("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d",
2338 trigger_name
, (int) trigger_owner_uid
);
2344 *_cmd_result
= cmd_result
;
2349 bool condition_is_supported(struct lttng_condition
*condition
)
2353 switch (lttng_condition_get_type(condition
)) {
2354 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2355 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2358 enum lttng_domain_type domain
;
2360 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2364 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2365 is_supported
= true;
2370 * Older kernel tracers don't expose the API to monitor their
2371 * buffers. Therefore, we reject triggers that require that
2372 * mechanism to be available to be evaluated.
2374 * Assume unsupported on error.
2376 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2379 case LTTNG_CONDITION_TYPE_ON_EVENT
:
2381 const struct lttng_event_rule
*event_rule
;
2382 enum lttng_domain_type domain
;
2383 const enum lttng_condition_status status
=
2384 lttng_condition_on_event_get_rule(
2385 condition
, &event_rule
);
2387 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2389 domain
= lttng_event_rule_get_domain_type(event_rule
);
2390 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2391 is_supported
= true;
2396 * Older kernel tracers can't emit notification. Therefore, we
2397 * reject triggers that require that mechanism to be available
2400 * Assume unsupported on error.
2402 is_supported
= kernel_supports_event_notifiers() == 1;
2406 is_supported
= true;
2409 return is_supported
;
2412 /* Must be called with RCU read lock held. */
2414 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2415 struct notification_thread_state
*state
)
2418 const struct lttng_condition
*condition
;
2419 const char *session_name
;
2420 struct lttng_session_trigger_list
*trigger_list
;
2422 condition
= lttng_trigger_get_const_condition(trigger
);
2423 switch (lttng_condition_get_type(condition
)) {
2424 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2425 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2427 enum lttng_condition_status status
;
2429 status
= lttng_condition_session_rotation_get_session_name(
2430 condition
, &session_name
);
2431 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2432 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2443 trigger_list
= get_session_trigger_list(state
, session_name
);
2444 if (!trigger_list
) {
2445 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2451 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2453 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2458 /* Must be called with RCU read lock held. */
2460 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2461 struct notification_thread_state
*state
)
2464 struct cds_lfht_node
*node
;
2465 struct cds_lfht_iter iter
;
2466 struct channel_info
*channel
;
2468 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2470 struct lttng_trigger_list_element
*trigger_list_element
;
2471 struct lttng_channel_trigger_list
*trigger_list
;
2472 struct cds_lfht_iter lookup_iter
;
2474 if (!trigger_applies_to_channel(trigger
, channel
)) {
2478 cds_lfht_lookup(state
->channel_triggers_ht
,
2479 hash_channel_key(&channel
->key
),
2480 match_channel_trigger_list
,
2483 node
= cds_lfht_iter_get_node(&lookup_iter
);
2485 trigger_list
= caa_container_of(node
,
2486 struct lttng_channel_trigger_list
,
2487 channel_triggers_ht_node
);
2489 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2490 if (!trigger_list_element
) {
2494 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2495 trigger_list_element
->trigger
= trigger
;
2496 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2497 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2505 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2507 bool is_notify
= false;
2508 unsigned int i
, count
;
2509 enum lttng_action_status action_status
;
2510 const struct lttng_action
*action
=
2511 lttng_trigger_get_const_action(trigger
);
2512 enum lttng_action_type action_type
;
2515 action_type
= lttng_action_get_type(action
);
2516 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2519 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2523 action_status
= lttng_action_group_get_count(action
, &count
);
2524 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2526 for (i
= 0; i
< count
; i
++) {
2527 const struct lttng_action
*inner_action
=
2528 lttng_action_group_get_at_index(
2531 action_type
= lttng_action_get_type(inner_action
);
2532 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2542 static bool trigger_name_taken(struct notification_thread_state
*state
,
2543 const struct lttng_trigger
*trigger
)
2545 struct cds_lfht_iter iter
;
2548 * No duplicata is allowed in the triggers_by_name_uid_ht.
2549 * The match is done against the trigger name and uid.
2551 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2552 hash_trigger_by_name_uid(trigger
),
2553 match_trigger_by_name_uid
,
2556 return !!cds_lfht_iter_get_node(&iter
);
2560 enum lttng_error_code
generate_trigger_name(
2561 struct notification_thread_state
*state
,
2562 struct lttng_trigger
*trigger
, const char **name
)
2564 enum lttng_error_code ret_code
= LTTNG_OK
;
2566 enum lttng_trigger_status status
;
2569 const int ret
= lttng_trigger_generate_name(trigger
,
2570 state
->trigger_id
.name_offset
++);
2572 /* The only reason this can fail right now. */
2573 ret_code
= LTTNG_ERR_NOMEM
;
2577 status
= lttng_trigger_get_name(trigger
, name
);
2578 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2580 taken
= trigger_name_taken(state
, trigger
);
2581 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2587 void notif_thread_state_remove_trigger_ht_elem(
2588 struct notification_thread_state
*state
,
2589 struct lttng_trigger_ht_element
*trigger_ht_element
)
2592 assert(trigger_ht_element
);
2594 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2595 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2599 enum lttng_error_code
setup_tracer_notifier(
2600 struct notification_thread_state
*state
,
2601 struct lttng_trigger
*trigger
)
2603 enum lttng_error_code ret
;
2604 enum event_notifier_error_accounting_status error_accounting_status
;
2605 struct cds_lfht_node
*node
;
2606 uint64_t error_counter_index
= 0;
2607 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2608 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2610 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2611 if (!trigger_tokens_ht_element
) {
2612 ret
= LTTNG_ERR_NOMEM
;
2616 /* Add trigger token to the trigger_tokens_ht. */
2617 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2618 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2619 trigger_tokens_ht_element
->trigger
= trigger
;
2621 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2622 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2623 match_trigger_token
,
2624 &trigger_tokens_ht_element
->token
,
2625 &trigger_tokens_ht_element
->node
);
2626 if (node
!= &trigger_tokens_ht_element
->node
) {
2627 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2628 goto error_free_ht_element
;
2631 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2632 trigger
, &error_counter_index
);
2633 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2634 if (error_accounting_status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2635 DBG("Trigger group error accounting counter full.");
2636 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2638 ERR("Error registering trigger for error accounting");
2639 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2642 goto error_remove_ht_element
;
2645 lttng_condition_on_event_set_error_counter_index(
2646 condition
, error_counter_index
);
2651 error_remove_ht_element
:
2652 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2653 error_free_ht_element
:
2654 free(trigger_tokens_ht_element
);
2660 * FIXME A client's credentials are not checked when registering a trigger.
2662 * The effects of this are benign since:
2663 * - The client will succeed in registering the trigger, as it is valid,
2664 * - The trigger will, internally, be bound to the channel/session,
2665 * - The notifications will not be sent since the client's credentials
2666 * are checked against the channel at that moment.
2668 * If this function returns a non-zero value, it means something is
2669 * fundamentally broken and the whole subsystem/thread will be torn down.
2671 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2675 int handle_notification_thread_command_register_trigger(
2676 struct notification_thread_state
*state
,
2677 struct lttng_trigger
*trigger
,
2678 enum lttng_error_code
*cmd_result
)
2681 struct lttng_condition
*condition
;
2682 struct notification_client_list
*client_list
= NULL
;
2683 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2684 struct cds_lfht_node
*node
;
2685 const char* trigger_name
;
2686 bool free_trigger
= true;
2687 struct lttng_evaluation
*evaluation
= NULL
;
2688 struct lttng_credentials object_creds
;
2691 enum action_executor_status executor_status
;
2692 const uint64_t trigger_tracer_token
=
2693 state
->trigger_id
.next_tracer_token
++;
2697 /* Set the trigger's tracer token. */
2698 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2700 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2701 LTTNG_TRIGGER_STATUS_UNSET
) {
2702 const enum lttng_error_code ret_code
= generate_trigger_name(
2703 state
, trigger
, &trigger_name
);
2705 if (ret_code
!= LTTNG_OK
) {
2708 *cmd_result
= ret_code
;
2711 } else if (trigger_name_taken(state
, trigger
)) {
2712 /* Not a fatal error. */
2713 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2718 condition
= lttng_trigger_get_condition(trigger
);
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 /* Not a fatal error, simply report it to the client. */
2756 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2757 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2758 goto error_free_ht_element
;
2761 /* From this point consider the trigger registered. */
2762 lttng_trigger_set_as_registered(trigger
);
2765 * Some triggers might need a tracer notifier depending on its
2766 * condition and actions.
2768 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2769 enum lttng_error_code error_code
;
2771 error_code
= setup_tracer_notifier(state
, trigger
);
2772 if (error_code
!= LTTNG_OK
) {
2773 notif_thread_state_remove_trigger_ht_elem(state
,
2774 trigger_ht_element
);
2775 if (error_code
== LTTNG_ERR_NOMEM
) {
2778 *cmd_result
= error_code
;
2782 goto error_free_ht_element
;
2787 * The rest only applies to triggers that have a "notify" action.
2788 * It is not skipped as this is the only action type currently
2791 if (is_trigger_action_notify(trigger
)) {
2793 * Find or create the client list of this condition. It may
2794 * already be present if another trigger is already registered
2795 * with the same condition.
2797 client_list
= get_client_list_from_condition(state
, condition
);
2800 * No client list for this condition yet. We create new
2801 * one and build it up.
2803 client_list
= notification_client_list_create(state
, condition
);
2805 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2806 goto error_free_ht_element
;
2810 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2812 pthread_mutex_lock(&client_list
->lock
);
2813 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2814 pthread_mutex_unlock(&client_list
->lock
);
2818 * Ownership of the trigger and of its wrapper was transfered to
2819 * the triggers_ht. Same for token ht element if necessary.
2821 trigger_ht_element
= NULL
;
2822 free_trigger
= false;
2824 switch (get_condition_binding_object(condition
)) {
2825 case LTTNG_OBJECT_TYPE_SESSION
:
2826 /* Add the trigger to the list if it matches a known session. */
2827 ret
= bind_trigger_to_matching_session(trigger
, state
);
2829 goto error_free_ht_element
;
2832 case LTTNG_OBJECT_TYPE_CHANNEL
:
2834 * Add the trigger to list of triggers bound to the channels
2837 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2839 goto error_free_ht_element
;
2842 case LTTNG_OBJECT_TYPE_NONE
:
2845 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2847 goto error_free_ht_element
;
2851 * The new trigger's condition must be evaluated against the current
2854 * In the case of `notify` action, nothing preventing clients from
2855 * subscribing to a condition before the corresponding trigger is
2856 * registered, we have to evaluate this new condition right away.
2858 * At some point, we were waiting for the next "evaluation" (e.g. on
2859 * reception of a channel sample) to evaluate this new condition, but
2862 * The reason it was broken is that waiting for the next sample
2863 * does not allow us to properly handle transitions for edge-triggered
2866 * Consider this example: when we handle a new channel sample, we
2867 * evaluate each conditions twice: once with the previous state, and
2868 * again with the newest state. We then use those two results to
2869 * determine whether a state change happened: a condition was false and
2870 * became true. If a state change happened, we have to notify clients.
2872 * Now, if a client subscribes to a given notification and registers
2873 * a trigger *after* that subscription, we have to make sure the
2874 * condition is evaluated at this point while considering only the
2875 * current state. Otherwise, the next evaluation cycle may only see
2876 * that the evaluations remain the same (true for samples n-1 and n) and
2877 * the client will never know that the condition has been met.
2879 switch (get_condition_binding_object(condition
)) {
2880 case LTTNG_OBJECT_TYPE_SESSION
:
2881 ret
= evaluate_session_condition_for_client(condition
, state
,
2882 &evaluation
, &object_uid
,
2884 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2885 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2887 case LTTNG_OBJECT_TYPE_CHANNEL
:
2888 ret
= evaluate_channel_condition_for_client(condition
, state
,
2889 &evaluation
, &object_uid
,
2891 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2892 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2894 case LTTNG_OBJECT_TYPE_NONE
:
2897 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2905 goto error_free_ht_element
;
2908 DBG("Newly registered trigger's condition evaluated to %s",
2909 evaluation
? "true" : "false");
2911 /* Evaluation yielded nothing. Normal exit. */
2917 * Ownership of `evaluation` transferred to the action executor
2918 * no matter the result.
2920 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2921 evaluation
, &object_creds
, client_list
);
2923 switch (executor_status
) {
2924 case ACTION_EXECUTOR_STATUS_OK
:
2926 case ACTION_EXECUTOR_STATUS_ERROR
:
2927 case ACTION_EXECUTOR_STATUS_INVALID
:
2929 * TODO Add trigger identification (name/id) when
2930 * it is added to the API.
2932 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2934 goto error_free_ht_element
;
2935 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2937 * TODO Add trigger identification (name/id) when
2938 * it is added to the API.
2940 * Not a fatal error.
2942 WARN("No space left when enqueuing action associated to newly registered trigger");
2950 *cmd_result
= LTTNG_OK
;
2951 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2952 trigger_name
, trigger_tracer_token
);
2955 error_free_ht_element
:
2956 if (trigger_ht_element
) {
2957 /* Delayed removal due to RCU constraint on delete. */
2958 call_rcu(&trigger_ht_element
->rcu_node
,
2959 free_lttng_trigger_ht_element_rcu
);
2964 * Other objects might have a reference to the trigger, mark it
2967 lttng_trigger_set_as_unregistered(trigger
);
2968 lttng_trigger_destroy(trigger
);
2976 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2978 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2983 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2985 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2990 void teardown_tracer_notifier(struct notification_thread_state
*state
,
2991 const struct lttng_trigger
*trigger
)
2993 struct cds_lfht_iter iter
;
2994 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2996 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
2997 trigger_tokens_ht_element
, node
) {
2999 if (!lttng_trigger_is_equal(trigger
,
3000 trigger_tokens_ht_element
->trigger
)) {
3004 event_notifier_error_accounting_unregister_event_notifier(
3005 trigger_tokens_ht_element
->trigger
);
3007 /* TODO talk to all app and remove it */
3008 DBG("[notification-thread] Removed trigger from tokens_ht");
3009 cds_lfht_del(state
->trigger_tokens_ht
,
3010 &trigger_tokens_ht_element
->node
);
3012 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
3013 free_notification_trigger_tokens_ht_element_rcu
);
3020 int handle_notification_thread_command_unregister_trigger(
3021 struct notification_thread_state
*state
,
3022 const struct lttng_trigger
*trigger
,
3023 enum lttng_error_code
*_cmd_reply
)
3025 struct cds_lfht_iter iter
;
3026 struct cds_lfht_node
*triggers_ht_node
;
3027 struct lttng_channel_trigger_list
*trigger_list
;
3028 struct notification_client_list
*client_list
;
3029 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
3030 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
3032 enum lttng_error_code cmd_reply
;
3036 cds_lfht_lookup(state
->triggers_ht
,
3037 lttng_condition_hash(condition
),
3041 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3042 if (!triggers_ht_node
) {
3043 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3046 cmd_reply
= LTTNG_OK
;
3049 trigger_ht_element
= caa_container_of(triggers_ht_node
,
3050 struct lttng_trigger_ht_element
, node
);
3052 /* From this point, consider the trigger unregistered no matter what. */
3053 lttng_trigger_set_as_unregistered(trigger_ht_element
->trigger
);
3055 /* Remove trigger from channel_triggers_ht. */
3056 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
3057 channel_triggers_ht_node
) {
3058 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3060 cds_list_for_each_entry_safe(trigger_element
, tmp
,
3061 &trigger_list
->list
, node
) {
3062 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3066 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
3067 cds_list_del(&trigger_element
->node
);
3068 /* A trigger can only appear once per channel */
3073 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3074 teardown_tracer_notifier(state
, trigger
);
3077 if (is_trigger_action_notify(trigger
)) {
3079 * Remove and release the client list from
3080 * notification_trigger_clients_ht.
3082 client_list
= get_client_list_from_condition(state
, condition
);
3083 assert(client_list
);
3085 pthread_mutex_lock(&client_list
->lock
);
3086 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3087 pthread_mutex_unlock(&client_list
->lock
);
3089 /* Put new reference and the hashtable's reference. */
3090 notification_client_list_put(client_list
);
3091 notification_client_list_put(client_list
);
3095 /* Remove trigger from triggers_ht. */
3096 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3098 /* Release the ownership of the trigger. */
3099 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3100 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3104 *_cmd_reply
= cmd_reply
;
3109 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3110 int handle_notification_thread_command(
3111 struct notification_thread_handle
*handle
,
3112 struct notification_thread_state
*state
)
3116 struct notification_thread_command
*cmd
;
3118 /* Read the event pipe to put it back into a quiescent state. */
3119 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
3121 if (ret
!= sizeof(counter
)) {
3125 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3126 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3127 struct notification_thread_command
, cmd_list_node
);
3128 cds_list_del(&cmd
->cmd_list_node
);
3129 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3131 DBG("[notification-thread] Received `%s` command",
3132 notification_command_type_str(cmd
->type
));
3133 switch (cmd
->type
) {
3134 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3135 ret
= handle_notification_thread_command_register_trigger(state
,
3136 cmd
->parameters
.register_trigger
.trigger
,
3139 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3140 ret
= handle_notification_thread_command_unregister_trigger(
3142 cmd
->parameters
.unregister_trigger
.trigger
,
3145 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3146 ret
= handle_notification_thread_command_add_channel(
3148 cmd
->parameters
.add_channel
.session
.name
,
3149 cmd
->parameters
.add_channel
.session
.uid
,
3150 cmd
->parameters
.add_channel
.session
.gid
,
3151 cmd
->parameters
.add_channel
.channel
.name
,
3152 cmd
->parameters
.add_channel
.channel
.domain
,
3153 cmd
->parameters
.add_channel
.channel
.key
,
3154 cmd
->parameters
.add_channel
.channel
.capacity
,
3157 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3158 ret
= handle_notification_thread_command_remove_channel(
3159 state
, cmd
->parameters
.remove_channel
.key
,
3160 cmd
->parameters
.remove_channel
.domain
,
3163 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3164 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3165 ret
= handle_notification_thread_command_session_rotation(
3168 cmd
->parameters
.session_rotation
.session_name
,
3169 cmd
->parameters
.session_rotation
.uid
,
3170 cmd
->parameters
.session_rotation
.gid
,
3171 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3172 cmd
->parameters
.session_rotation
.location
,
3175 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3176 ret
= handle_notification_thread_command_add_tracer_event_source(
3178 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3179 cmd
->parameters
.tracer_event_source
.domain
,
3182 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3183 ret
= handle_notification_thread_command_remove_tracer_event_source(
3185 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3188 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3190 struct lttng_triggers
*triggers
= NULL
;
3192 ret
= handle_notification_thread_command_list_triggers(
3195 cmd
->parameters
.list_triggers
.uid
,
3198 cmd
->reply
.list_triggers
.triggers
= triggers
;
3202 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3203 cmd
->reply_code
= LTTNG_OK
;
3206 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3208 struct lttng_trigger
*trigger
= NULL
;
3210 ret
= handle_notification_thread_command_get_trigger(state
,
3211 cmd
->parameters
.get_trigger
.trigger
, &trigger
,
3213 cmd
->reply
.get_trigger
.trigger
= trigger
;
3216 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3218 const enum client_transmission_status client_status
=
3219 cmd
->parameters
.client_communication_update
3221 const notification_client_id client_id
=
3222 cmd
->parameters
.client_communication_update
.id
;
3223 struct notification_client
*client
;
3226 client
= get_client_from_id(client_id
, state
);
3230 * Client error was probably already picked-up by the
3231 * notification thread or it has disconnected
3232 * gracefully while this command was queued.
3234 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3238 ret
= client_handle_transmission_status(
3239 client
, client_status
, state
);
3245 ERR("[notification-thread] Unknown internal command received");
3253 if (cmd
->is_async
) {
3257 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3261 /* Wake-up and return a fatal error to the calling thread. */
3262 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3263 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3265 /* Indicate a fatal error to the caller. */
3270 int socket_set_non_blocking(int socket
)
3274 /* Set the pipe as non-blocking. */
3275 ret
= fcntl(socket
, F_GETFL
, 0);
3277 PERROR("fcntl get socket flags");
3282 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3284 PERROR("fcntl set O_NONBLOCK socket flag");
3287 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3293 int client_reset_inbound_state(struct notification_client
*client
)
3298 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3300 client
->communication
.inbound
.bytes_to_receive
=
3301 sizeof(struct lttng_notification_channel_message
);
3302 client
->communication
.inbound
.msg_type
=
3303 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3304 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3305 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3306 ret
= lttng_dynamic_buffer_set_size(
3307 &client
->communication
.inbound
.payload
.buffer
,
3308 client
->communication
.inbound
.bytes_to_receive
);
3313 int handle_notification_thread_client_connect(
3314 struct notification_thread_state
*state
)
3317 struct notification_client
*client
;
3319 DBG("[notification-thread] Handling new notification channel client connection");
3321 client
= zmalloc(sizeof(*client
));
3328 pthread_mutex_init(&client
->lock
, NULL
);
3329 client
->id
= state
->next_notification_client_id
++;
3330 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3331 lttng_payload_init(&client
->communication
.inbound
.payload
);
3332 lttng_payload_init(&client
->communication
.outbound
.payload
);
3333 client
->communication
.inbound
.expect_creds
= true;
3335 ret
= client_reset_inbound_state(client
);
3337 ERR("[notification-thread] Failed to reset client communication's inbound state");
3342 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3344 ERR("[notification-thread] Failed to accept new notification channel client connection");
3349 client
->socket
= ret
;
3351 ret
= socket_set_non_blocking(client
->socket
);
3353 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3357 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3359 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3364 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3365 LPOLLIN
| LPOLLERR
|
3366 LPOLLHUP
| LPOLLRDHUP
);
3368 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3372 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3376 cds_lfht_add(state
->client_socket_ht
,
3377 hash_client_socket(client
->socket
),
3378 &client
->client_socket_ht_node
);
3379 cds_lfht_add(state
->client_id_ht
,
3380 hash_client_id(client
->id
),
3381 &client
->client_id_ht_node
);
3387 notification_client_destroy(client
, state
);
3392 * RCU read-lock must be held by the caller.
3393 * Client lock must _not_ be held by the caller.
3396 int notification_thread_client_disconnect(
3397 struct notification_client
*client
,
3398 struct notification_thread_state
*state
)
3401 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3403 /* Acquire the client lock to disable its communication atomically. */
3404 pthread_mutex_lock(&client
->lock
);
3405 client
->communication
.active
= false;
3406 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3407 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3408 pthread_mutex_unlock(&client
->lock
);
3410 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3412 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3416 /* Release all conditions to which the client was subscribed. */
3417 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3418 &client
->condition_list
, node
) {
3419 (void) notification_thread_client_unsubscribe(client
,
3420 condition_list_element
->condition
, state
, NULL
);
3424 * Client no longer accessible to other threads (through the
3427 notification_client_destroy(client
, state
);
3431 int handle_notification_thread_client_disconnect(
3432 int client_socket
, struct notification_thread_state
*state
)
3435 struct notification_client
*client
;
3438 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3440 client
= get_client_from_socket(client_socket
, state
);
3442 /* Internal state corruption, fatal error. */
3443 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3449 ret
= notification_thread_client_disconnect(client
, state
);
3455 int handle_notification_thread_client_disconnect_all(
3456 struct notification_thread_state
*state
)
3458 struct cds_lfht_iter iter
;
3459 struct notification_client
*client
;
3460 bool error_encoutered
= false;
3463 DBG("[notification-thread] Closing all client connections");
3464 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3465 client_socket_ht_node
) {
3468 ret
= notification_thread_client_disconnect(
3471 error_encoutered
= true;
3475 return error_encoutered
? 1 : 0;
3478 int handle_notification_thread_trigger_unregister_all(
3479 struct notification_thread_state
*state
)
3481 bool error_occurred
= false;
3482 struct cds_lfht_iter iter
;
3483 struct lttng_trigger_ht_element
*trigger_ht_element
;
3486 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3488 int ret
= handle_notification_thread_command_unregister_trigger(
3489 state
, trigger_ht_element
->trigger
, NULL
);
3491 error_occurred
= true;
3495 return error_occurred
? -1 : 0;
3499 int client_handle_transmission_status(
3500 struct notification_client
*client
,
3501 enum client_transmission_status transmission_status
,
3502 struct notification_thread_state
*state
)
3506 switch (transmission_status
) {
3507 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3508 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3509 CLIENT_POLL_MASK_IN
);
3515 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3517 * We want to be notified whenever there is buffer space
3518 * available to send the rest of the payload.
3520 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3521 CLIENT_POLL_MASK_IN_OUT
);
3526 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3527 ret
= notification_thread_client_disconnect(client
, state
);
3532 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3542 /* Client lock must be acquired by caller. */
3544 enum client_transmission_status
client_flush_outgoing_queue(
3545 struct notification_client
*client
)
3548 size_t to_send_count
;
3549 enum client_transmission_status status
;
3550 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3551 &client
->communication
.outbound
.payload
, 0, -1);
3552 const int fds_to_send_count
=
3553 lttng_payload_view_get_fd_handle_count(&pv
);
3555 ASSERT_LOCKED(client
->lock
);
3557 if (!client
->communication
.active
) {
3558 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3562 if (pv
.buffer
.size
== 0) {
3564 * If both data and fds are equal to zero, we are in an invalid
3567 assert(fds_to_send_count
!= 0);
3572 to_send_count
= pv
.buffer
.size
;
3573 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3576 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3579 if ((ret
>= 0 && ret
< to_send_count
)) {
3580 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3582 to_send_count
-= max(ret
, 0);
3584 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3586 pv
.buffer
.size
- to_send_count
,
3588 ret
= lttng_dynamic_buffer_set_size(
3589 &client
->communication
.outbound
.payload
.buffer
,
3595 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3597 } else if (ret
< 0) {
3598 /* Generic error, disable the client's communication. */
3599 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3601 client
->communication
.active
= false;
3602 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3606 * No error and flushed the queue completely.
3608 * The payload buffer size is used later to
3609 * check if there is notifications queued. So albeit that the
3610 * direct caller knows that the transmission is complete, we
3611 * need to set the buffer size to zero.
3613 ret
= lttng_dynamic_buffer_set_size(
3614 &client
->communication
.outbound
.payload
.buffer
, 0);
3621 /* No fds to send, transmission is complete. */
3622 if (fds_to_send_count
== 0) {
3623 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3627 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3628 client
->socket
, &pv
);
3630 /* Generic error, disable the client's communication. */
3631 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3633 client
->communication
.active
= false;
3634 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3636 } else if (ret
== 0) {
3637 /* Nothing could be sent. */
3638 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3640 /* Fd passing is an all or nothing kind of thing. */
3641 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3643 * The payload _fd_array count is used later to
3644 * check if there is notifications queued. So although the
3645 * direct caller knows that the transmission is complete, we
3646 * need to clear the _fd_array for the queuing check.
3648 lttng_dynamic_pointer_array_clear(
3649 &client
->communication
.outbound
.payload
3654 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3655 client
->communication
.outbound
.queued_command_reply
= false;
3656 client
->communication
.outbound
.dropped_notification
= false;
3657 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3662 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3666 bool client_has_outbound_data_left(
3667 const struct notification_client
*client
)
3669 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3670 &client
->communication
.outbound
.payload
, 0, -1);
3671 const bool has_data
= pv
.buffer
.size
!= 0;
3672 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3674 return has_data
|| has_fds
;
3677 /* Client lock must _not_ be held by the caller. */
3679 int client_send_command_reply(struct notification_client
*client
,
3680 struct notification_thread_state
*state
,
3681 enum lttng_notification_channel_status status
)
3684 struct lttng_notification_channel_command_reply reply
= {
3685 .status
= (int8_t) status
,
3687 struct lttng_notification_channel_message msg
= {
3688 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3689 .size
= sizeof(reply
),
3691 char buffer
[sizeof(msg
) + sizeof(reply
)];
3692 enum client_transmission_status transmission_status
;
3694 memcpy(buffer
, &msg
, sizeof(msg
));
3695 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3696 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3698 pthread_mutex_lock(&client
->lock
);
3699 if (client
->communication
.outbound
.queued_command_reply
) {
3700 /* Protocol error. */
3704 /* Enqueue buffer to outgoing queue and flush it. */
3705 ret
= lttng_dynamic_buffer_append(
3706 &client
->communication
.outbound
.payload
.buffer
,
3707 buffer
, sizeof(buffer
));
3712 transmission_status
= client_flush_outgoing_queue(client
);
3714 if (client_has_outbound_data_left(client
)) {
3715 /* Queue could not be emptied. */
3716 client
->communication
.outbound
.queued_command_reply
= true;
3719 pthread_mutex_unlock(&client
->lock
);
3720 ret
= client_handle_transmission_status(
3721 client
, transmission_status
, state
);
3728 pthread_mutex_unlock(&client
->lock
);
3734 int client_handle_message_unknown(struct notification_client
*client
,
3735 struct notification_thread_state
*state
)
3739 * Receiving message header. The function will be called again
3740 * once the rest of the message as been received and can be
3743 const struct lttng_notification_channel_message
*msg
;
3745 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3746 msg
= (const struct lttng_notification_channel_message
*)
3747 client
->communication
.inbound
.payload
.buffer
.data
;
3749 if (msg
->size
== 0 ||
3750 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3751 ERR("[notification-thread] Invalid notification channel message: length = %u",
3757 switch (msg
->type
) {
3758 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3759 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3760 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3764 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3768 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3769 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3770 client
->communication
.inbound
.msg_type
=
3771 (enum lttng_notification_channel_message_type
) msg
->type
;
3772 ret
= lttng_dynamic_buffer_set_size(
3773 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3775 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3782 int client_handle_message_handshake(struct notification_client
*client
,
3783 struct notification_thread_state
*state
)
3786 struct lttng_notification_channel_command_handshake
*handshake_client
;
3787 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3788 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3789 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3791 const struct lttng_notification_channel_message msg_header
= {
3792 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3793 .size
= sizeof(handshake_reply
),
3795 enum lttng_notification_channel_status status
=
3796 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3797 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3799 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3800 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3801 sizeof(handshake_reply
));
3804 (struct lttng_notification_channel_command_handshake
*)
3805 client
->communication
.inbound
.payload
.buffer
3807 client
->major
= handshake_client
->major
;
3808 client
->minor
= handshake_client
->minor
;
3809 if (!client
->communication
.inbound
.creds_received
) {
3810 ERR("[notification-thread] No credentials received from client");
3815 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3816 &client
->communication
.inbound
.creds
);
3817 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3818 &client
->communication
.inbound
.creds
);
3819 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3820 client
->uid
, client
->gid
, (int) client
->major
,
3821 (int) client
->minor
);
3823 if (handshake_client
->major
!=
3824 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3825 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3828 pthread_mutex_lock(&client
->lock
);
3829 /* Outgoing queue will be flushed when the command reply is sent. */
3830 ret
= lttng_dynamic_buffer_append(
3831 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3832 sizeof(send_buffer
));
3834 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3838 client
->validated
= true;
3839 client
->communication
.active
= true;
3840 pthread_mutex_unlock(&client
->lock
);
3842 /* Set reception state to receive the next message header. */
3843 ret
= client_reset_inbound_state(client
);
3845 ERR("[notification-thread] Failed to reset client communication's inbound state");
3849 /* Flushes the outgoing queue. */
3850 ret
= client_send_command_reply(client
, state
, status
);
3852 ERR("[notification-thread] Failed to send reply to notification channel client");
3858 pthread_mutex_unlock(&client
->lock
);
3864 int client_handle_message_subscription(
3865 struct notification_client
*client
,
3866 enum lttng_notification_channel_message_type msg_type
,
3867 struct notification_thread_state
*state
)
3870 struct lttng_condition
*condition
;
3871 enum lttng_notification_channel_status status
=
3872 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3873 struct lttng_payload_view condition_view
=
3874 lttng_payload_view_from_payload(
3875 &client
->communication
.inbound
.payload
,
3877 size_t expected_condition_size
;
3880 * No need to lock client to sample the inbound state as the only
3881 * other thread accessing clients (action executor) only uses the
3884 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3885 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3886 if (ret
!= expected_condition_size
) {
3887 ERR("[notification-thread] Malformed condition received from client");
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("[notification-thread] Failed to reset client communication's inbound state");
3910 ret
= client_send_command_reply(client
, state
, status
);
3912 ERR("[notification-thread] 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("[notification-thread] 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 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3979 client
->communication
.inbound
.bytes_to_receive
;
3980 if (client
->communication
.inbound
.expect_creds
) {
3981 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3982 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3983 client
->communication
.inbound
.bytes_to_receive
,
3984 &client
->communication
.inbound
.creds
);
3986 client
->communication
.inbound
.expect_creds
= false;
3987 client
->communication
.inbound
.creds_received
= true;
3990 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3991 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3992 client
->communication
.inbound
.bytes_to_receive
);
3994 if (recv_ret
>= 0) {
3995 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3997 goto error_disconnect_client
;
4000 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
4001 /* Message incomplete wait for more data. */
4006 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
4009 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
4010 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
4012 client
->communication
.inbound
.fds_to_receive
,
4013 &client
->communication
.inbound
.payload
);
4016 * Fds received. non blocking fds passing is all
4019 ssize_t expected_size
;
4021 expected_size
= sizeof(int) *
4022 client
->communication
.inbound
4024 assert(ret
== expected_size
);
4025 client
->communication
.inbound
.fds_to_receive
= 0;
4026 } else if (ret
== 0) {
4027 /* Received nothing. */
4031 goto error_disconnect_client
;
4035 /* At this point the message is complete.*/
4036 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4037 client
->communication
.inbound
.fds_to_receive
== 0);
4038 ret
= client_dispatch_message(client
, state
);
4041 * Only returns an error if this client must be
4044 goto error_disconnect_client
;
4051 error_disconnect_client
:
4052 ret
= notification_thread_client_disconnect(client
, state
);
4056 /* Client ready to receive outgoing data. */
4057 int handle_notification_thread_client_out(
4058 struct notification_thread_state
*state
, int socket
)
4061 struct notification_client
*client
;
4062 enum client_transmission_status transmission_status
;
4065 client
= get_client_from_socket(socket
, state
);
4067 /* Internal error, abort. */
4072 pthread_mutex_lock(&client
->lock
);
4073 transmission_status
= client_flush_outgoing_queue(client
);
4074 pthread_mutex_unlock(&client
->lock
);
4076 ret
= client_handle_transmission_status(
4077 client
, transmission_status
, state
);
4087 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4088 const struct channel_state_sample
*sample
,
4089 uint64_t buffer_capacity
)
4091 bool result
= false;
4093 enum lttng_condition_type condition_type
;
4094 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4095 condition
, struct lttng_condition_buffer_usage
,
4098 if (use_condition
->threshold_bytes
.set
) {
4099 threshold
= use_condition
->threshold_bytes
.value
;
4102 * Threshold was expressed as a ratio.
4104 * TODO the threshold (in bytes) of conditions expressed
4105 * as a ratio of total buffer size could be cached to
4106 * forego this double-multiplication or it could be performed
4107 * as fixed-point math.
4109 * Note that caching should accommodates the case where the
4110 * condition applies to multiple channels (i.e. don't assume
4111 * that all channels matching my_chann* have the same size...)
4113 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4114 (double) buffer_capacity
);
4117 condition_type
= lttng_condition_get_type(condition
);
4118 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4119 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4120 threshold
, sample
->highest_usage
);
4123 * The low condition should only be triggered once _all_ of the
4124 * streams in a channel have gone below the "low" threshold.
4126 if (sample
->highest_usage
<= threshold
) {
4130 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4131 threshold
, sample
->highest_usage
);
4134 * For high buffer usage scenarios, we want to trigger whenever
4135 * _any_ of the streams has reached the "high" threshold.
4137 if (sample
->highest_usage
>= threshold
) {
4146 bool evaluate_session_consumed_size_condition(
4147 const struct lttng_condition
*condition
,
4148 uint64_t session_consumed_size
)
4151 const struct lttng_condition_session_consumed_size
*size_condition
=
4152 container_of(condition
,
4153 struct lttng_condition_session_consumed_size
,
4156 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4157 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4158 threshold
, session_consumed_size
);
4159 return session_consumed_size
>= threshold
;
4163 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4164 struct lttng_evaluation
**evaluation
,
4165 const struct notification_thread_state
*state
,
4166 const struct channel_state_sample
*previous_sample
,
4167 const struct channel_state_sample
*latest_sample
,
4168 uint64_t previous_session_consumed_total
,
4169 uint64_t latest_session_consumed_total
,
4170 struct channel_info
*channel_info
)
4173 enum lttng_condition_type condition_type
;
4174 const bool previous_sample_available
= !!previous_sample
;
4175 bool previous_sample_result
= false;
4176 bool latest_sample_result
;
4178 condition_type
= lttng_condition_get_type(condition
);
4180 switch (condition_type
) {
4181 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4182 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4183 if (caa_likely(previous_sample_available
)) {
4184 previous_sample_result
=
4185 evaluate_buffer_usage_condition(condition
,
4186 previous_sample
, channel_info
->capacity
);
4188 latest_sample_result
= evaluate_buffer_usage_condition(
4189 condition
, latest_sample
,
4190 channel_info
->capacity
);
4192 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4193 if (caa_likely(previous_sample_available
)) {
4194 previous_sample_result
=
4195 evaluate_session_consumed_size_condition(
4197 previous_session_consumed_total
);
4199 latest_sample_result
=
4200 evaluate_session_consumed_size_condition(
4202 latest_session_consumed_total
);
4205 /* Unknown condition type; internal error. */
4209 if (!latest_sample_result
||
4210 (previous_sample_result
== latest_sample_result
)) {
4212 * Only trigger on a condition evaluation transition.
4214 * NOTE: This edge-triggered logic may not be appropriate for
4215 * future condition types.
4220 if (!evaluation
|| !latest_sample_result
) {
4224 switch (condition_type
) {
4225 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4226 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4227 *evaluation
= lttng_evaluation_buffer_usage_create(
4229 latest_sample
->highest_usage
,
4230 channel_info
->capacity
);
4232 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4233 *evaluation
= lttng_evaluation_session_consumed_size_create(
4234 latest_session_consumed_total
);
4249 int client_notification_overflow(struct notification_client
*client
)
4252 const struct lttng_notification_channel_message msg
= {
4253 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4256 ASSERT_LOCKED(client
->lock
);
4258 DBG("Dropping notification addressed to client (socket fd = %i)",
4260 if (client
->communication
.outbound
.dropped_notification
) {
4262 * The client already has a "notification dropped" message
4263 * in its outgoing queue. Nothing to do since all
4264 * of those messages are coalesced.
4269 client
->communication
.outbound
.dropped_notification
= true;
4270 ret
= lttng_dynamic_buffer_append(
4271 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4274 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4281 static int client_handle_transmission_status_wrapper(
4282 struct notification_client
*client
,
4283 enum client_transmission_status status
,
4286 return client_handle_transmission_status(client
, status
,
4287 (struct notification_thread_state
*) user_data
);
4291 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4292 const struct lttng_evaluation
*evaluation
,
4293 struct notification_client_list
* client_list
,
4294 struct notification_thread_state
*state
,
4295 uid_t object_uid
, gid_t object_gid
)
4297 const struct lttng_credentials creds
= {
4298 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4299 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4302 return notification_client_list_send_evaluation(client_list
,
4303 trigger
, evaluation
,
4305 client_handle_transmission_status_wrapper
, state
);
4309 * Permission checks relative to notification channel clients are performed
4310 * here. Notice how object, client, and trigger credentials are involved in
4313 * The `object` credentials are the credentials associated with the "subject"
4314 * of a condition. For instance, a `rotation completed` condition applies
4315 * to a session. When that condition is met, it will produce an evaluation
4316 * against a session. Hence, in this case, the `object` credentials are the
4317 * credentials of the "subject" session.
4319 * The `trigger` credentials are the credentials of the user that registered the
4322 * The `client` credentials are the credentials of the user that created a given
4323 * notification channel.
4325 * In terms of visibility, it is expected that non-privilieged users can only
4326 * register triggers against "their" objects (their own sessions and
4327 * applications they are allowed to interact with). They can then open a
4328 * notification channel and subscribe to notifications associated with those
4331 * As for privilieged users, they can register triggers against the objects of
4332 * other users. They can then subscribe to the notifications associated to their
4333 * triggers. Privilieged users _can't_ subscribe to the notifications of
4334 * triggers owned by other users; they must create their own triggers.
4336 * This is more a concern of usability than security. It would be difficult for
4337 * a root user reliably subscribe to a specific set of conditions without
4338 * interference from external users (those could, for instance, unregister
4342 int notification_client_list_send_evaluation(
4343 struct notification_client_list
*client_list
,
4344 const struct lttng_trigger
*trigger
,
4345 const struct lttng_evaluation
*evaluation
,
4346 const struct lttng_credentials
*source_object_creds
,
4347 report_client_transmission_result_cb client_report
,
4351 struct lttng_payload msg_payload
;
4352 struct notification_client_list_element
*client_list_element
, *tmp
;
4353 const struct lttng_notification notification
= {
4354 .trigger
= (struct lttng_trigger
*) trigger
,
4355 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4357 struct lttng_notification_channel_message msg_header
= {
4358 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4360 const struct lttng_credentials
*trigger_creds
=
4361 lttng_trigger_get_credentials(trigger
);
4363 lttng_payload_init(&msg_payload
);
4365 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4366 sizeof(msg_header
));
4371 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4373 ERR("[notification-thread] Failed to serialize notification");
4378 /* Update payload size. */
4379 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4380 ->size
= (uint32_t)(
4381 msg_payload
.buffer
.size
- sizeof(msg_header
));
4383 /* Update the payload number of fds. */
4385 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4386 &msg_payload
, 0, -1);
4388 ((struct lttng_notification_channel_message
*)
4389 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4390 lttng_payload_view_get_fd_handle_count(&pv
);
4393 pthread_mutex_lock(&client_list
->lock
);
4394 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4395 &client_list
->clients_list
, node
) {
4396 enum client_transmission_status transmission_status
;
4397 struct notification_client
*client
=
4398 client_list_element
->client
;
4401 pthread_mutex_lock(&client
->lock
);
4402 if (!client
->communication
.active
) {
4404 * Skip inactive client (protocol error or
4407 DBG("Skipping client at it is marked as inactive");
4411 if (source_object_creds
) {
4412 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4413 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4416 * Client is not allowed to monitor this
4419 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4424 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
)) {
4425 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4429 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4430 client
->socket
, msg_payload
.buffer
.size
);
4432 if (client_has_outbound_data_left(client
)) {
4434 * Outgoing data is already buffered for this client;
4435 * drop the notification and enqueue a "dropped
4436 * notification" message if this is the first dropped
4437 * notification since the socket spilled-over to the
4440 ret
= client_notification_overflow(client
);
4447 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4453 transmission_status
= client_flush_outgoing_queue(client
);
4454 pthread_mutex_unlock(&client
->lock
);
4455 ret
= client_report(client
, transmission_status
, user_data
);
4458 goto end_unlock_list
;
4464 pthread_mutex_unlock(&client
->lock
);
4467 goto end_unlock_list
;
4473 pthread_mutex_unlock(&client_list
->lock
);
4475 lttng_payload_reset(&msg_payload
);
4480 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4481 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4485 struct lttng_event_notifier_notification
*notification
= NULL
;
4486 char *capture_buffer
= NULL
;
4487 size_t capture_buffer_size
;
4488 void *reception_buffer
;
4489 size_t reception_size
;
4491 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4492 struct lttng_kernel_event_notifier_notification kernel_notification
;
4494 /* Init lttng_event_notifier_notification */
4496 case LTTNG_DOMAIN_UST
:
4497 reception_buffer
= (void *) &ust_notification
;
4498 reception_size
= sizeof(ust_notification
);
4500 case LTTNG_DOMAIN_KERNEL
:
4501 reception_buffer
= (void *) &kernel_notification
;
4502 reception_size
= sizeof(kernel_notification
);
4509 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4510 * ensuring that read/write of tracer notifications are atomic.
4512 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4514 if (ret
!= reception_size
) {
4515 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4516 notification_pipe_read_fd
, reception_size
, ret
);
4522 case LTTNG_DOMAIN_UST
:
4523 token
= ust_notification
.token
;
4524 capture_buffer_size
= ust_notification
.capture_buf_size
;
4526 case LTTNG_DOMAIN_KERNEL
:
4527 token
= kernel_notification
.token
;
4528 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4534 if (capture_buffer_size
== 0) {
4535 capture_buffer
= NULL
;
4539 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4540 ERR("[notification-thread] Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4541 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4545 capture_buffer
= zmalloc(capture_buffer_size
);
4546 if (!capture_buffer
) {
4547 ERR("[notification-thread] Failed to allocate capture buffer");
4551 /* Fetch additional payload (capture). */
4552 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4553 if (ret
!= capture_buffer_size
) {
4554 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4555 notification_pipe_read_fd
);
4560 notification
= lttng_event_notifier_notification_create(token
, domain
,
4561 capture_buffer
, capture_buffer_size
);
4562 if (notification
== NULL
) {
4567 * Ownership transfered to the lttng_event_notifier_notification object.
4569 capture_buffer
= NULL
;
4572 free(capture_buffer
);
4573 return notification
;
4577 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4578 struct lttng_event_notifier_notification
*notification
)
4580 struct cds_lfht_node
*node
;
4581 struct cds_lfht_iter iter
;
4582 struct notification_trigger_tokens_ht_element
*element
;
4583 enum lttng_trigger_status trigger_status
;
4584 struct lttng_evaluation
*evaluation
= NULL
;
4585 enum action_executor_status executor_status
;
4586 struct notification_client_list
*client_list
= NULL
;
4587 const char *trigger_name
;
4589 unsigned int capture_count
= 0;
4591 /* Find triggers associated with this token. */
4593 cds_lfht_lookup(state
->trigger_tokens_ht
,
4594 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4595 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4596 node
= cds_lfht_iter_get_node(&iter
);
4597 if (caa_unlikely(!node
)) {
4599 * This is not an error, slow consumption of the tracer
4600 * notifications can lead to situations where a trigger is
4601 * removed but we still get tracer notifications matching a
4602 * trigger that no longer exists.
4608 element
= caa_container_of(node
,
4609 struct notification_trigger_tokens_ht_element
,
4612 trigger_status
= lttng_trigger_get_name(element
->trigger
, &trigger_name
);
4613 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4615 if (lttng_condition_on_event_get_capture_descriptor_count(
4616 lttng_trigger_get_const_condition(element
->trigger
),
4617 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4618 ERR("Failed to get capture count");
4623 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4624 ERR("Expected capture but capture buffer is null");
4629 evaluation
= lttng_evaluation_on_event_create(
4630 container_of(lttng_trigger_get_const_condition(
4632 struct lttng_condition_on_event
,
4634 notification
->capture_buffer
,
4635 notification
->capture_buf_size
, false);
4637 if (evaluation
== NULL
) {
4638 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
4642 client_list
= get_client_list_from_condition(state
,
4643 lttng_trigger_get_const_condition(element
->trigger
));
4644 executor_status
= action_executor_enqueue(state
->executor
,
4645 element
->trigger
, evaluation
, NULL
, client_list
);
4646 switch (executor_status
) {
4647 case ACTION_EXECUTOR_STATUS_OK
:
4650 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4652 struct notification_client_list_element
*client_list_element
,
4656 * Not a fatal error; this is expected and simply means the
4657 * executor has too much work queued already.
4661 /* No clients subscribed to notifications for this trigger. */
4666 /* Warn clients that a notification (or more) was dropped. */
4667 pthread_mutex_lock(&client_list
->lock
);
4668 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4669 &client_list
->clients_list
, node
) {
4670 enum client_transmission_status transmission_status
;
4671 struct notification_client
*client
=
4672 client_list_element
->client
;
4674 pthread_mutex_lock(&client
->lock
);
4675 ret
= client_notification_overflow(client
);
4681 transmission_status
=
4682 client_flush_outgoing_queue(client
);
4683 ret
= client_handle_transmission_status(
4684 client
, transmission_status
, state
);
4690 pthread_mutex_unlock(&client
->lock
);
4696 pthread_mutex_unlock(&client_list
->lock
);
4699 case ACTION_EXECUTOR_STATUS_INVALID
:
4700 case ACTION_EXECUTOR_STATUS_ERROR
:
4701 /* Fatal error, shut down everything. */
4702 ERR("Fatal error encoutered while enqueuing action to the action executor");
4706 /* Unhandled error. */
4711 notification_client_list_put(client_list
);
4718 int handle_one_event_notifier_notification(
4719 struct notification_thread_state
*state
,
4720 int pipe
, enum lttng_domain_type domain
)
4723 struct lttng_event_notifier_notification
*notification
= NULL
;
4725 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4726 if (notification
== NULL
) {
4727 /* Reception failed, don't consider it fatal. */
4728 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4729 pipe
, lttng_domain_type_str(domain
));
4733 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4735 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4736 pipe
, lttng_domain_type_str(domain
));
4741 lttng_event_notifier_notification_destroy(notification
);
4745 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4746 int pipe
, enum lttng_domain_type domain
)
4748 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4751 int handle_notification_thread_channel_sample(
4752 struct notification_thread_state
*state
, int pipe
,
4753 enum lttng_domain_type domain
)
4756 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4757 struct channel_info
*channel_info
;
4758 struct cds_lfht_node
*node
;
4759 struct cds_lfht_iter iter
;
4760 struct lttng_channel_trigger_list
*trigger_list
;
4761 struct lttng_trigger_list_element
*trigger_list_element
;
4762 bool previous_sample_available
= false;
4763 struct channel_state_sample previous_sample
, latest_sample
;
4764 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4765 struct lttng_credentials channel_creds
;
4768 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4769 * ensuring that read/write of sampling messages are atomic.
4771 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4772 if (ret
!= sizeof(sample_msg
)) {
4773 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4780 latest_sample
.key
.key
= sample_msg
.key
;
4781 latest_sample
.key
.domain
= domain
;
4782 latest_sample
.highest_usage
= sample_msg
.highest
;
4783 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4784 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4788 /* Retrieve the channel's informations */
4789 cds_lfht_lookup(state
->channels_ht
,
4790 hash_channel_key(&latest_sample
.key
),
4794 node
= cds_lfht_iter_get_node(&iter
);
4795 if (caa_unlikely(!node
)) {
4797 * Not an error since the consumer can push a sample to the pipe
4798 * and the rest of the session daemon could notify us of the
4799 * channel's destruction before we get a chance to process that
4802 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4803 latest_sample
.key
.key
,
4804 lttng_domain_type_str(domain
));
4807 channel_info
= caa_container_of(node
, struct channel_info
,
4809 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4811 latest_sample
.key
.key
,
4812 channel_info
->session_info
->name
,
4813 latest_sample
.highest_usage
,
4814 latest_sample
.lowest_usage
,
4815 latest_sample
.channel_total_consumed
);
4817 previous_session_consumed_total
=
4818 channel_info
->session_info
->consumed_data_size
;
4820 /* Retrieve the channel's last sample, if it exists, and update it. */
4821 cds_lfht_lookup(state
->channel_state_ht
,
4822 hash_channel_key(&latest_sample
.key
),
4823 match_channel_state_sample
,
4826 node
= cds_lfht_iter_get_node(&iter
);
4827 if (caa_likely(node
)) {
4828 struct channel_state_sample
*stored_sample
;
4830 /* Update the sample stored. */
4831 stored_sample
= caa_container_of(node
,
4832 struct channel_state_sample
,
4833 channel_state_ht_node
);
4835 memcpy(&previous_sample
, stored_sample
,
4836 sizeof(previous_sample
));
4837 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4838 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4839 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4840 previous_sample_available
= true;
4842 latest_session_consumed_total
=
4843 previous_session_consumed_total
+
4844 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4847 * This is the channel's first sample, allocate space for and
4848 * store the new sample.
4850 struct channel_state_sample
*stored_sample
;
4852 stored_sample
= zmalloc(sizeof(*stored_sample
));
4853 if (!stored_sample
) {
4858 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4859 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4860 cds_lfht_add(state
->channel_state_ht
,
4861 hash_channel_key(&stored_sample
->key
),
4862 &stored_sample
->channel_state_ht_node
);
4864 latest_session_consumed_total
=
4865 previous_session_consumed_total
+
4866 latest_sample
.channel_total_consumed
;
4869 channel_info
->session_info
->consumed_data_size
=
4870 latest_session_consumed_total
;
4872 /* Find triggers associated with this channel. */
4873 cds_lfht_lookup(state
->channel_triggers_ht
,
4874 hash_channel_key(&latest_sample
.key
),
4875 match_channel_trigger_list
,
4878 node
= cds_lfht_iter_get_node(&iter
);
4879 if (caa_likely(!node
)) {
4883 channel_creds
= (typeof(channel_creds
)) {
4884 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4885 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4888 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4889 channel_triggers_ht_node
);
4890 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4892 const struct lttng_condition
*condition
;
4893 struct lttng_trigger
*trigger
;
4894 struct notification_client_list
*client_list
= NULL
;
4895 struct lttng_evaluation
*evaluation
= NULL
;
4896 enum action_executor_status executor_status
;
4899 trigger
= trigger_list_element
->trigger
;
4900 condition
= lttng_trigger_get_const_condition(trigger
);
4904 * Check if any client is subscribed to the result of this
4907 client_list
= get_client_list_from_condition(state
, condition
);
4909 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4910 previous_sample_available
? &previous_sample
: NULL
,
4912 previous_session_consumed_total
,
4913 latest_session_consumed_total
,
4915 if (caa_unlikely(ret
)) {
4919 if (caa_likely(!evaluation
)) {
4924 * Ownership of `evaluation` transferred to the action executor
4925 * no matter the result.
4927 executor_status
= action_executor_enqueue(state
->executor
,
4928 trigger
, evaluation
, &channel_creds
,
4931 switch (executor_status
) {
4932 case ACTION_EXECUTOR_STATUS_OK
:
4934 case ACTION_EXECUTOR_STATUS_ERROR
:
4935 case ACTION_EXECUTOR_STATUS_INVALID
:
4937 * TODO Add trigger identification (name/id) when
4938 * it is added to the API.
4940 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4943 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4945 * TODO Add trigger identification (name/id) when
4946 * it is added to the API.
4948 * Not a fatal error.
4950 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4958 notification_client_list_put(client_list
);
4959 if (caa_unlikely(ret
)) {