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/notification/notification-internal.h>
25 #include <lttng/condition/condition-internal.h>
26 #include <lttng/condition/buffer-usage-internal.h>
27 #include <lttng/condition/session-consumed-size-internal.h>
28 #include <lttng/condition/session-rotation-internal.h>
29 #include <lttng/condition/event-rule-internal.h>
30 #include <lttng/domain-internal.h>
31 #include <lttng/notification/channel-internal.h>
32 #include <lttng/trigger/trigger-internal.h>
33 #include <lttng/event-rule/event-rule-internal.h>
41 #include "condition-internal.h"
42 #include "notification-thread.h"
43 #include "notification-thread-events.h"
44 #include "notification-thread-commands.h"
45 #include "lttng-sessiond.h"
48 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
49 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
51 enum lttng_object_type
{
52 LTTNG_OBJECT_TYPE_UNKNOWN
,
53 LTTNG_OBJECT_TYPE_NONE
,
54 LTTNG_OBJECT_TYPE_CHANNEL
,
55 LTTNG_OBJECT_TYPE_SESSION
,
58 struct lttng_trigger_list_element
{
59 /* No ownership of the trigger object is assumed. */
60 struct lttng_trigger
*trigger
;
61 struct cds_list_head node
;
64 struct lttng_channel_trigger_list
{
65 struct channel_key channel_key
;
66 /* List of struct lttng_trigger_list_element. */
67 struct cds_list_head list
;
68 /* Node in the channel_triggers_ht */
69 struct cds_lfht_node channel_triggers_ht_node
;
70 /* call_rcu delayed reclaim. */
71 struct rcu_head rcu_node
;
75 * List of triggers applying to a given session.
78 * - lttng_session_trigger_list_create()
79 * - lttng_session_trigger_list_build()
80 * - lttng_session_trigger_list_destroy()
81 * - lttng_session_trigger_list_add()
83 struct lttng_session_trigger_list
{
85 * Not owned by this; points to the session_info structure's
88 const char *session_name
;
89 /* List of struct lttng_trigger_list_element. */
90 struct cds_list_head list
;
91 /* Node in the session_triggers_ht */
92 struct cds_lfht_node session_triggers_ht_node
;
94 * Weak reference to the notification system's session triggers
97 * The session trigger list structure structure is owned by
98 * the session's session_info.
100 * The session_info is kept alive the the channel_infos holding a
101 * reference to it (reference counting). When those channels are
102 * destroyed (at runtime or on teardown), the reference they hold
103 * to the session_info are released. On destruction of session_info,
104 * session_info_destroy() will remove the list of triggers applying
105 * to this session from the notification system's state.
107 * This implies that the session_triggers_ht must be destroyed
108 * after the channels.
110 struct cds_lfht
*session_triggers_ht
;
111 /* Used for delayed RCU reclaim. */
112 struct rcu_head rcu_node
;
115 struct lttng_trigger_ht_element
{
116 struct lttng_trigger
*trigger
;
117 struct cds_lfht_node node
;
118 struct cds_lfht_node node_by_name_uid
;
119 /* call_rcu delayed reclaim. */
120 struct rcu_head rcu_node
;
123 struct lttng_condition_list_element
{
124 struct lttng_condition
*condition
;
125 struct cds_list_head node
;
128 struct channel_state_sample
{
129 struct channel_key key
;
130 struct cds_lfht_node channel_state_ht_node
;
131 uint64_t highest_usage
;
132 uint64_t lowest_usage
;
133 uint64_t channel_total_consumed
;
134 /* call_rcu delayed reclaim. */
135 struct rcu_head rcu_node
;
138 static unsigned long hash_channel_key(struct channel_key
*key
);
139 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
140 struct lttng_evaluation
**evaluation
,
141 const struct notification_thread_state
*state
,
142 const struct channel_state_sample
*previous_sample
,
143 const struct channel_state_sample
*latest_sample
,
144 uint64_t previous_session_consumed_total
,
145 uint64_t latest_session_consumed_total
,
146 struct channel_info
*channel_info
);
148 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
149 const struct lttng_evaluation
*evaluation
,
150 struct notification_client_list
*client_list
,
151 struct notification_thread_state
*state
,
152 uid_t channel_uid
, gid_t channel_gid
);
155 /* session_info API */
157 void session_info_destroy(void *_data
);
159 void session_info_get(struct session_info
*session_info
);
161 void session_info_put(struct session_info
*session_info
);
163 struct session_info
*session_info_create(const char *name
,
164 uid_t uid
, gid_t gid
,
165 struct lttng_session_trigger_list
*trigger_list
,
166 struct cds_lfht
*sessions_ht
);
168 void session_info_add_channel(struct session_info
*session_info
,
169 struct channel_info
*channel_info
);
171 void session_info_remove_channel(struct session_info
*session_info
,
172 struct channel_info
*channel_info
);
174 /* lttng_session_trigger_list API */
176 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
177 const char *session_name
,
178 struct cds_lfht
*session_triggers_ht
);
180 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
181 const struct notification_thread_state
*state
,
182 const char *session_name
);
184 void lttng_session_trigger_list_destroy(
185 struct lttng_session_trigger_list
*list
);
187 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
188 struct lttng_trigger
*trigger
);
191 int client_handle_transmission_status(
192 struct notification_client
*client
,
193 enum client_transmission_status transmission_status
,
194 struct notification_thread_state
*state
);
197 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
200 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
202 /* This double-cast is intended to supress pointer-to-cast warning. */
203 const int socket
= (int) (intptr_t) key
;
204 const struct notification_client
*client
= caa_container_of(node
,
205 struct notification_client
, client_socket_ht_node
);
207 return client
->socket
== socket
;
211 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
213 /* This double-cast is intended to supress pointer-to-cast warning. */
214 const notification_client_id id
= *((notification_client_id
*) key
);
215 const struct notification_client
*client
= caa_container_of(
216 node
, struct notification_client
, client_id_ht_node
);
218 return client
->id
== id
;
222 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
224 struct channel_key
*channel_key
= (struct channel_key
*) key
;
225 struct lttng_channel_trigger_list
*trigger_list
;
227 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
228 channel_triggers_ht_node
);
230 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
231 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
235 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
237 const char *session_name
= (const char *) key
;
238 struct lttng_session_trigger_list
*trigger_list
;
240 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
241 session_triggers_ht_node
);
243 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
247 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
249 struct channel_key
*channel_key
= (struct channel_key
*) key
;
250 struct channel_state_sample
*sample
;
252 sample
= caa_container_of(node
, struct channel_state_sample
,
253 channel_state_ht_node
);
255 return !!((channel_key
->key
== sample
->key
.key
) &&
256 (channel_key
->domain
== sample
->key
.domain
));
260 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
262 struct channel_key
*channel_key
= (struct channel_key
*) key
;
263 struct channel_info
*channel_info
;
265 channel_info
= caa_container_of(node
, struct channel_info
,
268 return !!((channel_key
->key
== channel_info
->key
.key
) &&
269 (channel_key
->domain
== channel_info
->key
.domain
));
273 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
275 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
276 struct lttng_trigger_ht_element
*trigger_ht_element
;
278 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
281 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
285 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
287 const uint64_t *_key
= key
;
288 struct notification_trigger_tokens_ht_element
*element
;
290 element
= caa_container_of(node
,
291 struct notification_trigger_tokens_ht_element
, node
);
292 return *_key
== element
->token
;
296 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
298 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
299 struct notification_client_list
*client_list
;
300 const struct lttng_condition
*condition
;
302 assert(condition_key
);
304 client_list
= caa_container_of(node
, struct notification_client_list
,
305 notification_trigger_clients_ht_node
);
306 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
308 return !!lttng_condition_is_equal(condition_key
, condition
);
312 int match_session(struct cds_lfht_node
*node
, const void *key
)
314 const char *name
= key
;
315 struct session_info
*session_info
= caa_container_of(
316 node
, struct session_info
, sessions_ht_node
);
318 return !strcmp(session_info
->name
, name
);
322 * Match trigger based on name and credentials only.
323 * Name duplication is NOT allowed for the same uid.
326 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
331 const char *key_name
;
332 enum lttng_trigger_status status
;
333 const struct lttng_credentials
*key_creds
;
334 const struct lttng_credentials
*node_creds
;
335 const struct lttng_trigger
*trigger_key
=
336 (const struct lttng_trigger
*) key
;
337 const struct lttng_trigger_ht_element
*trigger_ht_element
=
338 caa_container_of(node
,
339 struct lttng_trigger_ht_element
,
342 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
343 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
345 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
346 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
348 /* Compare the names. */
349 if (strcmp(name
, key_name
) != 0) {
353 /* Compare the owners' UIDs. */
354 key_creds
= lttng_trigger_get_credentials(trigger_key
);
355 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
357 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
364 * Hash trigger based on name and credentials only.
367 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
369 unsigned long hash
= 0;
370 const struct lttng_credentials
*trigger_creds
;
371 const char *trigger_name
;
372 enum lttng_trigger_status status
;
374 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
375 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
376 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
379 trigger_creds
= lttng_trigger_get_credentials(trigger
);
380 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
387 unsigned long hash_channel_key(struct channel_key
*key
)
389 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
390 unsigned long domain_hash
= hash_key_ulong(
391 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
393 return key_hash
^ domain_hash
;
397 unsigned long hash_client_socket(int socket
)
399 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
403 unsigned long hash_client_id(notification_client_id id
)
405 return hash_key_u64(&id
, lttng_ht_seed
);
409 * Get the type of object to which a given condition applies. Bindings let
410 * the notification system evaluate a trigger's condition when a given
411 * object's state is updated.
413 * For instance, a condition bound to a channel will be evaluated everytime
414 * the channel's state is changed by a channel monitoring sample.
417 enum lttng_object_type
get_condition_binding_object(
418 const struct lttng_condition
*condition
)
420 switch (lttng_condition_get_type(condition
)) {
421 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
422 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
423 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
424 return LTTNG_OBJECT_TYPE_CHANNEL
;
425 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
426 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
427 return LTTNG_OBJECT_TYPE_SESSION
;
428 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
429 return LTTNG_OBJECT_TYPE_NONE
;
431 return LTTNG_OBJECT_TYPE_UNKNOWN
;
436 void free_channel_info_rcu(struct rcu_head
*node
)
438 free(caa_container_of(node
, struct channel_info
, rcu_node
));
442 void channel_info_destroy(struct channel_info
*channel_info
)
448 if (channel_info
->session_info
) {
449 session_info_remove_channel(channel_info
->session_info
,
451 session_info_put(channel_info
->session_info
);
453 if (channel_info
->name
) {
454 free(channel_info
->name
);
456 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
460 void free_session_info_rcu(struct rcu_head
*node
)
462 free(caa_container_of(node
, struct session_info
, rcu_node
));
465 /* Don't call directly, use the ref-counting mechanism. */
467 void session_info_destroy(void *_data
)
469 struct session_info
*session_info
= _data
;
472 assert(session_info
);
473 if (session_info
->channel_infos_ht
) {
474 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
476 ERR("[notification-thread] Failed to destroy channel information hash table");
479 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
482 cds_lfht_del(session_info
->sessions_ht
,
483 &session_info
->sessions_ht_node
);
485 free(session_info
->name
);
486 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
490 void session_info_get(struct session_info
*session_info
)
495 lttng_ref_get(&session_info
->ref
);
499 void session_info_put(struct session_info
*session_info
)
504 lttng_ref_put(&session_info
->ref
);
508 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
509 struct lttng_session_trigger_list
*trigger_list
,
510 struct cds_lfht
*sessions_ht
)
512 struct session_info
*session_info
;
516 session_info
= zmalloc(sizeof(*session_info
));
520 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
522 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
523 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
524 if (!session_info
->channel_infos_ht
) {
528 cds_lfht_node_init(&session_info
->sessions_ht_node
);
529 session_info
->name
= strdup(name
);
530 if (!session_info
->name
) {
533 session_info
->uid
= uid
;
534 session_info
->gid
= gid
;
535 session_info
->trigger_list
= trigger_list
;
536 session_info
->sessions_ht
= sessions_ht
;
540 session_info_put(session_info
);
545 void session_info_add_channel(struct session_info
*session_info
,
546 struct channel_info
*channel_info
)
549 cds_lfht_add(session_info
->channel_infos_ht
,
550 hash_channel_key(&channel_info
->key
),
551 &channel_info
->session_info_channels_ht_node
);
556 void session_info_remove_channel(struct session_info
*session_info
,
557 struct channel_info
*channel_info
)
560 cds_lfht_del(session_info
->channel_infos_ht
,
561 &channel_info
->session_info_channels_ht_node
);
566 struct channel_info
*channel_info_create(const char *channel_name
,
567 struct channel_key
*channel_key
, uint64_t channel_capacity
,
568 struct session_info
*session_info
)
570 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
576 cds_lfht_node_init(&channel_info
->channels_ht_node
);
577 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
578 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
579 channel_info
->capacity
= channel_capacity
;
581 channel_info
->name
= strdup(channel_name
);
582 if (!channel_info
->name
) {
587 * Set the references between session and channel infos:
588 * - channel_info holds a strong reference to session_info
589 * - session_info holds a weak reference to channel_info
591 session_info_get(session_info
);
592 session_info_add_channel(session_info
, channel_info
);
593 channel_info
->session_info
= session_info
;
597 channel_info_destroy(channel_info
);
602 bool notification_client_list_get(struct notification_client_list
*list
)
604 return urcu_ref_get_unless_zero(&list
->ref
);
608 void free_notification_client_list_rcu(struct rcu_head
*node
)
610 free(caa_container_of(node
, struct notification_client_list
,
615 void notification_client_list_release(struct urcu_ref
*list_ref
)
617 struct notification_client_list
*list
=
618 container_of(list_ref
, typeof(*list
), ref
);
619 struct notification_client_list_element
*client_list_element
, *tmp
;
621 if (list
->notification_trigger_clients_ht
) {
623 cds_lfht_del(list
->notification_trigger_clients_ht
,
624 &list
->notification_trigger_clients_ht_node
);
626 list
->notification_trigger_clients_ht
= NULL
;
628 cds_list_for_each_entry_safe(client_list_element
, tmp
,
630 free(client_list_element
);
632 pthread_mutex_destroy(&list
->lock
);
633 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
637 struct notification_client_list
*notification_client_list_create(
638 const struct lttng_trigger
*trigger
)
640 struct notification_client_list
*client_list
=
641 zmalloc(sizeof(*client_list
));
646 pthread_mutex_init(&client_list
->lock
, NULL
);
647 urcu_ref_init(&client_list
->ref
);
648 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
649 CDS_INIT_LIST_HEAD(&client_list
->list
);
650 client_list
->trigger
= trigger
;
656 void publish_notification_client_list(
657 struct notification_thread_state
*state
,
658 struct notification_client_list
*list
)
660 const struct lttng_condition
*condition
=
661 lttng_trigger_get_const_condition(list
->trigger
);
663 assert(!list
->notification_trigger_clients_ht
);
664 notification_client_list_get(list
);
666 list
->notification_trigger_clients_ht
=
667 state
->notification_trigger_clients_ht
;
670 cds_lfht_add(state
->notification_trigger_clients_ht
,
671 lttng_condition_hash(condition
),
672 &list
->notification_trigger_clients_ht_node
);
677 void notification_client_list_put(struct notification_client_list
*list
)
682 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
685 /* Provides a reference to the returned list. */
687 struct notification_client_list
*get_client_list_from_condition(
688 struct notification_thread_state
*state
,
689 const struct lttng_condition
*condition
)
691 struct cds_lfht_node
*node
;
692 struct cds_lfht_iter iter
;
693 struct notification_client_list
*list
= NULL
;
696 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
697 lttng_condition_hash(condition
),
698 match_client_list_condition
,
701 node
= cds_lfht_iter_get_node(&iter
);
703 list
= container_of(node
, struct notification_client_list
,
704 notification_trigger_clients_ht_node
);
705 list
= notification_client_list_get(list
) ? list
: NULL
;
713 int evaluate_channel_condition_for_client(
714 const struct lttng_condition
*condition
,
715 struct notification_thread_state
*state
,
716 struct lttng_evaluation
**evaluation
,
717 uid_t
*session_uid
, gid_t
*session_gid
)
720 struct cds_lfht_iter iter
;
721 struct cds_lfht_node
*node
;
722 struct channel_info
*channel_info
= NULL
;
723 struct channel_key
*channel_key
= NULL
;
724 struct channel_state_sample
*last_sample
= NULL
;
725 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
729 /* Find the channel associated with the condition. */
730 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
731 channel_trigger_list
, channel_triggers_ht_node
) {
732 struct lttng_trigger_list_element
*element
;
734 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
735 const struct lttng_condition
*current_condition
=
736 lttng_trigger_get_const_condition(
739 assert(current_condition
);
740 if (!lttng_condition_is_equal(condition
,
741 current_condition
)) {
745 /* Found the trigger, save the channel key. */
746 channel_key
= &channel_trigger_list
->channel_key
;
750 /* The channel key was found stop iteration. */
756 /* No channel found; normal exit. */
757 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
762 /* Fetch channel info for the matching channel. */
763 cds_lfht_lookup(state
->channels_ht
,
764 hash_channel_key(channel_key
),
768 node
= cds_lfht_iter_get_node(&iter
);
770 channel_info
= caa_container_of(node
, struct channel_info
,
773 /* Retrieve the channel's last sample, if it exists. */
774 cds_lfht_lookup(state
->channel_state_ht
,
775 hash_channel_key(channel_key
),
776 match_channel_state_sample
,
779 node
= cds_lfht_iter_get_node(&iter
);
781 last_sample
= caa_container_of(node
,
782 struct channel_state_sample
,
783 channel_state_ht_node
);
785 /* Nothing to evaluate, no sample was ever taken. Normal exit */
786 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
791 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
793 0, channel_info
->session_info
->consumed_data_size
,
796 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
800 *session_uid
= channel_info
->session_info
->uid
;
801 *session_gid
= channel_info
->session_info
->gid
;
808 const char *get_condition_session_name(const struct lttng_condition
*condition
)
810 const char *session_name
= NULL
;
811 enum lttng_condition_status status
;
813 switch (lttng_condition_get_type(condition
)) {
814 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
815 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
816 status
= lttng_condition_buffer_usage_get_session_name(
817 condition
, &session_name
);
819 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
820 status
= lttng_condition_session_consumed_size_get_session_name(
821 condition
, &session_name
);
823 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
824 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
825 status
= lttng_condition_session_rotation_get_session_name(
826 condition
, &session_name
);
831 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
832 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
840 int evaluate_session_condition_for_client(
841 const struct lttng_condition
*condition
,
842 struct notification_thread_state
*state
,
843 struct lttng_evaluation
**evaluation
,
844 uid_t
*session_uid
, gid_t
*session_gid
)
847 struct cds_lfht_iter iter
;
848 struct cds_lfht_node
*node
;
849 const char *session_name
;
850 struct session_info
*session_info
= NULL
;
853 session_name
= get_condition_session_name(condition
);
855 /* Find the session associated with the trigger. */
856 cds_lfht_lookup(state
->sessions_ht
,
857 hash_key_str(session_name
, lttng_ht_seed
),
861 node
= cds_lfht_iter_get_node(&iter
);
863 DBG("[notification-thread] No known session matching name \"%s\"",
869 session_info
= caa_container_of(node
, struct session_info
,
871 session_info_get(session_info
);
874 * Evaluation is performed in-line here since only one type of
875 * session-bound condition is handled for the moment.
877 switch (lttng_condition_get_type(condition
)) {
878 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
879 if (!session_info
->rotation
.ongoing
) {
881 goto end_session_put
;
884 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
885 session_info
->rotation
.id
);
888 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
891 goto end_session_put
;
897 goto end_session_put
;
900 *session_uid
= session_info
->uid
;
901 *session_gid
= session_info
->gid
;
904 session_info_put(session_info
);
911 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
912 const struct lttng_condition
*condition
,
913 struct notification_client
*client
,
914 struct notification_thread_state
*state
)
917 struct lttng_evaluation
*evaluation
= NULL
;
918 struct notification_client_list client_list
= {
919 .lock
= PTHREAD_MUTEX_INITIALIZER
,
921 struct notification_client_list_element client_list_element
= { 0 };
922 uid_t object_uid
= 0;
923 gid_t object_gid
= 0;
930 switch (get_condition_binding_object(condition
)) {
931 case LTTNG_OBJECT_TYPE_SESSION
:
932 ret
= evaluate_session_condition_for_client(condition
, state
,
933 &evaluation
, &object_uid
, &object_gid
);
935 case LTTNG_OBJECT_TYPE_CHANNEL
:
936 ret
= evaluate_channel_condition_for_client(condition
, state
,
937 &evaluation
, &object_uid
, &object_gid
);
939 case LTTNG_OBJECT_TYPE_NONE
:
940 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
943 case LTTNG_OBJECT_TYPE_UNKNOWN
:
953 /* Evaluation yielded nothing. Normal exit. */
954 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
960 * Create a temporary client list with the client currently
963 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
964 CDS_INIT_LIST_HEAD(&client_list
.list
);
965 client_list
.trigger
= trigger
;
967 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
968 client_list_element
.client
= client
;
969 cds_list_add(&client_list_element
.node
, &client_list
.list
);
971 /* Send evaluation result to the newly-subscribed client. */
972 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
973 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
974 state
, object_uid
, object_gid
);
981 int notification_thread_client_subscribe(struct notification_client
*client
,
982 struct lttng_condition
*condition
,
983 struct notification_thread_state
*state
,
984 enum lttng_notification_channel_status
*_status
)
987 struct notification_client_list
*client_list
= NULL
;
988 struct lttng_condition_list_element
*condition_list_element
= NULL
;
989 struct notification_client_list_element
*client_list_element
= NULL
;
990 enum lttng_notification_channel_status status
=
991 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
994 * Ensure that the client has not already subscribed to this condition
997 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
998 if (lttng_condition_is_equal(condition_list_element
->condition
,
1000 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1005 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1006 if (!condition_list_element
) {
1010 client_list_element
= zmalloc(sizeof(*client_list_element
));
1011 if (!client_list_element
) {
1017 * Add the newly-subscribed condition to the client's subscription list.
1019 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1020 condition_list_element
->condition
= condition
;
1021 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1023 client_list
= get_client_list_from_condition(state
, condition
);
1026 * No notification-emiting trigger registered with this
1027 * condition. We don't evaluate the condition right away
1028 * since this trigger is not registered yet.
1030 free(client_list_element
);
1035 * The condition to which the client just subscribed is evaluated
1036 * at this point so that conditions that are already TRUE result
1037 * in a notification being sent out.
1039 * The client_list's trigger is used without locking the list itself.
1040 * This is correct since the list doesn't own the trigger and the
1041 * object is immutable.
1043 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1045 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1047 free(client_list_element
);
1052 * Add the client to the list of clients interested in a given trigger
1053 * if a "notification" trigger with a corresponding condition was
1056 client_list_element
->client
= client
;
1057 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1059 pthread_mutex_lock(&client_list
->lock
);
1060 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1061 pthread_mutex_unlock(&client_list
->lock
);
1067 notification_client_list_put(client_list
);
1071 free(condition_list_element
);
1072 free(client_list_element
);
1077 int notification_thread_client_unsubscribe(
1078 struct notification_client
*client
,
1079 struct lttng_condition
*condition
,
1080 struct notification_thread_state
*state
,
1081 enum lttng_notification_channel_status
*_status
)
1083 struct notification_client_list
*client_list
;
1084 struct lttng_condition_list_element
*condition_list_element
,
1086 struct notification_client_list_element
*client_list_element
,
1088 bool condition_found
= false;
1089 enum lttng_notification_channel_status status
=
1090 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1092 /* Remove the condition from the client's condition list. */
1093 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1094 &client
->condition_list
, node
) {
1095 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1100 cds_list_del(&condition_list_element
->node
);
1102 * The caller may be iterating on the client's conditions to
1103 * tear down a client's connection. In this case, the condition
1104 * will be destroyed at the end.
1106 if (condition
!= condition_list_element
->condition
) {
1107 lttng_condition_destroy(
1108 condition_list_element
->condition
);
1110 free(condition_list_element
);
1111 condition_found
= true;
1115 if (!condition_found
) {
1116 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1121 * Remove the client from the list of clients interested the trigger
1122 * matching the condition.
1124 client_list
= get_client_list_from_condition(state
, condition
);
1129 pthread_mutex_lock(&client_list
->lock
);
1130 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1131 &client_list
->list
, node
) {
1132 if (client_list_element
->client
->id
!= client
->id
) {
1135 cds_list_del(&client_list_element
->node
);
1136 free(client_list_element
);
1139 pthread_mutex_unlock(&client_list
->lock
);
1140 notification_client_list_put(client_list
);
1143 lttng_condition_destroy(condition
);
1151 void free_notification_client_rcu(struct rcu_head
*node
)
1153 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1157 void notification_client_destroy(struct notification_client
*client
,
1158 struct notification_thread_state
*state
)
1165 * The client object is not reachable by other threads, no need to lock
1168 if (client
->socket
>= 0) {
1169 (void) lttcomm_close_unix_sock(client
->socket
);
1170 client
->socket
= -1;
1172 client
->communication
.active
= false;
1173 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1174 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1175 pthread_mutex_destroy(&client
->lock
);
1176 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1180 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1184 struct notification_client
*get_client_from_socket(int socket
,
1185 struct notification_thread_state
*state
)
1187 struct cds_lfht_iter iter
;
1188 struct cds_lfht_node
*node
;
1189 struct notification_client
*client
= NULL
;
1191 cds_lfht_lookup(state
->client_socket_ht
,
1192 hash_client_socket(socket
),
1193 match_client_socket
,
1194 (void *) (unsigned long) socket
,
1196 node
= cds_lfht_iter_get_node(&iter
);
1201 client
= caa_container_of(node
, struct notification_client
,
1202 client_socket_ht_node
);
1208 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1212 struct notification_client
*get_client_from_id(notification_client_id id
,
1213 struct notification_thread_state
*state
)
1215 struct cds_lfht_iter iter
;
1216 struct cds_lfht_node
*node
;
1217 struct notification_client
*client
= NULL
;
1219 cds_lfht_lookup(state
->client_id_ht
,
1224 node
= cds_lfht_iter_get_node(&iter
);
1229 client
= caa_container_of(node
, struct notification_client
,
1236 bool buffer_usage_condition_applies_to_channel(
1237 const struct lttng_condition
*condition
,
1238 const struct channel_info
*channel_info
)
1240 enum lttng_condition_status status
;
1241 enum lttng_domain_type condition_domain
;
1242 const char *condition_session_name
= NULL
;
1243 const char *condition_channel_name
= NULL
;
1245 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1247 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1248 if (channel_info
->key
.domain
!= condition_domain
) {
1252 status
= lttng_condition_buffer_usage_get_session_name(
1253 condition
, &condition_session_name
);
1254 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1256 status
= lttng_condition_buffer_usage_get_channel_name(
1257 condition
, &condition_channel_name
);
1258 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1260 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1263 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1273 bool session_consumed_size_condition_applies_to_channel(
1274 const struct lttng_condition
*condition
,
1275 const struct channel_info
*channel_info
)
1277 enum lttng_condition_status status
;
1278 const char *condition_session_name
= NULL
;
1280 status
= lttng_condition_session_consumed_size_get_session_name(
1281 condition
, &condition_session_name
);
1282 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1284 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1294 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1295 const struct channel_info
*channel_info
)
1297 const struct lttng_condition
*condition
;
1298 bool trigger_applies
;
1300 condition
= lttng_trigger_get_const_condition(trigger
);
1305 switch (lttng_condition_get_type(condition
)) {
1306 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1307 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1308 trigger_applies
= buffer_usage_condition_applies_to_channel(
1309 condition
, channel_info
);
1311 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1312 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1313 condition
, channel_info
);
1319 return trigger_applies
;
1325 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1326 struct notification_client
*client
)
1328 bool applies
= false;
1329 struct lttng_condition_list_element
*condition_list_element
;
1331 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1333 applies
= lttng_condition_is_equal(
1334 condition_list_element
->condition
,
1335 lttng_trigger_get_condition(trigger
));
1343 /* Must be called with RCU read lock held. */
1345 struct lttng_session_trigger_list
*get_session_trigger_list(
1346 struct notification_thread_state
*state
,
1347 const char *session_name
)
1349 struct lttng_session_trigger_list
*list
= NULL
;
1350 struct cds_lfht_node
*node
;
1351 struct cds_lfht_iter iter
;
1353 cds_lfht_lookup(state
->session_triggers_ht
,
1354 hash_key_str(session_name
, lttng_ht_seed
),
1355 match_session_trigger_list
,
1358 node
= cds_lfht_iter_get_node(&iter
);
1361 * Not an error, the list of triggers applying to that session
1362 * will be initialized when the session is created.
1364 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1369 list
= caa_container_of(node
,
1370 struct lttng_session_trigger_list
,
1371 session_triggers_ht_node
);
1377 * Allocate an empty lttng_session_trigger_list for the session named
1380 * No ownership of 'session_name' is assumed by the session trigger list.
1381 * It is the caller's responsability to ensure the session name is alive
1382 * for as long as this list is.
1385 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1386 const char *session_name
,
1387 struct cds_lfht
*session_triggers_ht
)
1389 struct lttng_session_trigger_list
*list
;
1391 list
= zmalloc(sizeof(*list
));
1395 list
->session_name
= session_name
;
1396 CDS_INIT_LIST_HEAD(&list
->list
);
1397 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1398 list
->session_triggers_ht
= session_triggers_ht
;
1401 /* Publish the list through the session_triggers_ht. */
1402 cds_lfht_add(session_triggers_ht
,
1403 hash_key_str(session_name
, lttng_ht_seed
),
1404 &list
->session_triggers_ht_node
);
1411 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1413 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1418 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1420 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1422 /* Empty the list element by element, and then free the list itself. */
1423 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1424 &list
->list
, node
) {
1425 cds_list_del(&trigger_list_element
->node
);
1426 free(trigger_list_element
);
1429 /* Unpublish the list from the session_triggers_ht. */
1430 cds_lfht_del(list
->session_triggers_ht
,
1431 &list
->session_triggers_ht_node
);
1433 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1437 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1438 struct lttng_trigger
*trigger
)
1441 struct lttng_trigger_list_element
*new_element
=
1442 zmalloc(sizeof(*new_element
));
1448 CDS_INIT_LIST_HEAD(&new_element
->node
);
1449 new_element
->trigger
= trigger
;
1450 cds_list_add(&new_element
->node
, &list
->list
);
1456 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1457 const char *session_name
)
1459 bool applies
= false;
1460 const struct lttng_condition
*condition
;
1462 condition
= lttng_trigger_get_const_condition(trigger
);
1463 switch (lttng_condition_get_type(condition
)) {
1464 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1465 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1467 enum lttng_condition_status condition_status
;
1468 const char *condition_session_name
;
1470 condition_status
= lttng_condition_session_rotation_get_session_name(
1471 condition
, &condition_session_name
);
1472 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1473 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1477 assert(condition_session_name
);
1478 applies
= !strcmp(condition_session_name
, session_name
);
1489 * Allocate and initialize an lttng_session_trigger_list which contains
1490 * all triggers that apply to the session named 'session_name'.
1492 * No ownership of 'session_name' is assumed by the session trigger list.
1493 * It is the caller's responsability to ensure the session name is alive
1494 * for as long as this list is.
1497 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1498 const struct notification_thread_state
*state
,
1499 const char *session_name
)
1501 int trigger_count
= 0;
1502 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1503 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1504 struct cds_lfht_iter iter
;
1506 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1507 state
->session_triggers_ht
);
1509 /* Add all triggers applying to the session named 'session_name'. */
1510 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1514 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1519 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1520 trigger_ht_element
->trigger
);
1528 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1530 return session_trigger_list
;
1532 lttng_session_trigger_list_destroy(session_trigger_list
);
1537 struct session_info
*find_or_create_session_info(
1538 struct notification_thread_state
*state
,
1539 const char *name
, uid_t uid
, gid_t gid
)
1541 struct session_info
*session
= NULL
;
1542 struct cds_lfht_node
*node
;
1543 struct cds_lfht_iter iter
;
1544 struct lttng_session_trigger_list
*trigger_list
;
1547 cds_lfht_lookup(state
->sessions_ht
,
1548 hash_key_str(name
, lttng_ht_seed
),
1552 node
= cds_lfht_iter_get_node(&iter
);
1554 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1556 session
= caa_container_of(node
, struct session_info
,
1558 assert(session
->uid
== uid
);
1559 assert(session
->gid
== gid
);
1560 session_info_get(session
);
1564 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1565 if (!trigger_list
) {
1569 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1570 state
->sessions_ht
);
1572 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1574 lttng_session_trigger_list_destroy(trigger_list
);
1577 trigger_list
= NULL
;
1579 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1580 &session
->sessions_ht_node
);
1586 session_info_put(session
);
1591 int handle_notification_thread_command_add_channel(
1592 struct notification_thread_state
*state
,
1593 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1594 const char *channel_name
, enum lttng_domain_type channel_domain
,
1595 uint64_t channel_key_int
, uint64_t channel_capacity
,
1596 enum lttng_error_code
*cmd_result
)
1598 struct cds_list_head trigger_list
;
1599 struct channel_info
*new_channel_info
= NULL
;
1600 struct channel_key channel_key
= {
1601 .key
= channel_key_int
,
1602 .domain
= channel_domain
,
1604 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1605 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1606 int trigger_count
= 0;
1607 struct cds_lfht_iter iter
;
1608 struct session_info
*session_info
= NULL
;
1610 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1611 channel_name
, session_name
, channel_key_int
,
1612 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1614 CDS_INIT_LIST_HEAD(&trigger_list
);
1616 session_info
= find_or_create_session_info(state
, session_name
,
1617 session_uid
, session_gid
);
1618 if (!session_info
) {
1619 /* Allocation error or an internal error occurred. */
1623 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1624 channel_capacity
, session_info
);
1625 if (!new_channel_info
) {
1630 /* Build a list of all triggers applying to the new channel. */
1631 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1633 struct lttng_trigger_list_element
*new_element
;
1635 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1636 new_channel_info
)) {
1640 new_element
= zmalloc(sizeof(*new_element
));
1645 CDS_INIT_LIST_HEAD(&new_element
->node
);
1646 new_element
->trigger
= trigger_ht_element
->trigger
;
1647 cds_list_add(&new_element
->node
, &trigger_list
);
1652 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1654 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1655 if (!channel_trigger_list
) {
1658 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1659 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1660 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1661 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1664 /* Add channel to the channel_ht which owns the channel_infos. */
1665 cds_lfht_add(state
->channels_ht
,
1666 hash_channel_key(&new_channel_info
->key
),
1667 &new_channel_info
->channels_ht_node
);
1669 * Add the list of triggers associated with this channel to the
1670 * channel_triggers_ht.
1672 cds_lfht_add(state
->channel_triggers_ht
,
1673 hash_channel_key(&new_channel_info
->key
),
1674 &channel_trigger_list
->channel_triggers_ht_node
);
1676 session_info_put(session_info
);
1677 *cmd_result
= LTTNG_OK
;
1680 channel_info_destroy(new_channel_info
);
1681 session_info_put(session_info
);
1686 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1688 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1693 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1695 free(caa_container_of(node
, struct channel_state_sample
,
1700 int handle_notification_thread_command_remove_channel(
1701 struct notification_thread_state
*state
,
1702 uint64_t channel_key
, enum lttng_domain_type domain
,
1703 enum lttng_error_code
*cmd_result
)
1705 struct cds_lfht_node
*node
;
1706 struct cds_lfht_iter iter
;
1707 struct lttng_channel_trigger_list
*trigger_list
;
1708 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1709 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1710 struct channel_info
*channel_info
;
1712 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1713 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1717 cds_lfht_lookup(state
->channel_triggers_ht
,
1718 hash_channel_key(&key
),
1719 match_channel_trigger_list
,
1722 node
= cds_lfht_iter_get_node(&iter
);
1724 * There is a severe internal error if we are being asked to remove a
1725 * channel that doesn't exist.
1728 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1732 /* Free the list of triggers associated with this channel. */
1733 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1734 channel_triggers_ht_node
);
1735 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1736 &trigger_list
->list
, node
) {
1737 cds_list_del(&trigger_list_element
->node
);
1738 free(trigger_list_element
);
1740 cds_lfht_del(state
->channel_triggers_ht
, node
);
1741 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1743 /* Free sampled channel state. */
1744 cds_lfht_lookup(state
->channel_state_ht
,
1745 hash_channel_key(&key
),
1746 match_channel_state_sample
,
1749 node
= cds_lfht_iter_get_node(&iter
);
1751 * This is expected to be NULL if the channel is destroyed before we
1752 * received a sample.
1755 struct channel_state_sample
*sample
= caa_container_of(node
,
1756 struct channel_state_sample
,
1757 channel_state_ht_node
);
1759 cds_lfht_del(state
->channel_state_ht
, node
);
1760 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1763 /* Remove the channel from the channels_ht and free it. */
1764 cds_lfht_lookup(state
->channels_ht
,
1765 hash_channel_key(&key
),
1769 node
= cds_lfht_iter_get_node(&iter
);
1771 channel_info
= caa_container_of(node
, struct channel_info
,
1773 cds_lfht_del(state
->channels_ht
, node
);
1774 channel_info_destroy(channel_info
);
1777 *cmd_result
= LTTNG_OK
;
1782 int handle_notification_thread_command_session_rotation(
1783 struct notification_thread_state
*state
,
1784 enum notification_thread_command_type cmd_type
,
1785 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1786 uint64_t trace_archive_chunk_id
,
1787 struct lttng_trace_archive_location
*location
,
1788 enum lttng_error_code
*_cmd_result
)
1791 enum lttng_error_code cmd_result
= LTTNG_OK
;
1792 struct lttng_session_trigger_list
*trigger_list
;
1793 struct lttng_trigger_list_element
*trigger_list_element
;
1794 struct session_info
*session_info
;
1795 const struct lttng_credentials session_creds
= {
1796 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1797 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1802 session_info
= find_or_create_session_info(state
, session_name
,
1803 session_uid
, session_gid
);
1804 if (!session_info
) {
1805 /* Allocation error or an internal error occurred. */
1807 cmd_result
= LTTNG_ERR_NOMEM
;
1811 session_info
->rotation
.ongoing
=
1812 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1813 session_info
->rotation
.id
= trace_archive_chunk_id
;
1814 trigger_list
= get_session_trigger_list(state
, session_name
);
1815 if (!trigger_list
) {
1816 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1821 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1823 const struct lttng_condition
*condition
;
1824 struct lttng_trigger
*trigger
;
1825 struct notification_client_list
*client_list
;
1826 struct lttng_evaluation
*evaluation
= NULL
;
1827 enum lttng_condition_type condition_type
;
1828 enum action_executor_status executor_status
;
1830 trigger
= trigger_list_element
->trigger
;
1831 condition
= lttng_trigger_get_const_condition(trigger
);
1833 condition_type
= lttng_condition_get_type(condition
);
1835 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1836 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1838 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1839 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1843 client_list
= get_client_list_from_condition(state
, condition
);
1844 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1845 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1846 trace_archive_chunk_id
);
1848 evaluation
= lttng_evaluation_session_rotation_completed_create(
1849 trace_archive_chunk_id
, location
);
1853 /* Internal error */
1855 cmd_result
= LTTNG_ERR_UNK
;
1860 * Ownership of `evaluation` transferred to the action executor
1861 * no matter the result.
1863 executor_status
= action_executor_enqueue(state
->executor
,
1864 trigger
, evaluation
, &session_creds
,
1867 switch (executor_status
) {
1868 case ACTION_EXECUTOR_STATUS_OK
:
1870 case ACTION_EXECUTOR_STATUS_ERROR
:
1871 case ACTION_EXECUTOR_STATUS_INVALID
:
1873 * TODO Add trigger identification (name/id) when
1874 * it is added to the API.
1876 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1879 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1881 * TODO Add trigger identification (name/id) when
1882 * it is added to the API.
1884 * Not a fatal error.
1886 WARN("No space left when enqueuing action associated with session rotation trigger");
1894 notification_client_list_put(client_list
);
1895 if (caa_unlikely(ret
)) {
1900 session_info_put(session_info
);
1901 *_cmd_result
= cmd_result
;
1907 int handle_notification_thread_command_add_tracer_event_source(
1908 struct notification_thread_state
*state
,
1909 int tracer_event_source_fd
,
1910 enum lttng_domain_type domain_type
,
1911 enum lttng_error_code
*_cmd_result
)
1914 enum lttng_error_code cmd_result
= LTTNG_OK
;
1915 struct notification_event_tracer_event_source_element
*element
= NULL
;
1917 element
= zmalloc(sizeof(*element
));
1919 cmd_result
= LTTNG_ERR_NOMEM
;
1924 element
->fd
= tracer_event_source_fd
;
1925 element
->domain
= domain_type
;
1927 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
1929 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
1930 tracer_event_source_fd
,
1931 lttng_domain_type_str(domain_type
));
1933 /* Adding the read side pipe to the event poll. */
1934 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
1936 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
1937 tracer_event_source_fd
,
1938 lttng_domain_type_str(element
->domain
));
1939 cds_list_del(&element
->node
);
1944 element
->is_fd_in_poll_set
= true;
1947 *_cmd_result
= cmd_result
;
1952 int handle_notification_thread_command_remove_tracer_event_source(
1953 struct notification_thread_state
*state
,
1954 int tracer_event_source_fd
,
1955 enum lttng_error_code
*_cmd_result
)
1959 enum lttng_error_code cmd_result
= LTTNG_OK
;
1960 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
1962 cds_list_for_each_entry_safe(source_element
, tmp
,
1963 &state
->tracer_event_sources_list
, node
) {
1964 if (source_element
->fd
!= tracer_event_source_fd
) {
1968 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
1969 tracer_event_source_fd
,
1970 lttng_domain_type_str(source_element
->domain
));
1971 cds_list_del(&source_element
->node
);
1978 * This is temporarily allowed since the poll activity set is
1979 * not properly cleaned-up for the moment. This is adressed in
1982 source_element
= NULL
;
1986 if (!source_element
->is_fd_in_poll_set
) {
1987 /* Skip the poll set removal. */
1991 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
1992 tracer_event_source_fd
,
1993 lttng_domain_type_str(source_element
->domain
));
1995 /* Removing the fd from the event poll set. */
1996 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
1998 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
1999 tracer_event_source_fd
,
2000 lttng_domain_type_str(source_element
->domain
));
2001 cmd_result
= LTTNG_ERR_FATAL
;
2005 source_element
->is_fd_in_poll_set
= false;
2008 free(source_element
);
2009 *_cmd_result
= cmd_result
;
2013 int handle_notification_thread_remove_tracer_event_source_no_result(
2014 struct notification_thread_state
*state
,
2015 int tracer_event_source_fd
)
2018 enum lttng_error_code cmd_result
;
2020 ret
= handle_notification_thread_command_remove_tracer_event_source(
2021 state
, tracer_event_source_fd
, &cmd_result
);
2026 static int handle_notification_thread_command_list_triggers(
2027 struct notification_thread_handle
*handle
,
2028 struct notification_thread_state
*state
,
2030 struct lttng_triggers
**triggers
,
2031 enum lttng_error_code
*_cmd_result
)
2034 enum lttng_error_code cmd_result
= LTTNG_OK
;
2035 struct cds_lfht_iter iter
;
2036 struct lttng_trigger_ht_element
*trigger_ht_element
;
2037 struct lttng_triggers
*local_triggers
= NULL
;
2038 const struct lttng_credentials
*creds
;
2042 local_triggers
= lttng_triggers_create();
2043 if (!local_triggers
) {
2044 /* Not a fatal error. */
2045 cmd_result
= LTTNG_ERR_NOMEM
;
2049 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2050 trigger_ht_element
, node
) {
2052 * Only return the triggers to which the client has access.
2053 * The root user has visibility over all triggers.
2055 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2056 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2060 ret
= lttng_triggers_add(local_triggers
,
2061 trigger_ht_element
->trigger
);
2063 /* Not a fatal error. */
2065 cmd_result
= LTTNG_ERR_NOMEM
;
2070 /* Transferring ownership to the caller. */
2071 *triggers
= local_triggers
;
2072 local_triggers
= NULL
;
2076 lttng_triggers_destroy(local_triggers
);
2077 *_cmd_result
= cmd_result
;
2082 bool condition_is_supported(struct lttng_condition
*condition
)
2086 switch (lttng_condition_get_type(condition
)) {
2087 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2088 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2091 enum lttng_domain_type domain
;
2093 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2097 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2098 is_supported
= true;
2103 * Older kernel tracers don't expose the API to monitor their
2104 * buffers. Therefore, we reject triggers that require that
2105 * mechanism to be available to be evaluated.
2107 * Assume unsupported on error.
2109 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2112 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
2114 const struct lttng_event_rule
*event_rule
;
2115 enum lttng_domain_type domain
;
2116 const enum lttng_condition_status status
=
2117 lttng_condition_event_rule_get_rule(
2118 condition
, &event_rule
);
2120 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2122 domain
= lttng_event_rule_get_domain_type(event_rule
);
2123 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2124 is_supported
= true;
2129 * Older kernel tracers can't emit notification. Therefore, we
2130 * reject triggers that require that mechanism to be available
2133 * Assume unsupported on error.
2135 is_supported
= kernel_supports_event_notifiers() == 1;
2139 is_supported
= true;
2142 return is_supported
;
2145 /* Must be called with RCU read lock held. */
2147 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2148 struct notification_thread_state
*state
)
2151 const struct lttng_condition
*condition
;
2152 const char *session_name
;
2153 struct lttng_session_trigger_list
*trigger_list
;
2155 condition
= lttng_trigger_get_const_condition(trigger
);
2156 switch (lttng_condition_get_type(condition
)) {
2157 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2158 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2160 enum lttng_condition_status status
;
2162 status
= lttng_condition_session_rotation_get_session_name(
2163 condition
, &session_name
);
2164 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2165 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2176 trigger_list
= get_session_trigger_list(state
, session_name
);
2177 if (!trigger_list
) {
2178 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2184 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2186 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2191 /* Must be called with RCU read lock held. */
2193 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2194 struct notification_thread_state
*state
)
2197 struct cds_lfht_node
*node
;
2198 struct cds_lfht_iter iter
;
2199 struct channel_info
*channel
;
2201 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2203 struct lttng_trigger_list_element
*trigger_list_element
;
2204 struct lttng_channel_trigger_list
*trigger_list
;
2205 struct cds_lfht_iter lookup_iter
;
2207 if (!trigger_applies_to_channel(trigger
, channel
)) {
2211 cds_lfht_lookup(state
->channel_triggers_ht
,
2212 hash_channel_key(&channel
->key
),
2213 match_channel_trigger_list
,
2216 node
= cds_lfht_iter_get_node(&lookup_iter
);
2218 trigger_list
= caa_container_of(node
,
2219 struct lttng_channel_trigger_list
,
2220 channel_triggers_ht_node
);
2222 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2223 if (!trigger_list_element
) {
2227 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2228 trigger_list_element
->trigger
= trigger
;
2229 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2230 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2238 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2240 bool is_notify
= false;
2241 unsigned int i
, count
;
2242 enum lttng_action_status action_status
;
2243 const struct lttng_action
*action
=
2244 lttng_trigger_get_const_action(trigger
);
2245 enum lttng_action_type action_type
;
2248 action_type
= lttng_action_get_type(action
);
2249 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2252 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2256 action_status
= lttng_action_group_get_count(action
, &count
);
2257 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2259 for (i
= 0; i
< count
; i
++) {
2260 const struct lttng_action
*inner_action
=
2261 lttng_action_group_get_at_index(
2264 action_type
= lttng_action_get_type(inner_action
);
2265 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2275 static bool trigger_name_taken(struct notification_thread_state
*state
,
2276 const struct lttng_trigger
*trigger
)
2278 struct cds_lfht_iter iter
;
2281 * No duplicata is allowed in the triggers_by_name_uid_ht.
2282 * The match is done against the trigger name and uid.
2284 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2285 hash_trigger_by_name_uid(trigger
),
2286 match_trigger_by_name_uid
,
2289 return !!cds_lfht_iter_get_node(&iter
);
2293 enum lttng_error_code
generate_trigger_name(
2294 struct notification_thread_state
*state
,
2295 struct lttng_trigger
*trigger
, const char **name
)
2297 enum lttng_error_code ret_code
= LTTNG_OK
;
2299 enum lttng_trigger_status status
;
2302 const int ret
= lttng_trigger_generate_name(trigger
,
2303 state
->trigger_id
.name_offset
++);
2305 /* The only reason this can fail right now. */
2306 ret_code
= LTTNG_ERR_NOMEM
;
2310 status
= lttng_trigger_get_name(trigger
, name
);
2311 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2313 taken
= trigger_name_taken(state
, trigger
);
2314 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2320 * FIXME A client's credentials are not checked when registering a trigger.
2322 * The effects of this are benign since:
2323 * - The client will succeed in registering the trigger, as it is valid,
2324 * - The trigger will, internally, be bound to the channel/session,
2325 * - The notifications will not be sent since the client's credentials
2326 * are checked against the channel at that moment.
2328 * If this function returns a non-zero value, it means something is
2329 * fundamentally broken and the whole subsystem/thread will be torn down.
2331 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2335 int handle_notification_thread_command_register_trigger(
2336 struct notification_thread_state
*state
,
2337 struct lttng_trigger
*trigger
,
2338 enum lttng_error_code
*cmd_result
)
2341 struct lttng_condition
*condition
;
2342 struct notification_client
*client
;
2343 struct notification_client_list
*client_list
= NULL
;
2344 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2345 struct notification_client_list_element
*client_list_element
;
2346 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2347 struct cds_lfht_node
*node
;
2348 struct cds_lfht_iter iter
;
2349 const char* trigger_name
;
2350 bool free_trigger
= true;
2351 struct lttng_evaluation
*evaluation
= NULL
;
2352 struct lttng_credentials object_creds
;
2355 enum action_executor_status executor_status
;
2356 const uint64_t trigger_tracer_token
=
2357 state
->trigger_id
.next_tracer_token
++;
2361 /* Set the trigger's tracer token. */
2362 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2364 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2365 LTTNG_TRIGGER_STATUS_UNSET
) {
2366 const enum lttng_error_code ret_code
= generate_trigger_name(
2367 state
, trigger
, &trigger_name
);
2369 if (ret_code
!= LTTNG_OK
) {
2372 *cmd_result
= ret_code
;
2375 } else if (trigger_name_taken(state
, trigger
)) {
2376 /* Not a fatal error. */
2377 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2382 condition
= lttng_trigger_get_condition(trigger
);
2385 /* Some conditions require tracers to implement a minimal ABI version. */
2386 if (!condition_is_supported(condition
)) {
2387 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2391 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2392 if (!trigger_ht_element
) {
2397 /* Add trigger to the trigger_ht. */
2398 cds_lfht_node_init(&trigger_ht_element
->node
);
2399 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2400 trigger_ht_element
->trigger
= trigger
;
2402 node
= cds_lfht_add_unique(state
->triggers_ht
,
2403 lttng_condition_hash(condition
),
2406 &trigger_ht_element
->node
);
2407 if (node
!= &trigger_ht_element
->node
) {
2408 /* Not a fatal error, simply report it to the client. */
2409 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2410 goto error_free_ht_element
;
2413 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2414 hash_trigger_by_name_uid(trigger
),
2415 match_trigger_by_name_uid
,
2417 &trigger_ht_element
->node_by_name_uid
);
2418 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2419 /* Not a fatal error, simply report it to the client. */
2420 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2421 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2422 goto error_free_ht_element
;
2425 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2426 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2427 if (!trigger_tokens_ht_element
) {
2430 cds_lfht_del(state
->triggers_ht
,
2431 &trigger_ht_element
->node
);
2432 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2433 &trigger_ht_element
->node_by_name_uid
);
2434 goto error_free_ht_element
;
2437 /* Add trigger token to the trigger_tokens_ht. */
2438 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2439 trigger_tokens_ht_element
->token
=
2440 LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2441 trigger_tokens_ht_element
->trigger
= trigger
;
2443 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2444 hash_key_u64(&trigger_tokens_ht_element
->token
,
2446 match_trigger_token
,
2447 &trigger_tokens_ht_element
->token
,
2448 &trigger_tokens_ht_element
->node
);
2449 if (node
!= &trigger_tokens_ht_element
->node
) {
2450 /* Internal corruption, fatal error. */
2452 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2453 cds_lfht_del(state
->triggers_ht
,
2454 &trigger_ht_element
->node
);
2455 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2456 &trigger_ht_element
->node_by_name_uid
);
2457 goto error_free_ht_element
;
2462 * Ownership of the trigger and of its wrapper was transfered to
2463 * the triggers_ht. Same for token ht element if necessary.
2465 trigger_tokens_ht_element
= NULL
;
2466 trigger_ht_element
= NULL
;
2467 free_trigger
= false;
2470 * The rest only applies to triggers that have a "notify" action.
2471 * It is not skipped as this is the only action type currently
2474 if (is_trigger_action_notify(trigger
)) {
2475 client_list
= notification_client_list_create(trigger
);
2478 goto error_free_ht_element
;
2481 /* Build a list of clients to which this new trigger applies. */
2482 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2483 client_socket_ht_node
) {
2484 if (!trigger_applies_to_client(trigger
, client
)) {
2488 client_list_element
=
2489 zmalloc(sizeof(*client_list_element
));
2490 if (!client_list_element
) {
2492 goto error_put_client_list
;
2495 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2496 client_list_element
->client
= client
;
2497 cds_list_add(&client_list_element
->node
,
2498 &client_list
->list
);
2502 * Client list ownership transferred to the
2503 * notification_trigger_clients_ht.
2505 publish_notification_client_list(state
, client_list
);
2508 switch (get_condition_binding_object(condition
)) {
2509 case LTTNG_OBJECT_TYPE_SESSION
:
2510 /* Add the trigger to the list if it matches a known session. */
2511 ret
= bind_trigger_to_matching_session(trigger
, state
);
2513 goto error_put_client_list
;
2516 case LTTNG_OBJECT_TYPE_CHANNEL
:
2518 * Add the trigger to list of triggers bound to the channels
2521 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2523 goto error_put_client_list
;
2526 case LTTNG_OBJECT_TYPE_NONE
:
2529 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2531 goto error_put_client_list
;
2535 * The new trigger's condition must be evaluated against the current
2538 * In the case of `notify` action, nothing preventing clients from
2539 * subscribing to a condition before the corresponding trigger is
2540 * registered, we have to evaluate this new condition right away.
2542 * At some point, we were waiting for the next "evaluation" (e.g. on
2543 * reception of a channel sample) to evaluate this new condition, but
2546 * The reason it was broken is that waiting for the next sample
2547 * does not allow us to properly handle transitions for edge-triggered
2550 * Consider this example: when we handle a new channel sample, we
2551 * evaluate each conditions twice: once with the previous state, and
2552 * again with the newest state. We then use those two results to
2553 * determine whether a state change happened: a condition was false and
2554 * became true. If a state change happened, we have to notify clients.
2556 * Now, if a client subscribes to a given notification and registers
2557 * a trigger *after* that subscription, we have to make sure the
2558 * condition is evaluated at this point while considering only the
2559 * current state. Otherwise, the next evaluation cycle may only see
2560 * that the evaluations remain the same (true for samples n-1 and n) and
2561 * the client will never know that the condition has been met.
2563 switch (get_condition_binding_object(condition
)) {
2564 case LTTNG_OBJECT_TYPE_SESSION
:
2565 ret
= evaluate_session_condition_for_client(condition
, state
,
2566 &evaluation
, &object_uid
,
2569 case LTTNG_OBJECT_TYPE_CHANNEL
:
2570 ret
= evaluate_channel_condition_for_client(condition
, state
,
2571 &evaluation
, &object_uid
,
2574 case LTTNG_OBJECT_TYPE_NONE
:
2577 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2585 goto error_put_client_list
;
2588 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2589 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2591 DBG("Newly registered trigger's condition evaluated to %s",
2592 evaluation
? "true" : "false");
2594 /* Evaluation yielded nothing. Normal exit. */
2600 * Ownership of `evaluation` transferred to the action executor
2601 * no matter the result.
2603 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2604 evaluation
, &object_creds
, client_list
);
2606 switch (executor_status
) {
2607 case ACTION_EXECUTOR_STATUS_OK
:
2609 case ACTION_EXECUTOR_STATUS_ERROR
:
2610 case ACTION_EXECUTOR_STATUS_INVALID
:
2612 * TODO Add trigger identification (name/id) when
2613 * it is added to the API.
2615 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2617 goto error_put_client_list
;
2618 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2620 * TODO Add trigger identification (name/id) when
2621 * it is added to the API.
2623 * Not a fatal error.
2625 WARN("No space left when enqueuing action associated to newly registered trigger");
2633 *cmd_result
= LTTNG_OK
;
2634 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2635 trigger_name
, trigger_tracer_token
);
2637 error_put_client_list
:
2638 notification_client_list_put(client_list
);
2640 error_free_ht_element
:
2641 if (trigger_ht_element
) {
2642 /* Delayed removal due to RCU constraint on delete. */
2643 call_rcu(&trigger_ht_element
->rcu_node
,
2644 free_lttng_trigger_ht_element_rcu
);
2647 free(trigger_tokens_ht_element
);
2650 lttng_trigger_destroy(trigger
);
2657 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2659 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2664 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2666 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2671 int handle_notification_thread_command_unregister_trigger(
2672 struct notification_thread_state
*state
,
2673 struct lttng_trigger
*trigger
,
2674 enum lttng_error_code
*_cmd_reply
)
2676 struct cds_lfht_iter iter
;
2677 struct cds_lfht_node
*triggers_ht_node
;
2678 struct lttng_channel_trigger_list
*trigger_list
;
2679 struct notification_client_list
*client_list
;
2680 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2681 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2683 enum lttng_error_code cmd_reply
;
2687 cds_lfht_lookup(state
->triggers_ht
,
2688 lttng_condition_hash(condition
),
2692 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2693 if (!triggers_ht_node
) {
2694 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2697 cmd_reply
= LTTNG_OK
;
2700 /* Remove trigger from channel_triggers_ht. */
2701 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2702 channel_triggers_ht_node
) {
2703 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2705 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2706 &trigger_list
->list
, node
) {
2707 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2711 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2712 cds_list_del(&trigger_element
->node
);
2713 /* A trigger can only appear once per channel */
2718 if (lttng_condition_get_type(condition
) ==
2719 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2720 struct notification_trigger_tokens_ht_element
2721 *trigger_tokens_ht_element
;
2723 cds_lfht_for_each_entry (state
->trigger_tokens_ht
, &iter
,
2724 trigger_tokens_ht_element
, node
) {
2725 if (!lttng_trigger_is_equal(trigger
,
2726 trigger_tokens_ht_element
->trigger
)) {
2730 DBG("[notification-thread] Removed trigger from tokens_ht");
2731 cds_lfht_del(state
->trigger_tokens_ht
,
2732 &trigger_tokens_ht_element
->node
);
2733 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2734 free_notification_trigger_tokens_ht_element_rcu
);
2740 if (is_trigger_action_notify(trigger
)) {
2742 * Remove and release the client list from
2743 * notification_trigger_clients_ht.
2745 client_list
= get_client_list_from_condition(state
, condition
);
2746 assert(client_list
);
2748 /* Put new reference and the hashtable's reference. */
2749 notification_client_list_put(client_list
);
2750 notification_client_list_put(client_list
);
2754 /* Remove trigger from triggers_ht. */
2755 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2756 struct lttng_trigger_ht_element
, node
);
2757 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2758 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2760 /* Release the ownership of the trigger. */
2761 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2762 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2766 *_cmd_reply
= cmd_reply
;
2771 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2772 int handle_notification_thread_command(
2773 struct notification_thread_handle
*handle
,
2774 struct notification_thread_state
*state
)
2778 struct notification_thread_command
*cmd
;
2780 /* Read the event pipe to put it back into a quiescent state. */
2781 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2783 if (ret
!= sizeof(counter
)) {
2787 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2788 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2789 struct notification_thread_command
, cmd_list_node
);
2790 cds_list_del(&cmd
->cmd_list_node
);
2791 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2792 switch (cmd
->type
) {
2793 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2794 DBG("[notification-thread] Received register trigger command");
2795 ret
= handle_notification_thread_command_register_trigger(
2796 state
, cmd
->parameters
.trigger
,
2799 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2800 DBG("[notification-thread] Received unregister trigger command");
2801 ret
= handle_notification_thread_command_unregister_trigger(
2802 state
, cmd
->parameters
.trigger
,
2805 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2806 DBG("[notification-thread] Received add channel command");
2807 ret
= handle_notification_thread_command_add_channel(
2809 cmd
->parameters
.add_channel
.session
.name
,
2810 cmd
->parameters
.add_channel
.session
.uid
,
2811 cmd
->parameters
.add_channel
.session
.gid
,
2812 cmd
->parameters
.add_channel
.channel
.name
,
2813 cmd
->parameters
.add_channel
.channel
.domain
,
2814 cmd
->parameters
.add_channel
.channel
.key
,
2815 cmd
->parameters
.add_channel
.channel
.capacity
,
2818 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2819 DBG("[notification-thread] Received remove channel command");
2820 ret
= handle_notification_thread_command_remove_channel(
2821 state
, cmd
->parameters
.remove_channel
.key
,
2822 cmd
->parameters
.remove_channel
.domain
,
2825 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2826 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2827 DBG("[notification-thread] Received session rotation %s command",
2828 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2829 "ongoing" : "completed");
2830 ret
= handle_notification_thread_command_session_rotation(
2833 cmd
->parameters
.session_rotation
.session_name
,
2834 cmd
->parameters
.session_rotation
.uid
,
2835 cmd
->parameters
.session_rotation
.gid
,
2836 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2837 cmd
->parameters
.session_rotation
.location
,
2840 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
2841 ret
= handle_notification_thread_command_add_tracer_event_source(
2843 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2844 cmd
->parameters
.tracer_event_source
.domain
,
2847 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
2848 ret
= handle_notification_thread_command_remove_tracer_event_source(
2850 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2853 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2855 struct lttng_triggers
*triggers
= NULL
;
2857 ret
= handle_notification_thread_command_list_triggers(
2860 cmd
->parameters
.list_triggers
.uid
,
2863 cmd
->reply
.list_triggers
.triggers
= triggers
;
2867 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2868 DBG("[notification-thread] Received quit command");
2869 cmd
->reply_code
= LTTNG_OK
;
2872 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2874 const enum client_transmission_status client_status
=
2875 cmd
->parameters
.client_communication_update
2877 const notification_client_id client_id
=
2878 cmd
->parameters
.client_communication_update
.id
;
2879 struct notification_client
*client
;
2882 client
= get_client_from_id(client_id
, state
);
2886 * Client error was probably already picked-up by the
2887 * notification thread or it has disconnected
2888 * gracefully while this command was queued.
2890 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2894 ret
= client_handle_transmission_status(
2895 client
, client_status
, state
);
2901 ERR("[notification-thread] Unknown internal command received");
2909 if (cmd
->is_async
) {
2913 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2917 /* Wake-up and return a fatal error to the calling thread. */
2918 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2919 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2921 /* Indicate a fatal error to the caller. */
2926 int socket_set_non_blocking(int socket
)
2930 /* Set the pipe as non-blocking. */
2931 ret
= fcntl(socket
, F_GETFL
, 0);
2933 PERROR("fcntl get socket flags");
2938 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2940 PERROR("fcntl set O_NONBLOCK socket flag");
2943 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2949 int client_reset_inbound_state(struct notification_client
*client
)
2954 lttng_payload_clear(&client
->communication
.inbound
.payload
);
2956 client
->communication
.inbound
.bytes_to_receive
=
2957 sizeof(struct lttng_notification_channel_message
);
2958 client
->communication
.inbound
.msg_type
=
2959 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2960 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2961 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2962 ret
= lttng_dynamic_buffer_set_size(
2963 &client
->communication
.inbound
.payload
.buffer
,
2964 client
->communication
.inbound
.bytes_to_receive
);
2969 int handle_notification_thread_client_connect(
2970 struct notification_thread_state
*state
)
2973 struct notification_client
*client
;
2975 DBG("[notification-thread] Handling new notification channel client connection");
2977 client
= zmalloc(sizeof(*client
));
2984 pthread_mutex_init(&client
->lock
, NULL
);
2985 client
->id
= state
->next_notification_client_id
++;
2986 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2987 lttng_payload_init(&client
->communication
.inbound
.payload
);
2988 lttng_payload_init(&client
->communication
.outbound
.payload
);
2989 client
->communication
.inbound
.expect_creds
= true;
2991 ret
= client_reset_inbound_state(client
);
2993 ERR("[notification-thread] Failed to reset client communication's inbound state");
2998 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3000 ERR("[notification-thread] Failed to accept new notification channel client connection");
3005 client
->socket
= ret
;
3007 ret
= socket_set_non_blocking(client
->socket
);
3009 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3013 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3015 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3020 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3021 LPOLLIN
| LPOLLERR
|
3022 LPOLLHUP
| LPOLLRDHUP
);
3024 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3028 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3032 cds_lfht_add(state
->client_socket_ht
,
3033 hash_client_socket(client
->socket
),
3034 &client
->client_socket_ht_node
);
3035 cds_lfht_add(state
->client_id_ht
,
3036 hash_client_id(client
->id
),
3037 &client
->client_id_ht_node
);
3043 notification_client_destroy(client
, state
);
3048 * RCU read-lock must be held by the caller.
3049 * Client lock must _not_ be held by the caller.
3052 int notification_thread_client_disconnect(
3053 struct notification_client
*client
,
3054 struct notification_thread_state
*state
)
3057 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3059 /* Acquire the client lock to disable its communication atomically. */
3060 pthread_mutex_lock(&client
->lock
);
3061 client
->communication
.active
= false;
3062 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3063 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3064 pthread_mutex_unlock(&client
->lock
);
3066 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3068 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3072 /* Release all conditions to which the client was subscribed. */
3073 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3074 &client
->condition_list
, node
) {
3075 (void) notification_thread_client_unsubscribe(client
,
3076 condition_list_element
->condition
, state
, NULL
);
3080 * Client no longer accessible to other threads (through the
3083 notification_client_destroy(client
, state
);
3087 int handle_notification_thread_client_disconnect(
3088 int client_socket
, struct notification_thread_state
*state
)
3091 struct notification_client
*client
;
3094 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3096 client
= get_client_from_socket(client_socket
, state
);
3098 /* Internal state corruption, fatal error. */
3099 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3105 ret
= notification_thread_client_disconnect(client
, state
);
3111 int handle_notification_thread_client_disconnect_all(
3112 struct notification_thread_state
*state
)
3114 struct cds_lfht_iter iter
;
3115 struct notification_client
*client
;
3116 bool error_encoutered
= false;
3119 DBG("[notification-thread] Closing all client connections");
3120 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3121 client_socket_ht_node
) {
3124 ret
= notification_thread_client_disconnect(
3127 error_encoutered
= true;
3131 return error_encoutered
? 1 : 0;
3134 int handle_notification_thread_trigger_unregister_all(
3135 struct notification_thread_state
*state
)
3137 bool error_occurred
= false;
3138 struct cds_lfht_iter iter
;
3139 struct lttng_trigger_ht_element
*trigger_ht_element
;
3142 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3144 int ret
= handle_notification_thread_command_unregister_trigger(
3145 state
, trigger_ht_element
->trigger
, NULL
);
3147 error_occurred
= true;
3151 return error_occurred
? -1 : 0;
3155 int client_handle_transmission_status(
3156 struct notification_client
*client
,
3157 enum client_transmission_status transmission_status
,
3158 struct notification_thread_state
*state
)
3162 switch (transmission_status
) {
3163 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3164 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3165 CLIENT_POLL_MASK_IN
);
3171 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3173 * We want to be notified whenever there is buffer space
3174 * available to send the rest of the payload.
3176 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3177 CLIENT_POLL_MASK_IN_OUT
);
3182 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3183 ret
= notification_thread_client_disconnect(client
, state
);
3188 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3198 /* Client lock must be acquired by caller. */
3200 enum client_transmission_status
client_flush_outgoing_queue(
3201 struct notification_client
*client
)
3204 size_t to_send_count
;
3205 enum client_transmission_status status
;
3206 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3207 &client
->communication
.outbound
.payload
, 0, -1);
3208 const int fds_to_send_count
=
3209 lttng_payload_view_get_fd_handle_count(&pv
);
3211 ASSERT_LOCKED(client
->lock
);
3213 if (!client
->communication
.active
) {
3214 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3218 if (pv
.buffer
.size
== 0) {
3220 * If both data and fds are equal to zero, we are in an invalid
3223 assert(fds_to_send_count
!= 0);
3228 to_send_count
= pv
.buffer
.size
;
3229 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3232 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3235 if ((ret
>= 0 && ret
< to_send_count
)) {
3236 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3238 to_send_count
-= max(ret
, 0);
3240 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3242 pv
.buffer
.size
- to_send_count
,
3244 ret
= lttng_dynamic_buffer_set_size(
3245 &client
->communication
.outbound
.payload
.buffer
,
3251 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3253 } else if (ret
< 0) {
3254 /* Generic error, disable the client's communication. */
3255 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3257 client
->communication
.active
= false;
3258 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3262 * No error and flushed the queue completely.
3264 * The payload buffer size is used later to
3265 * check if there is notifications queued. So albeit that the
3266 * direct caller knows that the transmission is complete, we
3267 * need to set the buffer size to zero.
3269 ret
= lttng_dynamic_buffer_set_size(
3270 &client
->communication
.outbound
.payload
.buffer
, 0);
3277 /* No fds to send, transmission is complete. */
3278 if (fds_to_send_count
== 0) {
3279 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3283 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3284 client
->socket
, &pv
);
3286 /* Generic error, disable the client's communication. */
3287 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3289 client
->communication
.active
= false;
3290 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3292 } else if (ret
== 0) {
3293 /* Nothing could be sent. */
3294 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3296 /* Fd passing is an all or nothing kind of thing. */
3297 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3299 * The payload _fd_array count is used later to
3300 * check if there is notifications queued. So although the
3301 * direct caller knows that the transmission is complete, we
3302 * need to clear the _fd_array for the queuing check.
3304 lttng_dynamic_pointer_array_clear(
3305 &client
->communication
.outbound
.payload
3310 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3311 client
->communication
.outbound
.queued_command_reply
= false;
3312 client
->communication
.outbound
.dropped_notification
= false;
3313 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3318 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3322 bool client_has_outbound_data_left(
3323 const struct notification_client
*client
)
3325 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3326 &client
->communication
.outbound
.payload
, 0, -1);
3327 const bool has_data
= pv
.buffer
.size
!= 0;
3328 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3330 return has_data
|| has_fds
;
3333 /* Client lock must _not_ be held by the caller. */
3335 int client_send_command_reply(struct notification_client
*client
,
3336 struct notification_thread_state
*state
,
3337 enum lttng_notification_channel_status status
)
3340 struct lttng_notification_channel_command_reply reply
= {
3341 .status
= (int8_t) status
,
3343 struct lttng_notification_channel_message msg
= {
3344 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3345 .size
= sizeof(reply
),
3347 char buffer
[sizeof(msg
) + sizeof(reply
)];
3348 enum client_transmission_status transmission_status
;
3350 memcpy(buffer
, &msg
, sizeof(msg
));
3351 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3352 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3354 pthread_mutex_lock(&client
->lock
);
3355 if (client
->communication
.outbound
.queued_command_reply
) {
3356 /* Protocol error. */
3360 /* Enqueue buffer to outgoing queue and flush it. */
3361 ret
= lttng_dynamic_buffer_append(
3362 &client
->communication
.outbound
.payload
.buffer
,
3363 buffer
, sizeof(buffer
));
3368 transmission_status
= client_flush_outgoing_queue(client
);
3370 if (client_has_outbound_data_left(client
)) {
3371 /* Queue could not be emptied. */
3372 client
->communication
.outbound
.queued_command_reply
= true;
3375 pthread_mutex_unlock(&client
->lock
);
3376 ret
= client_handle_transmission_status(
3377 client
, transmission_status
, state
);
3384 pthread_mutex_unlock(&client
->lock
);
3390 int client_handle_message_unknown(struct notification_client
*client
,
3391 struct notification_thread_state
*state
)
3395 * Receiving message header. The function will be called again
3396 * once the rest of the message as been received and can be
3399 const struct lttng_notification_channel_message
*msg
;
3401 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3402 msg
= (const struct lttng_notification_channel_message
*)
3403 client
->communication
.inbound
.payload
.buffer
.data
;
3405 if (msg
->size
== 0 ||
3406 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3407 ERR("[notification-thread] Invalid notification channel message: length = %u",
3413 switch (msg
->type
) {
3414 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3415 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3416 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3420 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3424 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3425 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3426 client
->communication
.inbound
.msg_type
=
3427 (enum lttng_notification_channel_message_type
) msg
->type
;
3428 ret
= lttng_dynamic_buffer_set_size(
3429 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3431 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3438 int client_handle_message_handshake(struct notification_client
*client
,
3439 struct notification_thread_state
*state
)
3442 struct lttng_notification_channel_command_handshake
*handshake_client
;
3443 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3444 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3445 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3447 const struct lttng_notification_channel_message msg_header
= {
3448 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3449 .size
= sizeof(handshake_reply
),
3451 enum lttng_notification_channel_status status
=
3452 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3453 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3455 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3456 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3457 sizeof(handshake_reply
));
3460 (struct lttng_notification_channel_command_handshake
*)
3461 client
->communication
.inbound
.payload
.buffer
3463 client
->major
= handshake_client
->major
;
3464 client
->minor
= handshake_client
->minor
;
3465 if (!client
->communication
.inbound
.creds_received
) {
3466 ERR("[notification-thread] No credentials received from client");
3471 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3472 &client
->communication
.inbound
.creds
);
3473 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3474 &client
->communication
.inbound
.creds
);
3475 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3476 client
->uid
, client
->gid
, (int) client
->major
,
3477 (int) client
->minor
);
3479 if (handshake_client
->major
!=
3480 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3481 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3484 pthread_mutex_lock(&client
->lock
);
3485 /* Outgoing queue will be flushed when the command reply is sent. */
3486 ret
= lttng_dynamic_buffer_append(
3487 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3488 sizeof(send_buffer
));
3490 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3494 client
->validated
= true;
3495 client
->communication
.active
= true;
3496 pthread_mutex_unlock(&client
->lock
);
3498 /* Set reception state to receive the next message header. */
3499 ret
= client_reset_inbound_state(client
);
3501 ERR("[notification-thread] Failed to reset client communication's inbound state");
3505 /* Flushes the outgoing queue. */
3506 ret
= client_send_command_reply(client
, state
, status
);
3508 ERR("[notification-thread] Failed to send reply to notification channel client");
3514 pthread_mutex_unlock(&client
->lock
);
3520 int client_handle_message_subscription(
3521 struct notification_client
*client
,
3522 enum lttng_notification_channel_message_type msg_type
,
3523 struct notification_thread_state
*state
)
3526 struct lttng_condition
*condition
;
3527 enum lttng_notification_channel_status status
=
3528 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3529 struct lttng_payload_view condition_view
=
3530 lttng_payload_view_from_payload(
3531 &client
->communication
.inbound
.payload
,
3533 size_t expected_condition_size
;
3536 * No need to lock client to sample the inbound state as the only
3537 * other thread accessing clients (action executor) only uses the
3540 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3541 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3542 if (ret
!= expected_condition_size
) {
3543 ERR("[notification-thread] Malformed condition received from client");
3547 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3548 ret
= notification_thread_client_subscribe(
3549 client
, condition
, state
, &status
);
3551 ret
= notification_thread_client_unsubscribe(
3552 client
, condition
, state
, &status
);
3559 /* Set reception state to receive the next message header. */
3560 ret
= client_reset_inbound_state(client
);
3562 ERR("[notification-thread] Failed to reset client communication's inbound state");
3566 ret
= client_send_command_reply(client
, state
, status
);
3568 ERR("[notification-thread] Failed to send reply to notification channel client");
3577 int client_dispatch_message(struct notification_client
*client
,
3578 struct notification_thread_state
*state
)
3582 if (client
->communication
.inbound
.msg_type
!=
3583 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3584 client
->communication
.inbound
.msg_type
!=
3585 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3586 !client
->validated
) {
3587 WARN("[notification-thread] client attempted a command before handshake");
3592 switch (client
->communication
.inbound
.msg_type
) {
3593 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3595 ret
= client_handle_message_unknown(client
, state
);
3598 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3600 ret
= client_handle_message_handshake(client
, state
);
3603 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3604 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3606 ret
= client_handle_message_subscription(client
,
3607 client
->communication
.inbound
.msg_type
, state
);
3617 /* Incoming data from client. */
3618 int handle_notification_thread_client_in(
3619 struct notification_thread_state
*state
, int socket
)
3622 struct notification_client
*client
;
3627 client
= get_client_from_socket(socket
, state
);
3629 /* Internal error, abort. */
3634 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3635 client
->communication
.inbound
.bytes_to_receive
;
3636 if (client
->communication
.inbound
.expect_creds
) {
3637 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3638 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3639 client
->communication
.inbound
.bytes_to_receive
,
3640 &client
->communication
.inbound
.creds
);
3642 client
->communication
.inbound
.expect_creds
= false;
3643 client
->communication
.inbound
.creds_received
= true;
3646 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3647 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3648 client
->communication
.inbound
.bytes_to_receive
);
3650 if (recv_ret
>= 0) {
3651 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3653 goto error_disconnect_client
;
3656 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3657 /* Message incomplete wait for more data. */
3662 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3665 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3666 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3668 client
->communication
.inbound
.fds_to_receive
,
3669 &client
->communication
.inbound
.payload
);
3672 * Fds received. non blocking fds passing is all
3675 ssize_t expected_size
;
3677 expected_size
= sizeof(int) *
3678 client
->communication
.inbound
3680 assert(ret
== expected_size
);
3681 client
->communication
.inbound
.fds_to_receive
= 0;
3682 } else if (ret
== 0) {
3683 /* Received nothing. */
3687 goto error_disconnect_client
;
3691 /* At this point the message is complete.*/
3692 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3693 client
->communication
.inbound
.fds_to_receive
== 0);
3694 ret
= client_dispatch_message(client
, state
);
3697 * Only returns an error if this client must be
3700 goto error_disconnect_client
;
3707 error_disconnect_client
:
3708 ret
= notification_thread_client_disconnect(client
, state
);
3712 /* Client ready to receive outgoing data. */
3713 int handle_notification_thread_client_out(
3714 struct notification_thread_state
*state
, int socket
)
3717 struct notification_client
*client
;
3718 enum client_transmission_status transmission_status
;
3721 client
= get_client_from_socket(socket
, state
);
3723 /* Internal error, abort. */
3728 pthread_mutex_lock(&client
->lock
);
3729 transmission_status
= client_flush_outgoing_queue(client
);
3730 pthread_mutex_unlock(&client
->lock
);
3732 ret
= client_handle_transmission_status(
3733 client
, transmission_status
, state
);
3743 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3744 const struct channel_state_sample
*sample
,
3745 uint64_t buffer_capacity
)
3747 bool result
= false;
3749 enum lttng_condition_type condition_type
;
3750 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3751 condition
, struct lttng_condition_buffer_usage
,
3754 if (use_condition
->threshold_bytes
.set
) {
3755 threshold
= use_condition
->threshold_bytes
.value
;
3758 * Threshold was expressed as a ratio.
3760 * TODO the threshold (in bytes) of conditions expressed
3761 * as a ratio of total buffer size could be cached to
3762 * forego this double-multiplication or it could be performed
3763 * as fixed-point math.
3765 * Note that caching should accommodates the case where the
3766 * condition applies to multiple channels (i.e. don't assume
3767 * that all channels matching my_chann* have the same size...)
3769 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3770 (double) buffer_capacity
);
3773 condition_type
= lttng_condition_get_type(condition
);
3774 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3775 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3776 threshold
, sample
->highest_usage
);
3779 * The low condition should only be triggered once _all_ of the
3780 * streams in a channel have gone below the "low" threshold.
3782 if (sample
->highest_usage
<= threshold
) {
3786 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3787 threshold
, sample
->highest_usage
);
3790 * For high buffer usage scenarios, we want to trigger whenever
3791 * _any_ of the streams has reached the "high" threshold.
3793 if (sample
->highest_usage
>= threshold
) {
3802 bool evaluate_session_consumed_size_condition(
3803 const struct lttng_condition
*condition
,
3804 uint64_t session_consumed_size
)
3807 const struct lttng_condition_session_consumed_size
*size_condition
=
3808 container_of(condition
,
3809 struct lttng_condition_session_consumed_size
,
3812 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3813 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3814 threshold
, session_consumed_size
);
3815 return session_consumed_size
>= threshold
;
3819 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3820 struct lttng_evaluation
**evaluation
,
3821 const struct notification_thread_state
*state
,
3822 const struct channel_state_sample
*previous_sample
,
3823 const struct channel_state_sample
*latest_sample
,
3824 uint64_t previous_session_consumed_total
,
3825 uint64_t latest_session_consumed_total
,
3826 struct channel_info
*channel_info
)
3829 enum lttng_condition_type condition_type
;
3830 const bool previous_sample_available
= !!previous_sample
;
3831 bool previous_sample_result
= false;
3832 bool latest_sample_result
;
3834 condition_type
= lttng_condition_get_type(condition
);
3836 switch (condition_type
) {
3837 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3838 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3839 if (caa_likely(previous_sample_available
)) {
3840 previous_sample_result
=
3841 evaluate_buffer_usage_condition(condition
,
3842 previous_sample
, channel_info
->capacity
);
3844 latest_sample_result
= evaluate_buffer_usage_condition(
3845 condition
, latest_sample
,
3846 channel_info
->capacity
);
3848 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3849 if (caa_likely(previous_sample_available
)) {
3850 previous_sample_result
=
3851 evaluate_session_consumed_size_condition(
3853 previous_session_consumed_total
);
3855 latest_sample_result
=
3856 evaluate_session_consumed_size_condition(
3858 latest_session_consumed_total
);
3861 /* Unknown condition type; internal error. */
3865 if (!latest_sample_result
||
3866 (previous_sample_result
== latest_sample_result
)) {
3868 * Only trigger on a condition evaluation transition.
3870 * NOTE: This edge-triggered logic may not be appropriate for
3871 * future condition types.
3876 if (!evaluation
|| !latest_sample_result
) {
3880 switch (condition_type
) {
3881 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3882 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3883 *evaluation
= lttng_evaluation_buffer_usage_create(
3885 latest_sample
->highest_usage
,
3886 channel_info
->capacity
);
3888 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3889 *evaluation
= lttng_evaluation_session_consumed_size_create(
3890 latest_session_consumed_total
);
3905 int client_notification_overflow(struct notification_client
*client
)
3908 const struct lttng_notification_channel_message msg
= {
3909 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3912 ASSERT_LOCKED(client
->lock
);
3914 DBG("Dropping notification addressed to client (socket fd = %i)",
3916 if (client
->communication
.outbound
.dropped_notification
) {
3918 * The client already has a "notification dropped" message
3919 * in its outgoing queue. Nothing to do since all
3920 * of those messages are coalesced.
3925 client
->communication
.outbound
.dropped_notification
= true;
3926 ret
= lttng_dynamic_buffer_append(
3927 &client
->communication
.outbound
.payload
.buffer
, &msg
,
3930 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3937 static int client_handle_transmission_status_wrapper(
3938 struct notification_client
*client
,
3939 enum client_transmission_status status
,
3942 return client_handle_transmission_status(client
, status
,
3943 (struct notification_thread_state
*) user_data
);
3947 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3948 const struct lttng_evaluation
*evaluation
,
3949 struct notification_client_list
* client_list
,
3950 struct notification_thread_state
*state
,
3951 uid_t object_uid
, gid_t object_gid
)
3953 const struct lttng_credentials creds
= {
3954 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
3955 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
3958 return notification_client_list_send_evaluation(client_list
,
3959 lttng_trigger_get_const_condition(trigger
), evaluation
,
3960 lttng_trigger_get_credentials(trigger
),
3962 client_handle_transmission_status_wrapper
, state
);
3966 * Permission checks relative to notification channel clients are performed
3967 * here. Notice how object, client, and trigger credentials are involved in
3970 * The `object` credentials are the credentials associated with the "subject"
3971 * of a condition. For instance, a `rotation completed` condition applies
3972 * to a session. When that condition is met, it will produce an evaluation
3973 * against a session. Hence, in this case, the `object` credentials are the
3974 * credentials of the "subject" session.
3976 * The `trigger` credentials are the credentials of the user that registered the
3979 * The `client` credentials are the credentials of the user that created a given
3980 * notification channel.
3982 * In terms of visibility, it is expected that non-privilieged users can only
3983 * register triggers against "their" objects (their own sessions and
3984 * applications they are allowed to interact with). They can then open a
3985 * notification channel and subscribe to notifications associated with those
3988 * As for privilieged users, they can register triggers against the objects of
3989 * other users. They can then subscribe to the notifications associated to their
3990 * triggers. Privilieged users _can't_ subscribe to the notifications of
3991 * triggers owned by other users; they must create their own triggers.
3993 * This is more a concern of usability than security. It would be difficult for
3994 * a root user reliably subscribe to a specific set of conditions without
3995 * interference from external users (those could, for instance, unregister
3999 int notification_client_list_send_evaluation(
4000 struct notification_client_list
*client_list
,
4001 const struct lttng_condition
*condition
,
4002 const struct lttng_evaluation
*evaluation
,
4003 const struct lttng_credentials
*trigger_creds
,
4004 const struct lttng_credentials
*source_object_creds
,
4005 report_client_transmission_result_cb client_report
,
4009 struct lttng_payload msg_payload
;
4010 struct notification_client_list_element
*client_list_element
, *tmp
;
4011 const struct lttng_notification notification
= {
4012 .condition
= (struct lttng_condition
*) condition
,
4013 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4015 struct lttng_notification_channel_message msg_header
= {
4016 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4019 lttng_payload_init(&msg_payload
);
4021 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4022 sizeof(msg_header
));
4027 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4029 ERR("[notification-thread] Failed to serialize notification");
4034 /* Update payload size. */
4035 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4036 ->size
= (uint32_t)(
4037 msg_payload
.buffer
.size
- sizeof(msg_header
));
4039 /* Update the payload number of fds. */
4041 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4042 &msg_payload
, 0, -1);
4044 ((struct lttng_notification_channel_message
*)
4045 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4046 lttng_payload_view_get_fd_handle_count(&pv
);
4049 pthread_mutex_lock(&client_list
->lock
);
4050 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4051 &client_list
->list
, node
) {
4052 enum client_transmission_status transmission_status
;
4053 struct notification_client
*client
=
4054 client_list_element
->client
;
4057 pthread_mutex_lock(&client
->lock
);
4058 if (!client
->communication
.active
) {
4060 * Skip inactive client (protocol error or
4063 DBG("Skipping client at it is marked as inactive");
4067 if (source_object_creds
) {
4068 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4069 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4072 * Client is not allowed to monitor this
4075 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4080 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
4081 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4085 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4086 client
->socket
, msg_payload
.buffer
.size
);
4088 if (client_has_outbound_data_left(client
)) {
4090 * Outgoing data is already buffered for this client;
4091 * drop the notification and enqueue a "dropped
4092 * notification" message if this is the first dropped
4093 * notification since the socket spilled-over to the
4096 ret
= client_notification_overflow(client
);
4103 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4109 transmission_status
= client_flush_outgoing_queue(client
);
4110 pthread_mutex_unlock(&client
->lock
);
4111 ret
= client_report(client
, transmission_status
, user_data
);
4114 goto end_unlock_list
;
4120 pthread_mutex_unlock(&client
->lock
);
4123 goto end_unlock_list
;
4129 pthread_mutex_unlock(&client_list
->lock
);
4131 lttng_payload_reset(&msg_payload
);
4135 int handle_notification_thread_channel_sample(
4136 struct notification_thread_state
*state
, int pipe
,
4137 enum lttng_domain_type domain
)
4140 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4141 struct channel_info
*channel_info
;
4142 struct cds_lfht_node
*node
;
4143 struct cds_lfht_iter iter
;
4144 struct lttng_channel_trigger_list
*trigger_list
;
4145 struct lttng_trigger_list_element
*trigger_list_element
;
4146 bool previous_sample_available
= false;
4147 struct channel_state_sample previous_sample
, latest_sample
;
4148 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4149 struct lttng_credentials channel_creds
;
4152 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4153 * ensuring that read/write of sampling messages are atomic.
4155 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4156 if (ret
!= sizeof(sample_msg
)) {
4157 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4164 latest_sample
.key
.key
= sample_msg
.key
;
4165 latest_sample
.key
.domain
= domain
;
4166 latest_sample
.highest_usage
= sample_msg
.highest
;
4167 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4168 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4172 /* Retrieve the channel's informations */
4173 cds_lfht_lookup(state
->channels_ht
,
4174 hash_channel_key(&latest_sample
.key
),
4178 node
= cds_lfht_iter_get_node(&iter
);
4179 if (caa_unlikely(!node
)) {
4181 * Not an error since the consumer can push a sample to the pipe
4182 * and the rest of the session daemon could notify us of the
4183 * channel's destruction before we get a chance to process that
4186 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4187 latest_sample
.key
.key
,
4188 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
4192 channel_info
= caa_container_of(node
, struct channel_info
,
4194 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4196 latest_sample
.key
.key
,
4197 channel_info
->session_info
->name
,
4198 latest_sample
.highest_usage
,
4199 latest_sample
.lowest_usage
,
4200 latest_sample
.channel_total_consumed
);
4202 previous_session_consumed_total
=
4203 channel_info
->session_info
->consumed_data_size
;
4205 /* Retrieve the channel's last sample, if it exists, and update it. */
4206 cds_lfht_lookup(state
->channel_state_ht
,
4207 hash_channel_key(&latest_sample
.key
),
4208 match_channel_state_sample
,
4211 node
= cds_lfht_iter_get_node(&iter
);
4212 if (caa_likely(node
)) {
4213 struct channel_state_sample
*stored_sample
;
4215 /* Update the sample stored. */
4216 stored_sample
= caa_container_of(node
,
4217 struct channel_state_sample
,
4218 channel_state_ht_node
);
4220 memcpy(&previous_sample
, stored_sample
,
4221 sizeof(previous_sample
));
4222 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4223 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4224 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4225 previous_sample_available
= true;
4227 latest_session_consumed_total
=
4228 previous_session_consumed_total
+
4229 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4232 * This is the channel's first sample, allocate space for and
4233 * store the new sample.
4235 struct channel_state_sample
*stored_sample
;
4237 stored_sample
= zmalloc(sizeof(*stored_sample
));
4238 if (!stored_sample
) {
4243 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4244 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4245 cds_lfht_add(state
->channel_state_ht
,
4246 hash_channel_key(&stored_sample
->key
),
4247 &stored_sample
->channel_state_ht_node
);
4249 latest_session_consumed_total
=
4250 previous_session_consumed_total
+
4251 latest_sample
.channel_total_consumed
;
4254 channel_info
->session_info
->consumed_data_size
=
4255 latest_session_consumed_total
;
4257 /* Find triggers associated with this channel. */
4258 cds_lfht_lookup(state
->channel_triggers_ht
,
4259 hash_channel_key(&latest_sample
.key
),
4260 match_channel_trigger_list
,
4263 node
= cds_lfht_iter_get_node(&iter
);
4264 if (caa_likely(!node
)) {
4268 channel_creds
= (typeof(channel_creds
)) {
4269 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4270 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4273 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4274 channel_triggers_ht_node
);
4275 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4277 const struct lttng_condition
*condition
;
4278 struct lttng_trigger
*trigger
;
4279 struct notification_client_list
*client_list
= NULL
;
4280 struct lttng_evaluation
*evaluation
= NULL
;
4281 enum action_executor_status executor_status
;
4284 trigger
= trigger_list_element
->trigger
;
4285 condition
= lttng_trigger_get_const_condition(trigger
);
4289 * Check if any client is subscribed to the result of this
4292 client_list
= get_client_list_from_condition(state
, condition
);
4294 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4295 previous_sample_available
? &previous_sample
: NULL
,
4297 previous_session_consumed_total
,
4298 latest_session_consumed_total
,
4300 if (caa_unlikely(ret
)) {
4304 if (caa_likely(!evaluation
)) {
4308 if (!lttng_trigger_should_fire(trigger
)) {
4312 lttng_trigger_fire(trigger
);
4315 * Ownership of `evaluation` transferred to the action executor
4316 * no matter the result.
4318 executor_status
= action_executor_enqueue(state
->executor
,
4319 trigger
, evaluation
, &channel_creds
,
4322 switch (executor_status
) {
4323 case ACTION_EXECUTOR_STATUS_OK
:
4325 case ACTION_EXECUTOR_STATUS_ERROR
:
4326 case ACTION_EXECUTOR_STATUS_INVALID
:
4328 * TODO Add trigger identification (name/id) when
4329 * it is added to the API.
4331 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4334 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4336 * TODO Add trigger identification (name/id) when
4337 * it is added to the API.
4339 * Not a fatal error.
4341 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4349 notification_client_list_put(client_list
);
4350 if (caa_unlikely(ret
)) {