2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.h"
12 #include <urcu/rculfhash.h>
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/futex.h>
17 #include <common/unix.h>
18 #include <common/dynamic-buffer.h>
19 #include <common/hashtable/utils.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/macros.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.h>
24 #include <lttng/notification/notification-internal.h>
25 #include <lttng/condition/condition-internal.h>
26 #include <lttng/condition/buffer-usage-internal.h>
27 #include <lttng/condition/session-consumed-size-internal.h>
28 #include <lttng/condition/session-rotation-internal.h>
29 #include <lttng/condition/event-rule-internal.h>
30 #include <lttng/notification/channel-internal.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <lttng/event-rule/event-rule-internal.h>
40 #include "condition-internal.h"
41 #include "notification-thread.h"
42 #include "notification-thread-events.h"
43 #include "notification-thread-commands.h"
44 #include "lttng-sessiond.h"
47 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
48 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
50 enum lttng_object_type
{
51 LTTNG_OBJECT_TYPE_UNKNOWN
,
52 LTTNG_OBJECT_TYPE_NONE
,
53 LTTNG_OBJECT_TYPE_CHANNEL
,
54 LTTNG_OBJECT_TYPE_SESSION
,
57 struct lttng_trigger_list_element
{
58 /* No ownership of the trigger object is assumed. */
59 struct lttng_trigger
*trigger
;
60 struct cds_list_head node
;
63 struct lttng_channel_trigger_list
{
64 struct channel_key channel_key
;
65 /* List of struct lttng_trigger_list_element. */
66 struct cds_list_head list
;
67 /* Node in the channel_triggers_ht */
68 struct cds_lfht_node channel_triggers_ht_node
;
69 /* call_rcu delayed reclaim. */
70 struct rcu_head rcu_node
;
74 * List of triggers applying to a given session.
77 * - lttng_session_trigger_list_create()
78 * - lttng_session_trigger_list_build()
79 * - lttng_session_trigger_list_destroy()
80 * - lttng_session_trigger_list_add()
82 struct lttng_session_trigger_list
{
84 * Not owned by this; points to the session_info structure's
87 const char *session_name
;
88 /* List of struct lttng_trigger_list_element. */
89 struct cds_list_head list
;
90 /* Node in the session_triggers_ht */
91 struct cds_lfht_node session_triggers_ht_node
;
93 * Weak reference to the notification system's session triggers
96 * The session trigger list structure structure is owned by
97 * the session's session_info.
99 * The session_info is kept alive the the channel_infos holding a
100 * reference to it (reference counting). When those channels are
101 * destroyed (at runtime or on teardown), the reference they hold
102 * to the session_info are released. On destruction of session_info,
103 * session_info_destroy() will remove the list of triggers applying
104 * to this session from the notification system's state.
106 * This implies that the session_triggers_ht must be destroyed
107 * after the channels.
109 struct cds_lfht
*session_triggers_ht
;
110 /* Used for delayed RCU reclaim. */
111 struct rcu_head rcu_node
;
114 struct lttng_trigger_ht_element
{
115 struct lttng_trigger
*trigger
;
116 struct cds_lfht_node node
;
117 struct cds_lfht_node node_by_name_uid
;
118 /* call_rcu delayed reclaim. */
119 struct rcu_head rcu_node
;
122 struct lttng_condition_list_element
{
123 struct lttng_condition
*condition
;
124 struct cds_list_head node
;
127 struct channel_state_sample
{
128 struct channel_key key
;
129 struct cds_lfht_node channel_state_ht_node
;
130 uint64_t highest_usage
;
131 uint64_t lowest_usage
;
132 uint64_t channel_total_consumed
;
133 /* call_rcu delayed reclaim. */
134 struct rcu_head rcu_node
;
137 static unsigned long hash_channel_key(struct channel_key
*key
);
138 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
139 struct lttng_evaluation
**evaluation
,
140 const struct notification_thread_state
*state
,
141 const struct channel_state_sample
*previous_sample
,
142 const struct channel_state_sample
*latest_sample
,
143 uint64_t previous_session_consumed_total
,
144 uint64_t latest_session_consumed_total
,
145 struct channel_info
*channel_info
);
147 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
148 const struct lttng_evaluation
*evaluation
,
149 struct notification_client_list
*client_list
,
150 struct notification_thread_state
*state
,
151 uid_t channel_uid
, gid_t channel_gid
);
154 /* session_info API */
156 void session_info_destroy(void *_data
);
158 void session_info_get(struct session_info
*session_info
);
160 void session_info_put(struct session_info
*session_info
);
162 struct session_info
*session_info_create(const char *name
,
163 uid_t uid
, gid_t gid
,
164 struct lttng_session_trigger_list
*trigger_list
,
165 struct cds_lfht
*sessions_ht
);
167 void session_info_add_channel(struct session_info
*session_info
,
168 struct channel_info
*channel_info
);
170 void session_info_remove_channel(struct session_info
*session_info
,
171 struct channel_info
*channel_info
);
173 /* lttng_session_trigger_list API */
175 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
176 const char *session_name
,
177 struct cds_lfht
*session_triggers_ht
);
179 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
180 const struct notification_thread_state
*state
,
181 const char *session_name
);
183 void lttng_session_trigger_list_destroy(
184 struct lttng_session_trigger_list
*list
);
186 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
187 struct lttng_trigger
*trigger
);
190 int client_handle_transmission_status(
191 struct notification_client
*client
,
192 enum client_transmission_status transmission_status
,
193 struct notification_thread_state
*state
);
196 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
199 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
201 /* This double-cast is intended to supress pointer-to-cast warning. */
202 const int socket
= (int) (intptr_t) key
;
203 const struct notification_client
*client
= caa_container_of(node
,
204 struct notification_client
, client_socket_ht_node
);
206 return client
->socket
== socket
;
210 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
212 /* This double-cast is intended to supress pointer-to-cast warning. */
213 const notification_client_id id
= *((notification_client_id
*) key
);
214 const struct notification_client
*client
= caa_container_of(
215 node
, struct notification_client
, client_id_ht_node
);
217 return client
->id
== id
;
221 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
223 struct channel_key
*channel_key
= (struct channel_key
*) key
;
224 struct lttng_channel_trigger_list
*trigger_list
;
226 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
227 channel_triggers_ht_node
);
229 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
230 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
234 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
236 const char *session_name
= (const char *) key
;
237 struct lttng_session_trigger_list
*trigger_list
;
239 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
240 session_triggers_ht_node
);
242 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
246 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
248 struct channel_key
*channel_key
= (struct channel_key
*) key
;
249 struct channel_state_sample
*sample
;
251 sample
= caa_container_of(node
, struct channel_state_sample
,
252 channel_state_ht_node
);
254 return !!((channel_key
->key
== sample
->key
.key
) &&
255 (channel_key
->domain
== sample
->key
.domain
));
259 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
261 struct channel_key
*channel_key
= (struct channel_key
*) key
;
262 struct channel_info
*channel_info
;
264 channel_info
= caa_container_of(node
, struct channel_info
,
267 return !!((channel_key
->key
== channel_info
->key
.key
) &&
268 (channel_key
->domain
== channel_info
->key
.domain
));
272 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
274 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
275 struct lttng_trigger_ht_element
*trigger_ht_element
;
277 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
280 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
284 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
286 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
287 struct notification_client_list
*client_list
;
288 const struct lttng_condition
*condition
;
290 assert(condition_key
);
292 client_list
= caa_container_of(node
, struct notification_client_list
,
293 notification_trigger_clients_ht_node
);
294 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
296 return !!lttng_condition_is_equal(condition_key
, condition
);
300 int match_session(struct cds_lfht_node
*node
, const void *key
)
302 const char *name
= key
;
303 struct session_info
*session_info
= caa_container_of(
304 node
, struct session_info
, sessions_ht_node
);
306 return !strcmp(session_info
->name
, name
);
310 * Match trigger based on name and credentials only.
311 * Name duplication is NOT allowed for the same uid.
314 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
319 const char *key_name
;
320 enum lttng_trigger_status status
;
321 const struct lttng_credentials
*key_creds
;
322 const struct lttng_credentials
*node_creds
;
323 const struct lttng_trigger
*trigger_key
=
324 (const struct lttng_trigger
*) key
;
325 const struct lttng_trigger_ht_element
*trigger_ht_element
=
326 caa_container_of(node
,
327 struct lttng_trigger_ht_element
,
330 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
331 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
333 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
334 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
336 /* Compare the names. */
337 if (strcmp(name
, key_name
) != 0) {
341 /* Compare the owners' UIDs. */
342 key_creds
= lttng_trigger_get_credentials(trigger_key
);
343 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
345 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
352 * Hash trigger based on name and credentials only.
355 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
357 unsigned long hash
= 0;
358 const struct lttng_credentials
*trigger_creds
;
359 const char *trigger_name
;
360 enum lttng_trigger_status status
;
362 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
363 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
364 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
367 trigger_creds
= lttng_trigger_get_credentials(trigger
);
368 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
375 unsigned long hash_channel_key(struct channel_key
*key
)
377 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
378 unsigned long domain_hash
= hash_key_ulong(
379 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
381 return key_hash
^ domain_hash
;
385 unsigned long hash_client_socket(int socket
)
387 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
391 unsigned long hash_client_id(notification_client_id id
)
393 return hash_key_u64(&id
, lttng_ht_seed
);
397 * Get the type of object to which a given condition applies. Bindings let
398 * the notification system evaluate a trigger's condition when a given
399 * object's state is updated.
401 * For instance, a condition bound to a channel will be evaluated everytime
402 * the channel's state is changed by a channel monitoring sample.
405 enum lttng_object_type
get_condition_binding_object(
406 const struct lttng_condition
*condition
)
408 switch (lttng_condition_get_type(condition
)) {
409 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
410 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
411 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
412 return LTTNG_OBJECT_TYPE_CHANNEL
;
413 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
414 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
415 return LTTNG_OBJECT_TYPE_SESSION
;
416 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
417 return LTTNG_OBJECT_TYPE_NONE
;
419 return LTTNG_OBJECT_TYPE_UNKNOWN
;
424 void free_channel_info_rcu(struct rcu_head
*node
)
426 free(caa_container_of(node
, struct channel_info
, rcu_node
));
430 void channel_info_destroy(struct channel_info
*channel_info
)
436 if (channel_info
->session_info
) {
437 session_info_remove_channel(channel_info
->session_info
,
439 session_info_put(channel_info
->session_info
);
441 if (channel_info
->name
) {
442 free(channel_info
->name
);
444 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
448 void free_session_info_rcu(struct rcu_head
*node
)
450 free(caa_container_of(node
, struct session_info
, rcu_node
));
453 /* Don't call directly, use the ref-counting mechanism. */
455 void session_info_destroy(void *_data
)
457 struct session_info
*session_info
= _data
;
460 assert(session_info
);
461 if (session_info
->channel_infos_ht
) {
462 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
464 ERR("[notification-thread] Failed to destroy channel information hash table");
467 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
470 cds_lfht_del(session_info
->sessions_ht
,
471 &session_info
->sessions_ht_node
);
473 free(session_info
->name
);
474 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
478 void session_info_get(struct session_info
*session_info
)
483 lttng_ref_get(&session_info
->ref
);
487 void session_info_put(struct session_info
*session_info
)
492 lttng_ref_put(&session_info
->ref
);
496 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
497 struct lttng_session_trigger_list
*trigger_list
,
498 struct cds_lfht
*sessions_ht
)
500 struct session_info
*session_info
;
504 session_info
= zmalloc(sizeof(*session_info
));
508 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
510 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
511 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
512 if (!session_info
->channel_infos_ht
) {
516 cds_lfht_node_init(&session_info
->sessions_ht_node
);
517 session_info
->name
= strdup(name
);
518 if (!session_info
->name
) {
521 session_info
->uid
= uid
;
522 session_info
->gid
= gid
;
523 session_info
->trigger_list
= trigger_list
;
524 session_info
->sessions_ht
= sessions_ht
;
528 session_info_put(session_info
);
533 void session_info_add_channel(struct session_info
*session_info
,
534 struct channel_info
*channel_info
)
537 cds_lfht_add(session_info
->channel_infos_ht
,
538 hash_channel_key(&channel_info
->key
),
539 &channel_info
->session_info_channels_ht_node
);
544 void session_info_remove_channel(struct session_info
*session_info
,
545 struct channel_info
*channel_info
)
548 cds_lfht_del(session_info
->channel_infos_ht
,
549 &channel_info
->session_info_channels_ht_node
);
554 struct channel_info
*channel_info_create(const char *channel_name
,
555 struct channel_key
*channel_key
, uint64_t channel_capacity
,
556 struct session_info
*session_info
)
558 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
564 cds_lfht_node_init(&channel_info
->channels_ht_node
);
565 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
566 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
567 channel_info
->capacity
= channel_capacity
;
569 channel_info
->name
= strdup(channel_name
);
570 if (!channel_info
->name
) {
575 * Set the references between session and channel infos:
576 * - channel_info holds a strong reference to session_info
577 * - session_info holds a weak reference to channel_info
579 session_info_get(session_info
);
580 session_info_add_channel(session_info
, channel_info
);
581 channel_info
->session_info
= session_info
;
585 channel_info_destroy(channel_info
);
590 bool notification_client_list_get(struct notification_client_list
*list
)
592 return urcu_ref_get_unless_zero(&list
->ref
);
596 void free_notification_client_list_rcu(struct rcu_head
*node
)
598 free(caa_container_of(node
, struct notification_client_list
,
603 void notification_client_list_release(struct urcu_ref
*list_ref
)
605 struct notification_client_list
*list
=
606 container_of(list_ref
, typeof(*list
), ref
);
607 struct notification_client_list_element
*client_list_element
, *tmp
;
609 if (list
->notification_trigger_clients_ht
) {
611 cds_lfht_del(list
->notification_trigger_clients_ht
,
612 &list
->notification_trigger_clients_ht_node
);
614 list
->notification_trigger_clients_ht
= NULL
;
616 cds_list_for_each_entry_safe(client_list_element
, tmp
,
618 free(client_list_element
);
620 pthread_mutex_destroy(&list
->lock
);
621 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
625 struct notification_client_list
*notification_client_list_create(
626 const struct lttng_trigger
*trigger
)
628 struct notification_client_list
*client_list
=
629 zmalloc(sizeof(*client_list
));
634 pthread_mutex_init(&client_list
->lock
, NULL
);
635 urcu_ref_init(&client_list
->ref
);
636 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
637 CDS_INIT_LIST_HEAD(&client_list
->list
);
638 client_list
->trigger
= trigger
;
644 void publish_notification_client_list(
645 struct notification_thread_state
*state
,
646 struct notification_client_list
*list
)
648 const struct lttng_condition
*condition
=
649 lttng_trigger_get_const_condition(list
->trigger
);
651 assert(!list
->notification_trigger_clients_ht
);
652 notification_client_list_get(list
);
654 list
->notification_trigger_clients_ht
=
655 state
->notification_trigger_clients_ht
;
658 cds_lfht_add(state
->notification_trigger_clients_ht
,
659 lttng_condition_hash(condition
),
660 &list
->notification_trigger_clients_ht_node
);
665 void notification_client_list_put(struct notification_client_list
*list
)
670 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
673 /* Provides a reference to the returned list. */
675 struct notification_client_list
*get_client_list_from_condition(
676 struct notification_thread_state
*state
,
677 const struct lttng_condition
*condition
)
679 struct cds_lfht_node
*node
;
680 struct cds_lfht_iter iter
;
681 struct notification_client_list
*list
= NULL
;
684 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
685 lttng_condition_hash(condition
),
686 match_client_list_condition
,
689 node
= cds_lfht_iter_get_node(&iter
);
691 list
= container_of(node
, struct notification_client_list
,
692 notification_trigger_clients_ht_node
);
693 list
= notification_client_list_get(list
) ? list
: NULL
;
701 int evaluate_channel_condition_for_client(
702 const struct lttng_condition
*condition
,
703 struct notification_thread_state
*state
,
704 struct lttng_evaluation
**evaluation
,
705 uid_t
*session_uid
, gid_t
*session_gid
)
708 struct cds_lfht_iter iter
;
709 struct cds_lfht_node
*node
;
710 struct channel_info
*channel_info
= NULL
;
711 struct channel_key
*channel_key
= NULL
;
712 struct channel_state_sample
*last_sample
= NULL
;
713 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
717 /* Find the channel associated with the condition. */
718 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
719 channel_trigger_list
, channel_triggers_ht_node
) {
720 struct lttng_trigger_list_element
*element
;
722 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
723 const struct lttng_condition
*current_condition
=
724 lttng_trigger_get_const_condition(
727 assert(current_condition
);
728 if (!lttng_condition_is_equal(condition
,
729 current_condition
)) {
733 /* Found the trigger, save the channel key. */
734 channel_key
= &channel_trigger_list
->channel_key
;
738 /* The channel key was found stop iteration. */
744 /* No channel found; normal exit. */
745 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
750 /* Fetch channel info for the matching channel. */
751 cds_lfht_lookup(state
->channels_ht
,
752 hash_channel_key(channel_key
),
756 node
= cds_lfht_iter_get_node(&iter
);
758 channel_info
= caa_container_of(node
, struct channel_info
,
761 /* Retrieve the channel's last sample, if it exists. */
762 cds_lfht_lookup(state
->channel_state_ht
,
763 hash_channel_key(channel_key
),
764 match_channel_state_sample
,
767 node
= cds_lfht_iter_get_node(&iter
);
769 last_sample
= caa_container_of(node
,
770 struct channel_state_sample
,
771 channel_state_ht_node
);
773 /* Nothing to evaluate, no sample was ever taken. Normal exit */
774 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
779 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
781 0, channel_info
->session_info
->consumed_data_size
,
784 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
788 *session_uid
= channel_info
->session_info
->uid
;
789 *session_gid
= channel_info
->session_info
->gid
;
796 const char *get_condition_session_name(const struct lttng_condition
*condition
)
798 const char *session_name
= NULL
;
799 enum lttng_condition_status status
;
801 switch (lttng_condition_get_type(condition
)) {
802 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
803 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
804 status
= lttng_condition_buffer_usage_get_session_name(
805 condition
, &session_name
);
807 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
808 status
= lttng_condition_session_consumed_size_get_session_name(
809 condition
, &session_name
);
811 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
812 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
813 status
= lttng_condition_session_rotation_get_session_name(
814 condition
, &session_name
);
819 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
820 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
828 int evaluate_session_condition_for_client(
829 const struct lttng_condition
*condition
,
830 struct notification_thread_state
*state
,
831 struct lttng_evaluation
**evaluation
,
832 uid_t
*session_uid
, gid_t
*session_gid
)
835 struct cds_lfht_iter iter
;
836 struct cds_lfht_node
*node
;
837 const char *session_name
;
838 struct session_info
*session_info
= NULL
;
841 session_name
= get_condition_session_name(condition
);
843 /* Find the session associated with the trigger. */
844 cds_lfht_lookup(state
->sessions_ht
,
845 hash_key_str(session_name
, lttng_ht_seed
),
849 node
= cds_lfht_iter_get_node(&iter
);
851 DBG("[notification-thread] No known session matching name \"%s\"",
857 session_info
= caa_container_of(node
, struct session_info
,
859 session_info_get(session_info
);
862 * Evaluation is performed in-line here since only one type of
863 * session-bound condition is handled for the moment.
865 switch (lttng_condition_get_type(condition
)) {
866 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
867 if (!session_info
->rotation
.ongoing
) {
869 goto end_session_put
;
872 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
873 session_info
->rotation
.id
);
876 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
879 goto end_session_put
;
885 goto end_session_put
;
888 *session_uid
= session_info
->uid
;
889 *session_gid
= session_info
->gid
;
892 session_info_put(session_info
);
899 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
900 const struct lttng_condition
*condition
,
901 struct notification_client
*client
,
902 struct notification_thread_state
*state
)
905 struct lttng_evaluation
*evaluation
= NULL
;
906 struct notification_client_list client_list
= {
907 .lock
= PTHREAD_MUTEX_INITIALIZER
,
909 struct notification_client_list_element client_list_element
= { 0 };
910 uid_t object_uid
= 0;
911 gid_t object_gid
= 0;
918 switch (get_condition_binding_object(condition
)) {
919 case LTTNG_OBJECT_TYPE_SESSION
:
920 ret
= evaluate_session_condition_for_client(condition
, state
,
921 &evaluation
, &object_uid
, &object_gid
);
923 case LTTNG_OBJECT_TYPE_CHANNEL
:
924 ret
= evaluate_channel_condition_for_client(condition
, state
,
925 &evaluation
, &object_uid
, &object_gid
);
927 case LTTNG_OBJECT_TYPE_NONE
:
930 case LTTNG_OBJECT_TYPE_UNKNOWN
:
940 /* Evaluation yielded nothing. Normal exit. */
941 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
947 * Create a temporary client list with the client currently
950 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
951 CDS_INIT_LIST_HEAD(&client_list
.list
);
952 client_list
.trigger
= trigger
;
954 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
955 client_list_element
.client
= client
;
956 cds_list_add(&client_list_element
.node
, &client_list
.list
);
958 /* Send evaluation result to the newly-subscribed client. */
959 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
960 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
961 state
, object_uid
, object_gid
);
968 int notification_thread_client_subscribe(struct notification_client
*client
,
969 struct lttng_condition
*condition
,
970 struct notification_thread_state
*state
,
971 enum lttng_notification_channel_status
*_status
)
974 struct notification_client_list
*client_list
= NULL
;
975 struct lttng_condition_list_element
*condition_list_element
= NULL
;
976 struct notification_client_list_element
*client_list_element
= NULL
;
977 enum lttng_notification_channel_status status
=
978 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
981 * Ensure that the client has not already subscribed to this condition
984 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
985 if (lttng_condition_is_equal(condition_list_element
->condition
,
987 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
992 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
993 if (!condition_list_element
) {
997 client_list_element
= zmalloc(sizeof(*client_list_element
));
998 if (!client_list_element
) {
1004 * Add the newly-subscribed condition to the client's subscription list.
1006 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1007 condition_list_element
->condition
= condition
;
1008 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1010 client_list
= get_client_list_from_condition(state
, condition
);
1013 * No notification-emiting trigger registered with this
1014 * condition. We don't evaluate the condition right away
1015 * since this trigger is not registered yet.
1017 free(client_list_element
);
1022 * The condition to which the client just subscribed is evaluated
1023 * at this point so that conditions that are already TRUE result
1024 * in a notification being sent out.
1026 * The client_list's trigger is used without locking the list itself.
1027 * This is correct since the list doesn't own the trigger and the
1028 * object is immutable.
1030 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1032 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1034 free(client_list_element
);
1039 * Add the client to the list of clients interested in a given trigger
1040 * if a "notification" trigger with a corresponding condition was
1043 client_list_element
->client
= client
;
1044 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1046 pthread_mutex_lock(&client_list
->lock
);
1047 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1048 pthread_mutex_unlock(&client_list
->lock
);
1054 notification_client_list_put(client_list
);
1058 free(condition_list_element
);
1059 free(client_list_element
);
1064 int notification_thread_client_unsubscribe(
1065 struct notification_client
*client
,
1066 struct lttng_condition
*condition
,
1067 struct notification_thread_state
*state
,
1068 enum lttng_notification_channel_status
*_status
)
1070 struct notification_client_list
*client_list
;
1071 struct lttng_condition_list_element
*condition_list_element
,
1073 struct notification_client_list_element
*client_list_element
,
1075 bool condition_found
= false;
1076 enum lttng_notification_channel_status status
=
1077 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1079 /* Remove the condition from the client's condition list. */
1080 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1081 &client
->condition_list
, node
) {
1082 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1087 cds_list_del(&condition_list_element
->node
);
1089 * The caller may be iterating on the client's conditions to
1090 * tear down a client's connection. In this case, the condition
1091 * will be destroyed at the end.
1093 if (condition
!= condition_list_element
->condition
) {
1094 lttng_condition_destroy(
1095 condition_list_element
->condition
);
1097 free(condition_list_element
);
1098 condition_found
= true;
1102 if (!condition_found
) {
1103 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1108 * Remove the client from the list of clients interested the trigger
1109 * matching the condition.
1111 client_list
= get_client_list_from_condition(state
, condition
);
1116 pthread_mutex_lock(&client_list
->lock
);
1117 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1118 &client_list
->list
, node
) {
1119 if (client_list_element
->client
->id
!= client
->id
) {
1122 cds_list_del(&client_list_element
->node
);
1123 free(client_list_element
);
1126 pthread_mutex_unlock(&client_list
->lock
);
1127 notification_client_list_put(client_list
);
1130 lttng_condition_destroy(condition
);
1138 void free_notification_client_rcu(struct rcu_head
*node
)
1140 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1144 void notification_client_destroy(struct notification_client
*client
,
1145 struct notification_thread_state
*state
)
1152 * The client object is not reachable by other threads, no need to lock
1155 if (client
->socket
>= 0) {
1156 (void) lttcomm_close_unix_sock(client
->socket
);
1157 client
->socket
= -1;
1159 client
->communication
.active
= false;
1160 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1161 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1162 pthread_mutex_destroy(&client
->lock
);
1163 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1167 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1171 struct notification_client
*get_client_from_socket(int socket
,
1172 struct notification_thread_state
*state
)
1174 struct cds_lfht_iter iter
;
1175 struct cds_lfht_node
*node
;
1176 struct notification_client
*client
= NULL
;
1178 cds_lfht_lookup(state
->client_socket_ht
,
1179 hash_client_socket(socket
),
1180 match_client_socket
,
1181 (void *) (unsigned long) socket
,
1183 node
= cds_lfht_iter_get_node(&iter
);
1188 client
= caa_container_of(node
, struct notification_client
,
1189 client_socket_ht_node
);
1195 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1199 struct notification_client
*get_client_from_id(notification_client_id id
,
1200 struct notification_thread_state
*state
)
1202 struct cds_lfht_iter iter
;
1203 struct cds_lfht_node
*node
;
1204 struct notification_client
*client
= NULL
;
1206 cds_lfht_lookup(state
->client_id_ht
,
1211 node
= cds_lfht_iter_get_node(&iter
);
1216 client
= caa_container_of(node
, struct notification_client
,
1223 bool buffer_usage_condition_applies_to_channel(
1224 const struct lttng_condition
*condition
,
1225 const struct channel_info
*channel_info
)
1227 enum lttng_condition_status status
;
1228 enum lttng_domain_type condition_domain
;
1229 const char *condition_session_name
= NULL
;
1230 const char *condition_channel_name
= NULL
;
1232 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1234 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1235 if (channel_info
->key
.domain
!= condition_domain
) {
1239 status
= lttng_condition_buffer_usage_get_session_name(
1240 condition
, &condition_session_name
);
1241 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1243 status
= lttng_condition_buffer_usage_get_channel_name(
1244 condition
, &condition_channel_name
);
1245 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1247 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1250 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1260 bool session_consumed_size_condition_applies_to_channel(
1261 const struct lttng_condition
*condition
,
1262 const struct channel_info
*channel_info
)
1264 enum lttng_condition_status status
;
1265 const char *condition_session_name
= NULL
;
1267 status
= lttng_condition_session_consumed_size_get_session_name(
1268 condition
, &condition_session_name
);
1269 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1271 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1281 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1282 const struct channel_info
*channel_info
)
1284 const struct lttng_condition
*condition
;
1285 bool trigger_applies
;
1287 condition
= lttng_trigger_get_const_condition(trigger
);
1292 switch (lttng_condition_get_type(condition
)) {
1293 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1294 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1295 trigger_applies
= buffer_usage_condition_applies_to_channel(
1296 condition
, channel_info
);
1298 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1299 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1300 condition
, channel_info
);
1306 return trigger_applies
;
1312 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1313 struct notification_client
*client
)
1315 bool applies
= false;
1316 struct lttng_condition_list_element
*condition_list_element
;
1318 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1320 applies
= lttng_condition_is_equal(
1321 condition_list_element
->condition
,
1322 lttng_trigger_get_condition(trigger
));
1330 /* Must be called with RCU read lock held. */
1332 struct lttng_session_trigger_list
*get_session_trigger_list(
1333 struct notification_thread_state
*state
,
1334 const char *session_name
)
1336 struct lttng_session_trigger_list
*list
= NULL
;
1337 struct cds_lfht_node
*node
;
1338 struct cds_lfht_iter iter
;
1340 cds_lfht_lookup(state
->session_triggers_ht
,
1341 hash_key_str(session_name
, lttng_ht_seed
),
1342 match_session_trigger_list
,
1345 node
= cds_lfht_iter_get_node(&iter
);
1348 * Not an error, the list of triggers applying to that session
1349 * will be initialized when the session is created.
1351 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1356 list
= caa_container_of(node
,
1357 struct lttng_session_trigger_list
,
1358 session_triggers_ht_node
);
1364 * Allocate an empty lttng_session_trigger_list for the session named
1367 * No ownership of 'session_name' is assumed by the session trigger list.
1368 * It is the caller's responsability to ensure the session name is alive
1369 * for as long as this list is.
1372 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1373 const char *session_name
,
1374 struct cds_lfht
*session_triggers_ht
)
1376 struct lttng_session_trigger_list
*list
;
1378 list
= zmalloc(sizeof(*list
));
1382 list
->session_name
= session_name
;
1383 CDS_INIT_LIST_HEAD(&list
->list
);
1384 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1385 list
->session_triggers_ht
= session_triggers_ht
;
1388 /* Publish the list through the session_triggers_ht. */
1389 cds_lfht_add(session_triggers_ht
,
1390 hash_key_str(session_name
, lttng_ht_seed
),
1391 &list
->session_triggers_ht_node
);
1398 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1400 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1405 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1407 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1409 /* Empty the list element by element, and then free the list itself. */
1410 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1411 &list
->list
, node
) {
1412 cds_list_del(&trigger_list_element
->node
);
1413 free(trigger_list_element
);
1416 /* Unpublish the list from the session_triggers_ht. */
1417 cds_lfht_del(list
->session_triggers_ht
,
1418 &list
->session_triggers_ht_node
);
1420 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1424 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1425 struct lttng_trigger
*trigger
)
1428 struct lttng_trigger_list_element
*new_element
=
1429 zmalloc(sizeof(*new_element
));
1435 CDS_INIT_LIST_HEAD(&new_element
->node
);
1436 new_element
->trigger
= trigger
;
1437 cds_list_add(&new_element
->node
, &list
->list
);
1443 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1444 const char *session_name
)
1446 bool applies
= false;
1447 const struct lttng_condition
*condition
;
1449 condition
= lttng_trigger_get_const_condition(trigger
);
1450 switch (lttng_condition_get_type(condition
)) {
1451 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1452 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1454 enum lttng_condition_status condition_status
;
1455 const char *condition_session_name
;
1457 condition_status
= lttng_condition_session_rotation_get_session_name(
1458 condition
, &condition_session_name
);
1459 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1460 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1464 assert(condition_session_name
);
1465 applies
= !strcmp(condition_session_name
, session_name
);
1476 * Allocate and initialize an lttng_session_trigger_list which contains
1477 * all triggers that apply to the session named 'session_name'.
1479 * No ownership of 'session_name' is assumed by the session trigger list.
1480 * It is the caller's responsability to ensure the session name is alive
1481 * for as long as this list is.
1484 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1485 const struct notification_thread_state
*state
,
1486 const char *session_name
)
1488 int trigger_count
= 0;
1489 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1490 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1491 struct cds_lfht_iter iter
;
1493 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1494 state
->session_triggers_ht
);
1496 /* Add all triggers applying to the session named 'session_name'. */
1497 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1501 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1506 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1507 trigger_ht_element
->trigger
);
1515 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1517 return session_trigger_list
;
1519 lttng_session_trigger_list_destroy(session_trigger_list
);
1524 struct session_info
*find_or_create_session_info(
1525 struct notification_thread_state
*state
,
1526 const char *name
, uid_t uid
, gid_t gid
)
1528 struct session_info
*session
= NULL
;
1529 struct cds_lfht_node
*node
;
1530 struct cds_lfht_iter iter
;
1531 struct lttng_session_trigger_list
*trigger_list
;
1534 cds_lfht_lookup(state
->sessions_ht
,
1535 hash_key_str(name
, lttng_ht_seed
),
1539 node
= cds_lfht_iter_get_node(&iter
);
1541 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1543 session
= caa_container_of(node
, struct session_info
,
1545 assert(session
->uid
== uid
);
1546 assert(session
->gid
== gid
);
1547 session_info_get(session
);
1551 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1552 if (!trigger_list
) {
1556 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1557 state
->sessions_ht
);
1559 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1561 lttng_session_trigger_list_destroy(trigger_list
);
1564 trigger_list
= NULL
;
1566 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1567 &session
->sessions_ht_node
);
1573 session_info_put(session
);
1578 int handle_notification_thread_command_add_channel(
1579 struct notification_thread_state
*state
,
1580 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1581 const char *channel_name
, enum lttng_domain_type channel_domain
,
1582 uint64_t channel_key_int
, uint64_t channel_capacity
,
1583 enum lttng_error_code
*cmd_result
)
1585 struct cds_list_head trigger_list
;
1586 struct channel_info
*new_channel_info
= NULL
;
1587 struct channel_key channel_key
= {
1588 .key
= channel_key_int
,
1589 .domain
= channel_domain
,
1591 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1592 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1593 int trigger_count
= 0;
1594 struct cds_lfht_iter iter
;
1595 struct session_info
*session_info
= NULL
;
1597 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1598 channel_name
, session_name
, channel_key_int
,
1599 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1601 CDS_INIT_LIST_HEAD(&trigger_list
);
1603 session_info
= find_or_create_session_info(state
, session_name
,
1604 session_uid
, session_gid
);
1605 if (!session_info
) {
1606 /* Allocation error or an internal error occurred. */
1610 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1611 channel_capacity
, session_info
);
1612 if (!new_channel_info
) {
1617 /* Build a list of all triggers applying to the new channel. */
1618 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1620 struct lttng_trigger_list_element
*new_element
;
1622 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1623 new_channel_info
)) {
1627 new_element
= zmalloc(sizeof(*new_element
));
1632 CDS_INIT_LIST_HEAD(&new_element
->node
);
1633 new_element
->trigger
= trigger_ht_element
->trigger
;
1634 cds_list_add(&new_element
->node
, &trigger_list
);
1639 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1641 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1642 if (!channel_trigger_list
) {
1645 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1646 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1647 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1648 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1651 /* Add channel to the channel_ht which owns the channel_infos. */
1652 cds_lfht_add(state
->channels_ht
,
1653 hash_channel_key(&new_channel_info
->key
),
1654 &new_channel_info
->channels_ht_node
);
1656 * Add the list of triggers associated with this channel to the
1657 * channel_triggers_ht.
1659 cds_lfht_add(state
->channel_triggers_ht
,
1660 hash_channel_key(&new_channel_info
->key
),
1661 &channel_trigger_list
->channel_triggers_ht_node
);
1663 session_info_put(session_info
);
1664 *cmd_result
= LTTNG_OK
;
1667 channel_info_destroy(new_channel_info
);
1668 session_info_put(session_info
);
1673 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1675 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1680 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1682 free(caa_container_of(node
, struct channel_state_sample
,
1687 int handle_notification_thread_command_remove_channel(
1688 struct notification_thread_state
*state
,
1689 uint64_t channel_key
, enum lttng_domain_type domain
,
1690 enum lttng_error_code
*cmd_result
)
1692 struct cds_lfht_node
*node
;
1693 struct cds_lfht_iter iter
;
1694 struct lttng_channel_trigger_list
*trigger_list
;
1695 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1696 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1697 struct channel_info
*channel_info
;
1699 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1700 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1704 cds_lfht_lookup(state
->channel_triggers_ht
,
1705 hash_channel_key(&key
),
1706 match_channel_trigger_list
,
1709 node
= cds_lfht_iter_get_node(&iter
);
1711 * There is a severe internal error if we are being asked to remove a
1712 * channel that doesn't exist.
1715 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1719 /* Free the list of triggers associated with this channel. */
1720 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1721 channel_triggers_ht_node
);
1722 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1723 &trigger_list
->list
, node
) {
1724 cds_list_del(&trigger_list_element
->node
);
1725 free(trigger_list_element
);
1727 cds_lfht_del(state
->channel_triggers_ht
, node
);
1728 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1730 /* Free sampled channel state. */
1731 cds_lfht_lookup(state
->channel_state_ht
,
1732 hash_channel_key(&key
),
1733 match_channel_state_sample
,
1736 node
= cds_lfht_iter_get_node(&iter
);
1738 * This is expected to be NULL if the channel is destroyed before we
1739 * received a sample.
1742 struct channel_state_sample
*sample
= caa_container_of(node
,
1743 struct channel_state_sample
,
1744 channel_state_ht_node
);
1746 cds_lfht_del(state
->channel_state_ht
, node
);
1747 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1750 /* Remove the channel from the channels_ht and free it. */
1751 cds_lfht_lookup(state
->channels_ht
,
1752 hash_channel_key(&key
),
1756 node
= cds_lfht_iter_get_node(&iter
);
1758 channel_info
= caa_container_of(node
, struct channel_info
,
1760 cds_lfht_del(state
->channels_ht
, node
);
1761 channel_info_destroy(channel_info
);
1764 *cmd_result
= LTTNG_OK
;
1769 int handle_notification_thread_command_session_rotation(
1770 struct notification_thread_state
*state
,
1771 enum notification_thread_command_type cmd_type
,
1772 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1773 uint64_t trace_archive_chunk_id
,
1774 struct lttng_trace_archive_location
*location
,
1775 enum lttng_error_code
*_cmd_result
)
1778 enum lttng_error_code cmd_result
= LTTNG_OK
;
1779 struct lttng_session_trigger_list
*trigger_list
;
1780 struct lttng_trigger_list_element
*trigger_list_element
;
1781 struct session_info
*session_info
;
1782 const struct lttng_credentials session_creds
= {
1783 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1784 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1789 session_info
= find_or_create_session_info(state
, session_name
,
1790 session_uid
, session_gid
);
1791 if (!session_info
) {
1792 /* Allocation error or an internal error occurred. */
1794 cmd_result
= LTTNG_ERR_NOMEM
;
1798 session_info
->rotation
.ongoing
=
1799 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1800 session_info
->rotation
.id
= trace_archive_chunk_id
;
1801 trigger_list
= get_session_trigger_list(state
, session_name
);
1802 if (!trigger_list
) {
1803 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1808 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1810 const struct lttng_condition
*condition
;
1811 struct lttng_trigger
*trigger
;
1812 struct notification_client_list
*client_list
;
1813 struct lttng_evaluation
*evaluation
= NULL
;
1814 enum lttng_condition_type condition_type
;
1815 enum action_executor_status executor_status
;
1817 trigger
= trigger_list_element
->trigger
;
1818 condition
= lttng_trigger_get_const_condition(trigger
);
1820 condition_type
= lttng_condition_get_type(condition
);
1822 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1823 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1825 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1826 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1830 client_list
= get_client_list_from_condition(state
, condition
);
1831 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1832 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1833 trace_archive_chunk_id
);
1835 evaluation
= lttng_evaluation_session_rotation_completed_create(
1836 trace_archive_chunk_id
, location
);
1840 /* Internal error */
1842 cmd_result
= LTTNG_ERR_UNK
;
1847 * Ownership of `evaluation` transferred to the action executor
1848 * no matter the result.
1850 executor_status
= action_executor_enqueue(state
->executor
,
1851 trigger
, evaluation
, &session_creds
,
1854 switch (executor_status
) {
1855 case ACTION_EXECUTOR_STATUS_OK
:
1857 case ACTION_EXECUTOR_STATUS_ERROR
:
1858 case ACTION_EXECUTOR_STATUS_INVALID
:
1860 * TODO Add trigger identification (name/id) when
1861 * it is added to the API.
1863 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1866 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1868 * TODO Add trigger identification (name/id) when
1869 * it is added to the API.
1871 * Not a fatal error.
1873 WARN("No space left when enqueuing action associated with session rotation trigger");
1881 notification_client_list_put(client_list
);
1882 if (caa_unlikely(ret
)) {
1887 session_info_put(session_info
);
1888 *_cmd_result
= cmd_result
;
1893 static int handle_notification_thread_command_list_triggers(
1894 struct notification_thread_handle
*handle
,
1895 struct notification_thread_state
*state
,
1897 struct lttng_triggers
**triggers
,
1898 enum lttng_error_code
*_cmd_result
)
1901 enum lttng_error_code cmd_result
= LTTNG_OK
;
1902 struct cds_lfht_iter iter
;
1903 struct lttng_trigger_ht_element
*trigger_ht_element
;
1904 struct lttng_triggers
*local_triggers
= NULL
;
1905 const struct lttng_credentials
*creds
;
1909 local_triggers
= lttng_triggers_create();
1910 if (!local_triggers
) {
1911 /* Not a fatal error. */
1912 cmd_result
= LTTNG_ERR_NOMEM
;
1916 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
1917 trigger_ht_element
, node
) {
1919 * Only return the triggers to which the client has access.
1920 * The root user has visibility over all triggers.
1922 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
1923 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
1927 ret
= lttng_triggers_add(local_triggers
,
1928 trigger_ht_element
->trigger
);
1930 /* Not a fatal error. */
1932 cmd_result
= LTTNG_ERR_NOMEM
;
1937 /* Transferring ownership to the caller. */
1938 *triggers
= local_triggers
;
1939 local_triggers
= NULL
;
1943 lttng_triggers_destroy(local_triggers
);
1944 *_cmd_result
= cmd_result
;
1949 bool condition_is_supported(struct lttng_condition
*condition
)
1953 switch (lttng_condition_get_type(condition
)) {
1954 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1955 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1958 enum lttng_domain_type domain
;
1960 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1964 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1965 is_supported
= true;
1970 * Older kernel tracers don't expose the API to monitor their
1971 * buffers. Therefore, we reject triggers that require that
1972 * mechanism to be available to be evaluated.
1974 * Assume unsupported on error.
1976 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
1979 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
1981 const struct lttng_event_rule
*event_rule
;
1982 enum lttng_domain_type domain
;
1983 const enum lttng_condition_status status
=
1984 lttng_condition_event_rule_get_rule(
1985 condition
, &event_rule
);
1987 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1989 domain
= lttng_event_rule_get_domain_type(event_rule
);
1990 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1991 is_supported
= true;
1996 * Older kernel tracers can't emit notification. Therefore, we
1997 * reject triggers that require that mechanism to be available
2000 * Assume unsupported on error.
2002 is_supported
= kernel_supports_event_notifiers() == 1;
2006 is_supported
= true;
2009 return is_supported
;
2012 /* Must be called with RCU read lock held. */
2014 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2015 struct notification_thread_state
*state
)
2018 const struct lttng_condition
*condition
;
2019 const char *session_name
;
2020 struct lttng_session_trigger_list
*trigger_list
;
2022 condition
= lttng_trigger_get_const_condition(trigger
);
2023 switch (lttng_condition_get_type(condition
)) {
2024 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2025 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2027 enum lttng_condition_status status
;
2029 status
= lttng_condition_session_rotation_get_session_name(
2030 condition
, &session_name
);
2031 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2032 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2043 trigger_list
= get_session_trigger_list(state
, session_name
);
2044 if (!trigger_list
) {
2045 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2051 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2053 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2058 /* Must be called with RCU read lock held. */
2060 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2061 struct notification_thread_state
*state
)
2064 struct cds_lfht_node
*node
;
2065 struct cds_lfht_iter iter
;
2066 struct channel_info
*channel
;
2068 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2070 struct lttng_trigger_list_element
*trigger_list_element
;
2071 struct lttng_channel_trigger_list
*trigger_list
;
2072 struct cds_lfht_iter lookup_iter
;
2074 if (!trigger_applies_to_channel(trigger
, channel
)) {
2078 cds_lfht_lookup(state
->channel_triggers_ht
,
2079 hash_channel_key(&channel
->key
),
2080 match_channel_trigger_list
,
2083 node
= cds_lfht_iter_get_node(&lookup_iter
);
2085 trigger_list
= caa_container_of(node
,
2086 struct lttng_channel_trigger_list
,
2087 channel_triggers_ht_node
);
2089 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2090 if (!trigger_list_element
) {
2094 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2095 trigger_list_element
->trigger
= trigger
;
2096 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2097 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2105 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2107 bool is_notify
= false;
2108 unsigned int i
, count
;
2109 enum lttng_action_status action_status
;
2110 const struct lttng_action
*action
=
2111 lttng_trigger_get_const_action(trigger
);
2112 enum lttng_action_type action_type
;
2115 action_type
= lttng_action_get_type(action
);
2116 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2119 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2123 action_status
= lttng_action_group_get_count(action
, &count
);
2124 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2126 for (i
= 0; i
< count
; i
++) {
2127 const struct lttng_action
*inner_action
=
2128 lttng_action_group_get_at_index(
2131 action_type
= lttng_action_get_type(inner_action
);
2132 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2142 static bool trigger_name_taken(struct notification_thread_state
*state
,
2143 const struct lttng_trigger
*trigger
)
2145 struct cds_lfht_iter iter
;
2148 * No duplicata is allowed in the triggers_by_name_uid_ht.
2149 * The match is done against the trigger name and uid.
2151 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2152 hash_trigger_by_name_uid(trigger
),
2153 match_trigger_by_name_uid
,
2156 return !!cds_lfht_iter_get_node(&iter
);
2160 enum lttng_error_code
generate_trigger_name(
2161 struct notification_thread_state
*state
,
2162 struct lttng_trigger
*trigger
, const char **name
)
2164 enum lttng_error_code ret_code
= LTTNG_OK
;
2166 enum lttng_trigger_status status
;
2169 const int ret
= lttng_trigger_generate_name(trigger
,
2170 state
->trigger_id
.name_offset
++);
2172 /* The only reason this can fail right now. */
2173 ret_code
= LTTNG_ERR_NOMEM
;
2177 status
= lttng_trigger_get_name(trigger
, name
);
2178 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2180 taken
= trigger_name_taken(state
, trigger
);
2181 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2187 * FIXME A client's credentials are not checked when registering a trigger.
2189 * The effects of this are benign since:
2190 * - The client will succeed in registering the trigger, as it is valid,
2191 * - The trigger will, internally, be bound to the channel/session,
2192 * - The notifications will not be sent since the client's credentials
2193 * are checked against the channel at that moment.
2195 * If this function returns a non-zero value, it means something is
2196 * fundamentally broken and the whole subsystem/thread will be torn down.
2198 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2202 int handle_notification_thread_command_register_trigger(
2203 struct notification_thread_state
*state
,
2204 struct lttng_trigger
*trigger
,
2205 enum lttng_error_code
*cmd_result
)
2208 struct lttng_condition
*condition
;
2209 struct notification_client
*client
;
2210 struct notification_client_list
*client_list
= NULL
;
2211 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2212 struct notification_client_list_element
*client_list_element
;
2213 struct cds_lfht_node
*node
;
2214 struct cds_lfht_iter iter
;
2215 const char* trigger_name
;
2216 bool free_trigger
= true;
2217 struct lttng_evaluation
*evaluation
= NULL
;
2218 struct lttng_credentials object_creds
;
2221 enum action_executor_status executor_status
;
2222 const uint64_t trigger_tracer_token
=
2223 state
->trigger_id
.next_tracer_token
++;
2227 /* Set the trigger's tracer token. */
2228 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2230 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2231 LTTNG_TRIGGER_STATUS_UNSET
) {
2232 const enum lttng_error_code ret_code
= generate_trigger_name(
2233 state
, trigger
, &trigger_name
);
2235 if (ret_code
!= LTTNG_OK
) {
2238 *cmd_result
= ret_code
;
2241 } else if (trigger_name_taken(state
, trigger
)) {
2242 /* Not a fatal error. */
2243 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2248 condition
= lttng_trigger_get_condition(trigger
);
2251 /* Some conditions require tracers to implement a minimal ABI version. */
2252 if (!condition_is_supported(condition
)) {
2253 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2257 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2258 if (!trigger_ht_element
) {
2263 /* Add trigger to the trigger_ht. */
2264 cds_lfht_node_init(&trigger_ht_element
->node
);
2265 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2266 trigger_ht_element
->trigger
= trigger
;
2268 node
= cds_lfht_add_unique(state
->triggers_ht
,
2269 lttng_condition_hash(condition
),
2272 &trigger_ht_element
->node
);
2273 if (node
!= &trigger_ht_element
->node
) {
2274 /* Not a fatal error, simply report it to the client. */
2275 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2276 goto error_free_ht_element
;
2279 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2280 hash_trigger_by_name_uid(trigger
),
2281 match_trigger_by_name_uid
,
2283 &trigger_ht_element
->node_by_name_uid
);
2284 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2285 /* Not a fatal error, simply report it to the client. */
2286 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2287 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2288 goto error_free_ht_element
;
2292 * Ownership of the trigger and of its wrapper was transfered to
2295 trigger_ht_element
= NULL
;
2296 free_trigger
= false;
2299 * The rest only applies to triggers that have a "notify" action.
2300 * It is not skipped as this is the only action type currently
2303 if (is_trigger_action_notify(trigger
)) {
2304 client_list
= notification_client_list_create(trigger
);
2307 goto error_free_ht_element
;
2310 /* Build a list of clients to which this new trigger applies. */
2311 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2312 client_socket_ht_node
) {
2313 if (!trigger_applies_to_client(trigger
, client
)) {
2317 client_list_element
=
2318 zmalloc(sizeof(*client_list_element
));
2319 if (!client_list_element
) {
2321 goto error_put_client_list
;
2324 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2325 client_list_element
->client
= client
;
2326 cds_list_add(&client_list_element
->node
,
2327 &client_list
->list
);
2331 * Client list ownership transferred to the
2332 * notification_trigger_clients_ht.
2334 publish_notification_client_list(state
, client_list
);
2337 switch (get_condition_binding_object(condition
)) {
2338 case LTTNG_OBJECT_TYPE_SESSION
:
2339 /* Add the trigger to the list if it matches a known session. */
2340 ret
= bind_trigger_to_matching_session(trigger
, state
);
2342 goto error_put_client_list
;
2345 case LTTNG_OBJECT_TYPE_CHANNEL
:
2347 * Add the trigger to list of triggers bound to the channels
2350 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2352 goto error_put_client_list
;
2355 case LTTNG_OBJECT_TYPE_NONE
:
2358 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2360 goto error_put_client_list
;
2364 * The new trigger's condition must be evaluated against the current
2367 * In the case of `notify` action, nothing preventing clients from
2368 * subscribing to a condition before the corresponding trigger is
2369 * registered, we have to evaluate this new condition right away.
2371 * At some point, we were waiting for the next "evaluation" (e.g. on
2372 * reception of a channel sample) to evaluate this new condition, but
2375 * The reason it was broken is that waiting for the next sample
2376 * does not allow us to properly handle transitions for edge-triggered
2379 * Consider this example: when we handle a new channel sample, we
2380 * evaluate each conditions twice: once with the previous state, and
2381 * again with the newest state. We then use those two results to
2382 * determine whether a state change happened: a condition was false and
2383 * became true. If a state change happened, we have to notify clients.
2385 * Now, if a client subscribes to a given notification and registers
2386 * a trigger *after* that subscription, we have to make sure the
2387 * condition is evaluated at this point while considering only the
2388 * current state. Otherwise, the next evaluation cycle may only see
2389 * that the evaluations remain the same (true for samples n-1 and n) and
2390 * the client will never know that the condition has been met.
2392 switch (get_condition_binding_object(condition
)) {
2393 case LTTNG_OBJECT_TYPE_SESSION
:
2394 ret
= evaluate_session_condition_for_client(condition
, state
,
2395 &evaluation
, &object_uid
,
2398 case LTTNG_OBJECT_TYPE_CHANNEL
:
2399 ret
= evaluate_channel_condition_for_client(condition
, state
,
2400 &evaluation
, &object_uid
,
2403 case LTTNG_OBJECT_TYPE_NONE
:
2406 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2414 goto error_put_client_list
;
2417 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2418 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2420 DBG("Newly registered trigger's condition evaluated to %s",
2421 evaluation
? "true" : "false");
2423 /* Evaluation yielded nothing. Normal exit. */
2429 * Ownership of `evaluation` transferred to the action executor
2430 * no matter the result.
2432 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2433 evaluation
, &object_creds
, client_list
);
2435 switch (executor_status
) {
2436 case ACTION_EXECUTOR_STATUS_OK
:
2438 case ACTION_EXECUTOR_STATUS_ERROR
:
2439 case ACTION_EXECUTOR_STATUS_INVALID
:
2441 * TODO Add trigger identification (name/id) when
2442 * it is added to the API.
2444 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2446 goto error_put_client_list
;
2447 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2449 * TODO Add trigger identification (name/id) when
2450 * it is added to the API.
2452 * Not a fatal error.
2454 WARN("No space left when enqueuing action associated to newly registered trigger");
2462 *cmd_result
= LTTNG_OK
;
2463 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2464 trigger_name
, trigger_tracer_token
);
2466 error_put_client_list
:
2467 notification_client_list_put(client_list
);
2469 error_free_ht_element
:
2470 if (trigger_ht_element
) {
2471 /* Delayed removal due to RCU constraint on delete. */
2472 call_rcu(&trigger_ht_element
->rcu_node
,
2473 free_lttng_trigger_ht_element_rcu
);
2478 lttng_trigger_destroy(trigger
);
2485 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2487 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2492 int handle_notification_thread_command_unregister_trigger(
2493 struct notification_thread_state
*state
,
2494 struct lttng_trigger
*trigger
,
2495 enum lttng_error_code
*_cmd_reply
)
2497 struct cds_lfht_iter iter
;
2498 struct cds_lfht_node
*triggers_ht_node
;
2499 struct lttng_channel_trigger_list
*trigger_list
;
2500 struct notification_client_list
*client_list
;
2501 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2502 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2504 enum lttng_error_code cmd_reply
;
2508 cds_lfht_lookup(state
->triggers_ht
,
2509 lttng_condition_hash(condition
),
2513 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2514 if (!triggers_ht_node
) {
2515 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2518 cmd_reply
= LTTNG_OK
;
2521 /* Remove trigger from channel_triggers_ht. */
2522 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2523 channel_triggers_ht_node
) {
2524 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2526 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2527 &trigger_list
->list
, node
) {
2528 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2532 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2533 cds_list_del(&trigger_element
->node
);
2534 /* A trigger can only appear once per channel */
2539 if (is_trigger_action_notify(trigger
)) {
2541 * Remove and release the client list from
2542 * notification_trigger_clients_ht.
2544 client_list
= get_client_list_from_condition(state
, condition
);
2545 assert(client_list
);
2547 /* Put new reference and the hashtable's reference. */
2548 notification_client_list_put(client_list
);
2549 notification_client_list_put(client_list
);
2553 /* Remove trigger from triggers_ht. */
2554 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2555 struct lttng_trigger_ht_element
, node
);
2556 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2557 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2559 /* Release the ownership of the trigger. */
2560 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2561 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2565 *_cmd_reply
= cmd_reply
;
2570 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2571 int handle_notification_thread_command(
2572 struct notification_thread_handle
*handle
,
2573 struct notification_thread_state
*state
)
2577 struct notification_thread_command
*cmd
;
2579 /* Read the event pipe to put it back into a quiescent state. */
2580 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2582 if (ret
!= sizeof(counter
)) {
2586 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2587 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2588 struct notification_thread_command
, cmd_list_node
);
2589 cds_list_del(&cmd
->cmd_list_node
);
2590 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2591 switch (cmd
->type
) {
2592 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2593 DBG("[notification-thread] Received register trigger command");
2594 ret
= handle_notification_thread_command_register_trigger(
2595 state
, cmd
->parameters
.trigger
,
2598 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2599 DBG("[notification-thread] Received unregister trigger command");
2600 ret
= handle_notification_thread_command_unregister_trigger(
2601 state
, cmd
->parameters
.trigger
,
2604 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2605 DBG("[notification-thread] Received add channel command");
2606 ret
= handle_notification_thread_command_add_channel(
2608 cmd
->parameters
.add_channel
.session
.name
,
2609 cmd
->parameters
.add_channel
.session
.uid
,
2610 cmd
->parameters
.add_channel
.session
.gid
,
2611 cmd
->parameters
.add_channel
.channel
.name
,
2612 cmd
->parameters
.add_channel
.channel
.domain
,
2613 cmd
->parameters
.add_channel
.channel
.key
,
2614 cmd
->parameters
.add_channel
.channel
.capacity
,
2617 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2618 DBG("[notification-thread] Received remove channel command");
2619 ret
= handle_notification_thread_command_remove_channel(
2620 state
, cmd
->parameters
.remove_channel
.key
,
2621 cmd
->parameters
.remove_channel
.domain
,
2624 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2625 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2626 DBG("[notification-thread] Received session rotation %s command",
2627 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2628 "ongoing" : "completed");
2629 ret
= handle_notification_thread_command_session_rotation(
2632 cmd
->parameters
.session_rotation
.session_name
,
2633 cmd
->parameters
.session_rotation
.uid
,
2634 cmd
->parameters
.session_rotation
.gid
,
2635 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2636 cmd
->parameters
.session_rotation
.location
,
2639 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2641 struct lttng_triggers
*triggers
= NULL
;
2643 ret
= handle_notification_thread_command_list_triggers(
2646 cmd
->parameters
.list_triggers
.uid
,
2649 cmd
->reply
.list_triggers
.triggers
= triggers
;
2653 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2654 DBG("[notification-thread] Received quit command");
2655 cmd
->reply_code
= LTTNG_OK
;
2658 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2660 const enum client_transmission_status client_status
=
2661 cmd
->parameters
.client_communication_update
2663 const notification_client_id client_id
=
2664 cmd
->parameters
.client_communication_update
.id
;
2665 struct notification_client
*client
;
2668 client
= get_client_from_id(client_id
, state
);
2672 * Client error was probably already picked-up by the
2673 * notification thread or it has disconnected
2674 * gracefully while this command was queued.
2676 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2680 ret
= client_handle_transmission_status(
2681 client
, client_status
, state
);
2687 ERR("[notification-thread] Unknown internal command received");
2695 if (cmd
->is_async
) {
2699 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2703 /* Wake-up and return a fatal error to the calling thread. */
2704 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2705 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2707 /* Indicate a fatal error to the caller. */
2712 int socket_set_non_blocking(int socket
)
2716 /* Set the pipe as non-blocking. */
2717 ret
= fcntl(socket
, F_GETFL
, 0);
2719 PERROR("fcntl get socket flags");
2724 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2726 PERROR("fcntl set O_NONBLOCK socket flag");
2729 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2735 int client_reset_inbound_state(struct notification_client
*client
)
2740 lttng_payload_clear(&client
->communication
.inbound
.payload
);
2742 client
->communication
.inbound
.bytes_to_receive
=
2743 sizeof(struct lttng_notification_channel_message
);
2744 client
->communication
.inbound
.msg_type
=
2745 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2746 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2747 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2748 ret
= lttng_dynamic_buffer_set_size(
2749 &client
->communication
.inbound
.payload
.buffer
,
2750 client
->communication
.inbound
.bytes_to_receive
);
2755 int handle_notification_thread_client_connect(
2756 struct notification_thread_state
*state
)
2759 struct notification_client
*client
;
2761 DBG("[notification-thread] Handling new notification channel client connection");
2763 client
= zmalloc(sizeof(*client
));
2770 pthread_mutex_init(&client
->lock
, NULL
);
2771 client
->id
= state
->next_notification_client_id
++;
2772 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2773 lttng_payload_init(&client
->communication
.inbound
.payload
);
2774 lttng_payload_init(&client
->communication
.outbound
.payload
);
2775 client
->communication
.inbound
.expect_creds
= true;
2777 ret
= client_reset_inbound_state(client
);
2779 ERR("[notification-thread] Failed to reset client communication's inbound state");
2784 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2786 ERR("[notification-thread] Failed to accept new notification channel client connection");
2791 client
->socket
= ret
;
2793 ret
= socket_set_non_blocking(client
->socket
);
2795 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2799 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2801 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2806 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2807 LPOLLIN
| LPOLLERR
|
2808 LPOLLHUP
| LPOLLRDHUP
);
2810 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2814 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2818 cds_lfht_add(state
->client_socket_ht
,
2819 hash_client_socket(client
->socket
),
2820 &client
->client_socket_ht_node
);
2821 cds_lfht_add(state
->client_id_ht
,
2822 hash_client_id(client
->id
),
2823 &client
->client_id_ht_node
);
2829 notification_client_destroy(client
, state
);
2834 * RCU read-lock must be held by the caller.
2835 * Client lock must _not_ be held by the caller.
2838 int notification_thread_client_disconnect(
2839 struct notification_client
*client
,
2840 struct notification_thread_state
*state
)
2843 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
2845 /* Acquire the client lock to disable its communication atomically. */
2846 pthread_mutex_lock(&client
->lock
);
2847 client
->communication
.active
= false;
2848 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
2849 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
2850 pthread_mutex_unlock(&client
->lock
);
2852 ret
= lttng_poll_del(&state
->events
, client
->socket
);
2854 ERR("[notification-thread] Failed to remove client socket %d from poll set",
2858 /* Release all conditions to which the client was subscribed. */
2859 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
2860 &client
->condition_list
, node
) {
2861 (void) notification_thread_client_unsubscribe(client
,
2862 condition_list_element
->condition
, state
, NULL
);
2866 * Client no longer accessible to other threads (through the
2869 notification_client_destroy(client
, state
);
2873 int handle_notification_thread_client_disconnect(
2874 int client_socket
, struct notification_thread_state
*state
)
2877 struct notification_client
*client
;
2880 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2882 client
= get_client_from_socket(client_socket
, state
);
2884 /* Internal state corruption, fatal error. */
2885 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2891 ret
= notification_thread_client_disconnect(client
, state
);
2897 int handle_notification_thread_client_disconnect_all(
2898 struct notification_thread_state
*state
)
2900 struct cds_lfht_iter iter
;
2901 struct notification_client
*client
;
2902 bool error_encoutered
= false;
2905 DBG("[notification-thread] Closing all client connections");
2906 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2907 client_socket_ht_node
) {
2910 ret
= notification_thread_client_disconnect(
2913 error_encoutered
= true;
2917 return error_encoutered
? 1 : 0;
2920 int handle_notification_thread_trigger_unregister_all(
2921 struct notification_thread_state
*state
)
2923 bool error_occurred
= false;
2924 struct cds_lfht_iter iter
;
2925 struct lttng_trigger_ht_element
*trigger_ht_element
;
2928 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2930 int ret
= handle_notification_thread_command_unregister_trigger(
2931 state
, trigger_ht_element
->trigger
, NULL
);
2933 error_occurred
= true;
2937 return error_occurred
? -1 : 0;
2941 int client_handle_transmission_status(
2942 struct notification_client
*client
,
2943 enum client_transmission_status transmission_status
,
2944 struct notification_thread_state
*state
)
2948 switch (transmission_status
) {
2949 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
2950 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2951 CLIENT_POLL_MASK_IN
);
2957 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
2959 * We want to be notified whenever there is buffer space
2960 * available to send the rest of the payload.
2962 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2963 CLIENT_POLL_MASK_IN_OUT
);
2968 case CLIENT_TRANSMISSION_STATUS_FAIL
:
2969 ret
= notification_thread_client_disconnect(client
, state
);
2974 case CLIENT_TRANSMISSION_STATUS_ERROR
:
2984 /* Client lock must be acquired by caller. */
2986 enum client_transmission_status
client_flush_outgoing_queue(
2987 struct notification_client
*client
)
2990 size_t to_send_count
;
2991 enum client_transmission_status status
;
2992 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
2993 &client
->communication
.outbound
.payload
, 0, -1);
2994 const int fds_to_send_count
=
2995 lttng_payload_view_get_fd_handle_count(&pv
);
2997 ASSERT_LOCKED(client
->lock
);
2999 if (!client
->communication
.active
) {
3000 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3004 if (pv
.buffer
.size
== 0) {
3006 * If both data and fds are equal to zero, we are in an invalid
3009 assert(fds_to_send_count
!= 0);
3014 to_send_count
= pv
.buffer
.size
;
3015 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3018 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3021 if ((ret
>= 0 && ret
< to_send_count
)) {
3022 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3024 to_send_count
-= max(ret
, 0);
3026 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3028 pv
.buffer
.size
- to_send_count
,
3030 ret
= lttng_dynamic_buffer_set_size(
3031 &client
->communication
.outbound
.payload
.buffer
,
3037 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3039 } else if (ret
< 0) {
3040 /* Generic error, disable the client's communication. */
3041 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3043 client
->communication
.active
= false;
3044 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3048 * No error and flushed the queue completely.
3050 * The payload buffer size is used later to
3051 * check if there is notifications queued. So albeit that the
3052 * direct caller knows that the transmission is complete, we
3053 * need to set the buffer size to zero.
3055 ret
= lttng_dynamic_buffer_set_size(
3056 &client
->communication
.outbound
.payload
.buffer
, 0);
3063 /* No fds to send, transmission is complete. */
3064 if (fds_to_send_count
== 0) {
3065 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3069 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3070 client
->socket
, &pv
);
3072 /* Generic error, disable the client's communication. */
3073 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3075 client
->communication
.active
= false;
3076 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3078 } else if (ret
== 0) {
3079 /* Nothing could be sent. */
3080 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3082 /* Fd passing is an all or nothing kind of thing. */
3083 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3085 * The payload _fd_array count is used later to
3086 * check if there is notifications queued. So although the
3087 * direct caller knows that the transmission is complete, we
3088 * need to clear the _fd_array for the queuing check.
3090 lttng_dynamic_pointer_array_clear(
3091 &client
->communication
.outbound
.payload
3096 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3097 client
->communication
.outbound
.queued_command_reply
= false;
3098 client
->communication
.outbound
.dropped_notification
= false;
3099 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3104 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3108 bool client_has_outbound_data_left(
3109 const struct notification_client
*client
)
3111 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3112 &client
->communication
.outbound
.payload
, 0, -1);
3113 const bool has_data
= pv
.buffer
.size
!= 0;
3114 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3116 return has_data
|| has_fds
;
3119 /* Client lock must _not_ be held by the caller. */
3121 int client_send_command_reply(struct notification_client
*client
,
3122 struct notification_thread_state
*state
,
3123 enum lttng_notification_channel_status status
)
3126 struct lttng_notification_channel_command_reply reply
= {
3127 .status
= (int8_t) status
,
3129 struct lttng_notification_channel_message msg
= {
3130 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3131 .size
= sizeof(reply
),
3133 char buffer
[sizeof(msg
) + sizeof(reply
)];
3134 enum client_transmission_status transmission_status
;
3136 memcpy(buffer
, &msg
, sizeof(msg
));
3137 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3138 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3140 pthread_mutex_lock(&client
->lock
);
3141 if (client
->communication
.outbound
.queued_command_reply
) {
3142 /* Protocol error. */
3146 /* Enqueue buffer to outgoing queue and flush it. */
3147 ret
= lttng_dynamic_buffer_append(
3148 &client
->communication
.outbound
.payload
.buffer
,
3149 buffer
, sizeof(buffer
));
3154 transmission_status
= client_flush_outgoing_queue(client
);
3156 if (client_has_outbound_data_left(client
)) {
3157 /* Queue could not be emptied. */
3158 client
->communication
.outbound
.queued_command_reply
= true;
3161 pthread_mutex_unlock(&client
->lock
);
3162 ret
= client_handle_transmission_status(
3163 client
, transmission_status
, state
);
3170 pthread_mutex_unlock(&client
->lock
);
3176 int client_handle_message_unknown(struct notification_client
*client
,
3177 struct notification_thread_state
*state
)
3181 * Receiving message header. The function will be called again
3182 * once the rest of the message as been received and can be
3185 const struct lttng_notification_channel_message
*msg
;
3187 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3188 msg
= (const struct lttng_notification_channel_message
*)
3189 client
->communication
.inbound
.payload
.buffer
.data
;
3191 if (msg
->size
== 0 ||
3192 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3193 ERR("[notification-thread] Invalid notification channel message: length = %u",
3199 switch (msg
->type
) {
3200 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3201 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3202 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3206 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3210 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3211 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3212 client
->communication
.inbound
.msg_type
=
3213 (enum lttng_notification_channel_message_type
) msg
->type
;
3214 ret
= lttng_dynamic_buffer_set_size(
3215 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3217 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3224 int client_handle_message_handshake(struct notification_client
*client
,
3225 struct notification_thread_state
*state
)
3228 struct lttng_notification_channel_command_handshake
*handshake_client
;
3229 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3230 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3231 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3233 const struct lttng_notification_channel_message msg_header
= {
3234 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3235 .size
= sizeof(handshake_reply
),
3237 enum lttng_notification_channel_status status
=
3238 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3239 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3241 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3242 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3243 sizeof(handshake_reply
));
3246 (struct lttng_notification_channel_command_handshake
*)
3247 client
->communication
.inbound
.payload
.buffer
3249 client
->major
= handshake_client
->major
;
3250 client
->minor
= handshake_client
->minor
;
3251 if (!client
->communication
.inbound
.creds_received
) {
3252 ERR("[notification-thread] No credentials received from client");
3257 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3258 &client
->communication
.inbound
.creds
);
3259 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3260 &client
->communication
.inbound
.creds
);
3261 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3262 client
->uid
, client
->gid
, (int) client
->major
,
3263 (int) client
->minor
);
3265 if (handshake_client
->major
!=
3266 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3267 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3270 pthread_mutex_lock(&client
->lock
);
3271 /* Outgoing queue will be flushed when the command reply is sent. */
3272 ret
= lttng_dynamic_buffer_append(
3273 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3274 sizeof(send_buffer
));
3276 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3280 client
->validated
= true;
3281 client
->communication
.active
= true;
3282 pthread_mutex_unlock(&client
->lock
);
3284 /* Set reception state to receive the next message header. */
3285 ret
= client_reset_inbound_state(client
);
3287 ERR("[notification-thread] Failed to reset client communication's inbound state");
3291 /* Flushes the outgoing queue. */
3292 ret
= client_send_command_reply(client
, state
, status
);
3294 ERR("[notification-thread] Failed to send reply to notification channel client");
3300 pthread_mutex_unlock(&client
->lock
);
3306 int client_handle_message_subscription(
3307 struct notification_client
*client
,
3308 enum lttng_notification_channel_message_type msg_type
,
3309 struct notification_thread_state
*state
)
3312 struct lttng_condition
*condition
;
3313 enum lttng_notification_channel_status status
=
3314 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3315 struct lttng_payload_view condition_view
=
3316 lttng_payload_view_from_payload(
3317 &client
->communication
.inbound
.payload
,
3319 size_t expected_condition_size
;
3322 * No need to lock client to sample the inbound state as the only
3323 * other thread accessing clients (action executor) only uses the
3326 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3327 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3328 if (ret
!= expected_condition_size
) {
3329 ERR("[notification-thread] Malformed condition received from client");
3333 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3334 ret
= notification_thread_client_subscribe(
3335 client
, condition
, state
, &status
);
3337 ret
= notification_thread_client_unsubscribe(
3338 client
, condition
, state
, &status
);
3345 /* Set reception state to receive the next message header. */
3346 ret
= client_reset_inbound_state(client
);
3348 ERR("[notification-thread] Failed to reset client communication's inbound state");
3352 ret
= client_send_command_reply(client
, state
, status
);
3354 ERR("[notification-thread] Failed to send reply to notification channel client");
3363 int client_dispatch_message(struct notification_client
*client
,
3364 struct notification_thread_state
*state
)
3368 if (client
->communication
.inbound
.msg_type
!=
3369 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3370 client
->communication
.inbound
.msg_type
!=
3371 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3372 !client
->validated
) {
3373 WARN("[notification-thread] client attempted a command before handshake");
3378 switch (client
->communication
.inbound
.msg_type
) {
3379 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3381 ret
= client_handle_message_unknown(client
, state
);
3384 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3386 ret
= client_handle_message_handshake(client
, state
);
3389 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3390 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3392 ret
= client_handle_message_subscription(client
,
3393 client
->communication
.inbound
.msg_type
, state
);
3403 /* Incoming data from client. */
3404 int handle_notification_thread_client_in(
3405 struct notification_thread_state
*state
, int socket
)
3408 struct notification_client
*client
;
3413 client
= get_client_from_socket(socket
, state
);
3415 /* Internal error, abort. */
3420 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3421 client
->communication
.inbound
.bytes_to_receive
;
3422 if (client
->communication
.inbound
.expect_creds
) {
3423 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3424 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3425 client
->communication
.inbound
.bytes_to_receive
,
3426 &client
->communication
.inbound
.creds
);
3428 client
->communication
.inbound
.expect_creds
= false;
3429 client
->communication
.inbound
.creds_received
= true;
3432 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3433 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3434 client
->communication
.inbound
.bytes_to_receive
);
3436 if (recv_ret
>= 0) {
3437 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3439 goto error_disconnect_client
;
3442 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3443 /* Message incomplete wait for more data. */
3448 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3451 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3452 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3454 client
->communication
.inbound
.fds_to_receive
,
3455 &client
->communication
.inbound
.payload
);
3458 * Fds received. non blocking fds passing is all
3461 ssize_t expected_size
;
3463 expected_size
= sizeof(int) *
3464 client
->communication
.inbound
3466 assert(ret
== expected_size
);
3467 client
->communication
.inbound
.fds_to_receive
= 0;
3468 } else if (ret
== 0) {
3469 /* Received nothing. */
3473 goto error_disconnect_client
;
3477 /* At this point the message is complete.*/
3478 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3479 client
->communication
.inbound
.fds_to_receive
== 0);
3480 ret
= client_dispatch_message(client
, state
);
3483 * Only returns an error if this client must be
3486 goto error_disconnect_client
;
3493 error_disconnect_client
:
3494 ret
= notification_thread_client_disconnect(client
, state
);
3498 /* Client ready to receive outgoing data. */
3499 int handle_notification_thread_client_out(
3500 struct notification_thread_state
*state
, int socket
)
3503 struct notification_client
*client
;
3504 enum client_transmission_status transmission_status
;
3507 client
= get_client_from_socket(socket
, state
);
3509 /* Internal error, abort. */
3514 pthread_mutex_lock(&client
->lock
);
3515 transmission_status
= client_flush_outgoing_queue(client
);
3516 pthread_mutex_unlock(&client
->lock
);
3518 ret
= client_handle_transmission_status(
3519 client
, transmission_status
, state
);
3529 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3530 const struct channel_state_sample
*sample
,
3531 uint64_t buffer_capacity
)
3533 bool result
= false;
3535 enum lttng_condition_type condition_type
;
3536 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3537 condition
, struct lttng_condition_buffer_usage
,
3540 if (use_condition
->threshold_bytes
.set
) {
3541 threshold
= use_condition
->threshold_bytes
.value
;
3544 * Threshold was expressed as a ratio.
3546 * TODO the threshold (in bytes) of conditions expressed
3547 * as a ratio of total buffer size could be cached to
3548 * forego this double-multiplication or it could be performed
3549 * as fixed-point math.
3551 * Note that caching should accommodates the case where the
3552 * condition applies to multiple channels (i.e. don't assume
3553 * that all channels matching my_chann* have the same size...)
3555 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3556 (double) buffer_capacity
);
3559 condition_type
= lttng_condition_get_type(condition
);
3560 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3561 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3562 threshold
, sample
->highest_usage
);
3565 * The low condition should only be triggered once _all_ of the
3566 * streams in a channel have gone below the "low" threshold.
3568 if (sample
->highest_usage
<= threshold
) {
3572 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3573 threshold
, sample
->highest_usage
);
3576 * For high buffer usage scenarios, we want to trigger whenever
3577 * _any_ of the streams has reached the "high" threshold.
3579 if (sample
->highest_usage
>= threshold
) {
3588 bool evaluate_session_consumed_size_condition(
3589 const struct lttng_condition
*condition
,
3590 uint64_t session_consumed_size
)
3593 const struct lttng_condition_session_consumed_size
*size_condition
=
3594 container_of(condition
,
3595 struct lttng_condition_session_consumed_size
,
3598 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3599 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3600 threshold
, session_consumed_size
);
3601 return session_consumed_size
>= threshold
;
3605 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3606 struct lttng_evaluation
**evaluation
,
3607 const struct notification_thread_state
*state
,
3608 const struct channel_state_sample
*previous_sample
,
3609 const struct channel_state_sample
*latest_sample
,
3610 uint64_t previous_session_consumed_total
,
3611 uint64_t latest_session_consumed_total
,
3612 struct channel_info
*channel_info
)
3615 enum lttng_condition_type condition_type
;
3616 const bool previous_sample_available
= !!previous_sample
;
3617 bool previous_sample_result
= false;
3618 bool latest_sample_result
;
3620 condition_type
= lttng_condition_get_type(condition
);
3622 switch (condition_type
) {
3623 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3624 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3625 if (caa_likely(previous_sample_available
)) {
3626 previous_sample_result
=
3627 evaluate_buffer_usage_condition(condition
,
3628 previous_sample
, channel_info
->capacity
);
3630 latest_sample_result
= evaluate_buffer_usage_condition(
3631 condition
, latest_sample
,
3632 channel_info
->capacity
);
3634 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3635 if (caa_likely(previous_sample_available
)) {
3636 previous_sample_result
=
3637 evaluate_session_consumed_size_condition(
3639 previous_session_consumed_total
);
3641 latest_sample_result
=
3642 evaluate_session_consumed_size_condition(
3644 latest_session_consumed_total
);
3647 /* Unknown condition type; internal error. */
3651 if (!latest_sample_result
||
3652 (previous_sample_result
== latest_sample_result
)) {
3654 * Only trigger on a condition evaluation transition.
3656 * NOTE: This edge-triggered logic may not be appropriate for
3657 * future condition types.
3662 if (!evaluation
|| !latest_sample_result
) {
3666 switch (condition_type
) {
3667 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3668 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3669 *evaluation
= lttng_evaluation_buffer_usage_create(
3671 latest_sample
->highest_usage
,
3672 channel_info
->capacity
);
3674 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3675 *evaluation
= lttng_evaluation_session_consumed_size_create(
3676 latest_session_consumed_total
);
3691 int client_notification_overflow(struct notification_client
*client
)
3694 const struct lttng_notification_channel_message msg
= {
3695 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3698 ASSERT_LOCKED(client
->lock
);
3700 DBG("Dropping notification addressed to client (socket fd = %i)",
3702 if (client
->communication
.outbound
.dropped_notification
) {
3704 * The client already has a "notification dropped" message
3705 * in its outgoing queue. Nothing to do since all
3706 * of those messages are coalesced.
3711 client
->communication
.outbound
.dropped_notification
= true;
3712 ret
= lttng_dynamic_buffer_append(
3713 &client
->communication
.outbound
.payload
.buffer
, &msg
,
3716 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
3723 static int client_handle_transmission_status_wrapper(
3724 struct notification_client
*client
,
3725 enum client_transmission_status status
,
3728 return client_handle_transmission_status(client
, status
,
3729 (struct notification_thread_state
*) user_data
);
3733 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3734 const struct lttng_evaluation
*evaluation
,
3735 struct notification_client_list
* client_list
,
3736 struct notification_thread_state
*state
,
3737 uid_t object_uid
, gid_t object_gid
)
3739 const struct lttng_credentials creds
= {
3740 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
3741 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
3744 return notification_client_list_send_evaluation(client_list
,
3745 lttng_trigger_get_const_condition(trigger
), evaluation
,
3746 lttng_trigger_get_credentials(trigger
),
3748 client_handle_transmission_status_wrapper
, state
);
3752 * Permission checks relative to notification channel clients are performed
3753 * here. Notice how object, client, and trigger credentials are involved in
3756 * The `object` credentials are the credentials associated with the "subject"
3757 * of a condition. For instance, a `rotation completed` condition applies
3758 * to a session. When that condition is met, it will produce an evaluation
3759 * against a session. Hence, in this case, the `object` credentials are the
3760 * credentials of the "subject" session.
3762 * The `trigger` credentials are the credentials of the user that registered the
3765 * The `client` credentials are the credentials of the user that created a given
3766 * notification channel.
3768 * In terms of visibility, it is expected that non-privilieged users can only
3769 * register triggers against "their" objects (their own sessions and
3770 * applications they are allowed to interact with). They can then open a
3771 * notification channel and subscribe to notifications associated with those
3774 * As for privilieged users, they can register triggers against the objects of
3775 * other users. They can then subscribe to the notifications associated to their
3776 * triggers. Privilieged users _can't_ subscribe to the notifications of
3777 * triggers owned by other users; they must create their own triggers.
3779 * This is more a concern of usability than security. It would be difficult for
3780 * a root user reliably subscribe to a specific set of conditions without
3781 * interference from external users (those could, for instance, unregister
3785 int notification_client_list_send_evaluation(
3786 struct notification_client_list
*client_list
,
3787 const struct lttng_condition
*condition
,
3788 const struct lttng_evaluation
*evaluation
,
3789 const struct lttng_credentials
*trigger_creds
,
3790 const struct lttng_credentials
*source_object_creds
,
3791 report_client_transmission_result_cb client_report
,
3795 struct lttng_payload msg_payload
;
3796 struct notification_client_list_element
*client_list_element
, *tmp
;
3797 const struct lttng_notification notification
= {
3798 .condition
= (struct lttng_condition
*) condition
,
3799 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3801 struct lttng_notification_channel_message msg_header
= {
3802 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3805 lttng_payload_init(&msg_payload
);
3807 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
3808 sizeof(msg_header
));
3813 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
3815 ERR("[notification-thread] Failed to serialize notification");
3820 /* Update payload size. */
3821 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
3822 ->size
= (uint32_t)(
3823 msg_payload
.buffer
.size
- sizeof(msg_header
));
3825 /* Update the payload number of fds. */
3827 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3828 &msg_payload
, 0, -1);
3830 ((struct lttng_notification_channel_message
*)
3831 msg_payload
.buffer
.data
)->fds
= (uint32_t)
3832 lttng_payload_view_get_fd_handle_count(&pv
);
3835 pthread_mutex_lock(&client_list
->lock
);
3836 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3837 &client_list
->list
, node
) {
3838 enum client_transmission_status transmission_status
;
3839 struct notification_client
*client
=
3840 client_list_element
->client
;
3843 pthread_mutex_lock(&client
->lock
);
3844 if (!client
->communication
.active
) {
3846 * Skip inactive client (protocol error or
3849 DBG("Skipping client at it is marked as inactive");
3853 if (source_object_creds
) {
3854 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
3855 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
3858 * Client is not allowed to monitor this
3861 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
3866 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
3867 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
3871 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3872 client
->socket
, msg_payload
.buffer
.size
);
3874 if (client_has_outbound_data_left(client
)) {
3876 * Outgoing data is already buffered for this client;
3877 * drop the notification and enqueue a "dropped
3878 * notification" message if this is the first dropped
3879 * notification since the socket spilled-over to the
3882 ret
= client_notification_overflow(client
);
3889 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
3895 transmission_status
= client_flush_outgoing_queue(client
);
3896 pthread_mutex_unlock(&client
->lock
);
3897 ret
= client_report(client
, transmission_status
, user_data
);
3900 goto end_unlock_list
;
3906 pthread_mutex_unlock(&client
->lock
);
3909 goto end_unlock_list
;
3915 pthread_mutex_unlock(&client_list
->lock
);
3917 lttng_payload_reset(&msg_payload
);
3921 int handle_notification_thread_channel_sample(
3922 struct notification_thread_state
*state
, int pipe
,
3923 enum lttng_domain_type domain
)
3926 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
3927 struct channel_info
*channel_info
;
3928 struct cds_lfht_node
*node
;
3929 struct cds_lfht_iter iter
;
3930 struct lttng_channel_trigger_list
*trigger_list
;
3931 struct lttng_trigger_list_element
*trigger_list_element
;
3932 bool previous_sample_available
= false;
3933 struct channel_state_sample previous_sample
, latest_sample
;
3934 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
3935 struct lttng_credentials channel_creds
;
3938 * The monitoring pipe only holds messages smaller than PIPE_BUF,
3939 * ensuring that read/write of sampling messages are atomic.
3941 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
3942 if (ret
!= sizeof(sample_msg
)) {
3943 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
3950 latest_sample
.key
.key
= sample_msg
.key
;
3951 latest_sample
.key
.domain
= domain
;
3952 latest_sample
.highest_usage
= sample_msg
.highest
;
3953 latest_sample
.lowest_usage
= sample_msg
.lowest
;
3954 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
3958 /* Retrieve the channel's informations */
3959 cds_lfht_lookup(state
->channels_ht
,
3960 hash_channel_key(&latest_sample
.key
),
3964 node
= cds_lfht_iter_get_node(&iter
);
3965 if (caa_unlikely(!node
)) {
3967 * Not an error since the consumer can push a sample to the pipe
3968 * and the rest of the session daemon could notify us of the
3969 * channel's destruction before we get a chance to process that
3972 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
3973 latest_sample
.key
.key
,
3974 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
3978 channel_info
= caa_container_of(node
, struct channel_info
,
3980 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
3982 latest_sample
.key
.key
,
3983 channel_info
->session_info
->name
,
3984 latest_sample
.highest_usage
,
3985 latest_sample
.lowest_usage
,
3986 latest_sample
.channel_total_consumed
);
3988 previous_session_consumed_total
=
3989 channel_info
->session_info
->consumed_data_size
;
3991 /* Retrieve the channel's last sample, if it exists, and update it. */
3992 cds_lfht_lookup(state
->channel_state_ht
,
3993 hash_channel_key(&latest_sample
.key
),
3994 match_channel_state_sample
,
3997 node
= cds_lfht_iter_get_node(&iter
);
3998 if (caa_likely(node
)) {
3999 struct channel_state_sample
*stored_sample
;
4001 /* Update the sample stored. */
4002 stored_sample
= caa_container_of(node
,
4003 struct channel_state_sample
,
4004 channel_state_ht_node
);
4006 memcpy(&previous_sample
, stored_sample
,
4007 sizeof(previous_sample
));
4008 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4009 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4010 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4011 previous_sample_available
= true;
4013 latest_session_consumed_total
=
4014 previous_session_consumed_total
+
4015 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4018 * This is the channel's first sample, allocate space for and
4019 * store the new sample.
4021 struct channel_state_sample
*stored_sample
;
4023 stored_sample
= zmalloc(sizeof(*stored_sample
));
4024 if (!stored_sample
) {
4029 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4030 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4031 cds_lfht_add(state
->channel_state_ht
,
4032 hash_channel_key(&stored_sample
->key
),
4033 &stored_sample
->channel_state_ht_node
);
4035 latest_session_consumed_total
=
4036 previous_session_consumed_total
+
4037 latest_sample
.channel_total_consumed
;
4040 channel_info
->session_info
->consumed_data_size
=
4041 latest_session_consumed_total
;
4043 /* Find triggers associated with this channel. */
4044 cds_lfht_lookup(state
->channel_triggers_ht
,
4045 hash_channel_key(&latest_sample
.key
),
4046 match_channel_trigger_list
,
4049 node
= cds_lfht_iter_get_node(&iter
);
4050 if (caa_likely(!node
)) {
4054 channel_creds
= (typeof(channel_creds
)) {
4055 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4056 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4059 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4060 channel_triggers_ht_node
);
4061 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4063 const struct lttng_condition
*condition
;
4064 struct lttng_trigger
*trigger
;
4065 struct notification_client_list
*client_list
= NULL
;
4066 struct lttng_evaluation
*evaluation
= NULL
;
4067 enum action_executor_status executor_status
;
4070 trigger
= trigger_list_element
->trigger
;
4071 condition
= lttng_trigger_get_const_condition(trigger
);
4075 * Check if any client is subscribed to the result of this
4078 client_list
= get_client_list_from_condition(state
, condition
);
4080 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4081 previous_sample_available
? &previous_sample
: NULL
,
4083 previous_session_consumed_total
,
4084 latest_session_consumed_total
,
4086 if (caa_unlikely(ret
)) {
4090 if (caa_likely(!evaluation
)) {
4094 if (!lttng_trigger_should_fire(trigger
)) {
4098 lttng_trigger_fire(trigger
);
4101 * Ownership of `evaluation` transferred to the action executor
4102 * no matter the result.
4104 executor_status
= action_executor_enqueue(state
->executor
,
4105 trigger
, evaluation
, &channel_creds
,
4108 switch (executor_status
) {
4109 case ACTION_EXECUTOR_STATUS_OK
:
4111 case ACTION_EXECUTOR_STATUS_ERROR
:
4112 case ACTION_EXECUTOR_STATUS_INVALID
:
4114 * TODO Add trigger identification (name/id) when
4115 * it is added to the API.
4117 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4120 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4122 * TODO Add trigger identification (name/id) when
4123 * it is added to the API.
4125 * Not a fatal error.
4127 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4135 notification_client_list_put(client_list
);
4136 if (caa_unlikely(ret
)) {