2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.h"
12 #include <urcu/rculfhash.h>
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/futex.h>
17 #include <common/unix.h>
18 #include <common/dynamic-buffer.h>
19 #include <common/hashtable/utils.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/macros.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.h>
24 #include <lttng/notification/notification-internal.h>
25 #include <lttng/condition/condition-internal.h>
26 #include <lttng/condition/buffer-usage-internal.h>
27 #include <lttng/condition/session-consumed-size-internal.h>
28 #include <lttng/condition/session-rotation-internal.h>
29 #include <lttng/notification/channel-internal.h>
30 #include <lttng/trigger/trigger-internal.h>
38 #include "notification-thread.h"
39 #include "notification-thread-events.h"
40 #include "notification-thread-commands.h"
41 #include "lttng-sessiond.h"
44 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
45 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
47 enum lttng_object_type
{
48 LTTNG_OBJECT_TYPE_UNKNOWN
,
49 LTTNG_OBJECT_TYPE_NONE
,
50 LTTNG_OBJECT_TYPE_CHANNEL
,
51 LTTNG_OBJECT_TYPE_SESSION
,
54 struct lttng_trigger_list_element
{
55 /* No ownership of the trigger object is assumed. */
56 struct lttng_trigger
*trigger
;
57 struct cds_list_head node
;
60 struct lttng_channel_trigger_list
{
61 struct channel_key channel_key
;
62 /* List of struct lttng_trigger_list_element. */
63 struct cds_list_head list
;
64 /* Node in the channel_triggers_ht */
65 struct cds_lfht_node channel_triggers_ht_node
;
66 /* call_rcu delayed reclaim. */
67 struct rcu_head rcu_node
;
71 * List of triggers applying to a given session.
74 * - lttng_session_trigger_list_create()
75 * - lttng_session_trigger_list_build()
76 * - lttng_session_trigger_list_destroy()
77 * - lttng_session_trigger_list_add()
79 struct lttng_session_trigger_list
{
81 * Not owned by this; points to the session_info structure's
84 const char *session_name
;
85 /* List of struct lttng_trigger_list_element. */
86 struct cds_list_head list
;
87 /* Node in the session_triggers_ht */
88 struct cds_lfht_node session_triggers_ht_node
;
90 * Weak reference to the notification system's session triggers
93 * The session trigger list structure structure is owned by
94 * the session's session_info.
96 * The session_info is kept alive the the channel_infos holding a
97 * reference to it (reference counting). When those channels are
98 * destroyed (at runtime or on teardown), the reference they hold
99 * to the session_info are released. On destruction of session_info,
100 * session_info_destroy() will remove the list of triggers applying
101 * to this session from the notification system's state.
103 * This implies that the session_triggers_ht must be destroyed
104 * after the channels.
106 struct cds_lfht
*session_triggers_ht
;
107 /* Used for delayed RCU reclaim. */
108 struct rcu_head rcu_node
;
111 struct lttng_trigger_ht_element
{
112 struct lttng_trigger
*trigger
;
113 struct cds_lfht_node node
;
114 /* call_rcu delayed reclaim. */
115 struct rcu_head rcu_node
;
118 struct lttng_condition_list_element
{
119 struct lttng_condition
*condition
;
120 struct cds_list_head node
;
123 struct channel_state_sample
{
124 struct channel_key key
;
125 struct cds_lfht_node channel_state_ht_node
;
126 uint64_t highest_usage
;
127 uint64_t lowest_usage
;
128 uint64_t channel_total_consumed
;
129 /* call_rcu delayed reclaim. */
130 struct rcu_head rcu_node
;
133 static unsigned long hash_channel_key(struct channel_key
*key
);
134 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
135 struct lttng_evaluation
**evaluation
,
136 const struct notification_thread_state
*state
,
137 const struct channel_state_sample
*previous_sample
,
138 const struct channel_state_sample
*latest_sample
,
139 uint64_t previous_session_consumed_total
,
140 uint64_t latest_session_consumed_total
,
141 struct channel_info
*channel_info
);
143 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
144 const struct lttng_evaluation
*evaluation
,
145 struct notification_client_list
*client_list
,
146 struct notification_thread_state
*state
,
147 uid_t channel_uid
, gid_t channel_gid
);
150 /* session_info API */
152 void session_info_destroy(void *_data
);
154 void session_info_get(struct session_info
*session_info
);
156 void session_info_put(struct session_info
*session_info
);
158 struct session_info
*session_info_create(const char *name
,
159 uid_t uid
, gid_t gid
,
160 struct lttng_session_trigger_list
*trigger_list
,
161 struct cds_lfht
*sessions_ht
);
163 void session_info_add_channel(struct session_info
*session_info
,
164 struct channel_info
*channel_info
);
166 void session_info_remove_channel(struct session_info
*session_info
,
167 struct channel_info
*channel_info
);
169 /* lttng_session_trigger_list API */
171 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
172 const char *session_name
,
173 struct cds_lfht
*session_triggers_ht
);
175 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
176 const struct notification_thread_state
*state
,
177 const char *session_name
);
179 void lttng_session_trigger_list_destroy(
180 struct lttng_session_trigger_list
*list
);
182 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
183 struct lttng_trigger
*trigger
);
186 int client_handle_transmission_status(
187 struct notification_client
*client
,
188 enum client_transmission_status transmission_status
,
189 struct notification_thread_state
*state
);
192 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
194 /* This double-cast is intended to supress pointer-to-cast warning. */
195 const int socket
= (int) (intptr_t) key
;
196 const struct notification_client
*client
= caa_container_of(node
,
197 struct notification_client
, client_socket_ht_node
);
199 return client
->socket
== socket
;
203 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
205 /* This double-cast is intended to supress pointer-to-cast warning. */
206 const notification_client_id id
= *((notification_client_id
*) key
);
207 const struct notification_client
*client
= caa_container_of(
208 node
, struct notification_client
, client_id_ht_node
);
210 return client
->id
== id
;
214 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
216 struct channel_key
*channel_key
= (struct channel_key
*) key
;
217 struct lttng_channel_trigger_list
*trigger_list
;
219 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
220 channel_triggers_ht_node
);
222 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
223 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
227 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
229 const char *session_name
= (const char *) key
;
230 struct lttng_session_trigger_list
*trigger_list
;
232 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
233 session_triggers_ht_node
);
235 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
239 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
241 struct channel_key
*channel_key
= (struct channel_key
*) key
;
242 struct channel_state_sample
*sample
;
244 sample
= caa_container_of(node
, struct channel_state_sample
,
245 channel_state_ht_node
);
247 return !!((channel_key
->key
== sample
->key
.key
) &&
248 (channel_key
->domain
== sample
->key
.domain
));
252 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
254 struct channel_key
*channel_key
= (struct channel_key
*) key
;
255 struct channel_info
*channel_info
;
257 channel_info
= caa_container_of(node
, struct channel_info
,
260 return !!((channel_key
->key
== channel_info
->key
.key
) &&
261 (channel_key
->domain
== channel_info
->key
.domain
));
265 int match_condition(struct cds_lfht_node
*node
, const void *key
)
267 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
268 struct lttng_trigger_ht_element
*trigger
;
269 struct lttng_condition
*condition
;
271 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
273 condition
= lttng_trigger_get_condition(trigger
->trigger
);
276 return !!lttng_condition_is_equal(condition_key
, condition
);
280 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
282 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
283 struct notification_client_list
*client_list
;
284 const struct lttng_condition
*condition
;
286 assert(condition_key
);
288 client_list
= caa_container_of(node
, struct notification_client_list
,
289 notification_trigger_clients_ht_node
);
290 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
292 return !!lttng_condition_is_equal(condition_key
, condition
);
296 int match_session(struct cds_lfht_node
*node
, const void *key
)
298 const char *name
= key
;
299 struct session_info
*session_info
= caa_container_of(
300 node
, struct session_info
, sessions_ht_node
);
302 return !strcmp(session_info
->name
, name
);
306 unsigned long lttng_condition_buffer_usage_hash(
307 const struct lttng_condition
*_condition
)
310 unsigned long condition_type
;
311 struct lttng_condition_buffer_usage
*condition
;
313 condition
= container_of(_condition
,
314 struct lttng_condition_buffer_usage
, parent
);
316 condition_type
= (unsigned long) condition
->parent
.type
;
317 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
318 if (condition
->session_name
) {
319 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
321 if (condition
->channel_name
) {
322 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
324 if (condition
->domain
.set
) {
325 hash
^= hash_key_ulong(
326 (void *) condition
->domain
.type
,
329 if (condition
->threshold_ratio
.set
) {
332 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
333 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
334 } else if (condition
->threshold_bytes
.set
) {
337 val
= condition
->threshold_bytes
.value
;
338 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
344 unsigned long lttng_condition_session_consumed_size_hash(
345 const struct lttng_condition
*_condition
)
348 unsigned long condition_type
=
349 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
350 struct lttng_condition_session_consumed_size
*condition
;
353 condition
= container_of(_condition
,
354 struct lttng_condition_session_consumed_size
, parent
);
356 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
357 if (condition
->session_name
) {
358 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
360 val
= condition
->consumed_threshold_bytes
.value
;
361 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
366 unsigned long lttng_condition_session_rotation_hash(
367 const struct lttng_condition
*_condition
)
369 unsigned long hash
, condition_type
;
370 struct lttng_condition_session_rotation
*condition
;
372 condition
= container_of(_condition
,
373 struct lttng_condition_session_rotation
, parent
);
374 condition_type
= (unsigned long) condition
->parent
.type
;
375 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
376 assert(condition
->session_name
);
377 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
382 * The lttng_condition hashing code is kept in this file (rather than
383 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
384 * don't want to link in liblttng-ctl.
387 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
389 switch (condition
->type
) {
390 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
391 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
392 return lttng_condition_buffer_usage_hash(condition
);
393 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
394 return lttng_condition_session_consumed_size_hash(condition
);
395 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
396 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
397 return lttng_condition_session_rotation_hash(condition
);
399 ERR("[notification-thread] Unexpected condition type caught");
405 unsigned long hash_channel_key(struct channel_key
*key
)
407 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
408 unsigned long domain_hash
= hash_key_ulong(
409 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
411 return key_hash
^ domain_hash
;
415 unsigned long hash_client_socket(int socket
)
417 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
421 unsigned long hash_client_id(notification_client_id id
)
423 return hash_key_u64(&id
, lttng_ht_seed
);
427 * Get the type of object to which a given condition applies. Bindings let
428 * the notification system evaluate a trigger's condition when a given
429 * object's state is updated.
431 * For instance, a condition bound to a channel will be evaluated everytime
432 * the channel's state is changed by a channel monitoring sample.
435 enum lttng_object_type
get_condition_binding_object(
436 const struct lttng_condition
*condition
)
438 switch (lttng_condition_get_type(condition
)) {
439 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
440 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
441 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
442 return LTTNG_OBJECT_TYPE_CHANNEL
;
443 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
444 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
445 return LTTNG_OBJECT_TYPE_SESSION
;
447 return LTTNG_OBJECT_TYPE_UNKNOWN
;
452 void free_channel_info_rcu(struct rcu_head
*node
)
454 free(caa_container_of(node
, struct channel_info
, rcu_node
));
458 void channel_info_destroy(struct channel_info
*channel_info
)
464 if (channel_info
->session_info
) {
465 session_info_remove_channel(channel_info
->session_info
,
467 session_info_put(channel_info
->session_info
);
469 if (channel_info
->name
) {
470 free(channel_info
->name
);
472 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
476 void free_session_info_rcu(struct rcu_head
*node
)
478 free(caa_container_of(node
, struct session_info
, rcu_node
));
481 /* Don't call directly, use the ref-counting mechanism. */
483 void session_info_destroy(void *_data
)
485 struct session_info
*session_info
= _data
;
488 assert(session_info
);
489 if (session_info
->channel_infos_ht
) {
490 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
492 ERR("[notification-thread] Failed to destroy channel information hash table");
495 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
498 cds_lfht_del(session_info
->sessions_ht
,
499 &session_info
->sessions_ht_node
);
501 free(session_info
->name
);
502 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
506 void session_info_get(struct session_info
*session_info
)
511 lttng_ref_get(&session_info
->ref
);
515 void session_info_put(struct session_info
*session_info
)
520 lttng_ref_put(&session_info
->ref
);
524 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
525 struct lttng_session_trigger_list
*trigger_list
,
526 struct cds_lfht
*sessions_ht
)
528 struct session_info
*session_info
;
532 session_info
= zmalloc(sizeof(*session_info
));
536 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
538 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
539 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
540 if (!session_info
->channel_infos_ht
) {
544 cds_lfht_node_init(&session_info
->sessions_ht_node
);
545 session_info
->name
= strdup(name
);
546 if (!session_info
->name
) {
549 session_info
->uid
= uid
;
550 session_info
->gid
= gid
;
551 session_info
->trigger_list
= trigger_list
;
552 session_info
->sessions_ht
= sessions_ht
;
556 session_info_put(session_info
);
561 void session_info_add_channel(struct session_info
*session_info
,
562 struct channel_info
*channel_info
)
565 cds_lfht_add(session_info
->channel_infos_ht
,
566 hash_channel_key(&channel_info
->key
),
567 &channel_info
->session_info_channels_ht_node
);
572 void session_info_remove_channel(struct session_info
*session_info
,
573 struct channel_info
*channel_info
)
576 cds_lfht_del(session_info
->channel_infos_ht
,
577 &channel_info
->session_info_channels_ht_node
);
582 struct channel_info
*channel_info_create(const char *channel_name
,
583 struct channel_key
*channel_key
, uint64_t channel_capacity
,
584 struct session_info
*session_info
)
586 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
592 cds_lfht_node_init(&channel_info
->channels_ht_node
);
593 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
594 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
595 channel_info
->capacity
= channel_capacity
;
597 channel_info
->name
= strdup(channel_name
);
598 if (!channel_info
->name
) {
603 * Set the references between session and channel infos:
604 * - channel_info holds a strong reference to session_info
605 * - session_info holds a weak reference to channel_info
607 session_info_get(session_info
);
608 session_info_add_channel(session_info
, channel_info
);
609 channel_info
->session_info
= session_info
;
613 channel_info_destroy(channel_info
);
618 bool notification_client_list_get(struct notification_client_list
*list
)
620 return urcu_ref_get_unless_zero(&list
->ref
);
624 void free_notification_client_list_rcu(struct rcu_head
*node
)
626 free(caa_container_of(node
, struct notification_client_list
,
631 void notification_client_list_release(struct urcu_ref
*list_ref
)
633 struct notification_client_list
*list
=
634 container_of(list_ref
, typeof(*list
), ref
);
635 struct notification_client_list_element
*client_list_element
, *tmp
;
637 if (list
->notification_trigger_clients_ht
) {
639 cds_lfht_del(list
->notification_trigger_clients_ht
,
640 &list
->notification_trigger_clients_ht_node
);
642 list
->notification_trigger_clients_ht
= NULL
;
644 cds_list_for_each_entry_safe(client_list_element
, tmp
,
646 free(client_list_element
);
648 pthread_mutex_destroy(&list
->lock
);
649 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
653 struct notification_client_list
*notification_client_list_create(
654 const struct lttng_trigger
*trigger
)
656 struct notification_client_list
*client_list
=
657 zmalloc(sizeof(*client_list
));
662 pthread_mutex_init(&client_list
->lock
, NULL
);
663 urcu_ref_init(&client_list
->ref
);
664 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
665 CDS_INIT_LIST_HEAD(&client_list
->list
);
666 client_list
->trigger
= trigger
;
672 void publish_notification_client_list(
673 struct notification_thread_state
*state
,
674 struct notification_client_list
*list
)
676 const struct lttng_condition
*condition
=
677 lttng_trigger_get_const_condition(list
->trigger
);
679 assert(!list
->notification_trigger_clients_ht
);
680 notification_client_list_get(list
);
682 list
->notification_trigger_clients_ht
=
683 state
->notification_trigger_clients_ht
;
686 cds_lfht_add(state
->notification_trigger_clients_ht
,
687 lttng_condition_hash(condition
),
688 &list
->notification_trigger_clients_ht_node
);
693 void notification_client_list_put(struct notification_client_list
*list
)
698 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
701 /* Provides a reference to the returned list. */
703 struct notification_client_list
*get_client_list_from_condition(
704 struct notification_thread_state
*state
,
705 const struct lttng_condition
*condition
)
707 struct cds_lfht_node
*node
;
708 struct cds_lfht_iter iter
;
709 struct notification_client_list
*list
= NULL
;
712 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
713 lttng_condition_hash(condition
),
714 match_client_list_condition
,
717 node
= cds_lfht_iter_get_node(&iter
);
719 list
= container_of(node
, struct notification_client_list
,
720 notification_trigger_clients_ht_node
);
721 list
= notification_client_list_get(list
) ? list
: NULL
;
729 int evaluate_channel_condition_for_client(
730 const struct lttng_condition
*condition
,
731 struct notification_thread_state
*state
,
732 struct lttng_evaluation
**evaluation
,
733 uid_t
*session_uid
, gid_t
*session_gid
)
736 struct cds_lfht_iter iter
;
737 struct cds_lfht_node
*node
;
738 struct channel_info
*channel_info
= NULL
;
739 struct channel_key
*channel_key
= NULL
;
740 struct channel_state_sample
*last_sample
= NULL
;
741 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
745 /* Find the channel associated with the condition. */
746 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
747 channel_trigger_list
, channel_triggers_ht_node
) {
748 struct lttng_trigger_list_element
*element
;
750 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
751 const struct lttng_condition
*current_condition
=
752 lttng_trigger_get_const_condition(
755 assert(current_condition
);
756 if (!lttng_condition_is_equal(condition
,
757 current_condition
)) {
761 /* Found the trigger, save the channel key. */
762 channel_key
= &channel_trigger_list
->channel_key
;
766 /* The channel key was found stop iteration. */
772 /* No channel found; normal exit. */
773 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
778 /* Fetch channel info for the matching channel. */
779 cds_lfht_lookup(state
->channels_ht
,
780 hash_channel_key(channel_key
),
784 node
= cds_lfht_iter_get_node(&iter
);
786 channel_info
= caa_container_of(node
, struct channel_info
,
789 /* Retrieve the channel's last sample, if it exists. */
790 cds_lfht_lookup(state
->channel_state_ht
,
791 hash_channel_key(channel_key
),
792 match_channel_state_sample
,
795 node
= cds_lfht_iter_get_node(&iter
);
797 last_sample
= caa_container_of(node
,
798 struct channel_state_sample
,
799 channel_state_ht_node
);
801 /* Nothing to evaluate, no sample was ever taken. Normal exit */
802 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
807 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
809 0, channel_info
->session_info
->consumed_data_size
,
812 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
816 *session_uid
= channel_info
->session_info
->uid
;
817 *session_gid
= channel_info
->session_info
->gid
;
824 const char *get_condition_session_name(const struct lttng_condition
*condition
)
826 const char *session_name
= NULL
;
827 enum lttng_condition_status status
;
829 switch (lttng_condition_get_type(condition
)) {
830 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
831 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
832 status
= lttng_condition_buffer_usage_get_session_name(
833 condition
, &session_name
);
835 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
836 status
= lttng_condition_session_consumed_size_get_session_name(
837 condition
, &session_name
);
839 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
840 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
841 status
= lttng_condition_session_rotation_get_session_name(
842 condition
, &session_name
);
847 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
848 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
856 int evaluate_session_condition_for_client(
857 const struct lttng_condition
*condition
,
858 struct notification_thread_state
*state
,
859 struct lttng_evaluation
**evaluation
,
860 uid_t
*session_uid
, gid_t
*session_gid
)
863 struct cds_lfht_iter iter
;
864 struct cds_lfht_node
*node
;
865 const char *session_name
;
866 struct session_info
*session_info
= NULL
;
869 session_name
= get_condition_session_name(condition
);
871 /* Find the session associated with the trigger. */
872 cds_lfht_lookup(state
->sessions_ht
,
873 hash_key_str(session_name
, lttng_ht_seed
),
877 node
= cds_lfht_iter_get_node(&iter
);
879 DBG("[notification-thread] No known session matching name \"%s\"",
885 session_info
= caa_container_of(node
, struct session_info
,
887 session_info_get(session_info
);
890 * Evaluation is performed in-line here since only one type of
891 * session-bound condition is handled for the moment.
893 switch (lttng_condition_get_type(condition
)) {
894 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
895 if (!session_info
->rotation
.ongoing
) {
897 goto end_session_put
;
900 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
901 session_info
->rotation
.id
);
904 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
907 goto end_session_put
;
913 goto end_session_put
;
916 *session_uid
= session_info
->uid
;
917 *session_gid
= session_info
->gid
;
920 session_info_put(session_info
);
927 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
928 const struct lttng_condition
*condition
,
929 struct notification_client
*client
,
930 struct notification_thread_state
*state
)
933 struct lttng_evaluation
*evaluation
= NULL
;
934 struct notification_client_list client_list
= {
935 .lock
= PTHREAD_MUTEX_INITIALIZER
,
937 struct notification_client_list_element client_list_element
= { 0 };
938 uid_t object_uid
= 0;
939 gid_t object_gid
= 0;
946 switch (get_condition_binding_object(condition
)) {
947 case LTTNG_OBJECT_TYPE_SESSION
:
948 ret
= evaluate_session_condition_for_client(condition
, state
,
949 &evaluation
, &object_uid
, &object_gid
);
951 case LTTNG_OBJECT_TYPE_CHANNEL
:
952 ret
= evaluate_channel_condition_for_client(condition
, state
,
953 &evaluation
, &object_uid
, &object_gid
);
955 case LTTNG_OBJECT_TYPE_NONE
:
958 case LTTNG_OBJECT_TYPE_UNKNOWN
:
968 /* Evaluation yielded nothing. Normal exit. */
969 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
975 * Create a temporary client list with the client currently
978 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
979 CDS_INIT_LIST_HEAD(&client_list
.list
);
980 client_list
.trigger
= trigger
;
982 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
983 client_list_element
.client
= client
;
984 cds_list_add(&client_list_element
.node
, &client_list
.list
);
986 /* Send evaluation result to the newly-subscribed client. */
987 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
988 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
989 state
, object_uid
, object_gid
);
996 int notification_thread_client_subscribe(struct notification_client
*client
,
997 struct lttng_condition
*condition
,
998 struct notification_thread_state
*state
,
999 enum lttng_notification_channel_status
*_status
)
1002 struct notification_client_list
*client_list
= NULL
;
1003 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1004 struct notification_client_list_element
*client_list_element
= NULL
;
1005 enum lttng_notification_channel_status status
=
1006 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1009 * Ensure that the client has not already subscribed to this condition
1012 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1013 if (lttng_condition_is_equal(condition_list_element
->condition
,
1015 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1020 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1021 if (!condition_list_element
) {
1025 client_list_element
= zmalloc(sizeof(*client_list_element
));
1026 if (!client_list_element
) {
1032 * Add the newly-subscribed condition to the client's subscription list.
1034 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1035 condition_list_element
->condition
= condition
;
1036 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1038 client_list
= get_client_list_from_condition(state
, condition
);
1041 * No notification-emiting trigger registered with this
1042 * condition. We don't evaluate the condition right away
1043 * since this trigger is not registered yet.
1045 free(client_list_element
);
1050 * The condition to which the client just subscribed is evaluated
1051 * at this point so that conditions that are already TRUE result
1052 * in a notification being sent out.
1054 * The client_list's trigger is used without locking the list itself.
1055 * This is correct since the list doesn't own the trigger and the
1056 * object is immutable.
1058 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1060 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1062 free(client_list_element
);
1067 * Add the client to the list of clients interested in a given trigger
1068 * if a "notification" trigger with a corresponding condition was
1071 client_list_element
->client
= client
;
1072 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1074 pthread_mutex_lock(&client_list
->lock
);
1075 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1076 pthread_mutex_unlock(&client_list
->lock
);
1082 notification_client_list_put(client_list
);
1086 free(condition_list_element
);
1087 free(client_list_element
);
1092 int notification_thread_client_unsubscribe(
1093 struct notification_client
*client
,
1094 struct lttng_condition
*condition
,
1095 struct notification_thread_state
*state
,
1096 enum lttng_notification_channel_status
*_status
)
1098 struct notification_client_list
*client_list
;
1099 struct lttng_condition_list_element
*condition_list_element
,
1101 struct notification_client_list_element
*client_list_element
,
1103 bool condition_found
= false;
1104 enum lttng_notification_channel_status status
=
1105 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1107 /* Remove the condition from the client's condition list. */
1108 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1109 &client
->condition_list
, node
) {
1110 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1115 cds_list_del(&condition_list_element
->node
);
1117 * The caller may be iterating on the client's conditions to
1118 * tear down a client's connection. In this case, the condition
1119 * will be destroyed at the end.
1121 if (condition
!= condition_list_element
->condition
) {
1122 lttng_condition_destroy(
1123 condition_list_element
->condition
);
1125 free(condition_list_element
);
1126 condition_found
= true;
1130 if (!condition_found
) {
1131 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1136 * Remove the client from the list of clients interested the trigger
1137 * matching the condition.
1139 client_list
= get_client_list_from_condition(state
, condition
);
1144 pthread_mutex_lock(&client_list
->lock
);
1145 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1146 &client_list
->list
, node
) {
1147 if (client_list_element
->client
->id
!= client
->id
) {
1150 cds_list_del(&client_list_element
->node
);
1151 free(client_list_element
);
1154 pthread_mutex_unlock(&client_list
->lock
);
1155 notification_client_list_put(client_list
);
1158 lttng_condition_destroy(condition
);
1166 void free_notification_client_rcu(struct rcu_head
*node
)
1168 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1172 void notification_client_destroy(struct notification_client
*client
,
1173 struct notification_thread_state
*state
)
1180 * The client object is not reachable by other threads, no need to lock
1183 if (client
->socket
>= 0) {
1184 (void) lttcomm_close_unix_sock(client
->socket
);
1185 client
->socket
= -1;
1187 client
->communication
.active
= false;
1188 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1189 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1190 pthread_mutex_destroy(&client
->lock
);
1191 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1195 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1199 struct notification_client
*get_client_from_socket(int socket
,
1200 struct notification_thread_state
*state
)
1202 struct cds_lfht_iter iter
;
1203 struct cds_lfht_node
*node
;
1204 struct notification_client
*client
= NULL
;
1206 cds_lfht_lookup(state
->client_socket_ht
,
1207 hash_client_socket(socket
),
1208 match_client_socket
,
1209 (void *) (unsigned long) socket
,
1211 node
= cds_lfht_iter_get_node(&iter
);
1216 client
= caa_container_of(node
, struct notification_client
,
1217 client_socket_ht_node
);
1223 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1227 struct notification_client
*get_client_from_id(notification_client_id id
,
1228 struct notification_thread_state
*state
)
1230 struct cds_lfht_iter iter
;
1231 struct cds_lfht_node
*node
;
1232 struct notification_client
*client
= NULL
;
1234 cds_lfht_lookup(state
->client_id_ht
,
1239 node
= cds_lfht_iter_get_node(&iter
);
1244 client
= caa_container_of(node
, struct notification_client
,
1251 bool buffer_usage_condition_applies_to_channel(
1252 const struct lttng_condition
*condition
,
1253 const struct channel_info
*channel_info
)
1255 enum lttng_condition_status status
;
1256 enum lttng_domain_type condition_domain
;
1257 const char *condition_session_name
= NULL
;
1258 const char *condition_channel_name
= NULL
;
1260 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1262 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1263 if (channel_info
->key
.domain
!= condition_domain
) {
1267 status
= lttng_condition_buffer_usage_get_session_name(
1268 condition
, &condition_session_name
);
1269 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1271 status
= lttng_condition_buffer_usage_get_channel_name(
1272 condition
, &condition_channel_name
);
1273 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1275 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1278 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1288 bool session_consumed_size_condition_applies_to_channel(
1289 const struct lttng_condition
*condition
,
1290 const struct channel_info
*channel_info
)
1292 enum lttng_condition_status status
;
1293 const char *condition_session_name
= NULL
;
1295 status
= lttng_condition_session_consumed_size_get_session_name(
1296 condition
, &condition_session_name
);
1297 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1299 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1309 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1310 const struct channel_info
*channel_info
)
1312 const struct lttng_condition
*condition
;
1313 bool trigger_applies
;
1315 condition
= lttng_trigger_get_const_condition(trigger
);
1320 switch (lttng_condition_get_type(condition
)) {
1321 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1322 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1323 trigger_applies
= buffer_usage_condition_applies_to_channel(
1324 condition
, channel_info
);
1326 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1327 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1328 condition
, channel_info
);
1334 return trigger_applies
;
1340 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1341 struct notification_client
*client
)
1343 bool applies
= false;
1344 struct lttng_condition_list_element
*condition_list_element
;
1346 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1348 applies
= lttng_condition_is_equal(
1349 condition_list_element
->condition
,
1350 lttng_trigger_get_condition(trigger
));
1358 /* Must be called with RCU read lock held. */
1360 struct lttng_session_trigger_list
*get_session_trigger_list(
1361 struct notification_thread_state
*state
,
1362 const char *session_name
)
1364 struct lttng_session_trigger_list
*list
= NULL
;
1365 struct cds_lfht_node
*node
;
1366 struct cds_lfht_iter iter
;
1368 cds_lfht_lookup(state
->session_triggers_ht
,
1369 hash_key_str(session_name
, lttng_ht_seed
),
1370 match_session_trigger_list
,
1373 node
= cds_lfht_iter_get_node(&iter
);
1376 * Not an error, the list of triggers applying to that session
1377 * will be initialized when the session is created.
1379 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1384 list
= caa_container_of(node
,
1385 struct lttng_session_trigger_list
,
1386 session_triggers_ht_node
);
1392 * Allocate an empty lttng_session_trigger_list for the session named
1395 * No ownership of 'session_name' is assumed by the session trigger list.
1396 * It is the caller's responsability to ensure the session name is alive
1397 * for as long as this list is.
1400 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1401 const char *session_name
,
1402 struct cds_lfht
*session_triggers_ht
)
1404 struct lttng_session_trigger_list
*list
;
1406 list
= zmalloc(sizeof(*list
));
1410 list
->session_name
= session_name
;
1411 CDS_INIT_LIST_HEAD(&list
->list
);
1412 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1413 list
->session_triggers_ht
= session_triggers_ht
;
1416 /* Publish the list through the session_triggers_ht. */
1417 cds_lfht_add(session_triggers_ht
,
1418 hash_key_str(session_name
, lttng_ht_seed
),
1419 &list
->session_triggers_ht_node
);
1426 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1428 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1433 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1435 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1437 /* Empty the list element by element, and then free the list itself. */
1438 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1439 &list
->list
, node
) {
1440 cds_list_del(&trigger_list_element
->node
);
1441 free(trigger_list_element
);
1444 /* Unpublish the list from the session_triggers_ht. */
1445 cds_lfht_del(list
->session_triggers_ht
,
1446 &list
->session_triggers_ht_node
);
1448 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1452 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1453 struct lttng_trigger
*trigger
)
1456 struct lttng_trigger_list_element
*new_element
=
1457 zmalloc(sizeof(*new_element
));
1463 CDS_INIT_LIST_HEAD(&new_element
->node
);
1464 new_element
->trigger
= trigger
;
1465 cds_list_add(&new_element
->node
, &list
->list
);
1471 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1472 const char *session_name
)
1474 bool applies
= false;
1475 const struct lttng_condition
*condition
;
1477 condition
= lttng_trigger_get_const_condition(trigger
);
1478 switch (lttng_condition_get_type(condition
)) {
1479 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1480 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1482 enum lttng_condition_status condition_status
;
1483 const char *condition_session_name
;
1485 condition_status
= lttng_condition_session_rotation_get_session_name(
1486 condition
, &condition_session_name
);
1487 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1488 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1492 assert(condition_session_name
);
1493 applies
= !strcmp(condition_session_name
, session_name
);
1504 * Allocate and initialize an lttng_session_trigger_list which contains
1505 * all triggers that apply to the session named 'session_name'.
1507 * No ownership of 'session_name' is assumed by the session trigger list.
1508 * It is the caller's responsability to ensure the session name is alive
1509 * for as long as this list is.
1512 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1513 const struct notification_thread_state
*state
,
1514 const char *session_name
)
1516 int trigger_count
= 0;
1517 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1518 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1519 struct cds_lfht_iter iter
;
1521 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1522 state
->session_triggers_ht
);
1524 /* Add all triggers applying to the session named 'session_name'. */
1525 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1529 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1534 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1535 trigger_ht_element
->trigger
);
1543 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1545 return session_trigger_list
;
1547 lttng_session_trigger_list_destroy(session_trigger_list
);
1552 struct session_info
*find_or_create_session_info(
1553 struct notification_thread_state
*state
,
1554 const char *name
, uid_t uid
, gid_t gid
)
1556 struct session_info
*session
= NULL
;
1557 struct cds_lfht_node
*node
;
1558 struct cds_lfht_iter iter
;
1559 struct lttng_session_trigger_list
*trigger_list
;
1562 cds_lfht_lookup(state
->sessions_ht
,
1563 hash_key_str(name
, lttng_ht_seed
),
1567 node
= cds_lfht_iter_get_node(&iter
);
1569 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1571 session
= caa_container_of(node
, struct session_info
,
1573 assert(session
->uid
== uid
);
1574 assert(session
->gid
== gid
);
1575 session_info_get(session
);
1579 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1580 if (!trigger_list
) {
1584 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1585 state
->sessions_ht
);
1587 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1589 lttng_session_trigger_list_destroy(trigger_list
);
1592 trigger_list
= NULL
;
1594 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1595 &session
->sessions_ht_node
);
1601 session_info_put(session
);
1606 int handle_notification_thread_command_add_channel(
1607 struct notification_thread_state
*state
,
1608 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1609 const char *channel_name
, enum lttng_domain_type channel_domain
,
1610 uint64_t channel_key_int
, uint64_t channel_capacity
,
1611 enum lttng_error_code
*cmd_result
)
1613 struct cds_list_head trigger_list
;
1614 struct channel_info
*new_channel_info
= NULL
;
1615 struct channel_key channel_key
= {
1616 .key
= channel_key_int
,
1617 .domain
= channel_domain
,
1619 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1620 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1621 int trigger_count
= 0;
1622 struct cds_lfht_iter iter
;
1623 struct session_info
*session_info
= NULL
;
1625 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1626 channel_name
, session_name
, channel_key_int
,
1627 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1629 CDS_INIT_LIST_HEAD(&trigger_list
);
1631 session_info
= find_or_create_session_info(state
, session_name
,
1632 session_uid
, session_gid
);
1633 if (!session_info
) {
1634 /* Allocation error or an internal error occurred. */
1638 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1639 channel_capacity
, session_info
);
1640 if (!new_channel_info
) {
1645 /* Build a list of all triggers applying to the new channel. */
1646 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1648 struct lttng_trigger_list_element
*new_element
;
1650 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1651 new_channel_info
)) {
1655 new_element
= zmalloc(sizeof(*new_element
));
1660 CDS_INIT_LIST_HEAD(&new_element
->node
);
1661 new_element
->trigger
= trigger_ht_element
->trigger
;
1662 cds_list_add(&new_element
->node
, &trigger_list
);
1667 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1669 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1670 if (!channel_trigger_list
) {
1673 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1674 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1675 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1676 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1679 /* Add channel to the channel_ht which owns the channel_infos. */
1680 cds_lfht_add(state
->channels_ht
,
1681 hash_channel_key(&new_channel_info
->key
),
1682 &new_channel_info
->channels_ht_node
);
1684 * Add the list of triggers associated with this channel to the
1685 * channel_triggers_ht.
1687 cds_lfht_add(state
->channel_triggers_ht
,
1688 hash_channel_key(&new_channel_info
->key
),
1689 &channel_trigger_list
->channel_triggers_ht_node
);
1691 session_info_put(session_info
);
1692 *cmd_result
= LTTNG_OK
;
1695 channel_info_destroy(new_channel_info
);
1696 session_info_put(session_info
);
1701 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1703 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1708 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1710 free(caa_container_of(node
, struct channel_state_sample
,
1715 int handle_notification_thread_command_remove_channel(
1716 struct notification_thread_state
*state
,
1717 uint64_t channel_key
, enum lttng_domain_type domain
,
1718 enum lttng_error_code
*cmd_result
)
1720 struct cds_lfht_node
*node
;
1721 struct cds_lfht_iter iter
;
1722 struct lttng_channel_trigger_list
*trigger_list
;
1723 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1724 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1725 struct channel_info
*channel_info
;
1727 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1728 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1732 cds_lfht_lookup(state
->channel_triggers_ht
,
1733 hash_channel_key(&key
),
1734 match_channel_trigger_list
,
1737 node
= cds_lfht_iter_get_node(&iter
);
1739 * There is a severe internal error if we are being asked to remove a
1740 * channel that doesn't exist.
1743 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1747 /* Free the list of triggers associated with this channel. */
1748 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1749 channel_triggers_ht_node
);
1750 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1751 &trigger_list
->list
, node
) {
1752 cds_list_del(&trigger_list_element
->node
);
1753 free(trigger_list_element
);
1755 cds_lfht_del(state
->channel_triggers_ht
, node
);
1756 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1758 /* Free sampled channel state. */
1759 cds_lfht_lookup(state
->channel_state_ht
,
1760 hash_channel_key(&key
),
1761 match_channel_state_sample
,
1764 node
= cds_lfht_iter_get_node(&iter
);
1766 * This is expected to be NULL if the channel is destroyed before we
1767 * received a sample.
1770 struct channel_state_sample
*sample
= caa_container_of(node
,
1771 struct channel_state_sample
,
1772 channel_state_ht_node
);
1774 cds_lfht_del(state
->channel_state_ht
, node
);
1775 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1778 /* Remove the channel from the channels_ht and free it. */
1779 cds_lfht_lookup(state
->channels_ht
,
1780 hash_channel_key(&key
),
1784 node
= cds_lfht_iter_get_node(&iter
);
1786 channel_info
= caa_container_of(node
, struct channel_info
,
1788 cds_lfht_del(state
->channels_ht
, node
);
1789 channel_info_destroy(channel_info
);
1792 *cmd_result
= LTTNG_OK
;
1797 int handle_notification_thread_command_session_rotation(
1798 struct notification_thread_state
*state
,
1799 enum notification_thread_command_type cmd_type
,
1800 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1801 uint64_t trace_archive_chunk_id
,
1802 struct lttng_trace_archive_location
*location
,
1803 enum lttng_error_code
*_cmd_result
)
1806 enum lttng_error_code cmd_result
= LTTNG_OK
;
1807 struct lttng_session_trigger_list
*trigger_list
;
1808 struct lttng_trigger_list_element
*trigger_list_element
;
1809 struct session_info
*session_info
;
1810 const struct lttng_credentials session_creds
= {
1811 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1812 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1817 session_info
= find_or_create_session_info(state
, session_name
,
1818 session_uid
, session_gid
);
1819 if (!session_info
) {
1820 /* Allocation error or an internal error occurred. */
1822 cmd_result
= LTTNG_ERR_NOMEM
;
1826 session_info
->rotation
.ongoing
=
1827 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1828 session_info
->rotation
.id
= trace_archive_chunk_id
;
1829 trigger_list
= get_session_trigger_list(state
, session_name
);
1830 if (!trigger_list
) {
1831 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1836 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1838 const struct lttng_condition
*condition
;
1839 struct lttng_trigger
*trigger
;
1840 struct notification_client_list
*client_list
;
1841 struct lttng_evaluation
*evaluation
= NULL
;
1842 enum lttng_condition_type condition_type
;
1843 enum action_executor_status executor_status
;
1845 trigger
= trigger_list_element
->trigger
;
1846 condition
= lttng_trigger_get_const_condition(trigger
);
1848 condition_type
= lttng_condition_get_type(condition
);
1850 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1851 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1853 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1854 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1858 client_list
= get_client_list_from_condition(state
, condition
);
1859 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1860 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1861 trace_archive_chunk_id
);
1863 evaluation
= lttng_evaluation_session_rotation_completed_create(
1864 trace_archive_chunk_id
, location
);
1868 /* Internal error */
1870 cmd_result
= LTTNG_ERR_UNK
;
1875 * Ownership of `evaluation` transferred to the action executor
1876 * no matter the result.
1878 executor_status
= action_executor_enqueue(state
->executor
,
1879 trigger
, evaluation
, &session_creds
,
1882 switch (executor_status
) {
1883 case ACTION_EXECUTOR_STATUS_OK
:
1885 case ACTION_EXECUTOR_STATUS_ERROR
:
1886 case ACTION_EXECUTOR_STATUS_INVALID
:
1888 * TODO Add trigger identification (name/id) when
1889 * it is added to the API.
1891 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1894 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1896 * TODO Add trigger identification (name/id) when
1897 * it is added to the API.
1899 * Not a fatal error.
1901 WARN("No space left when enqueuing action associated with session rotation trigger");
1909 notification_client_list_put(client_list
);
1910 if (caa_unlikely(ret
)) {
1915 session_info_put(session_info
);
1916 *_cmd_result
= cmd_result
;
1922 int condition_is_supported(struct lttng_condition
*condition
)
1926 switch (lttng_condition_get_type(condition
)) {
1927 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1928 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1930 enum lttng_domain_type domain
;
1932 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1939 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1945 * Older kernel tracers don't expose the API to monitor their
1946 * buffers. Therefore, we reject triggers that require that
1947 * mechanism to be available to be evaluated.
1949 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1959 /* Must be called with RCU read lock held. */
1961 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
1962 struct notification_thread_state
*state
)
1965 const struct lttng_condition
*condition
;
1966 const char *session_name
;
1967 struct lttng_session_trigger_list
*trigger_list
;
1969 condition
= lttng_trigger_get_const_condition(trigger
);
1970 switch (lttng_condition_get_type(condition
)) {
1971 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1972 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1974 enum lttng_condition_status status
;
1976 status
= lttng_condition_session_rotation_get_session_name(
1977 condition
, &session_name
);
1978 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1979 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
1990 trigger_list
= get_session_trigger_list(state
, session_name
);
1991 if (!trigger_list
) {
1992 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
1998 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2000 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2005 /* Must be called with RCU read lock held. */
2007 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2008 struct notification_thread_state
*state
)
2011 struct cds_lfht_node
*node
;
2012 struct cds_lfht_iter iter
;
2013 struct channel_info
*channel
;
2015 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2017 struct lttng_trigger_list_element
*trigger_list_element
;
2018 struct lttng_channel_trigger_list
*trigger_list
;
2019 struct cds_lfht_iter lookup_iter
;
2021 if (!trigger_applies_to_channel(trigger
, channel
)) {
2025 cds_lfht_lookup(state
->channel_triggers_ht
,
2026 hash_channel_key(&channel
->key
),
2027 match_channel_trigger_list
,
2030 node
= cds_lfht_iter_get_node(&lookup_iter
);
2032 trigger_list
= caa_container_of(node
,
2033 struct lttng_channel_trigger_list
,
2034 channel_triggers_ht_node
);
2036 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2037 if (!trigger_list_element
) {
2041 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2042 trigger_list_element
->trigger
= trigger
;
2043 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2044 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2052 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2054 bool is_notify
= false;
2055 unsigned int i
, count
;
2056 enum lttng_action_status action_status
;
2057 const struct lttng_action
*action
=
2058 lttng_trigger_get_const_action(trigger
);
2059 enum lttng_action_type action_type
;
2062 action_type
= lttng_action_get_type(action
);
2063 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2066 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2070 action_status
= lttng_action_group_get_count(action
, &count
);
2071 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2073 for (i
= 0; i
< count
; i
++) {
2074 const struct lttng_action
*inner_action
=
2075 lttng_action_group_get_at_index(
2078 action_type
= lttng_action_get_type(inner_action
);
2079 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2090 * FIXME A client's credentials are not checked when registering a trigger.
2092 * The effects of this are benign since:
2093 * - The client will succeed in registering the trigger, as it is valid,
2094 * - The trigger will, internally, be bound to the channel/session,
2095 * - The notifications will not be sent since the client's credentials
2096 * are checked against the channel at that moment.
2098 * If this function returns a non-zero value, it means something is
2099 * fundamentally broken and the whole subsystem/thread will be torn down.
2101 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2105 int handle_notification_thread_command_register_trigger(
2106 struct notification_thread_state
*state
,
2107 struct lttng_trigger
*trigger
,
2108 enum lttng_error_code
*cmd_result
)
2111 struct lttng_condition
*condition
;
2112 struct notification_client
*client
;
2113 struct notification_client_list
*client_list
= NULL
;
2114 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2115 struct notification_client_list_element
*client_list_element
;
2116 struct cds_lfht_node
*node
;
2117 struct cds_lfht_iter iter
;
2118 bool free_trigger
= true;
2119 struct lttng_evaluation
*evaluation
= NULL
;
2120 struct lttng_credentials object_creds
;
2123 enum action_executor_status executor_status
;
2127 condition
= lttng_trigger_get_condition(trigger
);
2130 ret
= condition_is_supported(condition
);
2133 } else if (ret
== 0) {
2134 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2137 /* Feature is supported, continue. */
2141 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2142 if (!trigger_ht_element
) {
2147 /* Add trigger to the trigger_ht. */
2148 cds_lfht_node_init(&trigger_ht_element
->node
);
2149 trigger_ht_element
->trigger
= trigger
;
2151 node
= cds_lfht_add_unique(state
->triggers_ht
,
2152 lttng_condition_hash(condition
),
2155 &trigger_ht_element
->node
);
2156 if (node
!= &trigger_ht_element
->node
) {
2157 /* Not a fatal error, simply report it to the client. */
2158 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2159 goto error_free_ht_element
;
2163 * Ownership of the trigger and of its wrapper was transfered to
2166 trigger_ht_element
= NULL
;
2167 free_trigger
= false;
2170 * The rest only applies to triggers that have a "notify" action.
2171 * It is not skipped as this is the only action type currently
2174 if (is_trigger_action_notify(trigger
)) {
2175 client_list
= notification_client_list_create(trigger
);
2178 goto error_free_ht_element
;
2181 /* Build a list of clients to which this new trigger applies. */
2182 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2183 client_socket_ht_node
) {
2184 if (!trigger_applies_to_client(trigger
, client
)) {
2188 client_list_element
=
2189 zmalloc(sizeof(*client_list_element
));
2190 if (!client_list_element
) {
2192 goto error_put_client_list
;
2195 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2196 client_list_element
->client
= client
;
2197 cds_list_add(&client_list_element
->node
,
2198 &client_list
->list
);
2202 * Client list ownership transferred to the
2203 * notification_trigger_clients_ht.
2205 publish_notification_client_list(state
, client_list
);
2208 switch (get_condition_binding_object(condition
)) {
2209 case LTTNG_OBJECT_TYPE_SESSION
:
2210 /* Add the trigger to the list if it matches a known session. */
2211 ret
= bind_trigger_to_matching_session(trigger
, state
);
2213 goto error_put_client_list
;
2216 case LTTNG_OBJECT_TYPE_CHANNEL
:
2218 * Add the trigger to list of triggers bound to the channels
2221 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2223 goto error_put_client_list
;
2226 case LTTNG_OBJECT_TYPE_NONE
:
2229 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2231 goto error_put_client_list
;
2235 * The new trigger's condition must be evaluated against the current
2238 * In the case of `notify` action, nothing preventing clients from
2239 * subscribing to a condition before the corresponding trigger is
2240 * registered, we have to evaluate this new condition right away.
2242 * At some point, we were waiting for the next "evaluation" (e.g. on
2243 * reception of a channel sample) to evaluate this new condition, but
2246 * The reason it was broken is that waiting for the next sample
2247 * does not allow us to properly handle transitions for edge-triggered
2250 * Consider this example: when we handle a new channel sample, we
2251 * evaluate each conditions twice: once with the previous state, and
2252 * again with the newest state. We then use those two results to
2253 * determine whether a state change happened: a condition was false and
2254 * became true. If a state change happened, we have to notify clients.
2256 * Now, if a client subscribes to a given notification and registers
2257 * a trigger *after* that subscription, we have to make sure the
2258 * condition is evaluated at this point while considering only the
2259 * current state. Otherwise, the next evaluation cycle may only see
2260 * that the evaluations remain the same (true for samples n-1 and n) and
2261 * the client will never know that the condition has been met.
2263 switch (get_condition_binding_object(condition
)) {
2264 case LTTNG_OBJECT_TYPE_SESSION
:
2265 ret
= evaluate_session_condition_for_client(condition
, state
,
2266 &evaluation
, &object_uid
,
2269 case LTTNG_OBJECT_TYPE_CHANNEL
:
2270 ret
= evaluate_channel_condition_for_client(condition
, state
,
2271 &evaluation
, &object_uid
,
2274 case LTTNG_OBJECT_TYPE_NONE
:
2276 goto error_put_client_list
;
2277 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2280 goto error_put_client_list
;
2285 goto error_put_client_list
;
2288 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2289 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2291 DBG("Newly registered trigger's condition evaluated to %s",
2292 evaluation
? "true" : "false");
2294 /* Evaluation yielded nothing. Normal exit. */
2296 goto error_put_client_list
;
2300 * Ownership of `evaluation` transferred to the action executor
2301 * no matter the result.
2303 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2304 evaluation
, &object_creds
, client_list
);
2306 switch (executor_status
) {
2307 case ACTION_EXECUTOR_STATUS_OK
:
2309 case ACTION_EXECUTOR_STATUS_ERROR
:
2310 case ACTION_EXECUTOR_STATUS_INVALID
:
2312 * TODO Add trigger identification (name/id) when
2313 * it is added to the API.
2315 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2317 goto error_put_client_list
;
2318 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2320 * TODO Add trigger identification (name/id) when
2321 * it is added to the API.
2323 * Not a fatal error.
2325 WARN("No space left when enqueuing action associated to newly registered trigger");
2327 goto error_put_client_list
;
2332 *cmd_result
= LTTNG_OK
;
2334 error_put_client_list
:
2335 notification_client_list_put(client_list
);
2337 error_free_ht_element
:
2338 free(trigger_ht_element
);
2341 lttng_trigger_destroy(trigger
);
2348 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2350 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2355 int handle_notification_thread_command_unregister_trigger(
2356 struct notification_thread_state
*state
,
2357 struct lttng_trigger
*trigger
,
2358 enum lttng_error_code
*_cmd_reply
)
2360 struct cds_lfht_iter iter
;
2361 struct cds_lfht_node
*triggers_ht_node
;
2362 struct lttng_channel_trigger_list
*trigger_list
;
2363 struct notification_client_list
*client_list
;
2364 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2365 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2367 enum lttng_error_code cmd_reply
;
2371 cds_lfht_lookup(state
->triggers_ht
,
2372 lttng_condition_hash(condition
),
2376 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2377 if (!triggers_ht_node
) {
2378 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2381 cmd_reply
= LTTNG_OK
;
2384 /* Remove trigger from channel_triggers_ht. */
2385 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2386 channel_triggers_ht_node
) {
2387 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2389 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2390 &trigger_list
->list
, node
) {
2391 const struct lttng_condition
*current_condition
=
2392 lttng_trigger_get_const_condition(
2393 trigger_element
->trigger
);
2395 assert(current_condition
);
2396 if (!lttng_condition_is_equal(condition
,
2397 current_condition
)) {
2401 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2402 cds_list_del(&trigger_element
->node
);
2403 /* A trigger can only appear once per channel */
2409 * Remove and release the client list from
2410 * notification_trigger_clients_ht.
2412 client_list
= get_client_list_from_condition(state
, condition
);
2413 assert(client_list
);
2415 /* Put new reference and the hashtable's reference. */
2416 notification_client_list_put(client_list
);
2417 notification_client_list_put(client_list
);
2420 /* Remove trigger from triggers_ht. */
2421 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2422 struct lttng_trigger_ht_element
, node
);
2423 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2425 /* Release the ownership of the trigger. */
2426 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2427 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2431 *_cmd_reply
= cmd_reply
;
2436 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2437 int handle_notification_thread_command(
2438 struct notification_thread_handle
*handle
,
2439 struct notification_thread_state
*state
)
2443 struct notification_thread_command
*cmd
;
2445 /* Read the event pipe to put it back into a quiescent state. */
2446 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2448 if (ret
!= sizeof(counter
)) {
2452 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2453 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2454 struct notification_thread_command
, cmd_list_node
);
2455 cds_list_del(&cmd
->cmd_list_node
);
2456 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2457 switch (cmd
->type
) {
2458 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2459 DBG("[notification-thread] Received register trigger command");
2460 ret
= handle_notification_thread_command_register_trigger(
2461 state
, cmd
->parameters
.trigger
,
2464 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2465 DBG("[notification-thread] Received unregister trigger command");
2466 ret
= handle_notification_thread_command_unregister_trigger(
2467 state
, cmd
->parameters
.trigger
,
2470 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2471 DBG("[notification-thread] Received add channel command");
2472 ret
= handle_notification_thread_command_add_channel(
2474 cmd
->parameters
.add_channel
.session
.name
,
2475 cmd
->parameters
.add_channel
.session
.uid
,
2476 cmd
->parameters
.add_channel
.session
.gid
,
2477 cmd
->parameters
.add_channel
.channel
.name
,
2478 cmd
->parameters
.add_channel
.channel
.domain
,
2479 cmd
->parameters
.add_channel
.channel
.key
,
2480 cmd
->parameters
.add_channel
.channel
.capacity
,
2483 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2484 DBG("[notification-thread] Received remove channel command");
2485 ret
= handle_notification_thread_command_remove_channel(
2486 state
, cmd
->parameters
.remove_channel
.key
,
2487 cmd
->parameters
.remove_channel
.domain
,
2490 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2491 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2492 DBG("[notification-thread] Received session rotation %s command",
2493 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2494 "ongoing" : "completed");
2495 ret
= handle_notification_thread_command_session_rotation(
2498 cmd
->parameters
.session_rotation
.session_name
,
2499 cmd
->parameters
.session_rotation
.uid
,
2500 cmd
->parameters
.session_rotation
.gid
,
2501 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2502 cmd
->parameters
.session_rotation
.location
,
2505 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2506 DBG("[notification-thread] Received quit command");
2507 cmd
->reply_code
= LTTNG_OK
;
2510 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2512 const enum client_transmission_status client_status
=
2513 cmd
->parameters
.client_communication_update
2515 const notification_client_id client_id
=
2516 cmd
->parameters
.client_communication_update
.id
;
2517 struct notification_client
*client
;
2520 client
= get_client_from_id(client_id
, state
);
2524 * Client error was probably already picked-up by the
2525 * notification thread or it has disconnected
2526 * gracefully while this command was queued.
2528 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2532 ret
= client_handle_transmission_status(
2533 client
, client_status
, state
);
2539 ERR("[notification-thread] Unknown internal command received");
2547 if (cmd
->is_async
) {
2551 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2555 /* Wake-up and return a fatal error to the calling thread. */
2556 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2557 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2559 /* Indicate a fatal error to the caller. */
2564 int socket_set_non_blocking(int socket
)
2568 /* Set the pipe as non-blocking. */
2569 ret
= fcntl(socket
, F_GETFL
, 0);
2571 PERROR("fcntl get socket flags");
2576 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2578 PERROR("fcntl set O_NONBLOCK socket flag");
2581 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2587 int client_reset_inbound_state(struct notification_client
*client
)
2592 lttng_payload_clear(&client
->communication
.inbound
.payload
);
2594 client
->communication
.inbound
.bytes_to_receive
=
2595 sizeof(struct lttng_notification_channel_message
);
2596 client
->communication
.inbound
.msg_type
=
2597 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2598 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2599 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2600 ret
= lttng_dynamic_buffer_set_size(
2601 &client
->communication
.inbound
.payload
.buffer
,
2602 client
->communication
.inbound
.bytes_to_receive
);
2607 int handle_notification_thread_client_connect(
2608 struct notification_thread_state
*state
)
2611 struct notification_client
*client
;
2613 DBG("[notification-thread] Handling new notification channel client connection");
2615 client
= zmalloc(sizeof(*client
));
2622 pthread_mutex_init(&client
->lock
, NULL
);
2623 client
->id
= state
->next_notification_client_id
++;
2624 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2625 lttng_payload_init(&client
->communication
.inbound
.payload
);
2626 lttng_payload_init(&client
->communication
.outbound
.payload
);
2627 client
->communication
.inbound
.expect_creds
= true;
2629 ret
= client_reset_inbound_state(client
);
2631 ERR("[notification-thread] Failed to reset client communication's inbound state");
2636 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2638 ERR("[notification-thread] Failed to accept new notification channel client connection");
2643 client
->socket
= ret
;
2645 ret
= socket_set_non_blocking(client
->socket
);
2647 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2651 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2653 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2658 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2659 LPOLLIN
| LPOLLERR
|
2660 LPOLLHUP
| LPOLLRDHUP
);
2662 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2666 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2670 cds_lfht_add(state
->client_socket_ht
,
2671 hash_client_socket(client
->socket
),
2672 &client
->client_socket_ht_node
);
2673 cds_lfht_add(state
->client_id_ht
,
2674 hash_client_id(client
->id
),
2675 &client
->client_id_ht_node
);
2681 notification_client_destroy(client
, state
);
2686 * RCU read-lock must be held by the caller.
2687 * Client lock must _not_ be held by the caller.
2690 int notification_thread_client_disconnect(
2691 struct notification_client
*client
,
2692 struct notification_thread_state
*state
)
2695 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
2697 /* Acquire the client lock to disable its communication atomically. */
2698 pthread_mutex_lock(&client
->lock
);
2699 client
->communication
.active
= false;
2700 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
2701 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
2702 pthread_mutex_unlock(&client
->lock
);
2704 ret
= lttng_poll_del(&state
->events
, client
->socket
);
2706 ERR("[notification-thread] Failed to remove client socket %d from poll set",
2710 /* Release all conditions to which the client was subscribed. */
2711 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
2712 &client
->condition_list
, node
) {
2713 (void) notification_thread_client_unsubscribe(client
,
2714 condition_list_element
->condition
, state
, NULL
);
2718 * Client no longer accessible to other threads (through the
2721 notification_client_destroy(client
, state
);
2725 int handle_notification_thread_client_disconnect(
2726 int client_socket
, struct notification_thread_state
*state
)
2729 struct notification_client
*client
;
2732 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2734 client
= get_client_from_socket(client_socket
, state
);
2736 /* Internal state corruption, fatal error. */
2737 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2743 ret
= notification_thread_client_disconnect(client
, state
);
2749 int handle_notification_thread_client_disconnect_all(
2750 struct notification_thread_state
*state
)
2752 struct cds_lfht_iter iter
;
2753 struct notification_client
*client
;
2754 bool error_encoutered
= false;
2757 DBG("[notification-thread] Closing all client connections");
2758 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2759 client_socket_ht_node
) {
2762 ret
= notification_thread_client_disconnect(
2765 error_encoutered
= true;
2769 return error_encoutered
? 1 : 0;
2772 int handle_notification_thread_trigger_unregister_all(
2773 struct notification_thread_state
*state
)
2775 bool error_occurred
= false;
2776 struct cds_lfht_iter iter
;
2777 struct lttng_trigger_ht_element
*trigger_ht_element
;
2780 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2782 int ret
= handle_notification_thread_command_unregister_trigger(
2783 state
, trigger_ht_element
->trigger
, NULL
);
2785 error_occurred
= true;
2789 return error_occurred
? -1 : 0;
2793 int client_handle_transmission_status(
2794 struct notification_client
*client
,
2795 enum client_transmission_status transmission_status
,
2796 struct notification_thread_state
*state
)
2800 switch (transmission_status
) {
2801 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
2802 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2803 CLIENT_POLL_MASK_IN
);
2809 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
2811 * We want to be notified whenever there is buffer space
2812 * available to send the rest of the payload.
2814 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2815 CLIENT_POLL_MASK_IN_OUT
);
2820 case CLIENT_TRANSMISSION_STATUS_FAIL
:
2821 ret
= notification_thread_client_disconnect(client
, state
);
2826 case CLIENT_TRANSMISSION_STATUS_ERROR
:
2836 /* Client lock must be acquired by caller. */
2838 enum client_transmission_status
client_flush_outgoing_queue(
2839 struct notification_client
*client
)
2842 size_t to_send_count
;
2843 enum client_transmission_status status
;
2844 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
2845 &client
->communication
.outbound
.payload
, 0, -1);
2846 const int fds_to_send_count
=
2847 lttng_payload_view_get_fd_handle_count(&pv
);
2849 ASSERT_LOCKED(client
->lock
);
2851 if (!client
->communication
.active
) {
2852 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
2856 if (pv
.buffer
.size
== 0) {
2858 * If both data and fds are equal to zero, we are in an invalid
2861 assert(fds_to_send_count
!= 0);
2866 to_send_count
= pv
.buffer
.size
;
2867 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
2870 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
2873 if ((ret
>= 0 && ret
< to_send_count
)) {
2874 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
2876 to_send_count
-= max(ret
, 0);
2878 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
2880 pv
.buffer
.size
- to_send_count
,
2882 ret
= lttng_dynamic_buffer_set_size(
2883 &client
->communication
.outbound
.payload
.buffer
,
2889 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
2891 } else if (ret
< 0) {
2892 /* Generic error, disable the client's communication. */
2893 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
2895 client
->communication
.active
= false;
2896 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
2900 * No error and flushed the queue completely.
2902 * The payload buffer size is used later to
2903 * check if there is notifications queued. So albeit that the
2904 * direct caller knows that the transmission is complete, we
2905 * need to set the buffer size to zero.
2907 ret
= lttng_dynamic_buffer_set_size(
2908 &client
->communication
.outbound
.payload
.buffer
, 0);
2915 /* No fds to send, transmission is complete. */
2916 if (fds_to_send_count
== 0) {
2917 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
2921 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
2922 client
->socket
, &pv
);
2924 /* Generic error, disable the client's communication. */
2925 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
2927 client
->communication
.active
= false;
2928 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
2930 } else if (ret
== 0) {
2931 /* Nothing could be sent. */
2932 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
2934 /* Fd passing is an all or nothing kind of thing. */
2935 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
2937 * The payload _fd_array count is used later to
2938 * check if there is notifications queued. So although the
2939 * direct caller knows that the transmission is complete, we
2940 * need to clear the _fd_array for the queuing check.
2942 lttng_dynamic_pointer_array_clear(
2943 &client
->communication
.outbound
.payload
2948 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
2949 client
->communication
.outbound
.queued_command_reply
= false;
2950 client
->communication
.outbound
.dropped_notification
= false;
2951 lttng_payload_clear(&client
->communication
.outbound
.payload
);
2956 return CLIENT_TRANSMISSION_STATUS_ERROR
;
2960 bool client_has_outbound_data_left(
2961 const struct notification_client
*client
)
2963 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
2964 &client
->communication
.outbound
.payload
, 0, -1);
2965 const bool has_data
= pv
.buffer
.size
!= 0;
2966 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
2968 return has_data
|| has_fds
;
2971 /* Client lock must _not_ be held by the caller. */
2973 int client_send_command_reply(struct notification_client
*client
,
2974 struct notification_thread_state
*state
,
2975 enum lttng_notification_channel_status status
)
2978 struct lttng_notification_channel_command_reply reply
= {
2979 .status
= (int8_t) status
,
2981 struct lttng_notification_channel_message msg
= {
2982 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
2983 .size
= sizeof(reply
),
2985 char buffer
[sizeof(msg
) + sizeof(reply
)];
2986 enum client_transmission_status transmission_status
;
2988 memcpy(buffer
, &msg
, sizeof(msg
));
2989 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
2990 DBG("[notification-thread] Send command reply (%i)", (int) status
);
2992 pthread_mutex_lock(&client
->lock
);
2993 if (client
->communication
.outbound
.queued_command_reply
) {
2994 /* Protocol error. */
2998 /* Enqueue buffer to outgoing queue and flush it. */
2999 ret
= lttng_dynamic_buffer_append(
3000 &client
->communication
.outbound
.payload
.buffer
,
3001 buffer
, sizeof(buffer
));
3006 transmission_status
= client_flush_outgoing_queue(client
);
3008 if (client_has_outbound_data_left(client
)) {
3009 /* Queue could not be emptied. */
3010 client
->communication
.outbound
.queued_command_reply
= true;
3013 pthread_mutex_unlock(&client
->lock
);
3014 ret
= client_handle_transmission_status(
3015 client
, transmission_status
, state
);
3022 pthread_mutex_unlock(&client
->lock
);
3028 int client_handle_message_unknown(struct notification_client
*client
,
3029 struct notification_thread_state
*state
)
3033 * Receiving message header. The function will be called again
3034 * once the rest of the message as been received and can be
3037 const struct lttng_notification_channel_message
*msg
;
3039 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3040 msg
= (const struct lttng_notification_channel_message
*)
3041 client
->communication
.inbound
.payload
.buffer
.data
;
3043 if (msg
->size
== 0 ||
3044 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3045 ERR("[notification-thread] Invalid notification channel message: length = %u",
3051 switch (msg
->type
) {
3052 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3053 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3054 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3058 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3062 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3063 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3064 client
->communication
.inbound
.msg_type
=
3065 (enum lttng_notification_channel_message_type
) msg
->type
;
3066 ret
= lttng_dynamic_buffer_set_size(
3067 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3069 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3076 int client_handle_message_handshake(struct notification_client
*client
,
3077 struct notification_thread_state
*state
)
3080 struct lttng_notification_channel_command_handshake
*handshake_client
;
3081 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3082 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3083 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3085 const struct lttng_notification_channel_message msg_header
= {
3086 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3087 .size
= sizeof(handshake_reply
),
3089 enum lttng_notification_channel_status status
=
3090 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3091 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3093 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3094 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3095 sizeof(handshake_reply
));
3098 (struct lttng_notification_channel_command_handshake
*)
3099 client
->communication
.inbound
.payload
.buffer
3101 client
->major
= handshake_client
->major
;
3102 client
->minor
= handshake_client
->minor
;
3103 if (!client
->communication
.inbound
.creds_received
) {
3104 ERR("[notification-thread] No credentials received from client");
3109 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3110 &client
->communication
.inbound
.creds
);
3111 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3112 &client
->communication
.inbound
.creds
);
3113 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3114 client
->uid
, client
->gid
, (int) client
->major
,
3115 (int) client
->minor
);
3117 if (handshake_client
->major
!=
3118 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3119 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3122 pthread_mutex_lock(&client
->lock
);
3123 /* Outgoing queue will be flushed when the command reply is sent. */
3124 ret
= lttng_dynamic_buffer_append(
3125 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3126 sizeof(send_buffer
));
3128 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3132 client
->validated
= true;
3133 client
->communication
.active
= true;
3134 pthread_mutex_unlock(&client
->lock
);
3136 /* Set reception state to receive the next message header. */
3137 ret
= client_reset_inbound_state(client
);
3139 ERR("[notification-thread] Failed to reset client communication's inbound state");
3143 /* Flushes the outgoing queue. */
3144 ret
= client_send_command_reply(client
, state
, status
);
3146 ERR("[notification-thread] Failed to send reply to notification channel client");
3152 pthread_mutex_unlock(&client
->lock
);
3158 int client_handle_message_subscription(
3159 struct notification_client
*client
,
3160 enum lttng_notification_channel_message_type msg_type
,
3161 struct notification_thread_state
*state
)
3164 struct lttng_condition
*condition
;
3165 enum lttng_notification_channel_status status
=
3166 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3167 struct lttng_payload_view condition_view
=
3168 lttng_payload_view_from_payload(
3169 &client
->communication
.inbound
.payload
,
3171 size_t expected_condition_size
;
3174 * No need to lock client to sample the inbound state as the only
3175 * other thread accessing clients (action executor) only uses the
3178 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3179 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3180 if (ret
!= expected_condition_size
) {
3181 ERR("[notification-thread] Malformed condition received from client");
3185 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3186 ret
= notification_thread_client_subscribe(
3187 client
, condition
, state
, &status
);
3189 ret
= notification_thread_client_unsubscribe(
3190 client
, condition
, state
, &status
);
3197 /* Set reception state to receive the next message header. */
3198 ret
= client_reset_inbound_state(client
);
3200 ERR("[notification-thread] Failed to reset client communication's inbound state");
3204 ret
= client_send_command_reply(client
, state
, status
);
3206 ERR("[notification-thread] Failed to send reply to notification channel client");
3215 int client_dispatch_message(struct notification_client
*client
,
3216 struct notification_thread_state
*state
)
3220 if (client
->communication
.inbound
.msg_type
!=
3221 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3222 client
->communication
.inbound
.msg_type
!=
3223 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3224 !client
->validated
) {
3225 WARN("[notification-thread] client attempted a command before handshake");
3230 switch (client
->communication
.inbound
.msg_type
) {
3231 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3233 ret
= client_handle_message_unknown(client
, state
);
3236 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3238 ret
= client_handle_message_handshake(client
, state
);
3241 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3242 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3244 ret
= client_handle_message_subscription(client
,
3245 client
->communication
.inbound
.msg_type
, state
);
3255 /* Incoming data from client. */
3256 int handle_notification_thread_client_in(
3257 struct notification_thread_state
*state
, int socket
)
3260 struct notification_client
*client
;
3265 client
= get_client_from_socket(socket
, state
);
3267 /* Internal error, abort. */
3272 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3273 client
->communication
.inbound
.bytes_to_receive
;
3274 if (client
->communication
.inbound
.expect_creds
) {
3275 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3276 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3277 client
->communication
.inbound
.bytes_to_receive
,
3278 &client
->communication
.inbound
.creds
);
3280 client
->communication
.inbound
.expect_creds
= false;
3281 client
->communication
.inbound
.creds_received
= true;
3284 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3285 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3286 client
->communication
.inbound
.bytes_to_receive
);
3288 if (recv_ret
>= 0) {
3289 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3291 goto error_disconnect_client
;
3294 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3295 /* Message incomplete wait for more data. */
3300 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3303 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3304 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3306 client
->communication
.inbound
.fds_to_receive
,
3307 &client
->communication
.inbound
.payload
);
3310 * Fds received. non blocking fds passing is all
3313 ssize_t expected_size
;
3315 expected_size
= sizeof(int) *
3316 client
->communication
.inbound
3318 assert(ret
== expected_size
);
3319 client
->communication
.inbound
.fds_to_receive
= 0;
3320 } else if (ret
== 0) {
3321 /* Received nothing. */
3325 goto error_disconnect_client
;
3329 /* At this point the message is complete.*/
3330 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3331 client
->communication
.inbound
.fds_to_receive
== 0);
3332 ret
= client_dispatch_message(client
, state
);
3335 * Only returns an error if this client must be
3338 goto error_disconnect_client
;
3345 error_disconnect_client
:
3346 ret
= notification_thread_client_disconnect(client
, state
);
3350 /* Client ready to receive outgoing data. */
3351 int handle_notification_thread_client_out(
3352 struct notification_thread_state
*state
, int socket
)
3355 struct notification_client
*client
;
3356 enum client_transmission_status transmission_status
;
3359 client
= get_client_from_socket(socket
, state
);
3361 /* Internal error, abort. */
3366 pthread_mutex_lock(&client
->lock
);
3367 transmission_status
= client_flush_outgoing_queue(client
);
3368 pthread_mutex_unlock(&client
->lock
);
3370 ret
= client_handle_transmission_status(
3371 client
, transmission_status
, state
);
3381 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3382 const struct channel_state_sample
*sample
,
3383 uint64_t buffer_capacity
)
3385 bool result
= false;
3387 enum lttng_condition_type condition_type
;
3388 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3389 condition
, struct lttng_condition_buffer_usage
,
3392 if (use_condition
->threshold_bytes
.set
) {
3393 threshold
= use_condition
->threshold_bytes
.value
;
3396 * Threshold was expressed as a ratio.
3398 * TODO the threshold (in bytes) of conditions expressed
3399 * as a ratio of total buffer size could be cached to
3400 * forego this double-multiplication or it could be performed
3401 * as fixed-point math.
3403 * Note that caching should accommodates the case where the
3404 * condition applies to multiple channels (i.e. don't assume
3405 * that all channels matching my_chann* have the same size...)
3407 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3408 (double) buffer_capacity
);
3411 condition_type
= lttng_condition_get_type(condition
);
3412 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3413 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3414 threshold
, sample
->highest_usage
);
3417 * The low condition should only be triggered once _all_ of the
3418 * streams in a channel have gone below the "low" threshold.
3420 if (sample
->highest_usage
<= threshold
) {
3424 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3425 threshold
, sample
->highest_usage
);
3428 * For high buffer usage scenarios, we want to trigger whenever
3429 * _any_ of the streams has reached the "high" threshold.
3431 if (sample
->highest_usage
>= threshold
) {
3440 bool evaluate_session_consumed_size_condition(
3441 const struct lttng_condition
*condition
,
3442 uint64_t session_consumed_size
)
3445 const struct lttng_condition_session_consumed_size
*size_condition
=
3446 container_of(condition
,
3447 struct lttng_condition_session_consumed_size
,
3450 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3451 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3452 threshold
, session_consumed_size
);
3453 return session_consumed_size
>= threshold
;
3457 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3458 struct lttng_evaluation
**evaluation
,
3459 const struct notification_thread_state
*state
,
3460 const struct channel_state_sample
*previous_sample
,
3461 const struct channel_state_sample
*latest_sample
,
3462 uint64_t previous_session_consumed_total
,
3463 uint64_t latest_session_consumed_total
,
3464 struct channel_info
*channel_info
)
3467 enum lttng_condition_type condition_type
;
3468 const bool previous_sample_available
= !!previous_sample
;
3469 bool previous_sample_result
= false;
3470 bool latest_sample_result
;
3472 condition_type
= lttng_condition_get_type(condition
);
3474 switch (condition_type
) {
3475 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3476 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3477 if (caa_likely(previous_sample_available
)) {
3478 previous_sample_result
=
3479 evaluate_buffer_usage_condition(condition
,
3480 previous_sample
, channel_info
->capacity
);
3482 latest_sample_result
= evaluate_buffer_usage_condition(
3483 condition
, latest_sample
,
3484 channel_info
->capacity
);
3486 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3487 if (caa_likely(previous_sample_available
)) {
3488 previous_sample_result
=
3489 evaluate_session_consumed_size_condition(
3491 previous_session_consumed_total
);
3493 latest_sample_result
=
3494 evaluate_session_consumed_size_condition(
3496 latest_session_consumed_total
);
3499 /* Unknown condition type; internal error. */
3503 if (!latest_sample_result
||
3504 (previous_sample_result
== latest_sample_result
)) {
3506 * Only trigger on a condition evaluation transition.
3508 * NOTE: This edge-triggered logic may not be appropriate for
3509 * future condition types.
3514 if (!evaluation
|| !latest_sample_result
) {
3518 switch (condition_type
) {
3519 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3520 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3521 *evaluation
= lttng_evaluation_buffer_usage_create(
3523 latest_sample
->highest_usage
,
3524 channel_info
->capacity
);
3526 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3527 *evaluation
= lttng_evaluation_session_consumed_size_create(
3528 latest_session_consumed_total
);
3543 int client_notification_overflow(struct notification_client
*client
)
3546 const struct lttng_notification_channel_message msg
= {
3547 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3550 ASSERT_LOCKED(client
->lock
);
3552 DBG("Dropping notification addressed to client (socket fd = %i)",
3554 if (client
->communication
.outbound
.dropped_notification
) {
3556 * The client already has a "notification dropped" message
3557 * in its outgoing queue. Nothing to do since all
3558 * of those messages are coalesced.
3563 client
->communication
.outbound
.dropped_notification
= true;
3564 ret
= lttng_dynamic_buffer_append(
3565 &client
->communication
.outbound
.payload
.buffer
, &msg
,
3568 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3575 static int client_handle_transmission_status_wrapper(
3576 struct notification_client
*client
,
3577 enum client_transmission_status status
,
3580 return client_handle_transmission_status(client
, status
,
3581 (struct notification_thread_state
*) user_data
);
3585 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3586 const struct lttng_evaluation
*evaluation
,
3587 struct notification_client_list
* client_list
,
3588 struct notification_thread_state
*state
,
3589 uid_t object_uid
, gid_t object_gid
)
3591 const struct lttng_credentials creds
= {
3592 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
3593 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
3596 return notification_client_list_send_evaluation(client_list
,
3597 lttng_trigger_get_const_condition(trigger
), evaluation
,
3598 lttng_trigger_get_credentials(trigger
),
3600 client_handle_transmission_status_wrapper
, state
);
3604 * Permission checks relative to notification channel clients are performed
3605 * here. Notice how object, client, and trigger credentials are involved in
3608 * The `object` credentials are the credentials associated with the "subject"
3609 * of a condition. For instance, a `rotation completed` condition applies
3610 * to a session. When that condition is met, it will produce an evaluation
3611 * against a session. Hence, in this case, the `object` credentials are the
3612 * credentials of the "subject" session.
3614 * The `trigger` credentials are the credentials of the user that registered the
3617 * The `client` credentials are the credentials of the user that created a given
3618 * notification channel.
3620 * In terms of visibility, it is expected that non-privilieged users can only
3621 * register triggers against "their" objects (their own sessions and
3622 * applications they are allowed to interact with). They can then open a
3623 * notification channel and subscribe to notifications associated with those
3626 * As for privilieged users, they can register triggers against the objects of
3627 * other users. They can then subscribe to the notifications associated to their
3628 * triggers. Privilieged users _can't_ subscribe to the notifications of
3629 * triggers owned by other users; they must create their own triggers.
3631 * This is more a concern of usability than security. It would be difficult for
3632 * a root user reliably subscribe to a specific set of conditions without
3633 * interference from external users (those could, for instance, unregister
3637 int notification_client_list_send_evaluation(
3638 struct notification_client_list
*client_list
,
3639 const struct lttng_condition
*condition
,
3640 const struct lttng_evaluation
*evaluation
,
3641 const struct lttng_credentials
*trigger_creds
,
3642 const struct lttng_credentials
*source_object_creds
,
3643 report_client_transmission_result_cb client_report
,
3647 struct lttng_payload msg_payload
;
3648 struct notification_client_list_element
*client_list_element
, *tmp
;
3649 const struct lttng_notification notification
= {
3650 .condition
= (struct lttng_condition
*) condition
,
3651 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3653 struct lttng_notification_channel_message msg_header
= {
3654 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3657 lttng_payload_init(&msg_payload
);
3659 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
3660 sizeof(msg_header
));
3665 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
3667 ERR("[notification-thread] Failed to serialize notification");
3672 /* Update payload size. */
3673 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
3674 ->size
= (uint32_t)(
3675 msg_payload
.buffer
.size
- sizeof(msg_header
));
3677 /* Update the payload number of fds. */
3679 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3680 &msg_payload
, 0, -1);
3682 ((struct lttng_notification_channel_message
*)
3683 msg_payload
.buffer
.data
)->fds
= (uint32_t)
3684 lttng_payload_view_get_fd_handle_count(&pv
);
3687 pthread_mutex_lock(&client_list
->lock
);
3688 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3689 &client_list
->list
, node
) {
3690 enum client_transmission_status transmission_status
;
3691 struct notification_client
*client
=
3692 client_list_element
->client
;
3695 pthread_mutex_lock(&client
->lock
);
3696 if (!client
->communication
.active
) {
3698 * Skip inactive client (protocol error or
3701 DBG("Skipping client at it is marked as inactive");
3705 if (source_object_creds
) {
3706 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
3707 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
3710 * Client is not allowed to monitor this
3713 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
3718 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
3719 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
3723 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3724 client
->socket
, msg_payload
.buffer
.size
);
3726 if (client_has_outbound_data_left(client
)) {
3728 * Outgoing data is already buffered for this client;
3729 * drop the notification and enqueue a "dropped
3730 * notification" message if this is the first dropped
3731 * notification since the socket spilled-over to the
3734 ret
= client_notification_overflow(client
);
3741 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
3747 transmission_status
= client_flush_outgoing_queue(client
);
3748 pthread_mutex_unlock(&client
->lock
);
3749 ret
= client_report(client
, transmission_status
, user_data
);
3752 goto end_unlock_list
;
3758 pthread_mutex_unlock(&client
->lock
);
3761 goto end_unlock_list
;
3767 pthread_mutex_unlock(&client_list
->lock
);
3769 lttng_payload_reset(&msg_payload
);
3773 int handle_notification_thread_channel_sample(
3774 struct notification_thread_state
*state
, int pipe
,
3775 enum lttng_domain_type domain
)
3778 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
3779 struct channel_info
*channel_info
;
3780 struct cds_lfht_node
*node
;
3781 struct cds_lfht_iter iter
;
3782 struct lttng_channel_trigger_list
*trigger_list
;
3783 struct lttng_trigger_list_element
*trigger_list_element
;
3784 bool previous_sample_available
= false;
3785 struct channel_state_sample previous_sample
, latest_sample
;
3786 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
3787 struct lttng_credentials channel_creds
;
3790 * The monitoring pipe only holds messages smaller than PIPE_BUF,
3791 * ensuring that read/write of sampling messages are atomic.
3793 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
3794 if (ret
!= sizeof(sample_msg
)) {
3795 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
3802 latest_sample
.key
.key
= sample_msg
.key
;
3803 latest_sample
.key
.domain
= domain
;
3804 latest_sample
.highest_usage
= sample_msg
.highest
;
3805 latest_sample
.lowest_usage
= sample_msg
.lowest
;
3806 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
3810 /* Retrieve the channel's informations */
3811 cds_lfht_lookup(state
->channels_ht
,
3812 hash_channel_key(&latest_sample
.key
),
3816 node
= cds_lfht_iter_get_node(&iter
);
3817 if (caa_unlikely(!node
)) {
3819 * Not an error since the consumer can push a sample to the pipe
3820 * and the rest of the session daemon could notify us of the
3821 * channel's destruction before we get a chance to process that
3824 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
3825 latest_sample
.key
.key
,
3826 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
3830 channel_info
= caa_container_of(node
, struct channel_info
,
3832 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
3834 latest_sample
.key
.key
,
3835 channel_info
->session_info
->name
,
3836 latest_sample
.highest_usage
,
3837 latest_sample
.lowest_usage
,
3838 latest_sample
.channel_total_consumed
);
3840 previous_session_consumed_total
=
3841 channel_info
->session_info
->consumed_data_size
;
3843 /* Retrieve the channel's last sample, if it exists, and update it. */
3844 cds_lfht_lookup(state
->channel_state_ht
,
3845 hash_channel_key(&latest_sample
.key
),
3846 match_channel_state_sample
,
3849 node
= cds_lfht_iter_get_node(&iter
);
3850 if (caa_likely(node
)) {
3851 struct channel_state_sample
*stored_sample
;
3853 /* Update the sample stored. */
3854 stored_sample
= caa_container_of(node
,
3855 struct channel_state_sample
,
3856 channel_state_ht_node
);
3858 memcpy(&previous_sample
, stored_sample
,
3859 sizeof(previous_sample
));
3860 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
3861 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
3862 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
3863 previous_sample_available
= true;
3865 latest_session_consumed_total
=
3866 previous_session_consumed_total
+
3867 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
3870 * This is the channel's first sample, allocate space for and
3871 * store the new sample.
3873 struct channel_state_sample
*stored_sample
;
3875 stored_sample
= zmalloc(sizeof(*stored_sample
));
3876 if (!stored_sample
) {
3881 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
3882 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
3883 cds_lfht_add(state
->channel_state_ht
,
3884 hash_channel_key(&stored_sample
->key
),
3885 &stored_sample
->channel_state_ht_node
);
3887 latest_session_consumed_total
=
3888 previous_session_consumed_total
+
3889 latest_sample
.channel_total_consumed
;
3892 channel_info
->session_info
->consumed_data_size
=
3893 latest_session_consumed_total
;
3895 /* Find triggers associated with this channel. */
3896 cds_lfht_lookup(state
->channel_triggers_ht
,
3897 hash_channel_key(&latest_sample
.key
),
3898 match_channel_trigger_list
,
3901 node
= cds_lfht_iter_get_node(&iter
);
3902 if (caa_likely(!node
)) {
3906 channel_creds
= (typeof(channel_creds
)) {
3907 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
3908 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
3911 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
3912 channel_triggers_ht_node
);
3913 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
3915 const struct lttng_condition
*condition
;
3916 struct lttng_trigger
*trigger
;
3917 struct notification_client_list
*client_list
= NULL
;
3918 struct lttng_evaluation
*evaluation
= NULL
;
3919 enum action_executor_status executor_status
;
3922 trigger
= trigger_list_element
->trigger
;
3923 condition
= lttng_trigger_get_const_condition(trigger
);
3927 * Check if any client is subscribed to the result of this
3930 client_list
= get_client_list_from_condition(state
, condition
);
3932 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
3933 previous_sample_available
? &previous_sample
: NULL
,
3935 previous_session_consumed_total
,
3936 latest_session_consumed_total
,
3938 if (caa_unlikely(ret
)) {
3942 if (caa_likely(!evaluation
)) {
3947 * Ownership of `evaluation` transferred to the action executor
3948 * no matter the result.
3950 executor_status
= action_executor_enqueue(state
->executor
,
3951 trigger
, evaluation
, &channel_creds
,
3954 switch (executor_status
) {
3955 case ACTION_EXECUTOR_STATUS_OK
:
3957 case ACTION_EXECUTOR_STATUS_ERROR
:
3958 case ACTION_EXECUTOR_STATUS_INVALID
:
3960 * TODO Add trigger identification (name/id) when
3961 * it is added to the API.
3963 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
3966 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
3968 * TODO Add trigger identification (name/id) when
3969 * it is added to the API.
3971 * Not a fatal error.
3973 WARN("No space left when enqueuing action associated with buffer-condition trigger");
3981 notification_client_list_put(client_list
);
3982 if (caa_unlikely(ret
)) {