2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include <urcu/rculfhash.h>
12 #include <common/defaults.h>
13 #include <common/error.h>
14 #include <common/futex.h>
15 #include <common/unix.h>
16 #include <common/dynamic-buffer.h>
17 #include <common/hashtable/utils.h>
18 #include <common/sessiond-comm/sessiond-comm.h>
19 #include <common/macros.h>
20 #include <lttng/condition/condition.h>
21 #include <lttng/action/action-internal.h>
22 #include <lttng/action/group-internal.h>
23 #include <lttng/domain-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/notification/channel-internal.h>
31 #include <lttng/trigger/trigger-internal.h>
39 #include "notification-thread.h"
40 #include "notification-thread-events.h"
41 #include "notification-thread-commands.h"
42 #include "lttng-sessiond.h"
45 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
46 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
48 enum lttng_object_type
{
49 LTTNG_OBJECT_TYPE_UNKNOWN
,
50 LTTNG_OBJECT_TYPE_NONE
,
51 LTTNG_OBJECT_TYPE_CHANNEL
,
52 LTTNG_OBJECT_TYPE_SESSION
,
55 struct lttng_trigger_list_element
{
56 /* No ownership of the trigger object is assumed. */
57 struct lttng_trigger
*trigger
;
58 struct cds_list_head node
;
61 struct lttng_channel_trigger_list
{
62 struct channel_key channel_key
;
63 /* List of struct lttng_trigger_list_element. */
64 struct cds_list_head list
;
65 /* Node in the channel_triggers_ht */
66 struct cds_lfht_node channel_triggers_ht_node
;
67 /* call_rcu delayed reclaim. */
68 struct rcu_head rcu_node
;
72 * List of triggers applying to a given session.
75 * - lttng_session_trigger_list_create()
76 * - lttng_session_trigger_list_build()
77 * - lttng_session_trigger_list_destroy()
78 * - lttng_session_trigger_list_add()
80 struct lttng_session_trigger_list
{
82 * Not owned by this; points to the session_info structure's
85 const char *session_name
;
86 /* List of struct lttng_trigger_list_element. */
87 struct cds_list_head list
;
88 /* Node in the session_triggers_ht */
89 struct cds_lfht_node session_triggers_ht_node
;
91 * Weak reference to the notification system's session triggers
94 * The session trigger list structure structure is owned by
95 * the session's session_info.
97 * The session_info is kept alive the the channel_infos holding a
98 * reference to it (reference counting). When those channels are
99 * destroyed (at runtime or on teardown), the reference they hold
100 * to the session_info are released. On destruction of session_info,
101 * session_info_destroy() will remove the list of triggers applying
102 * to this session from the notification system's state.
104 * This implies that the session_triggers_ht must be destroyed
105 * after the channels.
107 struct cds_lfht
*session_triggers_ht
;
108 /* Used for delayed RCU reclaim. */
109 struct rcu_head rcu_node
;
112 struct lttng_trigger_ht_element
{
113 struct lttng_trigger
*trigger
;
114 struct cds_lfht_node node
;
115 struct cds_lfht_node node_by_name
;
116 /* call_rcu delayed reclaim. */
117 struct rcu_head rcu_node
;
120 struct lttng_condition_list_element
{
121 struct lttng_condition
*condition
;
122 struct cds_list_head node
;
125 struct channel_state_sample
{
126 struct channel_key key
;
127 struct cds_lfht_node channel_state_ht_node
;
128 uint64_t highest_usage
;
129 uint64_t lowest_usage
;
130 uint64_t channel_total_consumed
;
131 /* call_rcu delayed reclaim. */
132 struct rcu_head rcu_node
;
135 static unsigned long hash_channel_key(struct channel_key
*key
);
136 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
137 struct lttng_evaluation
**evaluation
,
138 const struct notification_thread_state
*state
,
139 const struct channel_state_sample
*previous_sample
,
140 const struct channel_state_sample
*latest_sample
,
141 uint64_t previous_session_consumed_total
,
142 uint64_t latest_session_consumed_total
,
143 struct channel_info
*channel_info
);
145 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
146 const struct lttng_evaluation
*evaluation
,
147 struct notification_client_list
*client_list
,
148 struct notification_thread_state
*state
,
149 uid_t channel_uid
, gid_t channel_gid
);
152 /* session_info API */
154 void session_info_destroy(void *_data
);
156 void session_info_get(struct session_info
*session_info
);
158 void session_info_put(struct session_info
*session_info
);
160 struct session_info
*session_info_create(const char *name
,
161 uid_t uid
, gid_t gid
,
162 struct lttng_session_trigger_list
*trigger_list
,
163 struct cds_lfht
*sessions_ht
);
165 void session_info_add_channel(struct session_info
*session_info
,
166 struct channel_info
*channel_info
);
168 void session_info_remove_channel(struct session_info
*session_info
,
169 struct channel_info
*channel_info
);
171 /* lttng_session_trigger_list API */
173 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
174 const char *session_name
,
175 struct cds_lfht
*session_triggers_ht
);
177 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
178 const struct notification_thread_state
*state
,
179 const char *session_name
);
181 void lttng_session_trigger_list_destroy(
182 struct lttng_session_trigger_list
*list
);
184 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
185 struct lttng_trigger
*trigger
);
188 int client_handle_transmission_status(
189 struct notification_client
*client
,
190 enum client_transmission_status transmission_status
,
191 struct notification_thread_state
*state
);
194 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
196 /* This double-cast is intended to supress pointer-to-cast warning. */
197 const int socket
= (int) (intptr_t) key
;
198 const struct notification_client
*client
= caa_container_of(node
,
199 struct notification_client
, client_socket_ht_node
);
201 return client
->socket
== socket
;
205 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
207 /* This double-cast is intended to supress pointer-to-cast warning. */
208 const notification_client_id id
= *((notification_client_id
*) key
);
209 const struct notification_client
*client
= caa_container_of(
210 node
, struct notification_client
, client_id_ht_node
);
212 return client
->id
== id
;
216 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
218 struct channel_key
*channel_key
= (struct channel_key
*) key
;
219 struct lttng_channel_trigger_list
*trigger_list
;
221 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
222 channel_triggers_ht_node
);
224 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
225 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
229 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
231 const char *session_name
= (const char *) key
;
232 struct lttng_session_trigger_list
*trigger_list
;
234 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
235 session_triggers_ht_node
);
237 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
241 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
243 struct channel_key
*channel_key
= (struct channel_key
*) key
;
244 struct channel_state_sample
*sample
;
246 sample
= caa_container_of(node
, struct channel_state_sample
,
247 channel_state_ht_node
);
249 return !!((channel_key
->key
== sample
->key
.key
) &&
250 (channel_key
->domain
== sample
->key
.domain
));
254 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
256 struct channel_key
*channel_key
= (struct channel_key
*) key
;
257 struct channel_info
*channel_info
;
259 channel_info
= caa_container_of(node
, struct channel_info
,
262 return !!((channel_key
->key
== channel_info
->key
.key
) &&
263 (channel_key
->domain
== channel_info
->key
.domain
));
267 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
270 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
271 struct lttng_trigger_ht_element
*trigger_ht_element
;
272 const struct lttng_credentials
*creds_key
;
273 const struct lttng_credentials
*creds_node
;
275 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
278 match
= lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
283 /* Validate credential */
284 /* TODO: this could be moved to lttng_trigger_equal depending on how we
285 * handle root behaviour on disable and listing.
287 creds_key
= lttng_trigger_get_credentials(trigger_key
);
288 creds_node
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
289 match
= lttng_credentials_is_equal(creds_key
, creds_node
);
295 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
297 const uint64_t *_key
= key
;
298 struct notification_trigger_tokens_ht_element
*element
;
300 element
= caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
302 return *_key
== element
->token
;
306 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
308 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
309 struct notification_client_list
*client_list
;
310 const struct lttng_condition
*condition
;
312 assert(condition_key
);
314 client_list
= caa_container_of(node
, struct notification_client_list
,
315 notification_trigger_clients_ht_node
);
316 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
318 return !!lttng_condition_is_equal(condition_key
, condition
);
322 int match_session(struct cds_lfht_node
*node
, const void *key
)
324 const char *name
= key
;
325 struct session_info
*session_info
= caa_container_of(
326 node
, struct session_info
, sessions_ht_node
);
328 return !strcmp(session_info
->name
, name
);
332 * Match function for string node.
334 static int match_str(struct cds_lfht_node
*node
, const void *key
)
336 struct lttng_trigger_ht_element
*trigger_ht_element
;
339 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
342 /* TODO error checking */
343 lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
345 return hash_match_key_str(name
, (void *) key
);
349 unsigned long lttng_condition_buffer_usage_hash(
350 const struct lttng_condition
*_condition
)
353 unsigned long condition_type
;
354 struct lttng_condition_buffer_usage
*condition
;
356 condition
= container_of(_condition
,
357 struct lttng_condition_buffer_usage
, parent
);
359 condition_type
= (unsigned long) condition
->parent
.type
;
360 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
361 if (condition
->session_name
) {
362 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
364 if (condition
->channel_name
) {
365 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
367 if (condition
->domain
.set
) {
368 hash
^= hash_key_ulong(
369 (void *) condition
->domain
.type
,
372 if (condition
->threshold_ratio
.set
) {
375 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
376 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
377 } else if (condition
->threshold_bytes
.set
) {
380 val
= condition
->threshold_bytes
.value
;
381 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
387 unsigned long lttng_condition_session_consumed_size_hash(
388 const struct lttng_condition
*_condition
)
391 unsigned long condition_type
=
392 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
393 struct lttng_condition_session_consumed_size
*condition
;
396 condition
= container_of(_condition
,
397 struct lttng_condition_session_consumed_size
, parent
);
399 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
400 if (condition
->session_name
) {
401 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
403 val
= condition
->consumed_threshold_bytes
.value
;
404 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
409 unsigned long lttng_condition_session_rotation_hash(
410 const struct lttng_condition
*_condition
)
412 unsigned long hash
, condition_type
;
413 struct lttng_condition_session_rotation
*condition
;
415 condition
= container_of(_condition
,
416 struct lttng_condition_session_rotation
, parent
);
417 condition_type
= (unsigned long) condition
->parent
.type
;
418 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
419 assert(condition
->session_name
);
420 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
425 unsigned long lttng_condition_event_rule_hash(
426 const struct lttng_condition
*_condition
)
428 unsigned long hash
, condition_type
;
429 struct lttng_condition_event_rule
*condition
;
431 condition
= container_of(_condition
,
432 struct lttng_condition_event_rule
, parent
);
433 condition_type
= (unsigned long) condition
->parent
.type
;
434 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
436 /* TODO: further hasg using the event rule? on pattern maybe?*/
441 * The lttng_condition hashing code is kept in this file (rather than
442 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
443 * don't want to link in liblttng-ctl.
446 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
448 switch (condition
->type
) {
449 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
450 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
451 return lttng_condition_buffer_usage_hash(condition
);
452 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
453 return lttng_condition_session_consumed_size_hash(condition
);
454 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
455 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
456 return lttng_condition_session_rotation_hash(condition
);
457 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
458 return lttng_condition_event_rule_hash(condition
);
460 ERR("[notification-thread] Unexpected condition type caught");
466 unsigned long hash_channel_key(struct channel_key
*key
)
468 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
469 unsigned long domain_hash
= hash_key_ulong(
470 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
472 return key_hash
^ domain_hash
;
476 unsigned long hash_client_socket(int socket
)
478 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
482 unsigned long hash_client_id(notification_client_id id
)
484 return hash_key_u64(&id
, lttng_ht_seed
);
488 * Get the type of object to which a given condition applies. Bindings let
489 * the notification system evaluate a trigger's condition when a given
490 * object's state is updated.
492 * For instance, a condition bound to a channel will be evaluated everytime
493 * the channel's state is changed by a channel monitoring sample.
496 enum lttng_object_type
get_condition_binding_object(
497 const struct lttng_condition
*condition
)
499 switch (lttng_condition_get_type(condition
)) {
500 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
501 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
502 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
503 return LTTNG_OBJECT_TYPE_CHANNEL
;
504 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
505 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
506 return LTTNG_OBJECT_TYPE_SESSION
;
507 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
508 return LTTNG_OBJECT_TYPE_NONE
;
510 return LTTNG_OBJECT_TYPE_UNKNOWN
;
515 void free_channel_info_rcu(struct rcu_head
*node
)
517 free(caa_container_of(node
, struct channel_info
, rcu_node
));
521 void channel_info_destroy(struct channel_info
*channel_info
)
527 if (channel_info
->session_info
) {
528 session_info_remove_channel(channel_info
->session_info
,
530 session_info_put(channel_info
->session_info
);
532 if (channel_info
->name
) {
533 free(channel_info
->name
);
535 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
539 void free_session_info_rcu(struct rcu_head
*node
)
541 free(caa_container_of(node
, struct session_info
, rcu_node
));
544 /* Don't call directly, use the ref-counting mechanism. */
546 void session_info_destroy(void *_data
)
548 struct session_info
*session_info
= _data
;
551 assert(session_info
);
552 if (session_info
->channel_infos_ht
) {
553 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
555 ERR("[notification-thread] Failed to destroy channel information hash table");
558 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
561 cds_lfht_del(session_info
->sessions_ht
,
562 &session_info
->sessions_ht_node
);
564 free(session_info
->name
);
565 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
569 void session_info_get(struct session_info
*session_info
)
574 lttng_ref_get(&session_info
->ref
);
578 void session_info_put(struct session_info
*session_info
)
583 lttng_ref_put(&session_info
->ref
);
587 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
588 struct lttng_session_trigger_list
*trigger_list
,
589 struct cds_lfht
*sessions_ht
)
591 struct session_info
*session_info
;
595 session_info
= malloc(sizeof(*session_info
));
599 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
601 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
602 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
603 if (!session_info
->channel_infos_ht
) {
607 cds_lfht_node_init(&session_info
->sessions_ht_node
);
608 session_info
->name
= strdup(name
);
609 if (!session_info
->name
) {
612 session_info
->uid
= uid
;
613 session_info
->gid
= gid
;
614 session_info
->trigger_list
= trigger_list
;
615 session_info
->sessions_ht
= sessions_ht
;
619 session_info_put(session_info
);
624 void session_info_add_channel(struct session_info
*session_info
,
625 struct channel_info
*channel_info
)
628 cds_lfht_add(session_info
->channel_infos_ht
,
629 hash_channel_key(&channel_info
->key
),
630 &channel_info
->session_info_channels_ht_node
);
635 void session_info_remove_channel(struct session_info
*session_info
,
636 struct channel_info
*channel_info
)
639 cds_lfht_del(session_info
->channel_infos_ht
,
640 &channel_info
->session_info_channels_ht_node
);
645 struct channel_info
*channel_info_create(const char *channel_name
,
646 struct channel_key
*channel_key
, uint64_t channel_capacity
,
647 struct session_info
*session_info
)
649 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
655 cds_lfht_node_init(&channel_info
->channels_ht_node
);
656 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
657 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
658 channel_info
->capacity
= channel_capacity
;
660 channel_info
->name
= strdup(channel_name
);
661 if (!channel_info
->name
) {
666 * Set the references between session and channel infos:
667 * - channel_info holds a strong reference to session_info
668 * - session_info holds a weak reference to channel_info
670 session_info_get(session_info
);
671 session_info_add_channel(session_info
, channel_info
);
672 channel_info
->session_info
= session_info
;
676 channel_info_destroy(channel_info
);
681 bool notification_client_list_get(struct notification_client_list
*list
)
683 return urcu_ref_get_unless_zero(&list
->ref
);
687 void free_notification_client_list_rcu(struct rcu_head
*node
)
689 free(caa_container_of(node
, struct notification_client_list
,
694 void notification_client_list_release(struct urcu_ref
*list_ref
)
696 struct notification_client_list
*list
=
697 container_of(list_ref
, typeof(*list
), ref
);
698 struct notification_client_list_element
*client_list_element
, *tmp
;
700 if (list
->notification_trigger_clients_ht
) {
702 cds_lfht_del(list
->notification_trigger_clients_ht
,
703 &list
->notification_trigger_clients_ht_node
);
705 list
->notification_trigger_clients_ht
= NULL
;
707 cds_list_for_each_entry_safe(client_list_element
, tmp
,
709 free(client_list_element
);
711 pthread_mutex_destroy(&list
->lock
);
712 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
716 struct notification_client_list
*notification_client_list_create(
717 const struct lttng_trigger
*trigger
)
719 struct notification_client_list
*client_list
=
720 zmalloc(sizeof(*client_list
));
725 pthread_mutex_init(&client_list
->lock
, NULL
);
726 urcu_ref_init(&client_list
->ref
);
727 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
728 CDS_INIT_LIST_HEAD(&client_list
->list
);
729 client_list
->trigger
= trigger
;
735 void publish_notification_client_list(
736 struct notification_thread_state
*state
,
737 struct notification_client_list
*list
)
739 const struct lttng_condition
*condition
=
740 lttng_trigger_get_const_condition(list
->trigger
);
742 assert(!list
->notification_trigger_clients_ht
);
744 list
->notification_trigger_clients_ht
=
745 state
->notification_trigger_clients_ht
;
748 cds_lfht_add(state
->notification_trigger_clients_ht
,
749 lttng_condition_hash(condition
),
750 &list
->notification_trigger_clients_ht_node
);
755 void notification_client_list_put(struct notification_client_list
*list
)
760 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
763 /* Provides a reference to the returned list. */
765 struct notification_client_list
*get_client_list_from_condition(
766 struct notification_thread_state
*state
,
767 const struct lttng_condition
*condition
)
769 struct cds_lfht_node
*node
;
770 struct cds_lfht_iter iter
;
771 struct notification_client_list
*list
= NULL
;
774 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
775 lttng_condition_hash(condition
),
776 match_client_list_condition
,
779 node
= cds_lfht_iter_get_node(&iter
);
781 list
= container_of(node
, struct notification_client_list
,
782 notification_trigger_clients_ht_node
);
783 list
= notification_client_list_get(list
) ? list
: NULL
;
790 int evaluate_channel_condition_for_client(
791 const struct lttng_condition
*condition
,
792 struct notification_thread_state
*state
,
793 struct lttng_evaluation
**evaluation
,
794 uid_t
*session_uid
, gid_t
*session_gid
)
797 struct cds_lfht_iter iter
;
798 struct cds_lfht_node
*node
;
799 struct channel_info
*channel_info
= NULL
;
800 struct channel_key
*channel_key
= NULL
;
801 struct channel_state_sample
*last_sample
= NULL
;
802 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
806 /* Find the channel associated with the condition. */
807 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
808 channel_trigger_list
, channel_triggers_ht_node
) {
809 struct lttng_trigger_list_element
*element
;
811 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
812 const struct lttng_condition
*current_condition
=
813 lttng_trigger_get_const_condition(
816 assert(current_condition
);
817 if (!lttng_condition_is_equal(condition
,
818 current_condition
)) {
822 /* Found the trigger, save the channel key. */
823 channel_key
= &channel_trigger_list
->channel_key
;
827 /* The channel key was found stop iteration. */
833 /* No channel found; normal exit. */
834 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
839 /* Fetch channel info for the matching channel. */
840 cds_lfht_lookup(state
->channels_ht
,
841 hash_channel_key(channel_key
),
845 node
= cds_lfht_iter_get_node(&iter
);
847 channel_info
= caa_container_of(node
, struct channel_info
,
850 /* Retrieve the channel's last sample, if it exists. */
851 cds_lfht_lookup(state
->channel_state_ht
,
852 hash_channel_key(channel_key
),
853 match_channel_state_sample
,
856 node
= cds_lfht_iter_get_node(&iter
);
858 last_sample
= caa_container_of(node
,
859 struct channel_state_sample
,
860 channel_state_ht_node
);
862 /* Nothing to evaluate, no sample was ever taken. Normal exit */
863 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
868 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
870 0, channel_info
->session_info
->consumed_data_size
,
873 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
877 *session_uid
= channel_info
->session_info
->uid
;
878 *session_gid
= channel_info
->session_info
->gid
;
885 const char *get_condition_session_name(const struct lttng_condition
*condition
)
887 const char *session_name
= NULL
;
888 enum lttng_condition_status status
;
890 switch (lttng_condition_get_type(condition
)) {
891 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
892 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
893 status
= lttng_condition_buffer_usage_get_session_name(
894 condition
, &session_name
);
896 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
897 status
= lttng_condition_session_consumed_size_get_session_name(
898 condition
, &session_name
);
900 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
901 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
902 status
= lttng_condition_session_rotation_get_session_name(
903 condition
, &session_name
);
908 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
909 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
917 int evaluate_session_condition_for_client(
918 const struct lttng_condition
*condition
,
919 struct notification_thread_state
*state
,
920 struct lttng_evaluation
**evaluation
,
921 uid_t
*session_uid
, gid_t
*session_gid
)
924 struct cds_lfht_iter iter
;
925 struct cds_lfht_node
*node
;
926 const char *session_name
;
927 struct session_info
*session_info
= NULL
;
930 session_name
= get_condition_session_name(condition
);
932 /* Find the session associated with the trigger. */
933 cds_lfht_lookup(state
->sessions_ht
,
934 hash_key_str(session_name
, lttng_ht_seed
),
938 node
= cds_lfht_iter_get_node(&iter
);
940 DBG("[notification-thread] No known session matching name \"%s\"",
946 session_info
= caa_container_of(node
, struct session_info
,
948 session_info_get(session_info
);
951 * Evaluation is performed in-line here since only one type of
952 * session-bound condition is handled for the moment.
954 switch (lttng_condition_get_type(condition
)) {
955 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
956 if (!session_info
->rotation
.ongoing
) {
958 goto end_session_put
;
961 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
962 session_info
->rotation
.id
);
965 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
968 goto end_session_put
;
974 goto end_session_put
;
977 *session_uid
= session_info
->uid
;
978 *session_gid
= session_info
->gid
;
981 session_info_put(session_info
);
988 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
989 const struct lttng_condition
*condition
,
990 struct notification_client
*client
,
991 struct notification_thread_state
*state
)
994 struct lttng_evaluation
*evaluation
= NULL
;
995 struct notification_client_list client_list
= {
996 .lock
= PTHREAD_MUTEX_INITIALIZER
,
998 struct notification_client_list_element client_list_element
= { 0 };
999 uid_t object_uid
= 0;
1000 gid_t object_gid
= 0;
1007 switch (get_condition_binding_object(condition
)) {
1008 case LTTNG_OBJECT_TYPE_SESSION
:
1009 ret
= evaluate_session_condition_for_client(condition
, state
,
1010 &evaluation
, &object_uid
, &object_gid
);
1012 case LTTNG_OBJECT_TYPE_CHANNEL
:
1013 ret
= evaluate_channel_condition_for_client(condition
, state
,
1014 &evaluation
, &object_uid
, &object_gid
);
1016 case LTTNG_OBJECT_TYPE_NONE
:
1017 DBG("[notification-thread] Newly subscribed-to condition not binded to object, nothing to evaluate");
1020 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1030 /* Evaluation yielded nothing. Normal exit. */
1031 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
1037 * Create a temporary client list with the client currently
1040 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1041 CDS_INIT_LIST_HEAD(&client_list
.list
);
1042 client_list
.trigger
= trigger
;
1044 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1045 client_list_element
.client
= client
;
1046 cds_list_add(&client_list_element
.node
, &client_list
.list
);
1048 /* Send evaluation result to the newly-subscribed client. */
1049 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1050 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1051 state
, object_uid
, object_gid
);
1058 int notification_thread_client_subscribe(struct notification_client
*client
,
1059 struct lttng_condition
*condition
,
1060 struct notification_thread_state
*state
,
1061 enum lttng_notification_channel_status
*_status
)
1064 struct notification_client_list
*client_list
= NULL
;
1065 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1066 struct notification_client_list_element
*client_list_element
= NULL
;
1067 enum lttng_notification_channel_status status
=
1068 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1071 * Ensure that the client has not already subscribed to this condition
1074 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1075 if (lttng_condition_is_equal(condition_list_element
->condition
,
1077 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1082 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1083 if (!condition_list_element
) {
1087 client_list_element
= zmalloc(sizeof(*client_list_element
));
1088 if (!client_list_element
) {
1094 * Add the newly-subscribed condition to the client's subscription list.
1096 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1097 condition_list_element
->condition
= condition
;
1098 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1100 client_list
= get_client_list_from_condition(state
, condition
);
1103 * No notification-emiting trigger registered with this
1104 * condition. We don't evaluate the condition right away
1105 * since this trigger is not registered yet.
1107 free(client_list_element
);
1112 * The condition to which the client just subscribed is evaluated
1113 * at this point so that conditions that are already TRUE result
1114 * in a notification being sent out.
1116 * The client_list's trigger is used without locking the list itself.
1117 * This is correct since the list doesn't own the trigger and the
1118 * object is immutable.
1120 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1122 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1124 free(client_list_element
);
1129 * Add the client to the list of clients interested in a given trigger
1130 * if a "notification" trigger with a corresponding condition was
1133 client_list_element
->client
= client
;
1134 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1136 pthread_mutex_lock(&client_list
->lock
);
1137 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1138 pthread_mutex_unlock(&client_list
->lock
);
1144 notification_client_list_put(client_list
);
1148 free(condition_list_element
);
1149 free(client_list_element
);
1154 int notification_thread_client_unsubscribe(
1155 struct notification_client
*client
,
1156 struct lttng_condition
*condition
,
1157 struct notification_thread_state
*state
,
1158 enum lttng_notification_channel_status
*_status
)
1160 struct notification_client_list
*client_list
;
1161 struct lttng_condition_list_element
*condition_list_element
,
1163 struct notification_client_list_element
*client_list_element
,
1165 bool condition_found
= false;
1166 enum lttng_notification_channel_status status
=
1167 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1169 /* Remove the condition from the client's condition list. */
1170 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1171 &client
->condition_list
, node
) {
1172 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1177 cds_list_del(&condition_list_element
->node
);
1179 * The caller may be iterating on the client's conditions to
1180 * tear down a client's connection. In this case, the condition
1181 * will be destroyed at the end.
1183 if (condition
!= condition_list_element
->condition
) {
1184 lttng_condition_destroy(
1185 condition_list_element
->condition
);
1187 free(condition_list_element
);
1188 condition_found
= true;
1192 if (!condition_found
) {
1193 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1198 * Remove the client from the list of clients interested the trigger
1199 * matching the condition.
1201 client_list
= get_client_list_from_condition(state
, condition
);
1206 pthread_mutex_lock(&client_list
->lock
);
1207 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1208 &client_list
->list
, node
) {
1209 if (client_list_element
->client
->id
!= client
->id
) {
1212 cds_list_del(&client_list_element
->node
);
1213 free(client_list_element
);
1216 pthread_mutex_unlock(&client_list
->lock
);
1217 notification_client_list_put(client_list
);
1220 lttng_condition_destroy(condition
);
1228 void free_notification_client_rcu(struct rcu_head
*node
)
1230 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1234 void notification_client_destroy(struct notification_client
*client
,
1235 struct notification_thread_state
*state
)
1242 * The client object is not reachable by other threads, no need to lock
1245 if (client
->socket
>= 0) {
1246 (void) lttcomm_close_unix_sock(client
->socket
);
1247 client
->socket
= -1;
1249 client
->communication
.active
= false;
1250 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
1251 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
1252 pthread_mutex_destroy(&client
->lock
);
1253 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1257 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1261 struct notification_client
*get_client_from_socket(int socket
,
1262 struct notification_thread_state
*state
)
1264 struct cds_lfht_iter iter
;
1265 struct cds_lfht_node
*node
;
1266 struct notification_client
*client
= NULL
;
1268 cds_lfht_lookup(state
->client_socket_ht
,
1269 hash_client_socket(socket
),
1270 match_client_socket
,
1271 (void *) (unsigned long) socket
,
1273 node
= cds_lfht_iter_get_node(&iter
);
1278 client
= caa_container_of(node
, struct notification_client
,
1279 client_socket_ht_node
);
1285 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1289 struct notification_client
*get_client_from_id(notification_client_id id
,
1290 struct notification_thread_state
*state
)
1292 struct cds_lfht_iter iter
;
1293 struct cds_lfht_node
*node
;
1294 struct notification_client
*client
= NULL
;
1296 cds_lfht_lookup(state
->client_id_ht
,
1301 node
= cds_lfht_iter_get_node(&iter
);
1306 client
= caa_container_of(node
, struct notification_client
,
1313 bool buffer_usage_condition_applies_to_channel(
1314 const struct lttng_condition
*condition
,
1315 const struct channel_info
*channel_info
)
1317 enum lttng_condition_status status
;
1318 enum lttng_domain_type condition_domain
;
1319 const char *condition_session_name
= NULL
;
1320 const char *condition_channel_name
= NULL
;
1322 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1324 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1325 if (channel_info
->key
.domain
!= condition_domain
) {
1329 status
= lttng_condition_buffer_usage_get_session_name(
1330 condition
, &condition_session_name
);
1331 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1333 status
= lttng_condition_buffer_usage_get_channel_name(
1334 condition
, &condition_channel_name
);
1335 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1337 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1340 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1350 bool session_consumed_size_condition_applies_to_channel(
1351 const struct lttng_condition
*condition
,
1352 const struct channel_info
*channel_info
)
1354 enum lttng_condition_status status
;
1355 const char *condition_session_name
= NULL
;
1357 status
= lttng_condition_session_consumed_size_get_session_name(
1358 condition
, &condition_session_name
);
1359 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1361 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1371 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1372 const struct channel_info
*channel_info
)
1374 const struct lttng_condition
*condition
;
1375 bool trigger_applies
;
1377 condition
= lttng_trigger_get_const_condition(trigger
);
1382 switch (lttng_condition_get_type(condition
)) {
1383 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1384 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1385 trigger_applies
= buffer_usage_condition_applies_to_channel(
1386 condition
, channel_info
);
1388 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1389 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1390 condition
, channel_info
);
1396 return trigger_applies
;
1402 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1403 struct notification_client
*client
)
1405 bool applies
= false;
1406 struct lttng_condition_list_element
*condition_list_element
;
1408 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1410 applies
= lttng_condition_is_equal(
1411 condition_list_element
->condition
,
1412 lttng_trigger_get_condition(trigger
));
1420 /* Must be called with RCU read lock held. */
1422 struct lttng_session_trigger_list
*get_session_trigger_list(
1423 struct notification_thread_state
*state
,
1424 const char *session_name
)
1426 struct lttng_session_trigger_list
*list
= NULL
;
1427 struct cds_lfht_node
*node
;
1428 struct cds_lfht_iter iter
;
1430 cds_lfht_lookup(state
->session_triggers_ht
,
1431 hash_key_str(session_name
, lttng_ht_seed
),
1432 match_session_trigger_list
,
1435 node
= cds_lfht_iter_get_node(&iter
);
1438 * Not an error, the list of triggers applying to that session
1439 * will be initialized when the session is created.
1441 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1446 list
= caa_container_of(node
,
1447 struct lttng_session_trigger_list
,
1448 session_triggers_ht_node
);
1454 * Allocate an empty lttng_session_trigger_list for the session named
1457 * No ownership of 'session_name' is assumed by the session trigger list.
1458 * It is the caller's responsability to ensure the session name is alive
1459 * for as long as this list is.
1462 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1463 const char *session_name
,
1464 struct cds_lfht
*session_triggers_ht
)
1466 struct lttng_session_trigger_list
*list
;
1468 list
= zmalloc(sizeof(*list
));
1472 list
->session_name
= session_name
;
1473 CDS_INIT_LIST_HEAD(&list
->list
);
1474 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1475 list
->session_triggers_ht
= session_triggers_ht
;
1478 /* Publish the list through the session_triggers_ht. */
1479 cds_lfht_add(session_triggers_ht
,
1480 hash_key_str(session_name
, lttng_ht_seed
),
1481 &list
->session_triggers_ht_node
);
1488 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1490 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1495 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1497 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1499 /* Empty the list element by element, and then free the list itself. */
1500 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1501 &list
->list
, node
) {
1502 cds_list_del(&trigger_list_element
->node
);
1503 free(trigger_list_element
);
1506 /* Unpublish the list from the session_triggers_ht. */
1507 cds_lfht_del(list
->session_triggers_ht
,
1508 &list
->session_triggers_ht_node
);
1510 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1514 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1515 struct lttng_trigger
*trigger
)
1518 struct lttng_trigger_list_element
*new_element
=
1519 zmalloc(sizeof(*new_element
));
1525 CDS_INIT_LIST_HEAD(&new_element
->node
);
1526 new_element
->trigger
= trigger
;
1527 cds_list_add(&new_element
->node
, &list
->list
);
1533 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1534 const char *session_name
)
1536 bool applies
= false;
1537 const struct lttng_condition
*condition
;
1539 condition
= lttng_trigger_get_const_condition(trigger
);
1540 switch (lttng_condition_get_type(condition
)) {
1541 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1542 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1544 enum lttng_condition_status condition_status
;
1545 const char *condition_session_name
;
1547 condition_status
= lttng_condition_session_rotation_get_session_name(
1548 condition
, &condition_session_name
);
1549 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1550 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1554 assert(condition_session_name
);
1555 applies
= !strcmp(condition_session_name
, session_name
);
1566 * Allocate and initialize an lttng_session_trigger_list which contains
1567 * all triggers that apply to the session named 'session_name'.
1569 * No ownership of 'session_name' is assumed by the session trigger list.
1570 * It is the caller's responsability to ensure the session name is alive
1571 * for as long as this list is.
1574 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1575 const struct notification_thread_state
*state
,
1576 const char *session_name
)
1578 int trigger_count
= 0;
1579 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1580 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1581 struct cds_lfht_iter iter
;
1583 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1584 state
->session_triggers_ht
);
1586 /* Add all triggers applying to the session named 'session_name'. */
1587 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1591 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1596 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1597 trigger_ht_element
->trigger
);
1605 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1607 return session_trigger_list
;
1609 lttng_session_trigger_list_destroy(session_trigger_list
);
1614 struct session_info
*find_or_create_session_info(
1615 struct notification_thread_state
*state
,
1616 const char *name
, uid_t uid
, gid_t gid
)
1618 struct session_info
*session
= NULL
;
1619 struct cds_lfht_node
*node
;
1620 struct cds_lfht_iter iter
;
1621 struct lttng_session_trigger_list
*trigger_list
;
1624 cds_lfht_lookup(state
->sessions_ht
,
1625 hash_key_str(name
, lttng_ht_seed
),
1629 node
= cds_lfht_iter_get_node(&iter
);
1631 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1633 session
= caa_container_of(node
, struct session_info
,
1635 assert(session
->uid
== uid
);
1636 assert(session
->gid
== gid
);
1637 session_info_get(session
);
1641 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1642 if (!trigger_list
) {
1646 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1647 state
->sessions_ht
);
1649 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1651 lttng_session_trigger_list_destroy(trigger_list
);
1654 trigger_list
= NULL
;
1656 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1657 &session
->sessions_ht_node
);
1663 session_info_put(session
);
1668 int handle_notification_thread_command_add_channel(
1669 struct notification_thread_state
*state
,
1670 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1671 const char *channel_name
, enum lttng_domain_type channel_domain
,
1672 uint64_t channel_key_int
, uint64_t channel_capacity
,
1673 enum lttng_error_code
*cmd_result
)
1675 struct cds_list_head trigger_list
;
1676 struct channel_info
*new_channel_info
= NULL
;
1677 struct channel_key channel_key
= {
1678 .key
= channel_key_int
,
1679 .domain
= channel_domain
,
1681 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1682 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1683 int trigger_count
= 0;
1684 struct cds_lfht_iter iter
;
1685 struct session_info
*session_info
= NULL
;
1687 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
1689 channel_name
, session_name
, channel_key_int
,
1690 lttng_domain_type_str(channel_domain
));
1692 CDS_INIT_LIST_HEAD(&trigger_list
);
1694 session_info
= find_or_create_session_info(state
, session_name
,
1695 session_uid
, session_gid
);
1696 if (!session_info
) {
1697 /* Allocation error or an internal error occurred. */
1701 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1702 channel_capacity
, session_info
);
1703 if (!new_channel_info
) {
1708 /* Build a list of all triggers applying to the new channel. */
1709 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1711 struct lttng_trigger_list_element
*new_element
;
1713 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1714 new_channel_info
)) {
1718 new_element
= zmalloc(sizeof(*new_element
));
1723 CDS_INIT_LIST_HEAD(&new_element
->node
);
1724 new_element
->trigger
= trigger_ht_element
->trigger
;
1725 cds_list_add(&new_element
->node
, &trigger_list
);
1730 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1732 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1733 if (!channel_trigger_list
) {
1736 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1737 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1738 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1739 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1742 /* Add channel to the channel_ht which owns the channel_infos. */
1743 cds_lfht_add(state
->channels_ht
,
1744 hash_channel_key(&new_channel_info
->key
),
1745 &new_channel_info
->channels_ht_node
);
1747 * Add the list of triggers associated with this channel to the
1748 * channel_triggers_ht.
1750 cds_lfht_add(state
->channel_triggers_ht
,
1751 hash_channel_key(&new_channel_info
->key
),
1752 &channel_trigger_list
->channel_triggers_ht_node
);
1754 session_info_put(session_info
);
1755 *cmd_result
= LTTNG_OK
;
1758 channel_info_destroy(new_channel_info
);
1759 session_info_put(session_info
);
1764 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1766 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1771 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1773 free(caa_container_of(node
, struct channel_state_sample
,
1778 int handle_notification_thread_command_remove_channel(
1779 struct notification_thread_state
*state
,
1780 uint64_t channel_key
, enum lttng_domain_type domain
,
1781 enum lttng_error_code
*cmd_result
)
1783 struct cds_lfht_node
*node
;
1784 struct cds_lfht_iter iter
;
1785 struct lttng_channel_trigger_list
*trigger_list
;
1786 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1787 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1788 struct channel_info
*channel_info
;
1790 DBG("[notification-thread] Removing channel key = %" PRIu64
1792 channel_key
, lttng_domain_type_str(domain
));
1796 cds_lfht_lookup(state
->channel_triggers_ht
,
1797 hash_channel_key(&key
),
1798 match_channel_trigger_list
,
1801 node
= cds_lfht_iter_get_node(&iter
);
1803 * There is a severe internal error if we are being asked to remove a
1804 * channel that doesn't exist.
1807 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1811 /* Free the list of triggers associated with this channel. */
1812 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1813 channel_triggers_ht_node
);
1814 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1815 &trigger_list
->list
, node
) {
1816 cds_list_del(&trigger_list_element
->node
);
1817 free(trigger_list_element
);
1819 cds_lfht_del(state
->channel_triggers_ht
, node
);
1820 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1822 /* Free sampled channel state. */
1823 cds_lfht_lookup(state
->channel_state_ht
,
1824 hash_channel_key(&key
),
1825 match_channel_state_sample
,
1828 node
= cds_lfht_iter_get_node(&iter
);
1830 * This is expected to be NULL if the channel is destroyed before we
1831 * received a sample.
1834 struct channel_state_sample
*sample
= caa_container_of(node
,
1835 struct channel_state_sample
,
1836 channel_state_ht_node
);
1838 cds_lfht_del(state
->channel_state_ht
, node
);
1839 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1842 /* Remove the channel from the channels_ht and free it. */
1843 cds_lfht_lookup(state
->channels_ht
,
1844 hash_channel_key(&key
),
1848 node
= cds_lfht_iter_get_node(&iter
);
1850 channel_info
= caa_container_of(node
, struct channel_info
,
1852 cds_lfht_del(state
->channels_ht
, node
);
1853 channel_info_destroy(channel_info
);
1856 *cmd_result
= LTTNG_OK
;
1861 int handle_notification_thread_command_session_rotation(
1862 struct notification_thread_state
*state
,
1863 enum notification_thread_command_type cmd_type
,
1864 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1865 uint64_t trace_archive_chunk_id
,
1866 struct lttng_trace_archive_location
*location
,
1867 enum lttng_error_code
*_cmd_result
)
1870 enum lttng_error_code cmd_result
= LTTNG_OK
;
1871 struct lttng_session_trigger_list
*trigger_list
;
1872 struct lttng_trigger_list_element
*trigger_list_element
;
1873 struct session_info
*session_info
;
1877 session_info
= find_or_create_session_info(state
, session_name
,
1878 session_uid
, session_gid
);
1879 if (!session_info
) {
1880 /* Allocation error or an internal error occurred. */
1882 cmd_result
= LTTNG_ERR_NOMEM
;
1886 session_info
->rotation
.ongoing
=
1887 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1888 session_info
->rotation
.id
= trace_archive_chunk_id
;
1889 trigger_list
= get_session_trigger_list(state
, session_name
);
1890 if (!trigger_list
) {
1891 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1896 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1898 const struct lttng_condition
*condition
;
1899 const struct lttng_action
*action
;
1900 const struct lttng_trigger
*trigger
;
1901 struct notification_client_list
*client_list
;
1902 struct lttng_evaluation
*evaluation
= NULL
;
1903 enum lttng_condition_type condition_type
;
1904 bool client_list_is_empty
;
1906 trigger
= trigger_list_element
->trigger
;
1907 condition
= lttng_trigger_get_const_condition(trigger
);
1909 condition_type
= lttng_condition_get_type(condition
);
1911 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1912 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1914 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1915 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1919 action
= lttng_trigger_get_const_action(trigger
);
1921 /* Notify actions are the only type currently supported. */
1922 assert(lttng_action_get_type_const(action
) ==
1923 LTTNG_ACTION_TYPE_NOTIFY
);
1925 client_list
= get_client_list_from_condition(state
, condition
);
1926 assert(client_list
);
1928 pthread_mutex_lock(&client_list
->lock
);
1929 client_list_is_empty
= cds_list_empty(&client_list
->list
);
1930 pthread_mutex_unlock(&client_list
->lock
);
1931 if (client_list_is_empty
) {
1933 * No clients interested in the evaluation's result,
1939 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1940 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1941 trace_archive_chunk_id
);
1943 evaluation
= lttng_evaluation_session_rotation_completed_create(
1944 trace_archive_chunk_id
, location
);
1948 /* Internal error */
1950 cmd_result
= LTTNG_ERR_UNK
;
1954 /* Dispatch evaluation result to all clients. */
1955 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
1956 evaluation
, client_list
, state
,
1959 lttng_evaluation_destroy(evaluation
);
1961 notification_client_list_put(client_list
);
1962 if (caa_unlikely(ret
)) {
1967 session_info_put(session_info
);
1968 *_cmd_result
= cmd_result
;
1974 int handle_notification_thread_command_add_application(
1975 struct notification_thread_handle
*handle
,
1976 struct notification_thread_state
*state
,
1977 int read_side_trigger_event_application_pipe
,
1978 enum lttng_error_code
*_cmd_result
)
1981 enum lttng_error_code cmd_result
= LTTNG_OK
;
1982 struct notification_event_trigger_source_element
*element
= NULL
;
1984 element
= zmalloc(sizeof(*element
));
1986 cmd_result
= LTTNG_ERR_NOMEM
;
1991 CDS_INIT_LIST_HEAD(&element
->node
);
1992 element
->fd
= read_side_trigger_event_application_pipe
;
1994 pthread_mutex_lock(&handle
->event_trigger_sources
.lock
);
1995 cds_list_add(&element
->node
, &handle
->event_trigger_sources
.list
);
1996 pthread_mutex_unlock(&handle
->event_trigger_sources
.lock
);
1998 /* TODO: remove on failure to add to list? */
2000 /* Adding the read side pipe to the event poll */
2001 ret
= lttng_poll_add(&state
->events
,
2002 read_side_trigger_event_application_pipe
,
2003 LPOLLIN
| LPOLLERR
);
2005 DBG3("[notification-thread] Adding application event source from fd: %d", read_side_trigger_event_application_pipe
);
2007 /* TODO: what should be the value of cmd_result??? */
2008 ERR("[notification-thread] Failed to add event source pipe fd to pollset");
2013 *_cmd_result
= cmd_result
;
2018 int handle_notification_thread_command_remove_application(
2019 struct notification_thread_handle
*handle
,
2020 struct notification_thread_state
*state
,
2021 int read_side_trigger_event_application_pipe
,
2022 enum lttng_error_code
*_cmd_result
)
2025 enum lttng_error_code cmd_result
= LTTNG_OK
;
2027 /* TODO: missing a lock propably to revisit */
2028 struct notification_event_trigger_source_element
*source_element
, *tmp
;
2029 cds_list_for_each_entry_safe(source_element
, tmp
,
2030 &handle
->event_trigger_sources
.list
, node
) {
2031 if (source_element
->fd
!= read_side_trigger_event_application_pipe
) {
2035 DBG("[notification-thread] Removed event source from event source list");
2036 cds_list_del(&source_element
->node
);
2037 free(source_element
);
2041 DBG3("[notification-thread] Removing application event source from fd: %d", read_side_trigger_event_application_pipe
);
2042 /* Removing the read side pipe to the event poll */
2043 ret
= lttng_poll_del(&state
->events
,
2044 read_side_trigger_event_application_pipe
);
2046 /* TODO: what should be the value of cmd_result??? */
2047 ERR("[notification-thread] Failed to remove event source pipe fd from pollset");
2052 *_cmd_result
= cmd_result
;
2056 static int handle_notification_thread_command_get_tokens(
2057 struct notification_thread_handle
*handle
,
2058 struct notification_thread_state
*state
,
2059 struct lttng_triggers
**triggers
,
2060 enum lttng_error_code
*_cmd_result
)
2063 enum lttng_error_code cmd_result
= LTTNG_OK
;
2064 struct cds_lfht_iter iter
;
2065 struct notification_trigger_tokens_ht_element
*element
;
2066 struct lttng_triggers
*local_triggers
= NULL
;
2068 local_triggers
= lttng_triggers_create();
2069 if (!local_triggers
) {
2070 cmd_result
= LTTNG_ERR_NOMEM
;
2075 cds_lfht_for_each_entry (
2076 state
->trigger_tokens_ht
, &iter
, element
, node
) {
2077 ret
= lttng_triggers_add(local_triggers
, element
->trigger
);
2079 cmd_result
= LTTNG_ERR_FATAL
;
2084 /* Ownership is shared with the lttng_triggers object */
2085 lttng_trigger_get(element
->trigger
);
2090 /* Passing ownership up */
2091 *triggers
= local_triggers
;
2092 local_triggers
= NULL
;
2096 lttng_triggers_destroy(local_triggers
);
2097 *_cmd_result
= cmd_result
;
2102 int handle_notification_thread_command_list_triggers(
2103 struct notification_thread_handle
*handle
,
2104 struct notification_thread_state
*state
,
2107 struct lttng_triggers
**triggers
,
2108 enum lttng_error_code
*_cmd_result
)
2111 enum lttng_error_code cmd_result
= LTTNG_OK
;
2112 struct cds_lfht_iter iter
;
2113 struct lttng_trigger_ht_element
*trigger_ht_element
;
2114 struct lttng_triggers
*local_triggers
= NULL
;
2115 const struct lttng_credentials
*creds
;
2118 unsigned long count
;
2121 cds_lfht_count_nodes(state
->triggers_ht
, &scb
, &count
, &sca
);
2123 /* TODO check downcasting */
2124 local_triggers
= lttng_triggers_create();
2125 if (!local_triggers
) {
2126 cmd_result
= LTTNG_ERR_NOMEM
;
2130 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
,
2131 trigger_ht_element
, node
) {
2132 /* Only return the trigger for which the requestion client have
2133 * access. For now the root user can only list its own
2135 * TODO: root user behavior
2137 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2138 if ((uid
!= creds
->uid
) || (gid
!= creds
->gid
)) {
2142 ret
= lttng_triggers_add(local_triggers
, trigger_ht_element
->trigger
);
2147 /* Ownership is shared with the lttng_triggers object */
2148 lttng_trigger_get(trigger_ht_element
->trigger
);
2153 /* Passing ownership up */
2154 *triggers
= local_triggers
;
2155 local_triggers
= NULL
;
2159 lttng_triggers_destroy(local_triggers
);
2160 *_cmd_result
= cmd_result
;
2165 int condition_is_supported(struct lttng_condition
*condition
)
2169 switch (lttng_condition_get_type(condition
)) {
2170 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2171 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2173 enum lttng_domain_type domain
;
2175 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2182 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2188 * Older kernel tracers don't expose the API to monitor their
2189 * buffers. Therefore, we reject triggers that require that
2190 * mechanism to be available to be evaluated.
2192 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
2195 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
2198 * Check for kernel support.
2199 * Check for ust support ??
2212 int action_is_supported(struct lttng_action
*action
)
2216 switch (lttng_action_get_type(action
)) {
2217 case LTTNG_ACTION_TYPE_NOTIFY
:
2218 case LTTNG_ACTION_TYPE_START_SESSION
:
2219 case LTTNG_ACTION_TYPE_STOP_SESSION
:
2220 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
2221 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
2223 /* TODO validate that this is true for kernel in regards to
2224 * rotation and snapshot. Start stop is not a problem notify
2227 /* For now all type of actions are supported */
2231 case LTTNG_ACTION_TYPE_GROUP
:
2233 /* TODO: Iterate over all internal actions and validate that
2234 * they are supported
2247 /* Must be called with RCU read lock held. */
2249 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2250 struct notification_thread_state
*state
)
2253 const struct lttng_condition
*condition
;
2254 const char *session_name
;
2255 struct lttng_session_trigger_list
*trigger_list
;
2257 condition
= lttng_trigger_get_const_condition(trigger
);
2258 switch (lttng_condition_get_type(condition
)) {
2259 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2260 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2262 enum lttng_condition_status status
;
2264 status
= lttng_condition_session_rotation_get_session_name(
2265 condition
, &session_name
);
2266 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2267 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2278 trigger_list
= get_session_trigger_list(state
, session_name
);
2279 if (!trigger_list
) {
2280 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2286 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2288 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2293 /* Must be called with RCU read lock held. */
2295 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2296 struct notification_thread_state
*state
)
2299 struct cds_lfht_node
*node
;
2300 struct cds_lfht_iter iter
;
2301 struct channel_info
*channel
;
2303 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2305 struct lttng_trigger_list_element
*trigger_list_element
;
2306 struct lttng_channel_trigger_list
*trigger_list
;
2307 struct cds_lfht_iter lookup_iter
;
2309 if (!trigger_applies_to_channel(trigger
, channel
)) {
2313 cds_lfht_lookup(state
->channel_triggers_ht
,
2314 hash_channel_key(&channel
->key
),
2315 match_channel_trigger_list
,
2318 node
= cds_lfht_iter_get_node(&lookup_iter
);
2320 trigger_list
= caa_container_of(node
,
2321 struct lttng_channel_trigger_list
,
2322 channel_triggers_ht_node
);
2324 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2325 if (!trigger_list_element
) {
2329 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2330 trigger_list_element
->trigger
= trigger
;
2331 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2332 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2340 bool trigger_name_taken(struct notification_thread_state
*state
, const char *name
)
2342 struct cds_lfht_node
*triggers_by_name_ht_node
;
2343 struct cds_lfht_iter iter
;
2344 /* TODO change hashing for trigger */
2345 cds_lfht_lookup(state
->triggers_by_name_ht
,
2346 hash_key_str(name
, lttng_ht_seed
),
2350 triggers_by_name_ht_node
= cds_lfht_iter_get_node(&iter
);
2351 if (triggers_by_name_ht_node
) {
2360 void generate_trigger_name(struct notification_thread_state
*state
, struct lttng_trigger
*trigger
, const char **name
)
2362 /* Here the offset criteria guarantee an end. This will be a nice
2363 * bikeshedding conversation. I would simply generate uuid and use them
2368 lttng_trigger_generate_name(trigger
, state
->trigger_id
.name_offset
);
2369 /* TODO error checking */
2370 lttng_trigger_get_name(trigger
, name
);
2371 taken
= trigger_name_taken(state
, *name
);
2373 state
->trigger_id
.name_offset
++;
2375 } while (taken
|| state
->trigger_id
.name_offset
== UINT32_MAX
);
2378 static int action_notify_register_trigger(
2379 struct notification_thread_state
*state
,
2380 struct lttng_trigger
*trigger
)
2384 struct lttng_condition
*condition
;
2385 struct notification_client
*client
;
2386 struct notification_client_list
*client_list
= NULL
;
2387 struct cds_lfht_iter iter
;
2388 struct notification_client_list_element
*client_list_element
, *tmp
;
2390 condition
= lttng_trigger_get_condition(trigger
);
2393 client_list
= notification_client_list_create(trigger
);
2399 /* Build a list of clients to which this new trigger applies. */
2400 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2401 client_socket_ht_node
) {
2402 if (!trigger_applies_to_client(trigger
, client
)) {
2406 client_list_element
= zmalloc(sizeof(*client_list_element
));
2407 if (!client_list_element
) {
2409 goto error_put_client_list
;
2411 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2412 client_list_element
->client
= client
;
2413 cds_list_add(&client_list_element
->node
, &client_list
->list
);
2416 switch (get_condition_binding_object(condition
)) {
2417 case LTTNG_OBJECT_TYPE_SESSION
:
2418 /* Add the trigger to the list if it matches a known session. */
2419 ret
= bind_trigger_to_matching_session(trigger
, state
);
2421 goto error_put_client_list
;
2424 case LTTNG_OBJECT_TYPE_CHANNEL
:
2426 * Add the trigger to list of triggers bound to the channels
2429 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2431 goto error_put_client_list
;
2434 case LTTNG_OBJECT_TYPE_NONE
:
2437 ERR("[notification-thread] Unknown object type on which to bind a newly registered trigger was encountered");
2439 goto error_put_client_list
;
2443 * Since there is nothing preventing clients from subscribing to a
2444 * condition before the corresponding trigger is registered, we have
2445 * to evaluate this new condition right away.
2447 * At some point, we were waiting for the next "evaluation" (e.g. on
2448 * reception of a channel sample) to evaluate this new condition, but
2451 * The reason it was broken is that waiting for the next sample
2452 * does not allow us to properly handle transitions for edge-triggered
2455 * Consider this example: when we handle a new channel sample, we
2456 * evaluate each conditions twice: once with the previous state, and
2457 * again with the newest state. We then use those two results to
2458 * determine whether a state change happened: a condition was false and
2459 * became true. If a state change happened, we have to notify clients.
2461 * Now, if a client subscribes to a given notification and registers
2462 * a trigger *after* that subscription, we have to make sure the
2463 * condition is evaluated at this point while considering only the
2464 * current state. Otherwise, the next evaluation cycle may only see
2465 * that the evaluations remain the same (true for samples n-1 and n) and
2466 * the client will never know that the condition has been met.
2468 * No need to lock the list here as it has not been published yet.
2470 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2471 &client_list
->list
, node
) {
2472 ret
= evaluate_condition_for_client(trigger
, condition
,
2473 client_list_element
->client
, state
);
2475 goto error_put_client_list
;
2480 * Client list ownership transferred to the
2481 * notification_trigger_clients_ht.
2483 publish_notification_client_list(state
, client_list
);
2485 error_put_client_list
:
2486 notification_client_list_put(client_list
);
2491 static bool action_is_notify(const struct lttng_action
*action
)
2493 /* TODO for action groups we need to iterate over all of them */
2494 enum lttng_action_type type
= lttng_action_get_type_const(action
);
2496 enum lttng_action_status status
;
2497 const struct lttng_action
*tmp
;
2498 unsigned int i
, count
;
2501 case LTTNG_ACTION_TYPE_NOTIFY
:
2504 case LTTNG_ACTION_TYPE_GROUP
:
2505 status
= lttng_action_group_get_count(action
, &count
);
2506 if (status
!= LTTNG_ACTION_STATUS_OK
) {
2509 for (i
= 0; i
< count
; i
++) {
2510 tmp
= lttng_action_group_get_at_index_const(action
, i
);
2512 ret
= action_is_notify(tmp
);
2527 * TODO: REVIEW THIS COMMENT.
2528 * FIXME A client's credentials are not checked when registering a trigger, nor
2529 * are they stored alongside with the trigger.
2531 * The effects of this are benign since:
2532 * - The client will succeed in registering the trigger, as it is valid,
2533 * - The trigger will, internally, be bound to the channel/session,
2534 * - The notifications will not be sent since the client's credentials
2535 * are checked against the channel at that moment.
2537 * If this function returns a non-zero value, it means something is
2538 * fundamentally broken and the whole subsystem/thread will be torn down.
2540 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2544 int handle_notification_thread_command_register_trigger(
2545 struct notification_thread_state
*state
,
2546 struct lttng_trigger
*trigger
,
2547 enum lttng_error_code
*cmd_result
)
2551 struct lttng_condition
*condition
;
2552 struct lttng_action
*action
;
2553 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2554 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2555 struct cds_lfht_node
*node
;
2556 const char* trigger_name
;
2557 bool free_trigger
= true;
2559 assert(trigger
->creds
.set
);
2563 /* Set the trigger's key */
2564 lttng_trigger_set_key(trigger
, state
->trigger_id
.token_generator
);
2566 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2567 LTTNG_TRIGGER_STATUS_UNSET
) {
2568 generate_trigger_name(state
, trigger
, &trigger_name
);
2569 } else if (trigger_name_taken(state
, trigger_name
)) {
2570 /* Not a fatal error */
2571 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2576 condition
= lttng_trigger_get_condition(trigger
);
2579 action
= lttng_trigger_get_action(trigger
);
2582 is_supported
= condition_is_supported(condition
);
2583 if (is_supported
< 0) {
2585 } else if (is_supported
== 0) {
2587 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2591 is_supported
= action_is_supported(action
);
2592 if (is_supported
< 0) {
2594 } else if (is_supported
== 0) {
2596 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2600 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2601 if (!trigger_ht_element
) {
2606 /* Add trigger to the trigger_ht. */
2607 cds_lfht_node_init(&trigger_ht_element
->node
);
2608 cds_lfht_node_init(&trigger_ht_element
->node_by_name
);
2609 trigger_ht_element
->trigger
= trigger
;
2611 node
= cds_lfht_add_unique(state
->triggers_ht
,
2612 lttng_condition_hash(condition
),
2615 &trigger_ht_element
->node
);
2616 if (node
!= &trigger_ht_element
->node
) {
2617 /* Not a fatal error, simply report it to the client. */
2618 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2619 goto error_free_ht_element
;
2622 node
= cds_lfht_add_unique(state
->triggers_by_name_ht
,
2623 hash_key_str(trigger_name
, lttng_ht_seed
), match_str
,
2624 trigger_name
, &trigger_ht_element
->node_by_name
);
2625 if (node
!= &trigger_ht_element
->node_by_name
) {
2626 /* This should never happen */
2627 /* Not a fatal error, simply report it to the client. */
2628 /* TODO remove from the trigger_ht */
2629 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2630 goto error_free_ht_element
;
2633 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2634 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2635 if (!trigger_tokens_ht_element
) {
2640 /* Add trigger token to the trigger_tokens_ht. */
2641 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2642 trigger_tokens_ht_element
->token
= trigger
->key
.value
;
2643 trigger_tokens_ht_element
->trigger
= trigger
;
2645 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2646 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2647 match_trigger_token
,
2648 &trigger_tokens_ht_element
->token
,
2649 &trigger_tokens_ht_element
->node
);
2650 if (node
!= &trigger_tokens_ht_element
->node
) {
2651 /* TODO: THIS IS A FATAL ERROR... should never happen */
2652 /* Not a fatal error, simply report it to the client. */
2653 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2654 goto error_free_ht_element
;
2659 * Ownership of the trigger and of its wrapper was transfered to
2660 * the triggers_ht. Same for token ht element if necessary.
2662 trigger_tokens_ht_element
= NULL
;
2663 trigger_ht_element
= NULL
;
2664 free_trigger
= false;
2666 if (action_is_notify(action
)) {
2667 ret
= action_notify_register_trigger(state
, trigger
);
2669 /* TODO should cmd_result be set here? */
2671 goto error_free_ht_element
;
2675 /* Increment the trigger unique id generator */
2676 state
->trigger_id
.token_generator
++;
2677 *cmd_result
= LTTNG_OK
;
2679 error_free_ht_element
:
2680 free(trigger_ht_element
);
2681 free(trigger_tokens_ht_element
);
2684 lttng_trigger_destroy(trigger
);
2691 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2693 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2698 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2700 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2705 int handle_notification_thread_command_unregister_trigger(
2706 struct notification_thread_state
*state
,
2707 struct lttng_trigger
*trigger
,
2708 enum lttng_error_code
*_cmd_reply
)
2710 struct cds_lfht_iter iter
;
2711 struct cds_lfht_node
*triggers_ht_node
;
2712 struct lttng_channel_trigger_list
*trigger_list
;
2713 struct notification_client_list
*client_list
;
2714 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2715 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2717 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
2718 enum lttng_error_code cmd_reply
;
2722 /* TODO change hashing for trigger */
2723 /* TODO Disabling for the root user is not complete, for now the root
2724 * user cannot disable the trigger from another user.
2726 cds_lfht_lookup(state
->triggers_ht
,
2727 lttng_condition_hash(condition
),
2731 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2732 if (!triggers_ht_node
) {
2733 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2736 cmd_reply
= LTTNG_OK
;
2739 /* Remove trigger from channel_triggers_ht. */
2740 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2741 channel_triggers_ht_node
) {
2742 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2744 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2745 &trigger_list
->list
, node
) {
2746 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2750 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2751 cds_list_del(&trigger_element
->node
);
2752 /* A trigger can only appear once per channel */
2757 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2758 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2759 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
, trigger_tokens_ht_element
,
2761 if (!lttng_trigger_is_equal(trigger
, trigger_tokens_ht_element
->trigger
)) {
2765 /* TODO talk to all app and remove it */
2766 DBG("[notification-thread] Removed trigger from tokens_ht");
2767 cds_lfht_del(state
->trigger_tokens_ht
,
2768 &trigger_tokens_ht_element
->node
);
2769 call_rcu(&trigger_tokens_ht_element
->rcu_node
, free_notification_trigger_tokens_ht_element_rcu
);
2775 if (action_is_notify(action
)) {
2777 * Remove and release the client list from
2778 * notification_trigger_clients_ht.
2780 client_list
= get_client_list_from_condition(state
, condition
);
2781 assert(client_list
);
2783 /* Put new reference and the hashtable's reference. */
2784 notification_client_list_put(client_list
);
2785 notification_client_list_put(client_list
);
2789 /* Remove trigger from triggers_ht. */
2790 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2791 struct lttng_trigger_ht_element
, node
);
2792 cds_lfht_del(state
->triggers_by_name_ht
, &trigger_ht_element
->node_by_name
);
2793 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2795 /* Release the ownership of the trigger */
2796 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2797 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2801 *_cmd_reply
= cmd_reply
;
2806 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2807 int handle_notification_thread_command(
2808 struct notification_thread_handle
*handle
,
2809 struct notification_thread_state
*state
)
2813 struct notification_thread_command
*cmd
;
2815 /* Read the event pipe to put it back into a quiescent state. */
2816 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2818 if (ret
!= sizeof(counter
)) {
2822 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2823 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2824 struct notification_thread_command
, cmd_list_node
);
2825 cds_list_del(&cmd
->cmd_list_node
);
2826 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2827 switch (cmd
->type
) {
2828 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2829 DBG("[notification-thread] Received register trigger command");
2830 ret
= handle_notification_thread_command_register_trigger(
2831 state
, cmd
->parameters
.trigger
,
2834 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2835 DBG("[notification-thread] Received unregister trigger command");
2836 ret
= handle_notification_thread_command_unregister_trigger(
2837 state
, cmd
->parameters
.trigger
,
2840 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2841 DBG("[notification-thread] Received add channel command");
2842 ret
= handle_notification_thread_command_add_channel(
2844 cmd
->parameters
.add_channel
.session
.name
,
2845 cmd
->parameters
.add_channel
.session
.uid
,
2846 cmd
->parameters
.add_channel
.session
.gid
,
2847 cmd
->parameters
.add_channel
.channel
.name
,
2848 cmd
->parameters
.add_channel
.channel
.domain
,
2849 cmd
->parameters
.add_channel
.channel
.key
,
2850 cmd
->parameters
.add_channel
.channel
.capacity
,
2853 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2854 DBG("[notification-thread] Received remove channel command");
2855 ret
= handle_notification_thread_command_remove_channel(
2856 state
, cmd
->parameters
.remove_channel
.key
,
2857 cmd
->parameters
.remove_channel
.domain
,
2860 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2861 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2862 DBG("[notification-thread] Received session rotation %s command",
2863 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2864 "ongoing" : "completed");
2865 ret
= handle_notification_thread_command_session_rotation(
2868 cmd
->parameters
.session_rotation
.session_name
,
2869 cmd
->parameters
.session_rotation
.uid
,
2870 cmd
->parameters
.session_rotation
.gid
,
2871 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2872 cmd
->parameters
.session_rotation
.location
,
2875 case NOTIFICATION_COMMAND_TYPE_ADD_APPLICATION
:
2876 ret
= handle_notification_thread_command_add_application(
2879 cmd
->parameters
.application
.read_side_trigger_event_application_pipe
,
2882 case NOTIFICATION_COMMAND_TYPE_REMOVE_APPLICATION
:
2883 ret
= handle_notification_thread_command_remove_application(
2886 cmd
->parameters
.application
.read_side_trigger_event_application_pipe
,
2889 case NOTIFICATION_COMMAND_TYPE_GET_TOKENS
:
2891 struct lttng_triggers
*triggers
= NULL
;
2892 ret
= handle_notification_thread_command_get_tokens(
2893 handle
, state
, &triggers
, &cmd
->reply_code
);
2894 cmd
->reply
.get_tokens
.triggers
= triggers
;
2898 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2900 struct lttng_triggers
*triggers
= NULL
;
2901 ret
= handle_notification_thread_command_list_triggers(
2904 cmd
->parameters
.list_triggers
.uid
,
2905 cmd
->parameters
.list_triggers
.gid
,
2908 cmd
->reply
.list_triggers
.triggers
= triggers
;
2912 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2913 DBG("[notification-thread] Received quit command");
2914 cmd
->reply_code
= LTTNG_OK
;
2917 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2919 const enum client_transmission_status client_status
=
2920 cmd
->parameters
.client_communication_update
2922 const notification_client_id client_id
=
2923 cmd
->parameters
.client_communication_update
.id
;
2924 struct notification_client
*client
;
2927 client
= get_client_from_id(client_id
, state
);
2931 * Client error was probably already picked-up by the
2932 * notification thread or it has disconnected
2933 * gracefully while this command was queued.
2935 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2939 pthread_mutex_lock(&client
->lock
);
2940 ret
= client_handle_transmission_status(
2941 client
, client_status
, state
);
2942 pthread_mutex_unlock(&client
->lock
);
2948 ERR("[notification-thread] Unknown internal command received");
2956 if (cmd
->is_async
) {
2960 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2964 /* Wake-up and return a fatal error to the calling thread. */
2965 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2966 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2968 /* Indicate a fatal error to the caller. */
2973 int socket_set_non_blocking(int socket
)
2977 /* Set the pipe as non-blocking. */
2978 ret
= fcntl(socket
, F_GETFL
, 0);
2980 PERROR("fcntl get socket flags");
2985 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2987 PERROR("fcntl set O_NONBLOCK socket flag");
2990 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2995 /* Client lock must be acquired by caller. */
2997 int client_reset_inbound_state(struct notification_client
*client
)
3001 ASSERT_LOCKED(client
->lock
);
3003 ret
= lttng_dynamic_buffer_set_size(
3004 &client
->communication
.inbound
.buffer
, 0);
3007 client
->communication
.inbound
.bytes_to_receive
=
3008 sizeof(struct lttng_notification_channel_message
);
3009 client
->communication
.inbound
.msg_type
=
3010 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3011 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3012 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3013 ret
= lttng_dynamic_buffer_set_size(
3014 &client
->communication
.inbound
.buffer
,
3015 client
->communication
.inbound
.bytes_to_receive
);
3019 int handle_notification_thread_client_connect(
3020 struct notification_thread_state
*state
)
3023 struct notification_client
*client
;
3025 DBG("[notification-thread] Handling new notification channel client connection");
3027 client
= zmalloc(sizeof(*client
));
3033 pthread_mutex_init(&client
->lock
, NULL
);
3034 client
->id
= state
->next_notification_client_id
++;
3035 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3036 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
3037 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
3038 client
->communication
.inbound
.expect_creds
= true;
3040 pthread_mutex_lock(&client
->lock
);
3041 ret
= client_reset_inbound_state(client
);
3042 pthread_mutex_unlock(&client
->lock
);
3044 ERR("[notification-thread] Failed to reset client communication's inbound state");
3049 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3051 ERR("[notification-thread] Failed to accept new notification channel client connection");
3056 client
->socket
= ret
;
3058 ret
= socket_set_non_blocking(client
->socket
);
3060 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3064 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3066 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3071 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3072 LPOLLIN
| LPOLLERR
|
3073 LPOLLHUP
| LPOLLRDHUP
);
3075 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3079 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3083 cds_lfht_add(state
->client_socket_ht
,
3084 hash_client_socket(client
->socket
),
3085 &client
->client_socket_ht_node
);
3086 cds_lfht_add(state
->client_id_ht
,
3087 hash_client_id(client
->id
),
3088 &client
->client_id_ht_node
);
3093 notification_client_destroy(client
, state
);
3097 /* RCU read-lock must be held by the caller. */
3098 /* Client lock must be held by the caller */
3100 int notification_thread_client_disconnect(
3101 struct notification_client
*client
,
3102 struct notification_thread_state
*state
)
3105 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3107 /* Acquire the client lock to disable its communication atomically. */
3108 client
->communication
.active
= false;
3109 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3111 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3115 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3116 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3118 /* Release all conditions to which the client was subscribed. */
3119 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3120 &client
->condition_list
, node
) {
3121 (void) notification_thread_client_unsubscribe(client
,
3122 condition_list_element
->condition
, state
, NULL
);
3126 * Client no longer accessible to other threads (through the
3129 notification_client_destroy(client
, state
);
3133 int handle_notification_thread_client_disconnect(
3134 int client_socket
, struct notification_thread_state
*state
)
3137 struct notification_client
*client
;
3140 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3142 client
= get_client_from_socket(client_socket
, state
);
3144 /* Internal state corruption, fatal error. */
3145 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3151 pthread_mutex_lock(&client
->lock
);
3152 ret
= notification_thread_client_disconnect(client
, state
);
3153 pthread_mutex_unlock(&client
->lock
);
3159 int handle_notification_thread_client_disconnect_all(
3160 struct notification_thread_state
*state
)
3162 struct cds_lfht_iter iter
;
3163 struct notification_client
*client
;
3164 bool error_encoutered
= false;
3167 DBG("[notification-thread] Closing all client connections");
3168 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3169 client_socket_ht_node
) {
3172 pthread_mutex_lock(&client
->lock
);
3173 ret
= notification_thread_client_disconnect(
3175 pthread_mutex_unlock(&client
->lock
);
3177 error_encoutered
= true;
3181 return error_encoutered
? 1 : 0;
3184 int handle_notification_thread_trigger_unregister_all(
3185 struct notification_thread_state
*state
)
3187 bool error_occurred
= false;
3188 struct cds_lfht_iter iter
;
3189 struct lttng_trigger_ht_element
*trigger_ht_element
;
3192 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3194 int ret
= handle_notification_thread_command_unregister_trigger(
3195 state
, trigger_ht_element
->trigger
, NULL
);
3197 error_occurred
= true;
3201 return error_occurred
? -1 : 0;
3205 int client_handle_transmission_status(
3206 struct notification_client
*client
,
3207 enum client_transmission_status transmission_status
,
3208 struct notification_thread_state
*state
)
3212 ASSERT_LOCKED(client
->lock
);
3214 switch (transmission_status
) {
3215 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3216 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3217 CLIENT_POLL_MASK_IN
);
3222 client
->communication
.outbound
.queued_command_reply
= false;
3223 client
->communication
.outbound
.dropped_notification
= false;
3225 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3227 * We want to be notified whenever there is buffer space
3228 * available to send the rest of the payload.
3230 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3231 CLIENT_POLL_MASK_IN_OUT
);
3236 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3237 ret
= notification_thread_client_disconnect(client
, state
);
3242 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3252 /* Client lock must be acquired by caller. */
3254 enum client_transmission_status
client_flush_outgoing_queue(
3255 struct notification_client
*client
)
3258 size_t to_send_count
;
3259 enum client_transmission_status status
;
3261 ASSERT_LOCKED(client
->lock
);
3263 if (!client
->communication
.active
) {
3264 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3268 assert(client
->communication
.outbound
.buffer
.size
!= 0);
3269 to_send_count
= client
->communication
.outbound
.buffer
.size
;
3270 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3273 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3274 client
->communication
.outbound
.buffer
.data
,
3276 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
3277 (ret
> 0 && ret
< to_send_count
)) {
3278 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3280 to_send_count
-= max(ret
, 0);
3282 memcpy(client
->communication
.outbound
.buffer
.data
,
3283 client
->communication
.outbound
.buffer
.data
+
3284 client
->communication
.outbound
.buffer
.size
- to_send_count
,
3286 ret
= lttng_dynamic_buffer_set_size(
3287 &client
->communication
.outbound
.buffer
,
3292 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3293 } else if (ret
< 0) {
3294 /* Generic error, disconnect the client. */
3295 PERROR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3297 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3299 /* No error and flushed the queue completely. */
3300 ret
= lttng_dynamic_buffer_set_size(
3301 &client
->communication
.outbound
.buffer
, 0);
3305 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3310 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3313 /* Client lock must be acquired by caller. */
3315 int client_send_command_reply(struct notification_client
*client
,
3316 struct notification_thread_state
*state
,
3317 enum lttng_notification_channel_status status
)
3320 struct lttng_notification_channel_command_reply reply
= {
3321 .status
= (int8_t) status
,
3323 struct lttng_notification_channel_message msg
= {
3324 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3325 .size
= sizeof(reply
),
3327 char buffer
[sizeof(msg
) + sizeof(reply
)];
3328 enum client_transmission_status transmission_status
;
3330 ASSERT_LOCKED(client
->lock
);
3332 if (client
->communication
.outbound
.queued_command_reply
) {
3333 /* Protocol error. */
3337 memcpy(buffer
, &msg
, sizeof(msg
));
3338 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3339 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3341 /* Enqueue buffer to outgoing queue and flush it. */
3342 ret
= lttng_dynamic_buffer_append(
3343 &client
->communication
.outbound
.buffer
,
3344 buffer
, sizeof(buffer
));
3349 transmission_status
= client_flush_outgoing_queue(client
);
3350 ret
= client_handle_transmission_status(
3351 client
, transmission_status
, state
);
3356 if (client
->communication
.outbound
.buffer
.size
!= 0) {
3357 /* Queue could not be emptied. */
3358 client
->communication
.outbound
.queued_command_reply
= true;
3367 int client_handle_message_unknown(struct notification_client
*client
,
3368 struct notification_thread_state
*state
)
3372 pthread_mutex_lock(&client
->lock
);
3375 * Receiving message header. The function will be called again
3376 * once the rest of the message as been received and can be
3379 const struct lttng_notification_channel_message
*msg
;
3381 assert(sizeof(*msg
) == client
->communication
.inbound
.buffer
.size
);
3382 msg
= (const struct lttng_notification_channel_message
*)
3383 client
->communication
.inbound
.buffer
.data
;
3385 if (msg
->size
== 0 ||
3386 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3387 ERR("[notification-thread] Invalid notification channel message: length = %u",
3393 switch (msg
->type
) {
3394 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3395 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3396 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3400 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3404 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3405 client
->communication
.inbound
.msg_type
=
3406 (enum lttng_notification_channel_message_type
) msg
->type
;
3407 ret
= lttng_dynamic_buffer_set_size(
3408 &client
->communication
.inbound
.buffer
, msg
->size
);
3410 pthread_mutex_unlock(&client
->lock
);
3415 int client_handle_message_handshake(struct notification_client
*client
,
3416 struct notification_thread_state
*state
)
3419 struct lttng_notification_channel_command_handshake
*handshake_client
;
3420 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3421 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3422 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3424 const struct lttng_notification_channel_message msg_header
= {
3425 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3426 .size
= sizeof(handshake_reply
),
3428 enum lttng_notification_channel_status status
=
3429 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3430 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3431 enum client_transmission_status transmission_status
;
3433 pthread_mutex_lock(&client
->lock
);
3435 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3436 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3437 sizeof(handshake_reply
));
3440 (struct lttng_notification_channel_command_handshake
*)
3441 client
->communication
.inbound
.buffer
3443 client
->major
= handshake_client
->major
;
3444 client
->minor
= handshake_client
->minor
;
3445 if (!client
->communication
.inbound
.creds_received
) {
3446 ERR("[notification-thread] No credentials received from client");
3451 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3452 &client
->communication
.inbound
.creds
);
3453 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3454 &client
->communication
.inbound
.creds
);
3455 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3456 client
->uid
, client
->gid
, (int) client
->major
,
3457 (int) client
->minor
);
3459 if (handshake_client
->major
!=
3460 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3461 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3464 ret
= lttng_dynamic_buffer_append(
3465 &client
->communication
.outbound
.buffer
, send_buffer
,
3466 sizeof(send_buffer
));
3468 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3472 client
->validated
= true;
3473 client
->communication
.active
= true;
3475 transmission_status
= client_flush_outgoing_queue(client
);
3476 ret
= client_handle_transmission_status(
3477 client
, transmission_status
, state
);
3482 ret
= client_send_command_reply(client
, state
, status
);
3484 ERR("[notification-thread] Failed to send reply to notification channel client");
3488 /* Set reception state to receive the next message header. */
3489 ret
= client_reset_inbound_state(client
);
3491 ERR("[notification-thread] Failed to reset client communication's inbound state");
3496 pthread_mutex_unlock(&client
->lock
);
3501 int client_handle_message_subscription(
3502 struct notification_client
*client
,
3503 enum lttng_notification_channel_message_type msg_type
,
3504 struct notification_thread_state
*state
)
3507 struct lttng_condition
*condition
;
3508 enum lttng_notification_channel_status status
=
3509 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3510 const struct lttng_buffer_view condition_view
=
3511 lttng_buffer_view_from_dynamic_buffer(
3512 &client
->communication
.inbound
.buffer
,
3514 size_t expected_condition_size
;
3516 pthread_mutex_lock(&client
->lock
);
3517 expected_condition_size
= client
->communication
.inbound
.buffer
.size
;
3518 pthread_mutex_unlock(&client
->lock
);
3520 ret
= lttng_condition_create_from_buffer(&condition_view
, &condition
);
3521 if (ret
!= expected_condition_size
) {
3522 ERR("[notification-thread] Malformed condition received from client");
3526 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3527 ret
= notification_thread_client_subscribe(
3528 client
, condition
, state
, &status
);
3530 ret
= notification_thread_client_unsubscribe(
3531 client
, condition
, state
, &status
);
3537 pthread_mutex_lock(&client
->lock
);
3538 ret
= client_send_command_reply(client
, state
, status
);
3540 ERR("[notification-thread] Failed to send reply to notification channel client");
3544 /* Set reception state to receive the next message header. */
3545 ret
= client_reset_inbound_state(client
);
3547 ERR("[notification-thread] Failed to reset client communication's inbound state");
3552 pthread_mutex_unlock(&client
->lock
);
3558 int client_dispatch_message(struct notification_client
*client
,
3559 struct notification_thread_state
*state
)
3563 if (client
->communication
.inbound
.msg_type
!=
3564 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3565 client
->communication
.inbound
.msg_type
!=
3566 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3567 !client
->validated
) {
3568 WARN("[notification-thread] client attempted a command before handshake");
3573 switch (client
->communication
.inbound
.msg_type
) {
3574 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3576 ret
= client_handle_message_unknown(client
, state
);
3579 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3581 ret
= client_handle_message_handshake(client
, state
);
3584 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3585 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3587 ret
= client_handle_message_subscription(client
,
3588 client
->communication
.inbound
.msg_type
, state
);
3598 /* Incoming data from client. */
3599 int handle_notification_thread_client_in(
3600 struct notification_thread_state
*state
, int socket
)
3603 struct notification_client
*client
;
3606 bool message_is_complete
= false;
3608 client
= get_client_from_socket(socket
, state
);
3610 /* Internal error, abort. */
3615 pthread_mutex_lock(&client
->lock
);
3616 offset
= client
->communication
.inbound
.buffer
.size
-
3617 client
->communication
.inbound
.bytes_to_receive
;
3618 if (client
->communication
.inbound
.expect_creds
) {
3619 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3620 client
->communication
.inbound
.buffer
.data
+ offset
,
3621 client
->communication
.inbound
.bytes_to_receive
,
3622 &client
->communication
.inbound
.creds
);
3624 client
->communication
.inbound
.expect_creds
= false;
3625 client
->communication
.inbound
.creds_received
= true;
3628 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3629 client
->communication
.inbound
.buffer
.data
+ offset
,
3630 client
->communication
.inbound
.bytes_to_receive
);
3632 if (recv_ret
>= 0) {
3633 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3634 message_is_complete
= client
->communication
.inbound
3635 .bytes_to_receive
== 0;
3637 pthread_mutex_unlock(&client
->lock
);
3639 goto error_disconnect_client
;
3642 if (message_is_complete
) {
3643 ret
= client_dispatch_message(client
, state
);
3646 * Only returns an error if this client must be
3649 goto error_disconnect_client
;
3654 error_disconnect_client
:
3655 pthread_mutex_lock(&client
->lock
);
3656 ret
= notification_thread_client_disconnect(client
, state
);
3657 pthread_mutex_unlock(&client
->lock
);
3661 /* Client ready to receive outgoing data. */
3662 int handle_notification_thread_client_out(
3663 struct notification_thread_state
*state
, int socket
)
3666 struct notification_client
*client
;
3667 enum client_transmission_status transmission_status
;
3669 client
= get_client_from_socket(socket
, state
);
3671 /* Internal error, abort. */
3676 pthread_mutex_lock(&client
->lock
);
3677 transmission_status
= client_flush_outgoing_queue(client
);
3678 ret
= client_handle_transmission_status(
3679 client
, transmission_status
, state
);
3680 pthread_mutex_unlock(&client
->lock
);
3689 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3690 const struct channel_state_sample
*sample
,
3691 uint64_t buffer_capacity
)
3693 bool result
= false;
3695 enum lttng_condition_type condition_type
;
3696 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3697 condition
, struct lttng_condition_buffer_usage
,
3700 if (use_condition
->threshold_bytes
.set
) {
3701 threshold
= use_condition
->threshold_bytes
.value
;
3704 * Threshold was expressed as a ratio.
3706 * TODO the threshold (in bytes) of conditions expressed
3707 * as a ratio of total buffer size could be cached to
3708 * forego this double-multiplication or it could be performed
3709 * as fixed-point math.
3711 * Note that caching should accommodates the case where the
3712 * condition applies to multiple channels (i.e. don't assume
3713 * that all channels matching my_chann* have the same size...)
3715 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3716 (double) buffer_capacity
);
3719 condition_type
= lttng_condition_get_type(condition
);
3720 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3721 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3722 threshold
, sample
->highest_usage
);
3725 * The low condition should only be triggered once _all_ of the
3726 * streams in a channel have gone below the "low" threshold.
3728 if (sample
->highest_usage
<= threshold
) {
3732 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3733 threshold
, sample
->highest_usage
);
3736 * For high buffer usage scenarios, we want to trigger whenever
3737 * _any_ of the streams has reached the "high" threshold.
3739 if (sample
->highest_usage
>= threshold
) {
3748 bool evaluate_session_consumed_size_condition(
3749 const struct lttng_condition
*condition
,
3750 uint64_t session_consumed_size
)
3753 const struct lttng_condition_session_consumed_size
*size_condition
=
3754 container_of(condition
,
3755 struct lttng_condition_session_consumed_size
,
3758 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3759 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3760 threshold
, session_consumed_size
);
3761 return session_consumed_size
>= threshold
;
3765 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3766 struct lttng_evaluation
**evaluation
,
3767 const struct notification_thread_state
*state
,
3768 const struct channel_state_sample
*previous_sample
,
3769 const struct channel_state_sample
*latest_sample
,
3770 uint64_t previous_session_consumed_total
,
3771 uint64_t latest_session_consumed_total
,
3772 struct channel_info
*channel_info
)
3775 enum lttng_condition_type condition_type
;
3776 const bool previous_sample_available
= !!previous_sample
;
3777 bool previous_sample_result
= false;
3778 bool latest_sample_result
;
3780 condition_type
= lttng_condition_get_type(condition
);
3782 switch (condition_type
) {
3783 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3784 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3785 if (caa_likely(previous_sample_available
)) {
3786 previous_sample_result
=
3787 evaluate_buffer_usage_condition(condition
,
3788 previous_sample
, channel_info
->capacity
);
3790 latest_sample_result
= evaluate_buffer_usage_condition(
3791 condition
, latest_sample
,
3792 channel_info
->capacity
);
3794 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3795 if (caa_likely(previous_sample_available
)) {
3796 previous_sample_result
=
3797 evaluate_session_consumed_size_condition(
3799 previous_session_consumed_total
);
3801 latest_sample_result
=
3802 evaluate_session_consumed_size_condition(
3804 latest_session_consumed_total
);
3807 /* Unknown condition type; internal error. */
3811 if (!latest_sample_result
||
3812 (previous_sample_result
== latest_sample_result
)) {
3814 * Only trigger on a condition evaluation transition.
3816 * NOTE: This edge-triggered logic may not be appropriate for
3817 * future condition types.
3822 if (!evaluation
|| !latest_sample_result
) {
3826 switch (condition_type
) {
3827 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3828 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3829 *evaluation
= lttng_evaluation_buffer_usage_create(
3831 latest_sample
->highest_usage
,
3832 channel_info
->capacity
);
3834 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3835 *evaluation
= lttng_evaluation_session_consumed_size_create(
3836 latest_session_consumed_total
);
3851 int client_notification_overflow(struct notification_client
*client
)
3854 const struct lttng_notification_channel_message msg
= {
3855 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3858 ASSERT_LOCKED(client
->lock
);
3860 DBG("Dropping notification addressed to client (socket fd = %i)",
3862 if (client
->communication
.outbound
.dropped_notification
) {
3864 * The client already has a "notification dropped" message
3865 * in its outgoing queue. Nothing to do since all
3866 * of those messages are coalesced.
3871 client
->communication
.outbound
.dropped_notification
= true;
3872 ret
= lttng_dynamic_buffer_append(
3873 &client
->communication
.outbound
.buffer
, &msg
,
3876 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3883 static int client_handle_transmission_status_wrapper(
3884 struct notification_client
*client
,
3885 enum client_transmission_status status
,
3888 return client_handle_transmission_status(client
, status
,
3889 (struct notification_thread_state
*) user_data
);
3893 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3894 const struct lttng_evaluation
*evaluation
,
3895 struct notification_client_list
* client_list
,
3896 struct notification_thread_state
*state
,
3897 uid_t object_uid
, gid_t object_gid
)
3899 return notification_client_list_send_evaluation(client_list
,
3900 lttng_trigger_get_const_condition(trigger
), evaluation
,
3901 lttng_trigger_get_credentials(trigger
),
3902 &(struct lttng_credentials
){
3903 .uid
= object_uid
, .gid
= object_gid
},
3904 client_handle_transmission_status_wrapper
, state
);
3908 int notification_client_list_send_evaluation(
3909 struct notification_client_list
*client_list
,
3910 const struct lttng_condition
*condition
,
3911 const struct lttng_evaluation
*evaluation
,
3912 const struct lttng_credentials
*trigger_creds
,
3913 const struct lttng_credentials
*source_object_creds
,
3914 report_client_transmission_result_cb client_report
,
3918 struct lttng_dynamic_buffer msg_buffer
;
3919 struct notification_client_list_element
*client_list_element
, *tmp
;
3920 const struct lttng_notification notification
= {
3921 .condition
= (struct lttng_condition
*) condition
,
3922 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3924 struct lttng_notification_channel_message msg_header
= {
3925 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3928 lttng_dynamic_buffer_init(&msg_buffer
);
3930 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg_header
,
3931 sizeof(msg_header
));
3936 ret
= lttng_notification_serialize(¬ification
, &msg_buffer
);
3938 ERR("[notification-thread] Failed to serialize notification");
3943 /* Update payload size. */
3944 ((struct lttng_notification_channel_message
* ) msg_buffer
.data
)->size
=
3945 (uint32_t) (msg_buffer
.size
- sizeof(msg_header
));
3947 pthread_mutex_lock(&client_list
->lock
);
3948 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3949 &client_list
->list
, node
) {
3950 enum client_transmission_status transmission_status
;
3951 struct notification_client
*client
=
3952 client_list_element
->client
;
3955 pthread_mutex_lock(&client
->lock
);
3956 if (source_object_creds
) {
3957 if (client
->uid
!= source_object_creds
->uid
&&
3958 client
->gid
!= source_object_creds
->gid
&&
3961 * Client is not allowed to monitor this
3964 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
3969 /* TODO: what is the behavior for root client on non root
3970 * trigger? Since multiple triggers (different user) can have the same condition
3971 * but with different action group that can have each a notify.
3972 * Does the root client receive multiple notification for all
3973 * those triggers with the same condition or only notification
3974 * for triggers the root user configured?
3975 * For now we do the later. All users including the root user
3976 * can only receive notification from trigger it registered.
3978 if (client
->uid
!= trigger_creds
->uid
&& client
->gid
!= trigger_creds
->gid
) {
3979 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
3983 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3984 client
->socket
, msg_buffer
.size
);
3985 if (client
->communication
.outbound
.buffer
.size
) {
3987 * Outgoing data is already buffered for this client;
3988 * drop the notification and enqueue a "dropped
3989 * notification" message if this is the first dropped
3990 * notification since the socket spilled-over to the
3993 ret
= client_notification_overflow(client
);
3999 ret
= lttng_dynamic_buffer_append_buffer(
4000 &client
->communication
.outbound
.buffer
,
4006 transmission_status
= client_flush_outgoing_queue(client
);
4007 ret
= client_report(client
, transmission_status
, user_data
);
4012 pthread_mutex_unlock(&client
->lock
);
4014 goto end_unlock_list
;
4020 pthread_mutex_unlock(&client_list
->lock
);
4022 lttng_dynamic_buffer_reset(&msg_buffer
);
4026 static struct lttng_trigger_notification
*receive_notification(int pipe
,
4027 enum lttng_domain_type domain
)
4031 struct lttng_trigger_notification
*notification
= NULL
;
4032 char *capture_buffer
= NULL
;
4033 size_t capture_buffer_size
;
4034 void *reception_buffer
;
4035 size_t reception_size
;
4037 struct lttng_ust_trigger_notification ust_notification
;
4038 struct lttng_kernel_trigger_notification kernel_notification
;
4040 /* Init lttng_trigger_notification */
4043 case LTTNG_DOMAIN_UST
:
4044 reception_buffer
= (void *) &ust_notification
;
4045 reception_size
= sizeof(ust_notification
);
4047 case LTTNG_DOMAIN_KERNEL
:
4048 reception_buffer
= (void *) &kernel_notification
;
4049 reception_size
= sizeof(kernel_notification
);
4056 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4057 * ensuring that read/write of sampling messages are atomic.
4059 /* TODO: should we read as much as we can ? EWOULDBLOCK? */
4061 ret
= lttng_read(pipe
, reception_buffer
, reception_size
);
4062 if (ret
!= reception_size
) {
4063 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4065 /* TODO: Should this error out completly.
4066 * This can happen when an app is killed as of today
4067 * ret = -1 cause the whole thread to die and fuck up
4074 case LTTNG_DOMAIN_UST
:
4075 id
= ust_notification
.id
;
4076 capture_buffer_size
=
4077 ust_notification
.capture_buf_size
;
4079 case LTTNG_DOMAIN_KERNEL
:
4080 id
= kernel_notification
.id
;
4081 capture_buffer_size
=
4082 kernel_notification
.capture_buf_size
;
4088 if (capture_buffer_size
== 0) {
4089 capture_buffer
= NULL
;
4093 capture_buffer
= malloc(capture_buffer_size
);
4094 if (!capture_buffer
) {
4095 ERR("[notification-thread] Failed to allocate capture buffer");
4100 * Fetch additional payload (capture).
4102 ret
= lttng_read(pipe
, capture_buffer
, capture_buffer_size
);
4103 if (ret
!= capture_buffer_size
) {
4104 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4106 /* TODO: Should this error out completly.
4107 * This can happen when an app is killed as of today
4108 * ret = -1 cause the whole thread to die and fuck up
4115 notification
= lttng_trigger_notification_create(
4116 id
, domain
, capture_buffer
, capture_buffer_size
);
4117 if (notification
== NULL
) {
4121 /* Ownership transfered to the lttng_trigger_notification object */
4122 capture_buffer
= NULL
;
4125 free(capture_buffer
);
4126 return notification
;
4129 int handle_notification_thread_event(struct notification_thread_state
*state
,
4131 enum lttng_domain_type domain
)
4134 struct cds_lfht_node
*node
;
4135 struct cds_lfht_iter iter
;
4136 struct notification_trigger_tokens_ht_element
*element
;
4137 struct lttng_trigger_notification
*notification
= NULL
;
4138 enum action_executor_status executor_status
;
4139 struct notification_client_list
*client_list
= NULL
;
4141 notification
= receive_notification(pipe
, domain
);
4142 if (notification
== NULL
) {
4143 ERR("[notification-thread] Error receiving notification from tracer "
4144 "(fd = %i, domain = %s)",
4145 pipe
, lttng_domain_type_str(domain
));
4150 /* Find triggers associated with this token. */
4152 cds_lfht_lookup(state
->trigger_tokens_ht
,
4153 hash_key_u64(¬ification
->id
, lttng_ht_seed
),
4154 match_trigger_token
, ¬ification
->id
, &iter
);
4155 node
= cds_lfht_iter_get_node(&iter
);
4156 if (caa_unlikely(!node
)) {
4157 /* TODO: is this an error? This might happen if the receive side
4158 * is slow to process event from source and that the trigger was
4159 * removed but the app still kicking. This yield another
4160 * question on the trigger lifetime and when we can remove a
4161 * trigger. How to guarantee that all event with the token idea
4162 * have be processed? Do we want to provide this guarantee?
4164 * Update: I have encountered this when using a trigger on
4165 * sched_switch and then removing it. The frequency is quite
4166 * high hence we en up exactly in the mentionned scenario.
4167 * AFAIK this might be the best way to handle this.
4172 element
= caa_container_of(node
,
4173 struct notification_trigger_tokens_ht_element
,
4176 if (!lttng_trigger_is_ready_to_fire(element
->trigger
)) {
4181 client_list
= get_client_list_from_condition(state
,
4182 lttng_trigger_get_const_condition(element
->trigger
));
4183 executor_status
= action_executor_enqueue(state
->executor
,
4184 element
->trigger
, client_list
, notification
);
4186 /* Notification ownership passed to the executor. */
4187 notification
= NULL
;
4189 switch (executor_status
) {
4190 case ACTION_EXECUTOR_STATUS_OK
:
4193 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4195 struct notification_client_list_element
*client_list_element
,
4199 * Not a fatal error; this is expected and simply means the
4200 * executor has too much work queued already.
4208 /* Warn clients that a notification (or more) was dropped. */
4209 pthread_mutex_lock(&client_list
->lock
);
4210 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4211 &client_list
->list
, node
) {
4212 enum client_transmission_status transmission_status
;
4213 struct notification_client
*client
=
4214 client_list_element
->client
;
4216 pthread_mutex_lock(&client
->lock
);
4217 ret
= client_notification_overflow(client
);
4223 transmission_status
=
4224 client_flush_outgoing_queue(client
);
4225 ret
= client_handle_transmission_status(
4226 client
, transmission_status
, state
);
4232 pthread_mutex_unlock(&client
->lock
);
4237 pthread_mutex_lock(&client_list
->lock
);
4240 case ACTION_EXECUTOR_STATUS_ERROR
:
4241 /* Fatal error, shut down everything. */
4242 ERR("Fatal error encoutered while enqueuing action");
4246 /* Unhandled error. */
4251 lttng_trigger_notification_destroy(notification
);
4252 notification_client_list_put(client_list
);
4258 int handle_notification_thread_channel_sample(
4259 struct notification_thread_state
*state
, int pipe
,
4260 enum lttng_domain_type domain
)
4263 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4264 struct channel_info
*channel_info
;
4265 struct cds_lfht_node
*node
;
4266 struct cds_lfht_iter iter
;
4267 struct lttng_channel_trigger_list
*trigger_list
;
4268 struct lttng_trigger_list_element
*trigger_list_element
;
4269 bool previous_sample_available
= false;
4270 struct channel_state_sample previous_sample
, latest_sample
;
4271 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4274 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4275 * ensuring that read/write of sampling messages are atomic.
4277 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4278 if (ret
!= sizeof(sample_msg
)) {
4279 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4286 latest_sample
.key
.key
= sample_msg
.key
;
4287 latest_sample
.key
.domain
= domain
;
4288 latest_sample
.highest_usage
= sample_msg
.highest
;
4289 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4290 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4294 /* Retrieve the channel's informations */
4295 cds_lfht_lookup(state
->channels_ht
,
4296 hash_channel_key(&latest_sample
.key
),
4300 node
= cds_lfht_iter_get_node(&iter
);
4301 if (caa_unlikely(!node
)) {
4303 * Not an error since the consumer can push a sample to the pipe
4304 * and the rest of the session daemon could notify us of the
4305 * channel's destruction before we get a chance to process that
4308 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
4310 latest_sample
.key
.key
,
4311 lttng_domain_type_str(domain
));
4314 channel_info
= caa_container_of(node
, struct channel_info
,
4316 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4318 latest_sample
.key
.key
,
4319 channel_info
->session_info
->name
,
4320 latest_sample
.highest_usage
,
4321 latest_sample
.lowest_usage
,
4322 latest_sample
.channel_total_consumed
);
4324 previous_session_consumed_total
=
4325 channel_info
->session_info
->consumed_data_size
;
4327 /* Retrieve the channel's last sample, if it exists, and update it. */
4328 cds_lfht_lookup(state
->channel_state_ht
,
4329 hash_channel_key(&latest_sample
.key
),
4330 match_channel_state_sample
,
4333 node
= cds_lfht_iter_get_node(&iter
);
4334 if (caa_likely(node
)) {
4335 struct channel_state_sample
*stored_sample
;
4337 /* Update the sample stored. */
4338 stored_sample
= caa_container_of(node
,
4339 struct channel_state_sample
,
4340 channel_state_ht_node
);
4342 memcpy(&previous_sample
, stored_sample
,
4343 sizeof(previous_sample
));
4344 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4345 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4346 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4347 previous_sample_available
= true;
4349 latest_session_consumed_total
=
4350 previous_session_consumed_total
+
4351 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4354 * This is the channel's first sample, allocate space for and
4355 * store the new sample.
4357 struct channel_state_sample
*stored_sample
;
4359 stored_sample
= zmalloc(sizeof(*stored_sample
));
4360 if (!stored_sample
) {
4365 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4366 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4367 cds_lfht_add(state
->channel_state_ht
,
4368 hash_channel_key(&stored_sample
->key
),
4369 &stored_sample
->channel_state_ht_node
);
4371 latest_session_consumed_total
=
4372 previous_session_consumed_total
+
4373 latest_sample
.channel_total_consumed
;
4376 channel_info
->session_info
->consumed_data_size
=
4377 latest_session_consumed_total
;
4379 /* Find triggers associated with this channel. */
4380 cds_lfht_lookup(state
->channel_triggers_ht
,
4381 hash_channel_key(&latest_sample
.key
),
4382 match_channel_trigger_list
,
4385 node
= cds_lfht_iter_get_node(&iter
);
4386 if (caa_likely(!node
)) {
4390 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4391 channel_triggers_ht_node
);
4392 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4394 const struct lttng_condition
*condition
;
4395 const struct lttng_action
*action
;
4396 struct lttng_trigger
*trigger
;
4397 struct notification_client_list
*client_list
= NULL
;
4398 struct lttng_evaluation
*evaluation
= NULL
;
4399 bool client_list_is_empty
;
4402 trigger
= trigger_list_element
->trigger
;
4403 condition
= lttng_trigger_get_const_condition(trigger
);
4405 action
= lttng_trigger_get_const_action(trigger
);
4407 if (!lttng_trigger_is_ready_to_fire(trigger
)) {
4411 /* Notify actions are the only type currently supported. */
4412 /* TODO support other type of action */
4413 assert(lttng_action_get_type_const(action
) ==
4414 LTTNG_ACTION_TYPE_NOTIFY
);
4417 * Check if any client is subscribed to the result of this
4420 client_list
= get_client_list_from_condition(state
, condition
);
4421 assert(client_list
);
4422 client_list_is_empty
= cds_list_empty(&client_list
->list
);
4423 if (client_list_is_empty
) {
4425 * No clients interested in the evaluation's result,
4431 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4432 previous_sample_available
? &previous_sample
: NULL
,
4434 previous_session_consumed_total
,
4435 latest_session_consumed_total
,
4437 if (caa_unlikely(ret
)) {
4441 if (caa_likely(!evaluation
)) {
4445 /* Dispatch evaluation result to all clients. */
4446 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
4447 evaluation
, client_list
, state
,
4448 channel_info
->session_info
->uid
,
4449 channel_info
->session_info
->gid
);
4450 lttng_evaluation_destroy(evaluation
);
4452 notification_client_list_put(client_list
);
4453 if (caa_unlikely(ret
)) {