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/action/group-internal.h>
25 #include <lttng/domain-internal.h>
26 #include <lttng/notification/notification-internal.h>
27 #include <lttng/condition/condition-internal.h>
28 #include <lttng/condition/buffer-usage-internal.h>
29 #include <lttng/condition/session-consumed-size-internal.h>
30 #include <lttng/condition/session-rotation-internal.h>
31 #include <lttng/condition/event-rule-internal.h>
32 #include <lttng/domain-internal.h>
33 #include <lttng/notification/channel-internal.h>
34 #include <lttng/trigger/trigger-internal.h>
42 #include "condition-internal.h"
43 #include "notification-thread.h"
44 #include "notification-thread-events.h"
45 #include "notification-thread-commands.h"
46 #include "lttng-sessiond.h"
48 #include "trigger-error-accounting.h"
50 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
51 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
53 enum lttng_object_type
{
54 LTTNG_OBJECT_TYPE_UNKNOWN
,
55 LTTNG_OBJECT_TYPE_NONE
,
56 LTTNG_OBJECT_TYPE_CHANNEL
,
57 LTTNG_OBJECT_TYPE_SESSION
,
60 struct lttng_trigger_list_element
{
61 /* No ownership of the trigger object is assumed. */
62 struct lttng_trigger
*trigger
;
63 struct cds_list_head node
;
66 struct lttng_channel_trigger_list
{
67 struct channel_key channel_key
;
68 /* List of struct lttng_trigger_list_element. */
69 struct cds_list_head list
;
70 /* Node in the channel_triggers_ht */
71 struct cds_lfht_node channel_triggers_ht_node
;
72 /* call_rcu delayed reclaim. */
73 struct rcu_head rcu_node
;
77 * List of triggers applying to a given session.
80 * - lttng_session_trigger_list_create()
81 * - lttng_session_trigger_list_build()
82 * - lttng_session_trigger_list_destroy()
83 * - lttng_session_trigger_list_add()
85 struct lttng_session_trigger_list
{
87 * Not owned by this; points to the session_info structure's
90 const char *session_name
;
91 /* List of struct lttng_trigger_list_element. */
92 struct cds_list_head list
;
93 /* Node in the session_triggers_ht */
94 struct cds_lfht_node session_triggers_ht_node
;
96 * Weak reference to the notification system's session triggers
99 * The session trigger list structure structure is owned by
100 * the session's session_info.
102 * The session_info is kept alive the the channel_infos holding a
103 * reference to it (reference counting). When those channels are
104 * destroyed (at runtime or on teardown), the reference they hold
105 * to the session_info are released. On destruction of session_info,
106 * session_info_destroy() will remove the list of triggers applying
107 * to this session from the notification system's state.
109 * This implies that the session_triggers_ht must be destroyed
110 * after the channels.
112 struct cds_lfht
*session_triggers_ht
;
113 /* Used for delayed RCU reclaim. */
114 struct rcu_head rcu_node
;
117 struct lttng_trigger_ht_element
{
118 struct lttng_trigger
*trigger
;
119 struct cds_lfht_node node
;
120 struct cds_lfht_node node_by_name_uid
;
121 /* call_rcu delayed reclaim. */
122 struct rcu_head rcu_node
;
125 struct lttng_condition_list_element
{
126 struct lttng_condition
*condition
;
127 struct cds_list_head node
;
130 struct channel_state_sample
{
131 struct channel_key key
;
132 struct cds_lfht_node channel_state_ht_node
;
133 uint64_t highest_usage
;
134 uint64_t lowest_usage
;
135 uint64_t channel_total_consumed
;
136 /* call_rcu delayed reclaim. */
137 struct rcu_head rcu_node
;
140 static unsigned long hash_channel_key(struct channel_key
*key
);
141 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
142 struct lttng_evaluation
**evaluation
,
143 const struct notification_thread_state
*state
,
144 const struct channel_state_sample
*previous_sample
,
145 const struct channel_state_sample
*latest_sample
,
146 uint64_t previous_session_consumed_total
,
147 uint64_t latest_session_consumed_total
,
148 struct channel_info
*channel_info
);
150 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
151 const struct lttng_evaluation
*evaluation
,
152 struct notification_client_list
*client_list
,
153 struct notification_thread_state
*state
,
154 uid_t channel_uid
, gid_t channel_gid
);
157 /* session_info API */
159 void session_info_destroy(void *_data
);
161 void session_info_get(struct session_info
*session_info
);
163 void session_info_put(struct session_info
*session_info
);
165 struct session_info
*session_info_create(const char *name
,
166 uid_t uid
, gid_t gid
,
167 struct lttng_session_trigger_list
*trigger_list
,
168 struct cds_lfht
*sessions_ht
);
170 void session_info_add_channel(struct session_info
*session_info
,
171 struct channel_info
*channel_info
);
173 void session_info_remove_channel(struct session_info
*session_info
,
174 struct channel_info
*channel_info
);
176 /* lttng_session_trigger_list API */
178 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
179 const char *session_name
,
180 struct cds_lfht
*session_triggers_ht
);
182 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
183 const struct notification_thread_state
*state
,
184 const char *session_name
);
186 void lttng_session_trigger_list_destroy(
187 struct lttng_session_trigger_list
*list
);
189 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
190 struct lttng_trigger
*trigger
);
193 int client_handle_transmission_status(
194 struct notification_client
*client
,
195 enum client_transmission_status transmission_status
,
196 struct notification_thread_state
*state
);
199 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
202 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
204 /* This double-cast is intended to supress pointer-to-cast warning. */
205 const int socket
= (int) (intptr_t) key
;
206 const struct notification_client
*client
= caa_container_of(node
,
207 struct notification_client
, client_socket_ht_node
);
209 return client
->socket
== socket
;
213 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
215 /* This double-cast is intended to supress pointer-to-cast warning. */
216 const notification_client_id id
= *((notification_client_id
*) key
);
217 const struct notification_client
*client
= caa_container_of(
218 node
, struct notification_client
, client_id_ht_node
);
220 return client
->id
== id
;
224 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
226 struct channel_key
*channel_key
= (struct channel_key
*) key
;
227 struct lttng_channel_trigger_list
*trigger_list
;
229 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
230 channel_triggers_ht_node
);
232 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
233 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
237 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
239 const char *session_name
= (const char *) key
;
240 struct lttng_session_trigger_list
*trigger_list
;
242 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
243 session_triggers_ht_node
);
245 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
249 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
251 struct channel_key
*channel_key
= (struct channel_key
*) key
;
252 struct channel_state_sample
*sample
;
254 sample
= caa_container_of(node
, struct channel_state_sample
,
255 channel_state_ht_node
);
257 return !!((channel_key
->key
== sample
->key
.key
) &&
258 (channel_key
->domain
== sample
->key
.domain
));
262 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
264 struct channel_key
*channel_key
= (struct channel_key
*) key
;
265 struct channel_info
*channel_info
;
267 channel_info
= caa_container_of(node
, struct channel_info
,
270 return !!((channel_key
->key
== channel_info
->key
.key
) &&
271 (channel_key
->domain
== channel_info
->key
.domain
));
275 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
277 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
278 struct lttng_trigger_ht_element
*trigger_ht_element
;
280 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
283 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
287 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
289 const uint64_t *_key
= key
;
290 struct notification_trigger_tokens_ht_element
*element
;
292 element
= caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
294 return *_key
== element
->token
;
298 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
300 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
301 struct notification_client_list
*client_list
;
302 const struct lttng_condition
*condition
;
304 assert(condition_key
);
306 client_list
= caa_container_of(node
, struct notification_client_list
,
307 notification_trigger_clients_ht_node
);
308 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
310 return !!lttng_condition_is_equal(condition_key
, condition
);
314 int match_session(struct cds_lfht_node
*node
, const void *key
)
316 const char *name
= key
;
317 struct session_info
*session_info
= caa_container_of(
318 node
, struct session_info
, sessions_ht_node
);
320 return !strcmp(session_info
->name
, name
);
324 * Match trigger based on name and credentials only.
325 * Name duplication is NOT allowed for the same uid.
327 static int match_name_uid(struct cds_lfht_node
*node
, const void *key
)
330 struct lttng_trigger_ht_element
*trigger_ht_element
;
332 const char *key_name
;
333 enum lttng_trigger_status status
;
334 const struct lttng_credentials
*key_creds
;
335 const struct lttng_credentials
*node_creds
;
336 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
338 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
341 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
342 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
344 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
345 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
347 if (strcmp(name
, key_name
) != 0) {
352 key_creds
= lttng_trigger_get_credentials(trigger_key
);
353 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
355 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
362 unsigned long hash_channel_key(struct channel_key
*key
)
364 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
365 unsigned long domain_hash
= hash_key_ulong(
366 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
368 return key_hash
^ domain_hash
;
372 unsigned long hash_client_socket(int socket
)
374 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
378 unsigned long hash_client_id(notification_client_id id
)
380 return hash_key_u64(&id
, lttng_ht_seed
);
384 * Get the type of object to which a given condition applies. Bindings let
385 * the notification system evaluate a trigger's condition when a given
386 * object's state is updated.
388 * For instance, a condition bound to a channel will be evaluated everytime
389 * the channel's state is changed by a channel monitoring sample.
392 enum lttng_object_type
get_condition_binding_object(
393 const struct lttng_condition
*condition
)
395 switch (lttng_condition_get_type(condition
)) {
396 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
397 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
398 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
399 return LTTNG_OBJECT_TYPE_CHANNEL
;
400 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
401 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
402 return LTTNG_OBJECT_TYPE_SESSION
;
403 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
404 return LTTNG_OBJECT_TYPE_NONE
;
406 return LTTNG_OBJECT_TYPE_UNKNOWN
;
411 void free_channel_info_rcu(struct rcu_head
*node
)
413 free(caa_container_of(node
, struct channel_info
, rcu_node
));
417 void channel_info_destroy(struct channel_info
*channel_info
)
423 if (channel_info
->session_info
) {
424 session_info_remove_channel(channel_info
->session_info
,
426 session_info_put(channel_info
->session_info
);
428 if (channel_info
->name
) {
429 free(channel_info
->name
);
431 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
435 void free_session_info_rcu(struct rcu_head
*node
)
437 free(caa_container_of(node
, struct session_info
, rcu_node
));
440 /* Don't call directly, use the ref-counting mechanism. */
442 void session_info_destroy(void *_data
)
444 struct session_info
*session_info
= _data
;
447 assert(session_info
);
448 if (session_info
->channel_infos_ht
) {
449 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
451 ERR("[notification-thread] Failed to destroy channel information hash table");
454 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
457 cds_lfht_del(session_info
->sessions_ht
,
458 &session_info
->sessions_ht_node
);
460 free(session_info
->name
);
461 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
465 void session_info_get(struct session_info
*session_info
)
470 lttng_ref_get(&session_info
->ref
);
474 void session_info_put(struct session_info
*session_info
)
479 lttng_ref_put(&session_info
->ref
);
483 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
484 struct lttng_session_trigger_list
*trigger_list
,
485 struct cds_lfht
*sessions_ht
)
487 struct session_info
*session_info
;
491 session_info
= zmalloc(sizeof(*session_info
));
495 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
497 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
498 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
499 if (!session_info
->channel_infos_ht
) {
503 cds_lfht_node_init(&session_info
->sessions_ht_node
);
504 session_info
->name
= strdup(name
);
505 if (!session_info
->name
) {
508 session_info
->uid
= uid
;
509 session_info
->gid
= gid
;
510 session_info
->trigger_list
= trigger_list
;
511 session_info
->sessions_ht
= sessions_ht
;
515 session_info_put(session_info
);
520 void session_info_add_channel(struct session_info
*session_info
,
521 struct channel_info
*channel_info
)
524 cds_lfht_add(session_info
->channel_infos_ht
,
525 hash_channel_key(&channel_info
->key
),
526 &channel_info
->session_info_channels_ht_node
);
531 void session_info_remove_channel(struct session_info
*session_info
,
532 struct channel_info
*channel_info
)
535 cds_lfht_del(session_info
->channel_infos_ht
,
536 &channel_info
->session_info_channels_ht_node
);
541 struct channel_info
*channel_info_create(const char *channel_name
,
542 struct channel_key
*channel_key
, uint64_t channel_capacity
,
543 struct session_info
*session_info
)
545 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
551 cds_lfht_node_init(&channel_info
->channels_ht_node
);
552 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
553 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
554 channel_info
->capacity
= channel_capacity
;
556 channel_info
->name
= strdup(channel_name
);
557 if (!channel_info
->name
) {
562 * Set the references between session and channel infos:
563 * - channel_info holds a strong reference to session_info
564 * - session_info holds a weak reference to channel_info
566 session_info_get(session_info
);
567 session_info_add_channel(session_info
, channel_info
);
568 channel_info
->session_info
= session_info
;
572 channel_info_destroy(channel_info
);
577 bool notification_client_list_get(struct notification_client_list
*list
)
579 return urcu_ref_get_unless_zero(&list
->ref
);
583 void free_notification_client_list_rcu(struct rcu_head
*node
)
585 free(caa_container_of(node
, struct notification_client_list
,
590 void notification_client_list_release(struct urcu_ref
*list_ref
)
592 struct notification_client_list
*list
=
593 container_of(list_ref
, typeof(*list
), ref
);
594 struct notification_client_list_element
*client_list_element
, *tmp
;
596 if (list
->notification_trigger_clients_ht
) {
598 cds_lfht_del(list
->notification_trigger_clients_ht
,
599 &list
->notification_trigger_clients_ht_node
);
601 list
->notification_trigger_clients_ht
= NULL
;
603 cds_list_for_each_entry_safe(client_list_element
, tmp
,
605 free(client_list_element
);
607 pthread_mutex_destroy(&list
->lock
);
608 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
612 struct notification_client_list
*notification_client_list_create(
613 const struct lttng_trigger
*trigger
)
615 struct notification_client_list
*client_list
=
616 zmalloc(sizeof(*client_list
));
621 pthread_mutex_init(&client_list
->lock
, NULL
);
622 urcu_ref_init(&client_list
->ref
);
623 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
624 CDS_INIT_LIST_HEAD(&client_list
->list
);
625 client_list
->trigger
= trigger
;
631 void publish_notification_client_list(
632 struct notification_thread_state
*state
,
633 struct notification_client_list
*list
)
635 const struct lttng_condition
*condition
=
636 lttng_trigger_get_const_condition(list
->trigger
);
638 assert(!list
->notification_trigger_clients_ht
);
639 notification_client_list_get(list
);
641 list
->notification_trigger_clients_ht
=
642 state
->notification_trigger_clients_ht
;
645 cds_lfht_add(state
->notification_trigger_clients_ht
,
646 lttng_condition_hash(condition
),
647 &list
->notification_trigger_clients_ht_node
);
652 void notification_client_list_put(struct notification_client_list
*list
)
657 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
660 /* Provides a reference to the returned list. */
662 struct notification_client_list
*get_client_list_from_condition(
663 struct notification_thread_state
*state
,
664 const struct lttng_condition
*condition
)
666 struct cds_lfht_node
*node
;
667 struct cds_lfht_iter iter
;
668 struct notification_client_list
*list
= NULL
;
671 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
672 lttng_condition_hash(condition
),
673 match_client_list_condition
,
676 node
= cds_lfht_iter_get_node(&iter
);
678 list
= container_of(node
, struct notification_client_list
,
679 notification_trigger_clients_ht_node
);
680 list
= notification_client_list_get(list
) ? list
: NULL
;
688 int evaluate_channel_condition_for_client(
689 const struct lttng_condition
*condition
,
690 struct notification_thread_state
*state
,
691 struct lttng_evaluation
**evaluation
,
692 uid_t
*session_uid
, gid_t
*session_gid
)
695 struct cds_lfht_iter iter
;
696 struct cds_lfht_node
*node
;
697 struct channel_info
*channel_info
= NULL
;
698 struct channel_key
*channel_key
= NULL
;
699 struct channel_state_sample
*last_sample
= NULL
;
700 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
704 /* Find the channel associated with the condition. */
705 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
706 channel_trigger_list
, channel_triggers_ht_node
) {
707 struct lttng_trigger_list_element
*element
;
709 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
710 const struct lttng_condition
*current_condition
=
711 lttng_trigger_get_const_condition(
714 assert(current_condition
);
715 if (!lttng_condition_is_equal(condition
,
716 current_condition
)) {
720 /* Found the trigger, save the channel key. */
721 channel_key
= &channel_trigger_list
->channel_key
;
725 /* The channel key was found stop iteration. */
731 /* No channel found; normal exit. */
732 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
737 /* Fetch channel info for the matching channel. */
738 cds_lfht_lookup(state
->channels_ht
,
739 hash_channel_key(channel_key
),
743 node
= cds_lfht_iter_get_node(&iter
);
745 channel_info
= caa_container_of(node
, struct channel_info
,
748 /* Retrieve the channel's last sample, if it exists. */
749 cds_lfht_lookup(state
->channel_state_ht
,
750 hash_channel_key(channel_key
),
751 match_channel_state_sample
,
754 node
= cds_lfht_iter_get_node(&iter
);
756 last_sample
= caa_container_of(node
,
757 struct channel_state_sample
,
758 channel_state_ht_node
);
760 /* Nothing to evaluate, no sample was ever taken. Normal exit */
761 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
766 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
768 0, channel_info
->session_info
->consumed_data_size
,
771 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
775 *session_uid
= channel_info
->session_info
->uid
;
776 *session_gid
= channel_info
->session_info
->gid
;
783 const char *get_condition_session_name(const struct lttng_condition
*condition
)
785 const char *session_name
= NULL
;
786 enum lttng_condition_status status
;
788 switch (lttng_condition_get_type(condition
)) {
789 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
790 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
791 status
= lttng_condition_buffer_usage_get_session_name(
792 condition
, &session_name
);
794 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
795 status
= lttng_condition_session_consumed_size_get_session_name(
796 condition
, &session_name
);
798 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
799 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
800 status
= lttng_condition_session_rotation_get_session_name(
801 condition
, &session_name
);
806 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
807 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
815 int evaluate_session_condition_for_client(
816 const struct lttng_condition
*condition
,
817 struct notification_thread_state
*state
,
818 struct lttng_evaluation
**evaluation
,
819 uid_t
*session_uid
, gid_t
*session_gid
)
822 struct cds_lfht_iter iter
;
823 struct cds_lfht_node
*node
;
824 const char *session_name
;
825 struct session_info
*session_info
= NULL
;
828 session_name
= get_condition_session_name(condition
);
830 /* Find the session associated with the trigger. */
831 cds_lfht_lookup(state
->sessions_ht
,
832 hash_key_str(session_name
, lttng_ht_seed
),
836 node
= cds_lfht_iter_get_node(&iter
);
838 DBG("[notification-thread] No known session matching name \"%s\"",
844 session_info
= caa_container_of(node
, struct session_info
,
846 session_info_get(session_info
);
849 * Evaluation is performed in-line here since only one type of
850 * session-bound condition is handled for the moment.
852 switch (lttng_condition_get_type(condition
)) {
853 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
854 if (!session_info
->rotation
.ongoing
) {
856 goto end_session_put
;
859 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
860 session_info
->rotation
.id
);
863 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
866 goto end_session_put
;
872 goto end_session_put
;
875 *session_uid
= session_info
->uid
;
876 *session_gid
= session_info
->gid
;
879 session_info_put(session_info
);
886 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
887 const struct lttng_condition
*condition
,
888 struct notification_client
*client
,
889 struct notification_thread_state
*state
)
892 struct lttng_evaluation
*evaluation
= NULL
;
893 struct notification_client_list client_list
= {
894 .lock
= PTHREAD_MUTEX_INITIALIZER
,
896 struct notification_client_list_element client_list_element
= { 0 };
897 uid_t object_uid
= 0;
898 gid_t object_gid
= 0;
905 switch (get_condition_binding_object(condition
)) {
906 case LTTNG_OBJECT_TYPE_SESSION
:
907 ret
= evaluate_session_condition_for_client(condition
, state
,
908 &evaluation
, &object_uid
, &object_gid
);
910 case LTTNG_OBJECT_TYPE_CHANNEL
:
911 ret
= evaluate_channel_condition_for_client(condition
, state
,
912 &evaluation
, &object_uid
, &object_gid
);
914 case LTTNG_OBJECT_TYPE_NONE
:
915 DBG("[notification-thread] Newly subscribed-to condition not binded to object, nothing to evaluate");
918 case LTTNG_OBJECT_TYPE_UNKNOWN
:
928 /* Evaluation yielded nothing. Normal exit. */
929 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
935 * Create a temporary client list with the client currently
938 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
939 CDS_INIT_LIST_HEAD(&client_list
.list
);
940 client_list
.trigger
= trigger
;
942 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
943 client_list_element
.client
= client
;
944 cds_list_add(&client_list_element
.node
, &client_list
.list
);
946 /* Send evaluation result to the newly-subscribed client. */
947 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
948 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
949 state
, object_uid
, object_gid
);
956 int notification_thread_client_subscribe(struct notification_client
*client
,
957 struct lttng_condition
*condition
,
958 struct notification_thread_state
*state
,
959 enum lttng_notification_channel_status
*_status
)
962 struct notification_client_list
*client_list
= NULL
;
963 struct lttng_condition_list_element
*condition_list_element
= NULL
;
964 struct notification_client_list_element
*client_list_element
= NULL
;
965 enum lttng_notification_channel_status status
=
966 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
969 * Ensure that the client has not already subscribed to this condition
972 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
973 if (lttng_condition_is_equal(condition_list_element
->condition
,
975 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
980 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
981 if (!condition_list_element
) {
985 client_list_element
= zmalloc(sizeof(*client_list_element
));
986 if (!client_list_element
) {
992 * Add the newly-subscribed condition to the client's subscription list.
994 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
995 condition_list_element
->condition
= condition
;
996 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
998 client_list
= get_client_list_from_condition(state
, condition
);
1001 * No notification-emiting trigger registered with this
1002 * condition. We don't evaluate the condition right away
1003 * since this trigger is not registered yet.
1005 free(client_list_element
);
1010 * The condition to which the client just subscribed is evaluated
1011 * at this point so that conditions that are already TRUE result
1012 * in a notification being sent out.
1014 * The client_list's trigger is used without locking the list itself.
1015 * This is correct since the list doesn't own the trigger and the
1016 * object is immutable.
1018 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1020 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1022 free(client_list_element
);
1027 * Add the client to the list of clients interested in a given trigger
1028 * if a "notification" trigger with a corresponding condition was
1031 client_list_element
->client
= client
;
1032 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1034 pthread_mutex_lock(&client_list
->lock
);
1035 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1036 pthread_mutex_unlock(&client_list
->lock
);
1042 notification_client_list_put(client_list
);
1046 free(condition_list_element
);
1047 free(client_list_element
);
1052 int notification_thread_client_unsubscribe(
1053 struct notification_client
*client
,
1054 struct lttng_condition
*condition
,
1055 struct notification_thread_state
*state
,
1056 enum lttng_notification_channel_status
*_status
)
1058 struct notification_client_list
*client_list
;
1059 struct lttng_condition_list_element
*condition_list_element
,
1061 struct notification_client_list_element
*client_list_element
,
1063 bool condition_found
= false;
1064 enum lttng_notification_channel_status status
=
1065 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1067 /* Remove the condition from the client's condition list. */
1068 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1069 &client
->condition_list
, node
) {
1070 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1075 cds_list_del(&condition_list_element
->node
);
1077 * The caller may be iterating on the client's conditions to
1078 * tear down a client's connection. In this case, the condition
1079 * will be destroyed at the end.
1081 if (condition
!= condition_list_element
->condition
) {
1082 lttng_condition_destroy(
1083 condition_list_element
->condition
);
1085 free(condition_list_element
);
1086 condition_found
= true;
1090 if (!condition_found
) {
1091 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1096 * Remove the client from the list of clients interested the trigger
1097 * matching the condition.
1099 client_list
= get_client_list_from_condition(state
, condition
);
1104 pthread_mutex_lock(&client_list
->lock
);
1105 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1106 &client_list
->list
, node
) {
1107 if (client_list_element
->client
->id
!= client
->id
) {
1110 cds_list_del(&client_list_element
->node
);
1111 free(client_list_element
);
1114 pthread_mutex_unlock(&client_list
->lock
);
1115 notification_client_list_put(client_list
);
1118 lttng_condition_destroy(condition
);
1126 void free_notification_client_rcu(struct rcu_head
*node
)
1128 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1132 void notification_client_destroy(struct notification_client
*client
,
1133 struct notification_thread_state
*state
)
1140 * The client object is not reachable by other threads, no need to lock
1143 if (client
->socket
>= 0) {
1144 (void) lttcomm_close_unix_sock(client
->socket
);
1145 client
->socket
= -1;
1147 client
->communication
.active
= false;
1148 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1149 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1150 pthread_mutex_destroy(&client
->lock
);
1151 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1155 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1159 struct notification_client
*get_client_from_socket(int socket
,
1160 struct notification_thread_state
*state
)
1162 struct cds_lfht_iter iter
;
1163 struct cds_lfht_node
*node
;
1164 struct notification_client
*client
= NULL
;
1166 cds_lfht_lookup(state
->client_socket_ht
,
1167 hash_client_socket(socket
),
1168 match_client_socket
,
1169 (void *) (unsigned long) socket
,
1171 node
= cds_lfht_iter_get_node(&iter
);
1176 client
= caa_container_of(node
, struct notification_client
,
1177 client_socket_ht_node
);
1183 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1187 struct notification_client
*get_client_from_id(notification_client_id id
,
1188 struct notification_thread_state
*state
)
1190 struct cds_lfht_iter iter
;
1191 struct cds_lfht_node
*node
;
1192 struct notification_client
*client
= NULL
;
1194 cds_lfht_lookup(state
->client_id_ht
,
1199 node
= cds_lfht_iter_get_node(&iter
);
1204 client
= caa_container_of(node
, struct notification_client
,
1211 bool buffer_usage_condition_applies_to_channel(
1212 const struct lttng_condition
*condition
,
1213 const struct channel_info
*channel_info
)
1215 enum lttng_condition_status status
;
1216 enum lttng_domain_type condition_domain
;
1217 const char *condition_session_name
= NULL
;
1218 const char *condition_channel_name
= NULL
;
1220 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1222 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1223 if (channel_info
->key
.domain
!= condition_domain
) {
1227 status
= lttng_condition_buffer_usage_get_session_name(
1228 condition
, &condition_session_name
);
1229 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1231 status
= lttng_condition_buffer_usage_get_channel_name(
1232 condition
, &condition_channel_name
);
1233 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1235 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1238 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1248 bool session_consumed_size_condition_applies_to_channel(
1249 const struct lttng_condition
*condition
,
1250 const struct channel_info
*channel_info
)
1252 enum lttng_condition_status status
;
1253 const char *condition_session_name
= NULL
;
1255 status
= lttng_condition_session_consumed_size_get_session_name(
1256 condition
, &condition_session_name
);
1257 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1259 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1269 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1270 const struct channel_info
*channel_info
)
1272 const struct lttng_condition
*condition
;
1273 bool trigger_applies
;
1275 condition
= lttng_trigger_get_const_condition(trigger
);
1280 switch (lttng_condition_get_type(condition
)) {
1281 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1282 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1283 trigger_applies
= buffer_usage_condition_applies_to_channel(
1284 condition
, channel_info
);
1286 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1287 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1288 condition
, channel_info
);
1294 return trigger_applies
;
1300 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1301 struct notification_client
*client
)
1303 bool applies
= false;
1304 struct lttng_condition_list_element
*condition_list_element
;
1306 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1308 applies
= lttng_condition_is_equal(
1309 condition_list_element
->condition
,
1310 lttng_trigger_get_condition(trigger
));
1318 /* Must be called with RCU read lock held. */
1320 struct lttng_session_trigger_list
*get_session_trigger_list(
1321 struct notification_thread_state
*state
,
1322 const char *session_name
)
1324 struct lttng_session_trigger_list
*list
= NULL
;
1325 struct cds_lfht_node
*node
;
1326 struct cds_lfht_iter iter
;
1328 cds_lfht_lookup(state
->session_triggers_ht
,
1329 hash_key_str(session_name
, lttng_ht_seed
),
1330 match_session_trigger_list
,
1333 node
= cds_lfht_iter_get_node(&iter
);
1336 * Not an error, the list of triggers applying to that session
1337 * will be initialized when the session is created.
1339 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1344 list
= caa_container_of(node
,
1345 struct lttng_session_trigger_list
,
1346 session_triggers_ht_node
);
1352 * Allocate an empty lttng_session_trigger_list for the session named
1355 * No ownership of 'session_name' is assumed by the session trigger list.
1356 * It is the caller's responsability to ensure the session name is alive
1357 * for as long as this list is.
1360 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1361 const char *session_name
,
1362 struct cds_lfht
*session_triggers_ht
)
1364 struct lttng_session_trigger_list
*list
;
1366 list
= zmalloc(sizeof(*list
));
1370 list
->session_name
= session_name
;
1371 CDS_INIT_LIST_HEAD(&list
->list
);
1372 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1373 list
->session_triggers_ht
= session_triggers_ht
;
1376 /* Publish the list through the session_triggers_ht. */
1377 cds_lfht_add(session_triggers_ht
,
1378 hash_key_str(session_name
, lttng_ht_seed
),
1379 &list
->session_triggers_ht_node
);
1386 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1388 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1393 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1395 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1397 /* Empty the list element by element, and then free the list itself. */
1398 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1399 &list
->list
, node
) {
1400 cds_list_del(&trigger_list_element
->node
);
1401 free(trigger_list_element
);
1404 /* Unpublish the list from the session_triggers_ht. */
1405 cds_lfht_del(list
->session_triggers_ht
,
1406 &list
->session_triggers_ht_node
);
1408 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1412 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1413 struct lttng_trigger
*trigger
)
1416 struct lttng_trigger_list_element
*new_element
=
1417 zmalloc(sizeof(*new_element
));
1423 CDS_INIT_LIST_HEAD(&new_element
->node
);
1424 new_element
->trigger
= trigger
;
1425 cds_list_add(&new_element
->node
, &list
->list
);
1431 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1432 const char *session_name
)
1434 bool applies
= false;
1435 const struct lttng_condition
*condition
;
1437 condition
= lttng_trigger_get_const_condition(trigger
);
1438 switch (lttng_condition_get_type(condition
)) {
1439 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1440 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1442 enum lttng_condition_status condition_status
;
1443 const char *condition_session_name
;
1445 condition_status
= lttng_condition_session_rotation_get_session_name(
1446 condition
, &condition_session_name
);
1447 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1448 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1452 assert(condition_session_name
);
1453 applies
= !strcmp(condition_session_name
, session_name
);
1464 * Allocate and initialize an lttng_session_trigger_list which contains
1465 * all triggers that apply to the session named 'session_name'.
1467 * No ownership of 'session_name' is assumed by the session trigger list.
1468 * It is the caller's responsability to ensure the session name is alive
1469 * for as long as this list is.
1472 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1473 const struct notification_thread_state
*state
,
1474 const char *session_name
)
1476 int trigger_count
= 0;
1477 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1478 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1479 struct cds_lfht_iter iter
;
1481 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1482 state
->session_triggers_ht
);
1484 /* Add all triggers applying to the session named 'session_name'. */
1485 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1489 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1494 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1495 trigger_ht_element
->trigger
);
1503 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1505 return session_trigger_list
;
1507 lttng_session_trigger_list_destroy(session_trigger_list
);
1512 struct session_info
*find_or_create_session_info(
1513 struct notification_thread_state
*state
,
1514 const char *name
, uid_t uid
, gid_t gid
)
1516 struct session_info
*session
= NULL
;
1517 struct cds_lfht_node
*node
;
1518 struct cds_lfht_iter iter
;
1519 struct lttng_session_trigger_list
*trigger_list
;
1522 cds_lfht_lookup(state
->sessions_ht
,
1523 hash_key_str(name
, lttng_ht_seed
),
1527 node
= cds_lfht_iter_get_node(&iter
);
1529 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1531 session
= caa_container_of(node
, struct session_info
,
1533 assert(session
->uid
== uid
);
1534 assert(session
->gid
== gid
);
1535 session_info_get(session
);
1539 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1540 if (!trigger_list
) {
1544 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1545 state
->sessions_ht
);
1547 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1549 lttng_session_trigger_list_destroy(trigger_list
);
1552 trigger_list
= NULL
;
1554 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1555 &session
->sessions_ht_node
);
1561 session_info_put(session
);
1566 int handle_notification_thread_command_add_channel(
1567 struct notification_thread_state
*state
,
1568 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1569 const char *channel_name
, enum lttng_domain_type channel_domain
,
1570 uint64_t channel_key_int
, uint64_t channel_capacity
,
1571 enum lttng_error_code
*cmd_result
)
1573 struct cds_list_head trigger_list
;
1574 struct channel_info
*new_channel_info
= NULL
;
1575 struct channel_key channel_key
= {
1576 .key
= channel_key_int
,
1577 .domain
= channel_domain
,
1579 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1580 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1581 int trigger_count
= 0;
1582 struct cds_lfht_iter iter
;
1583 struct session_info
*session_info
= NULL
;
1585 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1586 channel_name
, session_name
, channel_key_int
,
1587 lttng_domain_type_str(channel_domain
));
1589 CDS_INIT_LIST_HEAD(&trigger_list
);
1591 session_info
= find_or_create_session_info(state
, session_name
,
1592 session_uid
, session_gid
);
1593 if (!session_info
) {
1594 /* Allocation error or an internal error occurred. */
1598 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1599 channel_capacity
, session_info
);
1600 if (!new_channel_info
) {
1605 /* Build a list of all triggers applying to the new channel. */
1606 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1608 struct lttng_trigger_list_element
*new_element
;
1610 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1611 new_channel_info
)) {
1615 new_element
= zmalloc(sizeof(*new_element
));
1620 CDS_INIT_LIST_HEAD(&new_element
->node
);
1621 new_element
->trigger
= trigger_ht_element
->trigger
;
1622 cds_list_add(&new_element
->node
, &trigger_list
);
1627 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1629 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1630 if (!channel_trigger_list
) {
1633 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1634 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1635 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1636 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1639 /* Add channel to the channel_ht which owns the channel_infos. */
1640 cds_lfht_add(state
->channels_ht
,
1641 hash_channel_key(&new_channel_info
->key
),
1642 &new_channel_info
->channels_ht_node
);
1644 * Add the list of triggers associated with this channel to the
1645 * channel_triggers_ht.
1647 cds_lfht_add(state
->channel_triggers_ht
,
1648 hash_channel_key(&new_channel_info
->key
),
1649 &channel_trigger_list
->channel_triggers_ht_node
);
1651 session_info_put(session_info
);
1652 *cmd_result
= LTTNG_OK
;
1655 channel_info_destroy(new_channel_info
);
1656 session_info_put(session_info
);
1661 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1663 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1668 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1670 free(caa_container_of(node
, struct channel_state_sample
,
1675 int handle_notification_thread_command_remove_channel(
1676 struct notification_thread_state
*state
,
1677 uint64_t channel_key
, enum lttng_domain_type domain
,
1678 enum lttng_error_code
*cmd_result
)
1680 struct cds_lfht_node
*node
;
1681 struct cds_lfht_iter iter
;
1682 struct lttng_channel_trigger_list
*trigger_list
;
1683 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1684 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1685 struct channel_info
*channel_info
;
1687 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1688 channel_key
, lttng_domain_type_str(domain
));
1692 cds_lfht_lookup(state
->channel_triggers_ht
,
1693 hash_channel_key(&key
),
1694 match_channel_trigger_list
,
1697 node
= cds_lfht_iter_get_node(&iter
);
1699 * There is a severe internal error if we are being asked to remove a
1700 * channel that doesn't exist.
1703 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1707 /* Free the list of triggers associated with this channel. */
1708 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1709 channel_triggers_ht_node
);
1710 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1711 &trigger_list
->list
, node
) {
1712 cds_list_del(&trigger_list_element
->node
);
1713 free(trigger_list_element
);
1715 cds_lfht_del(state
->channel_triggers_ht
, node
);
1716 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1718 /* Free sampled channel state. */
1719 cds_lfht_lookup(state
->channel_state_ht
,
1720 hash_channel_key(&key
),
1721 match_channel_state_sample
,
1724 node
= cds_lfht_iter_get_node(&iter
);
1726 * This is expected to be NULL if the channel is destroyed before we
1727 * received a sample.
1730 struct channel_state_sample
*sample
= caa_container_of(node
,
1731 struct channel_state_sample
,
1732 channel_state_ht_node
);
1734 cds_lfht_del(state
->channel_state_ht
, node
);
1735 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1738 /* Remove the channel from the channels_ht and free it. */
1739 cds_lfht_lookup(state
->channels_ht
,
1740 hash_channel_key(&key
),
1744 node
= cds_lfht_iter_get_node(&iter
);
1746 channel_info
= caa_container_of(node
, struct channel_info
,
1748 cds_lfht_del(state
->channels_ht
, node
);
1749 channel_info_destroy(channel_info
);
1752 *cmd_result
= LTTNG_OK
;
1757 int handle_notification_thread_command_session_rotation(
1758 struct notification_thread_state
*state
,
1759 enum notification_thread_command_type cmd_type
,
1760 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1761 uint64_t trace_archive_chunk_id
,
1762 struct lttng_trace_archive_location
*location
,
1763 enum lttng_error_code
*_cmd_result
)
1766 enum lttng_error_code cmd_result
= LTTNG_OK
;
1767 struct lttng_session_trigger_list
*trigger_list
;
1768 struct lttng_trigger_list_element
*trigger_list_element
;
1769 struct session_info
*session_info
;
1770 const struct lttng_credentials session_creds
= {
1771 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1772 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1777 session_info
= find_or_create_session_info(state
, session_name
,
1778 session_uid
, session_gid
);
1779 if (!session_info
) {
1780 /* Allocation error or an internal error occurred. */
1782 cmd_result
= LTTNG_ERR_NOMEM
;
1786 session_info
->rotation
.ongoing
=
1787 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1788 session_info
->rotation
.id
= trace_archive_chunk_id
;
1789 trigger_list
= get_session_trigger_list(state
, session_name
);
1790 if (!trigger_list
) {
1791 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1796 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1798 const struct lttng_condition
*condition
;
1799 struct lttng_trigger
*trigger
;
1800 struct notification_client_list
*client_list
;
1801 struct lttng_evaluation
*evaluation
= NULL
;
1802 enum lttng_condition_type condition_type
;
1803 enum action_executor_status executor_status
;
1805 trigger
= trigger_list_element
->trigger
;
1806 condition
= lttng_trigger_get_const_condition(trigger
);
1808 condition_type
= lttng_condition_get_type(condition
);
1810 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1811 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1813 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1814 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1818 client_list
= get_client_list_from_condition(state
, condition
);
1819 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1820 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1821 trace_archive_chunk_id
);
1823 evaluation
= lttng_evaluation_session_rotation_completed_create(
1824 trace_archive_chunk_id
, location
);
1828 /* Internal error */
1830 cmd_result
= LTTNG_ERR_UNK
;
1835 * Ownership of `evaluation` transferred to the action executor
1836 * no matter the result.
1838 executor_status
= action_executor_enqueue(state
->executor
,
1839 trigger
, evaluation
, &session_creds
,
1842 switch (executor_status
) {
1843 case ACTION_EXECUTOR_STATUS_OK
:
1845 case ACTION_EXECUTOR_STATUS_ERROR
:
1846 case ACTION_EXECUTOR_STATUS_INVALID
:
1848 * TODO Add trigger identification (name/id) when
1849 * it is added to the API.
1851 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1854 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1856 * TODO Add trigger identification (name/id) when
1857 * it is added to the API.
1859 * Not a fatal error.
1861 WARN("No space left when enqueuing action associated with session rotation trigger");
1869 notification_client_list_put(client_list
);
1870 if (caa_unlikely(ret
)) {
1875 session_info_put(session_info
);
1876 *_cmd_result
= cmd_result
;
1882 int handle_notification_thread_command_add_application(
1883 struct notification_thread_handle
*handle
,
1884 struct notification_thread_state
*state
,
1885 int read_side_trigger_event_application_pipe
,
1886 enum lttng_domain_type domain_type
,
1887 enum lttng_error_code
*_cmd_result
)
1890 enum lttng_error_code cmd_result
= LTTNG_OK
;
1891 struct notification_event_trigger_source_element
*element
= NULL
;
1893 element
= zmalloc(sizeof(*element
));
1895 cmd_result
= LTTNG_ERR_NOMEM
;
1900 CDS_INIT_LIST_HEAD(&element
->node
);
1901 element
->fd
= read_side_trigger_event_application_pipe
;
1902 element
->domain
= domain_type
;
1904 pthread_mutex_lock(&handle
->event_trigger_sources
.lock
);
1905 cds_list_add(&element
->node
, &handle
->event_trigger_sources
.list
);
1906 pthread_mutex_unlock(&handle
->event_trigger_sources
.lock
);
1908 /* TODO: remove on failure to add to list? */
1910 /* Adding the read side pipe to the event poll */
1911 ret
= lttng_poll_add(&state
->events
,
1912 read_side_trigger_event_application_pipe
,
1913 LPOLLIN
| LPOLLERR
);
1915 DBG3("[notification-thread] Adding application event source from fd: %d", read_side_trigger_event_application_pipe
);
1917 /* TODO: what should be the value of cmd_result??? */
1918 ERR("[notification-thread] Failed to add event source pipe fd to pollset");
1923 *_cmd_result
= cmd_result
;
1928 int handle_notification_thread_command_remove_application(
1929 struct notification_thread_handle
*handle
,
1930 struct notification_thread_state
*state
,
1931 int read_side_trigger_event_application_pipe
,
1932 enum lttng_error_code
*_cmd_result
)
1935 enum lttng_error_code cmd_result
= LTTNG_OK
;
1936 /* Used for logging */
1937 enum lttng_domain_type domain
= LTTNG_DOMAIN_NONE
;
1939 /* TODO: missing a lock propably to revisit */
1940 struct notification_event_trigger_source_element
*source_element
= NULL
, *tmp
;
1942 cds_list_for_each_entry_safe(source_element
, tmp
,
1943 &handle
->event_trigger_sources
.list
, node
) {
1944 if (source_element
->fd
!= read_side_trigger_event_application_pipe
) {
1948 cds_list_del(&source_element
->node
);
1952 /* It should always be found */
1953 assert(source_element
);
1955 DBG3("[notification-thread] Removing application event source from fd: %d of domain: %s",
1956 read_side_trigger_event_application_pipe
,
1957 lttng_domain_type_str(domain
));
1958 free(source_element
);
1960 /* Removing the read side pipe to the event poll */
1961 ret
= lttng_poll_del(&state
->events
,
1962 read_side_trigger_event_application_pipe
);
1964 /* TODO: what should be the value of cmd_result??? */
1965 ERR("[notification-thread] Failed to remove event source pipe fd from pollset");
1970 *_cmd_result
= cmd_result
;
1974 static int handle_notification_thread_command_get_tokens(
1975 struct notification_thread_handle
*handle
,
1976 struct notification_thread_state
*state
,
1977 struct lttng_triggers
**triggers
,
1978 enum lttng_error_code
*_cmd_result
)
1981 enum lttng_error_code cmd_result
= LTTNG_OK
;
1982 struct cds_lfht_iter iter
;
1983 struct notification_trigger_tokens_ht_element
*element
;
1984 struct lttng_triggers
*local_triggers
= NULL
;
1986 local_triggers
= lttng_triggers_create();
1987 if (!local_triggers
) {
1988 cmd_result
= LTTNG_ERR_NOMEM
;
1993 cds_lfht_for_each_entry (
1994 state
->trigger_tokens_ht
, &iter
, element
, node
) {
1995 ret
= lttng_triggers_add(local_triggers
, element
->trigger
);
1997 cmd_result
= LTTNG_ERR_FATAL
;
2005 /* Passing ownership up */
2006 *triggers
= local_triggers
;
2007 local_triggers
= NULL
;
2011 lttng_triggers_destroy(local_triggers
);
2012 *_cmd_result
= cmd_result
;
2017 int trigger_update_error_count(struct lttng_trigger
*trigger
)
2020 uint64_t error_count
= 0;
2021 enum trigger_error_accounting_status status
;
2023 status
= trigger_error_accounting_get_count(trigger
, &error_count
);
2024 if (status
!= TRIGGER_ERROR_ACCOUNTING_STATUS_OK
) {
2025 ERR("Error getting trigger error count");
2028 lttng_trigger_set_error_count(trigger
, error_count
);
2032 static int handle_notification_thread_command_list_triggers(
2033 struct notification_thread_handle
*handle
,
2034 struct notification_thread_state
*state
,
2036 struct lttng_triggers
**triggers
,
2037 enum lttng_error_code
*_cmd_result
)
2040 enum lttng_error_code cmd_result
= LTTNG_OK
;
2041 struct cds_lfht_iter iter
;
2042 struct lttng_trigger_ht_element
*trigger_ht_element
;
2043 struct lttng_triggers
*local_triggers
= NULL
;
2044 const struct lttng_credentials
*creds
;
2047 unsigned long count
;
2050 cds_lfht_count_nodes(state
->triggers_ht
, &scb
, &count
, &sca
);
2052 local_triggers
= lttng_triggers_create();
2053 if (!local_triggers
) {
2054 cmd_result
= LTTNG_ERR_NOMEM
;
2058 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
,
2059 trigger_ht_element
, node
) {
2061 * Only return the trigger for which the requestion client have
2063 * Root user have visibility over all triggers.
2065 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2066 if (uid
!= lttng_credentials_get_uid(creds
) && uid
!= 0) {
2070 ret
= trigger_update_error_count(trigger_ht_element
->trigger
);
2073 ret
= lttng_triggers_add(local_triggers
, trigger_ht_element
->trigger
);
2082 /* Passing ownership up */
2083 *triggers
= local_triggers
;
2084 local_triggers
= NULL
;
2088 lttng_triggers_destroy(local_triggers
);
2089 *_cmd_result
= cmd_result
;
2094 int condition_is_supported(struct lttng_condition
*condition
)
2098 switch (lttng_condition_get_type(condition
)) {
2099 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2100 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2102 enum lttng_domain_type domain
;
2104 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2111 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2117 * Older kernel tracers don't expose the API to monitor their
2118 * buffers. Therefore, we reject triggers that require that
2119 * mechanism to be available to be evaluated.
2121 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
2124 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
2127 * Check for kernel support.
2128 * Check for ust support ??
2141 int action_is_supported(struct lttng_action
*action
)
2145 switch (lttng_action_get_type(action
)) {
2146 case LTTNG_ACTION_TYPE_NOTIFY
:
2147 case LTTNG_ACTION_TYPE_START_SESSION
:
2148 case LTTNG_ACTION_TYPE_STOP_SESSION
:
2149 case LTTNG_ACTION_TYPE_ROTATE_SESSION
:
2150 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION
:
2152 /* TODO validate that this is true for kernel in regards to
2153 * rotation and snapshot. Start stop is not a problem notify
2156 /* For now all type of actions are supported */
2160 case LTTNG_ACTION_TYPE_GROUP
:
2162 /* TODO: Iterate over all internal actions and validate that
2163 * they are supported
2176 /* Must be called with RCU read lock held. */
2178 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2179 struct notification_thread_state
*state
)
2182 const struct lttng_condition
*condition
;
2183 const char *session_name
;
2184 struct lttng_session_trigger_list
*trigger_list
;
2186 condition
= lttng_trigger_get_const_condition(trigger
);
2187 switch (lttng_condition_get_type(condition
)) {
2188 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2189 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2191 enum lttng_condition_status status
;
2193 status
= lttng_condition_session_rotation_get_session_name(
2194 condition
, &session_name
);
2195 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2196 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2207 trigger_list
= get_session_trigger_list(state
, session_name
);
2208 if (!trigger_list
) {
2209 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2215 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2217 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2222 /* Must be called with RCU read lock held. */
2224 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2225 struct notification_thread_state
*state
)
2228 struct cds_lfht_node
*node
;
2229 struct cds_lfht_iter iter
;
2230 struct channel_info
*channel
;
2232 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2234 struct lttng_trigger_list_element
*trigger_list_element
;
2235 struct lttng_channel_trigger_list
*trigger_list
;
2236 struct cds_lfht_iter lookup_iter
;
2238 if (!trigger_applies_to_channel(trigger
, channel
)) {
2242 cds_lfht_lookup(state
->channel_triggers_ht
,
2243 hash_channel_key(&channel
->key
),
2244 match_channel_trigger_list
,
2247 node
= cds_lfht_iter_get_node(&lookup_iter
);
2249 trigger_list
= caa_container_of(node
,
2250 struct lttng_channel_trigger_list
,
2251 channel_triggers_ht_node
);
2253 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2254 if (!trigger_list_element
) {
2258 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2259 trigger_list_element
->trigger
= trigger
;
2260 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2261 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2269 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2271 bool is_notify
= false;
2272 unsigned int i
, count
;
2273 enum lttng_action_status action_status
;
2274 const struct lttng_action
*action
=
2275 lttng_trigger_get_const_action(trigger
);
2276 enum lttng_action_type action_type
;
2279 action_type
= lttng_action_get_type(action
);
2280 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2283 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2287 action_status
= lttng_action_group_get_count(action
, &count
);
2288 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2290 for (i
= 0; i
< count
; i
++) {
2291 const struct lttng_action
*inner_action
=
2292 lttng_action_group_get_at_index(
2295 action_type
= lttng_action_get_type(inner_action
);
2296 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2306 static bool trigger_name_taken(struct notification_thread_state
*state
,
2307 const struct lttng_trigger
*trigger
)
2309 struct cds_lfht_node
*triggers_by_name_uid_ht_node
;
2310 struct cds_lfht_iter iter
;
2313 * No duplicata is allowed in the triggers_by_name_uid_ht.
2314 * The match is done against the trigger name and uid.
2316 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2317 hash_key_str(trigger
->name
, lttng_ht_seed
),
2321 triggers_by_name_uid_ht_node
= cds_lfht_iter_get_node(&iter
);
2322 if (triggers_by_name_uid_ht_node
) {
2330 void generate_trigger_name(struct notification_thread_state
*state
, struct lttng_trigger
*trigger
, const char **name
)
2333 * Here the offset criteria guarantee an end. This will be a nice
2334 * bikeshedding conversation. I would simply generate uuid and use them
2338 enum lttng_trigger_status status
;
2340 lttng_trigger_generate_name(trigger
, state
->trigger_id
.name_offset
);
2342 status
= lttng_trigger_get_name(trigger
, name
);
2343 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2345 taken
= trigger_name_taken(state
, trigger
);
2347 state
->trigger_id
.name_offset
++;
2349 } while (taken
|| state
->trigger_id
.name_offset
== UINT32_MAX
);
2353 * FIXME A client's credentials are not checked when registering a trigger.
2355 * The effects of this are benign since:
2356 * - The client will succeed in registering the trigger, as it is valid,
2357 * - The trigger will, internally, be bound to the channel/session,
2358 * - The notifications will not be sent since the client's credentials
2359 * are checked against the channel at that moment.
2361 * If this function returns a non-zero value, it means something is
2362 * fundamentally broken and the whole subsystem/thread will be torn down.
2364 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2368 int handle_notification_thread_command_register_trigger(
2369 struct notification_thread_state
*state
,
2370 struct lttng_trigger
*trigger
,
2371 enum lttng_error_code
*cmd_result
)
2375 struct lttng_condition
*condition
;
2376 struct lttng_action
*action
;
2377 struct notification_client
*client
;
2378 struct notification_client_list
*client_list
= NULL
;
2379 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2380 struct notification_client_list_element
*client_list_element
;
2381 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2382 struct cds_lfht_node
*node
;
2383 struct cds_lfht_iter iter
;
2384 const char* trigger_name
;
2385 bool free_trigger
= true;
2386 struct lttng_evaluation
*evaluation
= NULL
;
2387 struct lttng_credentials object_creds
;
2390 enum action_executor_status executor_status
;
2394 /* Set the trigger's tracer token */
2395 lttng_trigger_set_tracer_token(trigger
, state
->trigger_id
.token_generator
);
2397 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2398 LTTNG_TRIGGER_STATUS_UNSET
) {
2399 generate_trigger_name(state
, trigger
, &trigger_name
);
2400 } else if (trigger_name_taken(state
, trigger
)) {
2401 /* Not a fatal error */
2402 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2407 condition
= lttng_trigger_get_condition(trigger
);
2410 action
= lttng_trigger_get_action(trigger
);
2413 is_supported
= condition_is_supported(condition
);
2414 if (is_supported
< 0) {
2416 } else if (is_supported
== 0) {
2417 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2421 is_supported
= action_is_supported(action
);
2422 if (is_supported
< 0) {
2424 } else if (is_supported
== 0) {
2426 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2430 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2431 if (!trigger_ht_element
) {
2436 /* Add trigger to the trigger_ht. */
2437 cds_lfht_node_init(&trigger_ht_element
->node
);
2438 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2439 trigger_ht_element
->trigger
= trigger
;
2441 node
= cds_lfht_add_unique(state
->triggers_ht
,
2442 lttng_condition_hash(condition
),
2445 &trigger_ht_element
->node
);
2446 if (node
!= &trigger_ht_element
->node
) {
2447 /* Not a fatal error, simply report it to the client. */
2448 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2449 goto error_free_ht_element
;
2452 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2453 hash_key_str(trigger_name
, lttng_ht_seed
), match_name_uid
,
2454 trigger
, &trigger_ht_element
->node_by_name_uid
);
2455 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2456 /* Not a fatal error, simply report it to the client. */
2457 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2458 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2459 goto error_free_ht_element
;
2462 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2463 uint64_t error_counter_index
= 0;
2464 enum trigger_error_accounting_status error_accounting_status
;
2466 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2467 if (!trigger_tokens_ht_element
) {
2469 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2470 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2474 error_accounting_status
= trigger_error_accounting_register_trigger(
2475 trigger
, &error_counter_index
);
2476 if (error_accounting_status
!= TRIGGER_ERROR_ACCOUNTING_STATUS_OK
) {
2477 ERR("Error registering trigger for error accounting");
2478 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2479 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2480 *cmd_result
= LTTNG_ERR_TRIGGER_GROUP_ERROR_COUNTER_FULL
;
2481 goto error_free_ht_element
;
2484 lttng_trigger_set_error_counter_index(trigger
, error_counter_index
);
2486 /* Add trigger token to the trigger_tokens_ht. */
2487 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2488 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2489 trigger_tokens_ht_element
->trigger
= trigger
;
2491 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2492 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2493 match_trigger_token
,
2494 &trigger_tokens_ht_element
->token
,
2495 &trigger_tokens_ht_element
->node
);
2496 if (node
!= &trigger_tokens_ht_element
->node
) {
2497 /* TODO: THIS IS A FATAL ERROR... should never happen */
2498 /* Not a fatal error, simply report it to the client. */
2499 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2500 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2501 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2502 trigger_error_accounting_unregister_trigger(trigger
);
2503 goto error_free_ht_element
;
2508 * Ownership of the trigger and of its wrapper was transfered to
2509 * the triggers_ht. Same for token ht element if necessary.
2511 trigger_tokens_ht_element
= NULL
;
2512 trigger_ht_element
= NULL
;
2513 free_trigger
= false;
2516 * The rest only applies to triggers that have a "notify" action.
2517 * It is not skipped as this is the only action type currently
2520 if (is_trigger_action_notify(trigger
)) {
2521 client_list
= notification_client_list_create(trigger
);
2524 goto error_free_ht_element
;
2527 /* Build a list of clients to which this new trigger applies. */
2528 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2529 client_socket_ht_node
) {
2530 if (!trigger_applies_to_client(trigger
, client
)) {
2534 client_list_element
=
2535 zmalloc(sizeof(*client_list_element
));
2536 if (!client_list_element
) {
2538 goto error_put_client_list
;
2541 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2542 client_list_element
->client
= client
;
2543 cds_list_add(&client_list_element
->node
,
2544 &client_list
->list
);
2548 * Client list ownership transferred to the
2549 * notification_trigger_clients_ht.
2551 publish_notification_client_list(state
, client_list
);
2554 switch (get_condition_binding_object(condition
)) {
2555 case LTTNG_OBJECT_TYPE_SESSION
:
2556 /* Add the trigger to the list if it matches a known session. */
2557 ret
= bind_trigger_to_matching_session(trigger
, state
);
2559 goto error_put_client_list
;
2562 case LTTNG_OBJECT_TYPE_CHANNEL
:
2564 * Add the trigger to list of triggers bound to the channels
2567 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2569 goto error_put_client_list
;
2572 case LTTNG_OBJECT_TYPE_NONE
:
2575 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2577 goto error_put_client_list
;
2581 * The new trigger's condition must be evaluated against the current
2584 * In the case of `notify` action, nothing preventing clients from
2585 * subscribing to a condition before the corresponding trigger is
2586 * registered, we have to evaluate this new condition right away.
2588 * At some point, we were waiting for the next "evaluation" (e.g. on
2589 * reception of a channel sample) to evaluate this new condition, but
2592 * The reason it was broken is that waiting for the next sample
2593 * does not allow us to properly handle transitions for edge-triggered
2596 * Consider this example: when we handle a new channel sample, we
2597 * evaluate each conditions twice: once with the previous state, and
2598 * again with the newest state. We then use those two results to
2599 * determine whether a state change happened: a condition was false and
2600 * became true. If a state change happened, we have to notify clients.
2602 * Now, if a client subscribes to a given notification and registers
2603 * a trigger *after* that subscription, we have to make sure the
2604 * condition is evaluated at this point while considering only the
2605 * current state. Otherwise, the next evaluation cycle may only see
2606 * that the evaluations remain the same (true for samples n-1 and n) and
2607 * the client will never know that the condition has been met.
2609 switch (get_condition_binding_object(condition
)) {
2610 case LTTNG_OBJECT_TYPE_SESSION
:
2611 ret
= evaluate_session_condition_for_client(condition
, state
,
2612 &evaluation
, &object_uid
,
2615 case LTTNG_OBJECT_TYPE_CHANNEL
:
2616 ret
= evaluate_channel_condition_for_client(condition
, state
,
2617 &evaluation
, &object_uid
,
2620 case LTTNG_OBJECT_TYPE_NONE
:
2623 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2631 goto error_put_client_list
;
2634 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2635 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2637 DBG("Newly registered trigger's condition evaluated to %s",
2638 evaluation
? "true" : "false");
2640 /* Evaluation yielded nothing. Normal exit. */
2646 * Ownership of `evaluation` transferred to the action executor
2647 * no matter the result.
2649 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2650 evaluation
, &object_creds
, client_list
);
2652 switch (executor_status
) {
2653 case ACTION_EXECUTOR_STATUS_OK
:
2655 case ACTION_EXECUTOR_STATUS_ERROR
:
2656 case ACTION_EXECUTOR_STATUS_INVALID
:
2658 * TODO Add trigger identification (name/id) when
2659 * it is added to the API.
2661 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2663 goto error_put_client_list
;
2664 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2666 * TODO Add trigger identification (name/id) when
2667 * it is added to the API.
2669 * Not a fatal error.
2671 WARN("No space left when enqueuing action associated to newly registered trigger");
2679 /* Increment the trigger unique id generator */
2680 state
->trigger_id
.token_generator
++;
2681 *cmd_result
= LTTNG_OK
;
2683 error_put_client_list
:
2684 notification_client_list_put(client_list
);
2686 error_free_ht_element
:
2687 if (trigger_ht_element
) {
2688 /* Delayed removal due to RCU constraint on delete. */
2689 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2692 free(trigger_tokens_ht_element
);
2695 lttng_trigger_destroy(trigger
);
2702 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2704 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2709 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2711 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2716 int handle_notification_thread_command_unregister_trigger(
2717 struct notification_thread_state
*state
,
2718 struct lttng_trigger
*trigger
,
2719 enum lttng_error_code
*_cmd_reply
)
2721 struct cds_lfht_iter iter
;
2722 struct cds_lfht_node
*triggers_ht_node
;
2723 struct lttng_channel_trigger_list
*trigger_list
;
2724 struct notification_client_list
*client_list
;
2725 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2726 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2728 enum lttng_error_code cmd_reply
;
2732 cds_lfht_lookup(state
->triggers_ht
,
2733 lttng_condition_hash(condition
),
2737 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2738 if (!triggers_ht_node
) {
2739 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2742 cmd_reply
= LTTNG_OK
;
2745 /* Remove trigger from channel_triggers_ht. */
2746 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2747 channel_triggers_ht_node
) {
2748 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2750 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2751 &trigger_list
->list
, node
) {
2752 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2756 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2757 cds_list_del(&trigger_element
->node
);
2758 /* A trigger can only appear once per channel */
2763 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2764 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2765 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
, trigger_tokens_ht_element
,
2767 if (!lttng_trigger_is_equal(trigger
, trigger_tokens_ht_element
->trigger
)) {
2771 trigger_error_accounting_unregister_trigger(trigger_tokens_ht_element
->trigger
);
2773 /* TODO talk to all app and remove it */
2774 DBG("[notification-thread] Removed trigger from tokens_ht");
2775 cds_lfht_del(state
->trigger_tokens_ht
,
2776 &trigger_tokens_ht_element
->node
);
2777 call_rcu(&trigger_tokens_ht_element
->rcu_node
, free_notification_trigger_tokens_ht_element_rcu
);
2783 if (is_trigger_action_notify(trigger
)) {
2785 * Remove and release the client list from
2786 * notification_trigger_clients_ht.
2788 client_list
= get_client_list_from_condition(state
, condition
);
2789 assert(client_list
);
2791 /* Put new reference and the hashtable's reference. */
2792 notification_client_list_put(client_list
);
2793 notification_client_list_put(client_list
);
2797 /* Remove trigger from triggers_ht. */
2798 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2799 struct lttng_trigger_ht_element
, node
);
2800 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2801 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2803 /* Release the ownership of the trigger. */
2804 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2805 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2809 *_cmd_reply
= cmd_reply
;
2814 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2815 int handle_notification_thread_command(
2816 struct notification_thread_handle
*handle
,
2817 struct notification_thread_state
*state
)
2821 struct notification_thread_command
*cmd
;
2823 /* Read the event pipe to put it back into a quiescent state. */
2824 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2826 if (ret
!= sizeof(counter
)) {
2830 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2831 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2832 struct notification_thread_command
, cmd_list_node
);
2833 cds_list_del(&cmd
->cmd_list_node
);
2834 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2835 switch (cmd
->type
) {
2836 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2837 DBG("[notification-thread] Received register trigger command");
2838 ret
= handle_notification_thread_command_register_trigger(
2839 state
, cmd
->parameters
.trigger
,
2842 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2843 DBG("[notification-thread] Received unregister trigger command");
2844 ret
= handle_notification_thread_command_unregister_trigger(
2845 state
, cmd
->parameters
.trigger
,
2848 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2849 DBG("[notification-thread] Received add channel command");
2850 ret
= handle_notification_thread_command_add_channel(
2852 cmd
->parameters
.add_channel
.session
.name
,
2853 cmd
->parameters
.add_channel
.session
.uid
,
2854 cmd
->parameters
.add_channel
.session
.gid
,
2855 cmd
->parameters
.add_channel
.channel
.name
,
2856 cmd
->parameters
.add_channel
.channel
.domain
,
2857 cmd
->parameters
.add_channel
.channel
.key
,
2858 cmd
->parameters
.add_channel
.channel
.capacity
,
2861 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2862 DBG("[notification-thread] Received remove channel command");
2863 ret
= handle_notification_thread_command_remove_channel(
2864 state
, cmd
->parameters
.remove_channel
.key
,
2865 cmd
->parameters
.remove_channel
.domain
,
2868 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2869 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2870 DBG("[notification-thread] Received session rotation %s command",
2871 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2872 "ongoing" : "completed");
2873 ret
= handle_notification_thread_command_session_rotation(
2876 cmd
->parameters
.session_rotation
.session_name
,
2877 cmd
->parameters
.session_rotation
.uid
,
2878 cmd
->parameters
.session_rotation
.gid
,
2879 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2880 cmd
->parameters
.session_rotation
.location
,
2883 case NOTIFICATION_COMMAND_TYPE_ADD_APPLICATION
:
2884 ret
= handle_notification_thread_command_add_application(
2887 cmd
->parameters
.application
.read_side_trigger_event_application_pipe
,
2888 cmd
->parameters
.application
.domain
,
2891 case NOTIFICATION_COMMAND_TYPE_REMOVE_APPLICATION
:
2892 ret
= handle_notification_thread_command_remove_application(
2895 cmd
->parameters
.application
.read_side_trigger_event_application_pipe
,
2898 case NOTIFICATION_COMMAND_TYPE_GET_TOKENS
:
2900 struct lttng_triggers
*triggers
= NULL
;
2901 ret
= handle_notification_thread_command_get_tokens(
2902 handle
, state
, &triggers
, &cmd
->reply_code
);
2903 cmd
->reply
.get_tokens
.triggers
= triggers
;
2907 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2909 struct lttng_triggers
*triggers
= NULL
;
2910 ret
= handle_notification_thread_command_list_triggers(
2913 cmd
->parameters
.list_triggers
.uid
,
2916 cmd
->reply
.list_triggers
.triggers
= triggers
;
2920 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2921 DBG("[notification-thread] Received quit command");
2922 cmd
->reply_code
= LTTNG_OK
;
2925 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2927 const enum client_transmission_status client_status
=
2928 cmd
->parameters
.client_communication_update
2930 const notification_client_id client_id
=
2931 cmd
->parameters
.client_communication_update
.id
;
2932 struct notification_client
*client
;
2935 client
= get_client_from_id(client_id
, state
);
2939 * Client error was probably already picked-up by the
2940 * notification thread or it has disconnected
2941 * gracefully while this command was queued.
2943 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2947 ret
= client_handle_transmission_status(
2948 client
, client_status
, state
);
2954 ERR("[notification-thread] Unknown internal command received");
2962 if (cmd
->is_async
) {
2966 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2970 /* Wake-up and return a fatal error to the calling thread. */
2971 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2972 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2974 /* Indicate a fatal error to the caller. */
2979 int socket_set_non_blocking(int socket
)
2983 /* Set the pipe as non-blocking. */
2984 ret
= fcntl(socket
, F_GETFL
, 0);
2986 PERROR("fcntl get socket flags");
2991 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2993 PERROR("fcntl set O_NONBLOCK socket flag");
2996 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3002 int client_reset_inbound_state(struct notification_client
*client
)
3007 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3009 client
->communication
.inbound
.bytes_to_receive
=
3010 sizeof(struct lttng_notification_channel_message
);
3011 client
->communication
.inbound
.msg_type
=
3012 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3013 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3014 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3015 ret
= lttng_dynamic_buffer_set_size(
3016 &client
->communication
.inbound
.payload
.buffer
,
3017 client
->communication
.inbound
.bytes_to_receive
);
3022 int handle_notification_thread_client_connect(
3023 struct notification_thread_state
*state
)
3026 struct notification_client
*client
;
3028 DBG("[notification-thread] Handling new notification channel client connection");
3030 client
= zmalloc(sizeof(*client
));
3037 pthread_mutex_init(&client
->lock
, NULL
);
3038 client
->id
= state
->next_notification_client_id
++;
3039 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3040 lttng_payload_init(&client
->communication
.inbound
.payload
);
3041 lttng_payload_init(&client
->communication
.outbound
.payload
);
3042 client
->communication
.inbound
.expect_creds
= true;