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.hpp"
12 #include <urcu/rculfhash.h>
14 #include <common/defaults.hpp>
15 #include <common/error.hpp>
16 #include <common/futex.hpp>
17 #include <common/unix.hpp>
18 #include <common/dynamic-buffer.hpp>
19 #include <common/hashtable/utils.hpp>
20 #include <common/sessiond-comm/sessiond-comm.hpp>
21 #include <common/macros.hpp>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.hpp>
24 #include <lttng/action/list-internal.hpp>
25 #include <lttng/domain-internal.hpp>
26 #include <lttng/notification/notification-internal.hpp>
27 #include <lttng/condition/condition-internal.hpp>
28 #include <lttng/condition/buffer-usage-internal.hpp>
29 #include <lttng/condition/session-consumed-size-internal.hpp>
30 #include <lttng/condition/session-rotation-internal.hpp>
31 #include <lttng/condition/event-rule-matches-internal.hpp>
32 #include <lttng/domain-internal.hpp>
33 #include <lttng/notification/channel-internal.hpp>
34 #include <lttng/trigger/trigger-internal.hpp>
35 #include <lttng/event-rule/event-rule-internal.hpp>
42 #include "condition-internal.hpp"
43 #include "event-notifier-error-accounting.hpp"
44 #include "notification-thread.hpp"
45 #include "notification-thread-events.hpp"
46 #include "notification-thread-commands.hpp"
47 #include "lttng-sessiond.hpp"
50 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
51 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
53 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
54 #define MAX_CAPTURE_SIZE (PIPE_BUF)
56 enum lttng_object_type
{
57 LTTNG_OBJECT_TYPE_UNKNOWN
,
58 LTTNG_OBJECT_TYPE_NONE
,
59 LTTNG_OBJECT_TYPE_CHANNEL
,
60 LTTNG_OBJECT_TYPE_SESSION
,
63 struct lttng_trigger_list_element
{
64 /* No ownership of the trigger object is assumed. */
65 struct lttng_trigger
*trigger
;
66 struct cds_list_head node
;
69 struct lttng_channel_trigger_list
{
70 struct channel_key channel_key
;
71 /* List of struct lttng_trigger_list_element. */
72 struct cds_list_head list
;
73 /* Node in the channel_triggers_ht */
74 struct cds_lfht_node channel_triggers_ht_node
;
75 /* call_rcu delayed reclaim. */
76 struct rcu_head rcu_node
;
80 * List of triggers applying to a given session.
83 * - lttng_session_trigger_list_create()
84 * - lttng_session_trigger_list_build()
85 * - lttng_session_trigger_list_destroy()
86 * - lttng_session_trigger_list_add()
88 struct lttng_session_trigger_list
{
90 * Not owned by this; points to the session_info structure's
93 const char *session_name
;
94 /* List of struct lttng_trigger_list_element. */
95 struct cds_list_head list
;
96 /* Node in the session_triggers_ht */
97 struct cds_lfht_node session_triggers_ht_node
;
99 * Weak reference to the notification system's session triggers
102 * The session trigger list structure structure is owned by
103 * the session's session_info.
105 * The session_info is kept alive the the channel_infos holding a
106 * reference to it (reference counting). When those channels are
107 * destroyed (at runtime or on teardown), the reference they hold
108 * to the session_info are released. On destruction of session_info,
109 * session_info_destroy() will remove the list of triggers applying
110 * to this session from the notification system's state.
112 * This implies that the session_triggers_ht must be destroyed
113 * after the channels.
115 struct cds_lfht
*session_triggers_ht
;
116 /* Used for delayed RCU reclaim. */
117 struct rcu_head rcu_node
;
120 struct lttng_trigger_ht_element
{
121 struct lttng_trigger
*trigger
;
122 struct cds_lfht_node node
;
123 struct cds_lfht_node node_by_name_uid
;
124 struct cds_list_head client_list_trigger_node
;
125 /* call_rcu delayed reclaim. */
126 struct rcu_head rcu_node
;
129 struct lttng_condition_list_element
{
130 struct lttng_condition
*condition
;
131 struct cds_list_head node
;
134 struct channel_state_sample
{
135 struct channel_key key
;
136 struct cds_lfht_node channel_state_ht_node
;
137 uint64_t highest_usage
;
138 uint64_t lowest_usage
;
139 uint64_t channel_total_consumed
;
140 /* call_rcu delayed reclaim. */
141 struct rcu_head rcu_node
;
144 static unsigned long hash_channel_key(struct channel_key
*key
);
145 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
146 struct lttng_evaluation
**evaluation
,
147 const struct notification_thread_state
*state
,
148 const struct channel_state_sample
*previous_sample
,
149 const struct channel_state_sample
*latest_sample
,
150 uint64_t previous_session_consumed_total
,
151 uint64_t latest_session_consumed_total
,
152 struct channel_info
*channel_info
);
154 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
155 const struct lttng_evaluation
*evaluation
,
156 struct notification_client_list
*client_list
,
157 struct notification_thread_state
*state
,
158 uid_t channel_uid
, gid_t channel_gid
);
161 /* session_info API */
163 void session_info_destroy(void *_data
);
165 void session_info_get(struct session_info
*session_info
);
167 void session_info_put(struct session_info
*session_info
);
169 struct session_info
*session_info_create(const char *name
,
170 uid_t uid
, gid_t gid
,
171 struct lttng_session_trigger_list
*trigger_list
,
172 struct cds_lfht
*sessions_ht
);
174 void session_info_add_channel(struct session_info
*session_info
,
175 struct channel_info
*channel_info
);
177 void session_info_remove_channel(struct session_info
*session_info
,
178 struct channel_info
*channel_info
);
180 /* lttng_session_trigger_list API */
182 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
183 const char *session_name
,
184 struct cds_lfht
*session_triggers_ht
);
186 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
187 const struct notification_thread_state
*state
,
188 const char *session_name
);
190 void lttng_session_trigger_list_destroy(
191 struct lttng_session_trigger_list
*list
);
193 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
194 struct lttng_trigger
*trigger
);
197 int client_handle_transmission_status(
198 struct notification_client
*client
,
199 enum client_transmission_status transmission_status
,
200 struct notification_thread_state
*state
);
203 int handle_one_event_notifier_notification(
204 struct notification_thread_state
*state
,
205 int pipe
, enum lttng_domain_type domain
);
208 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
211 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
213 /* This double-cast is intended to supress pointer-to-cast warning. */
214 const int socket
= (int) (intptr_t) key
;
215 const struct notification_client
*client
= caa_container_of(node
,
216 struct notification_client
, client_socket_ht_node
);
218 return client
->socket
== socket
;
222 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
224 /* This double-cast is intended to supress pointer-to-cast warning. */
225 const notification_client_id id
= *((notification_client_id
*) key
);
226 const struct notification_client
*client
= caa_container_of(
227 node
, struct notification_client
, client_id_ht_node
);
229 return client
->id
== id
;
233 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
235 struct channel_key
*channel_key
= (struct channel_key
*) key
;
236 struct lttng_channel_trigger_list
*trigger_list
;
238 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
239 channel_triggers_ht_node
);
241 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
242 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
246 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
248 const char *session_name
= (const char *) key
;
249 struct lttng_session_trigger_list
*trigger_list
;
251 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
252 session_triggers_ht_node
);
254 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
258 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
260 struct channel_key
*channel_key
= (struct channel_key
*) key
;
261 struct channel_state_sample
*sample
;
263 sample
= caa_container_of(node
, struct channel_state_sample
,
264 channel_state_ht_node
);
266 return !!((channel_key
->key
== sample
->key
.key
) &&
267 (channel_key
->domain
== sample
->key
.domain
));
271 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
273 struct channel_key
*channel_key
= (struct channel_key
*) key
;
274 struct channel_info
*channel_info
;
276 channel_info
= caa_container_of(node
, struct channel_info
,
279 return !!((channel_key
->key
== channel_info
->key
.key
) &&
280 (channel_key
->domain
== channel_info
->key
.domain
));
284 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
286 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
287 struct lttng_trigger_ht_element
*trigger_ht_element
;
289 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
292 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
296 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
298 const uint64_t *_key
= (uint64_t *) key
;
299 struct notification_trigger_tokens_ht_element
*element
;
301 element
= caa_container_of(node
,
302 struct notification_trigger_tokens_ht_element
, node
);
303 return *_key
== element
->token
;
307 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
309 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
310 struct notification_client_list
*client_list
;
311 const struct lttng_condition
*condition
;
313 LTTNG_ASSERT(condition_key
);
315 client_list
= caa_container_of(node
, struct notification_client_list
,
316 notification_trigger_clients_ht_node
);
317 condition
= client_list
->condition
;
319 return !!lttng_condition_is_equal(condition_key
, condition
);
323 int match_session(struct cds_lfht_node
*node
, const void *key
)
325 const char *name
= (const char *) key
;
326 struct session_info
*session_info
= caa_container_of(
327 node
, struct session_info
, sessions_ht_node
);
329 return !strcmp(session_info
->name
, name
);
333 const char *notification_command_type_str(
334 enum notification_thread_command_type type
)
337 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
338 return "REGISTER_TRIGGER";
339 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
340 return "UNREGISTER_TRIGGER";
341 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
342 return "ADD_CHANNEL";
343 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
344 return "REMOVE_CHANNEL";
345 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
346 return "SESSION_ROTATION_ONGOING";
347 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
348 return "SESSION_ROTATION_COMPLETED";
349 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
350 return "ADD_TRACER_EVENT_SOURCE";
351 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
352 return "REMOVE_TRACER_EVENT_SOURCE";
353 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
354 return "LIST_TRIGGERS";
355 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
356 return "GET_TRIGGER";
357 case NOTIFICATION_COMMAND_TYPE_QUIT
:
359 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
360 return "CLIENT_COMMUNICATION_UPDATE";
367 * Match trigger based on name and credentials only.
368 * Name duplication is NOT allowed for the same uid.
371 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
375 const char *element_trigger_name
;
376 const char *key_name
;
377 enum lttng_trigger_status status
;
378 const struct lttng_credentials
*key_creds
;
379 const struct lttng_credentials
*node_creds
;
380 const struct lttng_trigger
*trigger_key
=
381 (const struct lttng_trigger
*) key
;
382 const struct lttng_trigger_ht_element
*trigger_ht_element
=
383 caa_container_of(node
,
384 struct lttng_trigger_ht_element
,
387 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
,
388 &element_trigger_name
);
389 element_trigger_name
= status
== LTTNG_TRIGGER_STATUS_OK
?
390 element_trigger_name
: NULL
;
392 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
393 key_name
= status
== LTTNG_TRIGGER_STATUS_OK
? key_name
: NULL
;
397 * Consider null names as not equal. This is to maintain backwards
398 * compatibility with pre-2.13 anonymous triggers. Multiples anonymous
399 * triggers are allowed for a given user.
401 if (!element_trigger_name
|| !key_name
) {
405 if (strcmp(element_trigger_name
, key_name
) != 0) {
409 /* Compare the owners' UIDs. */
410 key_creds
= lttng_trigger_get_credentials(trigger_key
);
411 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
413 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
420 * Hash trigger based on name and credentials only.
423 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
425 unsigned long hash
= 0;
426 const struct lttng_credentials
*trigger_creds
;
427 const char *trigger_name
;
428 enum lttng_trigger_status status
;
430 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
431 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
432 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
435 trigger_creds
= lttng_trigger_get_credentials(trigger
);
436 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
443 unsigned long hash_channel_key(struct channel_key
*key
)
445 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
446 unsigned long domain_hash
= hash_key_ulong(
447 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
449 return key_hash
^ domain_hash
;
453 unsigned long hash_client_socket(int socket
)
455 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
459 unsigned long hash_client_id(notification_client_id id
)
461 return hash_key_u64(&id
, lttng_ht_seed
);
465 * Get the type of object to which a given condition applies. Bindings let
466 * the notification system evaluate a trigger's condition when a given
467 * object's state is updated.
469 * For instance, a condition bound to a channel will be evaluated everytime
470 * the channel's state is changed by a channel monitoring sample.
473 enum lttng_object_type
get_condition_binding_object(
474 const struct lttng_condition
*condition
)
476 switch (lttng_condition_get_type(condition
)) {
477 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
478 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
479 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
480 return LTTNG_OBJECT_TYPE_CHANNEL
;
481 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
482 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
483 return LTTNG_OBJECT_TYPE_SESSION
;
484 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
485 return LTTNG_OBJECT_TYPE_NONE
;
487 return LTTNG_OBJECT_TYPE_UNKNOWN
;
492 void free_channel_info_rcu(struct rcu_head
*node
)
494 free(caa_container_of(node
, struct channel_info
, rcu_node
));
498 void channel_info_destroy(struct channel_info
*channel_info
)
504 if (channel_info
->session_info
) {
505 session_info_remove_channel(channel_info
->session_info
,
507 session_info_put(channel_info
->session_info
);
509 if (channel_info
->name
) {
510 free(channel_info
->name
);
512 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
516 void free_session_info_rcu(struct rcu_head
*node
)
518 free(caa_container_of(node
, struct session_info
, rcu_node
));
521 /* Don't call directly, use the ref-counting mechanism. */
523 void session_info_destroy(void *_data
)
525 struct session_info
*session_info
= (struct session_info
*) _data
;
528 LTTNG_ASSERT(session_info
);
529 if (session_info
->channel_infos_ht
) {
530 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
532 ERR("Failed to destroy channel information hash table");
535 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
538 cds_lfht_del(session_info
->sessions_ht
,
539 &session_info
->sessions_ht_node
);
541 free(session_info
->name
);
542 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
546 void session_info_get(struct session_info
*session_info
)
551 lttng_ref_get(&session_info
->ref
);
555 void session_info_put(struct session_info
*session_info
)
560 lttng_ref_put(&session_info
->ref
);
564 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
565 struct lttng_session_trigger_list
*trigger_list
,
566 struct cds_lfht
*sessions_ht
)
568 struct session_info
*session_info
;
572 session_info
= zmalloc
<struct session_info
>();
576 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
578 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
579 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
580 if (!session_info
->channel_infos_ht
) {
584 cds_lfht_node_init(&session_info
->sessions_ht_node
);
585 session_info
->name
= strdup(name
);
586 if (!session_info
->name
) {
589 session_info
->uid
= uid
;
590 session_info
->gid
= gid
;
591 session_info
->trigger_list
= trigger_list
;
592 session_info
->sessions_ht
= sessions_ht
;
596 session_info_put(session_info
);
601 void session_info_add_channel(struct session_info
*session_info
,
602 struct channel_info
*channel_info
)
605 cds_lfht_add(session_info
->channel_infos_ht
,
606 hash_channel_key(&channel_info
->key
),
607 &channel_info
->session_info_channels_ht_node
);
612 void session_info_remove_channel(struct session_info
*session_info
,
613 struct channel_info
*channel_info
)
616 cds_lfht_del(session_info
->channel_infos_ht
,
617 &channel_info
->session_info_channels_ht_node
);
622 struct channel_info
*channel_info_create(const char *channel_name
,
623 struct channel_key
*channel_key
, uint64_t channel_capacity
,
624 struct session_info
*session_info
)
626 struct channel_info
*channel_info
= zmalloc
<struct channel_info
>();
632 cds_lfht_node_init(&channel_info
->channels_ht_node
);
633 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
634 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
635 channel_info
->capacity
= channel_capacity
;
637 channel_info
->name
= strdup(channel_name
);
638 if (!channel_info
->name
) {
643 * Set the references between session and channel infos:
644 * - channel_info holds a strong reference to session_info
645 * - session_info holds a weak reference to channel_info
647 session_info_get(session_info
);
648 session_info_add_channel(session_info
, channel_info
);
649 channel_info
->session_info
= session_info
;
653 channel_info_destroy(channel_info
);
657 bool notification_client_list_get(struct notification_client_list
*list
)
659 return urcu_ref_get_unless_zero(&list
->ref
);
663 void free_notification_client_list_rcu(struct rcu_head
*node
)
665 free(caa_container_of(node
, struct notification_client_list
,
670 void notification_client_list_release(struct urcu_ref
*list_ref
)
672 struct notification_client_list
*list
=
673 container_of(list_ref
, typeof(*list
), ref
);
674 struct notification_client_list_element
*client_list_element
, *tmp
;
676 lttng_condition_put(list
->condition
);
678 if (list
->notification_trigger_clients_ht
) {
681 cds_lfht_del(list
->notification_trigger_clients_ht
,
682 &list
->notification_trigger_clients_ht_node
);
684 list
->notification_trigger_clients_ht
= NULL
;
686 cds_list_for_each_entry_safe(client_list_element
, tmp
,
687 &list
->clients_list
, node
) {
688 free(client_list_element
);
691 LTTNG_ASSERT(cds_list_empty(&list
->triggers_list
));
693 pthread_mutex_destroy(&list
->lock
);
694 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
698 bool condition_applies_to_client(const struct lttng_condition
*condition
,
699 struct notification_client
*client
)
701 bool applies
= false;
702 struct lttng_condition_list_element
*condition_list_element
;
704 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
706 applies
= lttng_condition_is_equal(
707 condition_list_element
->condition
,
718 struct notification_client_list
*notification_client_list_create(
719 struct notification_thread_state
*state
,
720 const struct lttng_condition
*condition
)
722 struct notification_client
*client
;
723 struct cds_lfht_iter iter
;
724 struct notification_client_list
*client_list
;
726 client_list
= zmalloc
<notification_client_list
>();
728 PERROR("Failed to allocate notification client list");
732 pthread_mutex_init(&client_list
->lock
, NULL
);
734 * The trigger that owns the condition has the first reference to this
737 urcu_ref_init(&client_list
->ref
);
738 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
739 CDS_INIT_LIST_HEAD(&client_list
->clients_list
);
740 CDS_INIT_LIST_HEAD(&client_list
->triggers_list
);
743 * Create a copy of the condition so that it's independent of any
744 * trigger. The client list may outlive the trigger object (which owns
745 * the condition) that is used to create it.
747 client_list
->condition
= lttng_condition_copy(condition
);
749 /* Build a list of clients to which this new condition applies. */
750 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
751 client_socket_ht_node
) {
752 struct notification_client_list_element
*client_list_element
;
754 if (!condition_applies_to_client(condition
, client
)) {
758 client_list_element
= zmalloc
<notification_client_list_element
>();
759 if (!client_list_element
) {
760 goto error_put_client_list
;
763 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
764 client_list_element
->client
= client
;
765 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
768 client_list
->notification_trigger_clients_ht
=
769 state
->notification_trigger_clients_ht
;
773 * Add the client list to the global list of client list.
775 cds_lfht_add_unique(state
->notification_trigger_clients_ht
,
776 lttng_condition_hash(client_list
->condition
),
777 match_client_list_condition
,
778 client_list
->condition
,
779 &client_list
->notification_trigger_clients_ht_node
);
783 error_put_client_list
:
784 notification_client_list_put(client_list
);
791 void notification_client_list_put(struct notification_client_list
*list
)
796 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
799 /* Provides a reference to the returned list. */
801 struct notification_client_list
*get_client_list_from_condition(
802 struct notification_thread_state
*state
,
803 const struct lttng_condition
*condition
)
805 struct cds_lfht_node
*node
;
806 struct cds_lfht_iter iter
;
807 struct notification_client_list
*list
= NULL
;
810 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
811 lttng_condition_hash(condition
),
812 match_client_list_condition
,
815 node
= cds_lfht_iter_get_node(&iter
);
817 list
= container_of(node
, struct notification_client_list
,
818 notification_trigger_clients_ht_node
);
819 list
= notification_client_list_get(list
) ? list
: NULL
;
827 int evaluate_channel_condition_for_client(
828 const struct lttng_condition
*condition
,
829 struct notification_thread_state
*state
,
830 struct lttng_evaluation
**evaluation
,
831 uid_t
*session_uid
, gid_t
*session_gid
)
834 struct cds_lfht_iter iter
;
835 struct cds_lfht_node
*node
;
836 struct channel_info
*channel_info
= NULL
;
837 struct channel_key
*channel_key
= NULL
;
838 struct channel_state_sample
*last_sample
= NULL
;
839 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
843 /* Find the channel associated with the condition. */
844 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
845 channel_trigger_list
, channel_triggers_ht_node
) {
846 struct lttng_trigger_list_element
*element
;
848 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
849 const struct lttng_condition
*current_condition
=
850 lttng_trigger_get_const_condition(
853 LTTNG_ASSERT(current_condition
);
854 if (!lttng_condition_is_equal(condition
,
855 current_condition
)) {
859 /* Found the trigger, save the channel key. */
860 channel_key
= &channel_trigger_list
->channel_key
;
864 /* The channel key was found stop iteration. */
870 /* No channel found; normal exit. */
871 DBG("No known channel associated with newly subscribed-to condition");
876 /* Fetch channel info for the matching channel. */
877 cds_lfht_lookup(state
->channels_ht
,
878 hash_channel_key(channel_key
),
882 node
= cds_lfht_iter_get_node(&iter
);
884 channel_info
= caa_container_of(node
, struct channel_info
,
887 /* Retrieve the channel's last sample, if it exists. */
888 cds_lfht_lookup(state
->channel_state_ht
,
889 hash_channel_key(channel_key
),
890 match_channel_state_sample
,
893 node
= cds_lfht_iter_get_node(&iter
);
895 last_sample
= caa_container_of(node
,
896 struct channel_state_sample
,
897 channel_state_ht_node
);
899 /* Nothing to evaluate, no sample was ever taken. Normal exit */
900 DBG("No channel sample associated with newly subscribed-to condition");
905 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
907 0, channel_info
->session_info
->consumed_data_size
,
910 WARN("Fatal error occurred while evaluating a newly subscribed-to condition");
914 *session_uid
= channel_info
->session_info
->uid
;
915 *session_gid
= channel_info
->session_info
->gid
;
922 const char *get_condition_session_name(const struct lttng_condition
*condition
)
924 const char *session_name
= NULL
;
925 enum lttng_condition_status status
;
927 switch (lttng_condition_get_type(condition
)) {
928 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
929 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
930 status
= lttng_condition_buffer_usage_get_session_name(
931 condition
, &session_name
);
933 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
934 status
= lttng_condition_session_consumed_size_get_session_name(
935 condition
, &session_name
);
937 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
938 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
939 status
= lttng_condition_session_rotation_get_session_name(
940 condition
, &session_name
);
945 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
946 ERR("Failed to retrieve session rotation condition's session name");
954 int evaluate_session_condition_for_client(
955 const struct lttng_condition
*condition
,
956 struct notification_thread_state
*state
,
957 struct lttng_evaluation
**evaluation
,
958 uid_t
*session_uid
, gid_t
*session_gid
)
961 struct cds_lfht_iter iter
;
962 struct cds_lfht_node
*node
;
963 const char *session_name
;
964 struct session_info
*session_info
= NULL
;
967 session_name
= get_condition_session_name(condition
);
969 /* Find the session associated with the trigger. */
970 cds_lfht_lookup(state
->sessions_ht
,
971 hash_key_str(session_name
, lttng_ht_seed
),
975 node
= cds_lfht_iter_get_node(&iter
);
977 DBG("No known session matching name \"%s\"",
983 session_info
= caa_container_of(node
, struct session_info
,
985 session_info_get(session_info
);
988 * Evaluation is performed in-line here since only one type of
989 * session-bound condition is handled for the moment.
991 switch (lttng_condition_get_type(condition
)) {
992 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
993 if (!session_info
->rotation
.ongoing
) {
995 goto end_session_put
;
998 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
999 session_info
->rotation
.id
);
1002 ERR("Failed to create session rotation ongoing evaluation for session \"%s\"",
1003 session_info
->name
);
1005 goto end_session_put
;
1011 goto end_session_put
;
1014 *session_uid
= session_info
->uid
;
1015 *session_gid
= session_info
->gid
;
1018 session_info_put(session_info
);
1025 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1026 const struct lttng_condition
*condition
,
1027 struct notification_client
*client
,
1028 struct notification_thread_state
*state
)
1031 struct lttng_evaluation
*evaluation
= NULL
;
1032 struct notification_client_list client_list
= {
1033 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1036 .triggers_list
= {},
1038 .notification_trigger_clients_ht
= NULL
,
1039 .notification_trigger_clients_ht_node
= {},
1042 struct notification_client_list_element client_list_element
= {};
1043 uid_t object_uid
= 0;
1044 gid_t object_gid
= 0;
1046 LTTNG_ASSERT(trigger
);
1047 LTTNG_ASSERT(condition
);
1048 LTTNG_ASSERT(client
);
1049 LTTNG_ASSERT(state
);
1051 switch (get_condition_binding_object(condition
)) {
1052 case LTTNG_OBJECT_TYPE_SESSION
:
1053 ret
= evaluate_session_condition_for_client(condition
, state
,
1054 &evaluation
, &object_uid
, &object_gid
);
1056 case LTTNG_OBJECT_TYPE_CHANNEL
:
1057 ret
= evaluate_channel_condition_for_client(condition
, state
,
1058 &evaluation
, &object_uid
, &object_gid
);
1060 case LTTNG_OBJECT_TYPE_NONE
:
1061 DBG("Newly subscribed-to condition not bound to object, nothing to evaluate");
1064 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1074 /* Evaluation yielded nothing. Normal exit. */
1075 DBG("Newly subscribed-to condition evaluated to false, nothing to report to client");
1081 * Create a temporary client list with the client currently
1084 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1085 CDS_INIT_LIST_HEAD(&client_list
.clients_list
);
1087 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1088 client_list_element
.client
= client
;
1089 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1091 /* Send evaluation result to the newly-subscribed client. */
1092 DBG("Newly subscribed-to condition evaluated to true, notifying client");
1093 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1094 state
, object_uid
, object_gid
);
1101 int notification_thread_client_subscribe(struct notification_client
*client
,
1102 struct lttng_condition
*condition
,
1103 struct notification_thread_state
*state
,
1104 enum lttng_notification_channel_status
*_status
)
1107 struct notification_client_list
*client_list
= NULL
;
1108 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1109 struct notification_client_list_element
*client_list_element
= NULL
;
1110 struct lttng_trigger_ht_element
*trigger_ht_element
;
1111 enum lttng_notification_channel_status status
=
1112 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1115 * Ensure that the client has not already subscribed to this condition
1118 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1119 if (lttng_condition_is_equal(condition_list_element
->condition
,
1121 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1126 condition_list_element
= zmalloc
<lttng_condition_list_element
>();
1127 if (!condition_list_element
) {
1131 client_list_element
= zmalloc
<notification_client_list_element
>();
1132 if (!client_list_element
) {
1138 * Add the newly-subscribed condition to the client's subscription list.
1140 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1141 condition_list_element
->condition
= condition
;
1143 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1145 client_list
= get_client_list_from_condition(
1146 state
, condition_list_element
->condition
);
1149 * No notification-emiting trigger registered with this
1150 * condition. We don't evaluate the condition right away
1151 * since this trigger is not registered yet.
1153 free(client_list_element
);
1158 * The condition to which the client just subscribed is evaluated
1159 * at this point so that conditions that are already TRUE result
1160 * in a notification being sent out.
1162 * Note the iteration on all triggers which share an identical
1163 * `condition` than the one to which the client is registering. This is
1164 * done to ensure that the client receives a distinct notification for
1165 * all triggers that have a `notify` action that have this condition.
1167 pthread_mutex_lock(&client_list
->lock
);
1168 cds_list_for_each_entry(trigger_ht_element
,
1169 &client_list
->triggers_list
, client_list_trigger_node
) {
1170 if (evaluate_condition_for_client(trigger_ht_element
->trigger
, condition_list_element
->condition
,
1172 WARN("Evaluation of a condition on client subscription failed, aborting.");
1174 free(client_list_element
);
1175 pthread_mutex_unlock(&client_list
->lock
);
1179 pthread_mutex_unlock(&client_list
->lock
);
1182 * Add the client to the list of clients interested in a given trigger
1183 * if a "notification" trigger with a corresponding condition was
1186 client_list_element
->client
= client
;
1187 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1189 pthread_mutex_lock(&client_list
->lock
);
1190 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1191 pthread_mutex_unlock(&client_list
->lock
);
1197 notification_client_list_put(client_list
);
1199 lttng_condition_destroy(condition
);
1202 free(condition_list_element
);
1203 free(client_list_element
);
1204 lttng_condition_destroy(condition
);
1209 int notification_thread_client_unsubscribe(
1210 struct notification_client
*client
,
1211 struct lttng_condition
*condition
,
1212 struct notification_thread_state
*state
,
1213 enum lttng_notification_channel_status
*_status
)
1215 struct notification_client_list
*client_list
;
1216 struct lttng_condition_list_element
*condition_list_element
,
1218 struct notification_client_list_element
*client_list_element
,
1220 bool condition_found
= false;
1221 enum lttng_notification_channel_status status
=
1222 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1224 /* Remove the condition from the client's condition list. */
1225 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1226 &client
->condition_list
, node
) {
1227 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1232 cds_list_del(&condition_list_element
->node
);
1234 * The caller may be iterating on the client's conditions to
1235 * tear down a client's connection. In this case, the condition
1236 * will be destroyed at the end.
1238 if (condition
!= condition_list_element
->condition
) {
1239 lttng_condition_destroy(
1240 condition_list_element
->condition
);
1242 free(condition_list_element
);
1243 condition_found
= true;
1247 if (!condition_found
) {
1248 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1253 * Remove the client from the list of clients interested the trigger
1254 * matching the condition.
1256 client_list
= get_client_list_from_condition(state
, condition
);
1261 pthread_mutex_lock(&client_list
->lock
);
1262 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1263 &client_list
->clients_list
, node
) {
1264 if (client_list_element
->client
->id
!= client
->id
) {
1267 cds_list_del(&client_list_element
->node
);
1268 free(client_list_element
);
1271 pthread_mutex_unlock(&client_list
->lock
);
1272 notification_client_list_put(client_list
);
1275 lttng_condition_destroy(condition
);
1283 void free_notification_client_rcu(struct rcu_head
*node
)
1285 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1289 void notification_client_destroy(struct notification_client
*client
)
1296 * The client object is not reachable by other threads, no need to lock
1299 if (client
->socket
>= 0) {
1300 (void) lttcomm_close_unix_sock(client
->socket
);
1301 client
->socket
= -1;
1303 client
->communication
.active
= false;
1304 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1305 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1306 pthread_mutex_destroy(&client
->lock
);
1307 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1311 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1315 struct notification_client
*get_client_from_socket(int socket
,
1316 struct notification_thread_state
*state
)
1318 struct cds_lfht_iter iter
;
1319 struct cds_lfht_node
*node
;
1320 struct notification_client
*client
= NULL
;
1322 ASSERT_RCU_READ_LOCKED();
1324 cds_lfht_lookup(state
->client_socket_ht
,
1325 hash_client_socket(socket
),
1326 match_client_socket
,
1327 (void *) (unsigned long) socket
,
1329 node
= cds_lfht_iter_get_node(&iter
);
1334 client
= caa_container_of(node
, struct notification_client
,
1335 client_socket_ht_node
);
1341 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1345 struct notification_client
*get_client_from_id(notification_client_id id
,
1346 struct notification_thread_state
*state
)
1348 struct cds_lfht_iter iter
;
1349 struct cds_lfht_node
*node
;
1350 struct notification_client
*client
= NULL
;
1352 ASSERT_RCU_READ_LOCKED();
1354 cds_lfht_lookup(state
->client_id_ht
,
1359 node
= cds_lfht_iter_get_node(&iter
);
1364 client
= caa_container_of(node
, struct notification_client
,
1371 bool buffer_usage_condition_applies_to_channel(
1372 const struct lttng_condition
*condition
,
1373 const struct channel_info
*channel_info
)
1375 enum lttng_condition_status status
;
1376 enum lttng_domain_type condition_domain
;
1377 const char *condition_session_name
= NULL
;
1378 const char *condition_channel_name
= NULL
;
1380 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1382 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
1383 if (channel_info
->key
.domain
!= condition_domain
) {
1387 status
= lttng_condition_buffer_usage_get_session_name(
1388 condition
, &condition_session_name
);
1389 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1391 status
= lttng_condition_buffer_usage_get_channel_name(
1392 condition
, &condition_channel_name
);
1393 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1395 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1398 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1408 bool session_consumed_size_condition_applies_to_channel(
1409 const struct lttng_condition
*condition
,
1410 const struct channel_info
*channel_info
)
1412 enum lttng_condition_status status
;
1413 const char *condition_session_name
= NULL
;
1415 status
= lttng_condition_session_consumed_size_get_session_name(
1416 condition
, &condition_session_name
);
1417 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1419 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1429 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1430 const struct channel_info
*channel_info
)
1432 const struct lttng_condition
*condition
;
1433 bool trigger_applies
;
1435 condition
= lttng_trigger_get_const_condition(trigger
);
1440 switch (lttng_condition_get_type(condition
)) {
1441 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1442 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1443 trigger_applies
= buffer_usage_condition_applies_to_channel(
1444 condition
, channel_info
);
1446 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1447 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1448 condition
, channel_info
);
1454 return trigger_applies
;
1459 /* Must be called with RCU read lock held. */
1461 struct lttng_session_trigger_list
*get_session_trigger_list(
1462 struct notification_thread_state
*state
,
1463 const char *session_name
)
1465 struct lttng_session_trigger_list
*list
= NULL
;
1466 struct cds_lfht_node
*node
;
1467 struct cds_lfht_iter iter
;
1469 ASSERT_RCU_READ_LOCKED();
1471 cds_lfht_lookup(state
->session_triggers_ht
,
1472 hash_key_str(session_name
, lttng_ht_seed
),
1473 match_session_trigger_list
,
1476 node
= cds_lfht_iter_get_node(&iter
);
1479 * Not an error, the list of triggers applying to that session
1480 * will be initialized when the session is created.
1482 DBG("No trigger list found for session \"%s\" as it is not yet known to the notification system",
1487 list
= caa_container_of(node
,
1488 struct lttng_session_trigger_list
,
1489 session_triggers_ht_node
);
1495 * Allocate an empty lttng_session_trigger_list for the session named
1498 * No ownership of 'session_name' is assumed by the session trigger list.
1499 * It is the caller's responsability to ensure the session name is alive
1500 * for as long as this list is.
1503 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1504 const char *session_name
,
1505 struct cds_lfht
*session_triggers_ht
)
1507 struct lttng_session_trigger_list
*list
;
1509 list
= zmalloc
<lttng_session_trigger_list
>();
1513 list
->session_name
= session_name
;
1514 CDS_INIT_LIST_HEAD(&list
->list
);
1515 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1516 list
->session_triggers_ht
= session_triggers_ht
;
1519 /* Publish the list through the session_triggers_ht. */
1520 cds_lfht_add(session_triggers_ht
,
1521 hash_key_str(session_name
, lttng_ht_seed
),
1522 &list
->session_triggers_ht_node
);
1529 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1531 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1536 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1538 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1540 /* Empty the list element by element, and then free the list itself. */
1541 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1542 &list
->list
, node
) {
1543 cds_list_del(&trigger_list_element
->node
);
1544 free(trigger_list_element
);
1547 /* Unpublish the list from the session_triggers_ht. */
1548 cds_lfht_del(list
->session_triggers_ht
,
1549 &list
->session_triggers_ht_node
);
1551 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1555 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1556 struct lttng_trigger
*trigger
)
1559 struct lttng_trigger_list_element
*new_element
=
1560 zmalloc
<lttng_trigger_list_element
>();
1566 CDS_INIT_LIST_HEAD(&new_element
->node
);
1567 new_element
->trigger
= trigger
;
1568 cds_list_add(&new_element
->node
, &list
->list
);
1574 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1575 const char *session_name
)
1577 bool applies
= false;
1578 const struct lttng_condition
*condition
;
1580 condition
= lttng_trigger_get_const_condition(trigger
);
1581 switch (lttng_condition_get_type(condition
)) {
1582 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1583 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1585 enum lttng_condition_status condition_status
;
1586 const char *condition_session_name
;
1588 condition_status
= lttng_condition_session_rotation_get_session_name(
1589 condition
, &condition_session_name
);
1590 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1591 ERR("Failed to retrieve session rotation condition's session name");
1595 LTTNG_ASSERT(condition_session_name
);
1596 applies
= !strcmp(condition_session_name
, session_name
);
1607 * Allocate and initialize an lttng_session_trigger_list which contains
1608 * all triggers that apply to the session named 'session_name'.
1610 * No ownership of 'session_name' is assumed by the session trigger list.
1611 * It is the caller's responsability to ensure the session name is alive
1612 * for as long as this list is.
1615 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1616 const struct notification_thread_state
*state
,
1617 const char *session_name
)
1619 int trigger_count
= 0;
1620 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1621 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1622 struct cds_lfht_iter iter
;
1624 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1625 state
->session_triggers_ht
);
1627 /* Add all triggers applying to the session named 'session_name'. */
1628 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1632 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1637 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1638 trigger_ht_element
->trigger
);
1646 DBG("Found %i triggers that apply to newly created session",
1648 return session_trigger_list
;
1650 lttng_session_trigger_list_destroy(session_trigger_list
);
1655 struct session_info
*find_or_create_session_info(
1656 struct notification_thread_state
*state
,
1657 const char *name
, uid_t uid
, gid_t gid
)
1659 struct session_info
*session
= NULL
;
1660 struct cds_lfht_node
*node
;
1661 struct cds_lfht_iter iter
;
1662 struct lttng_session_trigger_list
*trigger_list
;
1665 cds_lfht_lookup(state
->sessions_ht
,
1666 hash_key_str(name
, lttng_ht_seed
),
1670 node
= cds_lfht_iter_get_node(&iter
);
1672 DBG("Found session info of session \"%s\" (uid = %i, gid = %i)",
1674 session
= caa_container_of(node
, struct session_info
,
1676 LTTNG_ASSERT(session
->uid
== uid
);
1677 LTTNG_ASSERT(session
->gid
== gid
);
1678 session_info_get(session
);
1682 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1683 if (!trigger_list
) {
1687 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1688 state
->sessions_ht
);
1690 ERR("Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1692 lttng_session_trigger_list_destroy(trigger_list
);
1695 trigger_list
= NULL
;
1697 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1698 &session
->sessions_ht_node
);
1704 session_info_put(session
);
1709 int handle_notification_thread_command_add_channel(
1710 struct notification_thread_state
*state
,
1711 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1712 const char *channel_name
, enum lttng_domain_type channel_domain
,
1713 uint64_t channel_key_int
, uint64_t channel_capacity
,
1714 enum lttng_error_code
*cmd_result
)
1716 struct cds_list_head trigger_list
;
1717 struct channel_info
*new_channel_info
= NULL
;
1718 struct channel_key channel_key
= {
1719 .key
= channel_key_int
,
1720 .domain
= channel_domain
,
1722 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1723 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1724 int trigger_count
= 0;
1725 struct cds_lfht_iter iter
;
1726 struct session_info
*session_info
= NULL
;
1728 DBG("Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1729 channel_name
, session_name
, channel_key_int
,
1730 lttng_domain_type_str(channel_domain
));
1732 CDS_INIT_LIST_HEAD(&trigger_list
);
1734 session_info
= find_or_create_session_info(state
, session_name
,
1735 session_uid
, session_gid
);
1736 if (!session_info
) {
1737 /* Allocation error or an internal error occurred. */
1741 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1742 channel_capacity
, session_info
);
1743 if (!new_channel_info
) {
1748 /* Build a list of all triggers applying to the new channel. */
1749 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1751 struct lttng_trigger_list_element
*new_element
;
1753 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1754 new_channel_info
)) {
1758 new_element
= zmalloc
<lttng_trigger_list_element
>();
1763 CDS_INIT_LIST_HEAD(&new_element
->node
);
1764 new_element
->trigger
= trigger_ht_element
->trigger
;
1765 cds_list_add(&new_element
->node
, &trigger_list
);
1770 DBG("Found %i triggers that apply to newly added channel",
1772 channel_trigger_list
= zmalloc
<lttng_channel_trigger_list
>();
1773 if (!channel_trigger_list
) {
1776 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1777 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1778 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1779 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1782 /* Add channel to the channel_ht which owns the channel_infos. */
1783 cds_lfht_add(state
->channels_ht
,
1784 hash_channel_key(&new_channel_info
->key
),
1785 &new_channel_info
->channels_ht_node
);
1787 * Add the list of triggers associated with this channel to the
1788 * channel_triggers_ht.
1790 cds_lfht_add(state
->channel_triggers_ht
,
1791 hash_channel_key(&new_channel_info
->key
),
1792 &channel_trigger_list
->channel_triggers_ht_node
);
1794 session_info_put(session_info
);
1795 *cmd_result
= LTTNG_OK
;
1798 channel_info_destroy(new_channel_info
);
1799 session_info_put(session_info
);
1804 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1806 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1811 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1813 free(caa_container_of(node
, struct channel_state_sample
,
1818 int handle_notification_thread_command_remove_channel(
1819 struct notification_thread_state
*state
,
1820 uint64_t channel_key
, enum lttng_domain_type domain
,
1821 enum lttng_error_code
*cmd_result
)
1823 struct cds_lfht_node
*node
;
1824 struct cds_lfht_iter iter
;
1825 struct lttng_channel_trigger_list
*trigger_list
;
1826 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1827 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1828 struct channel_info
*channel_info
;
1830 DBG("Removing channel key = %" PRIu64
" in %s domain",
1831 channel_key
, lttng_domain_type_str(domain
));
1835 cds_lfht_lookup(state
->channel_triggers_ht
,
1836 hash_channel_key(&key
),
1837 match_channel_trigger_list
,
1840 node
= cds_lfht_iter_get_node(&iter
);
1842 * There is a severe internal error if we are being asked to remove a
1843 * channel that doesn't exist.
1846 ERR("Channel being removed is unknown to the notification thread");
1850 /* Free the list of triggers associated with this channel. */
1851 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1852 channel_triggers_ht_node
);
1853 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1854 &trigger_list
->list
, node
) {
1855 cds_list_del(&trigger_list_element
->node
);
1856 free(trigger_list_element
);
1858 cds_lfht_del(state
->channel_triggers_ht
, node
);
1859 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1861 /* Free sampled channel state. */
1862 cds_lfht_lookup(state
->channel_state_ht
,
1863 hash_channel_key(&key
),
1864 match_channel_state_sample
,
1867 node
= cds_lfht_iter_get_node(&iter
);
1869 * This is expected to be NULL if the channel is destroyed before we
1870 * received a sample.
1873 struct channel_state_sample
*sample
= caa_container_of(node
,
1874 struct channel_state_sample
,
1875 channel_state_ht_node
);
1877 cds_lfht_del(state
->channel_state_ht
, node
);
1878 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1881 /* Remove the channel from the channels_ht and free it. */
1882 cds_lfht_lookup(state
->channels_ht
,
1883 hash_channel_key(&key
),
1887 node
= cds_lfht_iter_get_node(&iter
);
1889 channel_info
= caa_container_of(node
, struct channel_info
,
1891 cds_lfht_del(state
->channels_ht
, node
);
1892 channel_info_destroy(channel_info
);
1895 *cmd_result
= LTTNG_OK
;
1900 int handle_notification_thread_command_session_rotation(
1901 struct notification_thread_state
*state
,
1902 enum notification_thread_command_type cmd_type
,
1903 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1904 uint64_t trace_archive_chunk_id
,
1905 struct lttng_trace_archive_location
*location
,
1906 enum lttng_error_code
*_cmd_result
)
1909 enum lttng_error_code cmd_result
= LTTNG_OK
;
1910 struct lttng_session_trigger_list
*trigger_list
;
1911 struct lttng_trigger_list_element
*trigger_list_element
;
1912 struct session_info
*session_info
;
1913 const struct lttng_credentials session_creds
= {
1914 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1915 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1920 session_info
= find_or_create_session_info(state
, session_name
,
1921 session_uid
, session_gid
);
1922 if (!session_info
) {
1923 /* Allocation error or an internal error occurred. */
1925 cmd_result
= LTTNG_ERR_NOMEM
;
1929 session_info
->rotation
.ongoing
=
1930 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1931 session_info
->rotation
.id
= trace_archive_chunk_id
;
1932 trigger_list
= get_session_trigger_list(state
, session_name
);
1933 if (!trigger_list
) {
1934 DBG("No triggers applying to session \"%s\" found",
1939 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1941 const struct lttng_condition
*condition
;
1942 struct lttng_trigger
*trigger
;
1943 struct notification_client_list
*client_list
;
1944 struct lttng_evaluation
*evaluation
= NULL
;
1945 enum lttng_condition_type condition_type
;
1946 enum action_executor_status executor_status
;
1948 trigger
= trigger_list_element
->trigger
;
1949 condition
= lttng_trigger_get_const_condition(trigger
);
1950 LTTNG_ASSERT(condition
);
1951 condition_type
= lttng_condition_get_type(condition
);
1953 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1954 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1956 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1957 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1961 client_list
= get_client_list_from_condition(state
, condition
);
1962 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1963 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1964 trace_archive_chunk_id
);
1966 evaluation
= lttng_evaluation_session_rotation_completed_create(
1967 trace_archive_chunk_id
, location
);
1971 /* Internal error */
1973 cmd_result
= LTTNG_ERR_UNK
;
1978 * Ownership of `evaluation` transferred to the action executor
1979 * no matter the result.
1981 executor_status
= action_executor_enqueue_trigger(
1982 state
->executor
, trigger
, evaluation
,
1983 &session_creds
, client_list
);
1985 switch (executor_status
) {
1986 case ACTION_EXECUTOR_STATUS_OK
:
1988 case ACTION_EXECUTOR_STATUS_ERROR
:
1989 case ACTION_EXECUTOR_STATUS_INVALID
:
1991 * TODO Add trigger identification (name/id) when
1992 * it is added to the API.
1994 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1997 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1999 * TODO Add trigger identification (name/id) when
2000 * it is added to the API.
2002 * Not a fatal error.
2004 WARN("No space left when enqueuing action associated with session rotation trigger");
2012 notification_client_list_put(client_list
);
2013 if (caa_unlikely(ret
)) {
2018 session_info_put(session_info
);
2019 *_cmd_result
= cmd_result
;
2025 int handle_notification_thread_command_add_tracer_event_source(
2026 struct notification_thread_state
*state
,
2027 int tracer_event_source_fd
,
2028 enum lttng_domain_type domain_type
,
2029 enum lttng_error_code
*_cmd_result
)
2032 enum lttng_error_code cmd_result
= LTTNG_OK
;
2033 struct notification_event_tracer_event_source_element
*element
= NULL
;
2035 element
= zmalloc
<notification_event_tracer_event_source_element
>();
2037 cmd_result
= LTTNG_ERR_NOMEM
;
2042 element
->fd
= tracer_event_source_fd
;
2043 element
->domain
= domain_type
;
2045 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2047 DBG3("Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2048 tracer_event_source_fd
,
2049 lttng_domain_type_str(domain_type
));
2051 /* Adding the read side pipe to the event poll. */
2052 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLPRI
| LPOLLIN
| LPOLLERR
);
2054 ERR("Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2055 tracer_event_source_fd
,
2056 lttng_domain_type_str(element
->domain
));
2057 cds_list_del(&element
->node
);
2062 element
->is_fd_in_poll_set
= true;
2065 *_cmd_result
= cmd_result
;
2070 int drain_event_notifier_notification_pipe(
2071 struct notification_thread_state
*state
,
2072 int pipe
, enum lttng_domain_type domain
)
2074 struct lttng_poll_event events
= {};
2077 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2079 ERR("Error creating lttng_poll_event");
2083 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2085 ERR("Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2092 * Continue to consume notifications as long as there are new
2093 * ones coming in. The tracer has been asked to stop producing
2096 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2097 * monitored (on Linux, at least) and will be returned when
2098 * the pipe is closed but empty.
2100 ret
= lttng_poll_wait_interruptible(&events
, 0);
2101 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2102 /* No more notification to be read on this pipe. */
2105 } else if (ret
< 0) {
2106 PERROR("Failed on lttng_poll_wait_interruptible() call");
2111 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2113 ERR("Error consuming an event notifier notification from pipe: fd = %d",
2118 lttng_poll_clean(&events
);
2123 struct notification_event_tracer_event_source_element
*
2124 find_tracer_event_source_element(struct notification_thread_state
*state
,
2125 int tracer_event_source_fd
)
2127 struct notification_event_tracer_event_source_element
*source_element
;
2129 cds_list_for_each_entry(source_element
,
2130 &state
->tracer_event_sources_list
, node
) {
2131 if (source_element
->fd
== tracer_event_source_fd
) {
2136 source_element
= NULL
;
2138 return source_element
;
2142 int remove_tracer_event_source_from_pollset(
2143 struct notification_thread_state
*state
,
2144 struct notification_event_tracer_event_source_element
*source_element
)
2148 LTTNG_ASSERT(source_element
->is_fd_in_poll_set
);
2150 DBG3("Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2152 lttng_domain_type_str(source_element
->domain
));
2154 /* Removing the fd from the event poll set. */
2155 ret
= lttng_poll_del(&state
->events
, source_element
->fd
);
2157 ERR("Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2159 lttng_domain_type_str(source_element
->domain
));
2164 source_element
->is_fd_in_poll_set
= false;
2167 * Force the notification thread to restart the poll() loop to ensure
2168 * that any events from the removed fd are removed.
2170 state
->restart_poll
= true;
2172 ret
= drain_event_notifier_notification_pipe(state
, source_element
->fd
,
2173 source_element
->domain
);
2175 ERR("Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2177 lttng_domain_type_str(source_element
->domain
));
2186 int handle_notification_thread_tracer_event_source_died(
2187 struct notification_thread_state
*state
,
2188 int tracer_event_source_fd
)
2191 struct notification_event_tracer_event_source_element
*source_element
;
2193 source_element
= find_tracer_event_source_element(state
,
2194 tracer_event_source_fd
);
2196 LTTNG_ASSERT(source_element
);
2198 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2200 ERR("Failed to remove dead tracer event source from poll set");
2207 int handle_notification_thread_command_remove_tracer_event_source(
2208 struct notification_thread_state
*state
,
2209 int tracer_event_source_fd
,
2210 enum lttng_error_code
*_cmd_result
)
2213 enum lttng_error_code cmd_result
= LTTNG_OK
;
2214 struct notification_event_tracer_event_source_element
*source_element
= NULL
;
2216 source_element
= find_tracer_event_source_element(state
,
2217 tracer_event_source_fd
);
2219 LTTNG_ASSERT(source_element
);
2221 /* Remove the tracer source from the list. */
2222 cds_list_del(&source_element
->node
);
2224 if (!source_element
->is_fd_in_poll_set
) {
2225 /* Skip the poll set removal. */
2229 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2231 ERR("Failed to remove tracer event source from poll set");
2232 cmd_result
= LTTNG_ERR_FATAL
;
2236 free(source_element
);
2237 *_cmd_result
= cmd_result
;
2241 static int handle_notification_thread_command_list_triggers(
2242 struct notification_thread_handle
*handle
__attribute__((unused
)),
2243 struct notification_thread_state
*state
,
2245 struct lttng_triggers
**triggers
,
2246 enum lttng_error_code
*_cmd_result
)
2249 enum lttng_error_code cmd_result
= LTTNG_OK
;
2250 struct cds_lfht_iter iter
;
2251 struct lttng_trigger_ht_element
*trigger_ht_element
;
2252 struct lttng_triggers
*local_triggers
= NULL
;
2253 const struct lttng_credentials
*creds
;
2257 local_triggers
= lttng_triggers_create();
2258 if (!local_triggers
) {
2259 /* Not a fatal error. */
2260 cmd_result
= LTTNG_ERR_NOMEM
;
2264 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2265 trigger_ht_element
, node
) {
2267 * Only return the triggers to which the client has access.
2268 * The root user has visibility over all triggers.
2270 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2271 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2275 ret
= lttng_triggers_add(local_triggers
,
2276 trigger_ht_element
->trigger
);
2278 /* Not a fatal error. */
2280 cmd_result
= LTTNG_ERR_NOMEM
;
2285 /* Transferring ownership to the caller. */
2286 *triggers
= local_triggers
;
2287 local_triggers
= NULL
;
2291 lttng_triggers_destroy(local_triggers
);
2292 *_cmd_result
= cmd_result
;
2296 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
2297 const char **trigger_name
,
2298 uid_t
*trigger_owner_uid
)
2300 enum lttng_trigger_status trigger_status
;
2302 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
2303 switch (trigger_status
) {
2304 case LTTNG_TRIGGER_STATUS_OK
:
2306 case LTTNG_TRIGGER_STATUS_UNSET
:
2307 *trigger_name
= "(anonymous)";
2313 trigger_status
= lttng_trigger_get_owner_uid(trigger
,
2315 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2318 static int handle_notification_thread_command_get_trigger(
2319 struct notification_thread_state
*state
,
2320 const struct lttng_trigger
*trigger
,
2321 struct lttng_trigger
**registered_trigger
,
2322 enum lttng_error_code
*_cmd_result
)
2325 struct cds_lfht_iter iter
;
2326 struct lttng_trigger_ht_element
*trigger_ht_element
;
2327 enum lttng_error_code cmd_result
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2328 const char *trigger_name
;
2329 uid_t trigger_owner_uid
;
2333 cds_lfht_for_each_entry(
2334 state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2335 if (lttng_trigger_is_equal(
2336 trigger
, trigger_ht_element
->trigger
)) {
2337 /* Take one reference on the return trigger. */
2338 *registered_trigger
= trigger_ht_element
->trigger
;
2339 lttng_trigger_get(*registered_trigger
);
2341 cmd_result
= LTTNG_OK
;
2346 /* Not a fatal error if the trigger is not found. */
2347 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
2348 DBG("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d",
2349 trigger_name
, (int) trigger_owner_uid
);
2355 *_cmd_result
= cmd_result
;
2360 bool condition_is_supported(struct lttng_condition
*condition
)
2364 switch (lttng_condition_get_type(condition
)) {
2365 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2366 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2369 enum lttng_domain_type domain
;
2371 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2373 LTTNG_ASSERT(ret
== 0);
2375 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2376 is_supported
= true;
2381 * Older kernel tracers don't expose the API to monitor their
2382 * buffers. Therefore, we reject triggers that require that
2383 * mechanism to be available to be evaluated.
2385 * Assume unsupported on error.
2387 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2390 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
2392 const struct lttng_event_rule
*event_rule
;
2393 enum lttng_domain_type domain
;
2394 const enum lttng_condition_status status
=
2395 lttng_condition_event_rule_matches_get_rule(
2396 condition
, &event_rule
);
2398 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
2400 domain
= lttng_event_rule_get_domain_type(event_rule
);
2401 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2402 is_supported
= true;
2407 * Older kernel tracers can't emit notification. Therefore, we
2408 * reject triggers that require that mechanism to be available
2411 * Assume unsupported on error.
2413 is_supported
= kernel_supports_event_notifiers() == 1;
2417 is_supported
= true;
2420 return is_supported
;
2423 /* Must be called with RCU read lock held. */
2425 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2426 struct notification_thread_state
*state
)
2429 const struct lttng_condition
*condition
;
2430 const char *session_name
;
2431 struct lttng_session_trigger_list
*trigger_list
;
2433 ASSERT_RCU_READ_LOCKED();
2435 condition
= lttng_trigger_get_const_condition(trigger
);
2436 switch (lttng_condition_get_type(condition
)) {
2437 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2438 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2440 enum lttng_condition_status status
;
2442 status
= lttng_condition_session_rotation_get_session_name(
2443 condition
, &session_name
);
2444 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2445 ERR("Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2456 trigger_list
= get_session_trigger_list(state
, session_name
);
2457 if (!trigger_list
) {
2458 DBG("Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2464 DBG("Newly registered trigger bound to session \"%s\"",
2466 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2471 /* Must be called with RCU read lock held. */
2473 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2474 struct notification_thread_state
*state
)
2477 struct cds_lfht_node
*node
;
2478 struct cds_lfht_iter iter
;
2479 struct channel_info
*channel
;
2481 ASSERT_RCU_READ_LOCKED();
2483 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2485 struct lttng_trigger_list_element
*trigger_list_element
;
2486 struct lttng_channel_trigger_list
*trigger_list
;
2487 struct cds_lfht_iter lookup_iter
;
2489 if (!trigger_applies_to_channel(trigger
, channel
)) {
2493 cds_lfht_lookup(state
->channel_triggers_ht
,
2494 hash_channel_key(&channel
->key
),
2495 match_channel_trigger_list
,
2498 node
= cds_lfht_iter_get_node(&lookup_iter
);
2500 trigger_list
= caa_container_of(node
,
2501 struct lttng_channel_trigger_list
,
2502 channel_triggers_ht_node
);
2504 trigger_list_element
= zmalloc
<lttng_trigger_list_element
>();
2505 if (!trigger_list_element
) {
2509 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2510 trigger_list_element
->trigger
= trigger
;
2511 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2512 DBG("Newly registered trigger bound to channel \"%s\"",
2520 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2522 bool is_notify
= false;
2523 unsigned int i
, count
;
2524 enum lttng_action_status action_status
;
2525 const struct lttng_action
*action
=
2526 lttng_trigger_get_const_action(trigger
);
2527 enum lttng_action_type action_type
;
2529 LTTNG_ASSERT(action
);
2530 action_type
= lttng_action_get_type(action
);
2531 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2534 } else if (action_type
!= LTTNG_ACTION_TYPE_LIST
) {
2538 action_status
= lttng_action_list_get_count(action
, &count
);
2539 LTTNG_ASSERT(action_status
== LTTNG_ACTION_STATUS_OK
);
2541 for (i
= 0; i
< count
; i
++) {
2542 const struct lttng_action
*inner_action
=
2543 lttng_action_list_get_at_index(
2546 action_type
= lttng_action_get_type(inner_action
);
2547 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2557 static bool trigger_name_taken(struct notification_thread_state
*state
,
2558 const struct lttng_trigger
*trigger
)
2560 struct cds_lfht_iter iter
;
2563 * No duplicata is allowed in the triggers_by_name_uid_ht.
2564 * The match is done against the trigger name and uid.
2566 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2567 hash_trigger_by_name_uid(trigger
),
2568 match_trigger_by_name_uid
,
2571 return !!cds_lfht_iter_get_node(&iter
);
2575 enum lttng_error_code
generate_trigger_name(
2576 struct notification_thread_state
*state
,
2577 struct lttng_trigger
*trigger
, const char **name
)
2579 enum lttng_error_code ret_code
= LTTNG_OK
;
2581 enum lttng_trigger_status status
;
2584 const int ret
= lttng_trigger_generate_name(trigger
,
2585 state
->trigger_id
.name_offset
++);
2587 /* The only reason this can fail right now. */
2588 ret_code
= LTTNG_ERR_NOMEM
;
2592 status
= lttng_trigger_get_name(trigger
, name
);
2593 LTTNG_ASSERT(status
== LTTNG_TRIGGER_STATUS_OK
);
2595 taken
= trigger_name_taken(state
, trigger
);
2596 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2602 void notif_thread_state_remove_trigger_ht_elem(
2603 struct notification_thread_state
*state
,
2604 struct lttng_trigger_ht_element
*trigger_ht_element
)
2606 LTTNG_ASSERT(state
);
2607 LTTNG_ASSERT(trigger_ht_element
);
2609 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2610 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2614 enum lttng_error_code
setup_tracer_notifier(
2615 struct notification_thread_state
*state
,
2616 struct lttng_trigger
*trigger
)
2618 enum lttng_error_code ret
;
2619 enum event_notifier_error_accounting_status error_accounting_status
;
2620 struct cds_lfht_node
*node
;
2621 uint64_t error_counter_index
= 0;
2622 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2623 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2625 trigger_tokens_ht_element
= zmalloc
<notification_trigger_tokens_ht_element
>();
2626 if (!trigger_tokens_ht_element
) {
2627 ret
= LTTNG_ERR_NOMEM
;
2631 /* Add trigger token to the trigger_tokens_ht. */
2632 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2633 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2634 trigger_tokens_ht_element
->trigger
= trigger
;
2636 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2637 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2638 match_trigger_token
,
2639 &trigger_tokens_ht_element
->token
,
2640 &trigger_tokens_ht_element
->node
);
2641 if (node
!= &trigger_tokens_ht_element
->node
) {
2642 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2643 goto error_free_ht_element
;
2646 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2647 trigger
, &error_counter_index
);
2648 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2649 if (error_accounting_status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2650 DBG("Trigger list error accounting counter full.");
2651 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2653 ERR("Error registering trigger for error accounting");
2654 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2657 goto error_remove_ht_element
;
2660 lttng_condition_event_rule_matches_set_error_counter_index(
2661 condition
, error_counter_index
);
2666 error_remove_ht_element
:
2667 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2668 error_free_ht_element
:
2669 free(trigger_tokens_ht_element
);
2675 * FIXME A client's credentials are not checked when registering a trigger.
2677 * The effects of this are benign since:
2678 * - The client will succeed in registering the trigger, as it is valid,
2679 * - The trigger will, internally, be bound to the channel/session,
2680 * - The notifications will not be sent since the client's credentials
2681 * are checked against the channel at that moment.
2683 * If this function returns a non-zero value, it means something is
2684 * fundamentally broken and the whole subsystem/thread will be torn down.
2686 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2690 int handle_notification_thread_command_register_trigger(
2691 struct notification_thread_state
*state
,
2692 struct lttng_trigger
*trigger
,
2693 bool is_trigger_anonymous
,
2694 enum lttng_error_code
*cmd_result
)
2697 struct lttng_condition
*condition
;
2698 struct notification_client_list
*client_list
= NULL
;
2699 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2700 struct cds_lfht_node
*node
;
2701 const char* trigger_name
;
2702 bool free_trigger
= true;
2703 struct lttng_evaluation
*evaluation
= NULL
;
2704 struct lttng_credentials object_creds
;
2707 enum action_executor_status executor_status
;
2708 const uint64_t trigger_tracer_token
=
2709 state
->trigger_id
.next_tracer_token
++;
2713 /* Set the trigger's tracer token. */
2714 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2716 if (!is_trigger_anonymous
) {
2717 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2718 LTTNG_TRIGGER_STATUS_UNSET
) {
2719 const enum lttng_error_code ret_code
=
2720 generate_trigger_name(state
, trigger
,
2723 if (ret_code
!= LTTNG_OK
) {
2726 *cmd_result
= ret_code
;
2729 } else if (trigger_name_taken(state
, trigger
)) {
2730 /* Not a fatal error. */
2731 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2736 trigger_name
= "(anonymous)";
2739 condition
= lttng_trigger_get_condition(trigger
);
2740 LTTNG_ASSERT(condition
);
2742 /* Some conditions require tracers to implement a minimal ABI version. */
2743 if (!condition_is_supported(condition
)) {
2744 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2748 trigger_ht_element
= zmalloc
<lttng_trigger_ht_element
>();
2749 if (!trigger_ht_element
) {
2754 /* Add trigger to the trigger_ht. */
2755 cds_lfht_node_init(&trigger_ht_element
->node
);
2756 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2757 trigger_ht_element
->trigger
= trigger
;
2759 node
= cds_lfht_add_unique(state
->triggers_ht
,
2760 lttng_condition_hash(condition
),
2763 &trigger_ht_element
->node
);
2764 if (node
!= &trigger_ht_element
->node
) {
2765 /* Not a fatal error, simply report it to the client. */
2766 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2767 goto error_free_ht_element
;
2770 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2771 hash_trigger_by_name_uid(trigger
),
2772 match_trigger_by_name_uid
,
2774 &trigger_ht_element
->node_by_name_uid
);
2775 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2776 /* Internal error: add to triggers_ht should have failed. */
2778 goto error_free_ht_element
;
2781 /* From this point consider the trigger registered. */
2782 lttng_trigger_set_as_registered(trigger
);
2785 * Some triggers might need a tracer notifier depending on its
2786 * condition and actions.
2788 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2789 enum lttng_error_code error_code
;
2791 error_code
= setup_tracer_notifier(state
, trigger
);
2792 if (error_code
!= LTTNG_OK
) {
2793 notif_thread_state_remove_trigger_ht_elem(state
,
2794 trigger_ht_element
);
2795 if (error_code
== LTTNG_ERR_NOMEM
) {
2798 *cmd_result
= error_code
;
2802 goto error_free_ht_element
;
2807 * The rest only applies to triggers that have a "notify" action.
2808 * It is not skipped as this is the only action type currently
2811 if (is_trigger_action_notify(trigger
)) {
2813 * Find or create the client list of this condition. It may
2814 * already be present if another trigger is already registered
2815 * with the same condition.
2817 client_list
= get_client_list_from_condition(state
, condition
);
2820 * No client list for this condition yet. We create new
2821 * one and build it up.
2823 client_list
= notification_client_list_create(state
, condition
);
2825 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2826 goto error_free_ht_element
;
2830 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2832 pthread_mutex_lock(&client_list
->lock
);
2833 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2834 pthread_mutex_unlock(&client_list
->lock
);
2838 * Ownership of the trigger and of its wrapper was transfered to
2839 * the triggers_ht. Same for token ht element if necessary.
2841 trigger_ht_element
= NULL
;
2842 free_trigger
= false;
2844 switch (get_condition_binding_object(condition
)) {
2845 case LTTNG_OBJECT_TYPE_SESSION
:
2846 /* Add the trigger to the list if it matches a known session. */
2847 ret
= bind_trigger_to_matching_session(trigger
, state
);
2849 goto error_free_ht_element
;
2852 case LTTNG_OBJECT_TYPE_CHANNEL
:
2854 * Add the trigger to list of triggers bound to the channels
2857 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2859 goto error_free_ht_element
;
2862 case LTTNG_OBJECT_TYPE_NONE
:
2865 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2867 goto error_free_ht_element
;
2871 * The new trigger's condition must be evaluated against the current
2874 * In the case of `notify` action, nothing preventing clients from
2875 * subscribing to a condition before the corresponding trigger is
2876 * registered, we have to evaluate this new condition right away.
2878 * At some point, we were waiting for the next "evaluation" (e.g. on
2879 * reception of a channel sample) to evaluate this new condition, but
2882 * The reason it was broken is that waiting for the next sample
2883 * does not allow us to properly handle transitions for edge-triggered
2886 * Consider this example: when we handle a new channel sample, we
2887 * evaluate each conditions twice: once with the previous state, and
2888 * again with the newest state. We then use those two results to
2889 * determine whether a state change happened: a condition was false and
2890 * became true. If a state change happened, we have to notify clients.
2892 * Now, if a client subscribes to a given notification and registers
2893 * a trigger *after* that subscription, we have to make sure the
2894 * condition is evaluated at this point while considering only the
2895 * current state. Otherwise, the next evaluation cycle may only see
2896 * that the evaluations remain the same (true for samples n-1 and n) and
2897 * the client will never know that the condition has been met.
2899 switch (get_condition_binding_object(condition
)) {
2900 case LTTNG_OBJECT_TYPE_SESSION
:
2901 ret
= evaluate_session_condition_for_client(condition
, state
,
2902 &evaluation
, &object_uid
,
2904 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2905 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2907 case LTTNG_OBJECT_TYPE_CHANNEL
:
2908 ret
= evaluate_channel_condition_for_client(condition
, state
,
2909 &evaluation
, &object_uid
,
2911 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2912 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2914 case LTTNG_OBJECT_TYPE_NONE
:
2917 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2925 goto error_free_ht_element
;
2928 DBG("Newly registered trigger's condition evaluated to %s",
2929 evaluation
? "true" : "false");
2931 /* Evaluation yielded nothing. Normal exit. */
2937 * Ownership of `evaluation` transferred to the action executor
2938 * no matter the result.
2940 executor_status
= action_executor_enqueue_trigger(state
->executor
,
2941 trigger
, evaluation
, &object_creds
, client_list
);
2943 switch (executor_status
) {
2944 case ACTION_EXECUTOR_STATUS_OK
:
2946 case ACTION_EXECUTOR_STATUS_ERROR
:
2947 case ACTION_EXECUTOR_STATUS_INVALID
:
2949 * TODO Add trigger identification (name/id) when
2950 * it is added to the API.
2952 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2954 goto error_free_ht_element
;
2955 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2957 * TODO Add trigger identification (name/id) when
2958 * it is added to the API.
2960 * Not a fatal error.
2962 WARN("No space left when enqueuing action associated to newly registered trigger");
2970 *cmd_result
= LTTNG_OK
;
2971 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2972 trigger_name
, trigger_tracer_token
);
2975 error_free_ht_element
:
2976 if (trigger_ht_element
) {
2977 /* Delayed removal due to RCU constraint on delete. */
2978 call_rcu(&trigger_ht_element
->rcu_node
,
2979 free_lttng_trigger_ht_element_rcu
);
2984 * Other objects might have a reference to the trigger, mark it
2987 lttng_trigger_set_as_unregistered(trigger
);
2988 lttng_trigger_destroy(trigger
);
2996 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2998 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
3003 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
3005 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
3010 void teardown_tracer_notifier(struct notification_thread_state
*state
,
3011 const struct lttng_trigger
*trigger
)
3013 struct cds_lfht_iter iter
;
3014 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
3016 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
3017 trigger_tokens_ht_element
, node
) {
3019 if (!lttng_trigger_is_equal(trigger
,
3020 trigger_tokens_ht_element
->trigger
)) {
3024 event_notifier_error_accounting_unregister_event_notifier(
3025 trigger_tokens_ht_element
->trigger
);
3027 /* TODO talk to all app and remove it */
3028 DBG("Removed trigger from tokens_ht");
3029 cds_lfht_del(state
->trigger_tokens_ht
,
3030 &trigger_tokens_ht_element
->node
);
3032 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
3033 free_notification_trigger_tokens_ht_element_rcu
);
3040 int handle_notification_thread_command_unregister_trigger(
3041 struct notification_thread_state
*state
,
3042 const struct lttng_trigger
*trigger
,
3043 enum lttng_error_code
*_cmd_reply
)
3045 struct cds_lfht_iter iter
;
3046 struct cds_lfht_node
*triggers_ht_node
;
3047 struct lttng_channel_trigger_list
*trigger_list
;
3048 struct notification_client_list
*client_list
;
3049 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
3050 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
3052 enum lttng_error_code cmd_reply
;
3056 cds_lfht_lookup(state
->triggers_ht
,
3057 lttng_condition_hash(condition
),
3061 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3062 if (!triggers_ht_node
) {
3063 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3066 cmd_reply
= LTTNG_OK
;
3069 trigger_ht_element
= caa_container_of(triggers_ht_node
,
3070 struct lttng_trigger_ht_element
, node
);
3072 /* Remove trigger from channel_triggers_ht. */
3073 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
3074 channel_triggers_ht_node
) {
3075 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3077 cds_list_for_each_entry_safe(trigger_element
, tmp
,
3078 &trigger_list
->list
, node
) {
3079 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3083 DBG("Removed trigger from channel_triggers_ht");
3084 cds_list_del(&trigger_element
->node
);
3085 /* A trigger can only appear once per channel */
3090 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3091 teardown_tracer_notifier(state
, trigger
);
3094 if (is_trigger_action_notify(trigger
)) {
3096 * Remove and release the client list from
3097 * notification_trigger_clients_ht.
3099 client_list
= get_client_list_from_condition(state
, condition
);
3100 LTTNG_ASSERT(client_list
);
3102 pthread_mutex_lock(&client_list
->lock
);
3103 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3104 pthread_mutex_unlock(&client_list
->lock
);
3106 /* Put new reference and the hashtable's reference. */
3107 notification_client_list_put(client_list
);
3108 notification_client_list_put(client_list
);
3112 /* Remove trigger from triggers_ht. */
3113 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3115 /* Release the ownership of the trigger. */
3116 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3117 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3121 *_cmd_reply
= cmd_reply
;
3127 int pop_cmd_queue(struct notification_thread_handle
*handle
,
3128 struct notification_thread_command
**cmd
)
3133 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3134 ret
= lttng_read(handle
->cmd_queue
.event_fd
, &counter
, sizeof(counter
));
3135 if (ret
!= sizeof(counter
)) {
3140 /* Simulate behaviour of EFD_SEMAPHORE for older kernels. */
3143 ret
= lttng_write(handle
->cmd_queue
.event_fd
, &counter
,
3145 if (ret
!= sizeof(counter
)) {
3146 PERROR("Failed to write back to event_fd for EFD_SEMAPHORE emulation");
3152 *cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3153 struct notification_thread_command
, cmd_list_node
);
3154 cds_list_del(&((*cmd
)->cmd_list_node
));
3158 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3162 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3163 int handle_notification_thread_command(
3164 struct notification_thread_handle
*handle
,
3165 struct notification_thread_state
*state
)
3168 struct notification_thread_command
*cmd
;
3170 ret
= pop_cmd_queue(handle
, &cmd
);
3175 DBG("Received `%s` command",
3176 notification_command_type_str(cmd
->type
));
3177 switch (cmd
->type
) {
3178 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3179 ret
= handle_notification_thread_command_register_trigger(state
,
3180 cmd
->parameters
.register_trigger
.trigger
,
3181 cmd
->parameters
.register_trigger
.is_trigger_anonymous
,
3184 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3185 ret
= handle_notification_thread_command_unregister_trigger(
3187 cmd
->parameters
.unregister_trigger
.trigger
,
3190 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3191 ret
= handle_notification_thread_command_add_channel(
3193 cmd
->parameters
.add_channel
.session
.name
,
3194 cmd
->parameters
.add_channel
.session
.uid
,
3195 cmd
->parameters
.add_channel
.session
.gid
,
3196 cmd
->parameters
.add_channel
.channel
.name
,
3197 cmd
->parameters
.add_channel
.channel
.domain
,
3198 cmd
->parameters
.add_channel
.channel
.key
,
3199 cmd
->parameters
.add_channel
.channel
.capacity
,
3202 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3203 ret
= handle_notification_thread_command_remove_channel(
3204 state
, cmd
->parameters
.remove_channel
.key
,
3205 cmd
->parameters
.remove_channel
.domain
,
3208 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3209 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3210 ret
= handle_notification_thread_command_session_rotation(
3213 cmd
->parameters
.session_rotation
.session_name
,
3214 cmd
->parameters
.session_rotation
.uid
,
3215 cmd
->parameters
.session_rotation
.gid
,
3216 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3217 cmd
->parameters
.session_rotation
.location
,
3220 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3221 ret
= handle_notification_thread_command_add_tracer_event_source(
3223 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3224 cmd
->parameters
.tracer_event_source
.domain
,
3227 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3228 ret
= handle_notification_thread_command_remove_tracer_event_source(
3230 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3233 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3235 struct lttng_triggers
*triggers
= NULL
;
3237 ret
= handle_notification_thread_command_list_triggers(
3240 cmd
->parameters
.list_triggers
.uid
,
3243 cmd
->reply
.list_triggers
.triggers
= triggers
;
3247 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3248 cmd
->reply_code
= LTTNG_OK
;
3251 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3253 struct lttng_trigger
*trigger
= NULL
;
3255 ret
= handle_notification_thread_command_get_trigger(state
,
3256 cmd
->parameters
.get_trigger
.trigger
, &trigger
,
3258 cmd
->reply
.get_trigger
.trigger
= trigger
;
3261 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3263 const enum client_transmission_status client_status
=
3264 cmd
->parameters
.client_communication_update
3266 const notification_client_id client_id
=
3267 cmd
->parameters
.client_communication_update
.id
;
3268 struct notification_client
*client
;
3271 client
= get_client_from_id(client_id
, state
);
3275 * Client error was probably already picked-up by the
3276 * notification thread or it has disconnected
3277 * gracefully while this command was queued.
3279 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3283 ret
= client_handle_transmission_status(
3284 client
, client_status
, state
);
3290 ERR("Unknown internal command received");
3298 if (cmd
->is_async
) {
3302 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3306 /* Wake-up and return a fatal error to the calling thread. */
3307 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3308 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3310 /* Indicate a fatal error to the caller. */
3315 int socket_set_non_blocking(int socket
)
3319 /* Set the pipe as non-blocking. */
3320 ret
= fcntl(socket
, F_GETFL
, 0);
3322 PERROR("fcntl get socket flags");
3327 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3329 PERROR("fcntl set O_NONBLOCK socket flag");
3332 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3338 int client_reset_inbound_state(struct notification_client
*client
)
3343 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3345 client
->communication
.inbound
.bytes_to_receive
=
3346 sizeof(struct lttng_notification_channel_message
);
3347 client
->communication
.inbound
.msg_type
=
3348 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3349 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3350 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3351 ret
= lttng_dynamic_buffer_set_size(
3352 &client
->communication
.inbound
.payload
.buffer
,
3353 client
->communication
.inbound
.bytes_to_receive
);
3358 int handle_notification_thread_client_connect(
3359 struct notification_thread_state
*state
)
3362 struct notification_client
*client
;
3364 DBG("Handling new notification channel client connection");
3366 client
= zmalloc
<notification_client
>();
3373 pthread_mutex_init(&client
->lock
, NULL
);
3374 client
->id
= state
->next_notification_client_id
++;
3375 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3376 lttng_payload_init(&client
->communication
.inbound
.payload
);
3377 lttng_payload_init(&client
->communication
.outbound
.payload
);
3378 client
->communication
.inbound
.expect_creds
= true;
3380 ret
= client_reset_inbound_state(client
);
3382 ERR("Failed to reset client communication's inbound state");
3387 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3389 ERR("Failed to accept new notification channel client connection");
3394 client
->socket
= ret
;
3396 ret
= socket_set_non_blocking(client
->socket
);
3398 ERR("Failed to set new notification channel client connection socket as non-blocking");
3402 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3404 ERR("Failed to set socket options on new notification channel client socket");
3409 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3410 LPOLLIN
| LPOLLERR
|
3411 LPOLLHUP
| LPOLLRDHUP
);
3413 ERR("Failed to add notification channel client socket to poll set");
3417 DBG("Added new notification channel client socket (%i) to poll set",
3421 cds_lfht_add(state
->client_socket_ht
,
3422 hash_client_socket(client
->socket
),
3423 &client
->client_socket_ht_node
);
3424 cds_lfht_add(state
->client_id_ht
,
3425 hash_client_id(client
->id
),
3426 &client
->client_id_ht_node
);
3432 notification_client_destroy(client
);
3437 * RCU read-lock must be held by the caller.
3438 * Client lock must _not_ be held by the caller.
3441 int notification_thread_client_disconnect(
3442 struct notification_client
*client
,
3443 struct notification_thread_state
*state
)
3446 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3448 ASSERT_RCU_READ_LOCKED();
3450 /* Acquire the client lock to disable its communication atomically. */
3451 pthread_mutex_lock(&client
->lock
);
3452 client
->communication
.active
= false;
3453 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3454 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3455 pthread_mutex_unlock(&client
->lock
);
3457 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3459 ERR("Failed to remove client socket %d from poll set",
3463 /* Release all conditions to which the client was subscribed. */
3464 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3465 &client
->condition_list
, node
) {
3466 (void) notification_thread_client_unsubscribe(client
,
3467 condition_list_element
->condition
, state
, NULL
);
3471 * Client no longer accessible to other threads (through the
3474 notification_client_destroy(client
);
3478 int handle_notification_thread_client_disconnect(
3479 int client_socket
, struct notification_thread_state
*state
)
3482 struct notification_client
*client
;
3485 DBG("Closing client connection (socket fd = %i)",
3487 client
= get_client_from_socket(client_socket
, state
);
3489 /* Internal state corruption, fatal error. */
3490 ERR("Unable to find client (socket fd = %i)",
3496 ret
= notification_thread_client_disconnect(client
, state
);
3502 int handle_notification_thread_client_disconnect_all(
3503 struct notification_thread_state
*state
)
3505 struct cds_lfht_iter iter
;
3506 struct notification_client
*client
;
3507 bool error_encoutered
= false;
3510 DBG("Closing all client connections");
3511 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3512 client_socket_ht_node
) {
3515 ret
= notification_thread_client_disconnect(
3518 error_encoutered
= true;
3522 return error_encoutered
? 1 : 0;
3525 int handle_notification_thread_trigger_unregister_all(
3526 struct notification_thread_state
*state
)
3528 bool error_occurred
= false;
3529 struct cds_lfht_iter iter
;
3530 struct lttng_trigger_ht_element
*trigger_ht_element
;
3533 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3535 int ret
= handle_notification_thread_command_unregister_trigger(
3536 state
, trigger_ht_element
->trigger
, NULL
);
3538 error_occurred
= true;
3542 return error_occurred
? -1 : 0;
3546 int client_handle_transmission_status(
3547 struct notification_client
*client
,
3548 enum client_transmission_status transmission_status
,
3549 struct notification_thread_state
*state
)
3553 switch (transmission_status
) {
3554 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3555 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3556 CLIENT_POLL_MASK_IN
);
3562 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3564 * We want to be notified whenever there is buffer space
3565 * available to send the rest of the payload.
3567 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3568 CLIENT_POLL_MASK_IN_OUT
);
3573 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3574 ret
= notification_thread_client_disconnect(client
, state
);
3579 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3589 /* Client lock must be acquired by caller. */
3591 enum client_transmission_status
client_flush_outgoing_queue(
3592 struct notification_client
*client
)
3595 size_t to_send_count
;
3596 enum client_transmission_status status
;
3597 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3598 &client
->communication
.outbound
.payload
, 0, -1);
3599 const int fds_to_send_count
=
3600 lttng_payload_view_get_fd_handle_count(&pv
);
3602 ASSERT_LOCKED(client
->lock
);
3604 if (!client
->communication
.active
) {
3605 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3609 if (pv
.buffer
.size
== 0) {
3611 * If both data and fds are equal to zero, we are in an invalid
3614 LTTNG_ASSERT(fds_to_send_count
!= 0);
3619 to_send_count
= pv
.buffer
.size
;
3620 DBG("Flushing client (socket fd = %i) outgoing queue",
3623 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3626 if ((ret
>= 0 && ret
< to_send_count
)) {
3627 DBG("Client (socket fd = %i) outgoing queue could not be completely flushed",
3629 to_send_count
-= std::max(ret
, (ssize_t
) 0);
3631 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3633 pv
.buffer
.size
- to_send_count
,
3635 ret
= lttng_dynamic_buffer_set_size(
3636 &client
->communication
.outbound
.payload
.buffer
,
3642 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3644 } else if (ret
< 0) {
3645 /* Generic error, disable the client's communication. */
3646 ERR("Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3648 client
->communication
.active
= false;
3649 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3653 * No error and flushed the queue completely.
3655 * The payload buffer size is used later to
3656 * check if there is notifications queued. So albeit that the
3657 * direct caller knows that the transmission is complete, we
3658 * need to set the buffer size to zero.
3660 ret
= lttng_dynamic_buffer_set_size(
3661 &client
->communication
.outbound
.payload
.buffer
, 0);
3668 /* No fds to send, transmission is complete. */
3669 if (fds_to_send_count
== 0) {
3670 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3674 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3675 client
->socket
, &pv
);
3677 /* Generic error, disable the client's communication. */
3678 ERR("Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3680 client
->communication
.active
= false;
3681 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3683 } else if (ret
== 0) {
3684 /* Nothing could be sent. */
3685 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3687 /* Fd passing is an all or nothing kind of thing. */
3688 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3690 * The payload _fd_array count is used later to
3691 * check if there is notifications queued. So although the
3692 * direct caller knows that the transmission is complete, we
3693 * need to clear the _fd_array for the queuing check.
3695 lttng_dynamic_pointer_array_clear(
3696 &client
->communication
.outbound
.payload
3701 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3702 client
->communication
.outbound
.queued_command_reply
= false;
3703 client
->communication
.outbound
.dropped_notification
= false;
3704 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3709 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3713 bool client_has_outbound_data_left(
3714 const struct notification_client
*client
)
3716 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3717 &client
->communication
.outbound
.payload
, 0, -1);
3718 const bool has_data
= pv
.buffer
.size
!= 0;
3719 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3721 return has_data
|| has_fds
;
3724 /* Client lock must _not_ be held by the caller. */
3726 int client_send_command_reply(struct notification_client
*client
,
3727 struct notification_thread_state
*state
,
3728 enum lttng_notification_channel_status status
)
3731 struct lttng_notification_channel_command_reply reply
= {
3732 .status
= (int8_t) status
,
3734 struct lttng_notification_channel_message msg
= {
3735 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3736 .size
= sizeof(reply
),
3739 char buffer
[sizeof(msg
) + sizeof(reply
)];
3740 enum client_transmission_status transmission_status
;
3742 memcpy(buffer
, &msg
, sizeof(msg
));
3743 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3744 DBG("Send command reply (%i)", (int) status
);
3746 pthread_mutex_lock(&client
->lock
);
3747 if (client
->communication
.outbound
.queued_command_reply
) {
3748 /* Protocol error. */
3752 /* Enqueue buffer to outgoing queue and flush it. */
3753 ret
= lttng_dynamic_buffer_append(
3754 &client
->communication
.outbound
.payload
.buffer
,
3755 buffer
, sizeof(buffer
));
3760 transmission_status
= client_flush_outgoing_queue(client
);
3762 if (client_has_outbound_data_left(client
)) {
3763 /* Queue could not be emptied. */
3764 client
->communication
.outbound
.queued_command_reply
= true;
3767 pthread_mutex_unlock(&client
->lock
);
3768 ret
= client_handle_transmission_status(
3769 client
, transmission_status
, state
);
3776 pthread_mutex_unlock(&client
->lock
);
3782 int client_handle_message_unknown(struct notification_client
*client
,
3783 struct notification_thread_state
*state
__attribute__((unused
)))
3787 * Receiving message header. The function will be called again
3788 * once the rest of the message as been received and can be
3791 const struct lttng_notification_channel_message
*msg
;
3793 LTTNG_ASSERT(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3794 msg
= (const struct lttng_notification_channel_message
*)
3795 client
->communication
.inbound
.payload
.buffer
.data
;
3797 if (msg
->size
== 0 ||
3798 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3799 ERR("Invalid notification channel message: length = %u",
3805 switch (msg
->type
) {
3806 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3807 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3808 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3812 ERR("Invalid notification channel message: unexpected message type");
3816 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3817 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3818 client
->communication
.inbound
.msg_type
=
3819 (enum lttng_notification_channel_message_type
) msg
->type
;
3820 ret
= lttng_dynamic_buffer_set_size(
3821 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3823 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3830 int client_handle_message_handshake(struct notification_client
*client
,
3831 struct notification_thread_state
*state
)
3834 struct lttng_notification_channel_command_handshake
*handshake_client
;
3835 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3836 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3837 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3839 const struct lttng_notification_channel_message msg_header
= {
3840 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3841 .size
= sizeof(handshake_reply
),
3844 enum lttng_notification_channel_status status
=
3845 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3846 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3848 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3849 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3850 sizeof(handshake_reply
));
3853 (struct lttng_notification_channel_command_handshake
*)
3854 client
->communication
.inbound
.payload
.buffer
3856 client
->major
= handshake_client
->major
;
3857 client
->minor
= handshake_client
->minor
;
3858 if (!client
->communication
.inbound
.creds_received
) {
3859 ERR("No credentials received from client");
3864 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3865 &client
->communication
.inbound
.creds
);
3866 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3867 &client
->communication
.inbound
.creds
);
3868 client
->is_sessiond
= LTTNG_SOCK_GET_PID_CRED(&client
->communication
.inbound
.creds
) == getpid();
3869 DBG("Received handshake from client: uid = %u, gid = %u, protocol version = %i.%i, client is sessiond = %s",
3870 client
->uid
, client
->gid
, (int) client
->major
,
3871 (int) client
->minor
,
3872 client
->is_sessiond
? "true" : "false");
3874 if (handshake_client
->major
!=
3875 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3876 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3879 pthread_mutex_lock(&client
->lock
);
3880 /* Outgoing queue will be flushed when the command reply is sent. */
3881 ret
= lttng_dynamic_buffer_append(
3882 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3883 sizeof(send_buffer
));
3885 ERR("Failed to send protocol version to notification channel client");
3889 client
->validated
= true;
3890 client
->communication
.active
= true;
3891 pthread_mutex_unlock(&client
->lock
);
3893 /* Set reception state to receive the next message header. */
3894 ret
= client_reset_inbound_state(client
);
3896 ERR("Failed to reset client communication's inbound state");
3900 /* Flushes the outgoing queue. */
3901 ret
= client_send_command_reply(client
, state
, status
);
3903 ERR("Failed to send reply to notification channel client");
3909 pthread_mutex_unlock(&client
->lock
);
3915 int client_handle_message_subscription(
3916 struct notification_client
*client
,
3917 enum lttng_notification_channel_message_type msg_type
,
3918 struct notification_thread_state
*state
)
3921 struct lttng_condition
*condition
;
3922 enum lttng_notification_channel_status status
=
3923 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3924 struct lttng_payload_view condition_view
=
3925 lttng_payload_view_from_payload(
3926 &client
->communication
.inbound
.payload
,
3928 size_t expected_condition_size
;
3931 * No need to lock client to sample the inbound state as the only
3932 * other thread accessing clients (action executor) only uses the
3935 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3936 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3937 if (ret
!= expected_condition_size
) {
3938 ERR("Malformed condition received from client");
3942 /* Ownership of condition is always transferred. */
3943 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3944 ret
= notification_thread_client_subscribe(
3945 client
, condition
, state
, &status
);
3947 ret
= notification_thread_client_unsubscribe(
3948 client
, condition
, state
, &status
);
3955 /* Set reception state to receive the next message header. */
3956 ret
= client_reset_inbound_state(client
);
3958 ERR("Failed to reset client communication's inbound state");
3962 ret
= client_send_command_reply(client
, state
, status
);
3964 ERR("Failed to send reply to notification channel client");
3973 int client_dispatch_message(struct notification_client
*client
,
3974 struct notification_thread_state
*state
)
3978 if (client
->communication
.inbound
.msg_type
!=
3979 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3980 client
->communication
.inbound
.msg_type
!=
3981 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3982 !client
->validated
) {
3983 WARN("client attempted a command before handshake");
3988 switch (client
->communication
.inbound
.msg_type
) {
3989 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3991 ret
= client_handle_message_unknown(client
, state
);
3994 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3996 ret
= client_handle_message_handshake(client
, state
);
3999 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
4000 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
4002 ret
= client_handle_message_subscription(client
,
4003 client
->communication
.inbound
.msg_type
, state
);
4013 /* Incoming data from client. */
4014 int handle_notification_thread_client_in(
4015 struct notification_thread_state
*state
, int socket
)
4018 struct notification_client
*client
;
4023 client
= get_client_from_socket(socket
, state
);
4025 /* Internal error, abort. */
4030 if (client
->communication
.inbound
.bytes_to_receive
== 0 &&
4031 client
->communication
.inbound
.fds_to_receive
!= 0) {
4032 /* Only FDs left to receive. */
4036 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
4037 client
->communication
.inbound
.bytes_to_receive
;
4038 if (client
->communication
.inbound
.expect_creds
) {
4039 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
4040 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
4041 client
->communication
.inbound
.bytes_to_receive
,
4042 &client
->communication
.inbound
.creds
);
4044 client
->communication
.inbound
.expect_creds
= false;
4045 client
->communication
.inbound
.creds_received
= true;
4048 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
4049 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
4050 client
->communication
.inbound
.bytes_to_receive
);
4052 if (recv_ret
>= 0) {
4053 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
4055 goto error_disconnect_client
;
4058 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
4059 /* Message incomplete wait for more data. */
4065 LTTNG_ASSERT(client
->communication
.inbound
.bytes_to_receive
== 0);
4068 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
4069 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
4071 client
->communication
.inbound
.fds_to_receive
,
4072 &client
->communication
.inbound
.payload
);
4075 * Fds received. non blocking fds passing is all
4078 ssize_t expected_size
;
4080 expected_size
= sizeof(int) *
4081 client
->communication
.inbound
4083 LTTNG_ASSERT(ret
== expected_size
);
4084 client
->communication
.inbound
.fds_to_receive
= 0;
4085 } else if (ret
== 0) {
4086 /* Received nothing. */
4090 goto error_disconnect_client
;
4094 /* At this point the message is complete.*/
4095 LTTNG_ASSERT(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4096 client
->communication
.inbound
.fds_to_receive
== 0);
4097 ret
= client_dispatch_message(client
, state
);
4100 * Only returns an error if this client must be
4103 goto error_disconnect_client
;
4110 error_disconnect_client
:
4111 ret
= notification_thread_client_disconnect(client
, state
);
4115 /* Client ready to receive outgoing data. */
4116 int handle_notification_thread_client_out(
4117 struct notification_thread_state
*state
, int socket
)
4120 struct notification_client
*client
;
4121 enum client_transmission_status transmission_status
;
4124 client
= get_client_from_socket(socket
, state
);
4126 /* Internal error, abort. */
4131 pthread_mutex_lock(&client
->lock
);
4132 transmission_status
= client_flush_outgoing_queue(client
);
4133 pthread_mutex_unlock(&client
->lock
);
4135 ret
= client_handle_transmission_status(
4136 client
, transmission_status
, state
);
4146 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4147 const struct channel_state_sample
*sample
,
4148 uint64_t buffer_capacity
)
4150 bool result
= false;
4152 enum lttng_condition_type condition_type
;
4153 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4154 condition
, struct lttng_condition_buffer_usage
,
4157 if (use_condition
->threshold_bytes
.set
) {
4158 threshold
= use_condition
->threshold_bytes
.value
;
4161 * Threshold was expressed as a ratio.
4163 * TODO the threshold (in bytes) of conditions expressed
4164 * as a ratio of total buffer size could be cached to
4165 * forego this double-multiplication or it could be performed
4166 * as fixed-point math.
4168 * Note that caching should accommodates the case where the
4169 * condition applies to multiple channels (i.e. don't assume
4170 * that all channels matching my_chann* have the same size...)
4172 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4173 (double) buffer_capacity
);
4176 condition_type
= lttng_condition_get_type(condition
);
4177 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4178 DBG("Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4179 threshold
, sample
->highest_usage
);
4182 * The low condition should only be triggered once _all_ of the
4183 * streams in a channel have gone below the "low" threshold.
4185 if (sample
->highest_usage
<= threshold
) {
4189 DBG("High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4190 threshold
, sample
->highest_usage
);
4193 * For high buffer usage scenarios, we want to trigger whenever
4194 * _any_ of the streams has reached the "high" threshold.
4196 if (sample
->highest_usage
>= threshold
) {
4205 bool evaluate_session_consumed_size_condition(
4206 const struct lttng_condition
*condition
,
4207 uint64_t session_consumed_size
)
4210 const struct lttng_condition_session_consumed_size
*size_condition
=
4211 container_of(condition
,
4212 struct lttng_condition_session_consumed_size
,
4215 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4216 DBG("Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4217 threshold
, session_consumed_size
);
4218 return session_consumed_size
>= threshold
;
4222 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4223 struct lttng_evaluation
**evaluation
,
4224 const struct notification_thread_state
*state
__attribute__((unused
)),
4225 const struct channel_state_sample
*previous_sample
,
4226 const struct channel_state_sample
*latest_sample
,
4227 uint64_t previous_session_consumed_total
,
4228 uint64_t latest_session_consumed_total
,
4229 struct channel_info
*channel_info
)
4232 enum lttng_condition_type condition_type
;
4233 const bool previous_sample_available
= !!previous_sample
;
4234 bool previous_sample_result
= false;
4235 bool latest_sample_result
;
4237 condition_type
= lttng_condition_get_type(condition
);
4239 switch (condition_type
) {
4240 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4241 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4242 if (caa_likely(previous_sample_available
)) {
4243 previous_sample_result
=
4244 evaluate_buffer_usage_condition(condition
,
4245 previous_sample
, channel_info
->capacity
);
4247 latest_sample_result
= evaluate_buffer_usage_condition(
4248 condition
, latest_sample
,
4249 channel_info
->capacity
);
4251 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4252 if (caa_likely(previous_sample_available
)) {
4253 previous_sample_result
=
4254 evaluate_session_consumed_size_condition(
4256 previous_session_consumed_total
);
4258 latest_sample_result
=
4259 evaluate_session_consumed_size_condition(
4261 latest_session_consumed_total
);
4264 /* Unknown condition type; internal error. */
4268 if (!latest_sample_result
||
4269 (previous_sample_result
== latest_sample_result
)) {
4271 * Only trigger on a condition evaluation transition.
4273 * NOTE: This edge-triggered logic may not be appropriate for
4274 * future condition types.
4279 if (!evaluation
|| !latest_sample_result
) {
4283 switch (condition_type
) {
4284 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4285 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4286 *evaluation
= lttng_evaluation_buffer_usage_create(
4288 latest_sample
->highest_usage
,
4289 channel_info
->capacity
);
4291 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4292 *evaluation
= lttng_evaluation_session_consumed_size_create(
4293 latest_session_consumed_total
);
4308 int client_notification_overflow(struct notification_client
*client
)
4311 const struct lttng_notification_channel_message msg
= {
4312 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4317 ASSERT_LOCKED(client
->lock
);
4319 DBG("Dropping notification addressed to client (socket fd = %i)",
4321 if (client
->communication
.outbound
.dropped_notification
) {
4323 * The client already has a "notification dropped" message
4324 * in its outgoing queue. Nothing to do since all
4325 * of those messages are coalesced.
4330 client
->communication
.outbound
.dropped_notification
= true;
4331 ret
= lttng_dynamic_buffer_append(
4332 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4335 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4342 static int client_handle_transmission_status_wrapper(
4343 struct notification_client
*client
,
4344 enum client_transmission_status status
,
4347 return client_handle_transmission_status(client
, status
,
4348 (struct notification_thread_state
*) user_data
);
4352 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4353 const struct lttng_evaluation
*evaluation
,
4354 struct notification_client_list
* client_list
,
4355 struct notification_thread_state
*state
,
4356 uid_t object_uid
, gid_t object_gid
)
4358 const struct lttng_credentials creds
= {
4359 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4360 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4363 return notification_client_list_send_evaluation(client_list
,
4364 trigger
, evaluation
,
4366 client_handle_transmission_status_wrapper
, state
);
4370 * Permission checks relative to notification channel clients are performed
4371 * here. Notice how object, client, and trigger credentials are involved in
4374 * The `object` credentials are the credentials associated with the "subject"
4375 * of a condition. For instance, a `rotation completed` condition applies
4376 * to a session. When that condition is met, it will produce an evaluation
4377 * against a session. Hence, in this case, the `object` credentials are the
4378 * credentials of the "subject" session.
4380 * The `trigger` credentials are the credentials of the user that registered the
4383 * The `client` credentials are the credentials of the user that created a given
4384 * notification channel.
4386 * In terms of visibility, it is expected that non-privilieged users can only
4387 * register triggers against "their" objects (their own sessions and
4388 * applications they are allowed to interact with). They can then open a
4389 * notification channel and subscribe to notifications associated with those
4392 * As for privilieged users, they can register triggers against the objects of
4393 * other users. They can then subscribe to the notifications associated to their
4394 * triggers. Privilieged users _can't_ subscribe to the notifications of
4395 * triggers owned by other users; they must create their own triggers.
4397 * This is more a concern of usability than security. It would be difficult for
4398 * a root user reliably subscribe to a specific set of conditions without
4399 * interference from external users (those could, for instance, unregister
4402 int notification_client_list_send_evaluation(
4403 struct notification_client_list
*client_list
,
4404 const struct lttng_trigger
*trigger
,
4405 const struct lttng_evaluation
*evaluation
,
4406 const struct lttng_credentials
*source_object_creds
,
4407 report_client_transmission_result_cb client_report
,
4411 struct lttng_payload msg_payload
;
4412 struct notification_client_list_element
*client_list_element
, *tmp
;
4413 const struct lttng_notification notification
= {
4414 .trigger
= (struct lttng_trigger
*) trigger
,
4415 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4417 struct lttng_notification_channel_message msg_header
= {
4418 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4422 const struct lttng_credentials
*trigger_creds
=
4423 lttng_trigger_get_credentials(trigger
);
4425 lttng_payload_init(&msg_payload
);
4427 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4428 sizeof(msg_header
));
4433 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4435 ERR("Failed to serialize notification");
4440 /* Update payload size. */
4441 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4442 ->size
= (uint32_t)(
4443 msg_payload
.buffer
.size
- sizeof(msg_header
));
4445 /* Update the payload number of fds. */
4447 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4448 &msg_payload
, 0, -1);
4450 ((struct lttng_notification_channel_message
*)
4451 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4452 lttng_payload_view_get_fd_handle_count(&pv
);
4455 pthread_mutex_lock(&client_list
->lock
);
4456 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4457 &client_list
->clients_list
, node
) {
4458 enum client_transmission_status transmission_status
;
4459 struct notification_client
*client
=
4460 client_list_element
->client
;
4463 pthread_mutex_lock(&client
->lock
);
4464 if (!client
->communication
.active
) {
4466 * Skip inactive client (protocol error or
4469 DBG("Skipping client at it is marked as inactive");
4473 if (lttng_trigger_is_hidden(trigger
) && !client
->is_sessiond
) {
4475 * Notifications resulting from an hidden trigger are
4476 * only sent to the session daemon.
4478 DBG("Skipping client as the trigger is hidden and the client is not the session daemon");
4482 if (source_object_creds
) {
4483 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4484 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4487 * Client is not allowed to monitor this
4490 DBG("Skipping client at it does not have the object permission to receive notification for this trigger");
4495 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
)) {
4496 DBG("Skipping client at it does not have the permission to receive notification for this trigger");
4500 DBG("Sending notification to client (fd = %i, %zu bytes)",
4501 client
->socket
, msg_payload
.buffer
.size
);
4503 if (client_has_outbound_data_left(client
)) {
4505 * Outgoing data is already buffered for this client;
4506 * drop the notification and enqueue a "dropped
4507 * notification" message if this is the first dropped
4508 * notification since the socket spilled-over to the
4511 ret
= client_notification_overflow(client
);
4518 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4524 transmission_status
= client_flush_outgoing_queue(client
);
4525 pthread_mutex_unlock(&client
->lock
);
4526 ret
= client_report(client
, transmission_status
, user_data
);
4529 goto end_unlock_list
;
4535 pthread_mutex_unlock(&client
->lock
);
4538 goto end_unlock_list
;
4544 pthread_mutex_unlock(&client_list
->lock
);
4546 lttng_payload_reset(&msg_payload
);
4551 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4552 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4556 struct lttng_event_notifier_notification
*notification
= NULL
;
4557 char *capture_buffer
= NULL
;
4558 size_t capture_buffer_size
;
4559 void *reception_buffer
;
4560 size_t reception_size
;
4562 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4563 struct lttng_kernel_abi_event_notifier_notification kernel_notification
;
4565 /* Init lttng_event_notifier_notification */
4567 case LTTNG_DOMAIN_UST
:
4568 reception_buffer
= (void *) &ust_notification
;
4569 reception_size
= sizeof(ust_notification
);
4571 case LTTNG_DOMAIN_KERNEL
:
4572 reception_buffer
= (void *) &kernel_notification
;
4573 reception_size
= sizeof(kernel_notification
);
4580 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4581 * ensuring that read/write of tracer notifications are atomic.
4583 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4585 if (ret
!= reception_size
) {
4586 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4587 notification_pipe_read_fd
, reception_size
, ret
);
4593 case LTTNG_DOMAIN_UST
:
4594 token
= ust_notification
.token
;
4595 capture_buffer_size
= ust_notification
.capture_buf_size
;
4597 case LTTNG_DOMAIN_KERNEL
:
4598 token
= kernel_notification
.token
;
4599 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4605 if (capture_buffer_size
== 0) {
4606 capture_buffer
= NULL
;
4610 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4611 ERR("Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4612 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4616 capture_buffer
= calloc
<char>(capture_buffer_size
);
4617 if (!capture_buffer
) {
4618 ERR("Failed to allocate capture buffer");
4622 /* Fetch additional payload (capture). */
4623 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4624 if (ret
!= capture_buffer_size
) {
4625 ERR("Failed to read from event source pipe (fd = %i)",
4626 notification_pipe_read_fd
);
4631 notification
= lttng_event_notifier_notification_create(token
, domain
,
4632 capture_buffer
, capture_buffer_size
);
4633 if (notification
== NULL
) {
4638 * Ownership transfered to the lttng_event_notifier_notification object.
4640 capture_buffer
= NULL
;
4643 free(capture_buffer
);
4644 return notification
;
4648 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4649 struct lttng_event_notifier_notification
*notification
)
4651 struct cds_lfht_node
*node
;
4652 struct cds_lfht_iter iter
;
4653 struct notification_trigger_tokens_ht_element
*element
;
4654 struct lttng_evaluation
*evaluation
= NULL
;
4655 enum action_executor_status executor_status
;
4656 struct notification_client_list
*client_list
= NULL
;
4658 unsigned int capture_count
= 0;
4660 /* Find triggers associated with this token. */
4662 cds_lfht_lookup(state
->trigger_tokens_ht
,
4663 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4664 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4665 node
= cds_lfht_iter_get_node(&iter
);
4666 if (caa_unlikely(!node
)) {
4668 * This is not an error, slow consumption of the tracer
4669 * notifications can lead to situations where a trigger is
4670 * removed but we still get tracer notifications matching a
4671 * trigger that no longer exists.
4677 element
= caa_container_of(node
,
4678 struct notification_trigger_tokens_ht_element
,
4681 if (lttng_condition_event_rule_matches_get_capture_descriptor_count(
4682 lttng_trigger_get_const_condition(element
->trigger
),
4683 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4684 ERR("Failed to get capture count");
4689 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4690 ERR("Expected capture but capture buffer is null");
4695 evaluation
= lttng_evaluation_event_rule_matches_create(
4696 container_of(lttng_trigger_get_const_condition(
4698 struct lttng_condition_event_rule_matches
,
4700 notification
->capture_buffer
,
4701 notification
->capture_buf_size
, false);
4703 if (evaluation
== NULL
) {
4704 ERR("Failed to create event rule matches evaluation while creating and enqueuing action executor job");
4709 client_list
= get_client_list_from_condition(state
,
4710 lttng_trigger_get_const_condition(element
->trigger
));
4711 executor_status
= action_executor_enqueue_trigger(state
->executor
,
4712 element
->trigger
, evaluation
, NULL
, client_list
);
4713 switch (executor_status
) {
4714 case ACTION_EXECUTOR_STATUS_OK
:
4717 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4719 struct notification_client_list_element
*client_list_element
,
4723 * Not a fatal error; this is expected and simply means the
4724 * executor has too much work queued already.
4728 /* No clients subscribed to notifications for this trigger. */
4733 /* Warn clients that a notification (or more) was dropped. */
4734 pthread_mutex_lock(&client_list
->lock
);
4735 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4736 &client_list
->clients_list
, node
) {
4737 enum client_transmission_status transmission_status
;
4738 struct notification_client
*client
=
4739 client_list_element
->client
;
4741 pthread_mutex_lock(&client
->lock
);
4742 ret
= client_notification_overflow(client
);
4748 transmission_status
=
4749 client_flush_outgoing_queue(client
);
4750 ret
= client_handle_transmission_status(
4751 client
, transmission_status
, state
);
4757 pthread_mutex_unlock(&client
->lock
);
4763 pthread_mutex_unlock(&client_list
->lock
);
4766 case ACTION_EXECUTOR_STATUS_INVALID
:
4767 case ACTION_EXECUTOR_STATUS_ERROR
:
4768 /* Fatal error, shut down everything. */
4769 ERR("Fatal error encoutered while enqueuing action to the action executor");
4773 /* Unhandled error. */
4778 notification_client_list_put(client_list
);
4785 int handle_one_event_notifier_notification(
4786 struct notification_thread_state
*state
,
4787 int pipe
, enum lttng_domain_type domain
)
4790 struct lttng_event_notifier_notification
*notification
= NULL
;
4792 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4793 if (notification
== NULL
) {
4794 /* Reception failed, don't consider it fatal. */
4795 ERR("Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4796 pipe
, lttng_domain_type_str(domain
));
4800 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4802 ERR("Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4803 pipe
, lttng_domain_type_str(domain
));
4808 lttng_event_notifier_notification_destroy(notification
);
4812 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4813 int pipe
, enum lttng_domain_type domain
)
4815 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4818 int handle_notification_thread_channel_sample(
4819 struct notification_thread_state
*state
, int pipe
,
4820 enum lttng_domain_type domain
)
4823 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4824 struct channel_info
*channel_info
;
4825 struct cds_lfht_node
*node
;
4826 struct cds_lfht_iter iter
;
4827 struct lttng_channel_trigger_list
*trigger_list
;
4828 struct lttng_trigger_list_element
*trigger_list_element
;
4829 bool previous_sample_available
= false;
4830 struct channel_state_sample previous_sample
, latest_sample
;
4831 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4832 struct lttng_credentials channel_creds
;
4835 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4836 * ensuring that read/write of sampling messages are atomic.
4838 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4839 if (ret
!= sizeof(sample_msg
)) {
4840 ERR("Failed to read from monitoring pipe (fd = %i)",
4847 latest_sample
.key
.key
= sample_msg
.key
;
4848 latest_sample
.key
.domain
= domain
;
4849 latest_sample
.highest_usage
= sample_msg
.highest
;
4850 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4851 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4855 /* Retrieve the channel's informations */
4856 cds_lfht_lookup(state
->channels_ht
,
4857 hash_channel_key(&latest_sample
.key
),
4861 node
= cds_lfht_iter_get_node(&iter
);
4862 if (caa_unlikely(!node
)) {
4864 * Not an error since the consumer can push a sample to the pipe
4865 * and the rest of the session daemon could notify us of the
4866 * channel's destruction before we get a chance to process that
4869 DBG("Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4870 latest_sample
.key
.key
,
4871 lttng_domain_type_str(domain
));
4874 channel_info
= caa_container_of(node
, struct channel_info
,
4876 DBG("Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4878 latest_sample
.key
.key
,
4879 channel_info
->session_info
->name
,
4880 latest_sample
.highest_usage
,
4881 latest_sample
.lowest_usage
,
4882 latest_sample
.channel_total_consumed
);
4884 previous_session_consumed_total
=
4885 channel_info
->session_info
->consumed_data_size
;
4887 /* Retrieve the channel's last sample, if it exists, and update it. */
4888 cds_lfht_lookup(state
->channel_state_ht
,
4889 hash_channel_key(&latest_sample
.key
),
4890 match_channel_state_sample
,
4893 node
= cds_lfht_iter_get_node(&iter
);
4894 if (caa_likely(node
)) {
4895 struct channel_state_sample
*stored_sample
;
4897 /* Update the sample stored. */
4898 stored_sample
= caa_container_of(node
,
4899 struct channel_state_sample
,
4900 channel_state_ht_node
);
4902 memcpy(&previous_sample
, stored_sample
,
4903 sizeof(previous_sample
));
4904 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4905 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4906 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4907 previous_sample_available
= true;
4909 latest_session_consumed_total
=
4910 previous_session_consumed_total
+
4911 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4914 * This is the channel's first sample, allocate space for and
4915 * store the new sample.
4917 struct channel_state_sample
*stored_sample
;
4919 stored_sample
= zmalloc
<channel_state_sample
>();
4920 if (!stored_sample
) {
4925 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4926 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4927 cds_lfht_add(state
->channel_state_ht
,
4928 hash_channel_key(&stored_sample
->key
),
4929 &stored_sample
->channel_state_ht_node
);
4931 latest_session_consumed_total
=
4932 previous_session_consumed_total
+
4933 latest_sample
.channel_total_consumed
;
4936 channel_info
->session_info
->consumed_data_size
=
4937 latest_session_consumed_total
;
4939 /* Find triggers associated with this channel. */
4940 cds_lfht_lookup(state
->channel_triggers_ht
,
4941 hash_channel_key(&latest_sample
.key
),
4942 match_channel_trigger_list
,
4945 node
= cds_lfht_iter_get_node(&iter
);
4946 if (caa_likely(!node
)) {
4950 channel_creds
= (typeof(channel_creds
)) {
4951 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4952 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4955 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4956 channel_triggers_ht_node
);
4957 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4959 const struct lttng_condition
*condition
;
4960 struct lttng_trigger
*trigger
;
4961 struct notification_client_list
*client_list
= NULL
;
4962 struct lttng_evaluation
*evaluation
= NULL
;
4963 enum action_executor_status executor_status
;
4966 trigger
= trigger_list_element
->trigger
;
4967 condition
= lttng_trigger_get_const_condition(trigger
);
4968 LTTNG_ASSERT(condition
);
4971 * Check if any client is subscribed to the result of this
4974 client_list
= get_client_list_from_condition(state
, condition
);
4976 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4977 previous_sample_available
? &previous_sample
: NULL
,
4979 previous_session_consumed_total
,
4980 latest_session_consumed_total
,
4982 if (caa_unlikely(ret
)) {
4986 if (caa_likely(!evaluation
)) {
4991 * Ownership of `evaluation` transferred to the action executor
4992 * no matter the result.
4994 executor_status
= action_executor_enqueue_trigger(
4995 state
->executor
, trigger
, evaluation
,
4996 &channel_creds
, client_list
);
4998 switch (executor_status
) {
4999 case ACTION_EXECUTOR_STATUS_OK
:
5001 case ACTION_EXECUTOR_STATUS_ERROR
:
5002 case ACTION_EXECUTOR_STATUS_INVALID
:
5004 * TODO Add trigger identification (name/id) when
5005 * it is added to the API.
5007 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
5010 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
5012 * TODO Add trigger identification (name/id) when
5013 * it is added to the API.
5015 * Not a fatal error.
5017 WARN("No space left when enqueuing action associated with buffer-condition trigger");
5025 notification_client_list_put(client_list
);
5026 if (caa_unlikely(ret
)) {