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 /* Remove trigger from channel_triggers_ht. */
3053 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
3054 channel_triggers_ht_node
) {
3055 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3057 cds_list_for_each_entry_safe(trigger_element
, tmp
,
3058 &trigger_list
->list
, node
) {
3059 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3063 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
3064 cds_list_del(&trigger_element
->node
);
3065 /* A trigger can only appear once per channel */
3070 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3071 teardown_tracer_notifier(state
, trigger
);
3074 if (is_trigger_action_notify(trigger
)) {
3076 * Remove and release the client list from
3077 * notification_trigger_clients_ht.
3079 client_list
= get_client_list_from_condition(state
, condition
);
3080 assert(client_list
);
3082 pthread_mutex_lock(&client_list
->lock
);
3083 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3084 pthread_mutex_unlock(&client_list
->lock
);
3086 /* Put new reference and the hashtable's reference. */
3087 notification_client_list_put(client_list
);
3088 notification_client_list_put(client_list
);
3092 /* Remove trigger from triggers_ht. */
3093 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3095 /* Release the ownership of the trigger. */
3096 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3097 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3101 *_cmd_reply
= cmd_reply
;
3106 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3107 int handle_notification_thread_command(
3108 struct notification_thread_handle
*handle
,
3109 struct notification_thread_state
*state
)
3113 struct notification_thread_command
*cmd
;
3115 /* Read the event pipe to put it back into a quiescent state. */
3116 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
3118 if (ret
!= sizeof(counter
)) {
3122 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3123 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3124 struct notification_thread_command
, cmd_list_node
);
3125 cds_list_del(&cmd
->cmd_list_node
);
3126 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3128 DBG("[notification-thread] Received `%s` command",
3129 notification_command_type_str(cmd
->type
));
3130 switch (cmd
->type
) {
3131 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3132 ret
= handle_notification_thread_command_register_trigger(state
,
3133 cmd
->parameters
.register_trigger
.trigger
,
3136 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3137 ret
= handle_notification_thread_command_unregister_trigger(
3139 cmd
->parameters
.unregister_trigger
.trigger
,
3142 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3143 ret
= handle_notification_thread_command_add_channel(
3145 cmd
->parameters
.add_channel
.session
.name
,
3146 cmd
->parameters
.add_channel
.session
.uid
,
3147 cmd
->parameters
.add_channel
.session
.gid
,
3148 cmd
->parameters
.add_channel
.channel
.name
,
3149 cmd
->parameters
.add_channel
.channel
.domain
,
3150 cmd
->parameters
.add_channel
.channel
.key
,
3151 cmd
->parameters
.add_channel
.channel
.capacity
,
3154 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3155 ret
= handle_notification_thread_command_remove_channel(
3156 state
, cmd
->parameters
.remove_channel
.key
,
3157 cmd
->parameters
.remove_channel
.domain
,
3160 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3161 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3162 ret
= handle_notification_thread_command_session_rotation(
3165 cmd
->parameters
.session_rotation
.session_name
,
3166 cmd
->parameters
.session_rotation
.uid
,
3167 cmd
->parameters
.session_rotation
.gid
,
3168 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3169 cmd
->parameters
.session_rotation
.location
,
3172 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3173 ret
= handle_notification_thread_command_add_tracer_event_source(
3175 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3176 cmd
->parameters
.tracer_event_source
.domain
,
3179 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3180 ret
= handle_notification_thread_command_remove_tracer_event_source(
3182 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3185 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3187 struct lttng_triggers
*triggers
= NULL
;
3189 ret
= handle_notification_thread_command_list_triggers(
3192 cmd
->parameters
.list_triggers
.uid
,
3195 cmd
->reply
.list_triggers
.triggers
= triggers
;
3199 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3200 cmd
->reply_code
= LTTNG_OK
;
3203 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3205 struct lttng_trigger
*trigger
= NULL
;
3207 ret
= handle_notification_thread_command_get_trigger(state
,
3208 cmd
->parameters
.get_trigger
.trigger
, &trigger
,
3210 cmd
->reply
.get_trigger
.trigger
= trigger
;
3213 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3215 const enum client_transmission_status client_status
=
3216 cmd
->parameters
.client_communication_update
3218 const notification_client_id client_id
=
3219 cmd
->parameters
.client_communication_update
.id
;
3220 struct notification_client
*client
;
3223 client
= get_client_from_id(client_id
, state
);
3227 * Client error was probably already picked-up by the
3228 * notification thread or it has disconnected
3229 * gracefully while this command was queued.
3231 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3235 ret
= client_handle_transmission_status(
3236 client
, client_status
, state
);
3242 ERR("[notification-thread] Unknown internal command received");
3250 if (cmd
->is_async
) {
3254 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3258 /* Wake-up and return a fatal error to the calling thread. */
3259 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3260 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3262 /* Indicate a fatal error to the caller. */
3267 int socket_set_non_blocking(int socket
)
3271 /* Set the pipe as non-blocking. */
3272 ret
= fcntl(socket
, F_GETFL
, 0);
3274 PERROR("fcntl get socket flags");
3279 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3281 PERROR("fcntl set O_NONBLOCK socket flag");
3284 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3290 int client_reset_inbound_state(struct notification_client
*client
)
3295 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3297 client
->communication
.inbound
.bytes_to_receive
=
3298 sizeof(struct lttng_notification_channel_message
);
3299 client
->communication
.inbound
.msg_type
=
3300 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3301 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3302 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3303 ret
= lttng_dynamic_buffer_set_size(
3304 &client
->communication
.inbound
.payload
.buffer
,
3305 client
->communication
.inbound
.bytes_to_receive
);
3310 int handle_notification_thread_client_connect(
3311 struct notification_thread_state
*state
)
3314 struct notification_client
*client
;
3316 DBG("[notification-thread] Handling new notification channel client connection");
3318 client
= zmalloc(sizeof(*client
));
3325 pthread_mutex_init(&client
->lock
, NULL
);
3326 client
->id
= state
->next_notification_client_id
++;
3327 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3328 lttng_payload_init(&client
->communication
.inbound
.payload
);
3329 lttng_payload_init(&client
->communication
.outbound
.payload
);
3330 client
->communication
.inbound
.expect_creds
= true;
3332 ret
= client_reset_inbound_state(client
);
3334 ERR("[notification-thread] Failed to reset client communication's inbound state");
3339 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3341 ERR("[notification-thread] Failed to accept new notification channel client connection");
3346 client
->socket
= ret
;
3348 ret
= socket_set_non_blocking(client
->socket
);
3350 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3354 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3356 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3361 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3362 LPOLLIN
| LPOLLERR
|
3363 LPOLLHUP
| LPOLLRDHUP
);
3365 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3369 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3373 cds_lfht_add(state
->client_socket_ht
,
3374 hash_client_socket(client
->socket
),
3375 &client
->client_socket_ht_node
);
3376 cds_lfht_add(state
->client_id_ht
,
3377 hash_client_id(client
->id
),
3378 &client
->client_id_ht_node
);
3384 notification_client_destroy(client
, state
);
3389 * RCU read-lock must be held by the caller.
3390 * Client lock must _not_ be held by the caller.
3393 int notification_thread_client_disconnect(
3394 struct notification_client
*client
,
3395 struct notification_thread_state
*state
)
3398 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3400 /* Acquire the client lock to disable its communication atomically. */
3401 pthread_mutex_lock(&client
->lock
);
3402 client
->communication
.active
= false;
3403 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3404 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3405 pthread_mutex_unlock(&client
->lock
);
3407 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3409 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3413 /* Release all conditions to which the client was subscribed. */
3414 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3415 &client
->condition_list
, node
) {
3416 (void) notification_thread_client_unsubscribe(client
,
3417 condition_list_element
->condition
, state
, NULL
);
3421 * Client no longer accessible to other threads (through the
3424 notification_client_destroy(client
, state
);
3428 int handle_notification_thread_client_disconnect(
3429 int client_socket
, struct notification_thread_state
*state
)
3432 struct notification_client
*client
;
3435 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3437 client
= get_client_from_socket(client_socket
, state
);
3439 /* Internal state corruption, fatal error. */
3440 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3446 ret
= notification_thread_client_disconnect(client
, state
);
3452 int handle_notification_thread_client_disconnect_all(
3453 struct notification_thread_state
*state
)
3455 struct cds_lfht_iter iter
;
3456 struct notification_client
*client
;
3457 bool error_encoutered
= false;
3460 DBG("[notification-thread] Closing all client connections");
3461 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3462 client_socket_ht_node
) {
3465 ret
= notification_thread_client_disconnect(
3468 error_encoutered
= true;
3472 return error_encoutered
? 1 : 0;
3475 int handle_notification_thread_trigger_unregister_all(
3476 struct notification_thread_state
*state
)
3478 bool error_occurred
= false;
3479 struct cds_lfht_iter iter
;
3480 struct lttng_trigger_ht_element
*trigger_ht_element
;
3483 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3485 int ret
= handle_notification_thread_command_unregister_trigger(
3486 state
, trigger_ht_element
->trigger
, NULL
);
3488 error_occurred
= true;
3492 return error_occurred
? -1 : 0;
3496 int client_handle_transmission_status(
3497 struct notification_client
*client
,
3498 enum client_transmission_status transmission_status
,
3499 struct notification_thread_state
*state
)
3503 switch (transmission_status
) {
3504 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3505 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3506 CLIENT_POLL_MASK_IN
);
3512 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3514 * We want to be notified whenever there is buffer space
3515 * available to send the rest of the payload.
3517 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3518 CLIENT_POLL_MASK_IN_OUT
);
3523 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3524 ret
= notification_thread_client_disconnect(client
, state
);
3529 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3539 /* Client lock must be acquired by caller. */
3541 enum client_transmission_status
client_flush_outgoing_queue(
3542 struct notification_client
*client
)
3545 size_t to_send_count
;
3546 enum client_transmission_status status
;
3547 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3548 &client
->communication
.outbound
.payload
, 0, -1);
3549 const int fds_to_send_count
=
3550 lttng_payload_view_get_fd_handle_count(&pv
);
3552 ASSERT_LOCKED(client
->lock
);
3554 if (!client
->communication
.active
) {
3555 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3559 if (pv
.buffer
.size
== 0) {
3561 * If both data and fds are equal to zero, we are in an invalid
3564 assert(fds_to_send_count
!= 0);
3569 to_send_count
= pv
.buffer
.size
;
3570 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3573 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3576 if ((ret
>= 0 && ret
< to_send_count
)) {
3577 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3579 to_send_count
-= max(ret
, 0);
3581 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3583 pv
.buffer
.size
- to_send_count
,
3585 ret
= lttng_dynamic_buffer_set_size(
3586 &client
->communication
.outbound
.payload
.buffer
,
3592 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3594 } else if (ret
< 0) {
3595 /* Generic error, disable the client's communication. */
3596 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3598 client
->communication
.active
= false;
3599 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3603 * No error and flushed the queue completely.
3605 * The payload buffer size is used later to
3606 * check if there is notifications queued. So albeit that the
3607 * direct caller knows that the transmission is complete, we
3608 * need to set the buffer size to zero.
3610 ret
= lttng_dynamic_buffer_set_size(
3611 &client
->communication
.outbound
.payload
.buffer
, 0);
3618 /* No fds to send, transmission is complete. */
3619 if (fds_to_send_count
== 0) {
3620 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3624 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3625 client
->socket
, &pv
);
3627 /* Generic error, disable the client's communication. */
3628 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3630 client
->communication
.active
= false;
3631 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3633 } else if (ret
== 0) {
3634 /* Nothing could be sent. */
3635 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3637 /* Fd passing is an all or nothing kind of thing. */
3638 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3640 * The payload _fd_array count is used later to
3641 * check if there is notifications queued. So although the
3642 * direct caller knows that the transmission is complete, we
3643 * need to clear the _fd_array for the queuing check.
3645 lttng_dynamic_pointer_array_clear(
3646 &client
->communication
.outbound
.payload
3651 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3652 client
->communication
.outbound
.queued_command_reply
= false;
3653 client
->communication
.outbound
.dropped_notification
= false;
3654 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3659 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3663 bool client_has_outbound_data_left(
3664 const struct notification_client
*client
)
3666 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3667 &client
->communication
.outbound
.payload
, 0, -1);
3668 const bool has_data
= pv
.buffer
.size
!= 0;
3669 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3671 return has_data
|| has_fds
;
3674 /* Client lock must _not_ be held by the caller. */
3676 int client_send_command_reply(struct notification_client
*client
,
3677 struct notification_thread_state
*state
,
3678 enum lttng_notification_channel_status status
)
3681 struct lttng_notification_channel_command_reply reply
= {
3682 .status
= (int8_t) status
,
3684 struct lttng_notification_channel_message msg
= {
3685 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3686 .size
= sizeof(reply
),
3688 char buffer
[sizeof(msg
) + sizeof(reply
)];
3689 enum client_transmission_status transmission_status
;
3691 memcpy(buffer
, &msg
, sizeof(msg
));
3692 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3693 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3695 pthread_mutex_lock(&client
->lock
);
3696 if (client
->communication
.outbound
.queued_command_reply
) {
3697 /* Protocol error. */
3701 /* Enqueue buffer to outgoing queue and flush it. */
3702 ret
= lttng_dynamic_buffer_append(
3703 &client
->communication
.outbound
.payload
.buffer
,
3704 buffer
, sizeof(buffer
));
3709 transmission_status
= client_flush_outgoing_queue(client
);
3711 if (client_has_outbound_data_left(client
)) {
3712 /* Queue could not be emptied. */
3713 client
->communication
.outbound
.queued_command_reply
= true;
3716 pthread_mutex_unlock(&client
->lock
);
3717 ret
= client_handle_transmission_status(
3718 client
, transmission_status
, state
);
3725 pthread_mutex_unlock(&client
->lock
);
3731 int client_handle_message_unknown(struct notification_client
*client
,
3732 struct notification_thread_state
*state
)
3736 * Receiving message header. The function will be called again
3737 * once the rest of the message as been received and can be
3740 const struct lttng_notification_channel_message
*msg
;
3742 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3743 msg
= (const struct lttng_notification_channel_message
*)
3744 client
->communication
.inbound
.payload
.buffer
.data
;
3746 if (msg
->size
== 0 ||
3747 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3748 ERR("[notification-thread] Invalid notification channel message: length = %u",
3754 switch (msg
->type
) {
3755 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3756 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3757 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3761 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3765 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3766 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3767 client
->communication
.inbound
.msg_type
=
3768 (enum lttng_notification_channel_message_type
) msg
->type
;
3769 ret
= lttng_dynamic_buffer_set_size(
3770 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3772 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3779 int client_handle_message_handshake(struct notification_client
*client
,
3780 struct notification_thread_state
*state
)
3783 struct lttng_notification_channel_command_handshake
*handshake_client
;
3784 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3785 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3786 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3788 const struct lttng_notification_channel_message msg_header
= {
3789 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3790 .size
= sizeof(handshake_reply
),
3792 enum lttng_notification_channel_status status
=
3793 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3794 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3796 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3797 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3798 sizeof(handshake_reply
));
3801 (struct lttng_notification_channel_command_handshake
*)
3802 client
->communication
.inbound
.payload
.buffer
3804 client
->major
= handshake_client
->major
;
3805 client
->minor
= handshake_client
->minor
;
3806 if (!client
->communication
.inbound
.creds_received
) {
3807 ERR("[notification-thread] No credentials received from client");
3812 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3813 &client
->communication
.inbound
.creds
);
3814 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3815 &client
->communication
.inbound
.creds
);
3816 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3817 client
->uid
, client
->gid
, (int) client
->major
,
3818 (int) client
->minor
);
3820 if (handshake_client
->major
!=
3821 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3822 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3825 pthread_mutex_lock(&client
->lock
);
3826 /* Outgoing queue will be flushed when the command reply is sent. */
3827 ret
= lttng_dynamic_buffer_append(
3828 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3829 sizeof(send_buffer
));
3831 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3835 client
->validated
= true;
3836 client
->communication
.active
= true;
3837 pthread_mutex_unlock(&client
->lock
);
3839 /* Set reception state to receive the next message header. */
3840 ret
= client_reset_inbound_state(client
);
3842 ERR("[notification-thread] Failed to reset client communication's inbound state");
3846 /* Flushes the outgoing queue. */
3847 ret
= client_send_command_reply(client
, state
, status
);
3849 ERR("[notification-thread] Failed to send reply to notification channel client");
3855 pthread_mutex_unlock(&client
->lock
);
3861 int client_handle_message_subscription(
3862 struct notification_client
*client
,
3863 enum lttng_notification_channel_message_type msg_type
,
3864 struct notification_thread_state
*state
)
3867 struct lttng_condition
*condition
;
3868 enum lttng_notification_channel_status status
=
3869 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3870 struct lttng_payload_view condition_view
=
3871 lttng_payload_view_from_payload(
3872 &client
->communication
.inbound
.payload
,
3874 size_t expected_condition_size
;
3877 * No need to lock client to sample the inbound state as the only
3878 * other thread accessing clients (action executor) only uses the
3881 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3882 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3883 if (ret
!= expected_condition_size
) {
3884 ERR("[notification-thread] Malformed condition received from client");
3888 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3889 ret
= notification_thread_client_subscribe(
3890 client
, condition
, state
, &status
);
3892 ret
= notification_thread_client_unsubscribe(
3893 client
, condition
, state
, &status
);
3900 /* Set reception state to receive the next message header. */
3901 ret
= client_reset_inbound_state(client
);
3903 ERR("[notification-thread] Failed to reset client communication's inbound state");
3907 ret
= client_send_command_reply(client
, state
, status
);
3909 ERR("[notification-thread] Failed to send reply to notification channel client");
3918 int client_dispatch_message(struct notification_client
*client
,
3919 struct notification_thread_state
*state
)
3923 if (client
->communication
.inbound
.msg_type
!=
3924 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3925 client
->communication
.inbound
.msg_type
!=
3926 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3927 !client
->validated
) {
3928 WARN("[notification-thread] client attempted a command before handshake");
3933 switch (client
->communication
.inbound
.msg_type
) {
3934 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3936 ret
= client_handle_message_unknown(client
, state
);
3939 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3941 ret
= client_handle_message_handshake(client
, state
);
3944 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3945 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3947 ret
= client_handle_message_subscription(client
,
3948 client
->communication
.inbound
.msg_type
, state
);
3958 /* Incoming data from client. */
3959 int handle_notification_thread_client_in(
3960 struct notification_thread_state
*state
, int socket
)
3963 struct notification_client
*client
;
3968 client
= get_client_from_socket(socket
, state
);
3970 /* Internal error, abort. */
3975 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3976 client
->communication
.inbound
.bytes_to_receive
;
3977 if (client
->communication
.inbound
.expect_creds
) {
3978 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3979 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3980 client
->communication
.inbound
.bytes_to_receive
,
3981 &client
->communication
.inbound
.creds
);
3983 client
->communication
.inbound
.expect_creds
= false;
3984 client
->communication
.inbound
.creds_received
= true;
3987 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3988 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3989 client
->communication
.inbound
.bytes_to_receive
);
3991 if (recv_ret
>= 0) {
3992 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3994 goto error_disconnect_client
;
3997 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3998 /* Message incomplete wait for more data. */
4003 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
4006 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
4007 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
4009 client
->communication
.inbound
.fds_to_receive
,
4010 &client
->communication
.inbound
.payload
);
4013 * Fds received. non blocking fds passing is all
4016 ssize_t expected_size
;
4018 expected_size
= sizeof(int) *
4019 client
->communication
.inbound
4021 assert(ret
== expected_size
);
4022 client
->communication
.inbound
.fds_to_receive
= 0;
4023 } else if (ret
== 0) {
4024 /* Received nothing. */
4028 goto error_disconnect_client
;
4032 /* At this point the message is complete.*/
4033 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4034 client
->communication
.inbound
.fds_to_receive
== 0);
4035 ret
= client_dispatch_message(client
, state
);
4038 * Only returns an error if this client must be
4041 goto error_disconnect_client
;
4048 error_disconnect_client
:
4049 ret
= notification_thread_client_disconnect(client
, state
);
4053 /* Client ready to receive outgoing data. */
4054 int handle_notification_thread_client_out(
4055 struct notification_thread_state
*state
, int socket
)
4058 struct notification_client
*client
;
4059 enum client_transmission_status transmission_status
;
4062 client
= get_client_from_socket(socket
, state
);
4064 /* Internal error, abort. */
4069 pthread_mutex_lock(&client
->lock
);
4070 transmission_status
= client_flush_outgoing_queue(client
);
4071 pthread_mutex_unlock(&client
->lock
);
4073 ret
= client_handle_transmission_status(
4074 client
, transmission_status
, state
);
4084 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4085 const struct channel_state_sample
*sample
,
4086 uint64_t buffer_capacity
)
4088 bool result
= false;
4090 enum lttng_condition_type condition_type
;
4091 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4092 condition
, struct lttng_condition_buffer_usage
,
4095 if (use_condition
->threshold_bytes
.set
) {
4096 threshold
= use_condition
->threshold_bytes
.value
;
4099 * Threshold was expressed as a ratio.
4101 * TODO the threshold (in bytes) of conditions expressed
4102 * as a ratio of total buffer size could be cached to
4103 * forego this double-multiplication or it could be performed
4104 * as fixed-point math.
4106 * Note that caching should accommodates the case where the
4107 * condition applies to multiple channels (i.e. don't assume
4108 * that all channels matching my_chann* have the same size...)
4110 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4111 (double) buffer_capacity
);
4114 condition_type
= lttng_condition_get_type(condition
);
4115 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4116 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4117 threshold
, sample
->highest_usage
);
4120 * The low condition should only be triggered once _all_ of the
4121 * streams in a channel have gone below the "low" threshold.
4123 if (sample
->highest_usage
<= threshold
) {
4127 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4128 threshold
, sample
->highest_usage
);
4131 * For high buffer usage scenarios, we want to trigger whenever
4132 * _any_ of the streams has reached the "high" threshold.
4134 if (sample
->highest_usage
>= threshold
) {
4143 bool evaluate_session_consumed_size_condition(
4144 const struct lttng_condition
*condition
,
4145 uint64_t session_consumed_size
)
4148 const struct lttng_condition_session_consumed_size
*size_condition
=
4149 container_of(condition
,
4150 struct lttng_condition_session_consumed_size
,
4153 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4154 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4155 threshold
, session_consumed_size
);
4156 return session_consumed_size
>= threshold
;
4160 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4161 struct lttng_evaluation
**evaluation
,
4162 const struct notification_thread_state
*state
,
4163 const struct channel_state_sample
*previous_sample
,
4164 const struct channel_state_sample
*latest_sample
,
4165 uint64_t previous_session_consumed_total
,
4166 uint64_t latest_session_consumed_total
,
4167 struct channel_info
*channel_info
)
4170 enum lttng_condition_type condition_type
;
4171 const bool previous_sample_available
= !!previous_sample
;
4172 bool previous_sample_result
= false;
4173 bool latest_sample_result
;
4175 condition_type
= lttng_condition_get_type(condition
);
4177 switch (condition_type
) {
4178 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4179 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4180 if (caa_likely(previous_sample_available
)) {
4181 previous_sample_result
=
4182 evaluate_buffer_usage_condition(condition
,
4183 previous_sample
, channel_info
->capacity
);
4185 latest_sample_result
= evaluate_buffer_usage_condition(
4186 condition
, latest_sample
,
4187 channel_info
->capacity
);
4189 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4190 if (caa_likely(previous_sample_available
)) {
4191 previous_sample_result
=
4192 evaluate_session_consumed_size_condition(
4194 previous_session_consumed_total
);
4196 latest_sample_result
=
4197 evaluate_session_consumed_size_condition(
4199 latest_session_consumed_total
);
4202 /* Unknown condition type; internal error. */
4206 if (!latest_sample_result
||
4207 (previous_sample_result
== latest_sample_result
)) {
4209 * Only trigger on a condition evaluation transition.
4211 * NOTE: This edge-triggered logic may not be appropriate for
4212 * future condition types.
4217 if (!evaluation
|| !latest_sample_result
) {
4221 switch (condition_type
) {
4222 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4223 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4224 *evaluation
= lttng_evaluation_buffer_usage_create(
4226 latest_sample
->highest_usage
,
4227 channel_info
->capacity
);
4229 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4230 *evaluation
= lttng_evaluation_session_consumed_size_create(
4231 latest_session_consumed_total
);
4246 int client_notification_overflow(struct notification_client
*client
)
4249 const struct lttng_notification_channel_message msg
= {
4250 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4253 ASSERT_LOCKED(client
->lock
);
4255 DBG("Dropping notification addressed to client (socket fd = %i)",
4257 if (client
->communication
.outbound
.dropped_notification
) {
4259 * The client already has a "notification dropped" message
4260 * in its outgoing queue. Nothing to do since all
4261 * of those messages are coalesced.
4266 client
->communication
.outbound
.dropped_notification
= true;
4267 ret
= lttng_dynamic_buffer_append(
4268 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4271 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4278 static int client_handle_transmission_status_wrapper(
4279 struct notification_client
*client
,
4280 enum client_transmission_status status
,
4283 return client_handle_transmission_status(client
, status
,
4284 (struct notification_thread_state
*) user_data
);
4288 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4289 const struct lttng_evaluation
*evaluation
,
4290 struct notification_client_list
* client_list
,
4291 struct notification_thread_state
*state
,
4292 uid_t object_uid
, gid_t object_gid
)
4294 const struct lttng_credentials creds
= {
4295 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4296 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4299 return notification_client_list_send_evaluation(client_list
,
4300 trigger
, evaluation
,
4302 client_handle_transmission_status_wrapper
, state
);
4306 * Permission checks relative to notification channel clients are performed
4307 * here. Notice how object, client, and trigger credentials are involved in
4310 * The `object` credentials are the credentials associated with the "subject"
4311 * of a condition. For instance, a `rotation completed` condition applies
4312 * to a session. When that condition is met, it will produce an evaluation
4313 * against a session. Hence, in this case, the `object` credentials are the
4314 * credentials of the "subject" session.
4316 * The `trigger` credentials are the credentials of the user that registered the
4319 * The `client` credentials are the credentials of the user that created a given
4320 * notification channel.
4322 * In terms of visibility, it is expected that non-privilieged users can only
4323 * register triggers against "their" objects (their own sessions and
4324 * applications they are allowed to interact with). They can then open a
4325 * notification channel and subscribe to notifications associated with those
4328 * As for privilieged users, they can register triggers against the objects of
4329 * other users. They can then subscribe to the notifications associated to their
4330 * triggers. Privilieged users _can't_ subscribe to the notifications of
4331 * triggers owned by other users; they must create their own triggers.
4333 * This is more a concern of usability than security. It would be difficult for
4334 * a root user reliably subscribe to a specific set of conditions without
4335 * interference from external users (those could, for instance, unregister
4339 int notification_client_list_send_evaluation(
4340 struct notification_client_list
*client_list
,
4341 const struct lttng_trigger
*trigger
,
4342 const struct lttng_evaluation
*evaluation
,
4343 const struct lttng_credentials
*source_object_creds
,
4344 report_client_transmission_result_cb client_report
,
4348 struct lttng_payload msg_payload
;
4349 struct notification_client_list_element
*client_list_element
, *tmp
;
4350 const struct lttng_notification notification
= {
4351 .trigger
= (struct lttng_trigger
*) trigger
,
4352 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4354 struct lttng_notification_channel_message msg_header
= {
4355 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4357 const struct lttng_credentials
*trigger_creds
=
4358 lttng_trigger_get_credentials(trigger
);
4360 lttng_payload_init(&msg_payload
);
4362 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4363 sizeof(msg_header
));
4368 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4370 ERR("[notification-thread] Failed to serialize notification");
4375 /* Update payload size. */
4376 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4377 ->size
= (uint32_t)(
4378 msg_payload
.buffer
.size
- sizeof(msg_header
));
4380 /* Update the payload number of fds. */
4382 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4383 &msg_payload
, 0, -1);
4385 ((struct lttng_notification_channel_message
*)
4386 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4387 lttng_payload_view_get_fd_handle_count(&pv
);
4390 pthread_mutex_lock(&client_list
->lock
);
4391 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4392 &client_list
->clients_list
, node
) {
4393 enum client_transmission_status transmission_status
;
4394 struct notification_client
*client
=
4395 client_list_element
->client
;
4398 pthread_mutex_lock(&client
->lock
);
4399 if (!client
->communication
.active
) {
4401 * Skip inactive client (protocol error or
4404 DBG("Skipping client at it is marked as inactive");
4408 if (source_object_creds
) {
4409 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4410 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4413 * Client is not allowed to monitor this
4416 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4421 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
)) {
4422 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4426 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4427 client
->socket
, msg_payload
.buffer
.size
);
4429 if (client_has_outbound_data_left(client
)) {
4431 * Outgoing data is already buffered for this client;
4432 * drop the notification and enqueue a "dropped
4433 * notification" message if this is the first dropped
4434 * notification since the socket spilled-over to the
4437 ret
= client_notification_overflow(client
);
4444 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4450 transmission_status
= client_flush_outgoing_queue(client
);
4451 pthread_mutex_unlock(&client
->lock
);
4452 ret
= client_report(client
, transmission_status
, user_data
);
4455 goto end_unlock_list
;
4461 pthread_mutex_unlock(&client
->lock
);
4464 goto end_unlock_list
;
4470 pthread_mutex_unlock(&client_list
->lock
);
4472 lttng_payload_reset(&msg_payload
);
4477 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4478 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4482 struct lttng_event_notifier_notification
*notification
= NULL
;
4483 char *capture_buffer
= NULL
;
4484 size_t capture_buffer_size
;
4485 void *reception_buffer
;
4486 size_t reception_size
;
4488 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4489 struct lttng_kernel_event_notifier_notification kernel_notification
;
4491 /* Init lttng_event_notifier_notification */
4493 case LTTNG_DOMAIN_UST
:
4494 reception_buffer
= (void *) &ust_notification
;
4495 reception_size
= sizeof(ust_notification
);
4497 case LTTNG_DOMAIN_KERNEL
:
4498 reception_buffer
= (void *) &kernel_notification
;
4499 reception_size
= sizeof(kernel_notification
);
4506 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4507 * ensuring that read/write of tracer notifications are atomic.
4509 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4511 if (ret
!= reception_size
) {
4512 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4513 notification_pipe_read_fd
, reception_size
, ret
);
4519 case LTTNG_DOMAIN_UST
:
4520 token
= ust_notification
.token
;
4521 capture_buffer_size
= ust_notification
.capture_buf_size
;
4523 case LTTNG_DOMAIN_KERNEL
:
4524 token
= kernel_notification
.token
;
4525 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4531 if (capture_buffer_size
== 0) {
4532 capture_buffer
= NULL
;
4536 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4537 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",
4538 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4542 capture_buffer
= zmalloc(capture_buffer_size
);
4543 if (!capture_buffer
) {
4544 ERR("[notification-thread] Failed to allocate capture buffer");
4548 /* Fetch additional payload (capture). */
4549 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4550 if (ret
!= capture_buffer_size
) {
4551 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4552 notification_pipe_read_fd
);
4557 notification
= lttng_event_notifier_notification_create(token
, domain
,
4558 capture_buffer
, capture_buffer_size
);
4559 if (notification
== NULL
) {
4564 * Ownership transfered to the lttng_event_notifier_notification object.
4566 capture_buffer
= NULL
;
4569 free(capture_buffer
);
4570 return notification
;
4574 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4575 struct lttng_event_notifier_notification
*notification
)
4577 struct cds_lfht_node
*node
;
4578 struct cds_lfht_iter iter
;
4579 struct notification_trigger_tokens_ht_element
*element
;
4580 enum lttng_trigger_status trigger_status
;
4581 struct lttng_evaluation
*evaluation
= NULL
;
4582 enum action_executor_status executor_status
;
4583 struct notification_client_list
*client_list
= NULL
;
4584 const char *trigger_name
;
4586 unsigned int capture_count
= 0;
4588 /* Find triggers associated with this token. */
4590 cds_lfht_lookup(state
->trigger_tokens_ht
,
4591 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4592 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4593 node
= cds_lfht_iter_get_node(&iter
);
4594 if (caa_unlikely(!node
)) {
4596 * This is not an error, slow consumption of the tracer
4597 * notifications can lead to situations where a trigger is
4598 * removed but we still get tracer notifications matching a
4599 * trigger that no longer exists.
4605 element
= caa_container_of(node
,
4606 struct notification_trigger_tokens_ht_element
,
4609 trigger_status
= lttng_trigger_get_name(element
->trigger
, &trigger_name
);
4610 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4612 if (lttng_condition_on_event_get_capture_descriptor_count(
4613 lttng_trigger_get_const_condition(element
->trigger
),
4614 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4615 ERR("Failed to get capture count");
4620 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4621 ERR("Expected capture but capture buffer is null");
4626 evaluation
= lttng_evaluation_on_event_create(
4627 container_of(lttng_trigger_get_const_condition(
4629 struct lttng_condition_on_event
,
4631 notification
->capture_buffer
,
4632 notification
->capture_buf_size
, false);
4634 if (evaluation
== NULL
) {
4635 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
4639 client_list
= get_client_list_from_condition(state
,
4640 lttng_trigger_get_const_condition(element
->trigger
));
4641 executor_status
= action_executor_enqueue(state
->executor
,
4642 element
->trigger
, evaluation
, NULL
, client_list
);
4643 switch (executor_status
) {
4644 case ACTION_EXECUTOR_STATUS_OK
:
4647 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4649 struct notification_client_list_element
*client_list_element
,
4653 * Not a fatal error; this is expected and simply means the
4654 * executor has too much work queued already.
4658 /* No clients subscribed to notifications for this trigger. */
4663 /* Warn clients that a notification (or more) was dropped. */
4664 pthread_mutex_lock(&client_list
->lock
);
4665 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4666 &client_list
->clients_list
, node
) {
4667 enum client_transmission_status transmission_status
;
4668 struct notification_client
*client
=
4669 client_list_element
->client
;
4671 pthread_mutex_lock(&client
->lock
);
4672 ret
= client_notification_overflow(client
);
4678 transmission_status
=
4679 client_flush_outgoing_queue(client
);
4680 ret
= client_handle_transmission_status(
4681 client
, transmission_status
, state
);
4687 pthread_mutex_unlock(&client
->lock
);
4693 pthread_mutex_unlock(&client_list
->lock
);
4696 case ACTION_EXECUTOR_STATUS_INVALID
:
4697 case ACTION_EXECUTOR_STATUS_ERROR
:
4698 /* Fatal error, shut down everything. */
4699 ERR("Fatal error encoutered while enqueuing action to the action executor");
4703 /* Unhandled error. */
4708 notification_client_list_put(client_list
);
4715 int handle_one_event_notifier_notification(
4716 struct notification_thread_state
*state
,
4717 int pipe
, enum lttng_domain_type domain
)
4720 struct lttng_event_notifier_notification
*notification
= NULL
;
4722 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4723 if (notification
== NULL
) {
4724 /* Reception failed, don't consider it fatal. */
4725 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4726 pipe
, lttng_domain_type_str(domain
));
4730 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4732 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4733 pipe
, lttng_domain_type_str(domain
));
4738 lttng_event_notifier_notification_destroy(notification
);
4742 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4743 int pipe
, enum lttng_domain_type domain
)
4745 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4748 int handle_notification_thread_channel_sample(
4749 struct notification_thread_state
*state
, int pipe
,
4750 enum lttng_domain_type domain
)
4753 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4754 struct channel_info
*channel_info
;
4755 struct cds_lfht_node
*node
;
4756 struct cds_lfht_iter iter
;
4757 struct lttng_channel_trigger_list
*trigger_list
;
4758 struct lttng_trigger_list_element
*trigger_list_element
;
4759 bool previous_sample_available
= false;
4760 struct channel_state_sample previous_sample
, latest_sample
;
4761 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4762 struct lttng_credentials channel_creds
;
4765 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4766 * ensuring that read/write of sampling messages are atomic.
4768 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4769 if (ret
!= sizeof(sample_msg
)) {
4770 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4777 latest_sample
.key
.key
= sample_msg
.key
;
4778 latest_sample
.key
.domain
= domain
;
4779 latest_sample
.highest_usage
= sample_msg
.highest
;
4780 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4781 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4785 /* Retrieve the channel's informations */
4786 cds_lfht_lookup(state
->channels_ht
,
4787 hash_channel_key(&latest_sample
.key
),
4791 node
= cds_lfht_iter_get_node(&iter
);
4792 if (caa_unlikely(!node
)) {
4794 * Not an error since the consumer can push a sample to the pipe
4795 * and the rest of the session daemon could notify us of the
4796 * channel's destruction before we get a chance to process that
4799 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4800 latest_sample
.key
.key
,
4801 lttng_domain_type_str(domain
));
4804 channel_info
= caa_container_of(node
, struct channel_info
,
4806 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4808 latest_sample
.key
.key
,
4809 channel_info
->session_info
->name
,
4810 latest_sample
.highest_usage
,
4811 latest_sample
.lowest_usage
,
4812 latest_sample
.channel_total_consumed
);
4814 previous_session_consumed_total
=
4815 channel_info
->session_info
->consumed_data_size
;
4817 /* Retrieve the channel's last sample, if it exists, and update it. */
4818 cds_lfht_lookup(state
->channel_state_ht
,
4819 hash_channel_key(&latest_sample
.key
),
4820 match_channel_state_sample
,
4823 node
= cds_lfht_iter_get_node(&iter
);
4824 if (caa_likely(node
)) {
4825 struct channel_state_sample
*stored_sample
;
4827 /* Update the sample stored. */
4828 stored_sample
= caa_container_of(node
,
4829 struct channel_state_sample
,
4830 channel_state_ht_node
);
4832 memcpy(&previous_sample
, stored_sample
,
4833 sizeof(previous_sample
));
4834 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4835 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4836 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4837 previous_sample_available
= true;
4839 latest_session_consumed_total
=
4840 previous_session_consumed_total
+
4841 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4844 * This is the channel's first sample, allocate space for and
4845 * store the new sample.
4847 struct channel_state_sample
*stored_sample
;
4849 stored_sample
= zmalloc(sizeof(*stored_sample
));
4850 if (!stored_sample
) {
4855 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4856 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4857 cds_lfht_add(state
->channel_state_ht
,
4858 hash_channel_key(&stored_sample
->key
),
4859 &stored_sample
->channel_state_ht_node
);
4861 latest_session_consumed_total
=
4862 previous_session_consumed_total
+
4863 latest_sample
.channel_total_consumed
;
4866 channel_info
->session_info
->consumed_data_size
=
4867 latest_session_consumed_total
;
4869 /* Find triggers associated with this channel. */
4870 cds_lfht_lookup(state
->channel_triggers_ht
,
4871 hash_channel_key(&latest_sample
.key
),
4872 match_channel_trigger_list
,
4875 node
= cds_lfht_iter_get_node(&iter
);
4876 if (caa_likely(!node
)) {
4880 channel_creds
= (typeof(channel_creds
)) {
4881 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4882 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4885 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4886 channel_triggers_ht_node
);
4887 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4889 const struct lttng_condition
*condition
;
4890 struct lttng_trigger
*trigger
;
4891 struct notification_client_list
*client_list
= NULL
;
4892 struct lttng_evaluation
*evaluation
= NULL
;
4893 enum action_executor_status executor_status
;
4896 trigger
= trigger_list_element
->trigger
;
4897 condition
= lttng_trigger_get_const_condition(trigger
);
4901 * Check if any client is subscribed to the result of this
4904 client_list
= get_client_list_from_condition(state
, condition
);
4906 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4907 previous_sample_available
? &previous_sample
: NULL
,
4909 previous_session_consumed_total
,
4910 latest_session_consumed_total
,
4912 if (caa_unlikely(ret
)) {
4916 if (caa_likely(!evaluation
)) {
4921 * Ownership of `evaluation` transferred to the action executor
4922 * no matter the result.
4924 executor_status
= action_executor_enqueue(state
->executor
,
4925 trigger
, evaluation
, &channel_creds
,
4928 switch (executor_status
) {
4929 case ACTION_EXECUTOR_STATUS_OK
:
4931 case ACTION_EXECUTOR_STATUS_ERROR
:
4932 case ACTION_EXECUTOR_STATUS_INVALID
:
4934 * TODO Add trigger identification (name/id) when
4935 * it is added to the API.
4937 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4940 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4942 * TODO Add trigger identification (name/id) when
4943 * it is added to the API.
4945 * Not a fatal error.
4947 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4955 notification_client_list_put(client_list
);
4956 if (caa_unlikely(ret
)) {