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"
11 #include "condition-internal.hpp"
12 #include "event-notifier-error-accounting.hpp"
14 #include "lttng-sessiond.hpp"
15 #include "notification-thread-commands.hpp"
16 #include "notification-thread-events.hpp"
17 #include "notification-thread.hpp"
19 #include <common/defaults.hpp>
20 #include <common/dynamic-buffer.hpp>
21 #include <common/error.hpp>
22 #include <common/futex.hpp>
23 #include <common/hashtable/utils.hpp>
24 #include <common/macros.hpp>
25 #include <common/sessiond-comm/sessiond-comm.hpp>
26 #include <common/unix.hpp>
27 #include <common/urcu.hpp>
29 #include <lttng/action/action-internal.hpp>
30 #include <lttng/action/list-internal.hpp>
31 #include <lttng/condition/buffer-usage-internal.hpp>
32 #include <lttng/condition/condition-internal.hpp>
33 #include <lttng/condition/condition.h>
34 #include <lttng/condition/event-rule-matches-internal.hpp>
35 #include <lttng/condition/session-consumed-size-internal.hpp>
36 #include <lttng/condition/session-rotation-internal.hpp>
37 #include <lttng/domain-internal.hpp>
38 #include <lttng/event-rule/event-rule-internal.hpp>
39 #include <lttng/location-internal.hpp>
40 #include <lttng/notification/channel-internal.hpp>
41 #include <lttng/notification/notification-internal.hpp>
42 #include <lttng/trigger/trigger-internal.hpp>
49 #include <urcu/rculfhash.h>
51 #define CLIENT_POLL_EVENTS_IN (LPOLLIN | LPOLLRDHUP)
52 #define CLIENT_POLL_EVENTS_IN_OUT (CLIENT_POLL_EVENTS_IN | LPOLLOUT)
54 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
55 #define MAX_CAPTURE_SIZE (PIPE_BUF)
57 enum lttng_object_type
{
58 LTTNG_OBJECT_TYPE_UNKNOWN
,
59 LTTNG_OBJECT_TYPE_NONE
,
60 LTTNG_OBJECT_TYPE_CHANNEL
,
61 LTTNG_OBJECT_TYPE_SESSION
,
64 struct lttng_channel_trigger_list
{
65 struct channel_key channel_key
;
66 /* List of struct lttng_trigger_list_element. */
67 struct cds_list_head list
;
68 /* Node in the channel_triggers_ht */
69 struct cds_lfht_node channel_triggers_ht_node
;
70 /* call_rcu delayed reclaim. */
71 struct rcu_head rcu_node
;
75 * List of triggers applying to a given session.
78 * - lttng_session_trigger_list_create()
79 * - lttng_session_trigger_list_build()
80 * - lttng_session_trigger_list_destroy()
81 * - lttng_session_trigger_list_add()
83 struct lttng_session_trigger_list
{
85 /* List of struct lttng_trigger_list_element. */
86 struct cds_list_head list
;
87 /* Node in the session_triggers_ht */
88 struct cds_lfht_node session_triggers_ht_node
;
90 * Weak reference to the notification system's session triggers
93 * The session trigger list structure structure is owned by
94 * the session's session_info.
96 * The session_info is kept alive the the channel_infos holding a
97 * reference to it (reference counting). When those channels are
98 * destroyed (at runtime or on teardown), the reference they hold
99 * to the session_info are released. On destruction of session_info,
100 * session_info_destroy() will remove the list of triggers applying
101 * to this session from the notification system's state.
103 * This implies that the session_triggers_ht must be destroyed
104 * after the channels.
106 struct cds_lfht
*session_triggers_ht
;
107 /* Used for delayed RCU reclaim. */
108 struct rcu_head rcu_node
;
112 struct lttng_trigger_list_element
{
113 /* No ownership of the trigger object is assumed. */
114 struct lttng_trigger
*trigger
;
115 struct cds_list_head node
;
118 struct lttng_trigger_ht_element
{
119 struct lttng_trigger
*trigger
;
120 struct cds_lfht_node node
;
121 struct cds_lfht_node node_by_name_uid
;
122 struct cds_list_head client_list_trigger_node
;
123 /* call_rcu delayed reclaim. */
124 struct rcu_head rcu_node
;
127 struct lttng_condition_list_element
{
128 struct lttng_condition
*condition
;
129 struct cds_list_head node
;
132 struct channel_state_sample
{
133 struct channel_key key
;
134 struct cds_lfht_node channel_state_ht_node
;
135 uint64_t highest_usage
;
136 uint64_t lowest_usage
;
137 /* call_rcu delayed reclaim. */
138 struct rcu_head rcu_node
;
142 static unsigned long hash_channel_key(struct channel_key
*key
);
143 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
144 struct lttng_evaluation
**evaluation
,
145 const struct notification_thread_state
*state
,
146 const struct channel_state_sample
*previous_sample
,
147 const struct channel_state_sample
*latest_sample
,
148 struct channel_info
*channel_info
);
149 static int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
150 const struct lttng_evaluation
*evaluation
,
151 struct notification_client_list
*client_list
,
152 struct notification_thread_state
*state
,
156 /* session_info API */
157 static void session_info_destroy(void *_data
);
158 static void session_info_get(struct session_info
*session_info
);
159 static void session_info_put(struct session_info
*session_info
);
160 static struct session_info
*session_info_create(uint64_t id
,
164 struct lttng_session_trigger_list
*trigger_list
,
165 struct cds_lfht
*sessions_ht
);
166 static void session_info_add_channel(struct session_info
*session_info
,
167 struct channel_info
*channel_info
);
168 static void session_info_remove_channel(struct session_info
*session_info
,
169 struct channel_info
*channel_info
);
171 /* lttng_session_trigger_list API */
172 static struct lttng_session_trigger_list
*
173 lttng_session_trigger_list_create(const char *session_name
, struct cds_lfht
*session_triggers_ht
);
174 static struct lttng_session_trigger_list
*
175 lttng_session_trigger_list_build(const struct notification_thread_state
*state
,
176 const char *session_name
);
177 static void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
);
178 static int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
179 struct lttng_trigger
*trigger
);
181 static int client_handle_transmission_status(struct notification_client
*client
,
182 enum client_transmission_status transmission_status
,
183 struct notification_thread_state
*state
);
185 static int handle_one_event_notifier_notification(struct notification_thread_state
*state
,
187 enum lttng_domain_type domain
);
189 static void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
191 static int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
193 /* This double-cast is intended to supress pointer-to-cast warning. */
194 const int socket
= (int) (intptr_t) key
;
195 const struct notification_client
*client
=
196 caa_container_of(node
, struct notification_client
, client_socket_ht_node
);
198 return client
->socket
== socket
;
201 static int match_client_id(struct cds_lfht_node
*node
, const void *key
)
203 /* This double-cast is intended to supress pointer-to-cast warning. */
204 const notification_client_id id
= *((notification_client_id
*) key
);
205 const struct notification_client
*client
=
206 lttng::utils::container_of(node
, ¬ification_client::client_id_ht_node
);
208 return client
->id
== id
;
211 static int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
213 struct channel_key
*channel_key
= (struct channel_key
*) key
;
214 struct lttng_channel_trigger_list
*trigger_list
;
217 caa_container_of(node
, struct lttng_channel_trigger_list
, channel_triggers_ht_node
);
219 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
220 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
223 static int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
225 const char *session_name
= (const char *) key
;
226 struct lttng_session_trigger_list
*trigger_list
;
229 caa_container_of(node
, struct lttng_session_trigger_list
, session_triggers_ht_node
);
231 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
234 static int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
236 struct channel_key
*channel_key
= (struct channel_key
*) key
;
237 struct channel_state_sample
*sample
;
239 sample
= caa_container_of(node
, struct channel_state_sample
, channel_state_ht_node
);
241 return !!((channel_key
->key
== sample
->key
.key
) &&
242 (channel_key
->domain
== sample
->key
.domain
));
245 static int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
247 struct channel_key
*channel_key
= (struct channel_key
*) key
;
248 struct channel_info
*channel_info
;
250 channel_info
= caa_container_of(node
, struct channel_info
, channels_ht_node
);
252 return !!((channel_key
->key
== channel_info
->key
.key
) &&
253 (channel_key
->domain
== channel_info
->key
.domain
));
256 static int match_trigger(struct cds_lfht_node
*node
, const void *key
)
258 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
259 struct lttng_trigger_ht_element
*trigger_ht_element
;
261 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
, node
);
263 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
266 static int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
268 const uint64_t *_key
= (uint64_t *) key
;
269 struct notification_trigger_tokens_ht_element
*element
;
271 element
= caa_container_of(node
, struct notification_trigger_tokens_ht_element
, node
);
272 return *_key
== element
->token
;
275 static int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
277 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
278 struct notification_client_list
*client_list
;
279 const struct lttng_condition
*condition
;
281 LTTNG_ASSERT(condition_key
);
283 client_list
= caa_container_of(
284 node
, struct notification_client_list
, notification_trigger_clients_ht_node
);
285 condition
= client_list
->condition
;
287 return !!lttng_condition_is_equal(condition_key
, condition
);
290 static int match_session_info(struct cds_lfht_node
*node
, const void *key
)
292 const auto session_id
= *((uint64_t *) key
);
293 const auto *session_info
=
294 lttng::utils::container_of(node
, &session_info::sessions_ht_node
);
296 return session_id
== session_info
->id
;
299 static unsigned long hash_session_info_id(uint64_t id
)
301 return hash_key_u64(&id
, lttng_ht_seed
);
304 static unsigned long hash_session_info(const struct session_info
*session_info
)
306 return hash_session_info_id(session_info
->id
);
309 static struct session_info
*get_session_info_by_id(const struct notification_thread_state
*state
,
312 struct cds_lfht_iter iter
;
313 struct cds_lfht_node
*node
;
314 lttng::urcu::read_lock_guard read_lock_guard
;
317 state
->sessions_ht
, hash_session_info_id(id
), match_session_info
, &id
, &iter
);
318 node
= cds_lfht_iter_get_node(&iter
);
322 lttng::utils::container_of(node
, &session_info::sessions_ht_node
);
324 session_info_get(session_info
);
331 static struct session_info
*get_session_info_by_name(const struct notification_thread_state
*state
,
335 const auto found
= sample_session_id_by_name(name
, &session_id
);
337 return found
? get_session_info_by_id(state
, session_id
) : nullptr;
340 static const char *notification_command_type_str(enum notification_thread_command_type type
)
343 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
344 return "REGISTER_TRIGGER";
345 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
346 return "UNREGISTER_TRIGGER";
347 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
348 return "ADD_CHANNEL";
349 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
350 return "REMOVE_CHANNEL";
351 case NOTIFICATION_COMMAND_TYPE_ADD_SESSION
:
352 return "ADD_SESSION";
353 case NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION
:
354 return "REMOVE_SESSION";
355 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
356 return "SESSION_ROTATION_ONGOING";
357 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
358 return "SESSION_ROTATION_COMPLETED";
359 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
360 return "ADD_TRACER_EVENT_SOURCE";
361 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
362 return "REMOVE_TRACER_EVENT_SOURCE";
363 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
364 return "LIST_TRIGGERS";
365 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
366 return "GET_TRIGGER";
367 case NOTIFICATION_COMMAND_TYPE_QUIT
:
369 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
370 return "CLIENT_COMMUNICATION_UPDATE";
377 * Match trigger based on name and credentials only.
378 * Name duplication is NOT allowed for the same uid.
380 static int match_trigger_by_name_uid(struct cds_lfht_node
*node
, const void *key
)
383 const char *element_trigger_name
;
384 const char *key_name
;
385 enum lttng_trigger_status status
;
386 const struct lttng_credentials
*key_creds
;
387 const struct lttng_credentials
*node_creds
;
388 const struct lttng_trigger
*trigger_key
= (const struct lttng_trigger
*) key
;
389 const struct lttng_trigger_ht_element
*trigger_ht_element
=
390 caa_container_of(node
, struct lttng_trigger_ht_element
, node_by_name_uid
);
392 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &element_trigger_name
);
393 element_trigger_name
= status
== LTTNG_TRIGGER_STATUS_OK
? element_trigger_name
: nullptr;
395 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
396 key_name
= status
== LTTNG_TRIGGER_STATUS_OK
? key_name
: nullptr;
400 * Consider null names as not equal. This is to maintain backwards
401 * compatibility with pre-2.13 anonymous triggers. Multiples anonymous
402 * triggers are allowed for a given user.
404 if (!element_trigger_name
|| !key_name
) {
408 if (strcmp(element_trigger_name
, key_name
) != 0) {
412 /* Compare the owners' UIDs. */
413 key_creds
= lttng_trigger_get_credentials(trigger_key
);
414 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
416 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
423 * Hash trigger based on name and credentials only.
425 static unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
427 unsigned long hash
= 0;
428 const struct lttng_credentials
*trigger_creds
;
429 const char *trigger_name
;
430 enum lttng_trigger_status status
;
432 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
433 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
434 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
437 trigger_creds
= lttng_trigger_get_credentials(trigger
);
438 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
444 static unsigned long hash_channel_key(struct channel_key
*key
)
446 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
447 unsigned long domain_hash
=
448 hash_key_ulong((void *) (unsigned long) key
->domain
, lttng_ht_seed
);
450 return key_hash
^ domain_hash
;
453 static unsigned long hash_client_socket(int socket
)
455 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
458 static unsigned long hash_client_id(notification_client_id id
)
460 return hash_key_u64(&id
, lttng_ht_seed
);
464 * Get the type of object to which a given condition applies. Bindings let
465 * the notification system evaluate a trigger's condition when a given
466 * object's state is updated.
468 * For instance, a condition bound to a channel will be evaluated everytime
469 * the channel's state is changed by a channel monitoring sample.
471 static enum lttng_object_type
get_condition_binding_object(const struct lttng_condition
*condition
)
473 switch (lttng_condition_get_type(condition
)) {
474 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
475 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
476 return LTTNG_OBJECT_TYPE_CHANNEL
;
477 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
478 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
479 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
480 return LTTNG_OBJECT_TYPE_SESSION
;
481 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
482 return LTTNG_OBJECT_TYPE_NONE
;
484 return LTTNG_OBJECT_TYPE_UNKNOWN
;
488 static void free_channel_info_rcu(struct rcu_head
*node
)
490 free(lttng::utils::container_of(node
, &channel_info::rcu_node
));
493 static void channel_info_destroy(struct channel_info
*channel_info
)
499 if (channel_info
->session_info
) {
500 session_info_remove_channel(channel_info
->session_info
, channel_info
);
501 session_info_put(channel_info
->session_info
);
503 if (channel_info
->name
) {
504 free(channel_info
->name
);
506 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
509 static void free_session_info_rcu(struct rcu_head
*node
)
511 free(lttng::utils::container_of(node
, &session_info::rcu_node
));
514 /* Don't call directly, use the ref-counting mechanism. */
515 static void session_info_destroy(void *_data
)
517 struct session_info
*session_info
= (struct session_info
*) _data
;
520 LTTNG_ASSERT(session_info
);
521 if (session_info
->channel_infos_ht
) {
522 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, nullptr);
524 ERR("Failed to destroy channel information hash table");
527 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
529 lttng::urcu::read_lock_guard read_lock
;
530 cds_lfht_del(session_info
->sessions_ht
, &session_info
->sessions_ht_node
);
531 free(session_info
->name
);
532 lttng_trace_archive_location_put(session_info
->last_state_sample
.rotation
.location
);
533 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
536 static void session_info_get(struct session_info
*session_info
)
541 lttng_ref_get(&session_info
->ref
);
544 static void session_info_put(struct session_info
*session_info
)
549 lttng_ref_put(&session_info
->ref
);
552 static struct session_info
*session_info_create(uint64_t id
,
556 struct lttng_session_trigger_list
*trigger_list
,
557 struct cds_lfht
*sessions_ht
)
559 struct session_info
*session_info
;
563 session_info
= zmalloc
<struct session_info
>();
568 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
570 session_info
->channel_infos_ht
= cds_lfht_new(
571 DEFAULT_HT_SIZE
, 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, nullptr);
572 if (!session_info
->channel_infos_ht
) {
576 cds_lfht_node_init(&session_info
->sessions_ht_node
);
577 session_info
->id
= id
;
578 session_info
->name
= strdup(name
);
579 if (!session_info
->name
) {
583 session_info
->uid
= uid
;
584 session_info
->gid
= gid
;
585 session_info
->trigger_list
= trigger_list
;
586 session_info
->sessions_ht
= sessions_ht
;
590 session_info_put(session_info
);
594 static void session_info_add_channel(struct session_info
*session_info
,
595 struct channel_info
*channel_info
)
597 lttng::urcu::read_lock_guard read_lock
;
598 cds_lfht_add(session_info
->channel_infos_ht
,
599 hash_channel_key(&channel_info
->key
),
600 &channel_info
->session_info_channels_ht_node
);
603 static void session_info_remove_channel(struct session_info
*session_info
,
604 struct channel_info
*channel_info
)
606 lttng::urcu::read_lock_guard read_lock
;
607 cds_lfht_del(session_info
->channel_infos_ht
, &channel_info
->session_info_channels_ht_node
);
610 static struct channel_info
*channel_info_create(const char *channel_name
,
611 struct channel_key
*channel_key
,
612 uint64_t channel_capacity
,
613 struct session_info
*session_info
)
615 struct channel_info
*channel_info
= zmalloc
<struct channel_info
>();
621 cds_lfht_node_init(&channel_info
->channels_ht_node
);
622 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
623 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
624 channel_info
->capacity
= channel_capacity
;
626 channel_info
->name
= strdup(channel_name
);
627 if (!channel_info
->name
) {
632 * Set the references between session and channel infos:
633 * - channel_info holds a strong reference to session_info
634 * - session_info holds a weak reference to channel_info
636 session_info_get(session_info
);
637 session_info_add_channel(session_info
, channel_info
);
638 channel_info
->session_info
= session_info
;
642 channel_info_destroy(channel_info
);
646 bool notification_client_list_get(struct notification_client_list
*list
)
648 return urcu_ref_get_unless_zero(&list
->ref
);
651 static void free_notification_client_list_rcu(struct rcu_head
*node
)
653 free(caa_container_of(node
, struct notification_client_list
, rcu_node
));
656 static void notification_client_list_release(struct urcu_ref
*list_ref
)
658 struct notification_client_list
*list
=
659 lttng::utils::container_of(list_ref
, ¬ification_client_list::ref
);
660 struct notification_client_list_element
*client_list_element
, *tmp
;
662 lttng_condition_put(list
->condition
);
664 if (list
->notification_trigger_clients_ht
) {
665 lttng::urcu::read_lock_guard read_lock
;
667 cds_lfht_del(list
->notification_trigger_clients_ht
,
668 &list
->notification_trigger_clients_ht_node
);
669 list
->notification_trigger_clients_ht
= nullptr;
671 cds_list_for_each_entry_safe (client_list_element
, tmp
, &list
->clients_list
, node
) {
672 free(client_list_element
);
675 LTTNG_ASSERT(cds_list_empty(&list
->triggers_list
));
677 pthread_mutex_destroy(&list
->lock
);
678 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
681 static bool condition_applies_to_client(const struct lttng_condition
*condition
,
682 struct notification_client
*client
)
684 bool applies
= false;
685 struct lttng_condition_list_element
*condition_list_element
;
687 cds_list_for_each_entry (condition_list_element
, &client
->condition_list
, node
) {
688 applies
= lttng_condition_is_equal(condition_list_element
->condition
, condition
);
697 static struct notification_client_list
*
698 notification_client_list_create(struct notification_thread_state
*state
,
699 const struct lttng_condition
*condition
)
701 struct notification_client
*client
;
702 struct cds_lfht_iter iter
;
703 struct notification_client_list
*client_list
;
705 client_list
= zmalloc
<notification_client_list
>();
707 PERROR("Failed to allocate notification client list");
711 pthread_mutex_init(&client_list
->lock
, nullptr);
713 * The trigger that owns the condition has the first reference to this
716 urcu_ref_init(&client_list
->ref
);
717 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
718 CDS_INIT_LIST_HEAD(&client_list
->clients_list
);
719 CDS_INIT_LIST_HEAD(&client_list
->triggers_list
);
722 * Create a copy of the condition so that it's independent of any
723 * trigger. The client list may outlive the trigger object (which owns
724 * the condition) that is used to create it.
726 client_list
->condition
= lttng_condition_copy(condition
);
729 /* Build a list of clients to which this new condition applies. */
730 lttng::urcu::read_lock_guard read_lock
;
732 cds_lfht_for_each_entry (
733 state
->client_socket_ht
, &iter
, client
, client_socket_ht_node
) {
734 struct notification_client_list_element
*client_list_element
;
736 if (!condition_applies_to_client(condition
, client
)) {
740 client_list_element
= zmalloc
<notification_client_list_element
>();
741 if (!client_list_element
) {
742 goto error_put_client_list
;
745 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
746 client_list_element
->client
= client
;
747 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
751 client_list
->notification_trigger_clients_ht
= state
->notification_trigger_clients_ht
;
754 * Add the client list to the global list of client list.
757 lttng::urcu::read_lock_guard read_lock
;
759 cds_lfht_add_unique(state
->notification_trigger_clients_ht
,
760 lttng_condition_hash(client_list
->condition
),
761 match_client_list_condition
,
762 client_list
->condition
,
763 &client_list
->notification_trigger_clients_ht_node
);
767 error_put_client_list
:
768 notification_client_list_put(client_list
);
769 client_list
= nullptr;
775 void notification_client_list_put(struct notification_client_list
*list
)
780 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
783 /* Provides a reference to the returned list. */
784 static struct notification_client_list
*
785 get_client_list_from_condition(struct notification_thread_state
*state
,
786 const struct lttng_condition
*condition
)
788 struct cds_lfht_node
*node
;
789 struct cds_lfht_iter iter
;
790 struct notification_client_list
*list
= nullptr;
792 lttng::urcu::read_lock_guard read_lock
;
793 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
794 lttng_condition_hash(condition
),
795 match_client_list_condition
,
798 node
= cds_lfht_iter_get_node(&iter
);
800 list
= lttng::utils::container_of(
801 node
, ¬ification_client_list::notification_trigger_clients_ht_node
);
802 list
= notification_client_list_get(list
) ? list
: nullptr;
808 static int evaluate_channel_condition_for_client(const struct lttng_condition
*condition
,
809 struct notification_thread_state
*state
,
810 struct lttng_evaluation
**evaluation
,
815 struct cds_lfht_iter iter
;
816 struct cds_lfht_node
*node
;
817 struct channel_info
*channel_info
= nullptr;
818 struct channel_key
*channel_key
= nullptr;
819 struct channel_state_sample
*last_sample
= nullptr;
820 struct lttng_channel_trigger_list
*channel_trigger_list
= nullptr;
822 lttng::urcu::read_lock_guard read_lock
;
824 /* Find the channel associated with the condition. */
825 cds_lfht_for_each_entry (
826 state
->channel_triggers_ht
, &iter
, channel_trigger_list
, channel_triggers_ht_node
) {
827 struct lttng_trigger_list_element
*element
;
829 cds_list_for_each_entry (element
, &channel_trigger_list
->list
, node
) {
830 const struct lttng_condition
*current_condition
=
831 lttng_trigger_get_const_condition(element
->trigger
);
833 LTTNG_ASSERT(current_condition
);
834 if (!lttng_condition_is_equal(condition
, current_condition
)) {
838 /* Found the trigger, save the channel key. */
839 channel_key
= &channel_trigger_list
->channel_key
;
843 /* The channel key was found stop iteration. */
849 /* No channel found; normal exit. */
850 DBG("No known channel associated with newly subscribed-to condition");
855 /* Fetch channel info for the matching channel. */
856 cds_lfht_lookup(state
->channels_ht
,
857 hash_channel_key(channel_key
),
861 node
= cds_lfht_iter_get_node(&iter
);
863 channel_info
= caa_container_of(node
, struct channel_info
, channels_ht_node
);
865 /* Retrieve the channel's last sample, if it exists. */
866 cds_lfht_lookup(state
->channel_state_ht
,
867 hash_channel_key(channel_key
),
868 match_channel_state_sample
,
871 node
= cds_lfht_iter_get_node(&iter
);
874 caa_container_of(node
, struct channel_state_sample
, channel_state_ht_node
);
876 /* Nothing to evaluate, no sample was ever taken. Normal exit */
877 DBG("No channel sample associated with newly subscribed-to condition");
882 ret
= evaluate_buffer_condition(
883 condition
, evaluation
, state
, nullptr, last_sample
, channel_info
);
885 WARN("Fatal error occurred while evaluating a newly subscribed-to condition");
889 *session_uid
= channel_info
->session_info
->uid
;
890 *session_gid
= channel_info
->session_info
->gid
;
895 static const char *get_condition_session_name(const struct lttng_condition
*condition
)
897 const char *session_name
= nullptr;
898 enum lttng_condition_status status
;
900 switch (lttng_condition_get_type(condition
)) {
901 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
902 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
903 status
= lttng_condition_buffer_usage_get_session_name(condition
, &session_name
);
905 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
906 status
= lttng_condition_session_consumed_size_get_session_name(condition
,
909 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
910 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
912 lttng_condition_session_rotation_get_session_name(condition
, &session_name
);
917 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
918 ERR("Failed to retrieve session rotation condition's session name");
925 static bool evaluate_session_rotation_ongoing_condition(const struct lttng_condition
*condition
926 __attribute__((unused
)),
927 const struct session_state_sample
*sample
)
929 return sample
->rotation
.ongoing
;
932 static bool evaluate_session_consumed_size_condition(const struct lttng_condition
*condition
,
933 const struct session_state_sample
*sample
)
936 const struct lttng_condition_session_consumed_size
*size_condition
=
937 lttng::utils::container_of(condition
,
938 <tng_condition_session_consumed_size::parent
);
940 threshold
= size_condition
->consumed_threshold_bytes
.value
;
941 DBG("Session consumed size condition being evaluated: threshold = %" PRIu64
942 ", current size = %" PRIu64
,
944 sample
->consumed_data_size
);
945 return sample
->consumed_data_size
>= threshold
;
949 * `new_state` can be NULL to indicate that we are not evaluating a
950 * state transition. A client subscribed or a trigger was registered and
951 * we wish to perform an initial evaluation.
953 static int evaluate_session_condition(const struct lttng_condition
*condition
,
954 const struct session_info
*session_info
,
955 const struct session_state_sample
*new_state
,
956 struct lttng_evaluation
**evaluation
)
959 bool previous_result
, newest_result
;
961 switch (lttng_condition_get_type(condition
)) {
962 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
964 previous_result
= evaluate_session_rotation_ongoing_condition(
965 condition
, &session_info
->last_state_sample
);
967 evaluate_session_rotation_ongoing_condition(condition
, new_state
);
969 previous_result
= false;
970 newest_result
= evaluate_session_rotation_ongoing_condition(
971 condition
, &session_info
->last_state_sample
);
974 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
976 previous_result
= evaluate_session_consumed_size_condition(
977 condition
, &session_info
->last_state_sample
);
979 evaluate_session_consumed_size_condition(condition
, new_state
);
981 previous_result
= false;
982 newest_result
= evaluate_session_consumed_size_condition(
983 condition
, &session_info
->last_state_sample
);
986 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
988 * Note that LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED is
989 * evaluated differently to only consider state transitions without regard for the
990 * initial state. This is a deliberate choice as it is unlikely that a user would
991 * expect an action to occur for a rotation that occurred long before the trigger or
992 * subscription occurred.
999 previous_result
= !session_info
->last_state_sample
.rotation
.ongoing
;
1000 newest_result
= !new_state
->rotation
.ongoing
;
1007 if (!newest_result
|| (previous_result
== newest_result
)) {
1008 /* Not a state transition, evaluate to false. */
1013 switch (lttng_condition_get_type(condition
)) {
1014 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1016 const auto rotation_id
= new_state
? new_state
->rotation
.id
:
1017 session_info
->last_state_sample
.rotation
.id
;
1019 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(rotation_id
);
1022 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1024 const auto& sample
= new_state
? *new_state
: session_info
->last_state_sample
;
1025 const auto rotation_id
= sample
.rotation
.id
;
1027 /* Callee acquires a reference to location. */
1028 *evaluation
= lttng_evaluation_session_rotation_completed_create(
1029 rotation_id
, sample
.rotation
.location
);
1032 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1034 const auto latest_session_consumed_total
= new_state
?
1035 new_state
->consumed_data_size
:
1036 session_info
->last_state_sample
.consumed_data_size
;
1038 *evaluation
= lttng_evaluation_session_consumed_size_create(
1039 latest_session_consumed_total
);
1048 ERR("Failed to create session condition evaluation: session name = `%s`",
1049 session_info
->name
);
1059 static int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1060 const struct lttng_condition
*condition
,
1061 struct notification_client
*client
,
1062 struct notification_thread_state
*state
)
1065 struct lttng_evaluation
*evaluation
= nullptr;
1066 struct notification_client_list client_list
= {
1067 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1069 .condition
= nullptr,
1070 .triggers_list
= {},
1072 .notification_trigger_clients_ht
= nullptr,
1073 .notification_trigger_clients_ht_node
= {},
1076 struct notification_client_list_element client_list_element
= {};
1077 uid_t object_uid
= 0;
1078 gid_t object_gid
= 0;
1080 LTTNG_ASSERT(trigger
);
1081 LTTNG_ASSERT(condition
);
1082 LTTNG_ASSERT(client
);
1083 LTTNG_ASSERT(state
);
1085 switch (get_condition_binding_object(condition
)) {
1086 case LTTNG_OBJECT_TYPE_SESSION
:
1088 /* Find the session associated with the condition. */
1089 const auto *session_name
= get_condition_session_name(condition
);
1090 auto session_info
= get_session_info_by_name(state
, session_name
);
1091 if (!session_info
) {
1092 /* Not an error, the session doesn't exist yet. */
1093 DBG("Session not found while evaluating session condition for client: session name = `%s`",
1099 object_uid
= session_info
->uid
;
1100 object_gid
= session_info
->gid
;
1102 ret
= evaluate_session_condition(condition
, session_info
, nullptr, &evaluation
);
1103 session_info_put(session_info
);
1106 case LTTNG_OBJECT_TYPE_CHANNEL
:
1107 ret
= evaluate_channel_condition_for_client(
1108 condition
, state
, &evaluation
, &object_uid
, &object_gid
);
1110 case LTTNG_OBJECT_TYPE_NONE
:
1111 DBG("Newly subscribed-to condition not bound to object, nothing to evaluate");
1114 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1124 /* Evaluation yielded nothing. Normal exit. */
1125 DBG("Newly subscribed-to condition evaluated to false, nothing to report to client");
1131 * Create a temporary client list with the client currently
1134 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1135 CDS_INIT_LIST_HEAD(&client_list
.clients_list
);
1137 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1138 client_list_element
.client
= client
;
1139 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1141 /* Send evaluation result to the newly-subscribed client. */
1142 DBG("Newly subscribed-to condition evaluated to true, notifying client");
1143 ret
= send_evaluation_to_clients(
1144 trigger
, evaluation
, &client_list
, state
, object_uid
, object_gid
);
1150 static int notification_thread_client_subscribe(struct notification_client
*client
,
1151 struct lttng_condition
*condition
,
1152 struct notification_thread_state
*state
,
1153 enum lttng_notification_channel_status
*_status
)
1156 struct notification_client_list
*client_list
= nullptr;
1157 struct lttng_condition_list_element
*condition_list_element
= nullptr;
1158 struct notification_client_list_element
*client_list_element
= nullptr;
1159 struct lttng_trigger_ht_element
*trigger_ht_element
;
1160 enum lttng_notification_channel_status status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1163 * Ensure that the client has not already subscribed to this condition
1166 cds_list_for_each_entry (condition_list_element
, &client
->condition_list
, node
) {
1167 if (lttng_condition_is_equal(condition_list_element
->condition
, condition
)) {
1168 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1173 condition_list_element
= zmalloc
<lttng_condition_list_element
>();
1174 if (!condition_list_element
) {
1178 client_list_element
= zmalloc
<notification_client_list_element
>();
1179 if (!client_list_element
) {
1185 * Add the newly-subscribed condition to the client's subscription list.
1187 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1188 condition_list_element
->condition
= condition
;
1189 condition
= nullptr;
1190 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1192 client_list
= get_client_list_from_condition(state
, condition_list_element
->condition
);
1195 * No notification-emiting trigger registered with this
1196 * condition. We don't evaluate the condition right away
1197 * since this trigger is not registered yet.
1199 free(client_list_element
);
1204 * The condition to which the client just subscribed is evaluated
1205 * at this point so that conditions that are already TRUE result
1206 * in a notification being sent out.
1208 * Note the iteration on all triggers which share an identical
1209 * `condition` than the one to which the client is registering. This is
1210 * done to ensure that the client receives a distinct notification for
1211 * all triggers that have a `notify` action that have this condition.
1213 pthread_mutex_lock(&client_list
->lock
);
1214 cds_list_for_each_entry (
1215 trigger_ht_element
, &client_list
->triggers_list
, client_list_trigger_node
) {
1216 if (evaluate_condition_for_client(trigger_ht_element
->trigger
,
1217 condition_list_element
->condition
,
1220 WARN("Evaluation of a condition on client subscription failed, aborting.");
1222 free(client_list_element
);
1223 pthread_mutex_unlock(&client_list
->lock
);
1227 pthread_mutex_unlock(&client_list
->lock
);
1230 * Add the client to the list of clients interested in a given trigger
1231 * if a "notification" trigger with a corresponding condition was
1234 client_list_element
->client
= client
;
1235 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1237 pthread_mutex_lock(&client_list
->lock
);
1238 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1239 pthread_mutex_unlock(&client_list
->lock
);
1245 notification_client_list_put(client_list
);
1247 lttng_condition_destroy(condition
);
1250 free(condition_list_element
);
1251 free(client_list_element
);
1252 lttng_condition_destroy(condition
);
1256 static int notification_thread_client_unsubscribe(struct notification_client
*client
,
1257 struct lttng_condition
*condition
,
1258 struct notification_thread_state
*state
,
1259 enum lttng_notification_channel_status
*_status
)
1261 struct notification_client_list
*client_list
;
1262 struct lttng_condition_list_element
*condition_list_element
, *condition_tmp
;
1263 struct notification_client_list_element
*client_list_element
, *client_tmp
;
1264 bool condition_found
= false;
1265 enum lttng_notification_channel_status status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1267 /* Remove the condition from the client's condition list. */
1268 cds_list_for_each_entry_safe (
1269 condition_list_element
, condition_tmp
, &client
->condition_list
, node
) {
1270 if (!lttng_condition_is_equal(condition_list_element
->condition
, condition
)) {
1274 cds_list_del(&condition_list_element
->node
);
1276 * The caller may be iterating on the client's conditions to
1277 * tear down a client's connection. In this case, the condition
1278 * will be destroyed at the end.
1280 if (condition
!= condition_list_element
->condition
) {
1281 lttng_condition_destroy(condition_list_element
->condition
);
1283 free(condition_list_element
);
1284 condition_found
= true;
1288 if (!condition_found
) {
1289 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1294 * Remove the client from the list of clients interested the trigger
1295 * matching the condition.
1297 client_list
= get_client_list_from_condition(state
, condition
);
1302 pthread_mutex_lock(&client_list
->lock
);
1303 cds_list_for_each_entry_safe (
1304 client_list_element
, client_tmp
, &client_list
->clients_list
, node
) {
1305 if (client_list_element
->client
->id
!= client
->id
) {
1308 cds_list_del(&client_list_element
->node
);
1309 free(client_list_element
);
1312 pthread_mutex_unlock(&client_list
->lock
);
1313 notification_client_list_put(client_list
);
1314 client_list
= nullptr;
1316 lttng_condition_destroy(condition
);
1323 static void free_notification_client_rcu(struct rcu_head
*node
)
1325 free(lttng::utils::container_of(node
, ¬ification_client::rcu_node
));
1328 static void notification_client_destroy(struct notification_client
*client
)
1335 * The client object is not reachable by other threads, no need to lock
1338 if (client
->socket
>= 0) {
1339 (void) lttcomm_close_unix_sock(client
->socket
);
1340 client
->socket
= -1;
1342 client
->communication
.active
= false;
1343 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1344 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1345 pthread_mutex_destroy(&client
->lock
);
1346 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1350 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1353 static struct notification_client
*get_client_from_socket(int socket
,
1354 struct notification_thread_state
*state
)
1356 struct cds_lfht_iter iter
;
1357 struct cds_lfht_node
*node
;
1358 struct notification_client
*client
= nullptr;
1360 ASSERT_RCU_READ_LOCKED();
1362 cds_lfht_lookup(state
->client_socket_ht
,
1363 hash_client_socket(socket
),
1364 match_client_socket
,
1365 (void *) (unsigned long) socket
,
1367 node
= cds_lfht_iter_get_node(&iter
);
1372 client
= caa_container_of(node
, struct notification_client
, client_socket_ht_node
);
1378 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1381 static struct notification_client
*get_client_from_id(notification_client_id id
,
1382 struct notification_thread_state
*state
)
1384 struct cds_lfht_iter iter
;
1385 struct cds_lfht_node
*node
;
1386 struct notification_client
*client
= nullptr;
1388 ASSERT_RCU_READ_LOCKED();
1390 cds_lfht_lookup(state
->client_id_ht
, hash_client_id(id
), match_client_id
, &id
, &iter
);
1391 node
= cds_lfht_iter_get_node(&iter
);
1396 client
= caa_container_of(node
, struct notification_client
, client_id_ht_node
);
1401 static bool buffer_usage_condition_applies_to_channel(const struct lttng_condition
*condition
,
1402 const struct channel_info
*channel_info
)
1404 enum lttng_condition_status status
;
1405 enum lttng_domain_type condition_domain
;
1406 const char *condition_session_name
= nullptr;
1407 const char *condition_channel_name
= nullptr;
1409 status
= lttng_condition_buffer_usage_get_domain_type(condition
, &condition_domain
);
1410 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
1411 if (channel_info
->key
.domain
!= condition_domain
) {
1415 status
= lttng_condition_buffer_usage_get_session_name(condition
, &condition_session_name
);
1416 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1418 status
= lttng_condition_buffer_usage_get_channel_name(condition
, &condition_channel_name
);
1419 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1421 if (strcmp(channel_info
->session_info
->name
, condition_session_name
) != 0) {
1424 if (strcmp(channel_info
->name
, condition_channel_name
) != 0) {
1433 static bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1434 const struct channel_info
*channel_info
)
1436 const struct lttng_condition
*condition
;
1437 bool trigger_applies
;
1439 condition
= lttng_trigger_get_const_condition(trigger
);
1444 switch (lttng_condition_get_type(condition
)) {
1445 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1446 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1448 buffer_usage_condition_applies_to_channel(condition
, channel_info
);
1454 return trigger_applies
;
1459 /* Must be called with RCU read lock held. */
1460 static struct lttng_session_trigger_list
*
1461 get_session_trigger_list(struct notification_thread_state
*state
, const char *session_name
)
1463 struct lttng_session_trigger_list
*list
= nullptr;
1464 struct cds_lfht_node
*node
;
1465 struct cds_lfht_iter iter
;
1467 ASSERT_RCU_READ_LOCKED();
1469 cds_lfht_lookup(state
->session_triggers_ht
,
1470 hash_key_str(session_name
, lttng_ht_seed
),
1471 match_session_trigger_list
,
1474 node
= cds_lfht_iter_get_node(&iter
);
1477 * Not an error, the list of triggers applying to that session
1478 * will be initialized when the session is created.
1480 DBG("No trigger list found for session \"%s\" as it is not yet known to the notification system",
1485 list
= caa_container_of(node
, struct lttng_session_trigger_list
, session_triggers_ht_node
);
1491 * Allocate an empty lttng_session_trigger_list for the session named
1494 static struct lttng_session_trigger_list
*
1495 lttng_session_trigger_list_create(const char *session_name
, struct cds_lfht
*session_triggers_ht
)
1497 struct lttng_session_trigger_list
*list
= nullptr;
1498 char *session_name_copy
= strdup(session_name
);
1500 if (!session_name_copy
) {
1501 PERROR("Failed to allocate session name while building trigger list");
1505 list
= zmalloc
<lttng_session_trigger_list
>();
1507 PERROR("Failed to allocate session trigger list while building trigger list");
1511 list
->session_name
= session_name_copy
;
1512 CDS_INIT_LIST_HEAD(&list
->list
);
1513 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1514 list
->session_triggers_ht
= session_triggers_ht
;
1516 /* Publish the list through the session_triggers_ht. */
1518 lttng::urcu::read_lock_guard read_lock
;
1519 cds_lfht_add(session_triggers_ht
,
1520 hash_key_str(session_name
, lttng_ht_seed
),
1521 &list
->session_triggers_ht_node
);
1527 static void free_session_trigger_list_rcu(struct rcu_head
*node
)
1529 struct lttng_session_trigger_list
*list
=
1530 caa_container_of(node
, struct lttng_session_trigger_list
, rcu_node
);
1532 free(list
->session_name
);
1536 static 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
, &list
->list
, node
) {
1542 cds_list_del(&trigger_list_element
->node
);
1543 free(trigger_list_element
);
1545 lttng::urcu::read_lock_guard read_lock
;
1546 /* Unpublish the list from the session_triggers_ht. */
1547 cds_lfht_del(list
->session_triggers_ht
, &list
->session_triggers_ht_node
);
1548 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1551 static int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1552 struct lttng_trigger
*trigger
)
1555 struct lttng_trigger_list_element
*new_element
= zmalloc
<lttng_trigger_list_element
>();
1561 CDS_INIT_LIST_HEAD(&new_element
->node
);
1562 new_element
->trigger
= trigger
;
1563 cds_list_add(&new_element
->node
, &list
->list
);
1568 static bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1569 const char *session_name
)
1571 bool applies
= false;
1572 const struct lttng_condition
*condition
;
1574 condition
= lttng_trigger_get_const_condition(trigger
);
1575 switch (lttng_condition_get_type(condition
)) {
1576 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1577 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1578 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1580 const char *condition_session_name
;
1582 condition_session_name
= get_condition_session_name(condition
);
1583 LTTNG_ASSERT(condition_session_name
);
1584 applies
= !strcmp(condition_session_name
, session_name
);
1595 * Allocate and initialize an lttng_session_trigger_list which contains
1596 * all triggers that apply to the session named 'session_name'.
1598 static struct lttng_session_trigger_list
*
1599 lttng_session_trigger_list_build(const struct notification_thread_state
*state
,
1600 const char *session_name
)
1602 int trigger_count
= 0;
1603 struct lttng_session_trigger_list
*session_trigger_list
= nullptr;
1604 struct lttng_trigger_ht_element
*trigger_ht_element
= nullptr;
1605 struct cds_lfht_iter iter
;
1607 session_trigger_list
=
1608 lttng_session_trigger_list_create(session_name
, state
->session_triggers_ht
);
1611 /* Add all triggers applying to the session named 'session_name'. */
1612 lttng::urcu::read_lock_guard read_lock
;
1614 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
1617 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1622 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1623 trigger_ht_element
->trigger
);
1632 DBG("Found %i triggers that apply to newly created session", trigger_count
);
1633 return session_trigger_list
;
1635 lttng_session_trigger_list_destroy(session_trigger_list
);
1639 static struct session_info
*create_and_publish_session_info(struct notification_thread_state
*state
,
1645 struct session_info
*session
= nullptr;
1646 struct lttng_session_trigger_list
*trigger_list
;
1648 lttng::urcu::read_lock_guard read_lock
;
1649 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1650 if (!trigger_list
) {
1654 session
= session_info_create(id
, name
, uid
, gid
, trigger_list
, state
->sessions_ht
);
1656 ERR("Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1660 lttng_session_trigger_list_destroy(trigger_list
);
1664 /* Transferred ownership to the new session. */
1665 trigger_list
= nullptr;
1667 if (cds_lfht_add_unique(state
->sessions_ht
,
1668 hash_session_info(session
),
1671 &session
->sessions_ht_node
) != &session
->sessions_ht_node
) {
1672 ERR("Duplicate session found: name = `%s`, id = %" PRIu64
, name
, id
);
1678 session_info_put(session
);
1682 static int handle_notification_thread_command_add_channel(struct notification_thread_state
*state
,
1683 uint64_t session_id
,
1684 const char *channel_name
,
1685 enum lttng_domain_type channel_domain
,
1686 uint64_t channel_key_int
,
1687 uint64_t channel_capacity
,
1688 enum lttng_error_code
*cmd_result
)
1690 struct cds_list_head trigger_list
;
1691 struct channel_info
*new_channel_info
= nullptr;
1692 struct channel_key channel_key
= {
1693 .key
= channel_key_int
,
1694 .domain
= channel_domain
,
1696 struct lttng_channel_trigger_list
*channel_trigger_list
= nullptr;
1697 struct lttng_trigger_ht_element
*trigger_ht_element
= nullptr;
1698 int trigger_count
= 0;
1699 struct cds_lfht_iter iter
;
1700 struct session_info
*session_info
= nullptr;
1701 lttng::urcu::read_lock_guard read_lock
;
1703 DBG("Adding channel: channel name = `%s`, session id = %" PRIu64
", channel key = %" PRIu64
1708 lttng_domain_type_str(channel_domain
));
1710 CDS_INIT_LIST_HEAD(&trigger_list
);
1712 session_info
= get_session_info_by_id(state
, session_id
);
1713 if (!session_info
) {
1714 /* Fatal logic error. */
1715 ERR("Failed to find session while adding channel: session id = %" PRIu64
,
1721 channel_info_create(channel_name
, &channel_key
, channel_capacity
, session_info
);
1722 if (!new_channel_info
) {
1726 /* Build a list of all triggers applying to the new channel. */
1727 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
1728 struct lttng_trigger_list_element
*new_element
;
1730 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
, new_channel_info
)) {
1734 new_element
= zmalloc
<lttng_trigger_list_element
>();
1738 CDS_INIT_LIST_HEAD(&new_element
->node
);
1739 new_element
->trigger
= trigger_ht_element
->trigger
;
1740 cds_list_add(&new_element
->node
, &trigger_list
);
1744 DBG("Found %i triggers that apply to newly added channel", trigger_count
);
1745 channel_trigger_list
= zmalloc
<lttng_channel_trigger_list
>();
1746 if (!channel_trigger_list
) {
1749 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1750 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1751 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1752 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1754 /* Add channel to the channel_ht which owns the channel_infos. */
1755 cds_lfht_add(state
->channels_ht
,
1756 hash_channel_key(&new_channel_info
->key
),
1757 &new_channel_info
->channels_ht_node
);
1759 * Add the list of triggers associated with this channel to the
1760 * channel_triggers_ht.
1762 cds_lfht_add(state
->channel_triggers_ht
,
1763 hash_channel_key(&new_channel_info
->key
),
1764 &channel_trigger_list
->channel_triggers_ht_node
);
1765 session_info_put(session_info
);
1766 *cmd_result
= LTTNG_OK
;
1769 channel_info_destroy(new_channel_info
);
1770 session_info_put(session_info
);
1774 static int handle_notification_thread_command_add_session(struct notification_thread_state
*state
,
1775 uint64_t session_id
,
1776 const char *session_name
,
1779 enum lttng_error_code
*cmd_result
)
1783 DBG("Adding session: session name = `%s`, session id = %" PRIu64
1784 ", session uid = %d, session gid = %d",
1790 auto session
= create_and_publish_session_info(
1791 state
, session_id
, session_name
, session_uid
, session_gid
);
1793 PERROR("Failed to add session: session name = `%s`, session id = %" PRIu64
1794 ", session uid = %d, session gid = %d",
1800 *cmd_result
= LTTNG_ERR_NOMEM
;
1805 * Note that the reference to `session` is not released; this reference is
1806 * the "global" reference that is used to allow look-ups. This reference will
1807 * only be released when the session is removed. See
1808 * handle_notification_thread_command_remove_session.
1811 *cmd_result
= LTTNG_OK
;
1817 handle_notification_thread_command_remove_session(struct notification_thread_state
*state
,
1818 uint64_t session_id
,
1819 enum lttng_error_code
*cmd_result
)
1823 DBG("Removing session: session id = %" PRIu64
, session_id
);
1825 auto session
= get_session_info_by_id(state
, session_id
);
1827 ERR("Failed to remove session: session id = %" PRIu64
, session_id
);
1829 *cmd_result
= LTTNG_ERR_NO_SESSION
;
1833 /* Release the reference returned by the look-up, and then release the global reference. */
1834 session_info_put(session
);
1835 session_info_put(session
);
1837 *cmd_result
= LTTNG_OK
;
1842 static void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1844 free(caa_container_of(node
, struct lttng_channel_trigger_list
, rcu_node
));
1847 static void free_channel_state_sample_rcu(struct rcu_head
*node
)
1849 free(caa_container_of(node
, struct channel_state_sample
, rcu_node
));
1853 handle_notification_thread_command_remove_channel(struct notification_thread_state
*state
,
1854 uint64_t channel_key
,
1855 enum lttng_domain_type domain
,
1856 enum lttng_error_code
*cmd_result
)
1858 struct cds_lfht_node
*node
;
1859 struct cds_lfht_iter iter
;
1860 struct lttng_channel_trigger_list
*trigger_list
;
1861 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1862 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1863 struct channel_info
*channel_info
;
1865 DBG("Removing channel key = %" PRIu64
" in %s domain",
1867 lttng_domain_type_str(domain
));
1869 lttng::urcu::read_lock_guard read_lock
;
1871 cds_lfht_lookup(state
->channel_triggers_ht
,
1872 hash_channel_key(&key
),
1873 match_channel_trigger_list
,
1876 node
= cds_lfht_iter_get_node(&iter
);
1878 * There is a severe internal error if we are being asked to remove a
1879 * channel that doesn't exist.
1882 ERR("Channel being removed is unknown to the notification thread");
1886 /* Free the list of triggers associated with this channel. */
1888 caa_container_of(node
, struct lttng_channel_trigger_list
, channel_triggers_ht_node
);
1889 cds_list_for_each_entry_safe (trigger_list_element
, tmp
, &trigger_list
->list
, node
) {
1890 cds_list_del(&trigger_list_element
->node
);
1891 free(trigger_list_element
);
1893 cds_lfht_del(state
->channel_triggers_ht
, node
);
1894 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1896 /* Free sampled channel state. */
1897 cds_lfht_lookup(state
->channel_state_ht
,
1898 hash_channel_key(&key
),
1899 match_channel_state_sample
,
1902 node
= cds_lfht_iter_get_node(&iter
);
1904 * This is expected to be NULL if the channel is destroyed before we
1905 * received a sample.
1908 struct channel_state_sample
*sample
=
1909 caa_container_of(node
, struct channel_state_sample
, channel_state_ht_node
);
1911 cds_lfht_del(state
->channel_state_ht
, node
);
1912 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1915 /* Remove the channel from the channels_ht and free it. */
1917 state
->channels_ht
, hash_channel_key(&key
), match_channel_info
, &key
, &iter
);
1918 node
= cds_lfht_iter_get_node(&iter
);
1920 channel_info
= caa_container_of(node
, struct channel_info
, channels_ht_node
);
1921 cds_lfht_del(state
->channels_ht
, node
);
1922 channel_info_destroy(channel_info
);
1924 *cmd_result
= LTTNG_OK
;
1929 handle_notification_thread_command_session_rotation(struct notification_thread_state
*state
,
1930 enum notification_thread_command_type cmd_type
,
1931 uint64_t session_id
,
1932 uint64_t trace_archive_chunk_id
,
1933 struct lttng_trace_archive_location
*location
,
1934 enum lttng_error_code
*_cmd_result
)
1937 enum lttng_error_code cmd_result
= LTTNG_OK
;
1938 struct lttng_session_trigger_list
*trigger_list
;
1939 struct lttng_trigger_list_element
*trigger_list_element
;
1940 struct session_info
*session_info
;
1941 struct lttng_credentials session_creds
;
1942 struct session_state_sample new_session_state
;
1944 lttng::urcu::read_lock_guard read_lock
;
1946 session_info
= get_session_info_by_id(state
, session_id
);
1947 if (!session_info
) {
1948 /* Fatal logic error. */
1949 ERR("Failed to find session while handling rotation state change: session id = %" PRIu64
,
1952 cmd_result
= LTTNG_ERR_FATAL
;
1956 new_session_state
= session_info
->last_state_sample
;
1958 lttng_trace_archive_location_get(location
);
1959 new_session_state
.rotation
.location
= location
;
1961 new_session_state
.rotation
.location
= nullptr;
1965 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_info
->uid
),
1966 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_info
->gid
),
1969 new_session_state
.rotation
.ongoing
= cmd_type
==
1970 NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1971 new_session_state
.rotation
.id
= trace_archive_chunk_id
;
1973 trigger_list
= get_session_trigger_list(state
, session_info
->name
);
1974 LTTNG_ASSERT(trigger_list
);
1976 cds_list_for_each_entry (trigger_list_element
, &trigger_list
->list
, node
) {
1977 const struct lttng_condition
*condition
;
1978 struct lttng_trigger
*trigger
;
1979 struct notification_client_list
*client_list
;
1980 struct lttng_evaluation
*evaluation
= nullptr;
1981 enum action_executor_status executor_status
;
1983 trigger
= trigger_list_element
->trigger
;
1984 condition
= lttng_trigger_get_const_condition(trigger
);
1985 LTTNG_ASSERT(condition
);
1987 ret
= evaluate_session_condition(
1988 condition
, session_info
, &new_session_state
, &evaluation
);
1991 cmd_result
= LTTNG_ERR_NOMEM
;
2000 * Ownership of `evaluation` transferred to the action executor
2001 * no matter the result. The callee acquires a reference to the
2002 * client list: we can release our own.
2004 client_list
= get_client_list_from_condition(state
, condition
);
2005 executor_status
= action_executor_enqueue_trigger(
2006 state
->executor
, trigger
, evaluation
, &session_creds
, client_list
);
2007 notification_client_list_put(client_list
);
2008 evaluation
= nullptr;
2009 switch (executor_status
) {
2010 case ACTION_EXECUTOR_STATUS_OK
:
2012 case ACTION_EXECUTOR_STATUS_ERROR
:
2013 case ACTION_EXECUTOR_STATUS_INVALID
:
2015 * TODO Add trigger identification (name/id) when
2016 * it is added to the API.
2018 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
2021 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2023 * TODO Add trigger identification (name/id) when
2024 * it is added to the API.
2026 * Not a fatal error.
2028 WARN("No space left when enqueuing action associated with session rotation trigger");
2038 /* Ownership of new_session_state::location is transferred. */
2039 lttng_trace_archive_location_put(session_info
->last_state_sample
.rotation
.location
);
2040 session_info
->last_state_sample
= new_session_state
;
2043 session_info_put(session_info
);
2044 *_cmd_result
= cmd_result
;
2049 handle_notification_thread_command_add_tracer_event_source(struct notification_thread_state
*state
,
2050 int tracer_event_source_fd
,
2051 enum lttng_domain_type domain_type
,
2052 enum lttng_error_code
*_cmd_result
)
2055 enum lttng_error_code cmd_result
= LTTNG_OK
;
2056 struct notification_event_tracer_event_source_element
*element
= nullptr;
2058 element
= zmalloc
<notification_event_tracer_event_source_element
>();
2060 cmd_result
= LTTNG_ERR_NOMEM
;
2065 element
->fd
= tracer_event_source_fd
;
2066 element
->domain
= domain_type
;
2068 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2070 DBG3("Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2071 tracer_event_source_fd
,
2072 lttng_domain_type_str(domain_type
));
2074 /* Adding the read side pipe to the event poll. */
2075 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLPRI
| LPOLLIN
);
2077 ERR("Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2078 tracer_event_source_fd
,
2079 lttng_domain_type_str(element
->domain
));
2080 cds_list_del(&element
->node
);
2085 element
->is_fd_in_poll_set
= true;
2088 *_cmd_result
= cmd_result
;
2092 static int drain_event_notifier_notification_pipe(struct notification_thread_state
*state
,
2094 enum lttng_domain_type domain
)
2096 struct lttng_poll_event events
= {};
2099 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2101 ERR("Error creating lttng_poll_event");
2105 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2107 ERR("Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2114 * Continue to consume notifications as long as there are new
2115 * ones coming in. The tracer has been asked to stop producing
2118 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2119 * monitored (on Linux, at least) and will be returned when
2120 * the pipe is closed but empty.
2122 ret
= lttng_poll_wait_interruptible(&events
, 0);
2123 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2124 /* No more notification to be read on this pipe. */
2127 } else if (ret
< 0) {
2128 PERROR("Failed on lttng_poll_wait_interruptible() call");
2133 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2135 ERR("Error consuming an event notifier notification from pipe: fd = %d",
2140 lttng_poll_clean(&events
);
2144 static struct notification_event_tracer_event_source_element
*
2145 find_tracer_event_source_element(struct notification_thread_state
*state
,
2146 int tracer_event_source_fd
)
2148 struct notification_event_tracer_event_source_element
*source_element
;
2150 cds_list_for_each_entry (source_element
, &state
->tracer_event_sources_list
, node
) {
2151 if (source_element
->fd
== tracer_event_source_fd
) {
2156 source_element
= nullptr;
2158 return source_element
;
2161 static int remove_tracer_event_source_from_pollset(
2162 struct notification_thread_state
*state
,
2163 struct notification_event_tracer_event_source_element
*source_element
)
2167 LTTNG_ASSERT(source_element
->is_fd_in_poll_set
);
2169 DBG3("Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2171 lttng_domain_type_str(source_element
->domain
));
2173 /* Removing the fd from the event poll set. */
2174 ret
= lttng_poll_del(&state
->events
, source_element
->fd
);
2176 ERR("Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2178 lttng_domain_type_str(source_element
->domain
));
2183 source_element
->is_fd_in_poll_set
= false;
2186 * Force the notification thread to restart the poll() loop to ensure
2187 * that any events from the removed fd are removed.
2189 state
->restart_poll
= true;
2191 ret
= drain_event_notifier_notification_pipe(
2192 state
, source_element
->fd
, source_element
->domain
);
2194 ERR("Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2196 lttng_domain_type_str(source_element
->domain
));
2205 int handle_notification_thread_tracer_event_source_died(struct notification_thread_state
*state
,
2206 int tracer_event_source_fd
)
2209 struct notification_event_tracer_event_source_element
*source_element
;
2211 source_element
= find_tracer_event_source_element(state
, tracer_event_source_fd
);
2213 LTTNG_ASSERT(source_element
);
2215 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2217 ERR("Failed to remove dead tracer event source from poll set");
2223 static int handle_notification_thread_command_remove_tracer_event_source(
2224 struct notification_thread_state
*state
,
2225 int tracer_event_source_fd
,
2226 enum lttng_error_code
*_cmd_result
)
2229 enum lttng_error_code cmd_result
= LTTNG_OK
;
2230 struct notification_event_tracer_event_source_element
*source_element
= nullptr;
2232 source_element
= find_tracer_event_source_element(state
, tracer_event_source_fd
);
2234 LTTNG_ASSERT(source_element
);
2236 /* Remove the tracer source from the list. */
2237 cds_list_del(&source_element
->node
);
2239 if (!source_element
->is_fd_in_poll_set
) {
2240 /* Skip the poll set removal. */
2244 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2246 ERR("Failed to remove tracer event source from poll set");
2247 cmd_result
= LTTNG_ERR_FATAL
;
2251 free(source_element
);
2252 *_cmd_result
= cmd_result
;
2257 handle_notification_thread_command_list_triggers(struct notification_thread_handle
*handle
2258 __attribute__((unused
)),
2259 struct notification_thread_state
*state
,
2261 struct lttng_triggers
**triggers
,
2262 enum lttng_error_code
*_cmd_result
)
2265 enum lttng_error_code cmd_result
= LTTNG_OK
;
2266 struct cds_lfht_iter iter
;
2267 struct lttng_trigger_ht_element
*trigger_ht_element
;
2268 struct lttng_triggers
*local_triggers
= nullptr;
2269 const struct lttng_credentials
*creds
;
2271 lttng::urcu::read_lock_guard read_lock
;
2273 local_triggers
= lttng_triggers_create();
2274 if (!local_triggers
) {
2275 /* Not a fatal error. */
2276 cmd_result
= LTTNG_ERR_NOMEM
;
2280 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2282 * Only return the triggers to which the client has access.
2283 * The root user has visibility over all triggers.
2285 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2286 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2290 ret
= lttng_triggers_add(local_triggers
, trigger_ht_element
->trigger
);
2292 /* Not a fatal error. */
2294 cmd_result
= LTTNG_ERR_NOMEM
;
2299 /* Transferring ownership to the caller. */
2300 *triggers
= local_triggers
;
2301 local_triggers
= nullptr;
2304 lttng_triggers_destroy(local_triggers
);
2305 *_cmd_result
= cmd_result
;
2309 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
2310 const char **trigger_name
,
2311 uid_t
*trigger_owner_uid
)
2313 enum lttng_trigger_status trigger_status
;
2315 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
2316 switch (trigger_status
) {
2317 case LTTNG_TRIGGER_STATUS_OK
:
2319 case LTTNG_TRIGGER_STATUS_UNSET
:
2320 *trigger_name
= "(anonymous)";
2326 trigger_status
= lttng_trigger_get_owner_uid(trigger
, trigger_owner_uid
);
2327 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2330 static int handle_notification_thread_command_get_trigger(struct notification_thread_state
*state
,
2331 const struct lttng_trigger
*trigger
,
2332 struct lttng_trigger
**registered_trigger
,
2333 enum lttng_error_code
*_cmd_result
)
2336 struct cds_lfht_iter iter
;
2337 struct lttng_trigger_ht_element
*trigger_ht_element
;
2338 enum lttng_error_code cmd_result
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2339 const char *trigger_name
;
2340 uid_t trigger_owner_uid
;
2343 lttng::urcu::read_lock_guard read_lock
;
2345 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2346 if (lttng_trigger_is_equal(trigger
, trigger_ht_element
->trigger
)) {
2347 /* Take one reference on the return trigger. */
2348 *registered_trigger
= trigger_ht_element
->trigger
;
2349 lttng_trigger_get(*registered_trigger
);
2351 cmd_result
= LTTNG_OK
;
2357 /* Not a fatal error if the trigger is not found. */
2358 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
2359 DBG("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d",
2361 (int) trigger_owner_uid
);
2366 *_cmd_result
= cmd_result
;
2370 static bool condition_is_supported(struct lttng_condition
*condition
)
2374 switch (lttng_condition_get_type(condition
)) {
2375 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2376 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2379 enum lttng_domain_type domain
;
2381 ret
= lttng_condition_buffer_usage_get_domain_type(condition
, &domain
);
2382 LTTNG_ASSERT(ret
== 0);
2384 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2385 is_supported
= true;
2390 * Older kernel tracers don't expose the API to monitor their
2391 * buffers. Therefore, we reject triggers that require that
2392 * mechanism to be available to be evaluated.
2394 * Assume unsupported on error.
2396 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2399 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
2401 const struct lttng_event_rule
*event_rule
;
2402 enum lttng_domain_type domain
;
2403 const enum lttng_condition_status status
=
2404 lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2406 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
2408 domain
= lttng_event_rule_get_domain_type(event_rule
);
2409 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2410 is_supported
= true;
2415 * Older kernel tracers can't emit notification. Therefore, we
2416 * reject triggers that require that mechanism to be available
2419 * Assume unsupported on error.
2421 is_supported
= kernel_supports_event_notifiers() == 1;
2425 is_supported
= true;
2428 return is_supported
;
2431 /* Must be called with RCU read lock held. */
2432 static int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2433 struct notification_thread_state
*state
)
2436 const struct lttng_condition
*condition
;
2437 const char *session_name
;
2438 struct lttng_session_trigger_list
*trigger_list
;
2440 ASSERT_RCU_READ_LOCKED();
2442 condition
= lttng_trigger_get_const_condition(trigger
);
2443 session_name
= get_condition_session_name(condition
);
2445 trigger_list
= get_session_trigger_list(state
, session_name
);
2446 if (!trigger_list
) {
2447 DBG("Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2452 DBG("Newly registered trigger bound to session \"%s\"", session_name
);
2453 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2458 /* Must be called with RCU read lock held. */
2459 static int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2460 struct notification_thread_state
*state
)
2463 struct cds_lfht_node
*node
;
2464 struct cds_lfht_iter iter
;
2465 struct channel_info
*channel
;
2467 ASSERT_RCU_READ_LOCKED();
2469 cds_lfht_for_each_entry (state
->channels_ht
, &iter
, channel
, channels_ht_node
) {
2470 struct lttng_trigger_list_element
*trigger_list_element
;
2471 struct lttng_channel_trigger_list
*trigger_list
;
2472 struct cds_lfht_iter lookup_iter
;
2474 if (!trigger_applies_to_channel(trigger
, channel
)) {
2478 cds_lfht_lookup(state
->channel_triggers_ht
,
2479 hash_channel_key(&channel
->key
),
2480 match_channel_trigger_list
,
2483 node
= cds_lfht_iter_get_node(&lookup_iter
);
2485 trigger_list
= caa_container_of(
2486 node
, struct lttng_channel_trigger_list
, channel_triggers_ht_node
);
2488 trigger_list_element
= zmalloc
<lttng_trigger_list_element
>();
2489 if (!trigger_list_element
) {
2493 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2494 trigger_list_element
->trigger
= trigger
;
2495 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2496 DBG("Newly registered trigger bound to channel \"%s\"", channel
->name
);
2502 static bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2504 bool is_notify
= false;
2505 const struct lttng_action
*action
= lttng_trigger_get_const_action(trigger
);
2506 const struct lttng_action
*inner_action
;
2507 enum lttng_action_type action_type
;
2509 LTTNG_ASSERT(action
);
2510 action_type
= lttng_action_get_type(action
);
2511 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2514 } else if (action_type
!= LTTNG_ACTION_TYPE_LIST
) {
2518 for_each_action_const (inner_action
, action
) {
2519 if (lttng_action_get_type(inner_action
) == LTTNG_ACTION_TYPE_NOTIFY
) {
2529 static bool trigger_name_taken(struct notification_thread_state
*state
,
2530 const struct lttng_trigger
*trigger
)
2532 struct cds_lfht_iter iter
;
2535 * No duplicata is allowed in the triggers_by_name_uid_ht.
2536 * The match is done against the trigger name and uid.
2538 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2539 hash_trigger_by_name_uid(trigger
),
2540 match_trigger_by_name_uid
,
2543 return !!cds_lfht_iter_get_node(&iter
);
2546 static enum lttng_error_code
generate_trigger_name(struct notification_thread_state
*state
,
2547 struct lttng_trigger
*trigger
,
2550 enum lttng_error_code ret_code
= LTTNG_OK
;
2552 enum lttng_trigger_status status
;
2556 lttng_trigger_generate_name(trigger
, state
->trigger_id
.name_offset
++);
2558 /* The only reason this can fail right now. */
2559 ret_code
= LTTNG_ERR_NOMEM
;
2563 status
= lttng_trigger_get_name(trigger
, name
);
2564 LTTNG_ASSERT(status
== LTTNG_TRIGGER_STATUS_OK
);
2566 taken
= trigger_name_taken(state
, trigger
);
2567 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2573 notif_thread_state_remove_trigger_ht_elem(struct notification_thread_state
*state
,
2574 struct lttng_trigger_ht_element
*trigger_ht_element
)
2576 LTTNG_ASSERT(state
);
2577 LTTNG_ASSERT(trigger_ht_element
);
2579 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2580 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2583 static enum lttng_error_code
setup_tracer_notifier(struct notification_thread_state
*state
,
2584 struct lttng_trigger
*trigger
)
2586 enum lttng_error_code ret
;
2587 enum event_notifier_error_accounting_status error_accounting_status
;
2588 struct cds_lfht_node
*node
;
2589 uint64_t error_counter_index
= 0;
2590 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2591 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= nullptr;
2593 trigger_tokens_ht_element
= zmalloc
<notification_trigger_tokens_ht_element
>();
2594 if (!trigger_tokens_ht_element
) {
2595 ret
= LTTNG_ERR_NOMEM
;
2599 /* Add trigger token to the trigger_tokens_ht. */
2600 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2601 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2602 trigger_tokens_ht_element
->trigger
= trigger
;
2604 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2605 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2606 match_trigger_token
,
2607 &trigger_tokens_ht_element
->token
,
2608 &trigger_tokens_ht_element
->node
);
2609 if (node
!= &trigger_tokens_ht_element
->node
) {
2610 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2611 goto error_free_ht_element
;
2614 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2615 trigger
, &error_counter_index
);
2616 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2617 if (error_accounting_status
==
2618 EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2619 DBG("Trigger list error accounting counter full.");
2620 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2622 ERR("Error registering trigger for error accounting");
2623 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2626 goto error_remove_ht_element
;
2629 lttng_condition_event_rule_matches_set_error_counter_index(condition
, error_counter_index
);
2634 error_remove_ht_element
:
2635 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2636 error_free_ht_element
:
2637 free(trigger_tokens_ht_element
);
2643 * FIXME A client's credentials are not checked when registering a trigger.
2645 * The effects of this are benign since:
2646 * - The client will succeed in registering the trigger, as it is valid,
2647 * - The trigger will, internally, be bound to the channel/session,
2648 * - The notifications will not be sent since the client's credentials
2649 * are checked against the channel at that moment.
2651 * If this function returns a non-zero value, it means something is
2652 * fundamentally broken and the whole subsystem/thread will be torn down.
2654 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2658 handle_notification_thread_command_register_trigger(struct notification_thread_state
*state
,
2659 struct lttng_trigger
*trigger
,
2660 bool is_trigger_anonymous
,
2661 enum lttng_error_code
*cmd_result
)
2664 struct lttng_condition
*condition
;
2665 struct notification_client_list
*client_list
= nullptr;
2666 struct lttng_trigger_ht_element
*trigger_ht_element
= nullptr;
2667 struct cds_lfht_node
*node
;
2668 const char *trigger_name
;
2669 bool free_trigger
= true;
2670 struct lttng_evaluation
*evaluation
= nullptr;
2671 struct lttng_credentials object_creds
;
2674 enum action_executor_status executor_status
;
2675 const uint64_t trigger_tracer_token
= state
->trigger_id
.next_tracer_token
++;
2677 lttng::urcu::read_lock_guard read_lock
;
2679 /* Set the trigger's tracer token. */
2680 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2682 if (!is_trigger_anonymous
) {
2683 if (lttng_trigger_get_name(trigger
, &trigger_name
) == LTTNG_TRIGGER_STATUS_UNSET
) {
2684 const enum lttng_error_code ret_code
=
2685 generate_trigger_name(state
, trigger
, &trigger_name
);
2687 if (ret_code
!= LTTNG_OK
) {
2690 *cmd_result
= ret_code
;
2693 } else if (trigger_name_taken(state
, trigger
)) {
2694 /* Not a fatal error. */
2695 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2700 trigger_name
= "(anonymous)";
2703 condition
= lttng_trigger_get_condition(trigger
);
2704 LTTNG_ASSERT(condition
);
2706 /* Some conditions require tracers to implement a minimal ABI version. */
2707 if (!condition_is_supported(condition
)) {
2708 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2712 trigger_ht_element
= zmalloc
<lttng_trigger_ht_element
>();
2713 if (!trigger_ht_element
) {
2718 /* Add trigger to the trigger_ht. */
2719 cds_lfht_node_init(&trigger_ht_element
->node
);
2720 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2721 trigger_ht_element
->trigger
= trigger
;
2723 node
= cds_lfht_add_unique(state
->triggers_ht
,
2724 lttng_condition_hash(condition
),
2727 &trigger_ht_element
->node
);
2728 if (node
!= &trigger_ht_element
->node
) {
2729 /* Not a fatal error, simply report it to the client. */
2730 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2731 goto error_free_ht_element
;
2734 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2735 hash_trigger_by_name_uid(trigger
),
2736 match_trigger_by_name_uid
,
2738 &trigger_ht_element
->node_by_name_uid
);
2739 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2740 /* Internal error: add to triggers_ht should have failed. */
2742 goto error_free_ht_element
;
2745 /* From this point consider the trigger registered. */
2746 lttng_trigger_set_as_registered(trigger
);
2749 * Some triggers might need a tracer notifier depending on its
2750 * condition and actions.
2752 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2753 enum lttng_error_code error_code
;
2755 error_code
= setup_tracer_notifier(state
, trigger
);
2756 if (error_code
!= LTTNG_OK
) {
2757 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
2758 if (error_code
== LTTNG_ERR_NOMEM
) {
2761 *cmd_result
= error_code
;
2765 goto error_free_ht_element
;
2770 * The rest only applies to triggers that have a "notify" action.
2771 * It is not skipped as this is the only action type currently
2774 if (is_trigger_action_notify(trigger
)) {
2776 * Find or create the client list of this condition. It may
2777 * already be present if another trigger is already registered
2778 * with the same condition.
2780 client_list
= get_client_list_from_condition(state
, condition
);
2783 * No client list for this condition yet. We create new
2784 * one and build it up.
2786 client_list
= notification_client_list_create(state
, condition
);
2788 ERR("Error creating notification client list for trigger %s",
2791 goto error_free_ht_element
;
2795 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2797 pthread_mutex_lock(&client_list
->lock
);
2798 cds_list_add(&trigger_ht_element
->client_list_trigger_node
,
2799 &client_list
->triggers_list
);
2800 pthread_mutex_unlock(&client_list
->lock
);
2804 * Ownership of the trigger and of its wrapper was transfered to
2805 * the triggers_ht. Same for token ht element if necessary.
2807 trigger_ht_element
= nullptr;
2808 free_trigger
= false;
2810 switch (get_condition_binding_object(condition
)) {
2811 case LTTNG_OBJECT_TYPE_SESSION
:
2812 /* Add the trigger to the list if it matches a known session. */
2813 ret
= bind_trigger_to_matching_session(trigger
, state
);
2815 goto error_free_ht_element
;
2818 case LTTNG_OBJECT_TYPE_CHANNEL
:
2820 * Add the trigger to list of triggers bound to the channels
2823 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2825 goto error_free_ht_element
;
2828 case LTTNG_OBJECT_TYPE_NONE
:
2831 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2833 goto error_free_ht_element
;
2837 * The new trigger's condition must be evaluated against the current
2840 * In the case of `notify` action, nothing preventing clients from
2841 * subscribing to a condition before the corresponding trigger is
2842 * registered, we have to evaluate this new condition right away.
2844 * At some point, we were waiting for the next "evaluation" (e.g. on
2845 * reception of a channel sample) to evaluate this new condition, but
2848 * The reason it was broken is that waiting for the next sample
2849 * does not allow us to properly handle transitions for edge-triggered
2852 * Consider this example: when we handle a new channel sample, we
2853 * evaluate each conditions twice: once with the previous state, and
2854 * again with the newest state. We then use those two results to
2855 * determine whether a state change happened: a condition was false and
2856 * became true. If a state change happened, we have to notify clients.
2858 * Now, if a client subscribes to a given notification and registers
2859 * a trigger *after* that subscription, we have to make sure the
2860 * condition is evaluated at this point while considering only the
2861 * current state. Otherwise, the next evaluation cycle may only see
2862 * that the evaluations remain the same (true for samples n-1 and n) and
2863 * the client will never know that the condition has been met.
2865 switch (get_condition_binding_object(condition
)) {
2866 case LTTNG_OBJECT_TYPE_SESSION
:
2868 /* Find the session associated with the condition. */
2869 const auto *session_name
= get_condition_session_name(condition
);
2870 auto session_info
= get_session_info_by_name(state
, session_name
);
2871 if (!session_info
) {
2872 /* Not an error, the session doesn't exist yet. */
2873 DBG("Session not found while evaluating session condition during registration of trigger: session name = `%s`",
2879 LTTNG_OPTIONAL_SET(&object_creds
.uid
, session_info
->uid
);
2880 LTTNG_OPTIONAL_SET(&object_creds
.gid
, session_info
->gid
);
2882 ret
= evaluate_session_condition(condition
, session_info
, nullptr, &evaluation
);
2883 session_info_put(session_info
);
2886 case LTTNG_OBJECT_TYPE_CHANNEL
:
2887 ret
= evaluate_channel_condition_for_client(
2888 condition
, state
, &evaluation
, &object_uid
, &object_gid
);
2889 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2890 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2892 case LTTNG_OBJECT_TYPE_NONE
:
2895 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2903 goto error_free_ht_element
;
2906 DBG("Newly registered trigger's condition evaluated to %s", evaluation
? "true" : "false");
2908 /* Evaluation yielded nothing. Normal exit. */
2914 * Ownership of `evaluation` transferred to the action executor
2915 * no matter the result.
2917 executor_status
= action_executor_enqueue_trigger(
2918 state
->executor
, trigger
, evaluation
, &object_creds
, client_list
);
2919 evaluation
= nullptr;
2920 switch (executor_status
) {
2921 case ACTION_EXECUTOR_STATUS_OK
:
2923 case ACTION_EXECUTOR_STATUS_ERROR
:
2924 case ACTION_EXECUTOR_STATUS_INVALID
:
2926 * TODO Add trigger identification (name/id) when
2927 * it is added to the API.
2929 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2931 goto error_free_ht_element
;
2932 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2934 * TODO Add trigger identification (name/id) when
2935 * it is added to the API.
2937 * Not a fatal error.
2939 WARN("No space left when enqueuing action associated to newly registered trigger");
2947 *cmd_result
= LTTNG_OK
;
2948 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2950 trigger_tracer_token
);
2953 error_free_ht_element
:
2954 if (trigger_ht_element
) {
2955 /* Delayed removal due to RCU constraint on delete. */
2956 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2961 * Other objects might have a reference to the trigger, mark it
2964 lttng_trigger_set_as_unregistered(trigger
);
2965 lttng_trigger_destroy(trigger
);
2971 static void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2973 free(caa_container_of(node
, struct lttng_trigger_ht_element
, rcu_node
));
2976 static void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2978 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
, rcu_node
));
2981 static void teardown_tracer_notifier(struct notification_thread_state
*state
,
2982 const struct lttng_trigger
*trigger
)
2984 struct cds_lfht_iter iter
;
2985 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2988 lttng::urcu::read_lock_guard read_lock
;
2990 cds_lfht_for_each_entry (
2991 state
->trigger_tokens_ht
, &iter
, trigger_tokens_ht_element
, node
) {
2992 if (!lttng_trigger_is_equal(trigger
, trigger_tokens_ht_element
->trigger
)) {
2996 event_notifier_error_accounting_unregister_event_notifier(
2997 trigger_tokens_ht_element
->trigger
);
2999 /* TODO talk to all app and remove it */
3000 DBG("Removed trigger from tokens_ht");
3001 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
3003 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
3004 free_notification_trigger_tokens_ht_element_rcu
);
3012 remove_trigger_from_session_trigger_list(struct lttng_session_trigger_list
*trigger_list
,
3013 const struct lttng_trigger
*trigger
)
3016 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3018 cds_list_for_each_entry_safe (trigger_element
, tmp
, &trigger_list
->list
, node
) {
3019 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3023 DBG("Removed trigger from session_triggers_ht");
3024 cds_list_del(&trigger_element
->node
);
3025 free(trigger_element
);
3026 /* A trigger can only appear once per session. */
3032 ERR("Failed to find trigger associated with session: session name = `%s`",
3033 trigger_list
->session_name
);
3036 LTTNG_ASSERT(found
);
3040 handle_notification_thread_command_unregister_trigger(struct notification_thread_state
*state
,
3041 const struct lttng_trigger
*trigger
,
3042 enum lttng_error_code
*_cmd_reply
)
3044 struct cds_lfht_iter iter
;
3045 struct cds_lfht_node
*triggers_ht_node
;
3046 struct notification_client_list
*client_list
;
3047 struct lttng_trigger_ht_element
*trigger_ht_element
= nullptr;
3048 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
3049 enum lttng_error_code cmd_reply
;
3051 lttng::urcu::read_lock_guard read_lock
;
3054 state
->triggers_ht
, lttng_condition_hash(condition
), match_trigger
, trigger
, &iter
);
3055 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3056 if (!triggers_ht_node
) {
3057 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3060 cmd_reply
= LTTNG_OK
;
3063 trigger_ht_element
=
3064 caa_container_of(triggers_ht_node
, struct lttng_trigger_ht_element
, node
);
3066 switch (get_condition_binding_object(condition
)) {
3067 case LTTNG_OBJECT_TYPE_CHANNEL
:
3069 struct lttng_channel_trigger_list
*trigger_list
;
3072 * Remove trigger from channel_triggers_ht.
3074 * Note that multiple channels may have matched the trigger's
3075 * condition (e.g. all instances of a given channel in per-pid buffering
3078 * Iterate on all lists since we don't know the target channels' keys.
3080 cds_lfht_for_each_entry (
3081 state
->channel_triggers_ht
, &iter
, trigger_list
, channel_triggers_ht_node
) {
3082 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3084 cds_list_for_each_entry_safe (
3085 trigger_element
, tmp
, &trigger_list
->list
, node
) {
3086 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3090 DBG("Removed trigger from channel_triggers_ht");
3091 cds_list_del(&trigger_element
->node
);
3092 free(trigger_element
);
3093 /* A trigger can only appear once per channel */
3099 case LTTNG_OBJECT_TYPE_SESSION
:
3102 get_session_info_by_name(state
, get_condition_session_name(condition
));
3104 /* Session doesn't exist, no trigger to remove. */
3109 auto session_trigger_list
= get_session_trigger_list(state
, session
->name
);
3110 remove_trigger_from_session_trigger_list(session_trigger_list
, trigger
);
3111 session_info_put(session
);
3113 case LTTNG_OBJECT_TYPE_NONE
:
3119 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3120 teardown_tracer_notifier(state
, trigger
);
3123 if (is_trigger_action_notify(trigger
)) {
3125 * Remove and release the client list from
3126 * notification_trigger_clients_ht.
3128 client_list
= get_client_list_from_condition(state
, condition
);
3129 LTTNG_ASSERT(client_list
);
3131 pthread_mutex_lock(&client_list
->lock
);
3132 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3133 pthread_mutex_unlock(&client_list
->lock
);
3135 /* Put new reference and the hashtable's reference. */
3136 notification_client_list_put(client_list
);
3137 notification_client_list_put(client_list
);
3138 client_list
= nullptr;
3141 /* Remove trigger from triggers_ht. */
3142 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3144 /* Release the ownership of the trigger. */
3145 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3146 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3149 *_cmd_reply
= cmd_reply
;
3154 static int pop_cmd_queue(struct notification_thread_handle
*handle
,
3155 struct notification_thread_command
**cmd
)
3160 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3161 ret
= lttng_read(handle
->cmd_queue
.event_fd
, &counter
, sizeof(counter
));
3162 if (ret
!= sizeof(counter
)) {
3167 *cmd
= cds_list_first_entry(
3168 &handle
->cmd_queue
.list
, struct notification_thread_command
, cmd_list_node
);
3169 cds_list_del(&((*cmd
)->cmd_list_node
));
3173 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3177 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3178 int handle_notification_thread_command(struct notification_thread_handle
*handle
,
3179 struct notification_thread_state
*state
)
3182 struct notification_thread_command
*cmd
;
3184 ret
= pop_cmd_queue(handle
, &cmd
);
3189 DBG("Received `%s` command", notification_command_type_str(cmd
->type
));
3190 switch (cmd
->type
) {
3191 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3192 ret
= handle_notification_thread_command_register_trigger(
3194 cmd
->parameters
.register_trigger
.trigger
,
3195 cmd
->parameters
.register_trigger
.is_trigger_anonymous
,
3198 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3199 ret
= handle_notification_thread_command_unregister_trigger(
3200 state
, cmd
->parameters
.unregister_trigger
.trigger
, &cmd
->reply_code
);
3202 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3203 ret
= handle_notification_thread_command_add_channel(
3205 cmd
->parameters
.add_channel
.session
.id
,
3206 cmd
->parameters
.add_channel
.channel
.name
,
3207 cmd
->parameters
.add_channel
.channel
.domain
,
3208 cmd
->parameters
.add_channel
.channel
.key
,
3209 cmd
->parameters
.add_channel
.channel
.capacity
,
3212 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3213 ret
= handle_notification_thread_command_remove_channel(
3215 cmd
->parameters
.remove_channel
.key
,
3216 cmd
->parameters
.remove_channel
.domain
,
3219 case NOTIFICATION_COMMAND_TYPE_ADD_SESSION
:
3220 ret
= handle_notification_thread_command_add_session(
3222 cmd
->parameters
.add_session
.session_id
,
3223 cmd
->parameters
.add_session
.session_name
,
3224 cmd
->parameters
.add_session
.session_uid
,
3225 cmd
->parameters
.add_session
.session_gid
,
3228 case NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION
:
3229 ret
= handle_notification_thread_command_remove_session(
3230 state
, cmd
->parameters
.remove_session
.session_id
, &cmd
->reply_code
);
3232 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3233 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3234 ret
= handle_notification_thread_command_session_rotation(
3237 cmd
->parameters
.session_rotation
.session_id
,
3238 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3239 cmd
->parameters
.session_rotation
.location
,
3242 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3243 ret
= handle_notification_thread_command_add_tracer_event_source(
3245 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3246 cmd
->parameters
.tracer_event_source
.domain
,
3249 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3250 ret
= handle_notification_thread_command_remove_tracer_event_source(
3252 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3255 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3257 struct lttng_triggers
*triggers
= nullptr;
3259 ret
= handle_notification_thread_command_list_triggers(
3262 cmd
->parameters
.list_triggers
.uid
,
3265 cmd
->reply
.list_triggers
.triggers
= triggers
;
3269 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3270 cmd
->reply_code
= LTTNG_OK
;
3273 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3275 struct lttng_trigger
*trigger
= nullptr;
3277 ret
= handle_notification_thread_command_get_trigger(
3278 state
, cmd
->parameters
.get_trigger
.trigger
, &trigger
, &cmd
->reply_code
);
3279 cmd
->reply
.get_trigger
.trigger
= trigger
;
3282 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3284 const enum client_transmission_status client_status
=
3285 cmd
->parameters
.client_communication_update
.status
;
3286 const notification_client_id client_id
=
3287 cmd
->parameters
.client_communication_update
.id
;
3288 struct notification_client
*client
;
3290 lttng::urcu::read_lock_guard read_lock
;
3291 client
= get_client_from_id(client_id
, state
);
3295 * Client error was probably already picked-up by the
3296 * notification thread or it has disconnected
3297 * gracefully while this command was queued.
3299 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3303 ret
= client_handle_transmission_status(client
, client_status
, state
);
3308 ERR("Unknown internal command received");
3316 if (cmd
->is_async
) {
3320 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3324 /* Wake-up and return a fatal error to the calling thread. */
3325 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3326 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3328 /* Indicate a fatal error to the caller. */
3332 static int socket_set_non_blocking(int socket
)
3336 /* Set the pipe as non-blocking. */
3337 ret
= fcntl(socket
, F_GETFL
, 0);
3339 PERROR("fcntl get socket flags");
3344 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3346 PERROR("fcntl set O_NONBLOCK socket flag");
3349 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3354 static int client_reset_inbound_state(struct notification_client
*client
)
3358 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3360 client
->communication
.inbound
.bytes_to_receive
=
3361 sizeof(struct lttng_notification_channel_message
);
3362 client
->communication
.inbound
.msg_type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3363 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3364 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3365 ret
= lttng_dynamic_buffer_set_size(&client
->communication
.inbound
.payload
.buffer
,
3366 client
->communication
.inbound
.bytes_to_receive
);
3371 int handle_notification_thread_client_connect(struct notification_thread_state
*state
)
3374 struct notification_client
*client
;
3376 DBG("Handling new notification channel client connection");
3378 client
= zmalloc
<notification_client
>();
3385 pthread_mutex_init(&client
->lock
, nullptr);
3386 client
->id
= state
->next_notification_client_id
++;
3387 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3388 lttng_payload_init(&client
->communication
.inbound
.payload
);
3389 lttng_payload_init(&client
->communication
.outbound
.payload
);
3390 client
->communication
.inbound
.expect_creds
= true;
3392 ret
= client_reset_inbound_state(client
);
3394 ERR("Failed to reset client communication's inbound state");
3399 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3401 ERR("Failed to accept new notification channel client connection");
3406 client
->socket
= ret
;
3408 ret
= socket_set_non_blocking(client
->socket
);
3410 ERR("Failed to set new notification channel client connection socket as non-blocking");
3414 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3416 ERR("Failed to set socket options on new notification channel client socket");
3421 client
->communication
.current_poll_events
= CLIENT_POLL_EVENTS_IN
;
3422 ret
= lttng_poll_add(
3423 &state
->events
, client
->socket
, client
->communication
.current_poll_events
);
3425 ERR("Failed to add notification channel client socket to poll set");
3429 DBG("Added new notification channel client socket (%i) to poll set", client
->socket
);
3432 lttng::urcu::read_lock_guard read_lock
;
3434 cds_lfht_add(state
->client_socket_ht
,
3435 hash_client_socket(client
->socket
),
3436 &client
->client_socket_ht_node
);
3437 cds_lfht_add(state
->client_id_ht
,
3438 hash_client_id(client
->id
),
3439 &client
->client_id_ht_node
);
3445 notification_client_destroy(client
);
3450 * RCU read-lock must be held by the caller.
3451 * Client lock must _not_ be held by the caller.
3453 static int notification_thread_client_disconnect(struct notification_client
*client
,
3454 struct notification_thread_state
*state
)
3457 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3459 ASSERT_RCU_READ_LOCKED();
3461 /* Acquire the client lock to disable its communication atomically. */
3462 pthread_mutex_lock(&client
->lock
);
3463 client
->communication
.active
= false;
3464 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3465 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3466 pthread_mutex_unlock(&client
->lock
);
3468 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3470 ERR("Failed to remove client socket %d from poll set", client
->socket
);
3473 /* Release all conditions to which the client was subscribed. */
3474 cds_list_for_each_entry_safe (condition_list_element
, tmp
, &client
->condition_list
, node
) {
3475 (void) notification_thread_client_unsubscribe(
3476 client
, condition_list_element
->condition
, state
, nullptr);
3480 * Client no longer accessible to other threads (through the
3483 notification_client_destroy(client
);
3487 int handle_notification_thread_client_disconnect(int client_socket
,
3488 struct notification_thread_state
*state
)
3491 struct notification_client
*client
;
3493 lttng::urcu::read_lock_guard read_lock
;
3495 DBG("Closing client connection (socket fd = %i)", client_socket
);
3496 client
= get_client_from_socket(client_socket
, state
);
3498 /* Internal state corruption, fatal error. */
3499 ERR("Unable to find client (socket fd = %i)", client_socket
);
3504 ret
= notification_thread_client_disconnect(client
, state
);
3509 int handle_notification_thread_client_disconnect_all(struct notification_thread_state
*state
)
3511 struct cds_lfht_iter iter
;
3512 struct notification_client
*client
;
3513 bool error_encoutered
= false;
3515 DBG("Closing all client connections");
3518 lttng::urcu::read_lock_guard read_lock
;
3520 cds_lfht_for_each_entry (
3521 state
->client_socket_ht
, &iter
, client
, client_socket_ht_node
) {
3524 ret
= notification_thread_client_disconnect(client
, state
);
3526 error_encoutered
= true;
3531 return error_encoutered
? 1 : 0;
3534 int handle_notification_thread_trigger_unregister_all(struct notification_thread_state
*state
)
3536 bool error_occurred
= false;
3537 struct cds_lfht_iter iter
;
3538 struct lttng_trigger_ht_element
*trigger_ht_element
;
3540 lttng::urcu::read_lock_guard read_lock
;
3541 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
3542 int ret
= handle_notification_thread_command_unregister_trigger(
3543 state
, trigger_ht_element
->trigger
, nullptr);
3545 error_occurred
= true;
3548 return error_occurred
? -1 : 0;
3551 static bool client_has_outbound_data_left(const struct notification_client
*client
)
3553 const struct lttng_payload_view pv
=
3554 lttng_payload_view_from_payload(&client
->communication
.outbound
.payload
, 0, -1);
3555 const bool has_data
= pv
.buffer
.size
!= 0;
3556 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3558 return has_data
|| has_fds
;
3561 static int client_handle_transmission_status(struct notification_client
*client
,
3562 enum client_transmission_status transmission_status
,
3563 struct notification_thread_state
*state
)
3567 switch (transmission_status
) {
3568 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3569 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3571 int current_poll_events
;
3572 int new_poll_events
;
3574 * We want to be notified whenever there is buffer space
3575 * available to send the rest of the payload if we are
3576 * waiting to send data to the client.
3578 * The state of the outbound queue being sampled here is
3580 * - it is okay to wake-up "for nothing" in case we see
3581 * that data is left, but another thread succeeds in
3582 * flushing it before us when handling the client "out"
3583 * event. We will simply stop monitoring that event the next
3584 * time it wakes us up and we see no data left to be sent,
3585 * - if another thread fails to flush the entire client
3586 * outgoing queue, it will issue a "communication update"
3587 * command and cause the client's (e)poll mask to be
3590 * The situation we seek to avoid would be to disable the
3591 * monitoring of "out" client events indefinitely when there is
3592 * data to be sent, which can't happen because of the
3593 * aforementioned "communication update" mechanism.
3595 pthread_mutex_lock(&client
->lock
);
3596 current_poll_events
= client
->communication
.current_poll_events
;
3597 new_poll_events
= client_has_outbound_data_left(client
) ?
3598 CLIENT_POLL_EVENTS_IN_OUT
:
3599 CLIENT_POLL_EVENTS_IN
;
3600 client
->communication
.current_poll_events
= new_poll_events
;
3601 pthread_mutex_unlock(&client
->lock
);
3603 /* Update the monitored event set only if it changed. */
3604 if (current_poll_events
!= new_poll_events
) {
3605 ret
= lttng_poll_mod(&state
->events
, client
->socket
, new_poll_events
);
3613 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3614 ret
= notification_thread_client_disconnect(client
, state
);
3619 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3629 /* Client lock must be acquired by caller. */
3630 static enum client_transmission_status
3631 client_flush_outgoing_queue(struct notification_client
*client
)
3634 size_t to_send_count
;
3635 enum client_transmission_status status
;
3636 struct lttng_payload_view pv
=
3637 lttng_payload_view_from_payload(&client
->communication
.outbound
.payload
, 0, -1);
3638 const int fds_to_send_count
= lttng_payload_view_get_fd_handle_count(&pv
);
3640 ASSERT_LOCKED(client
->lock
);
3642 if (!client
->communication
.active
) {
3643 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3647 if (pv
.buffer
.size
== 0) {
3649 * If both data and fds are equal to zero, we are in an invalid
3652 LTTNG_ASSERT(fds_to_send_count
!= 0);
3657 to_send_count
= pv
.buffer
.size
;
3658 DBG("Flushing client (socket fd = %i) outgoing queue", client
->socket
);
3660 ret
= lttcomm_send_unix_sock_non_block(client
->socket
, pv
.buffer
.data
, to_send_count
);
3661 if ((ret
>= 0 && ret
< to_send_count
)) {
3662 DBG("Client (socket fd = %i) outgoing queue could not be completely flushed",
3664 to_send_count
-= std::max(ret
, (ssize_t
) 0);
3666 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3667 pv
.buffer
.data
+ pv
.buffer
.size
- to_send_count
,
3669 ret
= lttng_dynamic_buffer_set_size(&client
->communication
.outbound
.payload
.buffer
,
3675 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3677 } else if (ret
< 0) {
3678 /* Generic error, disable the client's communication. */
3679 ERR("Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3681 client
->communication
.active
= false;
3682 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3686 * No error and flushed the queue completely.
3688 * The payload buffer size is used later to
3689 * check if there is notifications queued. So albeit that the
3690 * direct caller knows that the transmission is complete, we
3691 * need to set the buffer size to zero.
3693 ret
= lttng_dynamic_buffer_set_size(&client
->communication
.outbound
.payload
.buffer
,
3701 /* No fds to send, transmission is complete. */
3702 if (fds_to_send_count
== 0) {
3703 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3707 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(client
->socket
, &pv
);
3709 /* Generic error, disable the client's communication. */
3710 ERR("Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3712 client
->communication
.active
= false;
3713 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3715 } else if (ret
== 0) {
3716 /* Nothing could be sent. */
3717 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3719 /* Fd passing is an all or nothing kind of thing. */
3720 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3722 * The payload _fd_array count is used later to
3723 * check if there is notifications queued. So although the
3724 * direct caller knows that the transmission is complete, we
3725 * need to clear the _fd_array for the queuing check.
3727 lttng_dynamic_pointer_array_clear(
3728 &client
->communication
.outbound
.payload
._fd_handles
);
3732 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3733 client
->communication
.outbound
.queued_command_reply
= false;
3734 client
->communication
.outbound
.dropped_notification
= false;
3735 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3740 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3743 /* Client lock must _not_ be held by the caller. */
3744 static int client_send_command_reply(struct notification_client
*client
,
3745 struct notification_thread_state
*state
,
3746 enum lttng_notification_channel_status status
)
3749 struct lttng_notification_channel_command_reply reply
= {
3750 .status
= (int8_t) status
,
3752 struct lttng_notification_channel_message msg
;
3753 char buffer
[sizeof(msg
) + sizeof(reply
)];
3754 enum client_transmission_status transmission_status
;
3756 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
;
3757 msg
.size
= sizeof(reply
);
3760 memcpy(buffer
, &msg
, sizeof(msg
));
3761 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3762 DBG("Send command reply (%i)", (int) status
);
3764 pthread_mutex_lock(&client
->lock
);
3765 if (client
->communication
.outbound
.queued_command_reply
) {
3766 /* Protocol error. */
3770 /* Enqueue buffer to outgoing queue and flush it. */
3771 ret
= lttng_dynamic_buffer_append(
3772 &client
->communication
.outbound
.payload
.buffer
, buffer
, sizeof(buffer
));
3777 transmission_status
= client_flush_outgoing_queue(client
);
3779 if (client_has_outbound_data_left(client
)) {
3780 /* Queue could not be emptied. */
3781 client
->communication
.outbound
.queued_command_reply
= true;
3784 pthread_mutex_unlock(&client
->lock
);
3785 ret
= client_handle_transmission_status(client
, transmission_status
, state
);
3792 pthread_mutex_unlock(&client
->lock
);
3797 static int client_handle_message_unknown(struct notification_client
*client
,
3798 struct notification_thread_state
*state
3799 __attribute__((unused
)))
3803 * Receiving message header. The function will be called again
3804 * once the rest of the message as been received and can be
3807 const struct lttng_notification_channel_message
*msg
;
3809 LTTNG_ASSERT(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3810 msg
= (const struct lttng_notification_channel_message
*)
3811 client
->communication
.inbound
.payload
.buffer
.data
;
3813 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3814 ERR("Invalid notification channel message: length = %u", msg
->size
);
3819 switch (msg
->type
) {
3820 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3821 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3822 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3826 ERR("Invalid notification channel message: unexpected message type");
3830 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3831 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3832 client
->communication
.inbound
.msg_type
=
3833 (enum lttng_notification_channel_message_type
) msg
->type
;
3834 ret
= lttng_dynamic_buffer_set_size(&client
->communication
.inbound
.payload
.buffer
,
3837 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3843 static int client_handle_message_handshake(struct notification_client
*client
,
3844 struct notification_thread_state
*state
)
3847 struct lttng_notification_channel_command_handshake
*handshake_client
;
3848 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3849 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3850 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3852 struct lttng_notification_channel_message msg_header
;
3853 enum lttng_notification_channel_status status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3854 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3856 msg_header
.type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
;
3857 msg_header
.size
= sizeof(handshake_reply
);
3860 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3861 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
, sizeof(handshake_reply
));
3863 handshake_client
= (struct lttng_notification_channel_command_handshake
*)
3864 client
->communication
.inbound
.payload
.buffer
.data
;
3865 client
->major
= handshake_client
->major
;
3866 client
->minor
= handshake_client
->minor
;
3867 if (!client
->communication
.inbound
.creds_received
) {
3868 ERR("No credentials received from client");
3873 client
->uid
= LTTNG_SOCK_GET_UID_CRED(&client
->communication
.inbound
.creds
);
3874 client
->gid
= LTTNG_SOCK_GET_GID_CRED(&client
->communication
.inbound
.creds
);
3875 client
->is_sessiond
= LTTNG_SOCK_GET_PID_CRED(&client
->communication
.inbound
.creds
) ==
3877 DBG("Received handshake from client: uid = %u, gid = %u, protocol version = %i.%i, client is sessiond = %s",
3880 (int) client
->major
,
3881 (int) client
->minor
,
3882 client
->is_sessiond
? "true" : "false");
3884 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3885 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3888 pthread_mutex_lock(&client
->lock
);
3889 /* Outgoing queue will be flushed when the command reply is sent. */
3890 ret
= lttng_dynamic_buffer_append(
3891 &client
->communication
.outbound
.payload
.buffer
, send_buffer
, sizeof(send_buffer
));
3893 ERR("Failed to send protocol version to notification channel client");
3897 client
->validated
= true;
3898 client
->communication
.active
= true;
3899 pthread_mutex_unlock(&client
->lock
);
3901 /* Set reception state to receive the next message header. */
3902 ret
= client_reset_inbound_state(client
);
3904 ERR("Failed to reset client communication's inbound state");
3908 /* Flushes the outgoing queue. */
3909 ret
= client_send_command_reply(client
, state
, status
);
3911 ERR("Failed to send reply to notification channel client");
3917 pthread_mutex_unlock(&client
->lock
);
3922 static int client_handle_message_subscription(struct notification_client
*client
,
3923 enum lttng_notification_channel_message_type msg_type
,
3924 struct notification_thread_state
*state
)
3927 struct lttng_condition
*condition
;
3928 enum lttng_notification_channel_status status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3929 struct lttng_payload_view condition_view
=
3930 lttng_payload_view_from_payload(&client
->communication
.inbound
.payload
, 0, -1);
3931 size_t expected_condition_size
;
3934 * No need to lock client to sample the inbound state as the only
3935 * other thread accessing clients (action executor) only uses the
3938 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3939 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3940 if (ret
!= expected_condition_size
) {
3941 ERR("Malformed condition received from client");
3945 /* Ownership of condition is always transferred. */
3946 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3947 ret
= notification_thread_client_subscribe(client
, condition
, state
, &status
);
3949 ret
= notification_thread_client_unsubscribe(client
, condition
, state
, &status
);
3956 /* Set reception state to receive the next message header. */
3957 ret
= client_reset_inbound_state(client
);
3959 ERR("Failed to reset client communication's inbound state");
3963 ret
= client_send_command_reply(client
, state
, status
);
3965 ERR("Failed to send reply to notification channel client");
3973 static 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(
4003 client
, client
->communication
.inbound
.msg_type
, state
);
4013 /* Incoming data from client. */
4014 int handle_notification_thread_client_in(struct notification_thread_state
*state
, int socket
)
4017 struct notification_client
*client
;
4021 lttng::urcu::read_lock_guard read_lock
;
4022 client
= get_client_from_socket(socket
, state
);
4024 /* Internal error, abort. */
4029 if (client
->communication
.inbound
.bytes_to_receive
== 0 &&
4030 client
->communication
.inbound
.fds_to_receive
!= 0) {
4031 /* Only FDs left to receive. */
4035 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
4036 client
->communication
.inbound
.bytes_to_receive
;
4037 if (client
->communication
.inbound
.expect_creds
) {
4038 recv_ret
= lttcomm_recv_creds_unix_sock(
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(
4050 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
4051 client
->communication
.inbound
.bytes_to_receive
);
4053 if (recv_ret
>= 0) {
4054 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
4056 goto error_disconnect_client
;
4059 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
4060 /* Message incomplete wait for more data. */
4066 LTTNG_ASSERT(client
->communication
.inbound
.bytes_to_receive
== 0);
4069 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
4070 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
4072 client
->communication
.inbound
.fds_to_receive
,
4073 &client
->communication
.inbound
.payload
);
4076 * Fds received. non blocking fds passing is all
4079 ssize_t expected_size
;
4081 expected_size
= sizeof(int) * client
->communication
.inbound
.fds_to_receive
;
4082 LTTNG_ASSERT(ret
== expected_size
);
4083 client
->communication
.inbound
.fds_to_receive
= 0;
4084 } else if (ret
== 0) {
4085 /* Received nothing. */
4089 goto error_disconnect_client
;
4093 /* At this point the message is complete.*/
4094 LTTNG_ASSERT(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4095 client
->communication
.inbound
.fds_to_receive
== 0);
4096 ret
= client_dispatch_message(client
, state
);
4099 * Only returns an error if this client must be
4102 goto error_disconnect_client
;
4108 error_disconnect_client
:
4109 ret
= notification_thread_client_disconnect(client
, state
);
4113 /* Client ready to receive outgoing data. */
4114 int handle_notification_thread_client_out(struct notification_thread_state
*state
, int socket
)
4117 struct notification_client
*client
;
4118 enum client_transmission_status transmission_status
;
4120 lttng::urcu::read_lock_guard read_lock
;
4121 client
= get_client_from_socket(socket
, state
);
4123 /* Internal error, abort. */
4128 pthread_mutex_lock(&client
->lock
);
4129 if (!client_has_outbound_data_left(client
)) {
4131 * A client "out" event can be received when no payload is left
4132 * to send under some circumstances.
4134 * Many threads can flush a client's outgoing queue and, if they
4135 * had to queue their message (socket was full), will use the
4136 * "communication update" command to signal the (e)poll thread
4137 * to monitor for space being made available in the socket.
4139 * Commands are sent over an internal pipe serviced by the same
4140 * thread as the client sockets.
4142 * When space is made available in the socket, there is a race
4143 * between the (e)poll thread and the other threads that may
4144 * wish to use the client's socket to flush its outgoing queue.
4146 * A non-(e)poll thread may attempt (and succeed) in flushing
4147 * the queue before the (e)poll thread gets a chance to service
4148 * the client's "out" event.
4150 * In this situation, the (e)poll thread processing the client
4151 * out event will see an empty payload: there is nothing to do
4152 * except unsubscribing (e)poll "out" events.
4154 * Note that this thread is the (e)poll thread so it can modify
4155 * the (e)poll mask directly without using a communication
4156 * update command. Other threads that flush the outgoing queue
4157 * will use the "communication update" command to wake up this
4158 * thread and force it to monitor "out" events.
4160 * When other threads succeed in emptying the outgoing queue,
4161 * they don't need to update the (e)poll mask: if the "out"
4162 * event is monitored, it will fire once and the (e)poll
4163 * thread will reach this condition, causing the event to
4164 * stop being monitored.
4166 transmission_status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
4168 transmission_status
= client_flush_outgoing_queue(client
);
4170 pthread_mutex_unlock(&client
->lock
);
4172 ret
= client_handle_transmission_status(client
, transmission_status
, state
);
4180 static bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4181 const struct channel_state_sample
*sample
,
4182 uint64_t buffer_capacity
)
4184 bool result
= false;
4186 enum lttng_condition_type condition_type
;
4187 const struct lttng_condition_buffer_usage
*use_condition
=
4188 lttng::utils::container_of(condition
, <tng_condition_buffer_usage::parent
);
4190 if (use_condition
->threshold_bytes
.set
) {
4191 threshold
= use_condition
->threshold_bytes
.value
;
4194 * Threshold was expressed as a ratio.
4196 * TODO the threshold (in bytes) of conditions expressed
4197 * as a ratio of total buffer size could be cached to
4198 * forego this double-multiplication or it could be performed
4199 * as fixed-point math.
4201 * Note that caching should accommodates the case where the
4202 * condition applies to multiple channels (i.e. don't assume
4203 * that all channels matching my_chann* have the same size...)
4205 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4206 (double) buffer_capacity
);
4209 condition_type
= lttng_condition_get_type(condition
);
4210 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4211 DBG("Low buffer usage condition being evaluated: threshold = %" PRIu64
4212 ", highest usage = %" PRIu64
,
4214 sample
->highest_usage
);
4217 * The low condition should only be triggered once _all_ of the
4218 * streams in a channel have gone below the "low" threshold.
4220 if (sample
->highest_usage
<= threshold
) {
4224 DBG("High buffer usage condition being evaluated: threshold = %" PRIu64
4225 ", highest usage = %" PRIu64
,
4227 sample
->highest_usage
);
4230 * For high buffer usage scenarios, we want to trigger whenever
4231 * _any_ of the streams has reached the "high" threshold.
4233 if (sample
->highest_usage
>= threshold
) {
4241 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4242 struct lttng_evaluation
**evaluation
,
4243 const struct notification_thread_state
*state
4244 __attribute__((unused
)),
4245 const struct channel_state_sample
*previous_sample
,
4246 const struct channel_state_sample
*latest_sample
,
4247 struct channel_info
*channel_info
)
4250 enum lttng_condition_type condition_type
;
4251 const bool previous_sample_available
= !!previous_sample
;
4252 bool previous_sample_result
= false;
4253 bool latest_sample_result
;
4255 condition_type
= lttng_condition_get_type(condition
);
4257 switch (condition_type
) {
4258 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4259 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4260 if (caa_likely(previous_sample_available
)) {
4261 previous_sample_result
= evaluate_buffer_usage_condition(
4262 condition
, previous_sample
, channel_info
->capacity
);
4264 latest_sample_result
= evaluate_buffer_usage_condition(
4265 condition
, latest_sample
, channel_info
->capacity
);
4268 /* Unknown condition type; internal error. */
4272 if (!latest_sample_result
|| (previous_sample_result
== latest_sample_result
)) {
4274 * Only trigger on a condition evaluation transition.
4276 * NOTE: This edge-triggered logic may not be appropriate for
4277 * future condition types.
4282 if (!evaluation
|| !latest_sample_result
) {
4286 switch (condition_type
) {
4287 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4288 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4289 *evaluation
= lttng_evaluation_buffer_usage_create(
4290 condition_type
, latest_sample
->highest_usage
, channel_info
->capacity
);
4304 static int client_notification_overflow(struct notification_client
*client
)
4307 struct lttng_notification_channel_message msg
;
4309 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
;
4313 ASSERT_LOCKED(client
->lock
);
4315 DBG("Dropping notification addressed to client (socket fd = %i)", client
->socket
);
4316 if (client
->communication
.outbound
.dropped_notification
) {
4318 * The client already has a "notification dropped" message
4319 * in its outgoing queue. Nothing to do since all
4320 * of those messages are coalesced.
4325 client
->communication
.outbound
.dropped_notification
= true;
4326 ret
= lttng_dynamic_buffer_append(
4327 &client
->communication
.outbound
.payload
.buffer
, &msg
, sizeof(msg
));
4329 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4336 static int client_handle_transmission_status_wrapper(struct notification_client
*client
,
4337 enum client_transmission_status status
,
4340 return client_handle_transmission_status(
4341 client
, status
, (struct notification_thread_state
*) user_data
);
4344 static int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4345 const struct lttng_evaluation
*evaluation
,
4346 struct notification_client_list
*client_list
,
4347 struct notification_thread_state
*state
,
4351 const struct lttng_credentials creds
= {
4352 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4353 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4356 return notification_client_list_send_evaluation(client_list
,
4360 client_handle_transmission_status_wrapper
,
4365 * Permission checks relative to notification channel clients are performed
4366 * here. Notice how object, client, and trigger credentials are involved in
4369 * The `object` credentials are the credentials associated with the "subject"
4370 * of a condition. For instance, a `rotation completed` condition applies
4371 * to a session. When that condition is met, it will produce an evaluation
4372 * against a session. Hence, in this case, the `object` credentials are the
4373 * credentials of the "subject" session.
4375 * The `trigger` credentials are the credentials of the user that registered the
4378 * The `client` credentials are the credentials of the user that created a given
4379 * notification channel.
4381 * In terms of visibility, it is expected that non-privilieged users can only
4382 * register triggers against "their" objects (their own sessions and
4383 * applications they are allowed to interact with). They can then open a
4384 * notification channel and subscribe to notifications associated with those
4387 * As for privilieged users, they can register triggers against the objects of
4388 * other users. They can then subscribe to the notifications associated to their
4389 * triggers. Privilieged users _can't_ subscribe to the notifications of
4390 * triggers owned by other users; they must create their own triggers.
4392 * This is more a concern of usability than security. It would be difficult for
4393 * a root user reliably subscribe to a specific set of conditions without
4394 * interference from external users (those could, for instance, unregister
4397 int notification_client_list_send_evaluation(struct notification_client_list
*client_list
,
4398 const struct lttng_trigger
*trigger
,
4399 const struct lttng_evaluation
*evaluation
,
4400 const struct lttng_credentials
*source_object_creds
,
4401 report_client_transmission_result_cb client_report
,
4405 struct lttng_payload msg_payload
;
4406 struct notification_client_list_element
*client_list_element
, *tmp
;
4407 const struct lttng_notification notification
= {
4408 .trigger
= (struct lttng_trigger
*) trigger
,
4409 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4411 struct lttng_notification_channel_message msg_header
;
4412 const struct lttng_credentials
*trigger_creds
= lttng_trigger_get_credentials(trigger
);
4414 lttng_payload_init(&msg_payload
);
4416 msg_header
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
4417 msg_header
.size
= 0;
4420 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
, sizeof(msg_header
));
4425 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4427 ERR("Failed to serialize notification");
4432 /* Update payload size. */
4433 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)->size
=
4434 (uint32_t) (msg_payload
.buffer
.size
- sizeof(msg_header
));
4436 /* Update the payload number of fds. */
4438 const struct lttng_payload_view pv
=
4439 lttng_payload_view_from_payload(&msg_payload
, 0, -1);
4441 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)->fds
=
4442 (uint32_t) lttng_payload_view_get_fd_handle_count(&pv
);
4445 pthread_mutex_lock(&client_list
->lock
);
4446 cds_list_for_each_entry_safe (client_list_element
, tmp
, &client_list
->clients_list
, node
) {
4447 enum client_transmission_status transmission_status
;
4448 struct notification_client
*client
= client_list_element
->client
;
4451 pthread_mutex_lock(&client
->lock
);
4452 if (!client
->communication
.active
) {
4454 * Skip inactive client (protocol error or
4457 DBG("Skipping client at it is marked as inactive");
4461 if (lttng_trigger_is_hidden(trigger
) && !client
->is_sessiond
) {
4463 * Notifications resulting from an hidden trigger are
4464 * only sent to the session daemon.
4466 DBG("Skipping client as the trigger is hidden and the client is not the session daemon");
4470 if (source_object_creds
) {
4471 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4472 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4475 * Client is not allowed to monitor this
4478 DBG("Skipping client at it does not have the object permission to receive notification for this trigger");
4483 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
)) {
4484 DBG("Skipping client at it does not have the permission to receive notification for this trigger");
4488 DBG("Sending notification to client (fd = %i, %zu bytes)",
4490 msg_payload
.buffer
.size
);
4492 if (client_has_outbound_data_left(client
)) {
4494 * Outgoing data is already buffered for this client;
4495 * drop the notification and enqueue a "dropped
4496 * notification" message if this is the first dropped
4497 * notification since the socket spilled-over to the
4500 ret
= client_notification_overflow(client
);
4507 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4513 transmission_status
= client_flush_outgoing_queue(client
);
4514 pthread_mutex_unlock(&client
->lock
);
4515 ret
= client_report(client
, transmission_status
, user_data
);
4518 goto end_unlock_list
;
4524 pthread_mutex_unlock(&client
->lock
);
4527 goto end_unlock_list
;
4533 pthread_mutex_unlock(&client_list
->lock
);
4535 lttng_payload_reset(&msg_payload
);
4539 static struct lttng_event_notifier_notification
*
4540 recv_one_event_notifier_notification(int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4544 struct lttng_event_notifier_notification
*notification
= nullptr;
4545 char *capture_buffer
= nullptr;
4546 size_t capture_buffer_size
;
4547 void *reception_buffer
;
4548 size_t reception_size
;
4550 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4551 struct lttng_kernel_abi_event_notifier_notification kernel_notification
;
4553 /* Init lttng_event_notifier_notification */
4555 case LTTNG_DOMAIN_UST
:
4556 reception_buffer
= (void *) &ust_notification
;
4557 reception_size
= sizeof(ust_notification
);
4559 case LTTNG_DOMAIN_KERNEL
:
4560 reception_buffer
= (void *) &kernel_notification
;
4561 reception_size
= sizeof(kernel_notification
);
4568 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4569 * ensuring that read/write of tracer notifications are atomic.
4571 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
, reception_size
);
4572 if (ret
!= reception_size
) {
4573 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4574 notification_pipe_read_fd
,
4582 case LTTNG_DOMAIN_UST
:
4583 token
= ust_notification
.token
;
4584 capture_buffer_size
= ust_notification
.capture_buf_size
;
4586 case LTTNG_DOMAIN_KERNEL
:
4587 token
= kernel_notification
.token
;
4588 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4594 if (capture_buffer_size
== 0) {
4595 capture_buffer
= nullptr;
4599 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4600 ERR("Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4601 capture_buffer_size
,
4606 capture_buffer
= calloc
<char>(capture_buffer_size
);
4607 if (!capture_buffer
) {
4608 ERR("Failed to allocate capture buffer");
4612 /* Fetch additional payload (capture). */
4613 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4614 if (ret
!= capture_buffer_size
) {
4615 ERR("Failed to read from event source pipe (fd = %i)", notification_pipe_read_fd
);
4620 notification
= lttng_event_notifier_notification_create(
4621 token
, domain
, capture_buffer
, capture_buffer_size
);
4622 if (notification
== nullptr) {
4627 * Ownership transfered to the lttng_event_notifier_notification object.
4629 capture_buffer
= nullptr;
4632 free(capture_buffer
);
4633 return notification
;
4637 dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4638 struct lttng_event_notifier_notification
*notification
)
4640 struct cds_lfht_node
*node
;
4641 struct cds_lfht_iter iter
;
4642 struct notification_trigger_tokens_ht_element
*element
;
4643 struct lttng_evaluation
*evaluation
= nullptr;
4644 enum action_executor_status executor_status
;
4645 struct notification_client_list
*client_list
= nullptr;
4647 unsigned int capture_count
= 0;
4649 /* Find triggers associated with this token. */
4650 lttng::urcu::read_lock_guard read_lock
;
4651 cds_lfht_lookup(state
->trigger_tokens_ht
,
4652 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4653 match_trigger_token
,
4654 ¬ification
->tracer_token
,
4656 node
= cds_lfht_iter_get_node(&iter
);
4657 if (caa_unlikely(!node
)) {
4659 * This is not an error, slow consumption of the tracer
4660 * notifications can lead to situations where a trigger is
4661 * removed but we still get tracer notifications matching a
4662 * trigger that no longer exists.
4668 element
= caa_container_of(node
, struct notification_trigger_tokens_ht_element
, node
);
4670 if (lttng_condition_event_rule_matches_get_capture_descriptor_count(
4671 lttng_trigger_get_const_condition(element
->trigger
), &capture_count
) !=
4672 LTTNG_CONDITION_STATUS_OK
) {
4673 ERR("Failed to get capture count");
4678 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4679 ERR("Expected capture but capture buffer is null");
4684 evaluation
= lttng_evaluation_event_rule_matches_create(
4685 lttng::utils::container_of(lttng_trigger_get_const_condition(element
->trigger
),
4686 <tng_condition_event_rule_matches::parent
),
4687 notification
->capture_buffer
,
4688 notification
->capture_buf_size
,
4691 if (evaluation
== nullptr) {
4692 ERR("Failed to create event rule matches evaluation while creating and enqueuing action executor job");
4697 client_list
= get_client_list_from_condition(
4698 state
, lttng_trigger_get_const_condition(element
->trigger
));
4699 executor_status
= action_executor_enqueue_trigger(
4700 state
->executor
, element
->trigger
, evaluation
, nullptr, client_list
);
4701 switch (executor_status
) {
4702 case ACTION_EXECUTOR_STATUS_OK
:
4705 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4707 struct notification_client_list_element
*client_list_element
, *tmp
;
4710 * Not a fatal error; this is expected and simply means the
4711 * executor has too much work queued already.
4715 /* No clients subscribed to notifications for this trigger. */
4720 /* Warn clients that a notification (or more) was dropped. */
4721 pthread_mutex_lock(&client_list
->lock
);
4722 cds_list_for_each_entry_safe (
4723 client_list_element
, tmp
, &client_list
->clients_list
, node
) {
4724 enum client_transmission_status transmission_status
;
4725 struct notification_client
*client
= client_list_element
->client
;
4727 pthread_mutex_lock(&client
->lock
);
4728 ret
= client_notification_overflow(client
);
4734 transmission_status
= client_flush_outgoing_queue(client
);
4735 ret
= client_handle_transmission_status(client
, transmission_status
, state
);
4741 pthread_mutex_unlock(&client
->lock
);
4747 pthread_mutex_unlock(&client_list
->lock
);
4750 case ACTION_EXECUTOR_STATUS_INVALID
:
4751 case ACTION_EXECUTOR_STATUS_ERROR
:
4752 /* Fatal error, shut down everything. */
4753 ERR("Fatal error encoutered while enqueuing action to the action executor");
4757 /* Unhandled error. */
4762 notification_client_list_put(client_list
);
4767 static int handle_one_event_notifier_notification(struct notification_thread_state
*state
,
4769 enum lttng_domain_type domain
)
4772 struct lttng_event_notifier_notification
*notification
= nullptr;
4774 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4775 if (notification
== nullptr) {
4776 /* Reception failed, don't consider it fatal. */
4777 ERR("Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4779 lttng_domain_type_str(domain
));
4783 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4785 ERR("Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4787 lttng_domain_type_str(domain
));
4792 lttng_event_notifier_notification_destroy(notification
);
4796 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4798 enum lttng_domain_type domain
)
4800 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4803 int handle_notification_thread_channel_sample(struct notification_thread_state
*state
,
4805 enum lttng_domain_type domain
)
4808 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4809 struct channel_info
*channel_info
= nullptr;
4810 struct cds_lfht_node
*node
;
4811 struct cds_lfht_iter iter
;
4812 struct lttng_channel_trigger_list
*channel_trigger_list
;
4813 struct lttng_session_trigger_list
*session_trigger_list
;
4814 struct lttng_trigger_list_element
*trigger_list_element
;
4815 bool previous_sample_available
= false;
4816 struct channel_state_sample channel_previous_sample
, channel_new_sample
;
4817 struct session_state_sample session_new_sample
;
4818 struct lttng_credentials channel_creds
= {};
4819 struct lttng_credentials session_creds
= {};
4820 struct session_info
*session
;
4821 lttng::urcu::read_lock_guard read_lock
;
4824 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4825 * ensuring that read/write of sampling messages are atomic.
4827 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4828 if (ret
!= sizeof(sample_msg
)) {
4829 ERR("Failed to read from monitoring pipe (fd = %i)", pipe
);
4835 channel_new_sample
.key
.key
= sample_msg
.key
;
4836 channel_new_sample
.key
.domain
= domain
;
4837 channel_new_sample
.highest_usage
= sample_msg
.highest
;
4838 channel_new_sample
.lowest_usage
= sample_msg
.lowest
;
4840 session
= get_session_info_by_id(state
, sample_msg
.session_id
);
4842 DBG("Received a sample for an unknown session from consumerd: session id = %" PRIu64
,
4843 sample_msg
.session_id
);
4847 session_new_sample
= session
->last_state_sample
;
4848 session_new_sample
.consumed_data_size
+= sample_msg
.consumed_since_last_sample
;
4850 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
4851 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
4854 session_trigger_list
= get_session_trigger_list(state
, session
->name
);
4855 LTTNG_ASSERT(session_trigger_list
);
4856 cds_list_for_each_entry (trigger_list_element
, &session_trigger_list
->list
, node
) {
4857 const struct lttng_condition
*condition
;
4858 struct lttng_trigger
*trigger
;
4859 struct notification_client_list
*client_list
= nullptr;
4860 struct lttng_evaluation
*evaluation
= nullptr;
4861 enum action_executor_status executor_status
;
4864 trigger
= trigger_list_element
->trigger
;
4865 condition
= lttng_trigger_get_const_condition(trigger
);
4866 LTTNG_ASSERT(condition
);
4868 ret
= evaluate_session_condition(
4869 condition
, session
, &session_new_sample
, &evaluation
);
4870 if (caa_unlikely(ret
)) {
4874 if (caa_likely(!evaluation
)) {
4879 * Ownership of `evaluation` transferred to the action executor
4880 * no matter the result. The callee acquires a reference to the
4881 * client list: we can release our own.
4883 client_list
= get_client_list_from_condition(state
, condition
);
4884 executor_status
= action_executor_enqueue_trigger(
4885 state
->executor
, trigger
, evaluation
, &session_creds
, client_list
);
4886 notification_client_list_put(client_list
);
4887 evaluation
= nullptr;
4888 switch (executor_status
) {
4889 case ACTION_EXECUTOR_STATUS_OK
:
4891 case ACTION_EXECUTOR_STATUS_ERROR
:
4892 case ACTION_EXECUTOR_STATUS_INVALID
:
4894 * TODO Add trigger identification (name/id) when
4895 * it is added to the API.
4897 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4900 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4902 * TODO Add trigger identification (name/id) when
4903 * it is added to the API.
4905 * Not a fatal error.
4907 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4915 /* Retrieve the channel's informations */
4916 cds_lfht_lookup(state
->channels_ht
,
4917 hash_channel_key(&channel_new_sample
.key
),
4919 &channel_new_sample
.key
,
4921 node
= cds_lfht_iter_get_node(&iter
);
4922 if (caa_unlikely(!node
)) {
4924 * Not an error since the consumer can push a sample to the pipe
4925 * and the rest of the session daemon could notify us of the
4926 * channel's destruction before we get a chance to process that
4929 DBG("Received a sample for an unknown channel from consumerd, key = %" PRIu64
4931 channel_new_sample
.key
.key
,
4932 lttng_domain_type_str(domain
));
4936 channel_info
= caa_container_of(node
, struct channel_info
, channels_ht_node
);
4937 DBG("Handling channel sample for channel %s (key = %" PRIu64
4938 ") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
4939 ", consumed since last sample = %" PRIu64
")",
4941 channel_new_sample
.key
.key
,
4942 channel_info
->session_info
->name
,
4943 channel_new_sample
.highest_usage
,
4944 channel_new_sample
.lowest_usage
,
4945 sample_msg
.consumed_since_last_sample
);
4947 /* Retrieve the channel's last sample, if it exists, and update it. */
4948 cds_lfht_lookup(state
->channel_state_ht
,
4949 hash_channel_key(&channel_new_sample
.key
),
4950 match_channel_state_sample
,
4951 &channel_new_sample
.key
,
4953 node
= cds_lfht_iter_get_node(&iter
);
4954 if (caa_likely(node
)) {
4955 struct channel_state_sample
*stored_sample
;
4957 /* Update the sample stored. */
4959 caa_container_of(node
, struct channel_state_sample
, channel_state_ht_node
);
4961 memcpy(&channel_previous_sample
, stored_sample
, sizeof(channel_previous_sample
));
4962 stored_sample
->highest_usage
= channel_new_sample
.highest_usage
;
4963 stored_sample
->lowest_usage
= channel_new_sample
.lowest_usage
;
4964 previous_sample_available
= true;
4967 * This is the channel's first sample, allocate space for and
4968 * store the new sample.
4970 struct channel_state_sample
*stored_sample
;
4972 stored_sample
= zmalloc
<channel_state_sample
>();
4973 if (!stored_sample
) {
4978 memcpy(stored_sample
, &channel_new_sample
, sizeof(*stored_sample
));
4979 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4980 cds_lfht_add(state
->channel_state_ht
,
4981 hash_channel_key(&stored_sample
->key
),
4982 &stored_sample
->channel_state_ht_node
);
4985 /* Find triggers associated with this channel. */
4986 cds_lfht_lookup(state
->channel_triggers_ht
,
4987 hash_channel_key(&channel_new_sample
.key
),
4988 match_channel_trigger_list
,
4989 &channel_new_sample
.key
,
4991 node
= cds_lfht_iter_get_node(&iter
);
4994 channel_creds
= (typeof(channel_creds
)){
4995 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4996 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4999 channel_trigger_list
=
5000 caa_container_of(node
, struct lttng_channel_trigger_list
, channel_triggers_ht_node
);
5001 cds_list_for_each_entry (trigger_list_element
, &channel_trigger_list
->list
, node
) {
5002 const struct lttng_condition
*condition
;
5003 struct lttng_trigger
*trigger
;
5004 struct notification_client_list
*client_list
= nullptr;
5005 struct lttng_evaluation
*evaluation
= nullptr;
5006 enum action_executor_status executor_status
;
5009 trigger
= trigger_list_element
->trigger
;
5010 condition
= lttng_trigger_get_const_condition(trigger
);
5011 LTTNG_ASSERT(condition
);
5013 ret
= evaluate_buffer_condition(
5017 previous_sample_available
? &channel_previous_sample
: nullptr,
5018 &channel_new_sample
,
5020 if (caa_unlikely(ret
)) {
5024 if (caa_likely(!evaluation
)) {
5029 * Ownership of `evaluation` transferred to the action executor
5030 * no matter the result. The callee acquires a reference to the
5031 * client list: we can release our own.
5033 client_list
= get_client_list_from_condition(state
, condition
);
5034 executor_status
= action_executor_enqueue_trigger(
5035 state
->executor
, trigger
, evaluation
, &channel_creds
, client_list
);
5036 notification_client_list_put(client_list
);
5037 evaluation
= nullptr;
5038 switch (executor_status
) {
5039 case ACTION_EXECUTOR_STATUS_OK
:
5041 case ACTION_EXECUTOR_STATUS_ERROR
:
5042 case ACTION_EXECUTOR_STATUS_INVALID
:
5044 * TODO Add trigger identification (name/id) when
5045 * it is added to the API.
5047 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
5050 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
5052 * TODO Add trigger identification (name/id) when
5053 * it is added to the API.
5055 * Not a fatal error.
5057 WARN("No space left when enqueuing action associated with buffer-condition trigger");
5066 session
->last_state_sample
= session_new_sample
;
5068 session_info_put(session
);