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/domain-internal.h>
31 #include <lttng/notification/channel-internal.h>
32 #include <lttng/trigger/trigger-internal.h>
33 #include <lttng/event-rule/event-rule-internal.h>
41 #include "condition-internal.h"
42 #include "notification-thread.h"
43 #include "notification-thread-events.h"
44 #include "notification-thread-commands.h"
45 #include "lttng-sessiond.h"
48 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
49 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
51 enum lttng_object_type
{
52 LTTNG_OBJECT_TYPE_UNKNOWN
,
53 LTTNG_OBJECT_TYPE_NONE
,
54 LTTNG_OBJECT_TYPE_CHANNEL
,
55 LTTNG_OBJECT_TYPE_SESSION
,
58 struct lttng_trigger_list_element
{
59 /* No ownership of the trigger object is assumed. */
60 struct lttng_trigger
*trigger
;
61 struct cds_list_head node
;
64 struct lttng_channel_trigger_list
{
65 struct channel_key channel_key
;
66 /* List of struct lttng_trigger_list_element. */
67 struct cds_list_head list
;
68 /* Node in the channel_triggers_ht */
69 struct cds_lfht_node channel_triggers_ht_node
;
70 /* call_rcu delayed reclaim. */
71 struct rcu_head rcu_node
;
75 * List of triggers applying to a given session.
78 * - lttng_session_trigger_list_create()
79 * - lttng_session_trigger_list_build()
80 * - lttng_session_trigger_list_destroy()
81 * - lttng_session_trigger_list_add()
83 struct lttng_session_trigger_list
{
85 * Not owned by this; points to the session_info structure's
88 const char *session_name
;
89 /* List of struct lttng_trigger_list_element. */
90 struct cds_list_head list
;
91 /* Node in the session_triggers_ht */
92 struct cds_lfht_node session_triggers_ht_node
;
94 * Weak reference to the notification system's session triggers
97 * The session trigger list structure structure is owned by
98 * the session's session_info.
100 * The session_info is kept alive the the channel_infos holding a
101 * reference to it (reference counting). When those channels are
102 * destroyed (at runtime or on teardown), the reference they hold
103 * to the session_info are released. On destruction of session_info,
104 * session_info_destroy() will remove the list of triggers applying
105 * to this session from the notification system's state.
107 * This implies that the session_triggers_ht must be destroyed
108 * after the channels.
110 struct cds_lfht
*session_triggers_ht
;
111 /* Used for delayed RCU reclaim. */
112 struct rcu_head rcu_node
;
115 struct lttng_trigger_ht_element
{
116 struct lttng_trigger
*trigger
;
117 struct cds_lfht_node node
;
118 struct cds_lfht_node node_by_name_uid
;
119 /* call_rcu delayed reclaim. */
120 struct rcu_head rcu_node
;
123 struct lttng_condition_list_element
{
124 struct lttng_condition
*condition
;
125 struct cds_list_head node
;
128 struct channel_state_sample
{
129 struct channel_key key
;
130 struct cds_lfht_node channel_state_ht_node
;
131 uint64_t highest_usage
;
132 uint64_t lowest_usage
;
133 uint64_t channel_total_consumed
;
134 /* call_rcu delayed reclaim. */
135 struct rcu_head rcu_node
;
138 static unsigned long hash_channel_key(struct channel_key
*key
);
139 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
140 struct lttng_evaluation
**evaluation
,
141 const struct notification_thread_state
*state
,
142 const struct channel_state_sample
*previous_sample
,
143 const struct channel_state_sample
*latest_sample
,
144 uint64_t previous_session_consumed_total
,
145 uint64_t latest_session_consumed_total
,
146 struct channel_info
*channel_info
);
148 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
149 const struct lttng_evaluation
*evaluation
,
150 struct notification_client_list
*client_list
,
151 struct notification_thread_state
*state
,
152 uid_t channel_uid
, gid_t channel_gid
);
155 /* session_info API */
157 void session_info_destroy(void *_data
);
159 void session_info_get(struct session_info
*session_info
);
161 void session_info_put(struct session_info
*session_info
);
163 struct session_info
*session_info_create(const char *name
,
164 uid_t uid
, gid_t gid
,
165 struct lttng_session_trigger_list
*trigger_list
,
166 struct cds_lfht
*sessions_ht
);
168 void session_info_add_channel(struct session_info
*session_info
,
169 struct channel_info
*channel_info
);
171 void session_info_remove_channel(struct session_info
*session_info
,
172 struct channel_info
*channel_info
);
174 /* lttng_session_trigger_list API */
176 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
177 const char *session_name
,
178 struct cds_lfht
*session_triggers_ht
);
180 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
181 const struct notification_thread_state
*state
,
182 const char *session_name
);
184 void lttng_session_trigger_list_destroy(
185 struct lttng_session_trigger_list
*list
);
187 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
188 struct lttng_trigger
*trigger
);
191 int client_handle_transmission_status(
192 struct notification_client
*client
,
193 enum client_transmission_status transmission_status
,
194 struct notification_thread_state
*state
);
197 int handle_one_event_notifier_notification(
198 struct notification_thread_state
*state
,
199 int pipe
, enum lttng_domain_type domain
);
202 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
205 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
207 /* This double-cast is intended to supress pointer-to-cast warning. */
208 const int socket
= (int) (intptr_t) key
;
209 const struct notification_client
*client
= caa_container_of(node
,
210 struct notification_client
, client_socket_ht_node
);
212 return client
->socket
== socket
;
216 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
218 /* This double-cast is intended to supress pointer-to-cast warning. */
219 const notification_client_id id
= *((notification_client_id
*) key
);
220 const struct notification_client
*client
= caa_container_of(
221 node
, struct notification_client
, client_id_ht_node
);
223 return client
->id
== id
;
227 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
229 struct channel_key
*channel_key
= (struct channel_key
*) key
;
230 struct lttng_channel_trigger_list
*trigger_list
;
232 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
233 channel_triggers_ht_node
);
235 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
236 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
240 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
242 const char *session_name
= (const char *) key
;
243 struct lttng_session_trigger_list
*trigger_list
;
245 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
246 session_triggers_ht_node
);
248 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
252 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
254 struct channel_key
*channel_key
= (struct channel_key
*) key
;
255 struct channel_state_sample
*sample
;
257 sample
= caa_container_of(node
, struct channel_state_sample
,
258 channel_state_ht_node
);
260 return !!((channel_key
->key
== sample
->key
.key
) &&
261 (channel_key
->domain
== sample
->key
.domain
));
265 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
267 struct channel_key
*channel_key
= (struct channel_key
*) key
;
268 struct channel_info
*channel_info
;
270 channel_info
= caa_container_of(node
, struct channel_info
,
273 return !!((channel_key
->key
== channel_info
->key
.key
) &&
274 (channel_key
->domain
== channel_info
->key
.domain
));
278 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
280 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
281 struct lttng_trigger_ht_element
*trigger_ht_element
;
283 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
286 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
290 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
292 const uint64_t *_key
= key
;
293 struct notification_trigger_tokens_ht_element
*element
;
295 element
= caa_container_of(node
,
296 struct notification_trigger_tokens_ht_element
, node
);
297 return *_key
== element
->token
;
301 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
303 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
304 struct notification_client_list
*client_list
;
305 const struct lttng_condition
*condition
;
307 assert(condition_key
);
309 client_list
= caa_container_of(node
, struct notification_client_list
,
310 notification_trigger_clients_ht_node
);
311 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
313 return !!lttng_condition_is_equal(condition_key
, condition
);
317 int match_session(struct cds_lfht_node
*node
, const void *key
)
319 const char *name
= key
;
320 struct session_info
*session_info
= caa_container_of(
321 node
, struct session_info
, sessions_ht_node
);
323 return !strcmp(session_info
->name
, name
);
327 const char *notification_command_type_str(
328 enum notification_thread_command_type type
)
331 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
332 return "REGISTER_TRIGGER";
333 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
334 return "UNREGISTER_TRIGGER";
335 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
336 return "ADD_CHANNEL";
337 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
338 return "REMOVE_CHANNEL";
339 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
340 return "SESSION_ROTATION_ONGOING";
341 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
342 return "SESSION_ROTATION_COMPLETED";
343 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
344 return "ADD_TRACER_EVENT_SOURCE";
345 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
346 return "REMOVE_TRACER_EVENT_SOURCE";
347 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
348 return "LIST_TRIGGERS";
349 case NOTIFICATION_COMMAND_TYPE_QUIT
:
351 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
352 return "CLIENT_COMMUNICATION_UPDATE";
359 * Match trigger based on name and credentials only.
360 * Name duplication is NOT allowed for the same uid.
363 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
368 const char *key_name
;
369 enum lttng_trigger_status status
;
370 const struct lttng_credentials
*key_creds
;
371 const struct lttng_credentials
*node_creds
;
372 const struct lttng_trigger
*trigger_key
=
373 (const struct lttng_trigger
*) key
;
374 const struct lttng_trigger_ht_element
*trigger_ht_element
=
375 caa_container_of(node
,
376 struct lttng_trigger_ht_element
,
379 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
380 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
382 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
383 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
385 /* Compare the names. */
386 if (strcmp(name
, key_name
) != 0) {
390 /* Compare the owners' UIDs. */
391 key_creds
= lttng_trigger_get_credentials(trigger_key
);
392 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
394 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
401 * Hash trigger based on name and credentials only.
404 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
406 unsigned long hash
= 0;
407 const struct lttng_credentials
*trigger_creds
;
408 const char *trigger_name
;
409 enum lttng_trigger_status status
;
411 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
412 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
413 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
416 trigger_creds
= lttng_trigger_get_credentials(trigger
);
417 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
424 unsigned long hash_channel_key(struct channel_key
*key
)
426 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
427 unsigned long domain_hash
= hash_key_ulong(
428 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
430 return key_hash
^ domain_hash
;
434 unsigned long hash_client_socket(int socket
)
436 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
440 unsigned long hash_client_id(notification_client_id id
)
442 return hash_key_u64(&id
, lttng_ht_seed
);
446 * Get the type of object to which a given condition applies. Bindings let
447 * the notification system evaluate a trigger's condition when a given
448 * object's state is updated.
450 * For instance, a condition bound to a channel will be evaluated everytime
451 * the channel's state is changed by a channel monitoring sample.
454 enum lttng_object_type
get_condition_binding_object(
455 const struct lttng_condition
*condition
)
457 switch (lttng_condition_get_type(condition
)) {
458 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
459 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
460 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
461 return LTTNG_OBJECT_TYPE_CHANNEL
;
462 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
463 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
464 return LTTNG_OBJECT_TYPE_SESSION
;
465 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
466 return LTTNG_OBJECT_TYPE_NONE
;
468 return LTTNG_OBJECT_TYPE_UNKNOWN
;
473 void free_channel_info_rcu(struct rcu_head
*node
)
475 free(caa_container_of(node
, struct channel_info
, rcu_node
));
479 void channel_info_destroy(struct channel_info
*channel_info
)
485 if (channel_info
->session_info
) {
486 session_info_remove_channel(channel_info
->session_info
,
488 session_info_put(channel_info
->session_info
);
490 if (channel_info
->name
) {
491 free(channel_info
->name
);
493 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
497 void free_session_info_rcu(struct rcu_head
*node
)
499 free(caa_container_of(node
, struct session_info
, rcu_node
));
502 /* Don't call directly, use the ref-counting mechanism. */
504 void session_info_destroy(void *_data
)
506 struct session_info
*session_info
= _data
;
509 assert(session_info
);
510 if (session_info
->channel_infos_ht
) {
511 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
513 ERR("[notification-thread] Failed to destroy channel information hash table");
516 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
519 cds_lfht_del(session_info
->sessions_ht
,
520 &session_info
->sessions_ht_node
);
522 free(session_info
->name
);
523 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
527 void session_info_get(struct session_info
*session_info
)
532 lttng_ref_get(&session_info
->ref
);
536 void session_info_put(struct session_info
*session_info
)
541 lttng_ref_put(&session_info
->ref
);
545 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
546 struct lttng_session_trigger_list
*trigger_list
,
547 struct cds_lfht
*sessions_ht
)
549 struct session_info
*session_info
;
553 session_info
= zmalloc(sizeof(*session_info
));
557 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
559 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
560 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
561 if (!session_info
->channel_infos_ht
) {
565 cds_lfht_node_init(&session_info
->sessions_ht_node
);
566 session_info
->name
= strdup(name
);
567 if (!session_info
->name
) {
570 session_info
->uid
= uid
;
571 session_info
->gid
= gid
;
572 session_info
->trigger_list
= trigger_list
;
573 session_info
->sessions_ht
= sessions_ht
;
577 session_info_put(session_info
);
582 void session_info_add_channel(struct session_info
*session_info
,
583 struct channel_info
*channel_info
)
586 cds_lfht_add(session_info
->channel_infos_ht
,
587 hash_channel_key(&channel_info
->key
),
588 &channel_info
->session_info_channels_ht_node
);
593 void session_info_remove_channel(struct session_info
*session_info
,
594 struct channel_info
*channel_info
)
597 cds_lfht_del(session_info
->channel_infos_ht
,
598 &channel_info
->session_info_channels_ht_node
);
603 struct channel_info
*channel_info_create(const char *channel_name
,
604 struct channel_key
*channel_key
, uint64_t channel_capacity
,
605 struct session_info
*session_info
)
607 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
613 cds_lfht_node_init(&channel_info
->channels_ht_node
);
614 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
615 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
616 channel_info
->capacity
= channel_capacity
;
618 channel_info
->name
= strdup(channel_name
);
619 if (!channel_info
->name
) {
624 * Set the references between session and channel infos:
625 * - channel_info holds a strong reference to session_info
626 * - session_info holds a weak reference to channel_info
628 session_info_get(session_info
);
629 session_info_add_channel(session_info
, channel_info
);
630 channel_info
->session_info
= session_info
;
634 channel_info_destroy(channel_info
);
639 bool notification_client_list_get(struct notification_client_list
*list
)
641 return urcu_ref_get_unless_zero(&list
->ref
);
645 void free_notification_client_list_rcu(struct rcu_head
*node
)
647 free(caa_container_of(node
, struct notification_client_list
,
652 void notification_client_list_release(struct urcu_ref
*list_ref
)
654 struct notification_client_list
*list
=
655 container_of(list_ref
, typeof(*list
), ref
);
656 struct notification_client_list_element
*client_list_element
, *tmp
;
658 if (list
->notification_trigger_clients_ht
) {
660 cds_lfht_del(list
->notification_trigger_clients_ht
,
661 &list
->notification_trigger_clients_ht_node
);
663 list
->notification_trigger_clients_ht
= NULL
;
665 cds_list_for_each_entry_safe(client_list_element
, tmp
,
667 free(client_list_element
);
669 pthread_mutex_destroy(&list
->lock
);
670 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
674 struct notification_client_list
*notification_client_list_create(
675 const struct lttng_trigger
*trigger
)
677 struct notification_client_list
*client_list
=
678 zmalloc(sizeof(*client_list
));
683 pthread_mutex_init(&client_list
->lock
, NULL
);
684 urcu_ref_init(&client_list
->ref
);
685 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
686 CDS_INIT_LIST_HEAD(&client_list
->list
);
687 client_list
->trigger
= trigger
;
693 void publish_notification_client_list(
694 struct notification_thread_state
*state
,
695 struct notification_client_list
*list
)
697 const struct lttng_condition
*condition
=
698 lttng_trigger_get_const_condition(list
->trigger
);
700 assert(!list
->notification_trigger_clients_ht
);
701 notification_client_list_get(list
);
703 list
->notification_trigger_clients_ht
=
704 state
->notification_trigger_clients_ht
;
707 cds_lfht_add(state
->notification_trigger_clients_ht
,
708 lttng_condition_hash(condition
),
709 &list
->notification_trigger_clients_ht_node
);
714 void notification_client_list_put(struct notification_client_list
*list
)
719 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
722 /* Provides a reference to the returned list. */
724 struct notification_client_list
*get_client_list_from_condition(
725 struct notification_thread_state
*state
,
726 const struct lttng_condition
*condition
)
728 struct cds_lfht_node
*node
;
729 struct cds_lfht_iter iter
;
730 struct notification_client_list
*list
= NULL
;
733 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
734 lttng_condition_hash(condition
),
735 match_client_list_condition
,
738 node
= cds_lfht_iter_get_node(&iter
);
740 list
= container_of(node
, struct notification_client_list
,
741 notification_trigger_clients_ht_node
);
742 list
= notification_client_list_get(list
) ? list
: NULL
;
750 int evaluate_channel_condition_for_client(
751 const struct lttng_condition
*condition
,
752 struct notification_thread_state
*state
,
753 struct lttng_evaluation
**evaluation
,
754 uid_t
*session_uid
, gid_t
*session_gid
)
757 struct cds_lfht_iter iter
;
758 struct cds_lfht_node
*node
;
759 struct channel_info
*channel_info
= NULL
;
760 struct channel_key
*channel_key
= NULL
;
761 struct channel_state_sample
*last_sample
= NULL
;
762 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
766 /* Find the channel associated with the condition. */
767 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
768 channel_trigger_list
, channel_triggers_ht_node
) {
769 struct lttng_trigger_list_element
*element
;
771 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
772 const struct lttng_condition
*current_condition
=
773 lttng_trigger_get_const_condition(
776 assert(current_condition
);
777 if (!lttng_condition_is_equal(condition
,
778 current_condition
)) {
782 /* Found the trigger, save the channel key. */
783 channel_key
= &channel_trigger_list
->channel_key
;
787 /* The channel key was found stop iteration. */
793 /* No channel found; normal exit. */
794 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
799 /* Fetch channel info for the matching channel. */
800 cds_lfht_lookup(state
->channels_ht
,
801 hash_channel_key(channel_key
),
805 node
= cds_lfht_iter_get_node(&iter
);
807 channel_info
= caa_container_of(node
, struct channel_info
,
810 /* Retrieve the channel's last sample, if it exists. */
811 cds_lfht_lookup(state
->channel_state_ht
,
812 hash_channel_key(channel_key
),
813 match_channel_state_sample
,
816 node
= cds_lfht_iter_get_node(&iter
);
818 last_sample
= caa_container_of(node
,
819 struct channel_state_sample
,
820 channel_state_ht_node
);
822 /* Nothing to evaluate, no sample was ever taken. Normal exit */
823 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
828 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
830 0, channel_info
->session_info
->consumed_data_size
,
833 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
837 *session_uid
= channel_info
->session_info
->uid
;
838 *session_gid
= channel_info
->session_info
->gid
;
845 const char *get_condition_session_name(const struct lttng_condition
*condition
)
847 const char *session_name
= NULL
;
848 enum lttng_condition_status status
;
850 switch (lttng_condition_get_type(condition
)) {
851 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
852 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
853 status
= lttng_condition_buffer_usage_get_session_name(
854 condition
, &session_name
);
856 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
857 status
= lttng_condition_session_consumed_size_get_session_name(
858 condition
, &session_name
);
860 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
861 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
862 status
= lttng_condition_session_rotation_get_session_name(
863 condition
, &session_name
);
868 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
869 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
877 int evaluate_session_condition_for_client(
878 const struct lttng_condition
*condition
,
879 struct notification_thread_state
*state
,
880 struct lttng_evaluation
**evaluation
,
881 uid_t
*session_uid
, gid_t
*session_gid
)
884 struct cds_lfht_iter iter
;
885 struct cds_lfht_node
*node
;
886 const char *session_name
;
887 struct session_info
*session_info
= NULL
;
890 session_name
= get_condition_session_name(condition
);
892 /* Find the session associated with the trigger. */
893 cds_lfht_lookup(state
->sessions_ht
,
894 hash_key_str(session_name
, lttng_ht_seed
),
898 node
= cds_lfht_iter_get_node(&iter
);
900 DBG("[notification-thread] No known session matching name \"%s\"",
906 session_info
= caa_container_of(node
, struct session_info
,
908 session_info_get(session_info
);
911 * Evaluation is performed in-line here since only one type of
912 * session-bound condition is handled for the moment.
914 switch (lttng_condition_get_type(condition
)) {
915 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
916 if (!session_info
->rotation
.ongoing
) {
918 goto end_session_put
;
921 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
922 session_info
->rotation
.id
);
925 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
928 goto end_session_put
;
934 goto end_session_put
;
937 *session_uid
= session_info
->uid
;
938 *session_gid
= session_info
->gid
;
941 session_info_put(session_info
);
948 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
949 const struct lttng_condition
*condition
,
950 struct notification_client
*client
,
951 struct notification_thread_state
*state
)
954 struct lttng_evaluation
*evaluation
= NULL
;
955 struct notification_client_list client_list
= {
956 .lock
= PTHREAD_MUTEX_INITIALIZER
,
958 struct notification_client_list_element client_list_element
= { 0 };
959 uid_t object_uid
= 0;
960 gid_t object_gid
= 0;
967 switch (get_condition_binding_object(condition
)) {
968 case LTTNG_OBJECT_TYPE_SESSION
:
969 ret
= evaluate_session_condition_for_client(condition
, state
,
970 &evaluation
, &object_uid
, &object_gid
);
972 case LTTNG_OBJECT_TYPE_CHANNEL
:
973 ret
= evaluate_channel_condition_for_client(condition
, state
,
974 &evaluation
, &object_uid
, &object_gid
);
976 case LTTNG_OBJECT_TYPE_NONE
:
977 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
980 case LTTNG_OBJECT_TYPE_UNKNOWN
:
990 /* Evaluation yielded nothing. Normal exit. */
991 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
997 * Create a temporary client list with the client currently
1000 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1001 CDS_INIT_LIST_HEAD(&client_list
.list
);
1002 client_list
.trigger
= trigger
;
1004 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1005 client_list_element
.client
= client
;
1006 cds_list_add(&client_list_element
.node
, &client_list
.list
);
1008 /* Send evaluation result to the newly-subscribed client. */
1009 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1010 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1011 state
, object_uid
, object_gid
);
1018 int notification_thread_client_subscribe(struct notification_client
*client
,
1019 struct lttng_condition
*condition
,
1020 struct notification_thread_state
*state
,
1021 enum lttng_notification_channel_status
*_status
)
1024 struct notification_client_list
*client_list
= NULL
;
1025 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1026 struct notification_client_list_element
*client_list_element
= NULL
;
1027 enum lttng_notification_channel_status status
=
1028 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1031 * Ensure that the client has not already subscribed to this condition
1034 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1035 if (lttng_condition_is_equal(condition_list_element
->condition
,
1037 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1042 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1043 if (!condition_list_element
) {
1047 client_list_element
= zmalloc(sizeof(*client_list_element
));
1048 if (!client_list_element
) {
1054 * Add the newly-subscribed condition to the client's subscription list.
1056 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1057 condition_list_element
->condition
= condition
;
1058 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1060 client_list
= get_client_list_from_condition(state
, condition
);
1063 * No notification-emiting trigger registered with this
1064 * condition. We don't evaluate the condition right away
1065 * since this trigger is not registered yet.
1067 free(client_list_element
);
1072 * The condition to which the client just subscribed is evaluated
1073 * at this point so that conditions that are already TRUE result
1074 * in a notification being sent out.
1076 * The client_list's trigger is used without locking the list itself.
1077 * This is correct since the list doesn't own the trigger and the
1078 * object is immutable.
1080 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1082 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1084 free(client_list_element
);
1089 * Add the client to the list of clients interested in a given trigger
1090 * if a "notification" trigger with a corresponding condition was
1093 client_list_element
->client
= client
;
1094 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1096 pthread_mutex_lock(&client_list
->lock
);
1097 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1098 pthread_mutex_unlock(&client_list
->lock
);
1104 notification_client_list_put(client_list
);
1108 free(condition_list_element
);
1109 free(client_list_element
);
1114 int notification_thread_client_unsubscribe(
1115 struct notification_client
*client
,
1116 struct lttng_condition
*condition
,
1117 struct notification_thread_state
*state
,
1118 enum lttng_notification_channel_status
*_status
)
1120 struct notification_client_list
*client_list
;
1121 struct lttng_condition_list_element
*condition_list_element
,
1123 struct notification_client_list_element
*client_list_element
,
1125 bool condition_found
= false;
1126 enum lttng_notification_channel_status status
=
1127 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1129 /* Remove the condition from the client's condition list. */
1130 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1131 &client
->condition_list
, node
) {
1132 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1137 cds_list_del(&condition_list_element
->node
);
1139 * The caller may be iterating on the client's conditions to
1140 * tear down a client's connection. In this case, the condition
1141 * will be destroyed at the end.
1143 if (condition
!= condition_list_element
->condition
) {
1144 lttng_condition_destroy(
1145 condition_list_element
->condition
);
1147 free(condition_list_element
);
1148 condition_found
= true;
1152 if (!condition_found
) {
1153 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1158 * Remove the client from the list of clients interested the trigger
1159 * matching the condition.
1161 client_list
= get_client_list_from_condition(state
, condition
);
1166 pthread_mutex_lock(&client_list
->lock
);
1167 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1168 &client_list
->list
, node
) {
1169 if (client_list_element
->client
->id
!= client
->id
) {
1172 cds_list_del(&client_list_element
->node
);
1173 free(client_list_element
);
1176 pthread_mutex_unlock(&client_list
->lock
);
1177 notification_client_list_put(client_list
);
1180 lttng_condition_destroy(condition
);
1188 void free_notification_client_rcu(struct rcu_head
*node
)
1190 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1194 void notification_client_destroy(struct notification_client
*client
,
1195 struct notification_thread_state
*state
)
1202 * The client object is not reachable by other threads, no need to lock
1205 if (client
->socket
>= 0) {
1206 (void) lttcomm_close_unix_sock(client
->socket
);
1207 client
->socket
= -1;
1209 client
->communication
.active
= false;
1210 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1211 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1212 pthread_mutex_destroy(&client
->lock
);
1213 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1217 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1221 struct notification_client
*get_client_from_socket(int socket
,
1222 struct notification_thread_state
*state
)
1224 struct cds_lfht_iter iter
;
1225 struct cds_lfht_node
*node
;
1226 struct notification_client
*client
= NULL
;
1228 cds_lfht_lookup(state
->client_socket_ht
,
1229 hash_client_socket(socket
),
1230 match_client_socket
,
1231 (void *) (unsigned long) socket
,
1233 node
= cds_lfht_iter_get_node(&iter
);
1238 client
= caa_container_of(node
, struct notification_client
,
1239 client_socket_ht_node
);
1245 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1249 struct notification_client
*get_client_from_id(notification_client_id id
,
1250 struct notification_thread_state
*state
)
1252 struct cds_lfht_iter iter
;
1253 struct cds_lfht_node
*node
;
1254 struct notification_client
*client
= NULL
;
1256 cds_lfht_lookup(state
->client_id_ht
,
1261 node
= cds_lfht_iter_get_node(&iter
);
1266 client
= caa_container_of(node
, struct notification_client
,
1273 bool buffer_usage_condition_applies_to_channel(
1274 const struct lttng_condition
*condition
,
1275 const struct channel_info
*channel_info
)
1277 enum lttng_condition_status status
;
1278 enum lttng_domain_type condition_domain
;
1279 const char *condition_session_name
= NULL
;
1280 const char *condition_channel_name
= NULL
;
1282 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1284 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1285 if (channel_info
->key
.domain
!= condition_domain
) {
1289 status
= lttng_condition_buffer_usage_get_session_name(
1290 condition
, &condition_session_name
);
1291 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1293 status
= lttng_condition_buffer_usage_get_channel_name(
1294 condition
, &condition_channel_name
);
1295 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1297 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1300 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1310 bool session_consumed_size_condition_applies_to_channel(
1311 const struct lttng_condition
*condition
,
1312 const struct channel_info
*channel_info
)
1314 enum lttng_condition_status status
;
1315 const char *condition_session_name
= NULL
;
1317 status
= lttng_condition_session_consumed_size_get_session_name(
1318 condition
, &condition_session_name
);
1319 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1321 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1331 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1332 const struct channel_info
*channel_info
)
1334 const struct lttng_condition
*condition
;
1335 bool trigger_applies
;
1337 condition
= lttng_trigger_get_const_condition(trigger
);
1342 switch (lttng_condition_get_type(condition
)) {
1343 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1344 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1345 trigger_applies
= buffer_usage_condition_applies_to_channel(
1346 condition
, channel_info
);
1348 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1349 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1350 condition
, channel_info
);
1356 return trigger_applies
;
1362 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1363 struct notification_client
*client
)
1365 bool applies
= false;
1366 struct lttng_condition_list_element
*condition_list_element
;
1368 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1370 applies
= lttng_condition_is_equal(
1371 condition_list_element
->condition
,
1372 lttng_trigger_get_condition(trigger
));
1380 /* Must be called with RCU read lock held. */
1382 struct lttng_session_trigger_list
*get_session_trigger_list(
1383 struct notification_thread_state
*state
,
1384 const char *session_name
)
1386 struct lttng_session_trigger_list
*list
= NULL
;
1387 struct cds_lfht_node
*node
;
1388 struct cds_lfht_iter iter
;
1390 cds_lfht_lookup(state
->session_triggers_ht
,
1391 hash_key_str(session_name
, lttng_ht_seed
),
1392 match_session_trigger_list
,
1395 node
= cds_lfht_iter_get_node(&iter
);
1398 * Not an error, the list of triggers applying to that session
1399 * will be initialized when the session is created.
1401 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1406 list
= caa_container_of(node
,
1407 struct lttng_session_trigger_list
,
1408 session_triggers_ht_node
);
1414 * Allocate an empty lttng_session_trigger_list for the session named
1417 * No ownership of 'session_name' is assumed by the session trigger list.
1418 * It is the caller's responsability to ensure the session name is alive
1419 * for as long as this list is.
1422 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1423 const char *session_name
,
1424 struct cds_lfht
*session_triggers_ht
)
1426 struct lttng_session_trigger_list
*list
;
1428 list
= zmalloc(sizeof(*list
));
1432 list
->session_name
= session_name
;
1433 CDS_INIT_LIST_HEAD(&list
->list
);
1434 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1435 list
->session_triggers_ht
= session_triggers_ht
;
1438 /* Publish the list through the session_triggers_ht. */
1439 cds_lfht_add(session_triggers_ht
,
1440 hash_key_str(session_name
, lttng_ht_seed
),
1441 &list
->session_triggers_ht_node
);
1448 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1450 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1455 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1457 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1459 /* Empty the list element by element, and then free the list itself. */
1460 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1461 &list
->list
, node
) {
1462 cds_list_del(&trigger_list_element
->node
);
1463 free(trigger_list_element
);
1466 /* Unpublish the list from the session_triggers_ht. */
1467 cds_lfht_del(list
->session_triggers_ht
,
1468 &list
->session_triggers_ht_node
);
1470 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1474 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1475 struct lttng_trigger
*trigger
)
1478 struct lttng_trigger_list_element
*new_element
=
1479 zmalloc(sizeof(*new_element
));
1485 CDS_INIT_LIST_HEAD(&new_element
->node
);
1486 new_element
->trigger
= trigger
;
1487 cds_list_add(&new_element
->node
, &list
->list
);
1493 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1494 const char *session_name
)
1496 bool applies
= false;
1497 const struct lttng_condition
*condition
;
1499 condition
= lttng_trigger_get_const_condition(trigger
);
1500 switch (lttng_condition_get_type(condition
)) {
1501 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1502 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1504 enum lttng_condition_status condition_status
;
1505 const char *condition_session_name
;
1507 condition_status
= lttng_condition_session_rotation_get_session_name(
1508 condition
, &condition_session_name
);
1509 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1510 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1514 assert(condition_session_name
);
1515 applies
= !strcmp(condition_session_name
, session_name
);
1526 * Allocate and initialize an lttng_session_trigger_list which contains
1527 * all triggers that apply to the session named 'session_name'.
1529 * No ownership of 'session_name' is assumed by the session trigger list.
1530 * It is the caller's responsability to ensure the session name is alive
1531 * for as long as this list is.
1534 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1535 const struct notification_thread_state
*state
,
1536 const char *session_name
)
1538 int trigger_count
= 0;
1539 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1540 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1541 struct cds_lfht_iter iter
;
1543 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1544 state
->session_triggers_ht
);
1546 /* Add all triggers applying to the session named 'session_name'. */
1547 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1551 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1556 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1557 trigger_ht_element
->trigger
);
1565 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1567 return session_trigger_list
;
1569 lttng_session_trigger_list_destroy(session_trigger_list
);
1574 struct session_info
*find_or_create_session_info(
1575 struct notification_thread_state
*state
,
1576 const char *name
, uid_t uid
, gid_t gid
)
1578 struct session_info
*session
= NULL
;
1579 struct cds_lfht_node
*node
;
1580 struct cds_lfht_iter iter
;
1581 struct lttng_session_trigger_list
*trigger_list
;
1584 cds_lfht_lookup(state
->sessions_ht
,
1585 hash_key_str(name
, lttng_ht_seed
),
1589 node
= cds_lfht_iter_get_node(&iter
);
1591 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1593 session
= caa_container_of(node
, struct session_info
,
1595 assert(session
->uid
== uid
);
1596 assert(session
->gid
== gid
);
1597 session_info_get(session
);
1601 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1602 if (!trigger_list
) {
1606 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1607 state
->sessions_ht
);
1609 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1611 lttng_session_trigger_list_destroy(trigger_list
);
1614 trigger_list
= NULL
;
1616 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1617 &session
->sessions_ht_node
);
1623 session_info_put(session
);
1628 int handle_notification_thread_command_add_channel(
1629 struct notification_thread_state
*state
,
1630 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1631 const char *channel_name
, enum lttng_domain_type channel_domain
,
1632 uint64_t channel_key_int
, uint64_t channel_capacity
,
1633 enum lttng_error_code
*cmd_result
)
1635 struct cds_list_head trigger_list
;
1636 struct channel_info
*new_channel_info
= NULL
;
1637 struct channel_key channel_key
= {
1638 .key
= channel_key_int
,
1639 .domain
= channel_domain
,
1641 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1642 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1643 int trigger_count
= 0;
1644 struct cds_lfht_iter iter
;
1645 struct session_info
*session_info
= NULL
;
1647 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1648 channel_name
, session_name
, channel_key_int
,
1649 lttng_domain_type_str(channel_domain
));
1651 CDS_INIT_LIST_HEAD(&trigger_list
);
1653 session_info
= find_or_create_session_info(state
, session_name
,
1654 session_uid
, session_gid
);
1655 if (!session_info
) {
1656 /* Allocation error or an internal error occurred. */
1660 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1661 channel_capacity
, session_info
);
1662 if (!new_channel_info
) {
1667 /* Build a list of all triggers applying to the new channel. */
1668 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1670 struct lttng_trigger_list_element
*new_element
;
1672 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1673 new_channel_info
)) {
1677 new_element
= zmalloc(sizeof(*new_element
));
1682 CDS_INIT_LIST_HEAD(&new_element
->node
);
1683 new_element
->trigger
= trigger_ht_element
->trigger
;
1684 cds_list_add(&new_element
->node
, &trigger_list
);
1689 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1691 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1692 if (!channel_trigger_list
) {
1695 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1696 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1697 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1698 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1701 /* Add channel to the channel_ht which owns the channel_infos. */
1702 cds_lfht_add(state
->channels_ht
,
1703 hash_channel_key(&new_channel_info
->key
),
1704 &new_channel_info
->channels_ht_node
);
1706 * Add the list of triggers associated with this channel to the
1707 * channel_triggers_ht.
1709 cds_lfht_add(state
->channel_triggers_ht
,
1710 hash_channel_key(&new_channel_info
->key
),
1711 &channel_trigger_list
->channel_triggers_ht_node
);
1713 session_info_put(session_info
);
1714 *cmd_result
= LTTNG_OK
;
1717 channel_info_destroy(new_channel_info
);
1718 session_info_put(session_info
);
1723 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1725 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1730 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1732 free(caa_container_of(node
, struct channel_state_sample
,
1737 int handle_notification_thread_command_remove_channel(
1738 struct notification_thread_state
*state
,
1739 uint64_t channel_key
, enum lttng_domain_type domain
,
1740 enum lttng_error_code
*cmd_result
)
1742 struct cds_lfht_node
*node
;
1743 struct cds_lfht_iter iter
;
1744 struct lttng_channel_trigger_list
*trigger_list
;
1745 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1746 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1747 struct channel_info
*channel_info
;
1749 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1750 channel_key
, lttng_domain_type_str(domain
));
1754 cds_lfht_lookup(state
->channel_triggers_ht
,
1755 hash_channel_key(&key
),
1756 match_channel_trigger_list
,
1759 node
= cds_lfht_iter_get_node(&iter
);
1761 * There is a severe internal error if we are being asked to remove a
1762 * channel that doesn't exist.
1765 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1769 /* Free the list of triggers associated with this channel. */
1770 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1771 channel_triggers_ht_node
);
1772 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1773 &trigger_list
->list
, node
) {
1774 cds_list_del(&trigger_list_element
->node
);
1775 free(trigger_list_element
);
1777 cds_lfht_del(state
->channel_triggers_ht
, node
);
1778 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1780 /* Free sampled channel state. */
1781 cds_lfht_lookup(state
->channel_state_ht
,
1782 hash_channel_key(&key
),
1783 match_channel_state_sample
,
1786 node
= cds_lfht_iter_get_node(&iter
);
1788 * This is expected to be NULL if the channel is destroyed before we
1789 * received a sample.
1792 struct channel_state_sample
*sample
= caa_container_of(node
,
1793 struct channel_state_sample
,
1794 channel_state_ht_node
);
1796 cds_lfht_del(state
->channel_state_ht
, node
);
1797 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1800 /* Remove the channel from the channels_ht and free it. */
1801 cds_lfht_lookup(state
->channels_ht
,
1802 hash_channel_key(&key
),
1806 node
= cds_lfht_iter_get_node(&iter
);
1808 channel_info
= caa_container_of(node
, struct channel_info
,
1810 cds_lfht_del(state
->channels_ht
, node
);
1811 channel_info_destroy(channel_info
);
1814 *cmd_result
= LTTNG_OK
;
1819 int handle_notification_thread_command_session_rotation(
1820 struct notification_thread_state
*state
,
1821 enum notification_thread_command_type cmd_type
,
1822 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1823 uint64_t trace_archive_chunk_id
,
1824 struct lttng_trace_archive_location
*location
,
1825 enum lttng_error_code
*_cmd_result
)
1828 enum lttng_error_code cmd_result
= LTTNG_OK
;
1829 struct lttng_session_trigger_list
*trigger_list
;
1830 struct lttng_trigger_list_element
*trigger_list_element
;
1831 struct session_info
*session_info
;
1832 const struct lttng_credentials session_creds
= {
1833 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1834 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1839 session_info
= find_or_create_session_info(state
, session_name
,
1840 session_uid
, session_gid
);
1841 if (!session_info
) {
1842 /* Allocation error or an internal error occurred. */
1844 cmd_result
= LTTNG_ERR_NOMEM
;
1848 session_info
->rotation
.ongoing
=
1849 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1850 session_info
->rotation
.id
= trace_archive_chunk_id
;
1851 trigger_list
= get_session_trigger_list(state
, session_name
);
1852 if (!trigger_list
) {
1853 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1858 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1860 const struct lttng_condition
*condition
;
1861 struct lttng_trigger
*trigger
;
1862 struct notification_client_list
*client_list
;
1863 struct lttng_evaluation
*evaluation
= NULL
;
1864 enum lttng_condition_type condition_type
;
1865 enum action_executor_status executor_status
;
1867 trigger
= trigger_list_element
->trigger
;
1868 condition
= lttng_trigger_get_const_condition(trigger
);
1870 condition_type
= lttng_condition_get_type(condition
);
1872 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1873 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1875 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1876 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1880 client_list
= get_client_list_from_condition(state
, condition
);
1881 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1882 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1883 trace_archive_chunk_id
);
1885 evaluation
= lttng_evaluation_session_rotation_completed_create(
1886 trace_archive_chunk_id
, location
);
1890 /* Internal error */
1892 cmd_result
= LTTNG_ERR_UNK
;
1897 * Ownership of `evaluation` transferred to the action executor
1898 * no matter the result.
1900 executor_status
= action_executor_enqueue(state
->executor
,
1901 trigger
, evaluation
, &session_creds
,
1904 switch (executor_status
) {
1905 case ACTION_EXECUTOR_STATUS_OK
:
1907 case ACTION_EXECUTOR_STATUS_ERROR
:
1908 case ACTION_EXECUTOR_STATUS_INVALID
:
1910 * TODO Add trigger identification (name/id) when
1911 * it is added to the API.
1913 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1916 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1918 * TODO Add trigger identification (name/id) when
1919 * it is added to the API.
1921 * Not a fatal error.
1923 WARN("No space left when enqueuing action associated with session rotation trigger");
1931 notification_client_list_put(client_list
);
1932 if (caa_unlikely(ret
)) {
1937 session_info_put(session_info
);
1938 *_cmd_result
= cmd_result
;
1944 int handle_notification_thread_command_add_tracer_event_source(
1945 struct notification_thread_state
*state
,
1946 int tracer_event_source_fd
,
1947 enum lttng_domain_type domain_type
,
1948 enum lttng_error_code
*_cmd_result
)
1951 enum lttng_error_code cmd_result
= LTTNG_OK
;
1952 struct notification_event_tracer_event_source_element
*element
= NULL
;
1954 element
= zmalloc(sizeof(*element
));
1956 cmd_result
= LTTNG_ERR_NOMEM
;
1961 element
->fd
= tracer_event_source_fd
;
1962 element
->domain
= domain_type
;
1964 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
1966 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
1967 tracer_event_source_fd
,
1968 lttng_domain_type_str(domain_type
));
1970 /* Adding the read side pipe to the event poll. */
1971 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
1973 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
1974 tracer_event_source_fd
,
1975 lttng_domain_type_str(element
->domain
));
1976 cds_list_del(&element
->node
);
1981 element
->is_fd_in_poll_set
= true;
1984 *_cmd_result
= cmd_result
;
1989 int drain_event_notifier_notification_pipe(
1990 struct notification_thread_state
*state
,
1991 int pipe
, enum lttng_domain_type domain
)
1993 struct lttng_poll_event events
= {0};
1996 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
1998 ERR("[notification-thread] Error creating lttng_poll_event");
2002 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2004 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2011 * Continue to consume notifications as long as there are new
2012 * ones coming in. The tracer has been asked to stop producing
2015 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2016 * monitored (on Linux, at least) and will be returned when
2017 * the pipe is closed but empty.
2019 ret
= lttng_poll_wait_interruptible(&events
, 0);
2020 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 1) & LPOLLIN
) == 0) {
2021 /* No more notification to be read on this pipe. */
2024 } else if (ret
< 0) {
2025 PERROR("Failed on lttng_poll_wait_interruptible() call");
2030 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2032 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2037 lttng_poll_clean(&events
);
2042 int handle_notification_thread_command_remove_tracer_event_source(
2043 struct notification_thread_state
*state
,
2044 int tracer_event_source_fd
,
2045 enum lttng_error_code
*_cmd_result
)
2049 enum lttng_error_code cmd_result
= LTTNG_OK
;
2050 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
2052 cds_list_for_each_entry_safe(source_element
, tmp
,
2053 &state
->tracer_event_sources_list
, node
) {
2054 if (source_element
->fd
!= tracer_event_source_fd
) {
2058 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2059 tracer_event_source_fd
,
2060 lttng_domain_type_str(source_element
->domain
));
2061 cds_list_del(&source_element
->node
);
2068 * This is temporarily allowed since the poll activity set is
2069 * not properly cleaned-up for the moment. This is adressed in
2072 source_element
= NULL
;
2076 if (!source_element
->is_fd_in_poll_set
) {
2077 /* Skip the poll set removal. */
2081 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2082 tracer_event_source_fd
,
2083 lttng_domain_type_str(source_element
->domain
));
2085 /* Removing the fd from the event poll set. */
2086 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2088 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2089 tracer_event_source_fd
,
2090 lttng_domain_type_str(source_element
->domain
));
2091 cmd_result
= LTTNG_ERR_FATAL
;
2095 source_element
->is_fd_in_poll_set
= false;
2097 ret
= drain_event_notifier_notification_pipe(state
, tracer_event_source_fd
,
2098 source_element
->domain
);
2100 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2101 tracer_event_source_fd
,
2102 lttng_domain_type_str(source_element
->domain
));
2103 cmd_result
= LTTNG_ERR_FATAL
;
2108 * The drain_event_notifier_notification_pipe() call might have read
2109 * data from an fd that we received in event in the latest _poll_wait()
2110 * call. Make sure the thread call poll_wait() again to ensure we have
2113 state
->restart_poll
= true;
2116 free(source_element
);
2117 *_cmd_result
= cmd_result
;
2121 int handle_notification_thread_remove_tracer_event_source_no_result(
2122 struct notification_thread_state
*state
,
2123 int tracer_event_source_fd
)
2126 enum lttng_error_code cmd_result
;
2128 ret
= handle_notification_thread_command_remove_tracer_event_source(
2129 state
, tracer_event_source_fd
, &cmd_result
);
2134 static int handle_notification_thread_command_list_triggers(
2135 struct notification_thread_handle
*handle
,
2136 struct notification_thread_state
*state
,
2138 struct lttng_triggers
**triggers
,
2139 enum lttng_error_code
*_cmd_result
)
2142 enum lttng_error_code cmd_result
= LTTNG_OK
;
2143 struct cds_lfht_iter iter
;
2144 struct lttng_trigger_ht_element
*trigger_ht_element
;
2145 struct lttng_triggers
*local_triggers
= NULL
;
2146 const struct lttng_credentials
*creds
;
2150 local_triggers
= lttng_triggers_create();
2151 if (!local_triggers
) {
2152 /* Not a fatal error. */
2153 cmd_result
= LTTNG_ERR_NOMEM
;
2157 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2158 trigger_ht_element
, node
) {
2160 * Only return the triggers to which the client has access.
2161 * The root user has visibility over all triggers.
2163 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2164 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2168 ret
= lttng_triggers_add(local_triggers
,
2169 trigger_ht_element
->trigger
);
2171 /* Not a fatal error. */
2173 cmd_result
= LTTNG_ERR_NOMEM
;
2178 /* Transferring ownership to the caller. */
2179 *triggers
= local_triggers
;
2180 local_triggers
= NULL
;
2184 lttng_triggers_destroy(local_triggers
);
2185 *_cmd_result
= cmd_result
;
2190 bool condition_is_supported(struct lttng_condition
*condition
)
2194 switch (lttng_condition_get_type(condition
)) {
2195 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2196 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2199 enum lttng_domain_type domain
;
2201 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2205 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2206 is_supported
= true;
2211 * Older kernel tracers don't expose the API to monitor their
2212 * buffers. Therefore, we reject triggers that require that
2213 * mechanism to be available to be evaluated.
2215 * Assume unsupported on error.
2217 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2220 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
2222 const struct lttng_event_rule
*event_rule
;
2223 enum lttng_domain_type domain
;
2224 const enum lttng_condition_status status
=
2225 lttng_condition_event_rule_get_rule(
2226 condition
, &event_rule
);
2228 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2230 domain
= lttng_event_rule_get_domain_type(event_rule
);
2231 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2232 is_supported
= true;
2237 * Older kernel tracers can't emit notification. Therefore, we
2238 * reject triggers that require that mechanism to be available
2241 * Assume unsupported on error.
2243 is_supported
= kernel_supports_event_notifiers() == 1;
2247 is_supported
= true;
2250 return is_supported
;
2253 /* Must be called with RCU read lock held. */
2255 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2256 struct notification_thread_state
*state
)
2259 const struct lttng_condition
*condition
;
2260 const char *session_name
;
2261 struct lttng_session_trigger_list
*trigger_list
;
2263 condition
= lttng_trigger_get_const_condition(trigger
);
2264 switch (lttng_condition_get_type(condition
)) {
2265 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2266 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2268 enum lttng_condition_status status
;
2270 status
= lttng_condition_session_rotation_get_session_name(
2271 condition
, &session_name
);
2272 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2273 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2284 trigger_list
= get_session_trigger_list(state
, session_name
);
2285 if (!trigger_list
) {
2286 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2292 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2294 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2299 /* Must be called with RCU read lock held. */
2301 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2302 struct notification_thread_state
*state
)
2305 struct cds_lfht_node
*node
;
2306 struct cds_lfht_iter iter
;
2307 struct channel_info
*channel
;
2309 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2311 struct lttng_trigger_list_element
*trigger_list_element
;
2312 struct lttng_channel_trigger_list
*trigger_list
;
2313 struct cds_lfht_iter lookup_iter
;
2315 if (!trigger_applies_to_channel(trigger
, channel
)) {
2319 cds_lfht_lookup(state
->channel_triggers_ht
,
2320 hash_channel_key(&channel
->key
),
2321 match_channel_trigger_list
,
2324 node
= cds_lfht_iter_get_node(&lookup_iter
);
2326 trigger_list
= caa_container_of(node
,
2327 struct lttng_channel_trigger_list
,
2328 channel_triggers_ht_node
);
2330 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2331 if (!trigger_list_element
) {
2335 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2336 trigger_list_element
->trigger
= trigger
;
2337 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2338 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2346 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2348 bool is_notify
= false;
2349 unsigned int i
, count
;
2350 enum lttng_action_status action_status
;
2351 const struct lttng_action
*action
=
2352 lttng_trigger_get_const_action(trigger
);
2353 enum lttng_action_type action_type
;
2356 action_type
= lttng_action_get_type(action
);
2357 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2360 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2364 action_status
= lttng_action_group_get_count(action
, &count
);
2365 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2367 for (i
= 0; i
< count
; i
++) {
2368 const struct lttng_action
*inner_action
=
2369 lttng_action_group_get_at_index(
2372 action_type
= lttng_action_get_type(inner_action
);
2373 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2383 static bool trigger_name_taken(struct notification_thread_state
*state
,
2384 const struct lttng_trigger
*trigger
)
2386 struct cds_lfht_iter iter
;
2389 * No duplicata is allowed in the triggers_by_name_uid_ht.
2390 * The match is done against the trigger name and uid.
2392 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2393 hash_trigger_by_name_uid(trigger
),
2394 match_trigger_by_name_uid
,
2397 return !!cds_lfht_iter_get_node(&iter
);
2401 enum lttng_error_code
generate_trigger_name(
2402 struct notification_thread_state
*state
,
2403 struct lttng_trigger
*trigger
, const char **name
)
2405 enum lttng_error_code ret_code
= LTTNG_OK
;
2407 enum lttng_trigger_status status
;
2410 const int ret
= lttng_trigger_generate_name(trigger
,
2411 state
->trigger_id
.name_offset
++);
2413 /* The only reason this can fail right now. */
2414 ret_code
= LTTNG_ERR_NOMEM
;
2418 status
= lttng_trigger_get_name(trigger
, name
);
2419 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2421 taken
= trigger_name_taken(state
, trigger
);
2422 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2428 * FIXME A client's credentials are not checked when registering a trigger.
2430 * The effects of this are benign since:
2431 * - The client will succeed in registering the trigger, as it is valid,
2432 * - The trigger will, internally, be bound to the channel/session,
2433 * - The notifications will not be sent since the client's credentials
2434 * are checked against the channel at that moment.
2436 * If this function returns a non-zero value, it means something is
2437 * fundamentally broken and the whole subsystem/thread will be torn down.
2439 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2443 int handle_notification_thread_command_register_trigger(
2444 struct notification_thread_state
*state
,
2445 struct lttng_trigger
*trigger
,
2446 enum lttng_error_code
*cmd_result
)
2449 struct lttng_condition
*condition
;
2450 struct notification_client
*client
;
2451 struct notification_client_list
*client_list
= NULL
;
2452 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2453 struct notification_client_list_element
*client_list_element
;
2454 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2455 struct cds_lfht_node
*node
;
2456 struct cds_lfht_iter iter
;
2457 const char* trigger_name
;
2458 bool free_trigger
= true;
2459 struct lttng_evaluation
*evaluation
= NULL
;
2460 struct lttng_credentials object_creds
;
2463 enum action_executor_status executor_status
;
2464 const uint64_t trigger_tracer_token
=
2465 state
->trigger_id
.next_tracer_token
++;
2469 /* Set the trigger's tracer token. */
2470 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2472 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2473 LTTNG_TRIGGER_STATUS_UNSET
) {
2474 const enum lttng_error_code ret_code
= generate_trigger_name(
2475 state
, trigger
, &trigger_name
);
2477 if (ret_code
!= LTTNG_OK
) {
2480 *cmd_result
= ret_code
;
2483 } else if (trigger_name_taken(state
, trigger
)) {
2484 /* Not a fatal error. */
2485 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2490 condition
= lttng_trigger_get_condition(trigger
);
2493 /* Some conditions require tracers to implement a minimal ABI version. */
2494 if (!condition_is_supported(condition
)) {
2495 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2499 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2500 if (!trigger_ht_element
) {
2505 /* Add trigger to the trigger_ht. */
2506 cds_lfht_node_init(&trigger_ht_element
->node
);
2507 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2508 trigger_ht_element
->trigger
= trigger
;
2510 node
= cds_lfht_add_unique(state
->triggers_ht
,
2511 lttng_condition_hash(condition
),
2514 &trigger_ht_element
->node
);
2515 if (node
!= &trigger_ht_element
->node
) {
2516 /* Not a fatal error, simply report it to the client. */
2517 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2518 goto error_free_ht_element
;
2521 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2522 hash_trigger_by_name_uid(trigger
),
2523 match_trigger_by_name_uid
,
2525 &trigger_ht_element
->node_by_name_uid
);
2526 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2527 /* Not a fatal error, simply report it to the client. */
2528 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2529 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2530 goto error_free_ht_element
;
2533 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2534 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2535 if (!trigger_tokens_ht_element
) {
2538 cds_lfht_del(state
->triggers_ht
,
2539 &trigger_ht_element
->node
);
2540 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2541 &trigger_ht_element
->node_by_name_uid
);
2542 goto error_free_ht_element
;
2545 /* Add trigger token to the trigger_tokens_ht. */
2546 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2547 trigger_tokens_ht_element
->token
=
2548 LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2549 trigger_tokens_ht_element
->trigger
= trigger
;
2551 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2552 hash_key_u64(&trigger_tokens_ht_element
->token
,
2554 match_trigger_token
,
2555 &trigger_tokens_ht_element
->token
,
2556 &trigger_tokens_ht_element
->node
);
2557 if (node
!= &trigger_tokens_ht_element
->node
) {
2558 /* Internal corruption, fatal error. */
2560 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2561 cds_lfht_del(state
->triggers_ht
,
2562 &trigger_ht_element
->node
);
2563 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2564 &trigger_ht_element
->node_by_name_uid
);
2565 goto error_free_ht_element
;
2570 * Ownership of the trigger and of its wrapper was transfered to
2571 * the triggers_ht. Same for token ht element if necessary.
2573 trigger_tokens_ht_element
= NULL
;
2574 trigger_ht_element
= NULL
;
2575 free_trigger
= false;
2578 * The rest only applies to triggers that have a "notify" action.
2579 * It is not skipped as this is the only action type currently
2582 if (is_trigger_action_notify(trigger
)) {
2583 client_list
= notification_client_list_create(trigger
);
2586 goto error_free_ht_element
;
2589 /* Build a list of clients to which this new trigger applies. */
2590 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2591 client_socket_ht_node
) {
2592 if (!trigger_applies_to_client(trigger
, client
)) {
2596 client_list_element
=
2597 zmalloc(sizeof(*client_list_element
));
2598 if (!client_list_element
) {
2600 goto error_put_client_list
;
2603 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2604 client_list_element
->client
= client
;
2605 cds_list_add(&client_list_element
->node
,
2606 &client_list
->list
);
2610 * Client list ownership transferred to the
2611 * notification_trigger_clients_ht.
2613 publish_notification_client_list(state
, client_list
);
2616 switch (get_condition_binding_object(condition
)) {
2617 case LTTNG_OBJECT_TYPE_SESSION
:
2618 /* Add the trigger to the list if it matches a known session. */
2619 ret
= bind_trigger_to_matching_session(trigger
, state
);
2621 goto error_put_client_list
;
2624 case LTTNG_OBJECT_TYPE_CHANNEL
:
2626 * Add the trigger to list of triggers bound to the channels
2629 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2631 goto error_put_client_list
;
2634 case LTTNG_OBJECT_TYPE_NONE
:
2637 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2639 goto error_put_client_list
;
2643 * The new trigger's condition must be evaluated against the current
2646 * In the case of `notify` action, nothing preventing clients from
2647 * subscribing to a condition before the corresponding trigger is
2648 * registered, we have to evaluate this new condition right away.
2650 * At some point, we were waiting for the next "evaluation" (e.g. on
2651 * reception of a channel sample) to evaluate this new condition, but
2654 * The reason it was broken is that waiting for the next sample
2655 * does not allow us to properly handle transitions for edge-triggered
2658 * Consider this example: when we handle a new channel sample, we
2659 * evaluate each conditions twice: once with the previous state, and
2660 * again with the newest state. We then use those two results to
2661 * determine whether a state change happened: a condition was false and
2662 * became true. If a state change happened, we have to notify clients.
2664 * Now, if a client subscribes to a given notification and registers
2665 * a trigger *after* that subscription, we have to make sure the
2666 * condition is evaluated at this point while considering only the
2667 * current state. Otherwise, the next evaluation cycle may only see
2668 * that the evaluations remain the same (true for samples n-1 and n) and
2669 * the client will never know that the condition has been met.
2671 switch (get_condition_binding_object(condition
)) {
2672 case LTTNG_OBJECT_TYPE_SESSION
:
2673 ret
= evaluate_session_condition_for_client(condition
, state
,
2674 &evaluation
, &object_uid
,
2676 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2677 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2679 case LTTNG_OBJECT_TYPE_CHANNEL
:
2680 ret
= evaluate_channel_condition_for_client(condition
, state
,
2681 &evaluation
, &object_uid
,
2683 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2684 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2686 case LTTNG_OBJECT_TYPE_NONE
:
2689 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2697 goto error_put_client_list
;
2700 DBG("Newly registered trigger's condition evaluated to %s",
2701 evaluation
? "true" : "false");
2703 /* Evaluation yielded nothing. Normal exit. */
2709 * Ownership of `evaluation` transferred to the action executor
2710 * no matter the result.
2712 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2713 evaluation
, &object_creds
, client_list
);
2715 switch (executor_status
) {
2716 case ACTION_EXECUTOR_STATUS_OK
:
2718 case ACTION_EXECUTOR_STATUS_ERROR
:
2719 case ACTION_EXECUTOR_STATUS_INVALID
:
2721 * TODO Add trigger identification (name/id) when
2722 * it is added to the API.
2724 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2726 goto error_put_client_list
;
2727 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2729 * TODO Add trigger identification (name/id) when
2730 * it is added to the API.
2732 * Not a fatal error.
2734 WARN("No space left when enqueuing action associated to newly registered trigger");
2742 *cmd_result
= LTTNG_OK
;
2743 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2744 trigger_name
, trigger_tracer_token
);
2746 error_put_client_list
:
2747 notification_client_list_put(client_list
);
2749 error_free_ht_element
:
2750 if (trigger_ht_element
) {
2751 /* Delayed removal due to RCU constraint on delete. */
2752 call_rcu(&trigger_ht_element
->rcu_node
,
2753 free_lttng_trigger_ht_element_rcu
);
2756 free(trigger_tokens_ht_element
);
2759 lttng_trigger_destroy(trigger
);
2766 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2768 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2773 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2775 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2780 int handle_notification_thread_command_unregister_trigger(
2781 struct notification_thread_state
*state
,
2782 const struct lttng_trigger
*trigger
,
2783 enum lttng_error_code
*_cmd_reply
)
2785 struct cds_lfht_iter iter
;
2786 struct cds_lfht_node
*triggers_ht_node
;
2787 struct lttng_channel_trigger_list
*trigger_list
;
2788 struct notification_client_list
*client_list
;
2789 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2790 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
2792 enum lttng_error_code cmd_reply
;
2796 cds_lfht_lookup(state
->triggers_ht
,
2797 lttng_condition_hash(condition
),
2801 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2802 if (!triggers_ht_node
) {
2803 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2806 cmd_reply
= LTTNG_OK
;
2809 /* Remove trigger from channel_triggers_ht. */
2810 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2811 channel_triggers_ht_node
) {
2812 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2814 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2815 &trigger_list
->list
, node
) {
2816 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2820 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2821 cds_list_del(&trigger_element
->node
);
2822 /* A trigger can only appear once per channel */
2827 if (lttng_condition_get_type(condition
) ==
2828 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2829 struct notification_trigger_tokens_ht_element
2830 *trigger_tokens_ht_element
;
2832 cds_lfht_for_each_entry (state
->trigger_tokens_ht
, &iter
,
2833 trigger_tokens_ht_element
, node
) {
2834 if (!lttng_trigger_is_equal(trigger
,
2835 trigger_tokens_ht_element
->trigger
)) {
2839 DBG("[notification-thread] Removed trigger from tokens_ht");
2840 cds_lfht_del(state
->trigger_tokens_ht
,
2841 &trigger_tokens_ht_element
->node
);
2842 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2843 free_notification_trigger_tokens_ht_element_rcu
);
2849 if (is_trigger_action_notify(trigger
)) {
2851 * Remove and release the client list from
2852 * notification_trigger_clients_ht.
2854 client_list
= get_client_list_from_condition(state
, condition
);
2855 assert(client_list
);
2857 /* Put new reference and the hashtable's reference. */
2858 notification_client_list_put(client_list
);
2859 notification_client_list_put(client_list
);
2863 /* Remove trigger from triggers_ht. */
2864 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2865 struct lttng_trigger_ht_element
, node
);
2866 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2867 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2869 /* Release the ownership of the trigger. */
2870 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2871 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2875 *_cmd_reply
= cmd_reply
;
2880 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2881 int handle_notification_thread_command(
2882 struct notification_thread_handle
*handle
,
2883 struct notification_thread_state
*state
)
2887 struct notification_thread_command
*cmd
;
2889 /* Read the event pipe to put it back into a quiescent state. */
2890 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2892 if (ret
!= sizeof(counter
)) {
2896 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2897 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2898 struct notification_thread_command
, cmd_list_node
);
2899 cds_list_del(&cmd
->cmd_list_node
);
2900 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2902 DBG("[notification-thread] Received `%s` command",
2903 notification_command_type_str(cmd
->type
));
2904 switch (cmd
->type
) {
2905 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2906 ret
= handle_notification_thread_command_register_trigger(state
,
2907 cmd
->parameters
.register_trigger
.trigger
,
2910 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2911 ret
= handle_notification_thread_command_unregister_trigger(
2913 cmd
->parameters
.unregister_trigger
.trigger
,
2916 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2917 ret
= handle_notification_thread_command_add_channel(
2919 cmd
->parameters
.add_channel
.session
.name
,
2920 cmd
->parameters
.add_channel
.session
.uid
,
2921 cmd
->parameters
.add_channel
.session
.gid
,
2922 cmd
->parameters
.add_channel
.channel
.name
,
2923 cmd
->parameters
.add_channel
.channel
.domain
,
2924 cmd
->parameters
.add_channel
.channel
.key
,
2925 cmd
->parameters
.add_channel
.channel
.capacity
,
2928 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2929 ret
= handle_notification_thread_command_remove_channel(
2930 state
, cmd
->parameters
.remove_channel
.key
,
2931 cmd
->parameters
.remove_channel
.domain
,
2934 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2935 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2936 ret
= handle_notification_thread_command_session_rotation(
2939 cmd
->parameters
.session_rotation
.session_name
,
2940 cmd
->parameters
.session_rotation
.uid
,
2941 cmd
->parameters
.session_rotation
.gid
,
2942 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2943 cmd
->parameters
.session_rotation
.location
,
2946 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
2947 ret
= handle_notification_thread_command_add_tracer_event_source(
2949 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2950 cmd
->parameters
.tracer_event_source
.domain
,
2953 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
2954 ret
= handle_notification_thread_command_remove_tracer_event_source(
2956 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2959 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2961 struct lttng_triggers
*triggers
= NULL
;
2963 ret
= handle_notification_thread_command_list_triggers(
2966 cmd
->parameters
.list_triggers
.uid
,
2969 cmd
->reply
.list_triggers
.triggers
= triggers
;
2973 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2974 cmd
->reply_code
= LTTNG_OK
;
2977 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2979 const enum client_transmission_status client_status
=
2980 cmd
->parameters
.client_communication_update
2982 const notification_client_id client_id
=
2983 cmd
->parameters
.client_communication_update
.id
;
2984 struct notification_client
*client
;
2987 client
= get_client_from_id(client_id
, state
);
2991 * Client error was probably already picked-up by the
2992 * notification thread or it has disconnected
2993 * gracefully while this command was queued.
2995 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2999 ret
= client_handle_transmission_status(
3000 client
, client_status
, state
);
3006 ERR("[notification-thread] Unknown internal command received");
3014 if (cmd
->is_async
) {
3018 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3022 /* Wake-up and return a fatal error to the calling thread. */
3023 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3024 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3026 /* Indicate a fatal error to the caller. */
3031 int socket_set_non_blocking(int socket
)
3035 /* Set the pipe as non-blocking. */
3036 ret
= fcntl(socket
, F_GETFL
, 0);
3038 PERROR("fcntl get socket flags");
3043 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3045 PERROR("fcntl set O_NONBLOCK socket flag");
3048 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3054 int client_reset_inbound_state(struct notification_client
*client
)
3059 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3061 client
->communication
.inbound
.bytes_to_receive
=
3062 sizeof(struct lttng_notification_channel_message
);
3063 client
->communication
.inbound
.msg_type
=
3064 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3065 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3066 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3067 ret
= lttng_dynamic_buffer_set_size(
3068 &client
->communication
.inbound
.payload
.buffer
,
3069 client
->communication
.inbound
.bytes_to_receive
);
3074 int handle_notification_thread_client_connect(
3075 struct notification_thread_state
*state
)
3078 struct notification_client
*client
;
3080 DBG("[notification-thread] Handling new notification channel client connection");
3082 client
= zmalloc(sizeof(*client
));
3089 pthread_mutex_init(&client
->lock
, NULL
);
3090 client
->id
= state
->next_notification_client_id
++;
3091 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3092 lttng_payload_init(&client
->communication
.inbound
.payload
);
3093 lttng_payload_init(&client
->communication
.outbound
.payload
);
3094 client
->communication
.inbound
.expect_creds
= true;
3096 ret
= client_reset_inbound_state(client
);
3098 ERR("[notification-thread] Failed to reset client communication's inbound state");
3103 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3105 ERR("[notification-thread] Failed to accept new notification channel client connection");
3110 client
->socket
= ret
;
3112 ret
= socket_set_non_blocking(client
->socket
);
3114 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3118 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3120 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3125 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3126 LPOLLIN
| LPOLLERR
|
3127 LPOLLHUP
| LPOLLRDHUP
);
3129 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3133 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3137 cds_lfht_add(state
->client_socket_ht
,
3138 hash_client_socket(client
->socket
),
3139 &client
->client_socket_ht_node
);
3140 cds_lfht_add(state
->client_id_ht
,
3141 hash_client_id(client
->id
),
3142 &client
->client_id_ht_node
);
3148 notification_client_destroy(client
, state
);
3153 * RCU read-lock must be held by the caller.
3154 * Client lock must _not_ be held by the caller.
3157 int notification_thread_client_disconnect(
3158 struct notification_client
*client
,
3159 struct notification_thread_state
*state
)
3162 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3164 /* Acquire the client lock to disable its communication atomically. */
3165 pthread_mutex_lock(&client
->lock
);
3166 client
->communication
.active
= false;
3167 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3168 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3169 pthread_mutex_unlock(&client
->lock
);
3171 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3173 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3177 /* Release all conditions to which the client was subscribed. */
3178 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3179 &client
->condition_list
, node
) {
3180 (void) notification_thread_client_unsubscribe(client
,
3181 condition_list_element
->condition
, state
, NULL
);
3185 * Client no longer accessible to other threads (through the
3188 notification_client_destroy(client
, state
);
3192 int handle_notification_thread_client_disconnect(
3193 int client_socket
, struct notification_thread_state
*state
)
3196 struct notification_client
*client
;
3199 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3201 client
= get_client_from_socket(client_socket
, state
);
3203 /* Internal state corruption, fatal error. */
3204 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3210 ret
= notification_thread_client_disconnect(client
, state
);
3216 int handle_notification_thread_client_disconnect_all(
3217 struct notification_thread_state
*state
)
3219 struct cds_lfht_iter iter
;
3220 struct notification_client
*client
;
3221 bool error_encoutered
= false;
3224 DBG("[notification-thread] Closing all client connections");
3225 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3226 client_socket_ht_node
) {
3229 ret
= notification_thread_client_disconnect(
3232 error_encoutered
= true;
3236 return error_encoutered
? 1 : 0;
3239 int handle_notification_thread_trigger_unregister_all(
3240 struct notification_thread_state
*state
)
3242 bool error_occurred
= false;
3243 struct cds_lfht_iter iter
;
3244 struct lttng_trigger_ht_element
*trigger_ht_element
;
3247 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3249 int ret
= handle_notification_thread_command_unregister_trigger(
3250 state
, trigger_ht_element
->trigger
, NULL
);
3252 error_occurred
= true;
3256 return error_occurred
? -1 : 0;
3260 int client_handle_transmission_status(
3261 struct notification_client
*client
,
3262 enum client_transmission_status transmission_status
,
3263 struct notification_thread_state
*state
)
3267 switch (transmission_status
) {
3268 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3269 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3270 CLIENT_POLL_MASK_IN
);
3276 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3278 * We want to be notified whenever there is buffer space
3279 * available to send the rest of the payload.
3281 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3282 CLIENT_POLL_MASK_IN_OUT
);
3287 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3288 ret
= notification_thread_client_disconnect(client
, state
);
3293 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3303 /* Client lock must be acquired by caller. */
3305 enum client_transmission_status
client_flush_outgoing_queue(
3306 struct notification_client
*client
)
3309 size_t to_send_count
;
3310 enum client_transmission_status status
;
3311 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3312 &client
->communication
.outbound
.payload
, 0, -1);
3313 const int fds_to_send_count
=
3314 lttng_payload_view_get_fd_handle_count(&pv
);
3316 ASSERT_LOCKED(client
->lock
);
3318 if (!client
->communication
.active
) {
3319 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3323 if (pv
.buffer
.size
== 0) {
3325 * If both data and fds are equal to zero, we are in an invalid
3328 assert(fds_to_send_count
!= 0);
3333 to_send_count
= pv
.buffer
.size
;
3334 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3337 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3340 if ((ret
>= 0 && ret
< to_send_count
)) {
3341 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3343 to_send_count
-= max(ret
, 0);
3345 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3347 pv
.buffer
.size
- to_send_count
,
3349 ret
= lttng_dynamic_buffer_set_size(
3350 &client
->communication
.outbound
.payload
.buffer
,
3356 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3358 } else if (ret
< 0) {
3359 /* Generic error, disable the client's communication. */
3360 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3362 client
->communication
.active
= false;
3363 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3367 * No error and flushed the queue completely.
3369 * The payload buffer size is used later to
3370 * check if there is notifications queued. So albeit that the
3371 * direct caller knows that the transmission is complete, we
3372 * need to set the buffer size to zero.
3374 ret
= lttng_dynamic_buffer_set_size(
3375 &client
->communication
.outbound
.payload
.buffer
, 0);
3382 /* No fds to send, transmission is complete. */
3383 if (fds_to_send_count
== 0) {
3384 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3388 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3389 client
->socket
, &pv
);
3391 /* Generic error, disable the client's communication. */
3392 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3394 client
->communication
.active
= false;
3395 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3397 } else if (ret
== 0) {
3398 /* Nothing could be sent. */
3399 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3401 /* Fd passing is an all or nothing kind of thing. */
3402 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3404 * The payload _fd_array count is used later to
3405 * check if there is notifications queued. So although the
3406 * direct caller knows that the transmission is complete, we
3407 * need to clear the _fd_array for the queuing check.
3409 lttng_dynamic_pointer_array_clear(
3410 &client
->communication
.outbound
.payload
3415 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3416 client
->communication
.outbound
.queued_command_reply
= false;
3417 client
->communication
.outbound
.dropped_notification
= false;
3418 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3423 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3427 bool client_has_outbound_data_left(
3428 const struct notification_client
*client
)
3430 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3431 &client
->communication
.outbound
.payload
, 0, -1);
3432 const bool has_data
= pv
.buffer
.size
!= 0;
3433 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3435 return has_data
|| has_fds
;
3438 /* Client lock must _not_ be held by the caller. */
3440 int client_send_command_reply(struct notification_client
*client
,
3441 struct notification_thread_state
*state
,
3442 enum lttng_notification_channel_status status
)
3445 struct lttng_notification_channel_command_reply reply
= {
3446 .status
= (int8_t) status
,
3448 struct lttng_notification_channel_message msg
= {
3449 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3450 .size
= sizeof(reply
),
3452 char buffer
[sizeof(msg
) + sizeof(reply
)];
3453 enum client_transmission_status transmission_status
;
3455 memcpy(buffer
, &msg
, sizeof(msg
));
3456 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3457 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3459 pthread_mutex_lock(&client
->lock
);
3460 if (client
->communication
.outbound
.queued_command_reply
) {
3461 /* Protocol error. */
3465 /* Enqueue buffer to outgoing queue and flush it. */
3466 ret
= lttng_dynamic_buffer_append(
3467 &client
->communication
.outbound
.payload
.buffer
,
3468 buffer
, sizeof(buffer
));
3473 transmission_status
= client_flush_outgoing_queue(client
);
3475 if (client_has_outbound_data_left(client
)) {
3476 /* Queue could not be emptied. */
3477 client
->communication
.outbound
.queued_command_reply
= true;
3480 pthread_mutex_unlock(&client
->lock
);
3481 ret
= client_handle_transmission_status(
3482 client
, transmission_status
, state
);
3489 pthread_mutex_unlock(&client
->lock
);
3495 int client_handle_message_unknown(struct notification_client
*client
,
3496 struct notification_thread_state
*state
)
3500 * Receiving message header. The function will be called again
3501 * once the rest of the message as been received and can be
3504 const struct lttng_notification_channel_message
*msg
;
3506 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3507 msg
= (const struct lttng_notification_channel_message
*)
3508 client
->communication
.inbound
.payload
.buffer
.data
;
3510 if (msg
->size
== 0 ||
3511 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3512 ERR("[notification-thread] Invalid notification channel message: length = %u",
3518 switch (msg
->type
) {
3519 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3520 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3521 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3525 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3529 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3530 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3531 client
->communication
.inbound
.msg_type
=
3532 (enum lttng_notification_channel_message_type
) msg
->type
;
3533 ret
= lttng_dynamic_buffer_set_size(
3534 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3536 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3543 int client_handle_message_handshake(struct notification_client
*client
,
3544 struct notification_thread_state
*state
)
3547 struct lttng_notification_channel_command_handshake
*handshake_client
;
3548 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3549 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3550 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3552 const struct lttng_notification_channel_message msg_header
= {
3553 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3554 .size
= sizeof(handshake_reply
),
3556 enum lttng_notification_channel_status status
=
3557 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3558 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3560 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3561 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3562 sizeof(handshake_reply
));
3565 (struct lttng_notification_channel_command_handshake
*)
3566 client
->communication
.inbound
.payload
.buffer
3568 client
->major
= handshake_client
->major
;
3569 client
->minor
= handshake_client
->minor
;
3570 if (!client
->communication
.inbound
.creds_received
) {
3571 ERR("[notification-thread] No credentials received from client");
3576 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3577 &client
->communication
.inbound
.creds
);
3578 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3579 &client
->communication
.inbound
.creds
);
3580 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3581 client
->uid
, client
->gid
, (int) client
->major
,
3582 (int) client
->minor
);
3584 if (handshake_client
->major
!=
3585 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3586 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3589 pthread_mutex_lock(&client
->lock
);
3590 /* Outgoing queue will be flushed when the command reply is sent. */
3591 ret
= lttng_dynamic_buffer_append(
3592 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3593 sizeof(send_buffer
));
3595 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3599 client
->validated
= true;
3600 client
->communication
.active
= true;
3601 pthread_mutex_unlock(&client
->lock
);
3603 /* Set reception state to receive the next message header. */
3604 ret
= client_reset_inbound_state(client
);
3606 ERR("[notification-thread] Failed to reset client communication's inbound state");
3610 /* Flushes the outgoing queue. */
3611 ret
= client_send_command_reply(client
, state
, status
);
3613 ERR("[notification-thread] Failed to send reply to notification channel client");
3619 pthread_mutex_unlock(&client
->lock
);
3625 int client_handle_message_subscription(
3626 struct notification_client
*client
,
3627 enum lttng_notification_channel_message_type msg_type
,
3628 struct notification_thread_state
*state
)
3631 struct lttng_condition
*condition
;
3632 enum lttng_notification_channel_status status
=
3633 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3634 struct lttng_payload_view condition_view
=
3635 lttng_payload_view_from_payload(
3636 &client
->communication
.inbound
.payload
,
3638 size_t expected_condition_size
;
3641 * No need to lock client to sample the inbound state as the only
3642 * other thread accessing clients (action executor) only uses the
3645 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3646 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3647 if (ret
!= expected_condition_size
) {
3648 ERR("[notification-thread] Malformed condition received from client");
3652 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3653 ret
= notification_thread_client_subscribe(
3654 client
, condition
, state
, &status
);
3656 ret
= notification_thread_client_unsubscribe(
3657 client
, condition
, state
, &status
);
3664 /* Set reception state to receive the next message header. */
3665 ret
= client_reset_inbound_state(client
);
3667 ERR("[notification-thread] Failed to reset client communication's inbound state");
3671 ret
= client_send_command_reply(client
, state
, status
);
3673 ERR("[notification-thread] Failed to send reply to notification channel client");
3682 int client_dispatch_message(struct notification_client
*client
,
3683 struct notification_thread_state
*state
)
3687 if (client
->communication
.inbound
.msg_type
!=
3688 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3689 client
->communication
.inbound
.msg_type
!=
3690 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3691 !client
->validated
) {
3692 WARN("[notification-thread] client attempted a command before handshake");
3697 switch (client
->communication
.inbound
.msg_type
) {
3698 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3700 ret
= client_handle_message_unknown(client
, state
);
3703 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3705 ret
= client_handle_message_handshake(client
, state
);
3708 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3709 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3711 ret
= client_handle_message_subscription(client
,
3712 client
->communication
.inbound
.msg_type
, state
);
3722 /* Incoming data from client. */
3723 int handle_notification_thread_client_in(
3724 struct notification_thread_state
*state
, int socket
)
3727 struct notification_client
*client
;
3732 client
= get_client_from_socket(socket
, state
);
3734 /* Internal error, abort. */
3739 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3740 client
->communication
.inbound
.bytes_to_receive
;
3741 if (client
->communication
.inbound
.expect_creds
) {
3742 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3743 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3744 client
->communication
.inbound
.bytes_to_receive
,
3745 &client
->communication
.inbound
.creds
);
3747 client
->communication
.inbound
.expect_creds
= false;
3748 client
->communication
.inbound
.creds_received
= true;
3751 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3752 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3753 client
->communication
.inbound
.bytes_to_receive
);
3755 if (recv_ret
>= 0) {
3756 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3758 goto error_disconnect_client
;
3761 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3762 /* Message incomplete wait for more data. */
3767 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3770 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3771 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3773 client
->communication
.inbound
.fds_to_receive
,
3774 &client
->communication
.inbound
.payload
);
3777 * Fds received. non blocking fds passing is all
3780 ssize_t expected_size
;
3782 expected_size
= sizeof(int) *
3783 client
->communication
.inbound
3785 assert(ret
== expected_size
);
3786 client
->communication
.inbound
.fds_to_receive
= 0;
3787 } else if (ret
== 0) {
3788 /* Received nothing. */
3792 goto error_disconnect_client
;
3796 /* At this point the message is complete.*/
3797 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3798 client
->communication
.inbound
.fds_to_receive
== 0);
3799 ret
= client_dispatch_message(client
, state
);
3802 * Only returns an error if this client must be
3805 goto error_disconnect_client
;
3812 error_disconnect_client
:
3813 ret
= notification_thread_client_disconnect(client
, state
);
3817 /* Client ready to receive outgoing data. */
3818 int handle_notification_thread_client_out(
3819 struct notification_thread_state
*state
, int socket
)
3822 struct notification_client
*client
;
3823 enum client_transmission_status transmission_status
;
3826 client
= get_client_from_socket(socket
, state
);
3828 /* Internal error, abort. */
3833 pthread_mutex_lock(&client
->lock
);
3834 transmission_status
= client_flush_outgoing_queue(client
);
3835 pthread_mutex_unlock(&client
->lock
);
3837 ret
= client_handle_transmission_status(
3838 client
, transmission_status
, state
);
3848 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3849 const struct channel_state_sample
*sample
,
3850 uint64_t buffer_capacity
)
3852 bool result
= false;
3854 enum lttng_condition_type condition_type
;
3855 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3856 condition
, struct lttng_condition_buffer_usage
,
3859 if (use_condition
->threshold_bytes
.set
) {
3860 threshold
= use_condition
->threshold_bytes
.value
;
3863 * Threshold was expressed as a ratio.
3865 * TODO the threshold (in bytes) of conditions expressed
3866 * as a ratio of total buffer size could be cached to
3867 * forego this double-multiplication or it could be performed
3868 * as fixed-point math.
3870 * Note that caching should accommodates the case where the
3871 * condition applies to multiple channels (i.e. don't assume
3872 * that all channels matching my_chann* have the same size...)
3874 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3875 (double) buffer_capacity
);
3878 condition_type
= lttng_condition_get_type(condition
);
3879 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3880 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3881 threshold
, sample
->highest_usage
);
3884 * The low condition should only be triggered once _all_ of the
3885 * streams in a channel have gone below the "low" threshold.
3887 if (sample
->highest_usage
<= threshold
) {
3891 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3892 threshold
, sample
->highest_usage
);
3895 * For high buffer usage scenarios, we want to trigger whenever
3896 * _any_ of the streams has reached the "high" threshold.
3898 if (sample
->highest_usage
>= threshold
) {
3907 bool evaluate_session_consumed_size_condition(
3908 const struct lttng_condition
*condition
,
3909 uint64_t session_consumed_size
)
3912 const struct lttng_condition_session_consumed_size
*size_condition
=
3913 container_of(condition
,
3914 struct lttng_condition_session_consumed_size
,
3917 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3918 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3919 threshold
, session_consumed_size
);
3920 return session_consumed_size
>= threshold
;
3924 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3925 struct lttng_evaluation
**evaluation
,
3926 const struct notification_thread_state
*state
,
3927 const struct channel_state_sample
*previous_sample
,
3928 const struct channel_state_sample
*latest_sample
,
3929 uint64_t previous_session_consumed_total
,
3930 uint64_t latest_session_consumed_total
,
3931 struct channel_info
*channel_info
)
3934 enum lttng_condition_type condition_type
;
3935 const bool previous_sample_available
= !!previous_sample
;
3936 bool previous_sample_result
= false;
3937 bool latest_sample_result
;
3939 condition_type
= lttng_condition_get_type(condition
);
3941 switch (condition_type
) {
3942 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3943 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3944 if (caa_likely(previous_sample_available
)) {
3945 previous_sample_result
=
3946 evaluate_buffer_usage_condition(condition
,
3947 previous_sample
, channel_info
->capacity
);
3949 latest_sample_result
= evaluate_buffer_usage_condition(
3950 condition
, latest_sample
,
3951 channel_info
->capacity
);
3953 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3954 if (caa_likely(previous_sample_available
)) {
3955 previous_sample_result
=
3956 evaluate_session_consumed_size_condition(
3958 previous_session_consumed_total
);
3960 latest_sample_result
=
3961 evaluate_session_consumed_size_condition(
3963 latest_session_consumed_total
);
3966 /* Unknown condition type; internal error. */
3970 if (!latest_sample_result
||
3971 (previous_sample_result
== latest_sample_result
)) {
3973 * Only trigger on a condition evaluation transition.
3975 * NOTE: This edge-triggered logic may not be appropriate for
3976 * future condition types.
3981 if (!evaluation
|| !latest_sample_result
) {
3985 switch (condition_type
) {
3986 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3987 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3988 *evaluation
= lttng_evaluation_buffer_usage_create(
3990 latest_sample
->highest_usage
,
3991 channel_info
->capacity
);
3993 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3994 *evaluation
= lttng_evaluation_session_consumed_size_create(
3995 latest_session_consumed_total
);
4010 int client_notification_overflow(struct notification_client
*client
)
4013 const struct lttng_notification_channel_message msg
= {
4014 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4017 ASSERT_LOCKED(client
->lock
);
4019 DBG("Dropping notification addressed to client (socket fd = %i)",
4021 if (client
->communication
.outbound
.dropped_notification
) {
4023 * The client already has a "notification dropped" message
4024 * in its outgoing queue. Nothing to do since all
4025 * of those messages are coalesced.
4030 client
->communication
.outbound
.dropped_notification
= true;
4031 ret
= lttng_dynamic_buffer_append(
4032 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4035 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4042 static int client_handle_transmission_status_wrapper(
4043 struct notification_client
*client
,
4044 enum client_transmission_status status
,
4047 return client_handle_transmission_status(client
, status
,
4048 (struct notification_thread_state
*) user_data
);
4052 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4053 const struct lttng_evaluation
*evaluation
,
4054 struct notification_client_list
* client_list
,
4055 struct notification_thread_state
*state
,
4056 uid_t object_uid
, gid_t object_gid
)
4058 const struct lttng_credentials creds
= {
4059 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4060 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4063 return notification_client_list_send_evaluation(client_list
,
4064 lttng_trigger_get_const_condition(trigger
), evaluation
,
4065 lttng_trigger_get_credentials(trigger
),
4067 client_handle_transmission_status_wrapper
, state
);
4071 * Permission checks relative to notification channel clients are performed
4072 * here. Notice how object, client, and trigger credentials are involved in
4075 * The `object` credentials are the credentials associated with the "subject"
4076 * of a condition. For instance, a `rotation completed` condition applies
4077 * to a session. When that condition is met, it will produce an evaluation
4078 * against a session. Hence, in this case, the `object` credentials are the
4079 * credentials of the "subject" session.
4081 * The `trigger` credentials are the credentials of the user that registered the
4084 * The `client` credentials are the credentials of the user that created a given
4085 * notification channel.
4087 * In terms of visibility, it is expected that non-privilieged users can only
4088 * register triggers against "their" objects (their own sessions and
4089 * applications they are allowed to interact with). They can then open a
4090 * notification channel and subscribe to notifications associated with those
4093 * As for privilieged users, they can register triggers against the objects of
4094 * other users. They can then subscribe to the notifications associated to their
4095 * triggers. Privilieged users _can't_ subscribe to the notifications of
4096 * triggers owned by other users; they must create their own triggers.
4098 * This is more a concern of usability than security. It would be difficult for
4099 * a root user reliably subscribe to a specific set of conditions without
4100 * interference from external users (those could, for instance, unregister
4104 int notification_client_list_send_evaluation(
4105 struct notification_client_list
*client_list
,
4106 const struct lttng_condition
*condition
,
4107 const struct lttng_evaluation
*evaluation
,
4108 const struct lttng_credentials
*trigger_creds
,
4109 const struct lttng_credentials
*source_object_creds
,
4110 report_client_transmission_result_cb client_report
,
4114 struct lttng_payload msg_payload
;
4115 struct notification_client_list_element
*client_list_element
, *tmp
;
4116 const struct lttng_notification notification
= {
4117 .condition
= (struct lttng_condition
*) condition
,
4118 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4120 struct lttng_notification_channel_message msg_header
= {
4121 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4124 lttng_payload_init(&msg_payload
);
4126 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4127 sizeof(msg_header
));
4132 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4134 ERR("[notification-thread] Failed to serialize notification");
4139 /* Update payload size. */
4140 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4141 ->size
= (uint32_t)(
4142 msg_payload
.buffer
.size
- sizeof(msg_header
));
4144 /* Update the payload number of fds. */
4146 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4147 &msg_payload
, 0, -1);
4149 ((struct lttng_notification_channel_message
*)
4150 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4151 lttng_payload_view_get_fd_handle_count(&pv
);
4154 pthread_mutex_lock(&client_list
->lock
);
4155 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4156 &client_list
->list
, node
) {
4157 enum client_transmission_status transmission_status
;
4158 struct notification_client
*client
=
4159 client_list_element
->client
;
4162 pthread_mutex_lock(&client
->lock
);
4163 if (!client
->communication
.active
) {
4165 * Skip inactive client (protocol error or
4168 DBG("Skipping client at it is marked as inactive");
4172 if (source_object_creds
) {
4173 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4174 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4177 * Client is not allowed to monitor this
4180 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4185 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
4186 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4190 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4191 client
->socket
, msg_payload
.buffer
.size
);
4193 if (client_has_outbound_data_left(client
)) {
4195 * Outgoing data is already buffered for this client;
4196 * drop the notification and enqueue a "dropped
4197 * notification" message if this is the first dropped
4198 * notification since the socket spilled-over to the
4201 ret
= client_notification_overflow(client
);
4208 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4214 transmission_status
= client_flush_outgoing_queue(client
);
4215 pthread_mutex_unlock(&client
->lock
);
4216 ret
= client_report(client
, transmission_status
, user_data
);
4219 goto end_unlock_list
;
4225 pthread_mutex_unlock(&client
->lock
);
4228 goto end_unlock_list
;
4234 pthread_mutex_unlock(&client_list
->lock
);
4236 lttng_payload_reset(&msg_payload
);
4241 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4242 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4246 struct lttng_event_notifier_notification
*notification
= NULL
;
4247 void *reception_buffer
;
4248 size_t reception_size
;
4250 struct lttng_ust_event_notifier_notification ust_notification
;
4251 struct lttng_kernel_event_notifier_notification kernel_notification
;
4253 /* Init lttng_event_notifier_notification */
4255 case LTTNG_DOMAIN_UST
:
4256 reception_buffer
= (void *) &ust_notification
;
4257 reception_size
= sizeof(ust_notification
);
4259 case LTTNG_DOMAIN_KERNEL
:
4260 reception_buffer
= (void *) &kernel_notification
;
4261 reception_size
= sizeof(kernel_notification
);
4268 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4269 * ensuring that read/write of tracer notifications are atomic.
4271 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4273 if (ret
!= reception_size
) {
4274 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4275 notification_pipe_read_fd
, reception_size
, ret
);
4281 case LTTNG_DOMAIN_UST
:
4282 token
= ust_notification
.token
;
4284 case LTTNG_DOMAIN_KERNEL
:
4285 token
= kernel_notification
.token
;
4291 notification
= lttng_event_notifier_notification_create(
4294 return notification
;
4298 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4299 struct lttng_event_notifier_notification
*notification
)
4301 struct cds_lfht_node
*node
;
4302 struct cds_lfht_iter iter
;
4303 struct notification_trigger_tokens_ht_element
*element
;
4304 enum lttng_trigger_status trigger_status
;
4305 struct lttng_evaluation
*evaluation
= NULL
;
4306 enum action_executor_status executor_status
;
4307 struct notification_client_list
*client_list
= NULL
;
4308 const char *trigger_name
;
4311 /* Find triggers associated with this token. */
4313 cds_lfht_lookup(state
->trigger_tokens_ht
,
4314 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4315 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4316 node
= cds_lfht_iter_get_node(&iter
);
4317 if (caa_unlikely(!node
)) {
4319 * This is not an error, slow consumption of the tracer
4320 * notifications can lead to situations where a trigger is
4321 * removed but we still get tracer notifications matching a
4322 * trigger that no longer exists.
4328 element
= caa_container_of(node
,
4329 struct notification_trigger_tokens_ht_element
,
4332 if (!lttng_trigger_should_fire(element
->trigger
)) {
4337 lttng_trigger_fire(element
->trigger
);
4339 trigger_status
= lttng_trigger_get_name(element
->trigger
, &trigger_name
);
4340 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4342 evaluation
= lttng_evaluation_event_rule_create(
4344 if (evaluation
== NULL
) {
4345 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
4350 client_list
= get_client_list_from_condition(state
,
4351 lttng_trigger_get_const_condition(element
->trigger
));
4352 executor_status
= action_executor_enqueue(state
->executor
,
4353 element
->trigger
, evaluation
, NULL
, client_list
);
4354 switch (executor_status
) {
4355 case ACTION_EXECUTOR_STATUS_OK
:
4358 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4360 struct notification_client_list_element
*client_list_element
,
4364 * Not a fatal error; this is expected and simply means the
4365 * executor has too much work queued already.
4369 /* No clients subscribed to notifications for this trigger. */
4374 /* Warn clients that a notification (or more) was dropped. */
4375 pthread_mutex_lock(&client_list
->lock
);
4376 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4377 &client_list
->list
, node
) {
4378 enum client_transmission_status transmission_status
;
4379 struct notification_client
*client
=
4380 client_list_element
->client
;
4382 pthread_mutex_lock(&client
->lock
);
4383 ret
= client_notification_overflow(client
);
4389 transmission_status
=
4390 client_flush_outgoing_queue(client
);
4391 ret
= client_handle_transmission_status(
4392 client
, transmission_status
, state
);
4398 pthread_mutex_unlock(&client
->lock
);
4404 pthread_mutex_unlock(&client_list
->lock
);
4407 case ACTION_EXECUTOR_STATUS_ERROR
:
4408 /* Fatal error, shut down everything. */
4409 ERR("Fatal error encoutered while enqueuing action to the action executor");
4413 /* Unhandled error. */
4418 notification_client_list_put(client_list
);
4424 int handle_one_event_notifier_notification(
4425 struct notification_thread_state
*state
,
4426 int pipe
, enum lttng_domain_type domain
)
4429 struct lttng_event_notifier_notification
*notification
= NULL
;
4431 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4432 if (notification
== NULL
) {
4433 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4434 pipe
, lttng_domain_type_str(domain
));
4439 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4441 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4442 pipe
, lttng_domain_type_str(domain
));
4447 lttng_event_notifier_notification_destroy(notification
);
4451 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4452 int pipe
, enum lttng_domain_type domain
)
4454 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4457 int handle_notification_thread_channel_sample(
4458 struct notification_thread_state
*state
, int pipe
,
4459 enum lttng_domain_type domain
)
4462 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4463 struct channel_info
*channel_info
;
4464 struct cds_lfht_node
*node
;
4465 struct cds_lfht_iter iter
;
4466 struct lttng_channel_trigger_list
*trigger_list
;
4467 struct lttng_trigger_list_element
*trigger_list_element
;
4468 bool previous_sample_available
= false;
4469 struct channel_state_sample previous_sample
, latest_sample
;
4470 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4471 struct lttng_credentials channel_creds
;
4474 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4475 * ensuring that read/write of sampling messages are atomic.
4477 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4478 if (ret
!= sizeof(sample_msg
)) {
4479 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4486 latest_sample
.key
.key
= sample_msg
.key
;
4487 latest_sample
.key
.domain
= domain
;
4488 latest_sample
.highest_usage
= sample_msg
.highest
;
4489 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4490 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4494 /* Retrieve the channel's informations */
4495 cds_lfht_lookup(state
->channels_ht
,
4496 hash_channel_key(&latest_sample
.key
),
4500 node
= cds_lfht_iter_get_node(&iter
);
4501 if (caa_unlikely(!node
)) {
4503 * Not an error since the consumer can push a sample to the pipe
4504 * and the rest of the session daemon could notify us of the
4505 * channel's destruction before we get a chance to process that
4508 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4509 latest_sample
.key
.key
,
4510 lttng_domain_type_str(domain
));
4513 channel_info
= caa_container_of(node
, struct channel_info
,
4515 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4517 latest_sample
.key
.key
,
4518 channel_info
->session_info
->name
,
4519 latest_sample
.highest_usage
,
4520 latest_sample
.lowest_usage
,
4521 latest_sample
.channel_total_consumed
);
4523 previous_session_consumed_total
=
4524 channel_info
->session_info
->consumed_data_size
;
4526 /* Retrieve the channel's last sample, if it exists, and update it. */
4527 cds_lfht_lookup(state
->channel_state_ht
,
4528 hash_channel_key(&latest_sample
.key
),
4529 match_channel_state_sample
,
4532 node
= cds_lfht_iter_get_node(&iter
);
4533 if (caa_likely(node
)) {
4534 struct channel_state_sample
*stored_sample
;
4536 /* Update the sample stored. */
4537 stored_sample
= caa_container_of(node
,
4538 struct channel_state_sample
,
4539 channel_state_ht_node
);
4541 memcpy(&previous_sample
, stored_sample
,
4542 sizeof(previous_sample
));
4543 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4544 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4545 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4546 previous_sample_available
= true;
4548 latest_session_consumed_total
=
4549 previous_session_consumed_total
+
4550 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4553 * This is the channel's first sample, allocate space for and
4554 * store the new sample.
4556 struct channel_state_sample
*stored_sample
;
4558 stored_sample
= zmalloc(sizeof(*stored_sample
));
4559 if (!stored_sample
) {
4564 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4565 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4566 cds_lfht_add(state
->channel_state_ht
,
4567 hash_channel_key(&stored_sample
->key
),
4568 &stored_sample
->channel_state_ht_node
);
4570 latest_session_consumed_total
=
4571 previous_session_consumed_total
+
4572 latest_sample
.channel_total_consumed
;
4575 channel_info
->session_info
->consumed_data_size
=
4576 latest_session_consumed_total
;
4578 /* Find triggers associated with this channel. */
4579 cds_lfht_lookup(state
->channel_triggers_ht
,
4580 hash_channel_key(&latest_sample
.key
),
4581 match_channel_trigger_list
,
4584 node
= cds_lfht_iter_get_node(&iter
);
4585 if (caa_likely(!node
)) {
4589 channel_creds
= (typeof(channel_creds
)) {
4590 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4591 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4594 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4595 channel_triggers_ht_node
);
4596 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4598 const struct lttng_condition
*condition
;
4599 struct lttng_trigger
*trigger
;
4600 struct notification_client_list
*client_list
= NULL
;
4601 struct lttng_evaluation
*evaluation
= NULL
;
4602 enum action_executor_status executor_status
;
4605 trigger
= trigger_list_element
->trigger
;
4606 condition
= lttng_trigger_get_const_condition(trigger
);
4610 * Check if any client is subscribed to the result of this
4613 client_list
= get_client_list_from_condition(state
, condition
);
4615 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4616 previous_sample_available
? &previous_sample
: NULL
,
4618 previous_session_consumed_total
,
4619 latest_session_consumed_total
,
4621 if (caa_unlikely(ret
)) {
4625 if (caa_likely(!evaluation
)) {
4629 if (!lttng_trigger_should_fire(trigger
)) {
4633 lttng_trigger_fire(trigger
);
4636 * Ownership of `evaluation` transferred to the action executor
4637 * no matter the result.
4639 executor_status
= action_executor_enqueue(state
->executor
,
4640 trigger
, evaluation
, &channel_creds
,
4643 switch (executor_status
) {
4644 case ACTION_EXECUTOR_STATUS_OK
:
4646 case ACTION_EXECUTOR_STATUS_ERROR
:
4647 case ACTION_EXECUTOR_STATUS_INVALID
:
4649 * TODO Add trigger identification (name/id) when
4650 * it is added to the API.
4652 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4655 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4657 * TODO Add trigger identification (name/id) when
4658 * it is added to the API.
4660 * Not a fatal error.
4662 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4670 notification_client_list_put(client_list
);
4671 if (caa_unlikely(ret
)) {