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 struct cds_lfht_node node_by_name_uid
;
115 /* call_rcu delayed reclaim. */
116 struct rcu_head rcu_node
;
119 struct lttng_condition_list_element
{
120 struct lttng_condition
*condition
;
121 struct cds_list_head node
;
124 struct channel_state_sample
{
125 struct channel_key key
;
126 struct cds_lfht_node channel_state_ht_node
;
127 uint64_t highest_usage
;
128 uint64_t lowest_usage
;
129 uint64_t channel_total_consumed
;
130 /* call_rcu delayed reclaim. */
131 struct rcu_head rcu_node
;
134 static unsigned long hash_channel_key(struct channel_key
*key
);
135 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
136 struct lttng_evaluation
**evaluation
,
137 const struct notification_thread_state
*state
,
138 const struct channel_state_sample
*previous_sample
,
139 const struct channel_state_sample
*latest_sample
,
140 uint64_t previous_session_consumed_total
,
141 uint64_t latest_session_consumed_total
,
142 struct channel_info
*channel_info
);
144 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
145 const struct lttng_evaluation
*evaluation
,
146 struct notification_client_list
*client_list
,
147 struct notification_thread_state
*state
,
148 uid_t channel_uid
, gid_t channel_gid
);
151 /* session_info API */
153 void session_info_destroy(void *_data
);
155 void session_info_get(struct session_info
*session_info
);
157 void session_info_put(struct session_info
*session_info
);
159 struct session_info
*session_info_create(const char *name
,
160 uid_t uid
, gid_t gid
,
161 struct lttng_session_trigger_list
*trigger_list
,
162 struct cds_lfht
*sessions_ht
);
164 void session_info_add_channel(struct session_info
*session_info
,
165 struct channel_info
*channel_info
);
167 void session_info_remove_channel(struct session_info
*session_info
,
168 struct channel_info
*channel_info
);
170 /* lttng_session_trigger_list API */
172 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
173 const char *session_name
,
174 struct cds_lfht
*session_triggers_ht
);
176 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
177 const struct notification_thread_state
*state
,
178 const char *session_name
);
180 void lttng_session_trigger_list_destroy(
181 struct lttng_session_trigger_list
*list
);
183 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
184 struct lttng_trigger
*trigger
);
187 int client_handle_transmission_status(
188 struct notification_client
*client
,
189 enum client_transmission_status transmission_status
,
190 struct notification_thread_state
*state
);
193 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
196 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
198 /* This double-cast is intended to supress pointer-to-cast warning. */
199 const int socket
= (int) (intptr_t) key
;
200 const struct notification_client
*client
= caa_container_of(node
,
201 struct notification_client
, client_socket_ht_node
);
203 return client
->socket
== socket
;
207 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
209 /* This double-cast is intended to supress pointer-to-cast warning. */
210 const notification_client_id id
= *((notification_client_id
*) key
);
211 const struct notification_client
*client
= caa_container_of(
212 node
, struct notification_client
, client_id_ht_node
);
214 return client
->id
== id
;
218 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
220 struct channel_key
*channel_key
= (struct channel_key
*) key
;
221 struct lttng_channel_trigger_list
*trigger_list
;
223 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
224 channel_triggers_ht_node
);
226 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
227 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
231 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
233 const char *session_name
= (const char *) key
;
234 struct lttng_session_trigger_list
*trigger_list
;
236 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
237 session_triggers_ht_node
);
239 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
243 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
245 struct channel_key
*channel_key
= (struct channel_key
*) key
;
246 struct channel_state_sample
*sample
;
248 sample
= caa_container_of(node
, struct channel_state_sample
,
249 channel_state_ht_node
);
251 return !!((channel_key
->key
== sample
->key
.key
) &&
252 (channel_key
->domain
== sample
->key
.domain
));
256 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
258 struct channel_key
*channel_key
= (struct channel_key
*) key
;
259 struct channel_info
*channel_info
;
261 channel_info
= caa_container_of(node
, struct channel_info
,
264 return !!((channel_key
->key
== channel_info
->key
.key
) &&
265 (channel_key
->domain
== channel_info
->key
.domain
));
269 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
271 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
272 struct lttng_trigger_ht_element
*trigger_ht_element
;
274 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
277 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
281 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
283 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
284 struct notification_client_list
*client_list
;
285 const struct lttng_condition
*condition
;
287 assert(condition_key
);
289 client_list
= caa_container_of(node
, struct notification_client_list
,
290 notification_trigger_clients_ht_node
);
291 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
293 return !!lttng_condition_is_equal(condition_key
, condition
);
297 int match_session(struct cds_lfht_node
*node
, const void *key
)
299 const char *name
= key
;
300 struct session_info
*session_info
= caa_container_of(
301 node
, struct session_info
, sessions_ht_node
);
303 return !strcmp(session_info
->name
, name
);
307 * Match trigger based on name and credentials only.
308 * Name duplication is NOT allowed for the same uid.
311 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
316 const char *key_name
;
317 enum lttng_trigger_status status
;
318 const struct lttng_credentials
*key_creds
;
319 const struct lttng_credentials
*node_creds
;
320 const struct lttng_trigger
*trigger_key
=
321 (const struct lttng_trigger
*) key
;
322 const struct lttng_trigger_ht_element
*trigger_ht_element
=
323 caa_container_of(node
,
324 struct lttng_trigger_ht_element
,
327 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
328 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
330 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
331 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
333 /* Compare the names. */
334 if (strcmp(name
, key_name
) != 0) {
338 /* Compare the owners' UIDs. */
339 key_creds
= lttng_trigger_get_credentials(trigger_key
);
340 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
342 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
349 * Hash trigger based on name and credentials only.
352 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
354 unsigned long hash
= 0;
355 const struct lttng_credentials
*trigger_creds
;
356 const char *trigger_name
;
357 enum lttng_trigger_status status
;
359 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
360 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
361 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
364 trigger_creds
= lttng_trigger_get_credentials(trigger
);
365 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
372 unsigned long lttng_condition_buffer_usage_hash(
373 const struct lttng_condition
*_condition
)
376 unsigned long condition_type
;
377 struct lttng_condition_buffer_usage
*condition
;
379 condition
= container_of(_condition
,
380 struct lttng_condition_buffer_usage
, parent
);
382 condition_type
= (unsigned long) condition
->parent
.type
;
383 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
384 if (condition
->session_name
) {
385 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
387 if (condition
->channel_name
) {
388 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
390 if (condition
->domain
.set
) {
391 hash
^= hash_key_ulong(
392 (void *) condition
->domain
.type
,
395 if (condition
->threshold_ratio
.set
) {
398 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
399 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
400 } else if (condition
->threshold_bytes
.set
) {
403 val
= condition
->threshold_bytes
.value
;
404 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
410 unsigned long lttng_condition_session_consumed_size_hash(
411 const struct lttng_condition
*_condition
)
414 unsigned long condition_type
=
415 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
416 struct lttng_condition_session_consumed_size
*condition
;
419 condition
= container_of(_condition
,
420 struct lttng_condition_session_consumed_size
, parent
);
422 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
423 if (condition
->session_name
) {
424 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
426 val
= condition
->consumed_threshold_bytes
.value
;
427 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
432 unsigned long lttng_condition_session_rotation_hash(
433 const struct lttng_condition
*_condition
)
435 unsigned long hash
, condition_type
;
436 struct lttng_condition_session_rotation
*condition
;
438 condition
= container_of(_condition
,
439 struct lttng_condition_session_rotation
, parent
);
440 condition_type
= (unsigned long) condition
->parent
.type
;
441 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
442 assert(condition
->session_name
);
443 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
448 * The lttng_condition hashing code is kept in this file (rather than
449 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
450 * don't want to link in liblttng-ctl.
453 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
455 switch (condition
->type
) {
456 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
457 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
458 return lttng_condition_buffer_usage_hash(condition
);
459 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
460 return lttng_condition_session_consumed_size_hash(condition
);
461 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
462 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
463 return lttng_condition_session_rotation_hash(condition
);
465 ERR("[notification-thread] Unexpected condition type caught");
471 unsigned long hash_channel_key(struct channel_key
*key
)
473 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
474 unsigned long domain_hash
= hash_key_ulong(
475 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
477 return key_hash
^ domain_hash
;
481 unsigned long hash_client_socket(int socket
)
483 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
487 unsigned long hash_client_id(notification_client_id id
)
489 return hash_key_u64(&id
, lttng_ht_seed
);
493 * Get the type of object to which a given condition applies. Bindings let
494 * the notification system evaluate a trigger's condition when a given
495 * object's state is updated.
497 * For instance, a condition bound to a channel will be evaluated everytime
498 * the channel's state is changed by a channel monitoring sample.
501 enum lttng_object_type
get_condition_binding_object(
502 const struct lttng_condition
*condition
)
504 switch (lttng_condition_get_type(condition
)) {
505 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
506 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
507 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
508 return LTTNG_OBJECT_TYPE_CHANNEL
;
509 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
510 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
511 return LTTNG_OBJECT_TYPE_SESSION
;
513 return LTTNG_OBJECT_TYPE_UNKNOWN
;
518 void free_channel_info_rcu(struct rcu_head
*node
)
520 free(caa_container_of(node
, struct channel_info
, rcu_node
));
524 void channel_info_destroy(struct channel_info
*channel_info
)
530 if (channel_info
->session_info
) {
531 session_info_remove_channel(channel_info
->session_info
,
533 session_info_put(channel_info
->session_info
);
535 if (channel_info
->name
) {
536 free(channel_info
->name
);
538 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
542 void free_session_info_rcu(struct rcu_head
*node
)
544 free(caa_container_of(node
, struct session_info
, rcu_node
));
547 /* Don't call directly, use the ref-counting mechanism. */
549 void session_info_destroy(void *_data
)
551 struct session_info
*session_info
= _data
;
554 assert(session_info
);
555 if (session_info
->channel_infos_ht
) {
556 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
558 ERR("[notification-thread] Failed to destroy channel information hash table");
561 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
564 cds_lfht_del(session_info
->sessions_ht
,
565 &session_info
->sessions_ht_node
);
567 free(session_info
->name
);
568 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
572 void session_info_get(struct session_info
*session_info
)
577 lttng_ref_get(&session_info
->ref
);
581 void session_info_put(struct session_info
*session_info
)
586 lttng_ref_put(&session_info
->ref
);
590 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
591 struct lttng_session_trigger_list
*trigger_list
,
592 struct cds_lfht
*sessions_ht
)
594 struct session_info
*session_info
;
598 session_info
= zmalloc(sizeof(*session_info
));
602 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
604 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
605 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
606 if (!session_info
->channel_infos_ht
) {
610 cds_lfht_node_init(&session_info
->sessions_ht_node
);
611 session_info
->name
= strdup(name
);
612 if (!session_info
->name
) {
615 session_info
->uid
= uid
;
616 session_info
->gid
= gid
;
617 session_info
->trigger_list
= trigger_list
;
618 session_info
->sessions_ht
= sessions_ht
;
622 session_info_put(session_info
);
627 void session_info_add_channel(struct session_info
*session_info
,
628 struct channel_info
*channel_info
)
631 cds_lfht_add(session_info
->channel_infos_ht
,
632 hash_channel_key(&channel_info
->key
),
633 &channel_info
->session_info_channels_ht_node
);
638 void session_info_remove_channel(struct session_info
*session_info
,
639 struct channel_info
*channel_info
)
642 cds_lfht_del(session_info
->channel_infos_ht
,
643 &channel_info
->session_info_channels_ht_node
);
648 struct channel_info
*channel_info_create(const char *channel_name
,
649 struct channel_key
*channel_key
, uint64_t channel_capacity
,
650 struct session_info
*session_info
)
652 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
658 cds_lfht_node_init(&channel_info
->channels_ht_node
);
659 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
660 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
661 channel_info
->capacity
= channel_capacity
;
663 channel_info
->name
= strdup(channel_name
);
664 if (!channel_info
->name
) {
669 * Set the references between session and channel infos:
670 * - channel_info holds a strong reference to session_info
671 * - session_info holds a weak reference to channel_info
673 session_info_get(session_info
);
674 session_info_add_channel(session_info
, channel_info
);
675 channel_info
->session_info
= session_info
;
679 channel_info_destroy(channel_info
);
684 bool notification_client_list_get(struct notification_client_list
*list
)
686 return urcu_ref_get_unless_zero(&list
->ref
);
690 void free_notification_client_list_rcu(struct rcu_head
*node
)
692 free(caa_container_of(node
, struct notification_client_list
,
697 void notification_client_list_release(struct urcu_ref
*list_ref
)
699 struct notification_client_list
*list
=
700 container_of(list_ref
, typeof(*list
), ref
);
701 struct notification_client_list_element
*client_list_element
, *tmp
;
703 if (list
->notification_trigger_clients_ht
) {
705 cds_lfht_del(list
->notification_trigger_clients_ht
,
706 &list
->notification_trigger_clients_ht_node
);
708 list
->notification_trigger_clients_ht
= NULL
;
710 cds_list_for_each_entry_safe(client_list_element
, tmp
,
712 free(client_list_element
);
714 pthread_mutex_destroy(&list
->lock
);
715 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
719 struct notification_client_list
*notification_client_list_create(
720 const struct lttng_trigger
*trigger
)
722 struct notification_client_list
*client_list
=
723 zmalloc(sizeof(*client_list
));
728 pthread_mutex_init(&client_list
->lock
, NULL
);
729 urcu_ref_init(&client_list
->ref
);
730 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
731 CDS_INIT_LIST_HEAD(&client_list
->list
);
732 client_list
->trigger
= trigger
;
738 void publish_notification_client_list(
739 struct notification_thread_state
*state
,
740 struct notification_client_list
*list
)
742 const struct lttng_condition
*condition
=
743 lttng_trigger_get_const_condition(list
->trigger
);
745 assert(!list
->notification_trigger_clients_ht
);
746 notification_client_list_get(list
);
748 list
->notification_trigger_clients_ht
=
749 state
->notification_trigger_clients_ht
;
752 cds_lfht_add(state
->notification_trigger_clients_ht
,
753 lttng_condition_hash(condition
),
754 &list
->notification_trigger_clients_ht_node
);
759 void notification_client_list_put(struct notification_client_list
*list
)
764 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
767 /* Provides a reference to the returned list. */
769 struct notification_client_list
*get_client_list_from_condition(
770 struct notification_thread_state
*state
,
771 const struct lttng_condition
*condition
)
773 struct cds_lfht_node
*node
;
774 struct cds_lfht_iter iter
;
775 struct notification_client_list
*list
= NULL
;
778 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
779 lttng_condition_hash(condition
),
780 match_client_list_condition
,
783 node
= cds_lfht_iter_get_node(&iter
);
785 list
= container_of(node
, struct notification_client_list
,
786 notification_trigger_clients_ht_node
);
787 list
= notification_client_list_get(list
) ? list
: NULL
;
795 int evaluate_channel_condition_for_client(
796 const struct lttng_condition
*condition
,
797 struct notification_thread_state
*state
,
798 struct lttng_evaluation
**evaluation
,
799 uid_t
*session_uid
, gid_t
*session_gid
)
802 struct cds_lfht_iter iter
;
803 struct cds_lfht_node
*node
;
804 struct channel_info
*channel_info
= NULL
;
805 struct channel_key
*channel_key
= NULL
;
806 struct channel_state_sample
*last_sample
= NULL
;
807 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
811 /* Find the channel associated with the condition. */
812 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
813 channel_trigger_list
, channel_triggers_ht_node
) {
814 struct lttng_trigger_list_element
*element
;
816 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
817 const struct lttng_condition
*current_condition
=
818 lttng_trigger_get_const_condition(
821 assert(current_condition
);
822 if (!lttng_condition_is_equal(condition
,
823 current_condition
)) {
827 /* Found the trigger, save the channel key. */
828 channel_key
= &channel_trigger_list
->channel_key
;
832 /* The channel key was found stop iteration. */
838 /* No channel found; normal exit. */
839 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
844 /* Fetch channel info for the matching channel. */
845 cds_lfht_lookup(state
->channels_ht
,
846 hash_channel_key(channel_key
),
850 node
= cds_lfht_iter_get_node(&iter
);
852 channel_info
= caa_container_of(node
, struct channel_info
,
855 /* Retrieve the channel's last sample, if it exists. */
856 cds_lfht_lookup(state
->channel_state_ht
,
857 hash_channel_key(channel_key
),
858 match_channel_state_sample
,
861 node
= cds_lfht_iter_get_node(&iter
);
863 last_sample
= caa_container_of(node
,
864 struct channel_state_sample
,
865 channel_state_ht_node
);
867 /* Nothing to evaluate, no sample was ever taken. Normal exit */
868 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
873 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
875 0, channel_info
->session_info
->consumed_data_size
,
878 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
882 *session_uid
= channel_info
->session_info
->uid
;
883 *session_gid
= channel_info
->session_info
->gid
;
890 const char *get_condition_session_name(const struct lttng_condition
*condition
)
892 const char *session_name
= NULL
;
893 enum lttng_condition_status status
;
895 switch (lttng_condition_get_type(condition
)) {
896 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
897 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
898 status
= lttng_condition_buffer_usage_get_session_name(
899 condition
, &session_name
);
901 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
902 status
= lttng_condition_session_consumed_size_get_session_name(
903 condition
, &session_name
);
905 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
906 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
907 status
= lttng_condition_session_rotation_get_session_name(
908 condition
, &session_name
);
913 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
914 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
922 int evaluate_session_condition_for_client(
923 const struct lttng_condition
*condition
,
924 struct notification_thread_state
*state
,
925 struct lttng_evaluation
**evaluation
,
926 uid_t
*session_uid
, gid_t
*session_gid
)
929 struct cds_lfht_iter iter
;
930 struct cds_lfht_node
*node
;
931 const char *session_name
;
932 struct session_info
*session_info
= NULL
;
935 session_name
= get_condition_session_name(condition
);
937 /* Find the session associated with the trigger. */
938 cds_lfht_lookup(state
->sessions_ht
,
939 hash_key_str(session_name
, lttng_ht_seed
),
943 node
= cds_lfht_iter_get_node(&iter
);
945 DBG("[notification-thread] No known session matching name \"%s\"",
951 session_info
= caa_container_of(node
, struct session_info
,
953 session_info_get(session_info
);
956 * Evaluation is performed in-line here since only one type of
957 * session-bound condition is handled for the moment.
959 switch (lttng_condition_get_type(condition
)) {
960 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
961 if (!session_info
->rotation
.ongoing
) {
963 goto end_session_put
;
966 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
967 session_info
->rotation
.id
);
970 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
973 goto end_session_put
;
979 goto end_session_put
;
982 *session_uid
= session_info
->uid
;
983 *session_gid
= session_info
->gid
;
986 session_info_put(session_info
);
993 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
994 const struct lttng_condition
*condition
,
995 struct notification_client
*client
,
996 struct notification_thread_state
*state
)
999 struct lttng_evaluation
*evaluation
= NULL
;
1000 struct notification_client_list client_list
= {
1001 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1003 struct notification_client_list_element client_list_element
= { 0 };
1004 uid_t object_uid
= 0;
1005 gid_t object_gid
= 0;
1012 switch (get_condition_binding_object(condition
)) {
1013 case LTTNG_OBJECT_TYPE_SESSION
:
1014 ret
= evaluate_session_condition_for_client(condition
, state
,
1015 &evaluation
, &object_uid
, &object_gid
);
1017 case LTTNG_OBJECT_TYPE_CHANNEL
:
1018 ret
= evaluate_channel_condition_for_client(condition
, state
,
1019 &evaluation
, &object_uid
, &object_gid
);
1021 case LTTNG_OBJECT_TYPE_NONE
:
1024 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1034 /* Evaluation yielded nothing. Normal exit. */
1035 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
1041 * Create a temporary client list with the client currently
1044 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1045 CDS_INIT_LIST_HEAD(&client_list
.list
);
1046 client_list
.trigger
= trigger
;
1048 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1049 client_list_element
.client
= client
;
1050 cds_list_add(&client_list_element
.node
, &client_list
.list
);
1052 /* Send evaluation result to the newly-subscribed client. */
1053 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1054 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1055 state
, object_uid
, object_gid
);
1062 int notification_thread_client_subscribe(struct notification_client
*client
,
1063 struct lttng_condition
*condition
,
1064 struct notification_thread_state
*state
,
1065 enum lttng_notification_channel_status
*_status
)
1068 struct notification_client_list
*client_list
= NULL
;
1069 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1070 struct notification_client_list_element
*client_list_element
= NULL
;
1071 enum lttng_notification_channel_status status
=
1072 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1075 * Ensure that the client has not already subscribed to this condition
1078 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1079 if (lttng_condition_is_equal(condition_list_element
->condition
,
1081 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1086 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1087 if (!condition_list_element
) {
1091 client_list_element
= zmalloc(sizeof(*client_list_element
));
1092 if (!client_list_element
) {
1098 * Add the newly-subscribed condition to the client's subscription list.
1100 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1101 condition_list_element
->condition
= condition
;
1102 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1104 client_list
= get_client_list_from_condition(state
, condition
);
1107 * No notification-emiting trigger registered with this
1108 * condition. We don't evaluate the condition right away
1109 * since this trigger is not registered yet.
1111 free(client_list_element
);
1116 * The condition to which the client just subscribed is evaluated
1117 * at this point so that conditions that are already TRUE result
1118 * in a notification being sent out.
1120 * The client_list's trigger is used without locking the list itself.
1121 * This is correct since the list doesn't own the trigger and the
1122 * object is immutable.
1124 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1126 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1128 free(client_list_element
);
1133 * Add the client to the list of clients interested in a given trigger
1134 * if a "notification" trigger with a corresponding condition was
1137 client_list_element
->client
= client
;
1138 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1140 pthread_mutex_lock(&client_list
->lock
);
1141 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1142 pthread_mutex_unlock(&client_list
->lock
);
1148 notification_client_list_put(client_list
);
1152 free(condition_list_element
);
1153 free(client_list_element
);
1158 int notification_thread_client_unsubscribe(
1159 struct notification_client
*client
,
1160 struct lttng_condition
*condition
,
1161 struct notification_thread_state
*state
,
1162 enum lttng_notification_channel_status
*_status
)
1164 struct notification_client_list
*client_list
;
1165 struct lttng_condition_list_element
*condition_list_element
,
1167 struct notification_client_list_element
*client_list_element
,
1169 bool condition_found
= false;
1170 enum lttng_notification_channel_status status
=
1171 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1173 /* Remove the condition from the client's condition list. */
1174 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1175 &client
->condition_list
, node
) {
1176 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1181 cds_list_del(&condition_list_element
->node
);
1183 * The caller may be iterating on the client's conditions to
1184 * tear down a client's connection. In this case, the condition
1185 * will be destroyed at the end.
1187 if (condition
!= condition_list_element
->condition
) {
1188 lttng_condition_destroy(
1189 condition_list_element
->condition
);
1191 free(condition_list_element
);
1192 condition_found
= true;
1196 if (!condition_found
) {
1197 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1202 * Remove the client from the list of clients interested the trigger
1203 * matching the condition.
1205 client_list
= get_client_list_from_condition(state
, condition
);
1210 pthread_mutex_lock(&client_list
->lock
);
1211 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1212 &client_list
->list
, node
) {
1213 if (client_list_element
->client
->id
!= client
->id
) {
1216 cds_list_del(&client_list_element
->node
);
1217 free(client_list_element
);
1220 pthread_mutex_unlock(&client_list
->lock
);
1221 notification_client_list_put(client_list
);
1224 lttng_condition_destroy(condition
);
1232 void free_notification_client_rcu(struct rcu_head
*node
)
1234 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1238 void notification_client_destroy(struct notification_client
*client
,
1239 struct notification_thread_state
*state
)
1246 * The client object is not reachable by other threads, no need to lock
1249 if (client
->socket
>= 0) {
1250 (void) lttcomm_close_unix_sock(client
->socket
);
1251 client
->socket
= -1;
1253 client
->communication
.active
= false;
1254 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1255 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1256 pthread_mutex_destroy(&client
->lock
);
1257 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1261 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1265 struct notification_client
*get_client_from_socket(int socket
,
1266 struct notification_thread_state
*state
)
1268 struct cds_lfht_iter iter
;
1269 struct cds_lfht_node
*node
;
1270 struct notification_client
*client
= NULL
;
1272 cds_lfht_lookup(state
->client_socket_ht
,
1273 hash_client_socket(socket
),
1274 match_client_socket
,
1275 (void *) (unsigned long) socket
,
1277 node
= cds_lfht_iter_get_node(&iter
);
1282 client
= caa_container_of(node
, struct notification_client
,
1283 client_socket_ht_node
);
1289 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1293 struct notification_client
*get_client_from_id(notification_client_id id
,
1294 struct notification_thread_state
*state
)
1296 struct cds_lfht_iter iter
;
1297 struct cds_lfht_node
*node
;
1298 struct notification_client
*client
= NULL
;
1300 cds_lfht_lookup(state
->client_id_ht
,
1305 node
= cds_lfht_iter_get_node(&iter
);
1310 client
= caa_container_of(node
, struct notification_client
,
1317 bool buffer_usage_condition_applies_to_channel(
1318 const struct lttng_condition
*condition
,
1319 const struct channel_info
*channel_info
)
1321 enum lttng_condition_status status
;
1322 enum lttng_domain_type condition_domain
;
1323 const char *condition_session_name
= NULL
;
1324 const char *condition_channel_name
= NULL
;
1326 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1328 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1329 if (channel_info
->key
.domain
!= condition_domain
) {
1333 status
= lttng_condition_buffer_usage_get_session_name(
1334 condition
, &condition_session_name
);
1335 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1337 status
= lttng_condition_buffer_usage_get_channel_name(
1338 condition
, &condition_channel_name
);
1339 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1341 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1344 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1354 bool session_consumed_size_condition_applies_to_channel(
1355 const struct lttng_condition
*condition
,
1356 const struct channel_info
*channel_info
)
1358 enum lttng_condition_status status
;
1359 const char *condition_session_name
= NULL
;
1361 status
= lttng_condition_session_consumed_size_get_session_name(
1362 condition
, &condition_session_name
);
1363 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1365 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1375 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1376 const struct channel_info
*channel_info
)
1378 const struct lttng_condition
*condition
;
1379 bool trigger_applies
;
1381 condition
= lttng_trigger_get_const_condition(trigger
);
1386 switch (lttng_condition_get_type(condition
)) {
1387 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1388 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1389 trigger_applies
= buffer_usage_condition_applies_to_channel(
1390 condition
, channel_info
);
1392 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1393 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1394 condition
, channel_info
);
1400 return trigger_applies
;
1406 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1407 struct notification_client
*client
)
1409 bool applies
= false;
1410 struct lttng_condition_list_element
*condition_list_element
;
1412 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1414 applies
= lttng_condition_is_equal(
1415 condition_list_element
->condition
,
1416 lttng_trigger_get_condition(trigger
));
1424 /* Must be called with RCU read lock held. */
1426 struct lttng_session_trigger_list
*get_session_trigger_list(
1427 struct notification_thread_state
*state
,
1428 const char *session_name
)
1430 struct lttng_session_trigger_list
*list
= NULL
;
1431 struct cds_lfht_node
*node
;
1432 struct cds_lfht_iter iter
;
1434 cds_lfht_lookup(state
->session_triggers_ht
,
1435 hash_key_str(session_name
, lttng_ht_seed
),
1436 match_session_trigger_list
,
1439 node
= cds_lfht_iter_get_node(&iter
);
1442 * Not an error, the list of triggers applying to that session
1443 * will be initialized when the session is created.
1445 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1450 list
= caa_container_of(node
,
1451 struct lttng_session_trigger_list
,
1452 session_triggers_ht_node
);
1458 * Allocate an empty lttng_session_trigger_list for the session named
1461 * No ownership of 'session_name' is assumed by the session trigger list.
1462 * It is the caller's responsability to ensure the session name is alive
1463 * for as long as this list is.
1466 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1467 const char *session_name
,
1468 struct cds_lfht
*session_triggers_ht
)
1470 struct lttng_session_trigger_list
*list
;
1472 list
= zmalloc(sizeof(*list
));
1476 list
->session_name
= session_name
;
1477 CDS_INIT_LIST_HEAD(&list
->list
);
1478 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1479 list
->session_triggers_ht
= session_triggers_ht
;
1482 /* Publish the list through the session_triggers_ht. */
1483 cds_lfht_add(session_triggers_ht
,
1484 hash_key_str(session_name
, lttng_ht_seed
),
1485 &list
->session_triggers_ht_node
);
1492 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1494 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1499 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1501 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1503 /* Empty the list element by element, and then free the list itself. */
1504 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1505 &list
->list
, node
) {
1506 cds_list_del(&trigger_list_element
->node
);
1507 free(trigger_list_element
);
1510 /* Unpublish the list from the session_triggers_ht. */
1511 cds_lfht_del(list
->session_triggers_ht
,
1512 &list
->session_triggers_ht_node
);
1514 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1518 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1519 struct lttng_trigger
*trigger
)
1522 struct lttng_trigger_list_element
*new_element
=
1523 zmalloc(sizeof(*new_element
));
1529 CDS_INIT_LIST_HEAD(&new_element
->node
);
1530 new_element
->trigger
= trigger
;
1531 cds_list_add(&new_element
->node
, &list
->list
);
1537 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1538 const char *session_name
)
1540 bool applies
= false;
1541 const struct lttng_condition
*condition
;
1543 condition
= lttng_trigger_get_const_condition(trigger
);
1544 switch (lttng_condition_get_type(condition
)) {
1545 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1546 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1548 enum lttng_condition_status condition_status
;
1549 const char *condition_session_name
;
1551 condition_status
= lttng_condition_session_rotation_get_session_name(
1552 condition
, &condition_session_name
);
1553 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1554 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1558 assert(condition_session_name
);
1559 applies
= !strcmp(condition_session_name
, session_name
);
1570 * Allocate and initialize an lttng_session_trigger_list which contains
1571 * all triggers that apply to the session named 'session_name'.
1573 * No ownership of 'session_name' is assumed by the session trigger list.
1574 * It is the caller's responsability to ensure the session name is alive
1575 * for as long as this list is.
1578 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1579 const struct notification_thread_state
*state
,
1580 const char *session_name
)
1582 int trigger_count
= 0;
1583 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1584 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1585 struct cds_lfht_iter iter
;
1587 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1588 state
->session_triggers_ht
);
1590 /* Add all triggers applying to the session named 'session_name'. */
1591 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1595 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1600 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1601 trigger_ht_element
->trigger
);
1609 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1611 return session_trigger_list
;
1613 lttng_session_trigger_list_destroy(session_trigger_list
);
1618 struct session_info
*find_or_create_session_info(
1619 struct notification_thread_state
*state
,
1620 const char *name
, uid_t uid
, gid_t gid
)
1622 struct session_info
*session
= NULL
;
1623 struct cds_lfht_node
*node
;
1624 struct cds_lfht_iter iter
;
1625 struct lttng_session_trigger_list
*trigger_list
;
1628 cds_lfht_lookup(state
->sessions_ht
,
1629 hash_key_str(name
, lttng_ht_seed
),
1633 node
= cds_lfht_iter_get_node(&iter
);
1635 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1637 session
= caa_container_of(node
, struct session_info
,
1639 assert(session
->uid
== uid
);
1640 assert(session
->gid
== gid
);
1641 session_info_get(session
);
1645 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1646 if (!trigger_list
) {
1650 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1651 state
->sessions_ht
);
1653 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1655 lttng_session_trigger_list_destroy(trigger_list
);
1658 trigger_list
= NULL
;
1660 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1661 &session
->sessions_ht_node
);
1667 session_info_put(session
);
1672 int handle_notification_thread_command_add_channel(
1673 struct notification_thread_state
*state
,
1674 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1675 const char *channel_name
, enum lttng_domain_type channel_domain
,
1676 uint64_t channel_key_int
, uint64_t channel_capacity
,
1677 enum lttng_error_code
*cmd_result
)
1679 struct cds_list_head trigger_list
;
1680 struct channel_info
*new_channel_info
= NULL
;
1681 struct channel_key channel_key
= {
1682 .key
= channel_key_int
,
1683 .domain
= channel_domain
,
1685 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1686 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1687 int trigger_count
= 0;
1688 struct cds_lfht_iter iter
;
1689 struct session_info
*session_info
= NULL
;
1691 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1692 channel_name
, session_name
, channel_key_int
,
1693 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1695 CDS_INIT_LIST_HEAD(&trigger_list
);
1697 session_info
= find_or_create_session_info(state
, session_name
,
1698 session_uid
, session_gid
);
1699 if (!session_info
) {
1700 /* Allocation error or an internal error occurred. */
1704 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1705 channel_capacity
, session_info
);
1706 if (!new_channel_info
) {
1711 /* Build a list of all triggers applying to the new channel. */
1712 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1714 struct lttng_trigger_list_element
*new_element
;
1716 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1717 new_channel_info
)) {
1721 new_element
= zmalloc(sizeof(*new_element
));
1726 CDS_INIT_LIST_HEAD(&new_element
->node
);
1727 new_element
->trigger
= trigger_ht_element
->trigger
;
1728 cds_list_add(&new_element
->node
, &trigger_list
);
1733 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1735 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1736 if (!channel_trigger_list
) {
1739 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1740 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1741 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1742 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1745 /* Add channel to the channel_ht which owns the channel_infos. */
1746 cds_lfht_add(state
->channels_ht
,
1747 hash_channel_key(&new_channel_info
->key
),
1748 &new_channel_info
->channels_ht_node
);
1750 * Add the list of triggers associated with this channel to the
1751 * channel_triggers_ht.
1753 cds_lfht_add(state
->channel_triggers_ht
,
1754 hash_channel_key(&new_channel_info
->key
),
1755 &channel_trigger_list
->channel_triggers_ht_node
);
1757 session_info_put(session_info
);
1758 *cmd_result
= LTTNG_OK
;
1761 channel_info_destroy(new_channel_info
);
1762 session_info_put(session_info
);
1767 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1769 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1774 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1776 free(caa_container_of(node
, struct channel_state_sample
,
1781 int handle_notification_thread_command_remove_channel(
1782 struct notification_thread_state
*state
,
1783 uint64_t channel_key
, enum lttng_domain_type domain
,
1784 enum lttng_error_code
*cmd_result
)
1786 struct cds_lfht_node
*node
;
1787 struct cds_lfht_iter iter
;
1788 struct lttng_channel_trigger_list
*trigger_list
;
1789 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1790 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1791 struct channel_info
*channel_info
;
1793 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1794 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1798 cds_lfht_lookup(state
->channel_triggers_ht
,
1799 hash_channel_key(&key
),
1800 match_channel_trigger_list
,
1803 node
= cds_lfht_iter_get_node(&iter
);
1805 * There is a severe internal error if we are being asked to remove a
1806 * channel that doesn't exist.
1809 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1813 /* Free the list of triggers associated with this channel. */
1814 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1815 channel_triggers_ht_node
);
1816 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1817 &trigger_list
->list
, node
) {
1818 cds_list_del(&trigger_list_element
->node
);
1819 free(trigger_list_element
);
1821 cds_lfht_del(state
->channel_triggers_ht
, node
);
1822 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1824 /* Free sampled channel state. */
1825 cds_lfht_lookup(state
->channel_state_ht
,
1826 hash_channel_key(&key
),
1827 match_channel_state_sample
,
1830 node
= cds_lfht_iter_get_node(&iter
);
1832 * This is expected to be NULL if the channel is destroyed before we
1833 * received a sample.
1836 struct channel_state_sample
*sample
= caa_container_of(node
,
1837 struct channel_state_sample
,
1838 channel_state_ht_node
);
1840 cds_lfht_del(state
->channel_state_ht
, node
);
1841 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1844 /* Remove the channel from the channels_ht and free it. */
1845 cds_lfht_lookup(state
->channels_ht
,
1846 hash_channel_key(&key
),
1850 node
= cds_lfht_iter_get_node(&iter
);
1852 channel_info
= caa_container_of(node
, struct channel_info
,
1854 cds_lfht_del(state
->channels_ht
, node
);
1855 channel_info_destroy(channel_info
);
1858 *cmd_result
= LTTNG_OK
;
1863 int handle_notification_thread_command_session_rotation(
1864 struct notification_thread_state
*state
,
1865 enum notification_thread_command_type cmd_type
,
1866 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1867 uint64_t trace_archive_chunk_id
,
1868 struct lttng_trace_archive_location
*location
,
1869 enum lttng_error_code
*_cmd_result
)
1872 enum lttng_error_code cmd_result
= LTTNG_OK
;
1873 struct lttng_session_trigger_list
*trigger_list
;
1874 struct lttng_trigger_list_element
*trigger_list_element
;
1875 struct session_info
*session_info
;
1876 const struct lttng_credentials session_creds
= {
1877 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1878 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1883 session_info
= find_or_create_session_info(state
, session_name
,
1884 session_uid
, session_gid
);
1885 if (!session_info
) {
1886 /* Allocation error or an internal error occurred. */
1888 cmd_result
= LTTNG_ERR_NOMEM
;
1892 session_info
->rotation
.ongoing
=
1893 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1894 session_info
->rotation
.id
= trace_archive_chunk_id
;
1895 trigger_list
= get_session_trigger_list(state
, session_name
);
1896 if (!trigger_list
) {
1897 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1902 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1904 const struct lttng_condition
*condition
;
1905 struct lttng_trigger
*trigger
;
1906 struct notification_client_list
*client_list
;
1907 struct lttng_evaluation
*evaluation
= NULL
;
1908 enum lttng_condition_type condition_type
;
1909 enum action_executor_status executor_status
;
1911 trigger
= trigger_list_element
->trigger
;
1912 condition
= lttng_trigger_get_const_condition(trigger
);
1914 condition_type
= lttng_condition_get_type(condition
);
1916 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1917 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1919 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1920 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1924 client_list
= get_client_list_from_condition(state
, condition
);
1925 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1926 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1927 trace_archive_chunk_id
);
1929 evaluation
= lttng_evaluation_session_rotation_completed_create(
1930 trace_archive_chunk_id
, location
);
1934 /* Internal error */
1936 cmd_result
= LTTNG_ERR_UNK
;
1941 * Ownership of `evaluation` transferred to the action executor
1942 * no matter the result.
1944 executor_status
= action_executor_enqueue(state
->executor
,
1945 trigger
, evaluation
, &session_creds
,
1948 switch (executor_status
) {
1949 case ACTION_EXECUTOR_STATUS_OK
:
1951 case ACTION_EXECUTOR_STATUS_ERROR
:
1952 case ACTION_EXECUTOR_STATUS_INVALID
:
1954 * TODO Add trigger identification (name/id) when
1955 * it is added to the API.
1957 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1960 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1962 * TODO Add trigger identification (name/id) when
1963 * it is added to the API.
1965 * Not a fatal error.
1967 WARN("No space left when enqueuing action associated with session rotation trigger");
1975 notification_client_list_put(client_list
);
1976 if (caa_unlikely(ret
)) {
1981 session_info_put(session_info
);
1982 *_cmd_result
= cmd_result
;
1988 int condition_is_supported(struct lttng_condition
*condition
)
1992 switch (lttng_condition_get_type(condition
)) {
1993 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1994 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1996 enum lttng_domain_type domain
;
1998 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2005 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2011 * Older kernel tracers don't expose the API to monitor their
2012 * buffers. Therefore, we reject triggers that require that
2013 * mechanism to be available to be evaluated.
2015 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
2025 /* Must be called with RCU read lock held. */
2027 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2028 struct notification_thread_state
*state
)
2031 const struct lttng_condition
*condition
;
2032 const char *session_name
;
2033 struct lttng_session_trigger_list
*trigger_list
;
2035 condition
= lttng_trigger_get_const_condition(trigger
);
2036 switch (lttng_condition_get_type(condition
)) {
2037 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2038 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2040 enum lttng_condition_status status
;
2042 status
= lttng_condition_session_rotation_get_session_name(
2043 condition
, &session_name
);
2044 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2045 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2056 trigger_list
= get_session_trigger_list(state
, session_name
);
2057 if (!trigger_list
) {
2058 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2064 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2066 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2071 /* Must be called with RCU read lock held. */
2073 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2074 struct notification_thread_state
*state
)
2077 struct cds_lfht_node
*node
;
2078 struct cds_lfht_iter iter
;
2079 struct channel_info
*channel
;
2081 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2083 struct lttng_trigger_list_element
*trigger_list_element
;
2084 struct lttng_channel_trigger_list
*trigger_list
;
2085 struct cds_lfht_iter lookup_iter
;
2087 if (!trigger_applies_to_channel(trigger
, channel
)) {
2091 cds_lfht_lookup(state
->channel_triggers_ht
,
2092 hash_channel_key(&channel
->key
),
2093 match_channel_trigger_list
,
2096 node
= cds_lfht_iter_get_node(&lookup_iter
);
2098 trigger_list
= caa_container_of(node
,
2099 struct lttng_channel_trigger_list
,
2100 channel_triggers_ht_node
);
2102 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2103 if (!trigger_list_element
) {
2107 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2108 trigger_list_element
->trigger
= trigger
;
2109 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2110 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2118 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2120 bool is_notify
= false;
2121 unsigned int i
, count
;
2122 enum lttng_action_status action_status
;
2123 const struct lttng_action
*action
=
2124 lttng_trigger_get_const_action(trigger
);
2125 enum lttng_action_type action_type
;
2128 action_type
= lttng_action_get_type(action
);
2129 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2132 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2136 action_status
= lttng_action_group_get_count(action
, &count
);
2137 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2139 for (i
= 0; i
< count
; i
++) {
2140 const struct lttng_action
*inner_action
=
2141 lttng_action_group_get_at_index(
2144 action_type
= lttng_action_get_type(inner_action
);
2145 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2155 static bool trigger_name_taken(struct notification_thread_state
*state
,
2156 const struct lttng_trigger
*trigger
)
2158 struct cds_lfht_iter iter
;
2161 * No duplicata is allowed in the triggers_by_name_uid_ht.
2162 * The match is done against the trigger name and uid.
2164 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2165 hash_trigger_by_name_uid(trigger
),
2166 match_trigger_by_name_uid
,
2169 return !!cds_lfht_iter_get_node(&iter
);
2173 enum lttng_error_code
generate_trigger_name(
2174 struct notification_thread_state
*state
,
2175 struct lttng_trigger
*trigger
, const char **name
)
2177 enum lttng_error_code ret_code
= LTTNG_OK
;
2179 enum lttng_trigger_status status
;
2182 const int ret
= lttng_trigger_generate_name(trigger
,
2183 state
->trigger_id
.name_offset
++);
2185 /* The only reason this can fail right now. */
2186 ret_code
= LTTNG_ERR_NOMEM
;
2190 status
= lttng_trigger_get_name(trigger
, name
);
2191 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2193 taken
= trigger_name_taken(state
, trigger
);
2194 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2200 * FIXME A client's credentials are not checked when registering a trigger.
2202 * The effects of this are benign since:
2203 * - The client will succeed in registering the trigger, as it is valid,
2204 * - The trigger will, internally, be bound to the channel/session,
2205 * - The notifications will not be sent since the client's credentials
2206 * are checked against the channel at that moment.
2208 * If this function returns a non-zero value, it means something is
2209 * fundamentally broken and the whole subsystem/thread will be torn down.
2211 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2215 int handle_notification_thread_command_register_trigger(
2216 struct notification_thread_state
*state
,
2217 struct lttng_trigger
*trigger
,
2218 enum lttng_error_code
*cmd_result
)
2221 struct lttng_condition
*condition
;
2222 struct notification_client
*client
;
2223 struct notification_client_list
*client_list
= NULL
;
2224 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2225 struct notification_client_list_element
*client_list_element
;
2226 struct cds_lfht_node
*node
;
2227 struct cds_lfht_iter iter
;
2228 const char* trigger_name
;
2229 bool free_trigger
= true;
2230 struct lttng_evaluation
*evaluation
= NULL
;
2231 struct lttng_credentials object_creds
;
2234 enum action_executor_status executor_status
;
2238 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2239 LTTNG_TRIGGER_STATUS_UNSET
) {
2240 const enum lttng_error_code ret_code
= generate_trigger_name(
2241 state
, trigger
, &trigger_name
);
2243 if (ret_code
!= LTTNG_OK
) {
2246 *cmd_result
= ret_code
;
2249 } else if (trigger_name_taken(state
, trigger
)) {
2250 /* Not a fatal error. */
2251 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2256 condition
= lttng_trigger_get_condition(trigger
);
2259 ret
= condition_is_supported(condition
);
2262 } else if (ret
== 0) {
2263 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2266 /* Feature is supported, continue. */
2270 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2271 if (!trigger_ht_element
) {
2276 /* Add trigger to the trigger_ht. */
2277 cds_lfht_node_init(&trigger_ht_element
->node
);
2278 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2279 trigger_ht_element
->trigger
= trigger
;
2281 node
= cds_lfht_add_unique(state
->triggers_ht
,
2282 lttng_condition_hash(condition
),
2285 &trigger_ht_element
->node
);
2286 if (node
!= &trigger_ht_element
->node
) {
2287 /* Not a fatal error, simply report it to the client. */
2288 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2289 goto error_free_ht_element
;
2292 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2293 hash_trigger_by_name_uid(trigger
),
2294 match_trigger_by_name_uid
,
2296 &trigger_ht_element
->node_by_name_uid
);
2297 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2298 /* Not a fatal error, simply report it to the client. */
2299 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2300 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2301 goto error_free_ht_element
;
2305 * Ownership of the trigger and of its wrapper was transfered to
2308 trigger_ht_element
= NULL
;
2309 free_trigger
= false;
2312 * The rest only applies to triggers that have a "notify" action.
2313 * It is not skipped as this is the only action type currently
2316 if (is_trigger_action_notify(trigger
)) {
2317 client_list
= notification_client_list_create(trigger
);
2320 goto error_free_ht_element
;
2323 /* Build a list of clients to which this new trigger applies. */
2324 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2325 client_socket_ht_node
) {
2326 if (!trigger_applies_to_client(trigger
, client
)) {
2330 client_list_element
=
2331 zmalloc(sizeof(*client_list_element
));
2332 if (!client_list_element
) {
2334 goto error_put_client_list
;
2337 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2338 client_list_element
->client
= client
;
2339 cds_list_add(&client_list_element
->node
,
2340 &client_list
->list
);
2344 * Client list ownership transferred to the
2345 * notification_trigger_clients_ht.
2347 publish_notification_client_list(state
, client_list
);
2350 switch (get_condition_binding_object(condition
)) {
2351 case LTTNG_OBJECT_TYPE_SESSION
:
2352 /* Add the trigger to the list if it matches a known session. */
2353 ret
= bind_trigger_to_matching_session(trigger
, state
);
2355 goto error_put_client_list
;
2358 case LTTNG_OBJECT_TYPE_CHANNEL
:
2360 * Add the trigger to list of triggers bound to the channels
2363 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2365 goto error_put_client_list
;
2368 case LTTNG_OBJECT_TYPE_NONE
:
2371 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2373 goto error_put_client_list
;
2377 * The new trigger's condition must be evaluated against the current
2380 * In the case of `notify` action, nothing preventing clients from
2381 * subscribing to a condition before the corresponding trigger is
2382 * registered, we have to evaluate this new condition right away.
2384 * At some point, we were waiting for the next "evaluation" (e.g. on
2385 * reception of a channel sample) to evaluate this new condition, but
2388 * The reason it was broken is that waiting for the next sample
2389 * does not allow us to properly handle transitions for edge-triggered
2392 * Consider this example: when we handle a new channel sample, we
2393 * evaluate each conditions twice: once with the previous state, and
2394 * again with the newest state. We then use those two results to
2395 * determine whether a state change happened: a condition was false and
2396 * became true. If a state change happened, we have to notify clients.
2398 * Now, if a client subscribes to a given notification and registers
2399 * a trigger *after* that subscription, we have to make sure the
2400 * condition is evaluated at this point while considering only the
2401 * current state. Otherwise, the next evaluation cycle may only see
2402 * that the evaluations remain the same (true for samples n-1 and n) and
2403 * the client will never know that the condition has been met.
2405 switch (get_condition_binding_object(condition
)) {
2406 case LTTNG_OBJECT_TYPE_SESSION
:
2407 ret
= evaluate_session_condition_for_client(condition
, state
,
2408 &evaluation
, &object_uid
,
2411 case LTTNG_OBJECT_TYPE_CHANNEL
:
2412 ret
= evaluate_channel_condition_for_client(condition
, state
,
2413 &evaluation
, &object_uid
,
2416 case LTTNG_OBJECT_TYPE_NONE
:
2419 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2427 goto error_put_client_list
;
2430 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2431 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2433 DBG("Newly registered trigger's condition evaluated to %s",
2434 evaluation
? "true" : "false");
2436 /* Evaluation yielded nothing. Normal exit. */
2442 * Ownership of `evaluation` transferred to the action executor
2443 * no matter the result.
2445 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2446 evaluation
, &object_creds
, client_list
);
2448 switch (executor_status
) {
2449 case ACTION_EXECUTOR_STATUS_OK
:
2451 case ACTION_EXECUTOR_STATUS_ERROR
:
2452 case ACTION_EXECUTOR_STATUS_INVALID
:
2454 * TODO Add trigger identification (name/id) when
2455 * it is added to the API.
2457 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2459 goto error_put_client_list
;
2460 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2462 * TODO Add trigger identification (name/id) when
2463 * it is added to the API.
2465 * Not a fatal error.
2467 WARN("No space left when enqueuing action associated to newly registered trigger");
2475 *cmd_result
= LTTNG_OK
;
2477 error_put_client_list
:
2478 notification_client_list_put(client_list
);
2480 error_free_ht_element
:
2481 if (trigger_ht_element
) {
2482 /* Delayed removal due to RCU constraint on delete. */
2483 call_rcu(&trigger_ht_element
->rcu_node
,
2484 free_lttng_trigger_ht_element_rcu
);
2489 lttng_trigger_destroy(trigger
);
2496 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2498 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2503 int handle_notification_thread_command_unregister_trigger(
2504 struct notification_thread_state
*state
,
2505 struct lttng_trigger
*trigger
,
2506 enum lttng_error_code
*_cmd_reply
)
2508 struct cds_lfht_iter iter
;
2509 struct cds_lfht_node
*triggers_ht_node
;
2510 struct lttng_channel_trigger_list
*trigger_list
;
2511 struct notification_client_list
*client_list
;
2512 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2513 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2515 enum lttng_error_code cmd_reply
;
2519 cds_lfht_lookup(state
->triggers_ht
,
2520 lttng_condition_hash(condition
),
2524 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2525 if (!triggers_ht_node
) {
2526 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2529 cmd_reply
= LTTNG_OK
;
2532 /* Remove trigger from channel_triggers_ht. */
2533 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2534 channel_triggers_ht_node
) {
2535 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2537 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2538 &trigger_list
->list
, node
) {
2539 const struct lttng_condition
*current_condition
=
2540 lttng_trigger_get_const_condition(
2541 trigger_element
->trigger
);
2543 assert(current_condition
);
2544 if (!lttng_condition_is_equal(condition
,
2545 current_condition
)) {
2549 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2550 cds_list_del(&trigger_element
->node
);
2551 /* A trigger can only appear once per channel */
2557 * Remove and release the client list from
2558 * notification_trigger_clients_ht.
2560 client_list
= get_client_list_from_condition(state
, condition
);
2561 assert(client_list
);
2563 /* Put new reference and the hashtable's reference. */
2564 notification_client_list_put(client_list
);
2565 notification_client_list_put(client_list
);
2568 /* Remove trigger from triggers_ht. */
2569 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2570 struct lttng_trigger_ht_element
, node
);
2571 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2572 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2574 /* Release the ownership of the trigger. */
2575 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2576 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2580 *_cmd_reply
= cmd_reply
;
2585 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2586 int handle_notification_thread_command(
2587 struct notification_thread_handle
*handle
,
2588 struct notification_thread_state
*state
)
2592 struct notification_thread_command
*cmd
;
2594 /* Read the event pipe to put it back into a quiescent state. */
2595 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2597 if (ret
!= sizeof(counter
)) {
2601 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2602 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2603 struct notification_thread_command
, cmd_list_node
);
2604 cds_list_del(&cmd
->cmd_list_node
);
2605 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2606 switch (cmd
->type
) {
2607 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2608 DBG("[notification-thread] Received register trigger command");
2609 ret
= handle_notification_thread_command_register_trigger(
2610 state
, cmd
->parameters
.trigger
,
2613 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2614 DBG("[notification-thread] Received unregister trigger command");
2615 ret
= handle_notification_thread_command_unregister_trigger(
2616 state
, cmd
->parameters
.trigger
,
2619 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2620 DBG("[notification-thread] Received add channel command");
2621 ret
= handle_notification_thread_command_add_channel(
2623 cmd
->parameters
.add_channel
.session
.name
,
2624 cmd
->parameters
.add_channel
.session
.uid
,
2625 cmd
->parameters
.add_channel
.session
.gid
,
2626 cmd
->parameters
.add_channel
.channel
.name
,
2627 cmd
->parameters
.add_channel
.channel
.domain
,
2628 cmd
->parameters
.add_channel
.channel
.key
,
2629 cmd
->parameters
.add_channel
.channel
.capacity
,
2632 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2633 DBG("[notification-thread] Received remove channel command");
2634 ret
= handle_notification_thread_command_remove_channel(
2635 state
, cmd
->parameters
.remove_channel
.key
,
2636 cmd
->parameters
.remove_channel
.domain
,
2639 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2640 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2641 DBG("[notification-thread] Received session rotation %s command",
2642 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2643 "ongoing" : "completed");
2644 ret
= handle_notification_thread_command_session_rotation(
2647 cmd
->parameters
.session_rotation
.session_name
,
2648 cmd
->parameters
.session_rotation
.uid
,
2649 cmd
->parameters
.session_rotation
.gid
,
2650 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2651 cmd
->parameters
.session_rotation
.location
,
2654 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2655 DBG("[notification-thread] Received quit command");
2656 cmd
->reply_code
= LTTNG_OK
;
2659 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2661 const enum client_transmission_status client_status
=
2662 cmd
->parameters
.client_communication_update
2664 const notification_client_id client_id
=
2665 cmd
->parameters
.client_communication_update
.id
;
2666 struct notification_client
*client
;
2669 client
= get_client_from_id(client_id
, state
);
2673 * Client error was probably already picked-up by the
2674 * notification thread or it has disconnected
2675 * gracefully while this command was queued.
2677 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2681 ret
= client_handle_transmission_status(
2682 client
, client_status
, state
);
2688 ERR("[notification-thread] Unknown internal command received");
2696 if (cmd
->is_async
) {
2700 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2704 /* Wake-up and return a fatal error to the calling thread. */
2705 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2706 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2708 /* Indicate a fatal error to the caller. */
2713 int socket_set_non_blocking(int socket
)
2717 /* Set the pipe as non-blocking. */
2718 ret
= fcntl(socket
, F_GETFL
, 0);
2720 PERROR("fcntl get socket flags");
2725 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2727 PERROR("fcntl set O_NONBLOCK socket flag");
2730 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2736 int client_reset_inbound_state(struct notification_client
*client
)
2741 lttng_payload_clear(&client
->communication
.inbound
.payload
);
2743 client
->communication
.inbound
.bytes_to_receive
=
2744 sizeof(struct lttng_notification_channel_message
);
2745 client
->communication
.inbound
.msg_type
=
2746 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2747 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2748 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2749 ret
= lttng_dynamic_buffer_set_size(
2750 &client
->communication
.inbound
.payload
.buffer
,
2751 client
->communication
.inbound
.bytes_to_receive
);
2756 int handle_notification_thread_client_connect(
2757 struct notification_thread_state
*state
)
2760 struct notification_client
*client
;
2762 DBG("[notification-thread] Handling new notification channel client connection");
2764 client
= zmalloc(sizeof(*client
));
2771 pthread_mutex_init(&client
->lock
, NULL
);
2772 client
->id
= state
->next_notification_client_id
++;
2773 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2774 lttng_payload_init(&client
->communication
.inbound
.payload
);
2775 lttng_payload_init(&client
->communication
.outbound
.payload
);
2776 client
->communication
.inbound
.expect_creds
= true;
2778 ret
= client_reset_inbound_state(client
);
2780 ERR("[notification-thread] Failed to reset client communication's inbound state");
2785 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2787 ERR("[notification-thread] Failed to accept new notification channel client connection");
2792 client
->socket
= ret
;
2794 ret
= socket_set_non_blocking(client
->socket
);
2796 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2800 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2802 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2807 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2808 LPOLLIN
| LPOLLERR
|
2809 LPOLLHUP
| LPOLLRDHUP
);
2811 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2815 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2819 cds_lfht_add(state
->client_socket_ht
,
2820 hash_client_socket(client
->socket
),
2821 &client
->client_socket_ht_node
);
2822 cds_lfht_add(state
->client_id_ht
,
2823 hash_client_id(client
->id
),
2824 &client
->client_id_ht_node
);
2830 notification_client_destroy(client
, state
);
2835 * RCU read-lock must be held by the caller.
2836 * Client lock must _not_ be held by the caller.
2839 int notification_thread_client_disconnect(
2840 struct notification_client
*client
,
2841 struct notification_thread_state
*state
)
2844 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
2846 /* Acquire the client lock to disable its communication atomically. */
2847 pthread_mutex_lock(&client
->lock
);
2848 client
->communication
.active
= false;
2849 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
2850 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
2851 pthread_mutex_unlock(&client
->lock
);
2853 ret
= lttng_poll_del(&state
->events
, client
->socket
);
2855 ERR("[notification-thread] Failed to remove client socket %d from poll set",
2859 /* Release all conditions to which the client was subscribed. */
2860 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
2861 &client
->condition_list
, node
) {
2862 (void) notification_thread_client_unsubscribe(client
,
2863 condition_list_element
->condition
, state
, NULL
);
2867 * Client no longer accessible to other threads (through the
2870 notification_client_destroy(client
, state
);
2874 int handle_notification_thread_client_disconnect(
2875 int client_socket
, struct notification_thread_state
*state
)
2878 struct notification_client
*client
;
2881 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2883 client
= get_client_from_socket(client_socket
, state
);
2885 /* Internal state corruption, fatal error. */
2886 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2892 ret
= notification_thread_client_disconnect(client
, state
);
2898 int handle_notification_thread_client_disconnect_all(
2899 struct notification_thread_state
*state
)
2901 struct cds_lfht_iter iter
;
2902 struct notification_client
*client
;
2903 bool error_encoutered
= false;
2906 DBG("[notification-thread] Closing all client connections");
2907 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2908 client_socket_ht_node
) {
2911 ret
= notification_thread_client_disconnect(
2914 error_encoutered
= true;
2918 return error_encoutered
? 1 : 0;
2921 int handle_notification_thread_trigger_unregister_all(
2922 struct notification_thread_state
*state
)
2924 bool error_occurred
= false;
2925 struct cds_lfht_iter iter
;
2926 struct lttng_trigger_ht_element
*trigger_ht_element
;
2929 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2931 int ret
= handle_notification_thread_command_unregister_trigger(
2932 state
, trigger_ht_element
->trigger
, NULL
);
2934 error_occurred
= true;
2938 return error_occurred
? -1 : 0;
2942 int client_handle_transmission_status(
2943 struct notification_client
*client
,
2944 enum client_transmission_status transmission_status
,
2945 struct notification_thread_state
*state
)
2949 switch (transmission_status
) {
2950 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
2951 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2952 CLIENT_POLL_MASK_IN
);
2958 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
2960 * We want to be notified whenever there is buffer space
2961 * available to send the rest of the payload.
2963 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2964 CLIENT_POLL_MASK_IN_OUT
);
2969 case CLIENT_TRANSMISSION_STATUS_FAIL
:
2970 ret
= notification_thread_client_disconnect(client
, state
);
2975 case CLIENT_TRANSMISSION_STATUS_ERROR
:
2985 /* Client lock must be acquired by caller. */
2987 enum client_transmission_status
client_flush_outgoing_queue(
2988 struct notification_client
*client
)
2991 size_t to_send_count
;
2992 enum client_transmission_status status
;
2993 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
2994 &client
->communication
.outbound
.payload
, 0, -1);
2995 const int fds_to_send_count
=
2996 lttng_payload_view_get_fd_handle_count(&pv
);
2998 ASSERT_LOCKED(client
->lock
);
3000 if (!client
->communication
.active
) {
3001 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3005 if (pv
.buffer
.size
== 0) {
3007 * If both data and fds are equal to zero, we are in an invalid
3010 assert(fds_to_send_count
!= 0);
3015 to_send_count
= pv
.buffer
.size
;
3016 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3019 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3022 if ((ret
>= 0 && ret
< to_send_count
)) {
3023 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3025 to_send_count
-= max(ret
, 0);
3027 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3029 pv
.buffer
.size
- to_send_count
,
3031 ret
= lttng_dynamic_buffer_set_size(
3032 &client
->communication
.outbound
.payload
.buffer
,
3038 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3040 } else if (ret
< 0) {
3041 /* Generic error, disable the client's communication. */
3042 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3044 client
->communication
.active
= false;
3045 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3049 * No error and flushed the queue completely.
3051 * The payload buffer size is used later to
3052 * check if there is notifications queued. So albeit that the
3053 * direct caller knows that the transmission is complete, we
3054 * need to set the buffer size to zero.
3056 ret
= lttng_dynamic_buffer_set_size(
3057 &client
->communication
.outbound
.payload
.buffer
, 0);
3064 /* No fds to send, transmission is complete. */
3065 if (fds_to_send_count
== 0) {
3066 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3070 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3071 client
->socket
, &pv
);
3073 /* Generic error, disable the client's communication. */
3074 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3076 client
->communication
.active
= false;
3077 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3079 } else if (ret
== 0) {
3080 /* Nothing could be sent. */
3081 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3083 /* Fd passing is an all or nothing kind of thing. */
3084 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3086 * The payload _fd_array count is used later to
3087 * check if there is notifications queued. So although the
3088 * direct caller knows that the transmission is complete, we
3089 * need to clear the _fd_array for the queuing check.
3091 lttng_dynamic_pointer_array_clear(
3092 &client
->communication
.outbound
.payload
3097 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3098 client
->communication
.outbound
.queued_command_reply
= false;
3099 client
->communication
.outbound
.dropped_notification
= false;
3100 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3105 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3109 bool client_has_outbound_data_left(
3110 const struct notification_client
*client
)
3112 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3113 &client
->communication
.outbound
.payload
, 0, -1);
3114 const bool has_data
= pv
.buffer
.size
!= 0;
3115 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3117 return has_data
|| has_fds
;
3120 /* Client lock must _not_ be held by the caller. */
3122 int client_send_command_reply(struct notification_client
*client
,
3123 struct notification_thread_state
*state
,
3124 enum lttng_notification_channel_status status
)
3127 struct lttng_notification_channel_command_reply reply
= {
3128 .status
= (int8_t) status
,
3130 struct lttng_notification_channel_message msg
= {
3131 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3132 .size
= sizeof(reply
),
3134 char buffer
[sizeof(msg
) + sizeof(reply
)];
3135 enum client_transmission_status transmission_status
;
3137 memcpy(buffer
, &msg
, sizeof(msg
));
3138 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3139 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3141 pthread_mutex_lock(&client
->lock
);
3142 if (client
->communication
.outbound
.queued_command_reply
) {
3143 /* Protocol error. */
3147 /* Enqueue buffer to outgoing queue and flush it. */
3148 ret
= lttng_dynamic_buffer_append(
3149 &client
->communication
.outbound
.payload
.buffer
,
3150 buffer
, sizeof(buffer
));
3155 transmission_status
= client_flush_outgoing_queue(client
);
3157 if (client_has_outbound_data_left(client
)) {
3158 /* Queue could not be emptied. */
3159 client
->communication
.outbound
.queued_command_reply
= true;
3162 pthread_mutex_unlock(&client
->lock
);
3163 ret
= client_handle_transmission_status(
3164 client
, transmission_status
, state
);
3171 pthread_mutex_unlock(&client
->lock
);
3177 int client_handle_message_unknown(struct notification_client
*client
,
3178 struct notification_thread_state
*state
)
3182 * Receiving message header. The function will be called again
3183 * once the rest of the message as been received and can be
3186 const struct lttng_notification_channel_message
*msg
;
3188 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3189 msg
= (const struct lttng_notification_channel_message
*)
3190 client
->communication
.inbound
.payload
.buffer
.data
;
3192 if (msg
->size
== 0 ||
3193 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3194 ERR("[notification-thread] Invalid notification channel message: length = %u",
3200 switch (msg
->type
) {
3201 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3202 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3203 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3207 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3211 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3212 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3213 client
->communication
.inbound
.msg_type
=
3214 (enum lttng_notification_channel_message_type
) msg
->type
;
3215 ret
= lttng_dynamic_buffer_set_size(
3216 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3218 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3225 int client_handle_message_handshake(struct notification_client
*client
,
3226 struct notification_thread_state
*state
)
3229 struct lttng_notification_channel_command_handshake
*handshake_client
;
3230 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3231 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3232 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3234 const struct lttng_notification_channel_message msg_header
= {
3235 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3236 .size
= sizeof(handshake_reply
),
3238 enum lttng_notification_channel_status status
=
3239 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3240 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3242 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3243 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3244 sizeof(handshake_reply
));
3247 (struct lttng_notification_channel_command_handshake
*)
3248 client
->communication
.inbound
.payload
.buffer
3250 client
->major
= handshake_client
->major
;
3251 client
->minor
= handshake_client
->minor
;
3252 if (!client
->communication
.inbound
.creds_received
) {
3253 ERR("[notification-thread] No credentials received from client");
3258 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3259 &client
->communication
.inbound
.creds
);
3260 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3261 &client
->communication
.inbound
.creds
);
3262 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3263 client
->uid
, client
->gid
, (int) client
->major
,
3264 (int) client
->minor
);
3266 if (handshake_client
->major
!=
3267 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3268 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3271 pthread_mutex_lock(&client
->lock
);
3272 /* Outgoing queue will be flushed when the command reply is sent. */
3273 ret
= lttng_dynamic_buffer_append(
3274 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3275 sizeof(send_buffer
));
3277 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3281 client
->validated
= true;
3282 client
->communication
.active
= true;
3283 pthread_mutex_unlock(&client
->lock
);
3285 /* Set reception state to receive the next message header. */
3286 ret
= client_reset_inbound_state(client
);
3288 ERR("[notification-thread] Failed to reset client communication's inbound state");
3292 /* Flushes the outgoing queue. */
3293 ret
= client_send_command_reply(client
, state
, status
);
3295 ERR("[notification-thread] Failed to send reply to notification channel client");
3301 pthread_mutex_unlock(&client
->lock
);
3307 int client_handle_message_subscription(
3308 struct notification_client
*client
,
3309 enum lttng_notification_channel_message_type msg_type
,
3310 struct notification_thread_state
*state
)
3313 struct lttng_condition
*condition
;
3314 enum lttng_notification_channel_status status
=
3315 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3316 struct lttng_payload_view condition_view
=
3317 lttng_payload_view_from_payload(
3318 &client
->communication
.inbound
.payload
,
3320 size_t expected_condition_size
;
3323 * No need to lock client to sample the inbound state as the only
3324 * other thread accessing clients (action executor) only uses the
3327 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3328 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3329 if (ret
!= expected_condition_size
) {
3330 ERR("[notification-thread] Malformed condition received from client");
3334 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3335 ret
= notification_thread_client_subscribe(
3336 client
, condition
, state
, &status
);
3338 ret
= notification_thread_client_unsubscribe(
3339 client
, condition
, state
, &status
);
3346 /* Set reception state to receive the next message header. */
3347 ret
= client_reset_inbound_state(client
);
3349 ERR("[notification-thread] Failed to reset client communication's inbound state");
3353 ret
= client_send_command_reply(client
, state
, status
);
3355 ERR("[notification-thread] Failed to send reply to notification channel client");
3364 int client_dispatch_message(struct notification_client
*client
,
3365 struct notification_thread_state
*state
)
3369 if (client
->communication
.inbound
.msg_type
!=
3370 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3371 client
->communication
.inbound
.msg_type
!=
3372 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3373 !client
->validated
) {
3374 WARN("[notification-thread] client attempted a command before handshake");
3379 switch (client
->communication
.inbound
.msg_type
) {
3380 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3382 ret
= client_handle_message_unknown(client
, state
);
3385 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3387 ret
= client_handle_message_handshake(client
, state
);
3390 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3391 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3393 ret
= client_handle_message_subscription(client
,
3394 client
->communication
.inbound
.msg_type
, state
);
3404 /* Incoming data from client. */
3405 int handle_notification_thread_client_in(
3406 struct notification_thread_state
*state
, int socket
)
3409 struct notification_client
*client
;
3414 client
= get_client_from_socket(socket
, state
);
3416 /* Internal error, abort. */
3421 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3422 client
->communication
.inbound
.bytes_to_receive
;
3423 if (client
->communication
.inbound
.expect_creds
) {
3424 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3425 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3426 client
->communication
.inbound
.bytes_to_receive
,
3427 &client
->communication
.inbound
.creds
);
3429 client
->communication
.inbound
.expect_creds
= false;
3430 client
->communication
.inbound
.creds_received
= true;
3433 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3434 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3435 client
->communication
.inbound
.bytes_to_receive
);
3437 if (recv_ret
>= 0) {
3438 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3440 goto error_disconnect_client
;
3443 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3444 /* Message incomplete wait for more data. */
3449 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3452 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3453 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3455 client
->communication
.inbound
.fds_to_receive
,
3456 &client
->communication
.inbound
.payload
);
3459 * Fds received. non blocking fds passing is all
3462 ssize_t expected_size
;
3464 expected_size
= sizeof(int) *
3465 client
->communication
.inbound
3467 assert(ret
== expected_size
);
3468 client
->communication
.inbound
.fds_to_receive
= 0;
3469 } else if (ret
== 0) {
3470 /* Received nothing. */
3474 goto error_disconnect_client
;
3478 /* At this point the message is complete.*/
3479 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3480 client
->communication
.inbound
.fds_to_receive
== 0);
3481 ret
= client_dispatch_message(client
, state
);
3484 * Only returns an error if this client must be
3487 goto error_disconnect_client
;
3494 error_disconnect_client
:
3495 ret
= notification_thread_client_disconnect(client
, state
);
3499 /* Client ready to receive outgoing data. */
3500 int handle_notification_thread_client_out(
3501 struct notification_thread_state
*state
, int socket
)
3504 struct notification_client
*client
;
3505 enum client_transmission_status transmission_status
;
3508 client
= get_client_from_socket(socket
, state
);
3510 /* Internal error, abort. */
3515 pthread_mutex_lock(&client
->lock
);
3516 transmission_status
= client_flush_outgoing_queue(client
);
3517 pthread_mutex_unlock(&client
->lock
);
3519 ret
= client_handle_transmission_status(
3520 client
, transmission_status
, state
);
3530 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3531 const struct channel_state_sample
*sample
,
3532 uint64_t buffer_capacity
)
3534 bool result
= false;
3536 enum lttng_condition_type condition_type
;
3537 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3538 condition
, struct lttng_condition_buffer_usage
,
3541 if (use_condition
->threshold_bytes
.set
) {
3542 threshold
= use_condition
->threshold_bytes
.value
;
3545 * Threshold was expressed as a ratio.
3547 * TODO the threshold (in bytes) of conditions expressed
3548 * as a ratio of total buffer size could be cached to
3549 * forego this double-multiplication or it could be performed
3550 * as fixed-point math.
3552 * Note that caching should accommodates the case where the
3553 * condition applies to multiple channels (i.e. don't assume
3554 * that all channels matching my_chann* have the same size...)
3556 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3557 (double) buffer_capacity
);
3560 condition_type
= lttng_condition_get_type(condition
);
3561 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3562 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3563 threshold
, sample
->highest_usage
);
3566 * The low condition should only be triggered once _all_ of the
3567 * streams in a channel have gone below the "low" threshold.
3569 if (sample
->highest_usage
<= threshold
) {
3573 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3574 threshold
, sample
->highest_usage
);
3577 * For high buffer usage scenarios, we want to trigger whenever
3578 * _any_ of the streams has reached the "high" threshold.
3580 if (sample
->highest_usage
>= threshold
) {
3589 bool evaluate_session_consumed_size_condition(
3590 const struct lttng_condition
*condition
,
3591 uint64_t session_consumed_size
)
3594 const struct lttng_condition_session_consumed_size
*size_condition
=
3595 container_of(condition
,
3596 struct lttng_condition_session_consumed_size
,
3599 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3600 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3601 threshold
, session_consumed_size
);
3602 return session_consumed_size
>= threshold
;
3606 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3607 struct lttng_evaluation
**evaluation
,
3608 const struct notification_thread_state
*state
,
3609 const struct channel_state_sample
*previous_sample
,
3610 const struct channel_state_sample
*latest_sample
,
3611 uint64_t previous_session_consumed_total
,
3612 uint64_t latest_session_consumed_total
,
3613 struct channel_info
*channel_info
)
3616 enum lttng_condition_type condition_type
;
3617 const bool previous_sample_available
= !!previous_sample
;
3618 bool previous_sample_result
= false;
3619 bool latest_sample_result
;
3621 condition_type
= lttng_condition_get_type(condition
);
3623 switch (condition_type
) {
3624 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3625 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3626 if (caa_likely(previous_sample_available
)) {
3627 previous_sample_result
=
3628 evaluate_buffer_usage_condition(condition
,
3629 previous_sample
, channel_info
->capacity
);
3631 latest_sample_result
= evaluate_buffer_usage_condition(
3632 condition
, latest_sample
,
3633 channel_info
->capacity
);
3635 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3636 if (caa_likely(previous_sample_available
)) {
3637 previous_sample_result
=
3638 evaluate_session_consumed_size_condition(
3640 previous_session_consumed_total
);
3642 latest_sample_result
=
3643 evaluate_session_consumed_size_condition(
3645 latest_session_consumed_total
);
3648 /* Unknown condition type; internal error. */
3652 if (!latest_sample_result
||
3653 (previous_sample_result
== latest_sample_result
)) {
3655 * Only trigger on a condition evaluation transition.
3657 * NOTE: This edge-triggered logic may not be appropriate for
3658 * future condition types.
3663 if (!evaluation
|| !latest_sample_result
) {
3667 switch (condition_type
) {
3668 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3669 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3670 *evaluation
= lttng_evaluation_buffer_usage_create(
3672 latest_sample
->highest_usage
,
3673 channel_info
->capacity
);
3675 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3676 *evaluation
= lttng_evaluation_session_consumed_size_create(
3677 latest_session_consumed_total
);
3692 int client_notification_overflow(struct notification_client
*client
)
3695 const struct lttng_notification_channel_message msg
= {
3696 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3699 ASSERT_LOCKED(client
->lock
);
3701 DBG("Dropping notification addressed to client (socket fd = %i)",
3703 if (client
->communication
.outbound
.dropped_notification
) {
3705 * The client already has a "notification dropped" message
3706 * in its outgoing queue. Nothing to do since all
3707 * of those messages are coalesced.
3712 client
->communication
.outbound
.dropped_notification
= true;
3713 ret
= lttng_dynamic_buffer_append(
3714 &client
->communication
.outbound
.payload
.buffer
, &msg
,
3717 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3724 static int client_handle_transmission_status_wrapper(
3725 struct notification_client
*client
,
3726 enum client_transmission_status status
,
3729 return client_handle_transmission_status(client
, status
,
3730 (struct notification_thread_state
*) user_data
);
3734 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3735 const struct lttng_evaluation
*evaluation
,
3736 struct notification_client_list
* client_list
,
3737 struct notification_thread_state
*state
,
3738 uid_t object_uid
, gid_t object_gid
)
3740 const struct lttng_credentials creds
= {
3741 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
3742 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
3745 return notification_client_list_send_evaluation(client_list
,
3746 lttng_trigger_get_const_condition(trigger
), evaluation
,
3747 lttng_trigger_get_credentials(trigger
),
3749 client_handle_transmission_status_wrapper
, state
);
3753 * Permission checks relative to notification channel clients are performed
3754 * here. Notice how object, client, and trigger credentials are involved in
3757 * The `object` credentials are the credentials associated with the "subject"
3758 * of a condition. For instance, a `rotation completed` condition applies
3759 * to a session. When that condition is met, it will produce an evaluation
3760 * against a session. Hence, in this case, the `object` credentials are the
3761 * credentials of the "subject" session.
3763 * The `trigger` credentials are the credentials of the user that registered the
3766 * The `client` credentials are the credentials of the user that created a given
3767 * notification channel.
3769 * In terms of visibility, it is expected that non-privilieged users can only
3770 * register triggers against "their" objects (their own sessions and
3771 * applications they are allowed to interact with). They can then open a
3772 * notification channel and subscribe to notifications associated with those
3775 * As for privilieged users, they can register triggers against the objects of
3776 * other users. They can then subscribe to the notifications associated to their
3777 * triggers. Privilieged users _can't_ subscribe to the notifications of
3778 * triggers owned by other users; they must create their own triggers.
3780 * This is more a concern of usability than security. It would be difficult for
3781 * a root user reliably subscribe to a specific set of conditions without
3782 * interference from external users (those could, for instance, unregister
3786 int notification_client_list_send_evaluation(
3787 struct notification_client_list
*client_list
,
3788 const struct lttng_condition
*condition
,
3789 const struct lttng_evaluation
*evaluation
,
3790 const struct lttng_credentials
*trigger_creds
,
3791 const struct lttng_credentials
*source_object_creds
,
3792 report_client_transmission_result_cb client_report
,
3796 struct lttng_payload msg_payload
;
3797 struct notification_client_list_element
*client_list_element
, *tmp
;
3798 const struct lttng_notification notification
= {
3799 .condition
= (struct lttng_condition
*) condition
,
3800 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3802 struct lttng_notification_channel_message msg_header
= {
3803 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3806 lttng_payload_init(&msg_payload
);
3808 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
3809 sizeof(msg_header
));
3814 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
3816 ERR("[notification-thread] Failed to serialize notification");
3821 /* Update payload size. */
3822 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
3823 ->size
= (uint32_t)(
3824 msg_payload
.buffer
.size
- sizeof(msg_header
));
3826 /* Update the payload number of fds. */
3828 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3829 &msg_payload
, 0, -1);
3831 ((struct lttng_notification_channel_message
*)
3832 msg_payload
.buffer
.data
)->fds
= (uint32_t)
3833 lttng_payload_view_get_fd_handle_count(&pv
);
3836 pthread_mutex_lock(&client_list
->lock
);
3837 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3838 &client_list
->list
, node
) {
3839 enum client_transmission_status transmission_status
;
3840 struct notification_client
*client
=
3841 client_list_element
->client
;
3844 pthread_mutex_lock(&client
->lock
);
3845 if (!client
->communication
.active
) {
3847 * Skip inactive client (protocol error or
3850 DBG("Skipping client at it is marked as inactive");
3854 if (source_object_creds
) {
3855 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
3856 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
3859 * Client is not allowed to monitor this
3862 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
3867 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
3868 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
3872 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3873 client
->socket
, msg_payload
.buffer
.size
);
3875 if (client_has_outbound_data_left(client
)) {
3877 * Outgoing data is already buffered for this client;
3878 * drop the notification and enqueue a "dropped
3879 * notification" message if this is the first dropped
3880 * notification since the socket spilled-over to the
3883 ret
= client_notification_overflow(client
);
3890 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
3896 transmission_status
= client_flush_outgoing_queue(client
);
3897 pthread_mutex_unlock(&client
->lock
);
3898 ret
= client_report(client
, transmission_status
, user_data
);
3901 goto end_unlock_list
;
3907 pthread_mutex_unlock(&client
->lock
);
3910 goto end_unlock_list
;
3916 pthread_mutex_unlock(&client_list
->lock
);
3918 lttng_payload_reset(&msg_payload
);
3922 int handle_notification_thread_channel_sample(
3923 struct notification_thread_state
*state
, int pipe
,
3924 enum lttng_domain_type domain
)
3927 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
3928 struct channel_info
*channel_info
;
3929 struct cds_lfht_node
*node
;
3930 struct cds_lfht_iter iter
;
3931 struct lttng_channel_trigger_list
*trigger_list
;
3932 struct lttng_trigger_list_element
*trigger_list_element
;
3933 bool previous_sample_available
= false;
3934 struct channel_state_sample previous_sample
, latest_sample
;
3935 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
3936 struct lttng_credentials channel_creds
;
3939 * The monitoring pipe only holds messages smaller than PIPE_BUF,
3940 * ensuring that read/write of sampling messages are atomic.
3942 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
3943 if (ret
!= sizeof(sample_msg
)) {
3944 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
3951 latest_sample
.key
.key
= sample_msg
.key
;
3952 latest_sample
.key
.domain
= domain
;
3953 latest_sample
.highest_usage
= sample_msg
.highest
;
3954 latest_sample
.lowest_usage
= sample_msg
.lowest
;
3955 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
3959 /* Retrieve the channel's informations */
3960 cds_lfht_lookup(state
->channels_ht
,
3961 hash_channel_key(&latest_sample
.key
),
3965 node
= cds_lfht_iter_get_node(&iter
);
3966 if (caa_unlikely(!node
)) {
3968 * Not an error since the consumer can push a sample to the pipe
3969 * and the rest of the session daemon could notify us of the
3970 * channel's destruction before we get a chance to process that
3973 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
3974 latest_sample
.key
.key
,
3975 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
3979 channel_info
= caa_container_of(node
, struct channel_info
,
3981 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
3983 latest_sample
.key
.key
,
3984 channel_info
->session_info
->name
,
3985 latest_sample
.highest_usage
,
3986 latest_sample
.lowest_usage
,
3987 latest_sample
.channel_total_consumed
);
3989 previous_session_consumed_total
=
3990 channel_info
->session_info
->consumed_data_size
;
3992 /* Retrieve the channel's last sample, if it exists, and update it. */
3993 cds_lfht_lookup(state
->channel_state_ht
,
3994 hash_channel_key(&latest_sample
.key
),
3995 match_channel_state_sample
,
3998 node
= cds_lfht_iter_get_node(&iter
);
3999 if (caa_likely(node
)) {
4000 struct channel_state_sample
*stored_sample
;
4002 /* Update the sample stored. */
4003 stored_sample
= caa_container_of(node
,
4004 struct channel_state_sample
,
4005 channel_state_ht_node
);
4007 memcpy(&previous_sample
, stored_sample
,
4008 sizeof(previous_sample
));
4009 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4010 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4011 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4012 previous_sample_available
= true;
4014 latest_session_consumed_total
=
4015 previous_session_consumed_total
+
4016 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4019 * This is the channel's first sample, allocate space for and
4020 * store the new sample.
4022 struct channel_state_sample
*stored_sample
;
4024 stored_sample
= zmalloc(sizeof(*stored_sample
));
4025 if (!stored_sample
) {
4030 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4031 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4032 cds_lfht_add(state
->channel_state_ht
,
4033 hash_channel_key(&stored_sample
->key
),
4034 &stored_sample
->channel_state_ht_node
);
4036 latest_session_consumed_total
=
4037 previous_session_consumed_total
+
4038 latest_sample
.channel_total_consumed
;
4041 channel_info
->session_info
->consumed_data_size
=
4042 latest_session_consumed_total
;
4044 /* Find triggers associated with this channel. */
4045 cds_lfht_lookup(state
->channel_triggers_ht
,
4046 hash_channel_key(&latest_sample
.key
),
4047 match_channel_trigger_list
,
4050 node
= cds_lfht_iter_get_node(&iter
);
4051 if (caa_likely(!node
)) {
4055 channel_creds
= (typeof(channel_creds
)) {
4056 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4057 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4060 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4061 channel_triggers_ht_node
);
4062 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4064 const struct lttng_condition
*condition
;
4065 struct lttng_trigger
*trigger
;
4066 struct notification_client_list
*client_list
= NULL
;
4067 struct lttng_evaluation
*evaluation
= NULL
;
4068 enum action_executor_status executor_status
;
4071 trigger
= trigger_list_element
->trigger
;
4072 condition
= lttng_trigger_get_const_condition(trigger
);
4076 * Check if any client is subscribed to the result of this
4079 client_list
= get_client_list_from_condition(state
, condition
);
4081 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4082 previous_sample_available
? &previous_sample
: NULL
,
4084 previous_session_consumed_total
,
4085 latest_session_consumed_total
,
4087 if (caa_unlikely(ret
)) {
4091 if (caa_likely(!evaluation
)) {
4096 * Ownership of `evaluation` transferred to the action executor
4097 * no matter the result.
4099 executor_status
= action_executor_enqueue(state
->executor
,
4100 trigger
, evaluation
, &channel_creds
,
4103 switch (executor_status
) {
4104 case ACTION_EXECUTOR_STATUS_OK
:
4106 case ACTION_EXECUTOR_STATUS_ERROR
:
4107 case ACTION_EXECUTOR_STATUS_INVALID
:
4109 * TODO Add trigger identification (name/id) when
4110 * it is added to the API.
4112 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4115 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4117 * TODO Add trigger identification (name/id) when
4118 * it is added to the API.
4120 * Not a fatal error.
4122 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4130 notification_client_list_put(client_list
);
4131 if (caa_unlikely(ret
)) {