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
;
129 * Facilities to carry the different notifications type in the action processing
132 struct lttng_event_notifier_notification
{
134 struct lttng_ust_event_notifier_notification
*ust
;
135 struct lttng_kernel_event_notifier_notification
*kernel
;
138 enum lttng_domain_type type
;
141 struct channel_state_sample
{
142 struct channel_key key
;
143 struct cds_lfht_node channel_state_ht_node
;
144 uint64_t highest_usage
;
145 uint64_t lowest_usage
;
146 uint64_t channel_total_consumed
;
147 /* call_rcu delayed reclaim. */
148 struct rcu_head rcu_node
;
151 static unsigned long hash_channel_key(struct channel_key
*key
);
152 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
153 struct lttng_evaluation
**evaluation
,
154 const struct notification_thread_state
*state
,
155 const struct channel_state_sample
*previous_sample
,
156 const struct channel_state_sample
*latest_sample
,
157 uint64_t previous_session_consumed_total
,
158 uint64_t latest_session_consumed_total
,
159 struct channel_info
*channel_info
);
161 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
162 const struct lttng_evaluation
*evaluation
,
163 struct notification_client_list
*client_list
,
164 struct notification_thread_state
*state
,
165 uid_t channel_uid
, gid_t channel_gid
);
168 /* session_info API */
170 void session_info_destroy(void *_data
);
172 void session_info_get(struct session_info
*session_info
);
174 void session_info_put(struct session_info
*session_info
);
176 struct session_info
*session_info_create(const char *name
,
177 uid_t uid
, gid_t gid
,
178 struct lttng_session_trigger_list
*trigger_list
,
179 struct cds_lfht
*sessions_ht
);
181 void session_info_add_channel(struct session_info
*session_info
,
182 struct channel_info
*channel_info
);
184 void session_info_remove_channel(struct session_info
*session_info
,
185 struct channel_info
*channel_info
);
187 /* lttng_session_trigger_list API */
189 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
190 const char *session_name
,
191 struct cds_lfht
*session_triggers_ht
);
193 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
194 const struct notification_thread_state
*state
,
195 const char *session_name
);
197 void lttng_session_trigger_list_destroy(
198 struct lttng_session_trigger_list
*list
);
200 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
201 struct lttng_trigger
*trigger
);
204 int client_handle_transmission_status(
205 struct notification_client
*client
,
206 enum client_transmission_status transmission_status
,
207 struct notification_thread_state
*state
);
210 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
213 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
215 /* This double-cast is intended to supress pointer-to-cast warning. */
216 const int socket
= (int) (intptr_t) key
;
217 const struct notification_client
*client
= caa_container_of(node
,
218 struct notification_client
, client_socket_ht_node
);
220 return client
->socket
== socket
;
224 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
226 /* This double-cast is intended to supress pointer-to-cast warning. */
227 const notification_client_id id
= *((notification_client_id
*) key
);
228 const struct notification_client
*client
= caa_container_of(
229 node
, struct notification_client
, client_id_ht_node
);
231 return client
->id
== id
;
235 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
237 struct channel_key
*channel_key
= (struct channel_key
*) key
;
238 struct lttng_channel_trigger_list
*trigger_list
;
240 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
241 channel_triggers_ht_node
);
243 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
244 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
248 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
250 const char *session_name
= (const char *) key
;
251 struct lttng_session_trigger_list
*trigger_list
;
253 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
254 session_triggers_ht_node
);
256 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
260 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
262 struct channel_key
*channel_key
= (struct channel_key
*) key
;
263 struct channel_state_sample
*sample
;
265 sample
= caa_container_of(node
, struct channel_state_sample
,
266 channel_state_ht_node
);
268 return !!((channel_key
->key
== sample
->key
.key
) &&
269 (channel_key
->domain
== sample
->key
.domain
));
273 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
275 struct channel_key
*channel_key
= (struct channel_key
*) key
;
276 struct channel_info
*channel_info
;
278 channel_info
= caa_container_of(node
, struct channel_info
,
281 return !!((channel_key
->key
== channel_info
->key
.key
) &&
282 (channel_key
->domain
== channel_info
->key
.domain
));
286 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
288 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
289 struct lttng_trigger_ht_element
*trigger_ht_element
;
291 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
294 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
298 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
300 const uint64_t *_key
= key
;
301 struct notification_trigger_tokens_ht_element
*element
;
303 element
= caa_container_of(node
,
304 struct notification_trigger_tokens_ht_element
, node
);
305 return *_key
== element
->token
;
309 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
311 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
312 struct notification_client_list
*client_list
;
313 const struct lttng_condition
*condition
;
315 assert(condition_key
);
317 client_list
= caa_container_of(node
, struct notification_client_list
,
318 notification_trigger_clients_ht_node
);
319 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
321 return !!lttng_condition_is_equal(condition_key
, condition
);
325 int match_session(struct cds_lfht_node
*node
, const void *key
)
327 const char *name
= key
;
328 struct session_info
*session_info
= caa_container_of(
329 node
, struct session_info
, sessions_ht_node
);
331 return !strcmp(session_info
->name
, name
);
335 * Match trigger based on name and credentials only.
336 * Name duplication is NOT allowed for the same uid.
339 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
344 const char *key_name
;
345 enum lttng_trigger_status status
;
346 const struct lttng_credentials
*key_creds
;
347 const struct lttng_credentials
*node_creds
;
348 const struct lttng_trigger
*trigger_key
=
349 (const struct lttng_trigger
*) key
;
350 const struct lttng_trigger_ht_element
*trigger_ht_element
=
351 caa_container_of(node
,
352 struct lttng_trigger_ht_element
,
355 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
356 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
358 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
359 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
361 /* Compare the names. */
362 if (strcmp(name
, key_name
) != 0) {
366 /* Compare the owners' UIDs. */
367 key_creds
= lttng_trigger_get_credentials(trigger_key
);
368 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
370 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
377 * Hash trigger based on name and credentials only.
380 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
382 unsigned long hash
= 0;
383 const struct lttng_credentials
*trigger_creds
;
384 const char *trigger_name
;
385 enum lttng_trigger_status status
;
387 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
388 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
389 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
392 trigger_creds
= lttng_trigger_get_credentials(trigger
);
393 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
400 unsigned long hash_channel_key(struct channel_key
*key
)
402 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
403 unsigned long domain_hash
= hash_key_ulong(
404 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
406 return key_hash
^ domain_hash
;
410 unsigned long hash_client_socket(int socket
)
412 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
416 unsigned long hash_client_id(notification_client_id id
)
418 return hash_key_u64(&id
, lttng_ht_seed
);
422 * Get the type of object to which a given condition applies. Bindings let
423 * the notification system evaluate a trigger's condition when a given
424 * object's state is updated.
426 * For instance, a condition bound to a channel will be evaluated everytime
427 * the channel's state is changed by a channel monitoring sample.
430 enum lttng_object_type
get_condition_binding_object(
431 const struct lttng_condition
*condition
)
433 switch (lttng_condition_get_type(condition
)) {
434 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
435 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
436 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
437 return LTTNG_OBJECT_TYPE_CHANNEL
;
438 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
439 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
440 return LTTNG_OBJECT_TYPE_SESSION
;
441 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
442 return LTTNG_OBJECT_TYPE_NONE
;
444 return LTTNG_OBJECT_TYPE_UNKNOWN
;
449 void free_channel_info_rcu(struct rcu_head
*node
)
451 free(caa_container_of(node
, struct channel_info
, rcu_node
));
455 void channel_info_destroy(struct channel_info
*channel_info
)
461 if (channel_info
->session_info
) {
462 session_info_remove_channel(channel_info
->session_info
,
464 session_info_put(channel_info
->session_info
);
466 if (channel_info
->name
) {
467 free(channel_info
->name
);
469 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
473 void free_session_info_rcu(struct rcu_head
*node
)
475 free(caa_container_of(node
, struct session_info
, rcu_node
));
478 /* Don't call directly, use the ref-counting mechanism. */
480 void session_info_destroy(void *_data
)
482 struct session_info
*session_info
= _data
;
485 assert(session_info
);
486 if (session_info
->channel_infos_ht
) {
487 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
489 ERR("[notification-thread] Failed to destroy channel information hash table");
492 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
495 cds_lfht_del(session_info
->sessions_ht
,
496 &session_info
->sessions_ht_node
);
498 free(session_info
->name
);
499 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
503 void session_info_get(struct session_info
*session_info
)
508 lttng_ref_get(&session_info
->ref
);
512 void session_info_put(struct session_info
*session_info
)
517 lttng_ref_put(&session_info
->ref
);
521 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
522 struct lttng_session_trigger_list
*trigger_list
,
523 struct cds_lfht
*sessions_ht
)
525 struct session_info
*session_info
;
529 session_info
= zmalloc(sizeof(*session_info
));
533 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
535 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
536 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
537 if (!session_info
->channel_infos_ht
) {
541 cds_lfht_node_init(&session_info
->sessions_ht_node
);
542 session_info
->name
= strdup(name
);
543 if (!session_info
->name
) {
546 session_info
->uid
= uid
;
547 session_info
->gid
= gid
;
548 session_info
->trigger_list
= trigger_list
;
549 session_info
->sessions_ht
= sessions_ht
;
553 session_info_put(session_info
);
558 void session_info_add_channel(struct session_info
*session_info
,
559 struct channel_info
*channel_info
)
562 cds_lfht_add(session_info
->channel_infos_ht
,
563 hash_channel_key(&channel_info
->key
),
564 &channel_info
->session_info_channels_ht_node
);
569 void session_info_remove_channel(struct session_info
*session_info
,
570 struct channel_info
*channel_info
)
573 cds_lfht_del(session_info
->channel_infos_ht
,
574 &channel_info
->session_info_channels_ht_node
);
579 struct channel_info
*channel_info_create(const char *channel_name
,
580 struct channel_key
*channel_key
, uint64_t channel_capacity
,
581 struct session_info
*session_info
)
583 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
589 cds_lfht_node_init(&channel_info
->channels_ht_node
);
590 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
591 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
592 channel_info
->capacity
= channel_capacity
;
594 channel_info
->name
= strdup(channel_name
);
595 if (!channel_info
->name
) {
600 * Set the references between session and channel infos:
601 * - channel_info holds a strong reference to session_info
602 * - session_info holds a weak reference to channel_info
604 session_info_get(session_info
);
605 session_info_add_channel(session_info
, channel_info
);
606 channel_info
->session_info
= session_info
;
610 channel_info_destroy(channel_info
);
615 bool notification_client_list_get(struct notification_client_list
*list
)
617 return urcu_ref_get_unless_zero(&list
->ref
);
621 void free_notification_client_list_rcu(struct rcu_head
*node
)
623 free(caa_container_of(node
, struct notification_client_list
,
628 void notification_client_list_release(struct urcu_ref
*list_ref
)
630 struct notification_client_list
*list
=
631 container_of(list_ref
, typeof(*list
), ref
);
632 struct notification_client_list_element
*client_list_element
, *tmp
;
634 if (list
->notification_trigger_clients_ht
) {
636 cds_lfht_del(list
->notification_trigger_clients_ht
,
637 &list
->notification_trigger_clients_ht_node
);
639 list
->notification_trigger_clients_ht
= NULL
;
641 cds_list_for_each_entry_safe(client_list_element
, tmp
,
643 free(client_list_element
);
645 pthread_mutex_destroy(&list
->lock
);
646 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
650 struct notification_client_list
*notification_client_list_create(
651 const struct lttng_trigger
*trigger
)
653 struct notification_client_list
*client_list
=
654 zmalloc(sizeof(*client_list
));
659 pthread_mutex_init(&client_list
->lock
, NULL
);
660 urcu_ref_init(&client_list
->ref
);
661 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
662 CDS_INIT_LIST_HEAD(&client_list
->list
);
663 client_list
->trigger
= trigger
;
669 void publish_notification_client_list(
670 struct notification_thread_state
*state
,
671 struct notification_client_list
*list
)
673 const struct lttng_condition
*condition
=
674 lttng_trigger_get_const_condition(list
->trigger
);
676 assert(!list
->notification_trigger_clients_ht
);
677 notification_client_list_get(list
);
679 list
->notification_trigger_clients_ht
=
680 state
->notification_trigger_clients_ht
;
683 cds_lfht_add(state
->notification_trigger_clients_ht
,
684 lttng_condition_hash(condition
),
685 &list
->notification_trigger_clients_ht_node
);
690 void notification_client_list_put(struct notification_client_list
*list
)
695 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
698 /* Provides a reference to the returned list. */
700 struct notification_client_list
*get_client_list_from_condition(
701 struct notification_thread_state
*state
,
702 const struct lttng_condition
*condition
)
704 struct cds_lfht_node
*node
;
705 struct cds_lfht_iter iter
;
706 struct notification_client_list
*list
= NULL
;
709 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
710 lttng_condition_hash(condition
),
711 match_client_list_condition
,
714 node
= cds_lfht_iter_get_node(&iter
);
716 list
= container_of(node
, struct notification_client_list
,
717 notification_trigger_clients_ht_node
);
718 list
= notification_client_list_get(list
) ? list
: NULL
;
726 int evaluate_channel_condition_for_client(
727 const struct lttng_condition
*condition
,
728 struct notification_thread_state
*state
,
729 struct lttng_evaluation
**evaluation
,
730 uid_t
*session_uid
, gid_t
*session_gid
)
733 struct cds_lfht_iter iter
;
734 struct cds_lfht_node
*node
;
735 struct channel_info
*channel_info
= NULL
;
736 struct channel_key
*channel_key
= NULL
;
737 struct channel_state_sample
*last_sample
= NULL
;
738 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
742 /* Find the channel associated with the condition. */
743 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
744 channel_trigger_list
, channel_triggers_ht_node
) {
745 struct lttng_trigger_list_element
*element
;
747 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
748 const struct lttng_condition
*current_condition
=
749 lttng_trigger_get_const_condition(
752 assert(current_condition
);
753 if (!lttng_condition_is_equal(condition
,
754 current_condition
)) {
758 /* Found the trigger, save the channel key. */
759 channel_key
= &channel_trigger_list
->channel_key
;
763 /* The channel key was found stop iteration. */
769 /* No channel found; normal exit. */
770 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
775 /* Fetch channel info for the matching channel. */
776 cds_lfht_lookup(state
->channels_ht
,
777 hash_channel_key(channel_key
),
781 node
= cds_lfht_iter_get_node(&iter
);
783 channel_info
= caa_container_of(node
, struct channel_info
,
786 /* Retrieve the channel's last sample, if it exists. */
787 cds_lfht_lookup(state
->channel_state_ht
,
788 hash_channel_key(channel_key
),
789 match_channel_state_sample
,
792 node
= cds_lfht_iter_get_node(&iter
);
794 last_sample
= caa_container_of(node
,
795 struct channel_state_sample
,
796 channel_state_ht_node
);
798 /* Nothing to evaluate, no sample was ever taken. Normal exit */
799 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
804 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
806 0, channel_info
->session_info
->consumed_data_size
,
809 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
813 *session_uid
= channel_info
->session_info
->uid
;
814 *session_gid
= channel_info
->session_info
->gid
;
821 const char *get_condition_session_name(const struct lttng_condition
*condition
)
823 const char *session_name
= NULL
;
824 enum lttng_condition_status status
;
826 switch (lttng_condition_get_type(condition
)) {
827 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
828 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
829 status
= lttng_condition_buffer_usage_get_session_name(
830 condition
, &session_name
);
832 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
833 status
= lttng_condition_session_consumed_size_get_session_name(
834 condition
, &session_name
);
836 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
837 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
838 status
= lttng_condition_session_rotation_get_session_name(
839 condition
, &session_name
);
844 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
845 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
853 int evaluate_session_condition_for_client(
854 const struct lttng_condition
*condition
,
855 struct notification_thread_state
*state
,
856 struct lttng_evaluation
**evaluation
,
857 uid_t
*session_uid
, gid_t
*session_gid
)
860 struct cds_lfht_iter iter
;
861 struct cds_lfht_node
*node
;
862 const char *session_name
;
863 struct session_info
*session_info
= NULL
;
866 session_name
= get_condition_session_name(condition
);
868 /* Find the session associated with the trigger. */
869 cds_lfht_lookup(state
->sessions_ht
,
870 hash_key_str(session_name
, lttng_ht_seed
),
874 node
= cds_lfht_iter_get_node(&iter
);
876 DBG("[notification-thread] No known session matching name \"%s\"",
882 session_info
= caa_container_of(node
, struct session_info
,
884 session_info_get(session_info
);
887 * Evaluation is performed in-line here since only one type of
888 * session-bound condition is handled for the moment.
890 switch (lttng_condition_get_type(condition
)) {
891 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
892 if (!session_info
->rotation
.ongoing
) {
894 goto end_session_put
;
897 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
898 session_info
->rotation
.id
);
901 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
904 goto end_session_put
;
910 goto end_session_put
;
913 *session_uid
= session_info
->uid
;
914 *session_gid
= session_info
->gid
;
917 session_info_put(session_info
);
924 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
925 const struct lttng_condition
*condition
,
926 struct notification_client
*client
,
927 struct notification_thread_state
*state
)
930 struct lttng_evaluation
*evaluation
= NULL
;
931 struct notification_client_list client_list
= {
932 .lock
= PTHREAD_MUTEX_INITIALIZER
,
934 struct notification_client_list_element client_list_element
= { 0 };
935 uid_t object_uid
= 0;
936 gid_t object_gid
= 0;
943 switch (get_condition_binding_object(condition
)) {
944 case LTTNG_OBJECT_TYPE_SESSION
:
945 ret
= evaluate_session_condition_for_client(condition
, state
,
946 &evaluation
, &object_uid
, &object_gid
);
948 case LTTNG_OBJECT_TYPE_CHANNEL
:
949 ret
= evaluate_channel_condition_for_client(condition
, state
,
950 &evaluation
, &object_uid
, &object_gid
);
952 case LTTNG_OBJECT_TYPE_NONE
:
953 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
956 case LTTNG_OBJECT_TYPE_UNKNOWN
:
966 /* Evaluation yielded nothing. Normal exit. */
967 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
973 * Create a temporary client list with the client currently
976 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
977 CDS_INIT_LIST_HEAD(&client_list
.list
);
978 client_list
.trigger
= trigger
;
980 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
981 client_list_element
.client
= client
;
982 cds_list_add(&client_list_element
.node
, &client_list
.list
);
984 /* Send evaluation result to the newly-subscribed client. */
985 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
986 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
987 state
, object_uid
, object_gid
);
994 int notification_thread_client_subscribe(struct notification_client
*client
,
995 struct lttng_condition
*condition
,
996 struct notification_thread_state
*state
,
997 enum lttng_notification_channel_status
*_status
)
1000 struct notification_client_list
*client_list
= NULL
;
1001 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1002 struct notification_client_list_element
*client_list_element
= NULL
;
1003 enum lttng_notification_channel_status status
=
1004 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1007 * Ensure that the client has not already subscribed to this condition
1010 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1011 if (lttng_condition_is_equal(condition_list_element
->condition
,
1013 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1018 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1019 if (!condition_list_element
) {
1023 client_list_element
= zmalloc(sizeof(*client_list_element
));
1024 if (!client_list_element
) {
1030 * Add the newly-subscribed condition to the client's subscription list.
1032 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1033 condition_list_element
->condition
= condition
;
1034 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1036 client_list
= get_client_list_from_condition(state
, condition
);
1039 * No notification-emiting trigger registered with this
1040 * condition. We don't evaluate the condition right away
1041 * since this trigger is not registered yet.
1043 free(client_list_element
);
1048 * The condition to which the client just subscribed is evaluated
1049 * at this point so that conditions that are already TRUE result
1050 * in a notification being sent out.
1052 * The client_list's trigger is used without locking the list itself.
1053 * This is correct since the list doesn't own the trigger and the
1054 * object is immutable.
1056 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1058 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1060 free(client_list_element
);
1065 * Add the client to the list of clients interested in a given trigger
1066 * if a "notification" trigger with a corresponding condition was
1069 client_list_element
->client
= client
;
1070 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1072 pthread_mutex_lock(&client_list
->lock
);
1073 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1074 pthread_mutex_unlock(&client_list
->lock
);
1080 notification_client_list_put(client_list
);
1084 free(condition_list_element
);
1085 free(client_list_element
);
1090 int notification_thread_client_unsubscribe(
1091 struct notification_client
*client
,
1092 struct lttng_condition
*condition
,
1093 struct notification_thread_state
*state
,
1094 enum lttng_notification_channel_status
*_status
)
1096 struct notification_client_list
*client_list
;
1097 struct lttng_condition_list_element
*condition_list_element
,
1099 struct notification_client_list_element
*client_list_element
,
1101 bool condition_found
= false;
1102 enum lttng_notification_channel_status status
=
1103 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1105 /* Remove the condition from the client's condition list. */
1106 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1107 &client
->condition_list
, node
) {
1108 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1113 cds_list_del(&condition_list_element
->node
);
1115 * The caller may be iterating on the client's conditions to
1116 * tear down a client's connection. In this case, the condition
1117 * will be destroyed at the end.
1119 if (condition
!= condition_list_element
->condition
) {
1120 lttng_condition_destroy(
1121 condition_list_element
->condition
);
1123 free(condition_list_element
);
1124 condition_found
= true;
1128 if (!condition_found
) {
1129 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1134 * Remove the client from the list of clients interested the trigger
1135 * matching the condition.
1137 client_list
= get_client_list_from_condition(state
, condition
);
1142 pthread_mutex_lock(&client_list
->lock
);
1143 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1144 &client_list
->list
, node
) {
1145 if (client_list_element
->client
->id
!= client
->id
) {
1148 cds_list_del(&client_list_element
->node
);
1149 free(client_list_element
);
1152 pthread_mutex_unlock(&client_list
->lock
);
1153 notification_client_list_put(client_list
);
1156 lttng_condition_destroy(condition
);
1164 void free_notification_client_rcu(struct rcu_head
*node
)
1166 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1170 void notification_client_destroy(struct notification_client
*client
,
1171 struct notification_thread_state
*state
)
1178 * The client object is not reachable by other threads, no need to lock
1181 if (client
->socket
>= 0) {
1182 (void) lttcomm_close_unix_sock(client
->socket
);
1183 client
->socket
= -1;
1185 client
->communication
.active
= false;
1186 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1187 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1188 pthread_mutex_destroy(&client
->lock
);
1189 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1193 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1197 struct notification_client
*get_client_from_socket(int socket
,
1198 struct notification_thread_state
*state
)
1200 struct cds_lfht_iter iter
;
1201 struct cds_lfht_node
*node
;
1202 struct notification_client
*client
= NULL
;
1204 cds_lfht_lookup(state
->client_socket_ht
,
1205 hash_client_socket(socket
),
1206 match_client_socket
,
1207 (void *) (unsigned long) socket
,
1209 node
= cds_lfht_iter_get_node(&iter
);
1214 client
= caa_container_of(node
, struct notification_client
,
1215 client_socket_ht_node
);
1221 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1225 struct notification_client
*get_client_from_id(notification_client_id id
,
1226 struct notification_thread_state
*state
)
1228 struct cds_lfht_iter iter
;
1229 struct cds_lfht_node
*node
;
1230 struct notification_client
*client
= NULL
;
1232 cds_lfht_lookup(state
->client_id_ht
,
1237 node
= cds_lfht_iter_get_node(&iter
);
1242 client
= caa_container_of(node
, struct notification_client
,
1249 bool buffer_usage_condition_applies_to_channel(
1250 const struct lttng_condition
*condition
,
1251 const struct channel_info
*channel_info
)
1253 enum lttng_condition_status status
;
1254 enum lttng_domain_type condition_domain
;
1255 const char *condition_session_name
= NULL
;
1256 const char *condition_channel_name
= NULL
;
1258 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1260 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1261 if (channel_info
->key
.domain
!= condition_domain
) {
1265 status
= lttng_condition_buffer_usage_get_session_name(
1266 condition
, &condition_session_name
);
1267 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1269 status
= lttng_condition_buffer_usage_get_channel_name(
1270 condition
, &condition_channel_name
);
1271 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1273 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1276 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1286 bool session_consumed_size_condition_applies_to_channel(
1287 const struct lttng_condition
*condition
,
1288 const struct channel_info
*channel_info
)
1290 enum lttng_condition_status status
;
1291 const char *condition_session_name
= NULL
;
1293 status
= lttng_condition_session_consumed_size_get_session_name(
1294 condition
, &condition_session_name
);
1295 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1297 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1307 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1308 const struct channel_info
*channel_info
)
1310 const struct lttng_condition
*condition
;
1311 bool trigger_applies
;
1313 condition
= lttng_trigger_get_const_condition(trigger
);
1318 switch (lttng_condition_get_type(condition
)) {
1319 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1320 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1321 trigger_applies
= buffer_usage_condition_applies_to_channel(
1322 condition
, channel_info
);
1324 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1325 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1326 condition
, channel_info
);
1332 return trigger_applies
;
1338 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1339 struct notification_client
*client
)
1341 bool applies
= false;
1342 struct lttng_condition_list_element
*condition_list_element
;
1344 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1346 applies
= lttng_condition_is_equal(
1347 condition_list_element
->condition
,
1348 lttng_trigger_get_condition(trigger
));
1356 /* Must be called with RCU read lock held. */
1358 struct lttng_session_trigger_list
*get_session_trigger_list(
1359 struct notification_thread_state
*state
,
1360 const char *session_name
)
1362 struct lttng_session_trigger_list
*list
= NULL
;
1363 struct cds_lfht_node
*node
;
1364 struct cds_lfht_iter iter
;
1366 cds_lfht_lookup(state
->session_triggers_ht
,
1367 hash_key_str(session_name
, lttng_ht_seed
),
1368 match_session_trigger_list
,
1371 node
= cds_lfht_iter_get_node(&iter
);
1374 * Not an error, the list of triggers applying to that session
1375 * will be initialized when the session is created.
1377 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1382 list
= caa_container_of(node
,
1383 struct lttng_session_trigger_list
,
1384 session_triggers_ht_node
);
1390 * Allocate an empty lttng_session_trigger_list for the session named
1393 * No ownership of 'session_name' is assumed by the session trigger list.
1394 * It is the caller's responsability to ensure the session name is alive
1395 * for as long as this list is.
1398 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1399 const char *session_name
,
1400 struct cds_lfht
*session_triggers_ht
)
1402 struct lttng_session_trigger_list
*list
;
1404 list
= zmalloc(sizeof(*list
));
1408 list
->session_name
= session_name
;
1409 CDS_INIT_LIST_HEAD(&list
->list
);
1410 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1411 list
->session_triggers_ht
= session_triggers_ht
;
1414 /* Publish the list through the session_triggers_ht. */
1415 cds_lfht_add(session_triggers_ht
,
1416 hash_key_str(session_name
, lttng_ht_seed
),
1417 &list
->session_triggers_ht_node
);
1424 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1426 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1431 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1433 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1435 /* Empty the list element by element, and then free the list itself. */
1436 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1437 &list
->list
, node
) {
1438 cds_list_del(&trigger_list_element
->node
);
1439 free(trigger_list_element
);
1442 /* Unpublish the list from the session_triggers_ht. */
1443 cds_lfht_del(list
->session_triggers_ht
,
1444 &list
->session_triggers_ht_node
);
1446 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1450 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1451 struct lttng_trigger
*trigger
)
1454 struct lttng_trigger_list_element
*new_element
=
1455 zmalloc(sizeof(*new_element
));
1461 CDS_INIT_LIST_HEAD(&new_element
->node
);
1462 new_element
->trigger
= trigger
;
1463 cds_list_add(&new_element
->node
, &list
->list
);
1469 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1470 const char *session_name
)
1472 bool applies
= false;
1473 const struct lttng_condition
*condition
;
1475 condition
= lttng_trigger_get_const_condition(trigger
);
1476 switch (lttng_condition_get_type(condition
)) {
1477 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1478 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1480 enum lttng_condition_status condition_status
;
1481 const char *condition_session_name
;
1483 condition_status
= lttng_condition_session_rotation_get_session_name(
1484 condition
, &condition_session_name
);
1485 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1486 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1490 assert(condition_session_name
);
1491 applies
= !strcmp(condition_session_name
, session_name
);
1502 * Allocate and initialize an lttng_session_trigger_list which contains
1503 * all triggers that apply to the session named 'session_name'.
1505 * No ownership of 'session_name' is assumed by the session trigger list.
1506 * It is the caller's responsability to ensure the session name is alive
1507 * for as long as this list is.
1510 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1511 const struct notification_thread_state
*state
,
1512 const char *session_name
)
1514 int trigger_count
= 0;
1515 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1516 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1517 struct cds_lfht_iter iter
;
1519 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1520 state
->session_triggers_ht
);
1522 /* Add all triggers applying to the session named 'session_name'. */
1523 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1527 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1532 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1533 trigger_ht_element
->trigger
);
1541 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1543 return session_trigger_list
;
1545 lttng_session_trigger_list_destroy(session_trigger_list
);
1550 struct session_info
*find_or_create_session_info(
1551 struct notification_thread_state
*state
,
1552 const char *name
, uid_t uid
, gid_t gid
)
1554 struct session_info
*session
= NULL
;
1555 struct cds_lfht_node
*node
;
1556 struct cds_lfht_iter iter
;
1557 struct lttng_session_trigger_list
*trigger_list
;
1560 cds_lfht_lookup(state
->sessions_ht
,
1561 hash_key_str(name
, lttng_ht_seed
),
1565 node
= cds_lfht_iter_get_node(&iter
);
1567 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1569 session
= caa_container_of(node
, struct session_info
,
1571 assert(session
->uid
== uid
);
1572 assert(session
->gid
== gid
);
1573 session_info_get(session
);
1577 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1578 if (!trigger_list
) {
1582 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1583 state
->sessions_ht
);
1585 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1587 lttng_session_trigger_list_destroy(trigger_list
);
1590 trigger_list
= NULL
;
1592 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1593 &session
->sessions_ht_node
);
1599 session_info_put(session
);
1604 int handle_notification_thread_command_add_channel(
1605 struct notification_thread_state
*state
,
1606 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1607 const char *channel_name
, enum lttng_domain_type channel_domain
,
1608 uint64_t channel_key_int
, uint64_t channel_capacity
,
1609 enum lttng_error_code
*cmd_result
)
1611 struct cds_list_head trigger_list
;
1612 struct channel_info
*new_channel_info
= NULL
;
1613 struct channel_key channel_key
= {
1614 .key
= channel_key_int
,
1615 .domain
= channel_domain
,
1617 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1618 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1619 int trigger_count
= 0;
1620 struct cds_lfht_iter iter
;
1621 struct session_info
*session_info
= NULL
;
1623 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1624 channel_name
, session_name
, channel_key_int
,
1625 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1627 CDS_INIT_LIST_HEAD(&trigger_list
);
1629 session_info
= find_or_create_session_info(state
, session_name
,
1630 session_uid
, session_gid
);
1631 if (!session_info
) {
1632 /* Allocation error or an internal error occurred. */
1636 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1637 channel_capacity
, session_info
);
1638 if (!new_channel_info
) {
1643 /* Build a list of all triggers applying to the new channel. */
1644 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1646 struct lttng_trigger_list_element
*new_element
;
1648 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1649 new_channel_info
)) {
1653 new_element
= zmalloc(sizeof(*new_element
));
1658 CDS_INIT_LIST_HEAD(&new_element
->node
);
1659 new_element
->trigger
= trigger_ht_element
->trigger
;
1660 cds_list_add(&new_element
->node
, &trigger_list
);
1665 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1667 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1668 if (!channel_trigger_list
) {
1671 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1672 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1673 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1674 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1677 /* Add channel to the channel_ht which owns the channel_infos. */
1678 cds_lfht_add(state
->channels_ht
,
1679 hash_channel_key(&new_channel_info
->key
),
1680 &new_channel_info
->channels_ht_node
);
1682 * Add the list of triggers associated with this channel to the
1683 * channel_triggers_ht.
1685 cds_lfht_add(state
->channel_triggers_ht
,
1686 hash_channel_key(&new_channel_info
->key
),
1687 &channel_trigger_list
->channel_triggers_ht_node
);
1689 session_info_put(session_info
);
1690 *cmd_result
= LTTNG_OK
;
1693 channel_info_destroy(new_channel_info
);
1694 session_info_put(session_info
);
1699 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1701 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1706 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1708 free(caa_container_of(node
, struct channel_state_sample
,
1713 int handle_notification_thread_command_remove_channel(
1714 struct notification_thread_state
*state
,
1715 uint64_t channel_key
, enum lttng_domain_type domain
,
1716 enum lttng_error_code
*cmd_result
)
1718 struct cds_lfht_node
*node
;
1719 struct cds_lfht_iter iter
;
1720 struct lttng_channel_trigger_list
*trigger_list
;
1721 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1722 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1723 struct channel_info
*channel_info
;
1725 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1726 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1730 cds_lfht_lookup(state
->channel_triggers_ht
,
1731 hash_channel_key(&key
),
1732 match_channel_trigger_list
,
1735 node
= cds_lfht_iter_get_node(&iter
);
1737 * There is a severe internal error if we are being asked to remove a
1738 * channel that doesn't exist.
1741 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1745 /* Free the list of triggers associated with this channel. */
1746 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1747 channel_triggers_ht_node
);
1748 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1749 &trigger_list
->list
, node
) {
1750 cds_list_del(&trigger_list_element
->node
);
1751 free(trigger_list_element
);
1753 cds_lfht_del(state
->channel_triggers_ht
, node
);
1754 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1756 /* Free sampled channel state. */
1757 cds_lfht_lookup(state
->channel_state_ht
,
1758 hash_channel_key(&key
),
1759 match_channel_state_sample
,
1762 node
= cds_lfht_iter_get_node(&iter
);
1764 * This is expected to be NULL if the channel is destroyed before we
1765 * received a sample.
1768 struct channel_state_sample
*sample
= caa_container_of(node
,
1769 struct channel_state_sample
,
1770 channel_state_ht_node
);
1772 cds_lfht_del(state
->channel_state_ht
, node
);
1773 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1776 /* Remove the channel from the channels_ht and free it. */
1777 cds_lfht_lookup(state
->channels_ht
,
1778 hash_channel_key(&key
),
1782 node
= cds_lfht_iter_get_node(&iter
);
1784 channel_info
= caa_container_of(node
, struct channel_info
,
1786 cds_lfht_del(state
->channels_ht
, node
);
1787 channel_info_destroy(channel_info
);
1790 *cmd_result
= LTTNG_OK
;
1795 int handle_notification_thread_command_session_rotation(
1796 struct notification_thread_state
*state
,
1797 enum notification_thread_command_type cmd_type
,
1798 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1799 uint64_t trace_archive_chunk_id
,
1800 struct lttng_trace_archive_location
*location
,
1801 enum lttng_error_code
*_cmd_result
)
1804 enum lttng_error_code cmd_result
= LTTNG_OK
;
1805 struct lttng_session_trigger_list
*trigger_list
;
1806 struct lttng_trigger_list_element
*trigger_list_element
;
1807 struct session_info
*session_info
;
1808 const struct lttng_credentials session_creds
= {
1809 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1810 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1815 session_info
= find_or_create_session_info(state
, session_name
,
1816 session_uid
, session_gid
);
1817 if (!session_info
) {
1818 /* Allocation error or an internal error occurred. */
1820 cmd_result
= LTTNG_ERR_NOMEM
;
1824 session_info
->rotation
.ongoing
=
1825 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1826 session_info
->rotation
.id
= trace_archive_chunk_id
;
1827 trigger_list
= get_session_trigger_list(state
, session_name
);
1828 if (!trigger_list
) {
1829 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1834 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1836 const struct lttng_condition
*condition
;
1837 struct lttng_trigger
*trigger
;
1838 struct notification_client_list
*client_list
;
1839 struct lttng_evaluation
*evaluation
= NULL
;
1840 enum lttng_condition_type condition_type
;
1841 enum action_executor_status executor_status
;
1843 trigger
= trigger_list_element
->trigger
;
1844 condition
= lttng_trigger_get_const_condition(trigger
);
1846 condition_type
= lttng_condition_get_type(condition
);
1848 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1849 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1851 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1852 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1856 client_list
= get_client_list_from_condition(state
, condition
);
1857 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1858 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1859 trace_archive_chunk_id
);
1861 evaluation
= lttng_evaluation_session_rotation_completed_create(
1862 trace_archive_chunk_id
, location
);
1866 /* Internal error */
1868 cmd_result
= LTTNG_ERR_UNK
;
1873 * Ownership of `evaluation` transferred to the action executor
1874 * no matter the result.
1876 executor_status
= action_executor_enqueue(state
->executor
,
1877 trigger
, evaluation
, &session_creds
,
1880 switch (executor_status
) {
1881 case ACTION_EXECUTOR_STATUS_OK
:
1883 case ACTION_EXECUTOR_STATUS_ERROR
:
1884 case ACTION_EXECUTOR_STATUS_INVALID
:
1886 * TODO Add trigger identification (name/id) when
1887 * it is added to the API.
1889 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1892 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1894 * TODO Add trigger identification (name/id) when
1895 * it is added to the API.
1897 * Not a fatal error.
1899 WARN("No space left when enqueuing action associated with session rotation trigger");
1907 notification_client_list_put(client_list
);
1908 if (caa_unlikely(ret
)) {
1913 session_info_put(session_info
);
1914 *_cmd_result
= cmd_result
;
1920 int handle_notification_thread_command_add_tracer_event_source(
1921 struct notification_thread_state
*state
,
1922 int tracer_event_source_fd
,
1923 enum lttng_domain_type domain_type
,
1924 enum lttng_error_code
*_cmd_result
)
1927 enum lttng_error_code cmd_result
= LTTNG_OK
;
1928 struct notification_event_tracer_event_source_element
*element
= NULL
;
1930 element
= zmalloc(sizeof(*element
));
1932 cmd_result
= LTTNG_ERR_NOMEM
;
1937 element
->fd
= tracer_event_source_fd
;
1938 element
->domain
= domain_type
;
1940 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
1942 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
1943 tracer_event_source_fd
,
1944 lttng_domain_type_str(domain_type
));
1946 /* Adding the read side pipe to the event poll. */
1947 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
1949 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
1950 tracer_event_source_fd
,
1951 lttng_domain_type_str(element
->domain
));
1952 cds_list_del(&element
->node
);
1957 element
->is_fd_in_poll_set
= true;
1960 *_cmd_result
= cmd_result
;
1965 int handle_notification_thread_command_remove_tracer_event_source(
1966 struct notification_thread_state
*state
,
1967 int tracer_event_source_fd
,
1968 enum lttng_error_code
*_cmd_result
)
1972 enum lttng_error_code cmd_result
= LTTNG_OK
;
1973 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
1975 cds_list_for_each_entry_safe(source_element
, tmp
,
1976 &state
->tracer_event_sources_list
, node
) {
1977 if (source_element
->fd
!= tracer_event_source_fd
) {
1981 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
1982 tracer_event_source_fd
,
1983 lttng_domain_type_str(source_element
->domain
));
1984 cds_list_del(&source_element
->node
);
1991 * This is temporarily allowed since the poll activity set is
1992 * not properly cleaned-up for the moment. This is adressed in
1995 source_element
= NULL
;
1999 if (!source_element
->is_fd_in_poll_set
) {
2000 /* Skip the poll set removal. */
2004 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2005 tracer_event_source_fd
,
2006 lttng_domain_type_str(source_element
->domain
));
2008 /* Removing the fd from the event poll set. */
2009 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2011 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2012 tracer_event_source_fd
,
2013 lttng_domain_type_str(source_element
->domain
));
2014 cmd_result
= LTTNG_ERR_FATAL
;
2018 source_element
->is_fd_in_poll_set
= false;
2021 free(source_element
);
2022 *_cmd_result
= cmd_result
;
2026 int handle_notification_thread_remove_tracer_event_source_no_result(
2027 struct notification_thread_state
*state
,
2028 int tracer_event_source_fd
)
2031 enum lttng_error_code cmd_result
;
2033 ret
= handle_notification_thread_command_remove_tracer_event_source(
2034 state
, tracer_event_source_fd
, &cmd_result
);
2039 static int handle_notification_thread_command_list_triggers(
2040 struct notification_thread_handle
*handle
,
2041 struct notification_thread_state
*state
,
2043 struct lttng_triggers
**triggers
,
2044 enum lttng_error_code
*_cmd_result
)
2047 enum lttng_error_code cmd_result
= LTTNG_OK
;
2048 struct cds_lfht_iter iter
;
2049 struct lttng_trigger_ht_element
*trigger_ht_element
;
2050 struct lttng_triggers
*local_triggers
= NULL
;
2051 const struct lttng_credentials
*creds
;
2055 local_triggers
= lttng_triggers_create();
2056 if (!local_triggers
) {
2057 /* Not a fatal error. */
2058 cmd_result
= LTTNG_ERR_NOMEM
;
2062 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2063 trigger_ht_element
, node
) {
2065 * Only return the triggers to which the client has access.
2066 * The root user has visibility over all triggers.
2068 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2069 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2073 ret
= lttng_triggers_add(local_triggers
,
2074 trigger_ht_element
->trigger
);
2076 /* Not a fatal error. */
2078 cmd_result
= LTTNG_ERR_NOMEM
;
2083 /* Transferring ownership to the caller. */
2084 *triggers
= local_triggers
;
2085 local_triggers
= NULL
;
2089 lttng_triggers_destroy(local_triggers
);
2090 *_cmd_result
= cmd_result
;
2095 bool condition_is_supported(struct lttng_condition
*condition
)
2099 switch (lttng_condition_get_type(condition
)) {
2100 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2101 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2104 enum lttng_domain_type domain
;
2106 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2110 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2111 is_supported
= true;
2116 * Older kernel tracers don't expose the API to monitor their
2117 * buffers. Therefore, we reject triggers that require that
2118 * mechanism to be available to be evaluated.
2120 * Assume unsupported on error.
2122 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2125 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
2127 const struct lttng_event_rule
*event_rule
;
2128 enum lttng_domain_type domain
;
2129 const enum lttng_condition_status status
=
2130 lttng_condition_event_rule_get_rule(
2131 condition
, &event_rule
);
2133 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2135 domain
= lttng_event_rule_get_domain_type(event_rule
);
2136 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2137 is_supported
= true;
2142 * Older kernel tracers can't emit notification. Therefore, we
2143 * reject triggers that require that mechanism to be available
2146 * Assume unsupported on error.
2148 is_supported
= kernel_supports_event_notifiers() == 1;
2152 is_supported
= true;
2155 return is_supported
;
2158 /* Must be called with RCU read lock held. */
2160 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2161 struct notification_thread_state
*state
)
2164 const struct lttng_condition
*condition
;
2165 const char *session_name
;
2166 struct lttng_session_trigger_list
*trigger_list
;
2168 condition
= lttng_trigger_get_const_condition(trigger
);
2169 switch (lttng_condition_get_type(condition
)) {
2170 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2171 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2173 enum lttng_condition_status status
;
2175 status
= lttng_condition_session_rotation_get_session_name(
2176 condition
, &session_name
);
2177 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2178 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2189 trigger_list
= get_session_trigger_list(state
, session_name
);
2190 if (!trigger_list
) {
2191 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2197 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2199 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2204 /* Must be called with RCU read lock held. */
2206 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2207 struct notification_thread_state
*state
)
2210 struct cds_lfht_node
*node
;
2211 struct cds_lfht_iter iter
;
2212 struct channel_info
*channel
;
2214 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2216 struct lttng_trigger_list_element
*trigger_list_element
;
2217 struct lttng_channel_trigger_list
*trigger_list
;
2218 struct cds_lfht_iter lookup_iter
;
2220 if (!trigger_applies_to_channel(trigger
, channel
)) {
2224 cds_lfht_lookup(state
->channel_triggers_ht
,
2225 hash_channel_key(&channel
->key
),
2226 match_channel_trigger_list
,
2229 node
= cds_lfht_iter_get_node(&lookup_iter
);
2231 trigger_list
= caa_container_of(node
,
2232 struct lttng_channel_trigger_list
,
2233 channel_triggers_ht_node
);
2235 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2236 if (!trigger_list_element
) {
2240 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2241 trigger_list_element
->trigger
= trigger
;
2242 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2243 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2251 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2253 bool is_notify
= false;
2254 unsigned int i
, count
;
2255 enum lttng_action_status action_status
;
2256 const struct lttng_action
*action
=
2257 lttng_trigger_get_const_action(trigger
);
2258 enum lttng_action_type action_type
;
2261 action_type
= lttng_action_get_type(action
);
2262 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2265 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2269 action_status
= lttng_action_group_get_count(action
, &count
);
2270 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2272 for (i
= 0; i
< count
; i
++) {
2273 const struct lttng_action
*inner_action
=
2274 lttng_action_group_get_at_index(
2277 action_type
= lttng_action_get_type(inner_action
);
2278 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2288 static bool trigger_name_taken(struct notification_thread_state
*state
,
2289 const struct lttng_trigger
*trigger
)
2291 struct cds_lfht_iter iter
;
2294 * No duplicata is allowed in the triggers_by_name_uid_ht.
2295 * The match is done against the trigger name and uid.
2297 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2298 hash_trigger_by_name_uid(trigger
),
2299 match_trigger_by_name_uid
,
2302 return !!cds_lfht_iter_get_node(&iter
);
2306 enum lttng_error_code
generate_trigger_name(
2307 struct notification_thread_state
*state
,
2308 struct lttng_trigger
*trigger
, const char **name
)
2310 enum lttng_error_code ret_code
= LTTNG_OK
;
2312 enum lttng_trigger_status status
;
2315 const int ret
= lttng_trigger_generate_name(trigger
,
2316 state
->trigger_id
.name_offset
++);
2318 /* The only reason this can fail right now. */
2319 ret_code
= LTTNG_ERR_NOMEM
;
2323 status
= lttng_trigger_get_name(trigger
, name
);
2324 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2326 taken
= trigger_name_taken(state
, trigger
);
2327 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2333 * FIXME A client's credentials are not checked when registering a trigger.
2335 * The effects of this are benign since:
2336 * - The client will succeed in registering the trigger, as it is valid,
2337 * - The trigger will, internally, be bound to the channel/session,
2338 * - The notifications will not be sent since the client's credentials
2339 * are checked against the channel at that moment.
2341 * If this function returns a non-zero value, it means something is
2342 * fundamentally broken and the whole subsystem/thread will be torn down.
2344 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2348 int handle_notification_thread_command_register_trigger(
2349 struct notification_thread_state
*state
,
2350 struct lttng_trigger
*trigger
,
2351 enum lttng_error_code
*cmd_result
)
2354 struct lttng_condition
*condition
;
2355 struct notification_client
*client
;
2356 struct notification_client_list
*client_list
= NULL
;
2357 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2358 struct notification_client_list_element
*client_list_element
;
2359 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2360 struct cds_lfht_node
*node
;
2361 struct cds_lfht_iter iter
;
2362 const char* trigger_name
;
2363 bool free_trigger
= true;
2364 struct lttng_evaluation
*evaluation
= NULL
;
2365 struct lttng_credentials object_creds
;
2368 enum action_executor_status executor_status
;
2369 const uint64_t trigger_tracer_token
=
2370 state
->trigger_id
.next_tracer_token
++;
2374 /* Set the trigger's tracer token. */
2375 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2377 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2378 LTTNG_TRIGGER_STATUS_UNSET
) {
2379 const enum lttng_error_code ret_code
= generate_trigger_name(
2380 state
, trigger
, &trigger_name
);
2382 if (ret_code
!= LTTNG_OK
) {
2385 *cmd_result
= ret_code
;
2388 } else if (trigger_name_taken(state
, trigger
)) {
2389 /* Not a fatal error. */
2390 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2395 condition
= lttng_trigger_get_condition(trigger
);
2398 /* Some conditions require tracers to implement a minimal ABI version. */
2399 if (!condition_is_supported(condition
)) {
2400 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2404 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2405 if (!trigger_ht_element
) {
2410 /* Add trigger to the trigger_ht. */
2411 cds_lfht_node_init(&trigger_ht_element
->node
);
2412 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2413 trigger_ht_element
->trigger
= trigger
;
2415 node
= cds_lfht_add_unique(state
->triggers_ht
,
2416 lttng_condition_hash(condition
),
2419 &trigger_ht_element
->node
);
2420 if (node
!= &trigger_ht_element
->node
) {
2421 /* Not a fatal error, simply report it to the client. */
2422 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2423 goto error_free_ht_element
;
2426 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2427 hash_trigger_by_name_uid(trigger
),
2428 match_trigger_by_name_uid
,
2430 &trigger_ht_element
->node_by_name_uid
);
2431 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2432 /* Not a fatal error, simply report it to the client. */
2433 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2434 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2435 goto error_free_ht_element
;
2438 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2439 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2440 if (!trigger_tokens_ht_element
) {
2443 cds_lfht_del(state
->triggers_ht
,
2444 &trigger_ht_element
->node
);
2445 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2446 &trigger_ht_element
->node_by_name_uid
);
2447 goto error_free_ht_element
;
2450 /* Add trigger token to the trigger_tokens_ht. */
2451 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2452 trigger_tokens_ht_element
->token
=
2453 LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2454 trigger_tokens_ht_element
->trigger
= trigger
;
2456 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2457 hash_key_u64(&trigger_tokens_ht_element
->token
,
2459 match_trigger_token
,
2460 &trigger_tokens_ht_element
->token
,
2461 &trigger_tokens_ht_element
->node
);
2462 if (node
!= &trigger_tokens_ht_element
->node
) {
2463 /* Internal corruption, fatal error. */
2465 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2466 cds_lfht_del(state
->triggers_ht
,
2467 &trigger_ht_element
->node
);
2468 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2469 &trigger_ht_element
->node_by_name_uid
);
2470 goto error_free_ht_element
;
2475 * Ownership of the trigger and of its wrapper was transfered to
2476 * the triggers_ht. Same for token ht element if necessary.
2478 trigger_tokens_ht_element
= NULL
;
2479 trigger_ht_element
= NULL
;
2480 free_trigger
= false;
2483 * The rest only applies to triggers that have a "notify" action.
2484 * It is not skipped as this is the only action type currently
2487 if (is_trigger_action_notify(trigger
)) {
2488 client_list
= notification_client_list_create(trigger
);
2491 goto error_free_ht_element
;
2494 /* Build a list of clients to which this new trigger applies. */
2495 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2496 client_socket_ht_node
) {
2497 if (!trigger_applies_to_client(trigger
, client
)) {
2501 client_list_element
=
2502 zmalloc(sizeof(*client_list_element
));
2503 if (!client_list_element
) {
2505 goto error_put_client_list
;
2508 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2509 client_list_element
->client
= client
;
2510 cds_list_add(&client_list_element
->node
,
2511 &client_list
->list
);
2515 * Client list ownership transferred to the
2516 * notification_trigger_clients_ht.
2518 publish_notification_client_list(state
, client_list
);
2521 switch (get_condition_binding_object(condition
)) {
2522 case LTTNG_OBJECT_TYPE_SESSION
:
2523 /* Add the trigger to the list if it matches a known session. */
2524 ret
= bind_trigger_to_matching_session(trigger
, state
);
2526 goto error_put_client_list
;
2529 case LTTNG_OBJECT_TYPE_CHANNEL
:
2531 * Add the trigger to list of triggers bound to the channels
2534 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2536 goto error_put_client_list
;
2539 case LTTNG_OBJECT_TYPE_NONE
:
2542 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2544 goto error_put_client_list
;
2548 * The new trigger's condition must be evaluated against the current
2551 * In the case of `notify` action, nothing preventing clients from
2552 * subscribing to a condition before the corresponding trigger is
2553 * registered, we have to evaluate this new condition right away.
2555 * At some point, we were waiting for the next "evaluation" (e.g. on
2556 * reception of a channel sample) to evaluate this new condition, but
2559 * The reason it was broken is that waiting for the next sample
2560 * does not allow us to properly handle transitions for edge-triggered
2563 * Consider this example: when we handle a new channel sample, we
2564 * evaluate each conditions twice: once with the previous state, and
2565 * again with the newest state. We then use those two results to
2566 * determine whether a state change happened: a condition was false and
2567 * became true. If a state change happened, we have to notify clients.
2569 * Now, if a client subscribes to a given notification and registers
2570 * a trigger *after* that subscription, we have to make sure the
2571 * condition is evaluated at this point while considering only the
2572 * current state. Otherwise, the next evaluation cycle may only see
2573 * that the evaluations remain the same (true for samples n-1 and n) and
2574 * the client will never know that the condition has been met.
2576 switch (get_condition_binding_object(condition
)) {
2577 case LTTNG_OBJECT_TYPE_SESSION
:
2578 ret
= evaluate_session_condition_for_client(condition
, state
,
2579 &evaluation
, &object_uid
,
2582 case LTTNG_OBJECT_TYPE_CHANNEL
:
2583 ret
= evaluate_channel_condition_for_client(condition
, state
,
2584 &evaluation
, &object_uid
,
2587 case LTTNG_OBJECT_TYPE_NONE
:
2590 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2598 goto error_put_client_list
;
2601 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2602 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2604 DBG("Newly registered trigger's condition evaluated to %s",
2605 evaluation
? "true" : "false");
2607 /* Evaluation yielded nothing. Normal exit. */
2613 * Ownership of `evaluation` transferred to the action executor
2614 * no matter the result.
2616 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2617 evaluation
, &object_creds
, client_list
);
2619 switch (executor_status
) {
2620 case ACTION_EXECUTOR_STATUS_OK
:
2622 case ACTION_EXECUTOR_STATUS_ERROR
:
2623 case ACTION_EXECUTOR_STATUS_INVALID
:
2625 * TODO Add trigger identification (name/id) when
2626 * it is added to the API.
2628 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2630 goto error_put_client_list
;
2631 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2633 * TODO Add trigger identification (name/id) when
2634 * it is added to the API.
2636 * Not a fatal error.
2638 WARN("No space left when enqueuing action associated to newly registered trigger");
2646 *cmd_result
= LTTNG_OK
;
2647 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2648 trigger_name
, trigger_tracer_token
);
2650 error_put_client_list
:
2651 notification_client_list_put(client_list
);
2653 error_free_ht_element
:
2654 if (trigger_ht_element
) {
2655 /* Delayed removal due to RCU constraint on delete. */
2656 call_rcu(&trigger_ht_element
->rcu_node
,
2657 free_lttng_trigger_ht_element_rcu
);
2660 free(trigger_tokens_ht_element
);
2663 lttng_trigger_destroy(trigger
);
2670 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2672 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2677 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2679 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2684 int handle_notification_thread_command_unregister_trigger(
2685 struct notification_thread_state
*state
,
2686 const struct lttng_trigger
*trigger
,
2687 enum lttng_error_code
*_cmd_reply
)
2689 struct cds_lfht_iter iter
;
2690 struct cds_lfht_node
*triggers_ht_node
;
2691 struct lttng_channel_trigger_list
*trigger_list
;
2692 struct notification_client_list
*client_list
;
2693 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2694 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
2696 enum lttng_error_code cmd_reply
;
2700 cds_lfht_lookup(state
->triggers_ht
,
2701 lttng_condition_hash(condition
),
2705 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2706 if (!triggers_ht_node
) {
2707 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2710 cmd_reply
= LTTNG_OK
;
2713 /* Remove trigger from channel_triggers_ht. */
2714 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2715 channel_triggers_ht_node
) {
2716 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2718 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2719 &trigger_list
->list
, node
) {
2720 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2724 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2725 cds_list_del(&trigger_element
->node
);
2726 /* A trigger can only appear once per channel */
2731 if (lttng_condition_get_type(condition
) ==
2732 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2733 struct notification_trigger_tokens_ht_element
2734 *trigger_tokens_ht_element
;
2736 cds_lfht_for_each_entry (state
->trigger_tokens_ht
, &iter
,
2737 trigger_tokens_ht_element
, node
) {
2738 if (!lttng_trigger_is_equal(trigger
,
2739 trigger_tokens_ht_element
->trigger
)) {
2743 DBG("[notification-thread] Removed trigger from tokens_ht");
2744 cds_lfht_del(state
->trigger_tokens_ht
,
2745 &trigger_tokens_ht_element
->node
);
2746 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2747 free_notification_trigger_tokens_ht_element_rcu
);
2753 if (is_trigger_action_notify(trigger
)) {
2755 * Remove and release the client list from
2756 * notification_trigger_clients_ht.
2758 client_list
= get_client_list_from_condition(state
, condition
);
2759 assert(client_list
);
2761 /* Put new reference and the hashtable's reference. */
2762 notification_client_list_put(client_list
);
2763 notification_client_list_put(client_list
);
2767 /* Remove trigger from triggers_ht. */
2768 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2769 struct lttng_trigger_ht_element
, node
);
2770 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2771 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2773 /* Release the ownership of the trigger. */
2774 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2775 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2779 *_cmd_reply
= cmd_reply
;
2784 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2785 int handle_notification_thread_command(
2786 struct notification_thread_handle
*handle
,
2787 struct notification_thread_state
*state
)
2791 struct notification_thread_command
*cmd
;
2793 /* Read the event pipe to put it back into a quiescent state. */
2794 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2796 if (ret
!= sizeof(counter
)) {
2800 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2801 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2802 struct notification_thread_command
, cmd_list_node
);
2803 cds_list_del(&cmd
->cmd_list_node
);
2804 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2805 switch (cmd
->type
) {
2806 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2807 DBG("[notification-thread] Received register trigger command");
2808 ret
= handle_notification_thread_command_register_trigger(state
,
2809 cmd
->parameters
.register_trigger
.trigger
,
2812 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2813 DBG("[notification-thread] Received unregister trigger command");
2814 ret
= handle_notification_thread_command_unregister_trigger(
2816 cmd
->parameters
.unregister_trigger
.trigger
,
2819 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2820 DBG("[notification-thread] Received add channel command");
2821 ret
= handle_notification_thread_command_add_channel(
2823 cmd
->parameters
.add_channel
.session
.name
,
2824 cmd
->parameters
.add_channel
.session
.uid
,
2825 cmd
->parameters
.add_channel
.session
.gid
,
2826 cmd
->parameters
.add_channel
.channel
.name
,
2827 cmd
->parameters
.add_channel
.channel
.domain
,
2828 cmd
->parameters
.add_channel
.channel
.key
,
2829 cmd
->parameters
.add_channel
.channel
.capacity
,
2832 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2833 DBG("[notification-thread] Received remove channel command");
2834 ret
= handle_notification_thread_command_remove_channel(
2835 state
, cmd
->parameters
.remove_channel
.key
,
2836 cmd
->parameters
.remove_channel
.domain
,
2839 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2840 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2841 DBG("[notification-thread] Received session rotation %s command",
2842 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2843 "ongoing" : "completed");
2844 ret
= handle_notification_thread_command_session_rotation(
2847 cmd
->parameters
.session_rotation
.session_name
,
2848 cmd
->parameters
.session_rotation
.uid
,
2849 cmd
->parameters
.session_rotation
.gid
,
2850 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2851 cmd
->parameters
.session_rotation
.location
,
2854 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
2855 ret
= handle_notification_thread_command_add_tracer_event_source(
2857 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2858 cmd
->parameters
.tracer_event_source
.domain
,
2861 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
2862 ret
= handle_notification_thread_command_remove_tracer_event_source(
2864 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2867 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2869 struct lttng_triggers
*triggers
= NULL
;
2871 ret
= handle_notification_thread_command_list_triggers(
2874 cmd
->parameters
.list_triggers
.uid
,
2877 cmd
->reply
.list_triggers
.triggers
= triggers
;
2881 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2882 DBG("[notification-thread] Received quit command");
2883 cmd
->reply_code
= LTTNG_OK
;
2886 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2888 const enum client_transmission_status client_status
=
2889 cmd
->parameters
.client_communication_update
2891 const notification_client_id client_id
=
2892 cmd
->parameters
.client_communication_update
.id
;
2893 struct notification_client
*client
;
2896 client
= get_client_from_id(client_id
, state
);
2900 * Client error was probably already picked-up by the
2901 * notification thread or it has disconnected
2902 * gracefully while this command was queued.
2904 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2908 ret
= client_handle_transmission_status(
2909 client
, client_status
, state
);
2915 ERR("[notification-thread] Unknown internal command received");
2923 if (cmd
->is_async
) {
2927 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2931 /* Wake-up and return a fatal error to the calling thread. */
2932 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2933 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2935 /* Indicate a fatal error to the caller. */
2940 int socket_set_non_blocking(int socket
)
2944 /* Set the pipe as non-blocking. */
2945 ret
= fcntl(socket
, F_GETFL
, 0);
2947 PERROR("fcntl get socket flags");
2952 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2954 PERROR("fcntl set O_NONBLOCK socket flag");
2957 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2963 int client_reset_inbound_state(struct notification_client
*client
)
2968 lttng_payload_clear(&client
->communication
.inbound
.payload
);
2970 client
->communication
.inbound
.bytes_to_receive
=
2971 sizeof(struct lttng_notification_channel_message
);
2972 client
->communication
.inbound
.msg_type
=
2973 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2974 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2975 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2976 ret
= lttng_dynamic_buffer_set_size(
2977 &client
->communication
.inbound
.payload
.buffer
,
2978 client
->communication
.inbound
.bytes_to_receive
);
2983 int handle_notification_thread_client_connect(
2984 struct notification_thread_state
*state
)
2987 struct notification_client
*client
;
2989 DBG("[notification-thread] Handling new notification channel client connection");
2991 client
= zmalloc(sizeof(*client
));
2998 pthread_mutex_init(&client
->lock
, NULL
);
2999 client
->id
= state
->next_notification_client_id
++;
3000 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3001 lttng_payload_init(&client
->communication
.inbound
.payload
);
3002 lttng_payload_init(&client
->communication
.outbound
.payload
);
3003 client
->communication
.inbound
.expect_creds
= true;
3005 ret
= client_reset_inbound_state(client
);
3007 ERR("[notification-thread] Failed to reset client communication's inbound state");
3012 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3014 ERR("[notification-thread] Failed to accept new notification channel client connection");
3019 client
->socket
= ret
;
3021 ret
= socket_set_non_blocking(client
->socket
);
3023 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3027 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3029 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3034 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3035 LPOLLIN
| LPOLLERR
|
3036 LPOLLHUP
| LPOLLRDHUP
);
3038 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3042 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3046 cds_lfht_add(state
->client_socket_ht
,
3047 hash_client_socket(client
->socket
),
3048 &client
->client_socket_ht_node
);
3049 cds_lfht_add(state
->client_id_ht
,
3050 hash_client_id(client
->id
),
3051 &client
->client_id_ht_node
);
3057 notification_client_destroy(client
, state
);
3062 * RCU read-lock must be held by the caller.
3063 * Client lock must _not_ be held by the caller.
3066 int notification_thread_client_disconnect(
3067 struct notification_client
*client
,
3068 struct notification_thread_state
*state
)
3071 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3073 /* Acquire the client lock to disable its communication atomically. */
3074 pthread_mutex_lock(&client
->lock
);
3075 client
->communication
.active
= false;
3076 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3077 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3078 pthread_mutex_unlock(&client
->lock
);
3080 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3082 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3086 /* Release all conditions to which the client was subscribed. */
3087 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3088 &client
->condition_list
, node
) {
3089 (void) notification_thread_client_unsubscribe(client
,
3090 condition_list_element
->condition
, state
, NULL
);
3094 * Client no longer accessible to other threads (through the
3097 notification_client_destroy(client
, state
);
3101 int handle_notification_thread_client_disconnect(
3102 int client_socket
, struct notification_thread_state
*state
)
3105 struct notification_client
*client
;
3108 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3110 client
= get_client_from_socket(client_socket
, state
);
3112 /* Internal state corruption, fatal error. */
3113 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3119 ret
= notification_thread_client_disconnect(client
, state
);
3125 int handle_notification_thread_client_disconnect_all(
3126 struct notification_thread_state
*state
)
3128 struct cds_lfht_iter iter
;
3129 struct notification_client
*client
;
3130 bool error_encoutered
= false;
3133 DBG("[notification-thread] Closing all client connections");
3134 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3135 client_socket_ht_node
) {
3138 ret
= notification_thread_client_disconnect(
3141 error_encoutered
= true;
3145 return error_encoutered
? 1 : 0;
3148 int handle_notification_thread_trigger_unregister_all(
3149 struct notification_thread_state
*state
)
3151 bool error_occurred
= false;
3152 struct cds_lfht_iter iter
;
3153 struct lttng_trigger_ht_element
*trigger_ht_element
;
3156 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3158 int ret
= handle_notification_thread_command_unregister_trigger(
3159 state
, trigger_ht_element
->trigger
, NULL
);
3161 error_occurred
= true;
3165 return error_occurred
? -1 : 0;
3169 int client_handle_transmission_status(
3170 struct notification_client
*client
,
3171 enum client_transmission_status transmission_status
,
3172 struct notification_thread_state
*state
)
3176 switch (transmission_status
) {
3177 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3178 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3179 CLIENT_POLL_MASK_IN
);
3185 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3187 * We want to be notified whenever there is buffer space
3188 * available to send the rest of the payload.
3190 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3191 CLIENT_POLL_MASK_IN_OUT
);
3196 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3197 ret
= notification_thread_client_disconnect(client
, state
);
3202 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3212 /* Client lock must be acquired by caller. */
3214 enum client_transmission_status
client_flush_outgoing_queue(
3215 struct notification_client
*client
)
3218 size_t to_send_count
;
3219 enum client_transmission_status status
;
3220 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3221 &client
->communication
.outbound
.payload
, 0, -1);
3222 const int fds_to_send_count
=
3223 lttng_payload_view_get_fd_handle_count(&pv
);
3225 ASSERT_LOCKED(client
->lock
);
3227 if (!client
->communication
.active
) {
3228 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3232 if (pv
.buffer
.size
== 0) {
3234 * If both data and fds are equal to zero, we are in an invalid
3237 assert(fds_to_send_count
!= 0);
3242 to_send_count
= pv
.buffer
.size
;
3243 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3246 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3249 if ((ret
>= 0 && ret
< to_send_count
)) {
3250 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3252 to_send_count
-= max(ret
, 0);
3254 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3256 pv
.buffer
.size
- to_send_count
,
3258 ret
= lttng_dynamic_buffer_set_size(
3259 &client
->communication
.outbound
.payload
.buffer
,
3265 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3267 } else if (ret
< 0) {
3268 /* Generic error, disable the client's communication. */
3269 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3271 client
->communication
.active
= false;
3272 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3276 * No error and flushed the queue completely.
3278 * The payload buffer size is used later to
3279 * check if there is notifications queued. So albeit that the
3280 * direct caller knows that the transmission is complete, we
3281 * need to set the buffer size to zero.
3283 ret
= lttng_dynamic_buffer_set_size(
3284 &client
->communication
.outbound
.payload
.buffer
, 0);
3291 /* No fds to send, transmission is complete. */
3292 if (fds_to_send_count
== 0) {
3293 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3297 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3298 client
->socket
, &pv
);
3300 /* Generic error, disable the client's communication. */
3301 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3303 client
->communication
.active
= false;
3304 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3306 } else if (ret
== 0) {
3307 /* Nothing could be sent. */
3308 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3310 /* Fd passing is an all or nothing kind of thing. */
3311 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3313 * The payload _fd_array count is used later to
3314 * check if there is notifications queued. So although the
3315 * direct caller knows that the transmission is complete, we
3316 * need to clear the _fd_array for the queuing check.
3318 lttng_dynamic_pointer_array_clear(
3319 &client
->communication
.outbound
.payload
3324 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3325 client
->communication
.outbound
.queued_command_reply
= false;
3326 client
->communication
.outbound
.dropped_notification
= false;
3327 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3332 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3336 bool client_has_outbound_data_left(
3337 const struct notification_client
*client
)
3339 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3340 &client
->communication
.outbound
.payload
, 0, -1);
3341 const bool has_data
= pv
.buffer
.size
!= 0;
3342 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3344 return has_data
|| has_fds
;
3347 /* Client lock must _not_ be held by the caller. */
3349 int client_send_command_reply(struct notification_client
*client
,
3350 struct notification_thread_state
*state
,
3351 enum lttng_notification_channel_status status
)
3354 struct lttng_notification_channel_command_reply reply
= {
3355 .status
= (int8_t) status
,
3357 struct lttng_notification_channel_message msg
= {
3358 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3359 .size
= sizeof(reply
),
3361 char buffer
[sizeof(msg
) + sizeof(reply
)];
3362 enum client_transmission_status transmission_status
;
3364 memcpy(buffer
, &msg
, sizeof(msg
));
3365 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3366 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3368 pthread_mutex_lock(&client
->lock
);
3369 if (client
->communication
.outbound
.queued_command_reply
) {
3370 /* Protocol error. */
3374 /* Enqueue buffer to outgoing queue and flush it. */
3375 ret
= lttng_dynamic_buffer_append(
3376 &client
->communication
.outbound
.payload
.buffer
,
3377 buffer
, sizeof(buffer
));
3382 transmission_status
= client_flush_outgoing_queue(client
);
3384 if (client_has_outbound_data_left(client
)) {
3385 /* Queue could not be emptied. */
3386 client
->communication
.outbound
.queued_command_reply
= true;
3389 pthread_mutex_unlock(&client
->lock
);
3390 ret
= client_handle_transmission_status(
3391 client
, transmission_status
, state
);
3398 pthread_mutex_unlock(&client
->lock
);
3404 int client_handle_message_unknown(struct notification_client
*client
,
3405 struct notification_thread_state
*state
)
3409 * Receiving message header. The function will be called again
3410 * once the rest of the message as been received and can be
3413 const struct lttng_notification_channel_message
*msg
;
3415 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3416 msg
= (const struct lttng_notification_channel_message
*)
3417 client
->communication
.inbound
.payload
.buffer
.data
;
3419 if (msg
->size
== 0 ||
3420 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3421 ERR("[notification-thread] Invalid notification channel message: length = %u",
3427 switch (msg
->type
) {
3428 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3429 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3430 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3434 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3438 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3439 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3440 client
->communication
.inbound
.msg_type
=
3441 (enum lttng_notification_channel_message_type
) msg
->type
;
3442 ret
= lttng_dynamic_buffer_set_size(
3443 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3445 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3452 int client_handle_message_handshake(struct notification_client
*client
,
3453 struct notification_thread_state
*state
)
3456 struct lttng_notification_channel_command_handshake
*handshake_client
;
3457 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3458 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3459 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3461 const struct lttng_notification_channel_message msg_header
= {
3462 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3463 .size
= sizeof(handshake_reply
),
3465 enum lttng_notification_channel_status status
=
3466 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3467 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3469 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3470 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3471 sizeof(handshake_reply
));
3474 (struct lttng_notification_channel_command_handshake
*)
3475 client
->communication
.inbound
.payload
.buffer
3477 client
->major
= handshake_client
->major
;
3478 client
->minor
= handshake_client
->minor
;
3479 if (!client
->communication
.inbound
.creds_received
) {
3480 ERR("[notification-thread] No credentials received from client");
3485 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3486 &client
->communication
.inbound
.creds
);
3487 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3488 &client
->communication
.inbound
.creds
);
3489 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3490 client
->uid
, client
->gid
, (int) client
->major
,
3491 (int) client
->minor
);
3493 if (handshake_client
->major
!=
3494 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3495 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3498 pthread_mutex_lock(&client
->lock
);
3499 /* Outgoing queue will be flushed when the command reply is sent. */
3500 ret
= lttng_dynamic_buffer_append(
3501 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3502 sizeof(send_buffer
));
3504 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3508 client
->validated
= true;
3509 client
->communication
.active
= true;
3510 pthread_mutex_unlock(&client
->lock
);
3512 /* Set reception state to receive the next message header. */
3513 ret
= client_reset_inbound_state(client
);
3515 ERR("[notification-thread] Failed to reset client communication's inbound state");
3519 /* Flushes the outgoing queue. */
3520 ret
= client_send_command_reply(client
, state
, status
);
3522 ERR("[notification-thread] Failed to send reply to notification channel client");
3528 pthread_mutex_unlock(&client
->lock
);
3534 int client_handle_message_subscription(
3535 struct notification_client
*client
,
3536 enum lttng_notification_channel_message_type msg_type
,
3537 struct notification_thread_state
*state
)
3540 struct lttng_condition
*condition
;
3541 enum lttng_notification_channel_status status
=
3542 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3543 struct lttng_payload_view condition_view
=
3544 lttng_payload_view_from_payload(
3545 &client
->communication
.inbound
.payload
,
3547 size_t expected_condition_size
;
3550 * No need to lock client to sample the inbound state as the only
3551 * other thread accessing clients (action executor) only uses the
3554 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3555 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3556 if (ret
!= expected_condition_size
) {
3557 ERR("[notification-thread] Malformed condition received from client");
3561 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3562 ret
= notification_thread_client_subscribe(
3563 client
, condition
, state
, &status
);
3565 ret
= notification_thread_client_unsubscribe(
3566 client
, condition
, state
, &status
);
3573 /* Set reception state to receive the next message header. */
3574 ret
= client_reset_inbound_state(client
);
3576 ERR("[notification-thread] Failed to reset client communication's inbound state");
3580 ret
= client_send_command_reply(client
, state
, status
);
3582 ERR("[notification-thread] Failed to send reply to notification channel client");
3591 int client_dispatch_message(struct notification_client
*client
,
3592 struct notification_thread_state
*state
)
3596 if (client
->communication
.inbound
.msg_type
!=
3597 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3598 client
->communication
.inbound
.msg_type
!=
3599 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3600 !client
->validated
) {
3601 WARN("[notification-thread] client attempted a command before handshake");
3606 switch (client
->communication
.inbound
.msg_type
) {
3607 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3609 ret
= client_handle_message_unknown(client
, state
);
3612 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3614 ret
= client_handle_message_handshake(client
, state
);
3617 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3618 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3620 ret
= client_handle_message_subscription(client
,
3621 client
->communication
.inbound
.msg_type
, state
);
3631 /* Incoming data from client. */
3632 int handle_notification_thread_client_in(
3633 struct notification_thread_state
*state
, int socket
)
3636 struct notification_client
*client
;
3641 client
= get_client_from_socket(socket
, state
);
3643 /* Internal error, abort. */
3648 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3649 client
->communication
.inbound
.bytes_to_receive
;
3650 if (client
->communication
.inbound
.expect_creds
) {
3651 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3652 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3653 client
->communication
.inbound
.bytes_to_receive
,
3654 &client
->communication
.inbound
.creds
);
3656 client
->communication
.inbound
.expect_creds
= false;
3657 client
->communication
.inbound
.creds_received
= true;
3660 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3661 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3662 client
->communication
.inbound
.bytes_to_receive
);
3664 if (recv_ret
>= 0) {
3665 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3667 goto error_disconnect_client
;
3670 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3671 /* Message incomplete wait for more data. */
3676 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3679 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3680 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3682 client
->communication
.inbound
.fds_to_receive
,
3683 &client
->communication
.inbound
.payload
);
3686 * Fds received. non blocking fds passing is all
3689 ssize_t expected_size
;
3691 expected_size
= sizeof(int) *
3692 client
->communication
.inbound
3694 assert(ret
== expected_size
);
3695 client
->communication
.inbound
.fds_to_receive
= 0;
3696 } else if (ret
== 0) {
3697 /* Received nothing. */
3701 goto error_disconnect_client
;
3705 /* At this point the message is complete.*/
3706 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3707 client
->communication
.inbound
.fds_to_receive
== 0);
3708 ret
= client_dispatch_message(client
, state
);
3711 * Only returns an error if this client must be
3714 goto error_disconnect_client
;
3721 error_disconnect_client
:
3722 ret
= notification_thread_client_disconnect(client
, state
);
3726 /* Client ready to receive outgoing data. */
3727 int handle_notification_thread_client_out(
3728 struct notification_thread_state
*state
, int socket
)
3731 struct notification_client
*client
;
3732 enum client_transmission_status transmission_status
;
3735 client
= get_client_from_socket(socket
, state
);
3737 /* Internal error, abort. */
3742 pthread_mutex_lock(&client
->lock
);
3743 transmission_status
= client_flush_outgoing_queue(client
);
3744 pthread_mutex_unlock(&client
->lock
);
3746 ret
= client_handle_transmission_status(
3747 client
, transmission_status
, state
);
3757 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3758 const struct channel_state_sample
*sample
,
3759 uint64_t buffer_capacity
)
3761 bool result
= false;
3763 enum lttng_condition_type condition_type
;
3764 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3765 condition
, struct lttng_condition_buffer_usage
,
3768 if (use_condition
->threshold_bytes
.set
) {
3769 threshold
= use_condition
->threshold_bytes
.value
;
3772 * Threshold was expressed as a ratio.
3774 * TODO the threshold (in bytes) of conditions expressed
3775 * as a ratio of total buffer size could be cached to
3776 * forego this double-multiplication or it could be performed
3777 * as fixed-point math.
3779 * Note that caching should accommodates the case where the
3780 * condition applies to multiple channels (i.e. don't assume
3781 * that all channels matching my_chann* have the same size...)
3783 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3784 (double) buffer_capacity
);
3787 condition_type
= lttng_condition_get_type(condition
);
3788 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3789 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3790 threshold
, sample
->highest_usage
);
3793 * The low condition should only be triggered once _all_ of the
3794 * streams in a channel have gone below the "low" threshold.
3796 if (sample
->highest_usage
<= threshold
) {
3800 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3801 threshold
, sample
->highest_usage
);
3804 * For high buffer usage scenarios, we want to trigger whenever
3805 * _any_ of the streams has reached the "high" threshold.
3807 if (sample
->highest_usage
>= threshold
) {
3816 bool evaluate_session_consumed_size_condition(
3817 const struct lttng_condition
*condition
,
3818 uint64_t session_consumed_size
)
3821 const struct lttng_condition_session_consumed_size
*size_condition
=
3822 container_of(condition
,
3823 struct lttng_condition_session_consumed_size
,
3826 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3827 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3828 threshold
, session_consumed_size
);
3829 return session_consumed_size
>= threshold
;
3833 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3834 struct lttng_evaluation
**evaluation
,
3835 const struct notification_thread_state
*state
,
3836 const struct channel_state_sample
*previous_sample
,
3837 const struct channel_state_sample
*latest_sample
,
3838 uint64_t previous_session_consumed_total
,
3839 uint64_t latest_session_consumed_total
,
3840 struct channel_info
*channel_info
)
3843 enum lttng_condition_type condition_type
;
3844 const bool previous_sample_available
= !!previous_sample
;
3845 bool previous_sample_result
= false;
3846 bool latest_sample_result
;
3848 condition_type
= lttng_condition_get_type(condition
);
3850 switch (condition_type
) {
3851 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3852 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3853 if (caa_likely(previous_sample_available
)) {
3854 previous_sample_result
=
3855 evaluate_buffer_usage_condition(condition
,
3856 previous_sample
, channel_info
->capacity
);
3858 latest_sample_result
= evaluate_buffer_usage_condition(
3859 condition
, latest_sample
,
3860 channel_info
->capacity
);
3862 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3863 if (caa_likely(previous_sample_available
)) {
3864 previous_sample_result
=
3865 evaluate_session_consumed_size_condition(
3867 previous_session_consumed_total
);
3869 latest_sample_result
=
3870 evaluate_session_consumed_size_condition(
3872 latest_session_consumed_total
);
3875 /* Unknown condition type; internal error. */
3879 if (!latest_sample_result
||
3880 (previous_sample_result
== latest_sample_result
)) {
3882 * Only trigger on a condition evaluation transition.
3884 * NOTE: This edge-triggered logic may not be appropriate for
3885 * future condition types.
3890 if (!evaluation
|| !latest_sample_result
) {
3894 switch (condition_type
) {
3895 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3896 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3897 *evaluation
= lttng_evaluation_buffer_usage_create(
3899 latest_sample
->highest_usage
,
3900 channel_info
->capacity
);
3902 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3903 *evaluation
= lttng_evaluation_session_consumed_size_create(
3904 latest_session_consumed_total
);
3919 int client_notification_overflow(struct notification_client
*client
)
3922 const struct lttng_notification_channel_message msg
= {
3923 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3926 ASSERT_LOCKED(client
->lock
);
3928 DBG("Dropping notification addressed to client (socket fd = %i)",
3930 if (client
->communication
.outbound
.dropped_notification
) {
3932 * The client already has a "notification dropped" message
3933 * in its outgoing queue. Nothing to do since all
3934 * of those messages are coalesced.
3939 client
->communication
.outbound
.dropped_notification
= true;
3940 ret
= lttng_dynamic_buffer_append(
3941 &client
->communication
.outbound
.payload
.buffer
, &msg
,
3944 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3951 static int client_handle_transmission_status_wrapper(
3952 struct notification_client
*client
,
3953 enum client_transmission_status status
,
3956 return client_handle_transmission_status(client
, status
,
3957 (struct notification_thread_state
*) user_data
);
3961 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3962 const struct lttng_evaluation
*evaluation
,
3963 struct notification_client_list
* client_list
,
3964 struct notification_thread_state
*state
,
3965 uid_t object_uid
, gid_t object_gid
)
3967 const struct lttng_credentials creds
= {
3968 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
3969 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
3972 return notification_client_list_send_evaluation(client_list
,
3973 lttng_trigger_get_const_condition(trigger
), evaluation
,
3974 lttng_trigger_get_credentials(trigger
),
3976 client_handle_transmission_status_wrapper
, state
);
3980 * Permission checks relative to notification channel clients are performed
3981 * here. Notice how object, client, and trigger credentials are involved in
3984 * The `object` credentials are the credentials associated with the "subject"
3985 * of a condition. For instance, a `rotation completed` condition applies
3986 * to a session. When that condition is met, it will produce an evaluation
3987 * against a session. Hence, in this case, the `object` credentials are the
3988 * credentials of the "subject" session.
3990 * The `trigger` credentials are the credentials of the user that registered the
3993 * The `client` credentials are the credentials of the user that created a given
3994 * notification channel.
3996 * In terms of visibility, it is expected that non-privilieged users can only
3997 * register triggers against "their" objects (their own sessions and
3998 * applications they are allowed to interact with). They can then open a
3999 * notification channel and subscribe to notifications associated with those
4002 * As for privilieged users, they can register triggers against the objects of
4003 * other users. They can then subscribe to the notifications associated to their
4004 * triggers. Privilieged users _can't_ subscribe to the notifications of
4005 * triggers owned by other users; they must create their own triggers.
4007 * This is more a concern of usability than security. It would be difficult for
4008 * a root user reliably subscribe to a specific set of conditions without
4009 * interference from external users (those could, for instance, unregister
4013 int notification_client_list_send_evaluation(
4014 struct notification_client_list
*client_list
,
4015 const struct lttng_condition
*condition
,
4016 const struct lttng_evaluation
*evaluation
,
4017 const struct lttng_credentials
*trigger_creds
,
4018 const struct lttng_credentials
*source_object_creds
,
4019 report_client_transmission_result_cb client_report
,
4023 struct lttng_payload msg_payload
;
4024 struct notification_client_list_element
*client_list_element
, *tmp
;
4025 const struct lttng_notification notification
= {
4026 .condition
= (struct lttng_condition
*) condition
,
4027 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4029 struct lttng_notification_channel_message msg_header
= {
4030 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4033 lttng_payload_init(&msg_payload
);
4035 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4036 sizeof(msg_header
));
4041 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4043 ERR("[notification-thread] Failed to serialize notification");
4048 /* Update payload size. */
4049 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4050 ->size
= (uint32_t)(
4051 msg_payload
.buffer
.size
- sizeof(msg_header
));
4053 /* Update the payload number of fds. */
4055 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4056 &msg_payload
, 0, -1);
4058 ((struct lttng_notification_channel_message
*)
4059 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4060 lttng_payload_view_get_fd_handle_count(&pv
);
4063 pthread_mutex_lock(&client_list
->lock
);
4064 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4065 &client_list
->list
, node
) {
4066 enum client_transmission_status transmission_status
;
4067 struct notification_client
*client
=
4068 client_list_element
->client
;
4071 pthread_mutex_lock(&client
->lock
);
4072 if (!client
->communication
.active
) {
4074 * Skip inactive client (protocol error or
4077 DBG("Skipping client at it is marked as inactive");
4081 if (source_object_creds
) {
4082 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4083 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4086 * Client is not allowed to monitor this
4089 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4094 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
4095 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4099 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4100 client
->socket
, msg_payload
.buffer
.size
);
4102 if (client_has_outbound_data_left(client
)) {
4104 * Outgoing data is already buffered for this client;
4105 * drop the notification and enqueue a "dropped
4106 * notification" message if this is the first dropped
4107 * notification since the socket spilled-over to the
4110 ret
= client_notification_overflow(client
);
4117 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4123 transmission_status
= client_flush_outgoing_queue(client
);
4124 pthread_mutex_unlock(&client
->lock
);
4125 ret
= client_report(client
, transmission_status
, user_data
);
4128 goto end_unlock_list
;
4134 pthread_mutex_unlock(&client
->lock
);
4137 goto end_unlock_list
;
4143 pthread_mutex_unlock(&client_list
->lock
);
4145 lttng_payload_reset(&msg_payload
);
4149 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4150 int notification_pipe_read_fd
,
4151 enum lttng_domain_type domain
)
4154 struct lttng_ust_event_notifier_notification ust_notification
;
4155 struct lttng_kernel_event_notifier_notification kernel_notification
;
4156 struct cds_lfht_node
*node
;
4157 struct cds_lfht_iter iter
;
4158 struct notification_trigger_tokens_ht_element
*element
;
4159 enum lttng_action_type action_type
;
4160 const struct lttng_action
*action
;
4161 struct lttng_event_notifier_notification notification
;
4162 void *reception_buffer
;
4163 size_t reception_size
;
4165 notification
.type
= domain
;
4168 case LTTNG_DOMAIN_UST
:
4169 reception_buffer
= (void *) &ust_notification
;
4170 reception_size
= sizeof(ust_notification
);
4171 notification
.notification
.ust
= &ust_notification
;
4173 case LTTNG_DOMAIN_KERNEL
:
4174 reception_buffer
= (void *) &kernel_notification
;
4175 reception_size
= sizeof(kernel_notification
);
4176 notification
.notification
.kernel
= &kernel_notification
;
4183 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4184 * ensuring that read/write of tracer notifications are atomic.
4186 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4188 if (ret
!= reception_size
) {
4189 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4190 notification_pipe_read_fd
, reception_size
, ret
);
4196 case LTTNG_DOMAIN_UST
:
4197 notification
.token
= ust_notification
.token
;
4199 case LTTNG_DOMAIN_KERNEL
:
4200 notification
.token
= kernel_notification
.token
;
4206 /* Find triggers associated with this token. */
4208 cds_lfht_lookup(state
->trigger_tokens_ht
,
4209 hash_key_u64(¬ification
.token
, lttng_ht_seed
),
4210 match_trigger_token
, ¬ification
.token
, &iter
);
4211 node
= cds_lfht_iter_get_node(&iter
);
4212 if (caa_likely(!node
)) {
4214 * This is not an error, slow consumption of the pipe can lead
4215 * to situations where a trigger is removed but we still get
4216 * tracer notification matching to a previous trigger.
4222 element
= caa_container_of(node
,
4223 struct notification_trigger_tokens_ht_element
,
4226 action
= lttng_trigger_get_const_action(element
->trigger
);
4227 action_type
= lttng_action_get_type(action
);
4228 DBG("Received message from tracer event source: event source fd = %d, token = %" PRIu64
", action type = '%s'",
4229 notification_pipe_read_fd
, notification
.token
,
4230 lttng_action_type_string(action_type
));
4232 /* TODO: Perform actions */
4242 int handle_notification_thread_channel_sample(
4243 struct notification_thread_state
*state
, int pipe
,
4244 enum lttng_domain_type domain
)
4247 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4248 struct channel_info
*channel_info
;
4249 struct cds_lfht_node
*node
;
4250 struct cds_lfht_iter iter
;
4251 struct lttng_channel_trigger_list
*trigger_list
;
4252 struct lttng_trigger_list_element
*trigger_list_element
;
4253 bool previous_sample_available
= false;
4254 struct channel_state_sample previous_sample
, latest_sample
;
4255 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4256 struct lttng_credentials channel_creds
;
4259 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4260 * ensuring that read/write of sampling messages are atomic.
4262 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4263 if (ret
!= sizeof(sample_msg
)) {
4264 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4271 latest_sample
.key
.key
= sample_msg
.key
;
4272 latest_sample
.key
.domain
= domain
;
4273 latest_sample
.highest_usage
= sample_msg
.highest
;
4274 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4275 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4279 /* Retrieve the channel's informations */
4280 cds_lfht_lookup(state
->channels_ht
,
4281 hash_channel_key(&latest_sample
.key
),
4285 node
= cds_lfht_iter_get_node(&iter
);
4286 if (caa_unlikely(!node
)) {
4288 * Not an error since the consumer can push a sample to the pipe
4289 * and the rest of the session daemon could notify us of the
4290 * channel's destruction before we get a chance to process that
4293 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4294 latest_sample
.key
.key
,
4295 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
4299 channel_info
= caa_container_of(node
, struct channel_info
,
4301 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4303 latest_sample
.key
.key
,
4304 channel_info
->session_info
->name
,
4305 latest_sample
.highest_usage
,
4306 latest_sample
.lowest_usage
,
4307 latest_sample
.channel_total_consumed
);
4309 previous_session_consumed_total
=
4310 channel_info
->session_info
->consumed_data_size
;
4312 /* Retrieve the channel's last sample, if it exists, and update it. */
4313 cds_lfht_lookup(state
->channel_state_ht
,
4314 hash_channel_key(&latest_sample
.key
),
4315 match_channel_state_sample
,
4318 node
= cds_lfht_iter_get_node(&iter
);
4319 if (caa_likely(node
)) {
4320 struct channel_state_sample
*stored_sample
;
4322 /* Update the sample stored. */
4323 stored_sample
= caa_container_of(node
,
4324 struct channel_state_sample
,
4325 channel_state_ht_node
);
4327 memcpy(&previous_sample
, stored_sample
,
4328 sizeof(previous_sample
));
4329 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4330 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4331 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4332 previous_sample_available
= true;
4334 latest_session_consumed_total
=
4335 previous_session_consumed_total
+
4336 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4339 * This is the channel's first sample, allocate space for and
4340 * store the new sample.
4342 struct channel_state_sample
*stored_sample
;
4344 stored_sample
= zmalloc(sizeof(*stored_sample
));
4345 if (!stored_sample
) {
4350 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4351 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4352 cds_lfht_add(state
->channel_state_ht
,
4353 hash_channel_key(&stored_sample
->key
),
4354 &stored_sample
->channel_state_ht_node
);
4356 latest_session_consumed_total
=
4357 previous_session_consumed_total
+
4358 latest_sample
.channel_total_consumed
;
4361 channel_info
->session_info
->consumed_data_size
=
4362 latest_session_consumed_total
;
4364 /* Find triggers associated with this channel. */
4365 cds_lfht_lookup(state
->channel_triggers_ht
,
4366 hash_channel_key(&latest_sample
.key
),
4367 match_channel_trigger_list
,
4370 node
= cds_lfht_iter_get_node(&iter
);
4371 if (caa_likely(!node
)) {
4375 channel_creds
= (typeof(channel_creds
)) {
4376 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4377 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4380 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4381 channel_triggers_ht_node
);
4382 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4384 const struct lttng_condition
*condition
;
4385 struct lttng_trigger
*trigger
;
4386 struct notification_client_list
*client_list
= NULL
;
4387 struct lttng_evaluation
*evaluation
= NULL
;
4388 enum action_executor_status executor_status
;
4391 trigger
= trigger_list_element
->trigger
;
4392 condition
= lttng_trigger_get_const_condition(trigger
);
4396 * Check if any client is subscribed to the result of this
4399 client_list
= get_client_list_from_condition(state
, condition
);
4401 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4402 previous_sample_available
? &previous_sample
: NULL
,
4404 previous_session_consumed_total
,
4405 latest_session_consumed_total
,
4407 if (caa_unlikely(ret
)) {
4411 if (caa_likely(!evaluation
)) {
4415 if (!lttng_trigger_should_fire(trigger
)) {
4419 lttng_trigger_fire(trigger
);
4422 * Ownership of `evaluation` transferred to the action executor
4423 * no matter the result.
4425 executor_status
= action_executor_enqueue(state
->executor
,
4426 trigger
, evaluation
, &channel_creds
,
4429 switch (executor_status
) {
4430 case ACTION_EXECUTOR_STATUS_OK
:
4432 case ACTION_EXECUTOR_STATUS_ERROR
:
4433 case ACTION_EXECUTOR_STATUS_INVALID
:
4435 * TODO Add trigger identification (name/id) when
4436 * it is added to the API.
4438 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4441 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4443 * TODO Add trigger identification (name/id) when
4444 * it is added to the API.
4446 * Not a fatal error.
4448 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4456 notification_client_list_put(client_list
);
4457 if (caa_unlikely(ret
)) {