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/event-rule-internal.h>
32 #include <lttng/domain-internal.h>
33 #include <lttng/notification/channel-internal.h>
34 #include <lttng/trigger/trigger-internal.h>
42 #include "condition-internal.h"
43 #include "notification-thread.h"
44 #include "notification-thread-events.h"
45 #include "notification-thread-commands.h"
46 #include "lttng-sessiond.h"
48 #include "trigger-error-accounting.h"
50 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
51 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
53 enum lttng_object_type
{
54 LTTNG_OBJECT_TYPE_UNKNOWN
,
55 LTTNG_OBJECT_TYPE_NONE
,
56 LTTNG_OBJECT_TYPE_CHANNEL
,
57 LTTNG_OBJECT_TYPE_SESSION
,
60 struct lttng_trigger_list_element
{
61 /* No ownership of the trigger object is assumed. */
62 struct lttng_trigger
*trigger
;
63 struct cds_list_head node
;
66 struct lttng_channel_trigger_list
{
67 struct channel_key channel_key
;
68 /* List of struct lttng_trigger_list_element. */
69 struct cds_list_head list
;
70 /* Node in the channel_triggers_ht */
71 struct cds_lfht_node channel_triggers_ht_node
;
72 /* call_rcu delayed reclaim. */
73 struct rcu_head rcu_node
;
77 * List of triggers applying to a given session.
80 * - lttng_session_trigger_list_create()
81 * - lttng_session_trigger_list_build()
82 * - lttng_session_trigger_list_destroy()
83 * - lttng_session_trigger_list_add()
85 struct lttng_session_trigger_list
{
87 * Not owned by this; points to the session_info structure's
90 const char *session_name
;
91 /* List of struct lttng_trigger_list_element. */
92 struct cds_list_head list
;
93 /* Node in the session_triggers_ht */
94 struct cds_lfht_node session_triggers_ht_node
;
96 * Weak reference to the notification system's session triggers
99 * The session trigger list structure structure is owned by
100 * the session's session_info.
102 * The session_info is kept alive the the channel_infos holding a
103 * reference to it (reference counting). When those channels are
104 * destroyed (at runtime or on teardown), the reference they hold
105 * to the session_info are released. On destruction of session_info,
106 * session_info_destroy() will remove the list of triggers applying
107 * to this session from the notification system's state.
109 * This implies that the session_triggers_ht must be destroyed
110 * after the channels.
112 struct cds_lfht
*session_triggers_ht
;
113 /* Used for delayed RCU reclaim. */
114 struct rcu_head rcu_node
;
117 struct lttng_trigger_ht_element
{
118 struct lttng_trigger
*trigger
;
119 struct cds_lfht_node node
;
120 struct cds_lfht_node node_by_name_uid
;
121 /* call_rcu delayed reclaim. */
122 struct rcu_head rcu_node
;
125 struct lttng_condition_list_element
{
126 struct lttng_condition
*condition
;
127 struct cds_list_head node
;
130 struct channel_state_sample
{
131 struct channel_key key
;
132 struct cds_lfht_node channel_state_ht_node
;
133 uint64_t highest_usage
;
134 uint64_t lowest_usage
;
135 uint64_t channel_total_consumed
;
136 /* call_rcu delayed reclaim. */
137 struct rcu_head rcu_node
;
140 static unsigned long hash_channel_key(struct channel_key
*key
);
141 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
142 struct lttng_evaluation
**evaluation
,
143 const struct notification_thread_state
*state
,
144 const struct channel_state_sample
*previous_sample
,
145 const struct channel_state_sample
*latest_sample
,
146 uint64_t previous_session_consumed_total
,
147 uint64_t latest_session_consumed_total
,
148 struct channel_info
*channel_info
);
150 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
151 const struct lttng_evaluation
*evaluation
,
152 struct notification_client_list
*client_list
,
153 struct notification_thread_state
*state
,
154 uid_t channel_uid
, gid_t channel_gid
);
157 /* session_info API */
159 void session_info_destroy(void *_data
);
161 void session_info_get(struct session_info
*session_info
);
163 void session_info_put(struct session_info
*session_info
);
165 struct session_info
*session_info_create(const char *name
,
166 uid_t uid
, gid_t gid
,
167 struct lttng_session_trigger_list
*trigger_list
,
168 struct cds_lfht
*sessions_ht
);
170 void session_info_add_channel(struct session_info
*session_info
,
171 struct channel_info
*channel_info
);
173 void session_info_remove_channel(struct session_info
*session_info
,
174 struct channel_info
*channel_info
);
176 /* lttng_session_trigger_list API */
178 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
179 const char *session_name
,
180 struct cds_lfht
*session_triggers_ht
);
182 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
183 const struct notification_thread_state
*state
,
184 const char *session_name
);
186 void lttng_session_trigger_list_destroy(
187 struct lttng_session_trigger_list
*list
);
189 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
190 struct lttng_trigger
*trigger
);
193 int client_handle_transmission_status(
194 struct notification_client
*client
,
195 enum client_transmission_status transmission_status
,
196 struct notification_thread_state
*state
);
199 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
202 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
204 /* This double-cast is intended to supress pointer-to-cast warning. */
205 const int socket
= (int) (intptr_t) key
;
206 const struct notification_client
*client
= caa_container_of(node
,
207 struct notification_client
, client_socket_ht_node
);
209 return client
->socket
== socket
;
213 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
215 /* This double-cast is intended to supress pointer-to-cast warning. */
216 const notification_client_id id
= *((notification_client_id
*) key
);
217 const struct notification_client
*client
= caa_container_of(
218 node
, struct notification_client
, client_id_ht_node
);
220 return client
->id
== id
;
224 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
226 struct channel_key
*channel_key
= (struct channel_key
*) key
;
227 struct lttng_channel_trigger_list
*trigger_list
;
229 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
230 channel_triggers_ht_node
);
232 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
233 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
237 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
239 const char *session_name
= (const char *) key
;
240 struct lttng_session_trigger_list
*trigger_list
;
242 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
243 session_triggers_ht_node
);
245 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
249 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
251 struct channel_key
*channel_key
= (struct channel_key
*) key
;
252 struct channel_state_sample
*sample
;
254 sample
= caa_container_of(node
, struct channel_state_sample
,
255 channel_state_ht_node
);
257 return !!((channel_key
->key
== sample
->key
.key
) &&
258 (channel_key
->domain
== sample
->key
.domain
));
262 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
264 struct channel_key
*channel_key
= (struct channel_key
*) key
;
265 struct channel_info
*channel_info
;
267 channel_info
= caa_container_of(node
, struct channel_info
,
270 return !!((channel_key
->key
== channel_info
->key
.key
) &&
271 (channel_key
->domain
== channel_info
->key
.domain
));
275 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
277 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
278 struct lttng_trigger_ht_element
*trigger_ht_element
;
280 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
283 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
287 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
289 const uint64_t *_key
= key
;
290 struct notification_trigger_tokens_ht_element
*element
;
292 element
= caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
294 return *_key
== element
->token
;
298 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
300 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
301 struct notification_client_list
*client_list
;
302 const struct lttng_condition
*condition
;
304 assert(condition_key
);
306 client_list
= caa_container_of(node
, struct notification_client_list
,
307 notification_trigger_clients_ht_node
);
308 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
310 return !!lttng_condition_is_equal(condition_key
, condition
);
314 int match_session(struct cds_lfht_node
*node
, const void *key
)
316 const char *name
= key
;
317 struct session_info
*session_info
= caa_container_of(
318 node
, struct session_info
, sessions_ht_node
);
320 return !strcmp(session_info
->name
, name
);
324 * Match trigger based on name and credentials only.
325 * Name duplication is NOT allowed for the same uid.
327 static int match_name_uid(struct cds_lfht_node
*node
, const void *key
)
330 struct lttng_trigger_ht_element
*trigger_ht_element
;
332 const char *key_name
;
333 enum lttng_trigger_status status
;
334 const struct lttng_credentials
*key_creds
;
335 const struct lttng_credentials
*node_creds
;
336 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
338 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
341 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
342 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
344 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
345 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
347 if (strcmp(name
, key_name
) != 0) {
352 key_creds
= lttng_trigger_get_credentials(trigger_key
);
353 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
355 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
362 unsigned long hash_channel_key(struct channel_key
*key
)
364 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
365 unsigned long domain_hash
= hash_key_ulong(
366 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
368 return key_hash
^ domain_hash
;
372 unsigned long hash_client_socket(int socket
)
374 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
378 unsigned long hash_client_id(notification_client_id id
)
380 return hash_key_u64(&id
, lttng_ht_seed
);
384 * Get the type of object to which a given condition applies. Bindings let
385 * the notification system evaluate a trigger's condition when a given
386 * object's state is updated.
388 * For instance, a condition bound to a channel will be evaluated everytime
389 * the channel's state is changed by a channel monitoring sample.
392 enum lttng_object_type
get_condition_binding_object(
393 const struct lttng_condition
*condition
)
395 switch (lttng_condition_get_type(condition
)) {
396 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
397 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
398 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
399 return LTTNG_OBJECT_TYPE_CHANNEL
;
400 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
401 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
402 return LTTNG_OBJECT_TYPE_SESSION
;
403 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
404 return LTTNG_OBJECT_TYPE_NONE
;
406 return LTTNG_OBJECT_TYPE_UNKNOWN
;
411 void free_channel_info_rcu(struct rcu_head
*node
)
413 free(caa_container_of(node
, struct channel_info
, rcu_node
));
417 void channel_info_destroy(struct channel_info
*channel_info
)
423 if (channel_info
->session_info
) {
424 session_info_remove_channel(channel_info
->session_info
,
426 session_info_put(channel_info
->session_info
);
428 if (channel_info
->name
) {
429 free(channel_info
->name
);
431 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
435 void free_session_info_rcu(struct rcu_head
*node
)
437 free(caa_container_of(node
, struct session_info
, rcu_node
));
440 /* Don't call directly, use the ref-counting mechanism. */
442 void session_info_destroy(void *_data
)
444 struct session_info
*session_info
= _data
;
447 assert(session_info
);
448 if (session_info
->channel_infos_ht
) {
449 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
451 ERR("[notification-thread] Failed to destroy channel information hash table");
454 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
457 cds_lfht_del(session_info
->sessions_ht
,
458 &session_info
->sessions_ht_node
);
460 free(session_info
->name
);
461 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
465 void session_info_get(struct session_info
*session_info
)
470 lttng_ref_get(&session_info
->ref
);
474 void session_info_put(struct session_info
*session_info
)
479 lttng_ref_put(&session_info
->ref
);
483 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
484 struct lttng_session_trigger_list
*trigger_list
,
485 struct cds_lfht
*sessions_ht
)
487 struct session_info
*session_info
;
491 session_info
= zmalloc(sizeof(*session_info
));
495 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
497 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
498 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
499 if (!session_info
->channel_infos_ht
) {
503 cds_lfht_node_init(&session_info
->sessions_ht_node
);
504 session_info
->name
= strdup(name
);
505 if (!session_info
->name
) {
508 session_info
->uid
= uid
;
509 session_info
->gid
= gid
;
510 session_info
->trigger_list
= trigger_list
;
511 session_info
->sessions_ht
= sessions_ht
;
515 session_info_put(session_info
);
520 void session_info_add_channel(struct session_info
*session_info
,
521 struct channel_info
*channel_info
)
524 cds_lfht_add(session_info
->channel_infos_ht
,
525 hash_channel_key(&channel_info
->key
),
526 &channel_info
->session_info_channels_ht_node
);
531 void session_info_remove_channel(struct session_info
*session_info
,
532 struct channel_info
*channel_info
)
535 cds_lfht_del(session_info
->channel_infos_ht
,
536 &channel_info
->session_info_channels_ht_node
);
541 struct channel_info
*channel_info_create(const char *channel_name
,
542 struct channel_key
*channel_key
, uint64_t channel_capacity
,
543 struct session_info
*session_info
)
545 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
551 cds_lfht_node_init(&channel_info
->channels_ht_node
);
552 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
553 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
554 channel_info
->capacity
= channel_capacity
;
556 channel_info
->name
= strdup(channel_name
);
557 if (!channel_info
->name
) {
562 * Set the references between session and channel infos:
563 * - channel_info holds a strong reference to session_info
564 * - session_info holds a weak reference to channel_info
566 session_info_get(session_info
);
567 session_info_add_channel(session_info
, channel_info
);
568 channel_info
->session_info
= session_info
;
572 channel_info_destroy(channel_info
);
577 bool notification_client_list_get(struct notification_client_list
*list
)
579 return urcu_ref_get_unless_zero(&list
->ref
);
583 void free_notification_client_list_rcu(struct rcu_head
*node
)
585 free(caa_container_of(node
, struct notification_client_list
,
590 void notification_client_list_release(struct urcu_ref
*list_ref
)
592 struct notification_client_list
*list
=
593 container_of(list_ref
, typeof(*list
), ref
);
594 struct notification_client_list_element
*client_list_element
, *tmp
;
596 if (list
->notification_trigger_clients_ht
) {
598 cds_lfht_del(list
->notification_trigger_clients_ht
,
599 &list
->notification_trigger_clients_ht_node
);
601 list
->notification_trigger_clients_ht
= NULL
;
603 cds_list_for_each_entry_safe(client_list_element
, tmp
,
605 free(client_list_element
);
607 pthread_mutex_destroy(&list
->lock
);
608 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
612 struct notification_client_list
*notification_client_list_create(
613 const struct lttng_trigger
*trigger
)
615 struct notification_client_list
*client_list
=
616 zmalloc(sizeof(*client_list
));
621 pthread_mutex_init(&client_list
->lock
, NULL
);
622 urcu_ref_init(&client_list
->ref
);
623 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
624 CDS_INIT_LIST_HEAD(&client_list
->list
);
625 client_list
->trigger
= trigger
;
631 void publish_notification_client_list(
632 struct notification_thread_state
*state
,
633 struct notification_client_list
*list
)
635 const struct lttng_condition
*condition
=
636 lttng_trigger_get_const_condition(list
->trigger
);
638 assert(!list
->notification_trigger_clients_ht
);
639 notification_client_list_get(list
);
641 list
->notification_trigger_clients_ht
=
642 state
->notification_trigger_clients_ht
;
645 cds_lfht_add(state
->notification_trigger_clients_ht
,
646 lttng_condition_hash(condition
),
647 &list
->notification_trigger_clients_ht_node
);
652 void notification_client_list_put(struct notification_client_list
*list
)
657 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
660 /* Provides a reference to the returned list. */
662 struct notification_client_list
*get_client_list_from_condition(
663 struct notification_thread_state
*state
,
664 const struct lttng_condition
*condition
)
666 struct cds_lfht_node
*node
;
667 struct cds_lfht_iter iter
;
668 struct notification_client_list
*list
= NULL
;
671 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
672 lttng_condition_hash(condition
),
673 match_client_list_condition
,
676 node
= cds_lfht_iter_get_node(&iter
);
678 list
= container_of(node
, struct notification_client_list
,
679 notification_trigger_clients_ht_node
);
680 list
= notification_client_list_get(list
) ? list
: NULL
;
688 int evaluate_channel_condition_for_client(
689 const struct lttng_condition
*condition
,
690 struct notification_thread_state
*state
,
691 struct lttng_evaluation
**evaluation
,
692 uid_t
*session_uid
, gid_t
*session_gid
)
695 struct cds_lfht_iter iter
;
696 struct cds_lfht_node
*node
;
697 struct channel_info
*channel_info
= NULL
;
698 struct channel_key
*channel_key
= NULL
;
699 struct channel_state_sample
*last_sample
= NULL
;
700 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
704 /* Find the channel associated with the condition. */
705 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
706 channel_trigger_list
, channel_triggers_ht_node
) {
707 struct lttng_trigger_list_element
*element
;
709 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
710 const struct lttng_condition
*current_condition
=
711 lttng_trigger_get_const_condition(
714 assert(current_condition
);
715 if (!lttng_condition_is_equal(condition
,
716 current_condition
)) {
720 /* Found the trigger, save the channel key. */
721 channel_key
= &channel_trigger_list
->channel_key
;
725 /* The channel key was found stop iteration. */
731 /* No channel found; normal exit. */
732 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
737 /* Fetch channel info for the matching channel. */
738 cds_lfht_lookup(state
->channels_ht
,
739 hash_channel_key(channel_key
),
743 node
= cds_lfht_iter_get_node(&iter
);
745 channel_info
= caa_container_of(node
, struct channel_info
,
748 /* Retrieve the channel's last sample, if it exists. */
749 cds_lfht_lookup(state
->channel_state_ht
,
750 hash_channel_key(channel_key
),
751 match_channel_state_sample
,
754 node
= cds_lfht_iter_get_node(&iter
);
756 last_sample
= caa_container_of(node
,
757 struct channel_state_sample
,
758 channel_state_ht_node
);
760 /* Nothing to evaluate, no sample was ever taken. Normal exit */
761 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
766 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
768 0, channel_info
->session_info
->consumed_data_size
,
771 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
775 *session_uid
= channel_info
->session_info
->uid
;
776 *session_gid
= channel_info
->session_info
->gid
;
783 const char *get_condition_session_name(const struct lttng_condition
*condition
)
785 const char *session_name
= NULL
;
786 enum lttng_condition_status status
;
788 switch (lttng_condition_get_type(condition
)) {
789 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
790 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
791 status
= lttng_condition_buffer_usage_get_session_name(
792 condition
, &session_name
);
794 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
795 status
= lttng_condition_session_consumed_size_get_session_name(
796 condition
, &session_name
);
798 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
799 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
800 status
= lttng_condition_session_rotation_get_session_name(
801 condition
, &session_name
);
806 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
807 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
815 int evaluate_session_condition_for_client(
816 const struct lttng_condition
*condition
,
817 struct notification_thread_state
*state
,
818 struct lttng_evaluation
**evaluation
,
819 uid_t
*session_uid
, gid_t
*session_gid
)
822 struct cds_lfht_iter iter
;
823 struct cds_lfht_node
*node
;
824 const char *session_name
;
825 struct session_info
*session_info
= NULL
;
828 session_name
= get_condition_session_name(condition
);
830 /* Find the session associated with the trigger. */
831 cds_lfht_lookup(state
->sessions_ht
,
832 hash_key_str(session_name
, lttng_ht_seed
),
836 node
= cds_lfht_iter_get_node(&iter
);
838 DBG("[notification-thread] No known session matching name \"%s\"",
844 session_info
= caa_container_of(node
, struct session_info
,
846 session_info_get(session_info
);
849 * Evaluation is performed in-line here since only one type of
850 * session-bound condition is handled for the moment.
852 switch (lttng_condition_get_type(condition
)) {
853 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
854 if (!session_info
->rotation
.ongoing
) {
856 goto end_session_put
;
859 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
860 session_info
->rotation
.id
);
863 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
866 goto end_session_put
;
872 goto end_session_put
;
875 *session_uid
= session_info
->uid
;
876 *session_gid
= session_info
->gid
;
879 session_info_put(session_info
);
886 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
887 const struct lttng_condition
*condition
,
888 struct notification_client
*client
,
889 struct notification_thread_state
*state
)
892 struct lttng_evaluation
*evaluation
= NULL
;
893 struct notification_client_list client_list
= {
894 .lock
= PTHREAD_MUTEX_INITIALIZER
,
896 struct notification_client_list_element client_list_element
= { 0 };
897 uid_t object_uid
= 0;
898 gid_t object_gid
= 0;
905 switch (get_condition_binding_object(condition
)) {
906 case LTTNG_OBJECT_TYPE_SESSION
:
907 ret
= evaluate_session_condition_for_client(condition
, state
,
908 &evaluation
, &object_uid
, &object_gid
);
910 case LTTNG_OBJECT_TYPE_CHANNEL
:
911 ret
= evaluate_channel_condition_for_client(condition
, state
,
912 &evaluation
, &object_uid
, &object_gid
);
914 case LTTNG_OBJECT_TYPE_NONE
:
915 DBG("[notification-thread] Newly subscribed-to condition not binded to object, nothing to evaluate");
918 case LTTNG_OBJECT_TYPE_UNKNOWN
:
928 /* Evaluation yielded nothing. Normal exit. */
929 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
935 * Create a temporary client list with the client currently
938 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
939 CDS_INIT_LIST_HEAD(&client_list
.list
);
940 client_list
.trigger
= trigger
;
942 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
943 client_list_element
.client
= client
;
944 cds_list_add(&client_list_element
.node
, &client_list
.list
);
946 /* Send evaluation result to the newly-subscribed client. */
947 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
948 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
949 state
, object_uid
, object_gid
);
956 int notification_thread_client_subscribe(struct notification_client
*client
,
957 struct lttng_condition
*condition
,
958 struct notification_thread_state
*state
,
959 enum lttng_notification_channel_status
*_status
)
962 struct notification_client_list
*client_list
= NULL
;
963 struct lttng_condition_list_element
*condition_list_element
= NULL
;
964 struct notification_client_list_element
*client_list_element
= NULL
;
965 enum lttng_notification_channel_status status
=
966 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
969 * Ensure that the client has not already subscribed to this condition
972 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
973 if (lttng_condition_is_equal(condition_list_element
->condition
,
975 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
980 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
981 if (!condition_list_element
) {
985 client_list_element
= zmalloc(sizeof(*client_list_element
));
986 if (!client_list_element
) {
992 * Add the newly-subscribed condition to the client's subscription list.
994 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
995 condition_list_element
->condition
= condition
;
996 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
998 client_list
= get_client_list_from_condition(state
, condition
);
1001 * No notification-emiting trigger registered with this
1002 * condition. We don't evaluate the condition right away
1003 * since this trigger is not registered yet.
1005 free(client_list_element
);
1010 * The condition to which the client just subscribed is evaluated
1011 * at this point so that conditions that are already TRUE result
1012 * in a notification being sent out.
1014 * The client_list's trigger is used without locking the list itself.
1015 * This is correct since the list doesn't own the trigger and the
1016 * object is immutable.
1018 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1020 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1022 free(client_list_element
);
1027 * Add the client to the list of clients interested in a given trigger
1028 * if a "notification" trigger with a corresponding condition was
1031 client_list_element
->client
= client
;
1032 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1034 pthread_mutex_lock(&client_list
->lock
);
1035 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1036 pthread_mutex_unlock(&client_list
->lock
);
1042 notification_client_list_put(client_list
);
1046 free(condition_list_element
);
1047 free(client_list_element
);
1052 int notification_thread_client_unsubscribe(
1053 struct notification_client
*client
,
1054 struct lttng_condition
*condition
,
1055 struct notification_thread_state
*state
,
1056 enum lttng_notification_channel_status
*_status
)
1058 struct notification_client_list
*client_list
;
1059 struct lttng_condition_list_element
*condition_list_element
,
1061 struct notification_client_list_element
*client_list_element
,
1063 bool condition_found
= false;
1064 enum lttng_notification_channel_status status
=
1065 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1067 /* Remove the condition from the client's condition list. */
1068 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1069 &client
->condition_list
, node
) {
1070 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1075 cds_list_del(&condition_list_element
->node
);
1077 * The caller may be iterating on the client's conditions to
1078 * tear down a client's connection. In this case, the condition
1079 * will be destroyed at the end.
1081 if (condition
!= condition_list_element
->condition
) {
1082 lttng_condition_destroy(
1083 condition_list_element
->condition
);
1085 free(condition_list_element
);
1086 condition_found
= true;
1090 if (!condition_found
) {
1091 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1096 * Remove the client from the list of clients interested the trigger
1097 * matching the condition.
1099 client_list
= get_client_list_from_condition(state
, condition
);
1104 pthread_mutex_lock(&client_list
->lock
);
1105 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1106 &client_list
->list
, node
) {
1107 if (client_list_element
->client
->id
!= client
->id
) {
1110 cds_list_del(&client_list_element
->node
);
1111 free(client_list_element
);
1114 pthread_mutex_unlock(&client_list
->lock
);
1115 notification_client_list_put(client_list
);
1118 lttng_condition_destroy(condition
);
1126 void free_notification_client_rcu(struct rcu_head
*node
)
1128 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1132 void notification_client_destroy(struct notification_client
*client
,
1133 struct notification_thread_state
*state
)
1140 * The client object is not reachable by other threads, no need to lock
1143 if (client
->socket
>= 0) {
1144 (void) lttcomm_close_unix_sock(client
->socket
);
1145 client
->socket
= -1;
1147 client
->communication
.active
= false;
1148 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1149 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1150 pthread_mutex_destroy(&client
->lock
);
1151 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1155 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1159 struct notification_client
*get_client_from_socket(int socket
,
1160 struct notification_thread_state
*state
)
1162 struct cds_lfht_iter iter
;
1163 struct cds_lfht_node
*node
;
1164 struct notification_client
*client
= NULL
;
1166 cds_lfht_lookup(state
->client_socket_ht
,
1167 hash_client_socket(socket
),
1168 match_client_socket
,
1169 (void *) (unsigned long) socket
,
1171 node
= cds_lfht_iter_get_node(&iter
);
1176 client
= caa_container_of(node
, struct notification_client
,
1177 client_socket_ht_node
);
1183 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1187 struct notification_client
*get_client_from_id(notification_client_id id
,
1188 struct notification_thread_state
*state
)
1190 struct cds_lfht_iter iter
;
1191 struct cds_lfht_node
*node
;
1192 struct notification_client
*client
= NULL
;
1194 cds_lfht_lookup(state
->client_id_ht
,
1199 node
= cds_lfht_iter_get_node(&iter
);
1204 client
= caa_container_of(node
, struct notification_client
,
1211 bool buffer_usage_condition_applies_to_channel(
1212 const struct lttng_condition
*condition
,
1213 const struct channel_info
*channel_info
)
1215 enum lttng_condition_status status
;
1216 enum lttng_domain_type condition_domain
;
1217 const char *condition_session_name
= NULL
;
1218 const char *condition_channel_name
= NULL
;
1220 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1222 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1223 if (channel_info
->key
.domain
!= condition_domain
) {
1227 status
= lttng_condition_buffer_usage_get_session_name(
1228 condition
, &condition_session_name
);
1229 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1231 status
= lttng_condition_buffer_usage_get_channel_name(
1232 condition
, &condition_channel_name
);
1233 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1235 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1238 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1248 bool session_consumed_size_condition_applies_to_channel(
1249 const struct lttng_condition
*condition
,
1250 const struct channel_info
*channel_info
)
1252 enum lttng_condition_status status
;
1253 const char *condition_session_name
= NULL
;
1255 status
= lttng_condition_session_consumed_size_get_session_name(
1256 condition
, &condition_session_name
);
1257 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1259 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1269 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1270 const struct channel_info
*channel_info
)
1272 const struct lttng_condition
*condition
;
1273 bool trigger_applies
;
1275 condition
= lttng_trigger_get_const_condition(trigger
);
1280 switch (lttng_condition_get_type(condition
)) {
1281 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1282 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1283 trigger_applies
= buffer_usage_condition_applies_to_channel(
1284 condition
, channel_info
);
1286 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1287 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1288 condition
, channel_info
);
1294 return trigger_applies
;
1300 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1301 struct notification_client
*client
)
1303 bool applies
= false;
1304 struct lttng_condition_list_element
*condition_list_element
;
1306 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1308 applies
= lttng_condition_is_equal(
1309 condition_list_element
->condition
,
1310 lttng_trigger_get_condition(trigger
));
1318 /* Must be called with RCU read lock held. */
1320 struct lttng_session_trigger_list
*get_session_trigger_list(
1321 struct notification_thread_state
*state
,
1322 const char *session_name
)
1324 struct lttng_session_trigger_list
*list
= NULL
;
1325 struct cds_lfht_node
*node
;
1326 struct cds_lfht_iter iter
;
1328 cds_lfht_lookup(state
->session_triggers_ht
,
1329 hash_key_str(session_name
, lttng_ht_seed
),
1330 match_session_trigger_list
,
1333 node
= cds_lfht_iter_get_node(&iter
);
1336 * Not an error, the list of triggers applying to that session
1337 * will be initialized when the session is created.
1339 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1344 list
= caa_container_of(node
,
1345 struct lttng_session_trigger_list
,
1346 session_triggers_ht_node
);
1352 * Allocate an empty lttng_session_trigger_list for the session named
1355 * No ownership of 'session_name' is assumed by the session trigger list.
1356 * It is the caller's responsability to ensure the session name is alive
1357 * for as long as this list is.
1360 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1361 const char *session_name
,
1362 struct cds_lfht
*session_triggers_ht
)
1364 struct lttng_session_trigger_list
*list
;
1366 list
= zmalloc(sizeof(*list
));
1370 list
->session_name
= session_name
;
1371 CDS_INIT_LIST_HEAD(&list
->list
);
1372 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1373 list
->session_triggers_ht
= session_triggers_ht
;
1376 /* Publish the list through the session_triggers_ht. */
1377 cds_lfht_add(session_triggers_ht
,
1378 hash_key_str(session_name
, lttng_ht_seed
),
1379 &list
->session_triggers_ht_node
);
1386 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1388 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1393 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1395 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1397 /* Empty the list element by element, and then free the list itself. */
1398 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1399 &list
->list
, node
) {
1400 cds_list_del(&trigger_list_element
->node
);
1401 free(trigger_list_element
);
1404 /* Unpublish the list from the session_triggers_ht. */
1405 cds_lfht_del(list
->session_triggers_ht
,
1406 &list
->session_triggers_ht_node
);
1408 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1412 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1413 struct lttng_trigger
*trigger
)
1416 struct lttng_trigger_list_element
*new_element
=
1417 zmalloc(sizeof(*new_element
));
1423 CDS_INIT_LIST_HEAD(&new_element
->node
);
1424 new_element
->trigger
= trigger
;
1425 cds_list_add(&new_element
->node
, &list
->list
);
1431 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1432 const char *session_name
)
1434 bool applies
= false;
1435 const struct lttng_condition
*condition
;
1437 condition
= lttng_trigger_get_const_condition(trigger
);
1438 switch (lttng_condition_get_type(condition
)) {
1439 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1440 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1442 enum lttng_condition_status condition_status
;
1443 const char *condition_session_name
;
1445 condition_status
= lttng_condition_session_rotation_get_session_name(
1446 condition
, &condition_session_name
);
1447 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1448 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1452 assert(condition_session_name
);
1453 applies
= !strcmp(condition_session_name
, session_name
);
1464 * Allocate and initialize an lttng_session_trigger_list which contains
1465 * all triggers that apply to the session named 'session_name'.
1467 * No ownership of 'session_name' is assumed by the session trigger list.
1468 * It is the caller's responsability to ensure the session name is alive
1469 * for as long as this list is.
1472 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1473 const struct notification_thread_state
*state
,
1474 const char *session_name
)
1476 int trigger_count
= 0;
1477 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1478 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1479 struct cds_lfht_iter iter
;
1481 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1482 state
->session_triggers_ht
);
1484 /* Add all triggers applying to the session named 'session_name'. */
1485 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1489 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1494 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1495 trigger_ht_element
->trigger
);
1503 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1505 return session_trigger_list
;
1507 lttng_session_trigger_list_destroy(session_trigger_list
);
1512 struct session_info
*find_or_create_session_info(
1513 struct notification_thread_state
*state
,
1514 const char *name
, uid_t uid
, gid_t gid
)
1516 struct session_info
*session
= NULL
;
1517 struct cds_lfht_node
*node
;
1518 struct cds_lfht_iter iter
;
1519 struct lttng_session_trigger_list
*trigger_list
;
1522 cds_lfht_lookup(state
->sessions_ht
,
1523 hash_key_str(name
, lttng_ht_seed
),
1527 node
= cds_lfht_iter_get_node(&iter
);
1529 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1531 session
= caa_container_of(node
, struct session_info
,
1533 assert(session
->uid
== uid
);
1534 assert(session
->gid
== gid
);
1535 session_info_get(session
);
1539 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1540 if (!trigger_list
) {
1544 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1545 state
->sessions_ht
);
1547 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1549 lttng_session_trigger_list_destroy(trigger_list
);
1552 trigger_list
= NULL
;
1554 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1555 &session
->sessions_ht_node
);
1561 session_info_put(session
);
1566 int handle_notification_thread_command_add_channel(
1567 struct notification_thread_state
*state
,
1568 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1569 const char *channel_name
, enum lttng_domain_type channel_domain
,
1570 uint64_t channel_key_int
, uint64_t channel_capacity
,
1571 enum lttng_error_code
*cmd_result
)
1573 struct cds_list_head trigger_list
;
1574 struct channel_info
*new_channel_info
= NULL
;
1575 struct channel_key channel_key
= {
1576 .key
= channel_key_int
,
1577 .domain
= channel_domain
,
1579 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1580 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1581 int trigger_count
= 0;
1582 struct cds_lfht_iter iter
;
1583 struct session_info
*session_info
= NULL
;
1585 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1586 channel_name
, session_name
, channel_key_int
,
1587 lttng_domain_type_str(channel_domain
));
1589 CDS_INIT_LIST_HEAD(&trigger_list
);
1591 session_info
= find_or_create_session_info(state
, session_name
,
1592 session_uid
, session_gid
);
1593 if (!session_info
) {
1594 /* Allocation error or an internal error occurred. */
1598 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1599 channel_capacity
, session_info
);
1600 if (!new_channel_info
) {
1605 /* Build a list of all triggers applying to the new channel. */
1606 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1608 struct lttng_trigger_list_element
*new_element
;
1610 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1611 new_channel_info
)) {
1615 new_element
= zmalloc(sizeof(*new_element
));
1620 CDS_INIT_LIST_HEAD(&new_element
->node
);
1621 new_element
->trigger
= trigger_ht_element
->trigger
;
1622 cds_list_add(&new_element
->node
, &trigger_list
);
1627 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1629 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1630 if (!channel_trigger_list
) {
1633 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1634 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1635 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1636 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1639 /* Add channel to the channel_ht which owns the channel_infos. */
1640 cds_lfht_add(state
->channels_ht
,
1641 hash_channel_key(&new_channel_info
->key
),
1642 &new_channel_info
->channels_ht_node
);
1644 * Add the list of triggers associated with this channel to the
1645 * channel_triggers_ht.
1647 cds_lfht_add(state
->channel_triggers_ht
,
1648 hash_channel_key(&new_channel_info
->key
),
1649 &channel_trigger_list
->channel_triggers_ht_node
);
1651 session_info_put(session_info
);
1652 *cmd_result
= LTTNG_OK
;
1655 channel_info_destroy(new_channel_info
);
1656 session_info_put(session_info
);
1661 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1663 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1668 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1670 free(caa_container_of(node
, struct channel_state_sample
,
1675 int handle_notification_thread_command_remove_channel(
1676 struct notification_thread_state
*state
,
1677 uint64_t channel_key
, enum lttng_domain_type domain
,
1678 enum lttng_error_code
*cmd_result
)
1680 struct cds_lfht_node
*node
;
1681 struct cds_lfht_iter iter
;
1682 struct lttng_channel_trigger_list
*trigger_list
;
1683 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1684 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1685 struct channel_info
*channel_info
;
1687 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1688 channel_key
, lttng_domain_type_str(domain
));
1692 cds_lfht_lookup(state
->channel_triggers_ht
,
1693 hash_channel_key(&key
),
1694 match_channel_trigger_list
,
1697 node
= cds_lfht_iter_get_node(&iter
);
1699 * There is a severe internal error if we are being asked to remove a
1700 * channel that doesn't exist.
1703 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1707 /* Free the list of triggers associated with this channel. */
1708 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1709 channel_triggers_ht_node
);
1710 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1711 &trigger_list
->list
, node
) {
1712 cds_list_del(&trigger_list_element
->node
);
1713 free(trigger_list_element
);
1715 cds_lfht_del(state
->channel_triggers_ht
, node
);
1716 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1718 /* Free sampled channel state. */
1719 cds_lfht_lookup(state
->channel_state_ht
,
1720 hash_channel_key(&key
),
1721 match_channel_state_sample
,
1724 node
= cds_lfht_iter_get_node(&iter
);
1726 * This is expected to be NULL if the channel is destroyed before we
1727 * received a sample.
1730 struct channel_state_sample
*sample
= caa_container_of(node
,
1731 struct channel_state_sample
,
1732 channel_state_ht_node
);
1734 cds_lfht_del(state
->channel_state_ht
, node
);
1735 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1738 /* Remove the channel from the channels_ht and free it. */
1739 cds_lfht_lookup(state
->channels_ht
,
1740 hash_channel_key(&key
),
1744 node
= cds_lfht_iter_get_node(&iter
);
1746 channel_info
= caa_container_of(node
, struct channel_info
,
1748 cds_lfht_del(state
->channels_ht
, node
);
1749 channel_info_destroy(channel_info
);
1752 *cmd_result
= LTTNG_OK
;
1757 int handle_notification_thread_command_session_rotation(
1758 struct notification_thread_state
*state
,
1759 enum notification_thread_command_type cmd_type
,
1760 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1761 uint64_t trace_archive_chunk_id
,
1762 struct lttng_trace_archive_location
*location
,
1763 enum lttng_error_code
*_cmd_result
)
1766 enum lttng_error_code cmd_result
= LTTNG_OK
;
1767 struct lttng_session_trigger_list
*trigger_list
;
1768 struct lttng_trigger_list_element
*trigger_list_element
;
1769 struct session_info
*session_info
;
1770 const struct lttng_credentials session_creds
= {
1771 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1772 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1777 session_info
= find_or_create_session_info(state
, session_name
,
1778 session_uid
, session_gid
);
1779 if (!session_info
) {
1780 /* Allocation error or an internal error occurred. */
1782 cmd_result
= LTTNG_ERR_NOMEM
;
1786 session_info
->rotation
.ongoing
=
1787 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1788 session_info
->rotation
.id
= trace_archive_chunk_id
;
1789 trigger_list
= get_session_trigger_list(state
, session_name
);
1790 if (!trigger_list
) {
1791 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1796 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1798 const struct lttng_condition
*condition
;
1799 struct lttng_trigger
*trigger
;
1800 struct notification_client_list
*client_list
;
1801 struct lttng_evaluation
*evaluation
= NULL
;
1802 enum lttng_condition_type condition_type
;
1803 enum action_executor_status executor_status
;
1805 trigger
= trigger_list_element
->trigger
;
1806 condition
= lttng_trigger_get_const_condition(trigger
);
1808 condition_type
= lttng_condition_get_type(condition
);
1810 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1811 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1813 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1814 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1818 client_list
= get_client_list_from_condition(state
, condition
);
1819 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1820 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1821 trace_archive_chunk_id
);
1823 evaluation
= lttng_evaluation_session_rotation_completed_create(
1824 trace_archive_chunk_id
, location
);
1828 /* Internal error */
1830 cmd_result
= LTTNG_ERR_UNK
;
1835 * Ownership of `evaluation` transferred to the action executor
1836 * no matter the result.
1838 executor_status
= action_executor_enqueue(state
->executor
,
1839 trigger
, evaluation
, &session_creds
,
1842 switch (executor_status
) {
1843 case ACTION_EXECUTOR_STATUS_OK
:
1845 case ACTION_EXECUTOR_STATUS_ERROR
:
1846 case ACTION_EXECUTOR_STATUS_INVALID
:
1848 * TODO Add trigger identification (name/id) when
1849 * it is added to the API.
1851 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1854 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1856 * TODO Add trigger identification (name/id) when
1857 * it is added to the API.
1859 * Not a fatal error.
1861 WARN("No space left when enqueuing action associated with session rotation trigger");
1869 notification_client_list_put(client_list
);
1870 if (caa_unlikely(ret
)) {
1875 session_info_put(session_info
);
1876 *_cmd_result
= cmd_result
;
1882 int handle_notification_thread_command_add_application(
1883 struct notification_thread_handle
*handle
,
1884 struct notification_thread_state
*state
,
1885 int read_side_trigger_event_application_pipe
,
1886 enum lttng_domain_type domain_type
,
1887 enum lttng_error_code
*_cmd_result
)
1890 enum lttng_error_code cmd_result
= LTTNG_OK
;
1891 struct notification_event_trigger_source_element
*element
= NULL
;
1893 element
= zmalloc(sizeof(*element
));
1895 cmd_result
= LTTNG_ERR_NOMEM
;
1900 CDS_INIT_LIST_HEAD(&element
->node
);
1901 element
->fd
= read_side_trigger_event_application_pipe
;
1902 element
->domain
= domain_type
;
1904 pthread_mutex_lock(&handle
->event_trigger_sources
.lock
);
1905 cds_list_add(&element
->node
, &handle
->event_trigger_sources
.list
);
1906 pthread_mutex_unlock(&handle
->event_trigger_sources
.lock
);
1908 /* TODO: remove on failure to add to list? */
1910 /* Adding the read side pipe to the event poll */
1911 ret
= lttng_poll_add(&state
->events
,
1912 read_side_trigger_event_application_pipe
,
1913 LPOLLIN
| LPOLLERR
);
1915 DBG3("[notification-thread] Adding application event source from fd: %d", read_side_trigger_event_application_pipe
);
1917 /* TODO: what should be the value of cmd_result??? */
1918 ERR("[notification-thread] Failed to add event source pipe fd to pollset");
1923 *_cmd_result
= cmd_result
;
1928 int handle_notification_thread_command_remove_application(
1929 struct notification_thread_handle
*handle
,
1930 struct notification_thread_state
*state
,
1931 int read_side_trigger_event_application_pipe
,
1932 enum lttng_error_code
*_cmd_result
)
1935 enum lttng_error_code cmd_result
= LTTNG_OK
;
1936 /* Used for logging */
1937 enum lttng_domain_type domain
= LTTNG_DOMAIN_NONE
;
1939 /* TODO: missing a lock propably to revisit */
1940 struct notification_event_trigger_source_element
*source_element
= NULL
, *tmp
;
1942 cds_list_for_each_entry_safe(source_element
, tmp
,
1943 &handle
->event_trigger_sources
.list
, node
) {
1944 if (source_element
->fd
!= read_side_trigger_event_application_pipe
) {
1948 cds_list_del(&source_element
->node
);
1952 /* It should always be found */
1953 assert(source_element
);
1955 DBG3("[notification-thread] Removing application event source from fd: %d of domain: %s",
1956 read_side_trigger_event_application_pipe
,
1957 lttng_domain_type_str(domain
));
1958 free(source_element
);
1960 /* Removing the read side pipe to the event poll */
1961 ret
= lttng_poll_del(&state
->events
,
1962 read_side_trigger_event_application_pipe
);
1964 /* TODO: what should be the value of cmd_result??? */
1965 ERR("[notification-thread] Failed to remove event source pipe fd from pollset");
1970 *_cmd_result
= cmd_result
;
1974 static int handle_notification_thread_command_get_tokens(
1975 struct notification_thread_handle
*handle
,
1976 struct notification_thread_state
*state
,
1977 struct lttng_triggers
**triggers
,
1978 enum lttng_error_code
*_cmd_result
)
1981 enum lttng_error_code cmd_result
= LTTNG_OK
;
1982 struct cds_lfht_iter iter
;
1983 struct notification_trigger_tokens_ht_element
*element
;
1984 struct lttng_triggers
*local_triggers
= NULL
;
1986 local_triggers
= lttng_triggers_create();
1987 if (!local_triggers
) {
1988 cmd_result
= LTTNG_ERR_NOMEM
;
1993 cds_lfht_for_each_entry (
1994 state
->trigger_tokens_ht
, &iter
, element
, node
) {
1995 ret
= lttng_triggers_add(local_triggers
, element
->trigger
);
1997 cmd_result
= LTTNG_ERR_FATAL
;
2005 /* Passing ownership up */
2006 *triggers
= local_triggers
;
2007 local_triggers
= NULL
;
2011 lttng_triggers_destroy(local_triggers
);
2012 *_cmd_result
= cmd_result
;
2017 int trigger_update_error_count(struct lttng_trigger
*trigger
)
2020 uint64_t error_count
= 0;
2021 enum trigger_error_accounting_status status
;
2023 status
= trigger_error_accounting_get_count(trigger
, &error_count
);
2024 if (status
!= TRIGGER_ERROR_ACCOUNTING_STATUS_OK
) {
2025 ERR("Error getting trigger error count");
2028 lttng_trigger_set_error_count(trigger
, error_count
);
2032 static int handle_notification_thread_command_list_triggers(
2033 struct notification_thread_handle
*handle
,
2034 struct notification_thread_state
*state
,
2036 struct lttng_triggers
**triggers
,
2037 enum lttng_error_code
*_cmd_result
)
2040 enum lttng_error_code cmd_result
= LTTNG_OK
;
2041 struct cds_lfht_iter iter
;
2042 struct lttng_trigger_ht_element
*trigger_ht_element
;
2043 struct lttng_triggers
*local_triggers
= NULL
;
2044 const struct lttng_credentials
*creds
;
2047 unsigned long count
;
2050 cds_lfht_count_nodes(state
->triggers_ht
, &scb
, &count
, &sca
);
2052 local_triggers
= lttng_triggers_create();
2053 if (!local_triggers
) {
2054 cmd_result
= LTTNG_ERR_NOMEM
;
2058 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
,
2059 trigger_ht_element
, node
) {
2061 * Only return the trigger for which the requestion client have
2063 * Root user have visibility over all triggers.
2065 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2066 if (uid
!= lttng_credentials_get_uid(creds
) && uid
!= 0) {
2070 ret
= trigger_update_error_count(trigger_ht_element
->trigger
);
2073 ret
= lttng_triggers_add(local_triggers
, trigger_ht_element
->trigger
);
2082 /* Passing ownership up */
2083 *triggers
= local_triggers
;
2084 local_triggers
= NULL
;
2088 lttng_triggers_destroy(local_triggers
);
2089 *_cmd_result
= cmd_result
;
2094 int condition_is_supported(struct lttng_condition
*condition
)
2098 switch (lttng_condition_get_type(condition
)) {
2099 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2100 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2102 enum lttng_domain_type domain
;
2104 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2111 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2117 * Older kernel tracers don't expose the API to monitor their
2118 * buffers. Therefore, we reject triggers that require that
2119 * mechanism to be available to be evaluated.
2121 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
2124 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
2127 * Check for kernel support.
2128 * Check for ust support ??
2141 int action_is_supported(struct lttng_action
*action
)
2145 switch (lttng_action_get_type(action
)) {
2146 case LTTNG_ACTION_TYPE_NOTIFY
:
2147 case LTTNG_ACTION_TYPE_START_SESSION
:
2148 case LTTNG_ACTION_TYPE_STOP_SESSION
:
2149 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
2150 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
2152 /* TODO validate that this is true for kernel in regards to
2153 * rotation and snapshot. Start stop is not a problem notify
2156 /* For now all type of actions are supported */
2160 case LTTNG_ACTION_TYPE_GROUP
:
2162 /* TODO: Iterate over all internal actions and validate that
2163 * they are supported
2176 /* Must be called with RCU read lock held. */
2178 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2179 struct notification_thread_state
*state
)
2182 const struct lttng_condition
*condition
;
2183 const char *session_name
;
2184 struct lttng_session_trigger_list
*trigger_list
;
2186 condition
= lttng_trigger_get_const_condition(trigger
);
2187 switch (lttng_condition_get_type(condition
)) {
2188 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2189 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2191 enum lttng_condition_status status
;
2193 status
= lttng_condition_session_rotation_get_session_name(
2194 condition
, &session_name
);
2195 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2196 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2207 trigger_list
= get_session_trigger_list(state
, session_name
);
2208 if (!trigger_list
) {
2209 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2215 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2217 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2222 /* Must be called with RCU read lock held. */
2224 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2225 struct notification_thread_state
*state
)
2228 struct cds_lfht_node
*node
;
2229 struct cds_lfht_iter iter
;
2230 struct channel_info
*channel
;
2232 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2234 struct lttng_trigger_list_element
*trigger_list_element
;
2235 struct lttng_channel_trigger_list
*trigger_list
;
2236 struct cds_lfht_iter lookup_iter
;
2238 if (!trigger_applies_to_channel(trigger
, channel
)) {
2242 cds_lfht_lookup(state
->channel_triggers_ht
,
2243 hash_channel_key(&channel
->key
),
2244 match_channel_trigger_list
,
2247 node
= cds_lfht_iter_get_node(&lookup_iter
);
2249 trigger_list
= caa_container_of(node
,
2250 struct lttng_channel_trigger_list
,
2251 channel_triggers_ht_node
);
2253 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2254 if (!trigger_list_element
) {
2258 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2259 trigger_list_element
->trigger
= trigger
;
2260 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2261 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2269 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2271 bool is_notify
= false;
2272 unsigned int i
, count
;
2273 enum lttng_action_status action_status
;
2274 const struct lttng_action
*action
=
2275 lttng_trigger_get_const_action(trigger
);
2276 enum lttng_action_type action_type
;
2279 action_type
= lttng_action_get_type(action
);
2280 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2283 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2287 action_status
= lttng_action_group_get_count(action
, &count
);
2288 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2290 for (i
= 0; i
< count
; i
++) {
2291 const struct lttng_action
*inner_action
=
2292 lttng_action_group_get_at_index(
2295 action_type
= lttng_action_get_type(inner_action
);
2296 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2306 static bool trigger_name_taken(struct notification_thread_state
*state
,
2307 const struct lttng_trigger
*trigger
)
2309 struct cds_lfht_node
*triggers_by_name_uid_ht_node
;
2310 struct cds_lfht_iter iter
;
2313 * No duplicata is allowed in the triggers_by_name_uid_ht.
2314 * The match is done against the trigger name and uid.
2316 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2317 hash_key_str(trigger
->name
, lttng_ht_seed
),
2321 triggers_by_name_uid_ht_node
= cds_lfht_iter_get_node(&iter
);
2322 if (triggers_by_name_uid_ht_node
) {
2330 void generate_trigger_name(struct notification_thread_state
*state
, struct lttng_trigger
*trigger
, const char **name
)
2333 * Here the offset criteria guarantee an end. This will be a nice
2334 * bikeshedding conversation. I would simply generate uuid and use them
2338 enum lttng_trigger_status status
;
2340 lttng_trigger_generate_name(trigger
, state
->trigger_id
.name_offset
);
2342 status
= lttng_trigger_get_name(trigger
, name
);
2343 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2345 taken
= trigger_name_taken(state
, trigger
);
2347 state
->trigger_id
.name_offset
++;
2349 } while (taken
|| state
->trigger_id
.name_offset
== UINT32_MAX
);
2353 * FIXME A client's credentials are not checked when registering a trigger.
2355 * The effects of this are benign since:
2356 * - The client will succeed in registering the trigger, as it is valid,
2357 * - The trigger will, internally, be bound to the channel/session,
2358 * - The notifications will not be sent since the client's credentials
2359 * are checked against the channel at that moment.
2361 * If this function returns a non-zero value, it means something is
2362 * fundamentally broken and the whole subsystem/thread will be torn down.
2364 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2368 int handle_notification_thread_command_register_trigger(
2369 struct notification_thread_state
*state
,
2370 struct lttng_trigger
*trigger
,
2371 enum lttng_error_code
*cmd_result
)
2375 struct lttng_condition
*condition
;
2376 struct lttng_action
*action
;
2377 struct notification_client
*client
;
2378 struct notification_client_list
*client_list
= NULL
;
2379 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2380 struct notification_client_list_element
*client_list_element
;
2381 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2382 struct cds_lfht_node
*node
;
2383 struct cds_lfht_iter iter
;
2384 const char* trigger_name
;
2385 bool free_trigger
= true;
2386 struct lttng_evaluation
*evaluation
= NULL
;
2387 struct lttng_credentials object_creds
;
2390 enum action_executor_status executor_status
;
2394 /* Set the trigger's tracer token */
2395 lttng_trigger_set_tracer_token(trigger
, state
->trigger_id
.token_generator
);
2397 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2398 LTTNG_TRIGGER_STATUS_UNSET
) {
2399 generate_trigger_name(state
, trigger
, &trigger_name
);
2400 } else if (trigger_name_taken(state
, trigger
)) {
2401 /* Not a fatal error */
2402 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2407 condition
= lttng_trigger_get_condition(trigger
);
2410 action
= lttng_trigger_get_action(trigger
);
2413 is_supported
= condition_is_supported(condition
);
2414 if (is_supported
< 0) {
2416 } else if (is_supported
== 0) {
2417 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2421 is_supported
= action_is_supported(action
);
2422 if (is_supported
< 0) {
2424 } else if (is_supported
== 0) {
2426 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2430 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2431 if (!trigger_ht_element
) {
2436 /* Add trigger to the trigger_ht. */
2437 cds_lfht_node_init(&trigger_ht_element
->node
);
2438 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2439 trigger_ht_element
->trigger
= trigger
;
2441 node
= cds_lfht_add_unique(state
->triggers_ht
,
2442 lttng_condition_hash(condition
),
2445 &trigger_ht_element
->node
);
2446 if (node
!= &trigger_ht_element
->node
) {
2447 /* Not a fatal error, simply report it to the client. */
2448 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2449 goto error_free_ht_element
;
2452 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2453 hash_key_str(trigger_name
, lttng_ht_seed
), match_name_uid
,
2454 trigger
, &trigger_ht_element
->node_by_name_uid
);
2455 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2456 /* Not a fatal error, simply report it to the client. */
2457 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2458 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2459 goto error_free_ht_element
;
2462 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2463 uint64_t error_counter_index
= 0;
2464 enum trigger_error_accounting_status error_accounting_status
;
2466 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2467 if (!trigger_tokens_ht_element
) {
2469 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2470 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2474 error_accounting_status
= trigger_error_accounting_register_trigger(
2475 trigger
, &error_counter_index
);
2476 if (error_accounting_status
!= TRIGGER_ERROR_ACCOUNTING_STATUS_OK
) {
2477 ERR("Error registering trigger for error accounting");
2478 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2479 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2480 *cmd_result
= LTTNG_ERR_TRIGGER_GROUP_ERROR_COUNTER_FULL
;
2481 goto error_free_ht_element
;
2484 lttng_trigger_set_error_counter_index(trigger
, error_counter_index
);
2486 /* Add trigger token to the trigger_tokens_ht. */
2487 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2488 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2489 trigger_tokens_ht_element
->trigger
= trigger
;
2491 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2492 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2493 match_trigger_token
,
2494 &trigger_tokens_ht_element
->token
,
2495 &trigger_tokens_ht_element
->node
);
2496 if (node
!= &trigger_tokens_ht_element
->node
) {
2497 /* TODO: THIS IS A FATAL ERROR... should never happen */
2498 /* Not a fatal error, simply report it to the client. */
2499 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2500 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2501 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2502 trigger_error_accounting_unregister_trigger(trigger
);
2503 goto error_free_ht_element
;
2508 * Ownership of the trigger and of its wrapper was transfered to
2509 * the triggers_ht. Same for token ht element if necessary.
2511 trigger_tokens_ht_element
= NULL
;
2512 trigger_ht_element
= NULL
;
2513 free_trigger
= false;
2516 * The rest only applies to triggers that have a "notify" action.
2517 * It is not skipped as this is the only action type currently
2520 if (is_trigger_action_notify(trigger
)) {
2521 client_list
= notification_client_list_create(trigger
);
2524 goto error_free_ht_element
;
2527 /* Build a list of clients to which this new trigger applies. */
2528 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2529 client_socket_ht_node
) {
2530 if (!trigger_applies_to_client(trigger
, client
)) {
2534 client_list_element
=
2535 zmalloc(sizeof(*client_list_element
));
2536 if (!client_list_element
) {
2538 goto error_put_client_list
;
2541 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2542 client_list_element
->client
= client
;
2543 cds_list_add(&client_list_element
->node
,
2544 &client_list
->list
);
2548 * Client list ownership transferred to the
2549 * notification_trigger_clients_ht.
2551 publish_notification_client_list(state
, client_list
);
2554 switch (get_condition_binding_object(condition
)) {
2555 case LTTNG_OBJECT_TYPE_SESSION
:
2556 /* Add the trigger to the list if it matches a known session. */
2557 ret
= bind_trigger_to_matching_session(trigger
, state
);
2559 goto error_put_client_list
;
2562 case LTTNG_OBJECT_TYPE_CHANNEL
:
2564 * Add the trigger to list of triggers bound to the channels
2567 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2569 goto error_put_client_list
;
2572 case LTTNG_OBJECT_TYPE_NONE
:
2575 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2577 goto error_put_client_list
;
2581 * The new trigger's condition must be evaluated against the current
2584 * In the case of `notify` action, nothing preventing clients from
2585 * subscribing to a condition before the corresponding trigger is
2586 * registered, we have to evaluate this new condition right away.
2588 * At some point, we were waiting for the next "evaluation" (e.g. on
2589 * reception of a channel sample) to evaluate this new condition, but
2592 * The reason it was broken is that waiting for the next sample
2593 * does not allow us to properly handle transitions for edge-triggered
2596 * Consider this example: when we handle a new channel sample, we
2597 * evaluate each conditions twice: once with the previous state, and
2598 * again with the newest state. We then use those two results to
2599 * determine whether a state change happened: a condition was false and
2600 * became true. If a state change happened, we have to notify clients.
2602 * Now, if a client subscribes to a given notification and registers
2603 * a trigger *after* that subscription, we have to make sure the
2604 * condition is evaluated at this point while considering only the
2605 * current state. Otherwise, the next evaluation cycle may only see
2606 * that the evaluations remain the same (true for samples n-1 and n) and
2607 * the client will never know that the condition has been met.
2609 switch (get_condition_binding_object(condition
)) {
2610 case LTTNG_OBJECT_TYPE_SESSION
:
2611 ret
= evaluate_session_condition_for_client(condition
, state
,
2612 &evaluation
, &object_uid
,
2615 case LTTNG_OBJECT_TYPE_CHANNEL
:
2616 ret
= evaluate_channel_condition_for_client(condition
, state
,
2617 &evaluation
, &object_uid
,
2620 case LTTNG_OBJECT_TYPE_NONE
:
2623 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2631 goto error_put_client_list
;
2634 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2635 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2637 DBG("Newly registered trigger's condition evaluated to %s",
2638 evaluation
? "true" : "false");
2640 /* Evaluation yielded nothing. Normal exit. */
2646 * Ownership of `evaluation` transferred to the action executor
2647 * no matter the result.
2649 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2650 evaluation
, &object_creds
, client_list
);
2652 switch (executor_status
) {
2653 case ACTION_EXECUTOR_STATUS_OK
:
2655 case ACTION_EXECUTOR_STATUS_ERROR
:
2656 case ACTION_EXECUTOR_STATUS_INVALID
:
2658 * TODO Add trigger identification (name/id) when
2659 * it is added to the API.
2661 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2663 goto error_put_client_list
;
2664 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2666 * TODO Add trigger identification (name/id) when
2667 * it is added to the API.
2669 * Not a fatal error.
2671 WARN("No space left when enqueuing action associated to newly registered trigger");
2679 /* Increment the trigger unique id generator */
2680 state
->trigger_id
.token_generator
++;
2681 *cmd_result
= LTTNG_OK
;
2683 error_put_client_list
:
2684 notification_client_list_put(client_list
);
2686 error_free_ht_element
:
2687 if (trigger_ht_element
) {
2688 /* Delayed removal due to RCU constraint on delete. */
2689 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2692 free(trigger_tokens_ht_element
);
2695 lttng_trigger_destroy(trigger
);
2702 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2704 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2709 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2711 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2716 int handle_notification_thread_command_unregister_trigger(
2717 struct notification_thread_state
*state
,
2718 struct lttng_trigger
*trigger
,
2719 enum lttng_error_code
*_cmd_reply
)
2721 struct cds_lfht_iter iter
;
2722 struct cds_lfht_node
*triggers_ht_node
;
2723 struct lttng_channel_trigger_list
*trigger_list
;
2724 struct notification_client_list
*client_list
;
2725 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2726 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2728 enum lttng_error_code cmd_reply
;
2732 cds_lfht_lookup(state
->triggers_ht
,
2733 lttng_condition_hash(condition
),
2737 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2738 if (!triggers_ht_node
) {
2739 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2742 cmd_reply
= LTTNG_OK
;
2745 /* Remove trigger from channel_triggers_ht. */
2746 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2747 channel_triggers_ht_node
) {
2748 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2750 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2751 &trigger_list
->list
, node
) {
2752 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2756 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2757 cds_list_del(&trigger_element
->node
);
2758 /* A trigger can only appear once per channel */
2763 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2764 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2765 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
, trigger_tokens_ht_element
,
2767 if (!lttng_trigger_is_equal(trigger
, trigger_tokens_ht_element
->trigger
)) {
2771 trigger_error_accounting_unregister_trigger(trigger_tokens_ht_element
->trigger
);
2773 /* TODO talk to all app and remove it */
2774 DBG("[notification-thread] Removed trigger from tokens_ht");
2775 cds_lfht_del(state
->trigger_tokens_ht
,
2776 &trigger_tokens_ht_element
->node
);
2777 call_rcu(&trigger_tokens_ht_element
->rcu_node
, free_notification_trigger_tokens_ht_element_rcu
);
2783 if (is_trigger_action_notify(trigger
)) {
2785 * Remove and release the client list from
2786 * notification_trigger_clients_ht.
2788 client_list
= get_client_list_from_condition(state
, condition
);
2789 assert(client_list
);
2791 /* Put new reference and the hashtable's reference. */
2792 notification_client_list_put(client_list
);
2793 notification_client_list_put(client_list
);
2797 /* Remove trigger from triggers_ht. */
2798 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2799 struct lttng_trigger_ht_element
, node
);
2800 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2801 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2803 /* Release the ownership of the trigger. */
2804 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2805 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2809 *_cmd_reply
= cmd_reply
;
2814 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2815 int handle_notification_thread_command(
2816 struct notification_thread_handle
*handle
,
2817 struct notification_thread_state
*state
)
2821 struct notification_thread_command
*cmd
;
2823 /* Read the event pipe to put it back into a quiescent state. */
2824 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2826 if (ret
!= sizeof(counter
)) {
2830 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2831 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2832 struct notification_thread_command
, cmd_list_node
);
2833 cds_list_del(&cmd
->cmd_list_node
);
2834 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2835 switch (cmd
->type
) {
2836 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2837 DBG("[notification-thread] Received register trigger command");
2838 ret
= handle_notification_thread_command_register_trigger(
2839 state
, cmd
->parameters
.trigger
,
2842 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2843 DBG("[notification-thread] Received unregister trigger command");
2844 ret
= handle_notification_thread_command_unregister_trigger(
2845 state
, cmd
->parameters
.trigger
,
2848 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2849 DBG("[notification-thread] Received add channel command");
2850 ret
= handle_notification_thread_command_add_channel(
2852 cmd
->parameters
.add_channel
.session
.name
,
2853 cmd
->parameters
.add_channel
.session
.uid
,
2854 cmd
->parameters
.add_channel
.session
.gid
,
2855 cmd
->parameters
.add_channel
.channel
.name
,
2856 cmd
->parameters
.add_channel
.channel
.domain
,
2857 cmd
->parameters
.add_channel
.channel
.key
,
2858 cmd
->parameters
.add_channel
.channel
.capacity
,
2861 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2862 DBG("[notification-thread] Received remove channel command");
2863 ret
= handle_notification_thread_command_remove_channel(
2864 state
, cmd
->parameters
.remove_channel
.key
,
2865 cmd
->parameters
.remove_channel
.domain
,
2868 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2869 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2870 DBG("[notification-thread] Received session rotation %s command",
2871 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2872 "ongoing" : "completed");
2873 ret
= handle_notification_thread_command_session_rotation(
2876 cmd
->parameters
.session_rotation
.session_name
,
2877 cmd
->parameters
.session_rotation
.uid
,
2878 cmd
->parameters
.session_rotation
.gid
,
2879 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2880 cmd
->parameters
.session_rotation
.location
,
2883 case NOTIFICATION_COMMAND_TYPE_ADD_APPLICATION
:
2884 ret
= handle_notification_thread_command_add_application(
2887 cmd
->parameters
.application
.read_side_trigger_event_application_pipe
,
2888 cmd
->parameters
.application
.domain
,
2891 case NOTIFICATION_COMMAND_TYPE_REMOVE_APPLICATION
:
2892 ret
= handle_notification_thread_command_remove_application(
2895 cmd
->parameters
.application
.read_side_trigger_event_application_pipe
,
2898 case NOTIFICATION_COMMAND_TYPE_GET_TOKENS
:
2900 struct lttng_triggers
*triggers
= NULL
;
2901 ret
= handle_notification_thread_command_get_tokens(
2902 handle
, state
, &triggers
, &cmd
->reply_code
);
2903 cmd
->reply
.get_tokens
.triggers
= triggers
;
2907 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2909 struct lttng_triggers
*triggers
= NULL
;
2910 ret
= handle_notification_thread_command_list_triggers(
2913 cmd
->parameters
.list_triggers
.uid
,
2916 cmd
->reply
.list_triggers
.triggers
= triggers
;
2920 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2921 DBG("[notification-thread] Received quit command");
2922 cmd
->reply_code
= LTTNG_OK
;
2925 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2927 const enum client_transmission_status client_status
=
2928 cmd
->parameters
.client_communication_update
2930 const notification_client_id client_id
=
2931 cmd
->parameters
.client_communication_update
.id
;
2932 struct notification_client
*client
;
2935 client
= get_client_from_id(client_id
, state
);
2939 * Client error was probably already picked-up by the
2940 * notification thread or it has disconnected
2941 * gracefully while this command was queued.
2943 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2947 ret
= client_handle_transmission_status(
2948 client
, client_status
, state
);
2954 ERR("[notification-thread] Unknown internal command received");
2962 if (cmd
->is_async
) {
2966 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2970 /* Wake-up and return a fatal error to the calling thread. */
2971 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2972 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2974 /* Indicate a fatal error to the caller. */
2979 int socket_set_non_blocking(int socket
)
2983 /* Set the pipe as non-blocking. */
2984 ret
= fcntl(socket
, F_GETFL
, 0);
2986 PERROR("fcntl get socket flags");
2991 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2993 PERROR("fcntl set O_NONBLOCK socket flag");
2996 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3002 int client_reset_inbound_state(struct notification_client
*client
)
3007 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3009 client
->communication
.inbound
.bytes_to_receive
=
3010 sizeof(struct lttng_notification_channel_message
);
3011 client
->communication
.inbound
.msg_type
=
3012 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3013 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3014 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3015 ret
= lttng_dynamic_buffer_set_size(
3016 &client
->communication
.inbound
.payload
.buffer
,
3017 client
->communication
.inbound
.bytes_to_receive
);
3022 int handle_notification_thread_client_connect(
3023 struct notification_thread_state
*state
)
3026 struct notification_client
*client
;
3028 DBG("[notification-thread] Handling new notification channel client connection");
3030 client
= zmalloc(sizeof(*client
));
3037 pthread_mutex_init(&client
->lock
, NULL
);
3038 client
->id
= state
->next_notification_client_id
++;
3039 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3040 lttng_payload_init(&client
->communication
.inbound
.payload
);
3041 lttng_payload_init(&client
->communication
.outbound
.payload
);
3042 client
->communication
.inbound
.expect_creds
= true;
3044 ret
= client_reset_inbound_state(client
);
3046 ERR("[notification-thread] Failed to reset client communication's inbound state");
3051 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3053 ERR("[notification-thread] Failed to accept new notification channel client connection");
3058 client
->socket
= ret
;
3060 ret
= socket_set_non_blocking(client
->socket
);
3062 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3066 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3068 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3073 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3074 LPOLLIN
| LPOLLERR
|
3075 LPOLLHUP
| LPOLLRDHUP
);
3077 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3081 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3085 cds_lfht_add(state
->client_socket_ht
,
3086 hash_client_socket(client
->socket
),
3087 &client
->client_socket_ht_node
);
3088 cds_lfht_add(state
->client_id_ht
,
3089 hash_client_id(client
->id
),
3090 &client
->client_id_ht_node
);
3096 notification_client_destroy(client
, state
);
3101 * RCU read-lock must be held by the caller.
3102 * Client lock must _not_ be held by the caller.
3105 int notification_thread_client_disconnect(
3106 struct notification_client
*client
,
3107 struct notification_thread_state
*state
)
3110 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3112 /* Acquire the client lock to disable its communication atomically. */
3113 pthread_mutex_lock(&client
->lock
);
3114 client
->communication
.active
= false;
3115 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3116 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3117 pthread_mutex_unlock(&client
->lock
);
3119 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3121 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3125 /* Release all conditions to which the client was subscribed. */
3126 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3127 &client
->condition_list
, node
) {
3128 (void) notification_thread_client_unsubscribe(client
,
3129 condition_list_element
->condition
, state
, NULL
);
3133 * Client no longer accessible to other threads (through the
3136 notification_client_destroy(client
, state
);
3140 int handle_notification_thread_client_disconnect(
3141 int client_socket
, struct notification_thread_state
*state
)
3144 struct notification_client
*client
;
3147 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3149 client
= get_client_from_socket(client_socket
, state
);
3151 /* Internal state corruption, fatal error. */
3152 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3158 ret
= notification_thread_client_disconnect(client
, state
);
3164 int handle_notification_thread_client_disconnect_all(
3165 struct notification_thread_state
*state
)
3167 struct cds_lfht_iter iter
;
3168 struct notification_client
*client
;
3169 bool error_encoutered
= false;
3172 DBG("[notification-thread] Closing all client connections");
3173 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3174 client_socket_ht_node
) {
3177 ret
= notification_thread_client_disconnect(
3180 error_encoutered
= true;
3184 return error_encoutered
? 1 : 0;
3187 int handle_notification_thread_trigger_unregister_all(
3188 struct notification_thread_state
*state
)
3190 bool error_occurred
= false;
3191 struct cds_lfht_iter iter
;
3192 struct lttng_trigger_ht_element
*trigger_ht_element
;
3195 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3197 int ret
= handle_notification_thread_command_unregister_trigger(
3198 state
, trigger_ht_element
->trigger
, NULL
);
3200 error_occurred
= true;
3204 return error_occurred
? -1 : 0;
3208 int client_handle_transmission_status(
3209 struct notification_client
*client
,
3210 enum client_transmission_status transmission_status
,
3211 struct notification_thread_state
*state
)
3215 switch (transmission_status
) {
3216 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3217 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3218 CLIENT_POLL_MASK_IN
);
3224 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3226 * We want to be notified whenever there is buffer space
3227 * available to send the rest of the payload.
3229 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3230 CLIENT_POLL_MASK_IN_OUT
);
3235 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3236 ret
= notification_thread_client_disconnect(client
, state
);
3241 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3251 /* Client lock must be acquired by caller. */
3253 enum client_transmission_status
client_flush_outgoing_queue(
3254 struct notification_client
*client
)
3257 size_t to_send_count
;
3258 enum client_transmission_status status
;
3259 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3260 &client
->communication
.outbound
.payload
, 0, -1);
3261 const int fds_to_send_count
=
3262 lttng_payload_view_get_fd_handle_count(&pv
);
3264 ASSERT_LOCKED(client
->lock
);
3266 if (!client
->communication
.active
) {
3267 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3271 if (pv
.buffer
.size
== 0) {
3273 * If both data and fds are equal to zero, we are in an invalid
3276 assert(fds_to_send_count
!= 0);
3281 to_send_count
= pv
.buffer
.size
;
3282 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3285 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3288 if ((ret
>= 0 && ret
< to_send_count
)) {
3289 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3291 to_send_count
-= max(ret
, 0);
3293 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3295 pv
.buffer
.size
- to_send_count
,
3297 ret
= lttng_dynamic_buffer_set_size(
3298 &client
->communication
.outbound
.payload
.buffer
,
3304 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3306 } else if (ret
< 0) {
3307 /* Generic error, disable the client's communication. */
3308 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3310 client
->communication
.active
= false;
3311 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3315 * No error and flushed the queue completely.
3317 * The payload buffer size is used later to
3318 * check if there is notifications queued. So albeit that the
3319 * direct caller knows that the transmission is complete, we
3320 * need to set the buffer size to zero.
3322 ret
= lttng_dynamic_buffer_set_size(
3323 &client
->communication
.outbound
.payload
.buffer
, 0);
3330 /* No fds to send, transmission is complete. */
3331 if (fds_to_send_count
== 0) {
3332 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3336 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3337 client
->socket
, &pv
);
3339 /* Generic error, disable the client's communication. */
3340 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3342 client
->communication
.active
= false;
3343 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3345 } else if (ret
== 0) {
3346 /* Nothing could be sent. */
3347 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3349 /* Fd passing is an all or nothing kind of thing. */
3350 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3352 * The payload _fd_array count is used later to
3353 * check if there is notifications queued. So although the
3354 * direct caller knows that the transmission is complete, we
3355 * need to clear the _fd_array for the queuing check.
3357 lttng_dynamic_pointer_array_clear(
3358 &client
->communication
.outbound
.payload
3363 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3364 client
->communication
.outbound
.queued_command_reply
= false;
3365 client
->communication
.outbound
.dropped_notification
= false;
3366 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3371 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3375 bool client_has_outbound_data_left(
3376 const struct notification_client
*client
)
3378 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3379 &client
->communication
.outbound
.payload
, 0, -1);
3380 const bool has_data
= pv
.buffer
.size
!= 0;
3381 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3383 return has_data
|| has_fds
;
3386 /* Client lock must _not_ be held by the caller. */
3388 int client_send_command_reply(struct notification_client
*client
,
3389 struct notification_thread_state
*state
,
3390 enum lttng_notification_channel_status status
)
3393 struct lttng_notification_channel_command_reply reply
= {
3394 .status
= (int8_t) status
,
3396 struct lttng_notification_channel_message msg
= {
3397 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3398 .size
= sizeof(reply
),
3400 char buffer
[sizeof(msg
) + sizeof(reply
)];
3401 enum client_transmission_status transmission_status
;
3403 memcpy(buffer
, &msg
, sizeof(msg
));
3404 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3405 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3407 pthread_mutex_lock(&client
->lock
);
3408 if (client
->communication
.outbound
.queued_command_reply
) {
3409 /* Protocol error. */
3413 /* Enqueue buffer to outgoing queue and flush it. */
3414 ret
= lttng_dynamic_buffer_append(
3415 &client
->communication
.outbound
.payload
.buffer
,
3416 buffer
, sizeof(buffer
));
3421 transmission_status
= client_flush_outgoing_queue(client
);
3423 if (client_has_outbound_data_left(client
)) {
3424 /* Queue could not be emptied. */
3425 client
->communication
.outbound
.queued_command_reply
= true;
3428 pthread_mutex_unlock(&client
->lock
);
3429 ret
= client_handle_transmission_status(
3430 client
, transmission_status
, state
);
3437 pthread_mutex_unlock(&client
->lock
);
3443 int client_handle_message_unknown(struct notification_client
*client
,
3444 struct notification_thread_state
*state
)
3448 * Receiving message header. The function will be called again
3449 * once the rest of the message as been received and can be
3452 const struct lttng_notification_channel_message
*msg
;
3454 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3455 msg
= (const struct lttng_notification_channel_message
*)
3456 client
->communication
.inbound
.payload
.buffer
.data
;
3458 if (msg
->size
== 0 ||
3459 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3460 ERR("[notification-thread] Invalid notification channel message: length = %u",
3466 switch (msg
->type
) {
3467 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3468 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3469 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3473 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3477 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3478 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3479 client
->communication
.inbound
.msg_type
=
3480 (enum lttng_notification_channel_message_type
) msg
->type
;
3481 ret
= lttng_dynamic_buffer_set_size(
3482 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3484 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3491 int client_handle_message_handshake(struct notification_client
*client
,
3492 struct notification_thread_state
*state
)
3495 struct lttng_notification_channel_command_handshake
*handshake_client
;
3496 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3497 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3498 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3500 const struct lttng_notification_channel_message msg_header
= {
3501 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3502 .size
= sizeof(handshake_reply
),
3504 enum lttng_notification_channel_status status
=
3505 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3506 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3508 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3509 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3510 sizeof(handshake_reply
));
3513 (struct lttng_notification_channel_command_handshake
*)
3514 client
->communication
.inbound
.payload
.buffer
3516 client
->major
= handshake_client
->major
;
3517 client
->minor
= handshake_client
->minor
;
3518 if (!client
->communication
.inbound
.creds_received
) {
3519 ERR("[notification-thread] No credentials received from client");
3524 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3525 &client
->communication
.inbound
.creds
);
3526 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3527 &client
->communication
.inbound
.creds
);
3528 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3529 client
->uid
, client
->gid
, (int) client
->major
,
3530 (int) client
->minor
);
3532 if (handshake_client
->major
!=
3533 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3534 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3537 pthread_mutex_lock(&client
->lock
);
3538 /* Outgoing queue will be flushed when the command reply is sent. */
3539 ret
= lttng_dynamic_buffer_append(
3540 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3541 sizeof(send_buffer
));
3543 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3547 client
->validated
= true;
3548 client
->communication
.active
= true;
3549 pthread_mutex_unlock(&client
->lock
);
3551 /* Set reception state to receive the next message header. */
3552 ret
= client_reset_inbound_state(client
);
3554 ERR("[notification-thread] Failed to reset client communication's inbound state");
3558 /* Flushes the outgoing queue. */
3559 ret
= client_send_command_reply(client
, state
, status
);
3561 ERR("[notification-thread] Failed to send reply to notification channel client");
3567 pthread_mutex_unlock(&client
->lock
);
3573 int client_handle_message_subscription(
3574 struct notification_client
*client
,
3575 enum lttng_notification_channel_message_type msg_type
,
3576 struct notification_thread_state
*state
)
3579 struct lttng_condition
*condition
;
3580 enum lttng_notification_channel_status status
=
3581 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3582 struct lttng_payload_view condition_view
=
3583 lttng_payload_view_from_payload(
3584 &client
->communication
.inbound
.payload
,
3586 size_t expected_condition_size
;
3589 * No need to lock client to sample the inbound state as the only
3590 * other thread accessing clients (action executor) only uses the
3593 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3594 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3595 if (ret
!= expected_condition_size
) {
3596 ERR("[notification-thread] Malformed condition received from client");
3600 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3601 ret
= notification_thread_client_subscribe(
3602 client
, condition
, state
, &status
);
3604 ret
= notification_thread_client_unsubscribe(
3605 client
, condition
, state
, &status
);
3612 /* Set reception state to receive the next message header. */
3613 ret
= client_reset_inbound_state(client
);
3615 ERR("[notification-thread] Failed to reset client communication's inbound state");
3619 ret
= client_send_command_reply(client
, state
, status
);
3621 ERR("[notification-thread] Failed to send reply to notification channel client");
3630 int client_dispatch_message(struct notification_client
*client
,
3631 struct notification_thread_state
*state
)
3635 if (client
->communication
.inbound
.msg_type
!=
3636 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3637 client
->communication
.inbound
.msg_type
!=
3638 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3639 !client
->validated
) {
3640 WARN("[notification-thread] client attempted a command before handshake");
3645 switch (client
->communication
.inbound
.msg_type
) {
3646 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3648 ret
= client_handle_message_unknown(client
, state
);
3651 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3653 ret
= client_handle_message_handshake(client
, state
);
3656 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3657 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3659 ret
= client_handle_message_subscription(client
,
3660 client
->communication
.inbound
.msg_type
, state
);
3670 /* Incoming data from client. */
3671 int handle_notification_thread_client_in(
3672 struct notification_thread_state
*state
, int socket
)
3675 struct notification_client
*client
;
3680 client
= get_client_from_socket(socket
, state
);
3682 /* Internal error, abort. */
3687 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3688 client
->communication
.inbound
.bytes_to_receive
;
3689 if (client
->communication
.inbound
.expect_creds
) {
3690 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3691 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3692 client
->communication
.inbound
.bytes_to_receive
,
3693 &client
->communication
.inbound
.creds
);
3695 client
->communication
.inbound
.expect_creds
= false;
3696 client
->communication
.inbound
.creds_received
= true;
3699 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3700 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3701 client
->communication
.inbound
.bytes_to_receive
);
3703 if (recv_ret
>= 0) {
3704 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3706 goto error_disconnect_client
;
3709 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3710 /* Message incomplete wait for more data. */
3715 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3718 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3719 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3721 client
->communication
.inbound
.fds_to_receive
,
3722 &client
->communication
.inbound
.payload
);
3725 * Fds received. non blocking fds passing is all
3728 ssize_t expected_size
;
3730 expected_size
= sizeof(int) *
3731 client
->communication
.inbound
3733 assert(ret
== expected_size
);
3734 client
->communication
.inbound
.fds_to_receive
= 0;
3735 } else if (ret
== 0) {
3736 /* Received nothing. */
3740 goto error_disconnect_client
;
3744 /* At this point the message is complete.*/
3745 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3746 client
->communication
.inbound
.fds_to_receive
== 0);
3747 ret
= client_dispatch_message(client
, state
);
3750 * Only returns an error if this client must be
3753 goto error_disconnect_client
;
3760 error_disconnect_client
:
3761 ret
= notification_thread_client_disconnect(client
, state
);
3765 /* Client ready to receive outgoing data. */
3766 int handle_notification_thread_client_out(
3767 struct notification_thread_state
*state
, int socket
)
3770 struct notification_client
*client
;
3771 enum client_transmission_status transmission_status
;
3774 client
= get_client_from_socket(socket
, state
);
3776 /* Internal error, abort. */
3781 pthread_mutex_lock(&client
->lock
);
3782 transmission_status
= client_flush_outgoing_queue(client
);
3783 pthread_mutex_unlock(&client
->lock
);
3785 ret
= client_handle_transmission_status(
3786 client
, transmission_status
, state
);
3796 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3797 const struct channel_state_sample
*sample
,
3798 uint64_t buffer_capacity
)
3800 bool result
= false;
3802 enum lttng_condition_type condition_type
;
3803 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3804 condition
, struct lttng_condition_buffer_usage
,
3807 if (use_condition
->threshold_bytes
.set
) {
3808 threshold
= use_condition
->threshold_bytes
.value
;
3811 * Threshold was expressed as a ratio.
3813 * TODO the threshold (in bytes) of conditions expressed
3814 * as a ratio of total buffer size could be cached to
3815 * forego this double-multiplication or it could be performed
3816 * as fixed-point math.
3818 * Note that caching should accommodates the case where the
3819 * condition applies to multiple channels (i.e. don't assume
3820 * that all channels matching my_chann* have the same size...)
3822 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3823 (double) buffer_capacity
);
3826 condition_type
= lttng_condition_get_type(condition
);
3827 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3828 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3829 threshold
, sample
->highest_usage
);
3832 * The low condition should only be triggered once _all_ of the
3833 * streams in a channel have gone below the "low" threshold.
3835 if (sample
->highest_usage
<= threshold
) {
3839 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3840 threshold
, sample
->highest_usage
);
3843 * For high buffer usage scenarios, we want to trigger whenever
3844 * _any_ of the streams has reached the "high" threshold.
3846 if (sample
->highest_usage
>= threshold
) {
3855 bool evaluate_session_consumed_size_condition(
3856 const struct lttng_condition
*condition
,
3857 uint64_t session_consumed_size
)
3860 const struct lttng_condition_session_consumed_size
*size_condition
=
3861 container_of(condition
,
3862 struct lttng_condition_session_consumed_size
,
3865 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3866 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3867 threshold
, session_consumed_size
);
3868 return session_consumed_size
>= threshold
;
3872 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3873 struct lttng_evaluation
**evaluation
,
3874 const struct notification_thread_state
*state
,
3875 const struct channel_state_sample
*previous_sample
,
3876 const struct channel_state_sample
*latest_sample
,
3877 uint64_t previous_session_consumed_total
,
3878 uint64_t latest_session_consumed_total
,
3879 struct channel_info
*channel_info
)
3882 enum lttng_condition_type condition_type
;
3883 const bool previous_sample_available
= !!previous_sample
;
3884 bool previous_sample_result
= false;
3885 bool latest_sample_result
;
3887 condition_type
= lttng_condition_get_type(condition
);
3889 switch (condition_type
) {
3890 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3891 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3892 if (caa_likely(previous_sample_available
)) {
3893 previous_sample_result
=
3894 evaluate_buffer_usage_condition(condition
,
3895 previous_sample
, channel_info
->capacity
);
3897 latest_sample_result
= evaluate_buffer_usage_condition(
3898 condition
, latest_sample
,
3899 channel_info
->capacity
);
3901 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3902 if (caa_likely(previous_sample_available
)) {
3903 previous_sample_result
=
3904 evaluate_session_consumed_size_condition(
3906 previous_session_consumed_total
);
3908 latest_sample_result
=
3909 evaluate_session_consumed_size_condition(
3911 latest_session_consumed_total
);
3914 /* Unknown condition type; internal error. */
3918 if (!latest_sample_result
||
3919 (previous_sample_result
== latest_sample_result
)) {
3921 * Only trigger on a condition evaluation transition.
3923 * NOTE: This edge-triggered logic may not be appropriate for
3924 * future condition types.
3929 if (!evaluation
|| !latest_sample_result
) {
3933 switch (condition_type
) {
3934 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3935 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3936 *evaluation
= lttng_evaluation_buffer_usage_create(
3938 latest_sample
->highest_usage
,
3939 channel_info
->capacity
);
3941 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3942 *evaluation
= lttng_evaluation_session_consumed_size_create(
3943 latest_session_consumed_total
);
3958 int client_notification_overflow(struct notification_client
*client
)
3961 const struct lttng_notification_channel_message msg
= {
3962 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3965 ASSERT_LOCKED(client
->lock
);
3967 DBG("Dropping notification addressed to client (socket fd = %i)",
3969 if (client
->communication
.outbound
.dropped_notification
) {
3971 * The client already has a "notification dropped" message
3972 * in its outgoing queue. Nothing to do since all
3973 * of those messages are coalesced.
3978 client
->communication
.outbound
.dropped_notification
= true;
3979 ret
= lttng_dynamic_buffer_append(
3980 &client
->communication
.outbound
.payload
.buffer
, &msg
,
3983 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3990 static int client_handle_transmission_status_wrapper(
3991 struct notification_client
*client
,
3992 enum client_transmission_status status
,
3995 return client_handle_transmission_status(client
, status
,
3996 (struct notification_thread_state
*) user_data
);
4000 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4001 const struct lttng_evaluation
*evaluation
,
4002 struct notification_client_list
* client_list
,
4003 struct notification_thread_state
*state
,
4004 uid_t object_uid
, gid_t object_gid
)
4006 const struct lttng_credentials creds
= {
4007 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4008 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4011 return notification_client_list_send_evaluation(client_list
,
4012 lttng_trigger_get_const_condition(trigger
), evaluation
,
4013 lttng_trigger_get_credentials(trigger
),
4015 client_handle_transmission_status_wrapper
, state
);
4019 * Permission checks relative to notification channel clients are performed
4020 * here. Notice how object, client, and trigger credentials are involved in
4023 * The `object` credentials are the credentials associated with the "subject"
4024 * of a condition. For instance, a `rotation completed` condition applies
4025 * to a session. When that condition is met, it will produce an evaluation
4026 * against a session. Hence, in this case, the `object` credentials are the
4027 * credentials of the "subject" session.
4029 * The `trigger` credentials are the credentials of the user that registered the
4032 * The `client` credentials are the credentials of the user that created a given
4033 * notification channel.
4035 * In terms of visibility, it is expected that non-privilieged users can only
4036 * register triggers against "their" objects (their own sessions and
4037 * applications they are allowed to interact with). They can then open a
4038 * notification channel and subscribe to notifications associated with those
4041 * As for privilieged users, they can register triggers against the objects of
4042 * other users. They can then subscribe to the notifications associated to their
4043 * triggers. Privilieged users _can't_ subscribe to the notifications of
4044 * triggers owned by other users; they must create their own triggers.
4046 * This is more a concern of usability than security. It would be difficult for
4047 * a root user reliably subscribe to a specific set of conditions without
4048 * interference from external users (those could, for instance, unregister
4052 int notification_client_list_send_evaluation(
4053 struct notification_client_list
*client_list
,
4054 const struct lttng_condition
*condition
,
4055 const struct lttng_evaluation
*evaluation
,
4056 const struct lttng_credentials
*trigger_creds
,
4057 const struct lttng_credentials
*source_object_creds
,
4058 report_client_transmission_result_cb client_report
,
4062 struct lttng_payload msg_payload
;
4063 struct notification_client_list_element
*client_list_element
, *tmp
;
4064 const struct lttng_notification notification
= {
4065 .condition
= (struct lttng_condition
*) condition
,
4066 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4068 struct lttng_notification_channel_message msg_header
= {
4069 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4072 lttng_payload_init(&msg_payload
);
4074 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4075 sizeof(msg_header
));
4080 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4082 ERR("[notification-thread] Failed to serialize notification");
4087 /* Update payload size. */
4088 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4089 ->size
= (uint32_t)(
4090 msg_payload
.buffer
.size
- sizeof(msg_header
));
4092 /* Update the payload number of fds. */
4094 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4095 &msg_payload
, 0, -1);
4097 ((struct lttng_notification_channel_message
*)
4098 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4099 lttng_payload_view_get_fd_handle_count(&pv
);
4102 pthread_mutex_lock(&client_list
->lock
);
4103 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4104 &client_list
->list
, node
) {
4105 enum client_transmission_status transmission_status
;
4106 struct notification_client
*client
=
4107 client_list_element
->client
;
4110 pthread_mutex_lock(&client
->lock
);
4111 if (!client
->communication
.active
) {
4113 * Skip inactive client (protocol error or
4116 DBG("Skipping client at it is marked as inactive");
4120 if (source_object_creds
) {
4121 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4122 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4125 * Client is not allowed to monitor this
4128 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4133 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
4134 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4138 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4139 client
->socket
, msg_payload
.buffer
.size
);
4141 if (client_has_outbound_data_left(client
)) {
4143 * Outgoing data is already buffered for this client;
4144 * drop the notification and enqueue a "dropped
4145 * notification" message if this is the first dropped
4146 * notification since the socket spilled-over to the
4149 ret
= client_notification_overflow(client
);
4156 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4162 transmission_status
= client_flush_outgoing_queue(client
);
4163 pthread_mutex_unlock(&client
->lock
);
4164 ret
= client_report(client
, transmission_status
, user_data
);
4167 goto end_unlock_list
;
4173 pthread_mutex_unlock(&client
->lock
);
4176 goto end_unlock_list
;
4182 pthread_mutex_unlock(&client_list
->lock
);
4184 lttng_payload_reset(&msg_payload
);
4188 static struct lttng_trigger_notification
*receive_notification(int pipe
,
4189 enum lttng_domain_type domain
)
4193 struct lttng_trigger_notification
*notification
= NULL
;
4194 char *capture_buffer
= NULL
;
4195 size_t capture_buffer_size
;
4196 void *reception_buffer
;
4197 size_t reception_size
;
4199 struct lttng_ust_trigger_notification ust_notification
;
4200 struct lttng_kernel_trigger_notification kernel_notification
;
4202 /* Init lttng_trigger_notification */
4205 case LTTNG_DOMAIN_UST
:
4206 reception_buffer
= (void *) &ust_notification
;
4207 reception_size
= sizeof(ust_notification
);
4209 case LTTNG_DOMAIN_KERNEL
:
4210 reception_buffer
= (void *) &kernel_notification
;
4211 reception_size
= sizeof(kernel_notification
);
4218 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4219 * ensuring that read/write of sampling messages are atomic.
4221 /* TODO: should we read as much as we can ? EWOULDBLOCK? */
4223 ret
= lttng_read(pipe
, reception_buffer
, reception_size
);
4224 if (ret
!= reception_size
) {
4225 PERROR("Failed to read from event source pipe (fd = %i, size to read=%zu, ret=%d)",
4226 pipe
, reception_size
, ret
);
4227 /* TODO: Should this error out completly.
4228 * This can happen when an app is killed as of today
4229 * ret = -1 cause the whole thread to die and fuck up
4236 case LTTNG_DOMAIN_UST
:
4237 id
= ust_notification
.id
;
4238 capture_buffer_size
=
4239 ust_notification
.capture_buf_size
;
4241 case LTTNG_DOMAIN_KERNEL
:
4242 id
= kernel_notification
.id
;
4243 capture_buffer_size
=
4244 kernel_notification
.capture_buf_size
;
4250 if (capture_buffer_size
== 0) {
4251 capture_buffer
= NULL
;
4255 capture_buffer
= zmalloc(capture_buffer_size
);
4256 if (!capture_buffer
) {
4257 ERR("[notification-thread] Failed to allocate capture buffer");
4262 * Fetch additional payload (capture).
4264 ret
= lttng_read(pipe
, capture_buffer
, capture_buffer_size
);
4265 if (ret
!= capture_buffer_size
) {
4266 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4268 /* TODO: Should this error out completly.
4269 * This can happen when an app is killed as of today
4270 * ret = -1 cause the whole thread to die and fuck up
4277 notification
= lttng_trigger_notification_create(
4278 id
, domain
, capture_buffer
, capture_buffer_size
);
4279 if (notification
== NULL
) {
4283 /* Ownership transfered to the lttng_trigger_notification object */
4284 capture_buffer
= NULL
;
4287 free(capture_buffer
);
4288 return notification
;
4291 int handle_notification_thread_event(struct notification_thread_state
*state
,
4293 enum lttng_domain_type domain
)
4296 enum lttng_trigger_status trigger_status
;
4297 struct cds_lfht_node
*node
;
4298 struct cds_lfht_iter iter
;
4299 struct notification_trigger_tokens_ht_element
*element
;
4300 struct lttng_evaluation
*evaluation
= NULL
;
4301 struct lttng_trigger_notification
*notification
= NULL
;
4302 enum action_executor_status executor_status
;
4303 struct notification_client_list
*client_list
= NULL
;
4304 const char *trigger_name
;
4305 unsigned int capture_count
= 0;
4307 notification
= receive_notification(pipe
, domain
);
4308 if (notification
== NULL
) {
4309 ERR("[notification-thread] Error receiving notification from tracer (fd = %i, domain = %s)",
4310 pipe
, lttng_domain_type_str(domain
));
4315 /* Find triggers associated with this token. */
4317 cds_lfht_lookup(state
->trigger_tokens_ht
,
4318 hash_key_u64(¬ification
->id
, lttng_ht_seed
),
4319 match_trigger_token
, ¬ification
->id
, &iter
);
4320 node
= cds_lfht_iter_get_node(&iter
);
4321 if (caa_unlikely(!node
)) {
4322 /* TODO: is this an error? This might happen if the receive side
4323 * is slow to process event from source and that the trigger was
4324 * removed but the app still kicking. This yield another
4325 * question on the trigger lifetime and when we can remove a
4326 * trigger. How to guarantee that all event with the token idea
4327 * have be processed? Do we want to provide this guarantee?
4329 * Update: I have encountered this when using a trigger on
4330 * sched_switch and then removing it. The frequency is quite
4331 * high hence we en up exactly in the mentionned scenario.
4332 * AFAIK this might be the best way to handle this.
4337 element
= caa_container_of(node
,
4338 struct notification_trigger_tokens_ht_element
,
4341 if (!lttng_trigger_should_fire(element
->trigger
)) {
4346 lttng_trigger_fire(element
->trigger
);
4348 trigger_status
= lttng_trigger_get_name(element
->trigger
, &trigger_name
);
4349 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4351 if (LTTNG_CONDITION_STATUS_OK
!=
4352 lttng_condition_event_rule_get_capture_descriptor_count(
4353 lttng_trigger_get_const_condition(
4356 ERR("Get capture count");
4361 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4362 ERR("Expected capture but capture buffer is null");
4367 evaluation
= lttng_evaluation_event_rule_create(
4368 container_of(lttng_trigger_get_const_condition(
4370 struct lttng_condition_event_rule
,
4373 notification
->capture_buffer
,
4374 notification
->capture_buf_size
, false);
4376 if (evaluation
== NULL
) {
4377 ERR("[notification-thread] Failed to create event rule hit evaluation");
4381 client_list
= get_client_list_from_condition(state
,
4382 lttng_trigger_get_const_condition(element
->trigger
));
4383 executor_status
= action_executor_enqueue(state
->executor
,
4384 element
->trigger
, evaluation
, NULL
, client_list
);
4385 switch (executor_status
) {
4386 case ACTION_EXECUTOR_STATUS_OK
:
4389 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4391 struct notification_client_list_element
*client_list_element
,
4395 * Not a fatal error; this is expected and simply means the
4396 * executor has too much work queued already.
4404 /* Warn clients that a notification (or more) was dropped. */
4405 pthread_mutex_lock(&client_list
->lock
);
4406 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4407 &client_list
->list
, node
) {
4408 enum client_transmission_status transmission_status
;
4409 struct notification_client
*client
=
4410 client_list_element
->client
;
4412 pthread_mutex_lock(&client
->lock
);
4413 ret
= client_notification_overflow(client
);
4419 transmission_status
=
4420 client_flush_outgoing_queue(client
);
4421 ret
= client_handle_transmission_status(
4422 client
, transmission_status
, state
);
4428 pthread_mutex_unlock(&client
->lock
);
4433 pthread_mutex_unlock(&client_list
->lock
);
4436 case ACTION_EXECUTOR_STATUS_INVALID
:
4437 case ACTION_EXECUTOR_STATUS_ERROR
:
4438 /* Fatal error, shut down everything. */
4439 ERR("Fatal error encoutered while enqueuing action");
4443 /* Unhandled error. */
4448 lttng_trigger_notification_destroy(notification
);
4449 notification_client_list_put(client_list
);
4455 int handle_notification_thread_channel_sample(
4456 struct notification_thread_state
*state
, int pipe
,
4457 enum lttng_domain_type domain
)
4460 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4461 struct channel_info
*channel_info
;
4462 struct cds_lfht_node
*node
;
4463 struct cds_lfht_iter iter
;
4464 struct lttng_channel_trigger_list
*trigger_list
;
4465 struct lttng_trigger_list_element
*trigger_list_element
;
4466 bool previous_sample_available
= false;
4467 struct channel_state_sample previous_sample
, latest_sample
;
4468 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4469 struct lttng_credentials channel_creds
;
4472 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4473 * ensuring that read/write of sampling messages are atomic.
4475 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4476 if (ret
!= sizeof(sample_msg
)) {
4477 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4484 latest_sample
.key
.key
= sample_msg
.key
;
4485 latest_sample
.key
.domain
= domain
;
4486 latest_sample
.highest_usage
= sample_msg
.highest
;
4487 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4488 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4492 /* Retrieve the channel's informations */
4493 cds_lfht_lookup(state
->channels_ht
,
4494 hash_channel_key(&latest_sample
.key
),
4498 node
= cds_lfht_iter_get_node(&iter
);
4499 if (caa_unlikely(!node
)) {
4501 * Not an error since the consumer can push a sample to the pipe
4502 * and the rest of the session daemon could notify us of the
4503 * channel's destruction before we get a chance to process that
4506 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4507 latest_sample
.key
.key
,
4508 lttng_domain_type_str(domain
));
4511 channel_info
= caa_container_of(node
, struct channel_info
,
4513 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4515 latest_sample
.key
.key
,
4516 channel_info
->session_info
->name
,
4517 latest_sample
.highest_usage
,
4518 latest_sample
.lowest_usage
,
4519 latest_sample
.channel_total_consumed
);
4521 previous_session_consumed_total
=
4522 channel_info
->session_info
->consumed_data_size
;
4524 /* Retrieve the channel's last sample, if it exists, and update it. */
4525 cds_lfht_lookup(state
->channel_state_ht
,
4526 hash_channel_key(&latest_sample
.key
),
4527 match_channel_state_sample
,
4530 node
= cds_lfht_iter_get_node(&iter
);
4531 if (caa_likely(node
)) {
4532 struct channel_state_sample
*stored_sample
;
4534 /* Update the sample stored. */
4535 stored_sample
= caa_container_of(node
,
4536 struct channel_state_sample
,
4537 channel_state_ht_node
);
4539 memcpy(&previous_sample
, stored_sample
,
4540 sizeof(previous_sample
));
4541 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4542 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4543 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4544 previous_sample_available
= true;
4546 latest_session_consumed_total
=
4547 previous_session_consumed_total
+
4548 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4551 * This is the channel's first sample, allocate space for and
4552 * store the new sample.
4554 struct channel_state_sample
*stored_sample
;
4556 stored_sample
= zmalloc(sizeof(*stored_sample
));
4557 if (!stored_sample
) {
4562 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4563 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4564 cds_lfht_add(state
->channel_state_ht
,
4565 hash_channel_key(&stored_sample
->key
),
4566 &stored_sample
->channel_state_ht_node
);
4568 latest_session_consumed_total
=
4569 previous_session_consumed_total
+
4570 latest_sample
.channel_total_consumed
;
4573 channel_info
->session_info
->consumed_data_size
=
4574 latest_session_consumed_total
;
4576 /* Find triggers associated with this channel. */
4577 cds_lfht_lookup(state
->channel_triggers_ht
,
4578 hash_channel_key(&latest_sample
.key
),
4579 match_channel_trigger_list
,
4582 node
= cds_lfht_iter_get_node(&iter
);
4583 if (caa_likely(!node
)) {
4587 channel_creds
= (typeof(channel_creds
)) {
4588 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4589 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4592 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4593 channel_triggers_ht_node
);
4594 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4596 const struct lttng_condition
*condition
;
4597 const struct lttng_action
*action
;
4598 struct lttng_trigger
*trigger
;
4599 struct notification_client_list
*client_list
= NULL
;
4600 struct lttng_evaluation
*evaluation
= NULL
;
4601 enum action_executor_status executor_status
;
4604 trigger
= trigger_list_element
->trigger
;
4605 condition
= lttng_trigger_get_const_condition(trigger
);
4607 action
= lttng_trigger_get_const_action(trigger
);
4609 if (!lttng_trigger_should_fire(trigger
)) {
4613 lttng_trigger_fire(trigger
);
4615 /* Notify actions are the only type currently supported. */
4616 /* TODO support other type of action */
4617 assert(lttng_action_get_type(action
) ==
4618 LTTNG_ACTION_TYPE_NOTIFY
);
4621 * Check if any client is subscribed to the result of this
4624 client_list
= get_client_list_from_condition(state
, condition
);
4626 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4627 previous_sample_available
? &previous_sample
: NULL
,
4629 previous_session_consumed_total
,
4630 latest_session_consumed_total
,
4632 if (caa_unlikely(ret
)) {
4636 if (caa_likely(!evaluation
)) {
4641 * Ownership of `evaluation` transferred to the action executor
4642 * no matter the result.
4644 executor_status
= action_executor_enqueue(state
->executor
,
4645 trigger
, evaluation
, &channel_creds
,
4648 switch (executor_status
) {
4649 case ACTION_EXECUTOR_STATUS_OK
:
4651 case ACTION_EXECUTOR_STATUS_ERROR
:
4652 case ACTION_EXECUTOR_STATUS_INVALID
:
4654 * TODO Add trigger identification (name/id) when
4655 * it is added to the API.
4657 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4660 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4662 * TODO Add trigger identification (name/id) when
4663 * it is added to the API.
4665 * Not a fatal error.
4667 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4675 notification_client_list_put(client_list
);
4676 if (caa_unlikely(ret
)) {