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>
28 #include <lttng/action/action-internal.hpp>
29 #include <lttng/action/list-internal.hpp>
30 #include <lttng/condition/buffer-usage-internal.hpp>
31 #include <lttng/condition/condition-internal.hpp>
32 #include <lttng/condition/condition.h>
33 #include <lttng/condition/event-rule-matches-internal.hpp>
34 #include <lttng/condition/session-consumed-size-internal.hpp>
35 #include <lttng/condition/session-rotation-internal.hpp>
36 #include <lttng/domain-internal.hpp>
37 #include <lttng/event-rule/event-rule-internal.hpp>
38 #include <lttng/location-internal.hpp>
39 #include <lttng/notification/channel-internal.hpp>
40 #include <lttng/notification/notification-internal.hpp>
41 #include <lttng/trigger/trigger-internal.hpp>
48 #include <urcu/rculfhash.h>
50 #define CLIENT_POLL_EVENTS_IN (LPOLLIN | LPOLLRDHUP)
51 #define CLIENT_POLL_EVENTS_IN_OUT (CLIENT_POLL_EVENTS_IN | LPOLLOUT)
53 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
54 #define MAX_CAPTURE_SIZE (PIPE_BUF)
56 enum lttng_object_type
{
57 LTTNG_OBJECT_TYPE_UNKNOWN
,
58 LTTNG_OBJECT_TYPE_NONE
,
59 LTTNG_OBJECT_TYPE_CHANNEL
,
60 LTTNG_OBJECT_TYPE_SESSION
,
63 struct lttng_channel_trigger_list
{
64 struct channel_key channel_key
;
65 /* List of struct lttng_trigger_list_element. */
66 struct cds_list_head list
;
67 /* Node in the channel_triggers_ht */
68 struct cds_lfht_node channel_triggers_ht_node
;
69 /* call_rcu delayed reclaim. */
70 struct rcu_head rcu_node
;
74 * List of triggers applying to a given session.
77 * - lttng_session_trigger_list_create()
78 * - lttng_session_trigger_list_build()
79 * - lttng_session_trigger_list_destroy()
80 * - lttng_session_trigger_list_add()
82 struct lttng_session_trigger_list
{
84 /* List of struct lttng_trigger_list_element. */
85 struct cds_list_head list
;
86 /* Node in the session_triggers_ht */
87 struct cds_lfht_node session_triggers_ht_node
;
89 * Weak reference to the notification system's session triggers
92 * The session trigger list structure structure is owned by
93 * the session's session_info.
95 * The session_info is kept alive the the channel_infos holding a
96 * reference to it (reference counting). When those channels are
97 * destroyed (at runtime or on teardown), the reference they hold
98 * to the session_info are released. On destruction of session_info,
99 * session_info_destroy() will remove the list of triggers applying
100 * to this session from the notification system's state.
102 * This implies that the session_triggers_ht must be destroyed
103 * after the channels.
105 struct cds_lfht
*session_triggers_ht
;
106 /* Used for delayed RCU reclaim. */
107 struct rcu_head rcu_node
;
111 struct lttng_trigger_list_element
{
112 /* No ownership of the trigger object is assumed. */
113 struct lttng_trigger
*trigger
;
114 struct cds_list_head node
;
117 struct lttng_trigger_ht_element
{
118 struct lttng_trigger
*trigger
;
119 struct cds_lfht_node node
;
120 struct cds_lfht_node node_by_name_uid
;
121 struct cds_list_head client_list_trigger_node
;
122 /* call_rcu delayed reclaim. */
123 struct rcu_head rcu_node
;
126 struct lttng_condition_list_element
{
127 struct lttng_condition
*condition
;
128 struct cds_list_head node
;
131 struct channel_state_sample
{
132 struct channel_key key
;
133 struct cds_lfht_node channel_state_ht_node
;
134 uint64_t highest_usage
;
135 uint64_t lowest_usage
;
136 /* call_rcu delayed reclaim. */
137 struct rcu_head rcu_node
;
141 static unsigned long hash_channel_key(struct channel_key
*key
);
142 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
143 struct lttng_evaluation
**evaluation
,
144 const struct notification_thread_state
*state
,
145 const struct channel_state_sample
*previous_sample
,
146 const struct channel_state_sample
*latest_sample
,
147 struct channel_info
*channel_info
);
148 static int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
149 const struct lttng_evaluation
*evaluation
,
150 struct notification_client_list
*client_list
,
151 struct notification_thread_state
*state
,
155 /* session_info API */
156 static void session_info_destroy(void *_data
);
157 static void session_info_get(struct session_info
*session_info
);
158 static void session_info_put(struct session_info
*session_info
);
159 static struct session_info
*session_info_create(uint64_t id
,
163 struct lttng_session_trigger_list
*trigger_list
,
164 struct cds_lfht
*sessions_ht
);
165 static void session_info_add_channel(struct session_info
*session_info
,
166 struct channel_info
*channel_info
);
167 static void session_info_remove_channel(struct session_info
*session_info
,
168 struct channel_info
*channel_info
);
170 /* lttng_session_trigger_list API */
171 static struct lttng_session_trigger_list
*
172 lttng_session_trigger_list_create(const char *session_name
, struct cds_lfht
*session_triggers_ht
);
173 static struct lttng_session_trigger_list
*
174 lttng_session_trigger_list_build(const struct notification_thread_state
*state
,
175 const char *session_name
);
176 static void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
);
177 static int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
178 struct lttng_trigger
*trigger
);
180 static int client_handle_transmission_status(struct notification_client
*client
,
181 enum client_transmission_status transmission_status
,
182 struct notification_thread_state
*state
);
184 static int handle_one_event_notifier_notification(struct notification_thread_state
*state
,
186 enum lttng_domain_type domain
);
188 static void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
190 static int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
192 /* This double-cast is intended to supress pointer-to-cast warning. */
193 const int socket
= (int) (intptr_t) key
;
194 const struct notification_client
*client
=
195 caa_container_of(node
, struct notification_client
, client_socket_ht_node
);
197 return client
->socket
== socket
;
200 static int match_client_id(struct cds_lfht_node
*node
, const void *key
)
202 /* This double-cast is intended to supress pointer-to-cast warning. */
203 const notification_client_id id
= *((notification_client_id
*) key
);
204 const struct notification_client
*client
=
205 lttng::utils::container_of(node
, ¬ification_client::client_id_ht_node
);
207 return client
->id
== id
;
210 static int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
212 struct channel_key
*channel_key
= (struct channel_key
*) key
;
213 struct lttng_channel_trigger_list
*trigger_list
;
216 caa_container_of(node
, struct lttng_channel_trigger_list
, channel_triggers_ht_node
);
218 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
219 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
222 static int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
224 const char *session_name
= (const char *) key
;
225 struct lttng_session_trigger_list
*trigger_list
;
228 caa_container_of(node
, struct lttng_session_trigger_list
, session_triggers_ht_node
);
230 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
233 static int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
235 struct channel_key
*channel_key
= (struct channel_key
*) key
;
236 struct channel_state_sample
*sample
;
238 sample
= caa_container_of(node
, struct channel_state_sample
, channel_state_ht_node
);
240 return !!((channel_key
->key
== sample
->key
.key
) &&
241 (channel_key
->domain
== sample
->key
.domain
));
244 static int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
246 struct channel_key
*channel_key
= (struct channel_key
*) key
;
247 struct channel_info
*channel_info
;
249 channel_info
= caa_container_of(node
, struct channel_info
, channels_ht_node
);
251 return !!((channel_key
->key
== channel_info
->key
.key
) &&
252 (channel_key
->domain
== channel_info
->key
.domain
));
255 static int match_trigger(struct cds_lfht_node
*node
, const void *key
)
257 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
258 struct lttng_trigger_ht_element
*trigger_ht_element
;
260 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
, node
);
262 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
265 static int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
267 const uint64_t *_key
= (uint64_t *) key
;
268 struct notification_trigger_tokens_ht_element
*element
;
270 element
= caa_container_of(node
, struct notification_trigger_tokens_ht_element
, node
);
271 return *_key
== element
->token
;
274 static int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
276 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
277 struct notification_client_list
*client_list
;
278 const struct lttng_condition
*condition
;
280 LTTNG_ASSERT(condition_key
);
282 client_list
= caa_container_of(
283 node
, struct notification_client_list
, notification_trigger_clients_ht_node
);
284 condition
= client_list
->condition
;
286 return !!lttng_condition_is_equal(condition_key
, condition
);
289 static int match_session_info(struct cds_lfht_node
*node
, const void *key
)
291 const auto session_id
= *((uint64_t *) key
);
292 const auto *session_info
=
293 lttng::utils::container_of(node
, &session_info::sessions_ht_node
);
295 return session_id
== session_info
->id
;
298 static unsigned long hash_session_info_id(uint64_t id
)
300 return hash_key_u64(&id
, lttng_ht_seed
);
303 static unsigned long hash_session_info(const struct session_info
*session_info
)
305 return hash_session_info_id(session_info
->id
);
308 static struct session_info
*get_session_info_by_id(const struct notification_thread_state
*state
,
311 struct cds_lfht_iter iter
;
312 struct cds_lfht_node
*node
;
313 lttng::urcu::read_lock_guard read_lock_guard
;
316 state
->sessions_ht
, hash_session_info_id(id
), match_session_info
, &id
, &iter
);
317 node
= cds_lfht_iter_get_node(&iter
);
321 lttng::utils::container_of(node
, &session_info::sessions_ht_node
);
323 session_info_get(session_info
);
330 static struct session_info
*get_session_info_by_name(const struct notification_thread_state
*state
,
334 const auto found
= sample_session_id_by_name(name
, &session_id
);
336 return found
? get_session_info_by_id(state
, session_id
) : nullptr;
339 static const char *notification_command_type_str(enum notification_thread_command_type type
)
342 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
343 return "REGISTER_TRIGGER";
344 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
345 return "UNREGISTER_TRIGGER";
346 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
347 return "ADD_CHANNEL";
348 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
349 return "REMOVE_CHANNEL";
350 case NOTIFICATION_COMMAND_TYPE_ADD_SESSION
:
351 return "ADD_SESSION";
352 case NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION
:
353 return "REMOVE_SESSION";
354 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
355 return "SESSION_ROTATION_ONGOING";
356 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
357 return "SESSION_ROTATION_COMPLETED";
358 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
359 return "ADD_TRACER_EVENT_SOURCE";
360 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
361 return "REMOVE_TRACER_EVENT_SOURCE";
362 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
363 return "LIST_TRIGGERS";
364 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
365 return "GET_TRIGGER";
366 case NOTIFICATION_COMMAND_TYPE_QUIT
:
368 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
369 return "CLIENT_COMMUNICATION_UPDATE";
376 * Match trigger based on name and credentials only.
377 * Name duplication is NOT allowed for the same uid.
379 static int match_trigger_by_name_uid(struct cds_lfht_node
*node
, const void *key
)
382 const char *element_trigger_name
;
383 const char *key_name
;
384 enum lttng_trigger_status status
;
385 const struct lttng_credentials
*key_creds
;
386 const struct lttng_credentials
*node_creds
;
387 const struct lttng_trigger
*trigger_key
= (const struct lttng_trigger
*) key
;
388 const struct lttng_trigger_ht_element
*trigger_ht_element
=
389 caa_container_of(node
, struct lttng_trigger_ht_element
, node_by_name_uid
);
391 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &element_trigger_name
);
392 element_trigger_name
= status
== LTTNG_TRIGGER_STATUS_OK
? element_trigger_name
: nullptr;
394 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
395 key_name
= status
== LTTNG_TRIGGER_STATUS_OK
? key_name
: nullptr;
399 * Consider null names as not equal. This is to maintain backwards
400 * compatibility with pre-2.13 anonymous triggers. Multiples anonymous
401 * triggers are allowed for a given user.
403 if (!element_trigger_name
|| !key_name
) {
407 if (strcmp(element_trigger_name
, key_name
) != 0) {
411 /* Compare the owners' UIDs. */
412 key_creds
= lttng_trigger_get_credentials(trigger_key
);
413 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
415 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
422 * Hash trigger based on name and credentials only.
424 static unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
426 unsigned long hash
= 0;
427 const struct lttng_credentials
*trigger_creds
;
428 const char *trigger_name
;
429 enum lttng_trigger_status status
;
431 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
432 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
433 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
436 trigger_creds
= lttng_trigger_get_credentials(trigger
);
437 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
443 static unsigned long hash_channel_key(struct channel_key
*key
)
445 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
446 unsigned long domain_hash
=
447 hash_key_ulong((void *) (unsigned long) key
->domain
, lttng_ht_seed
);
449 return key_hash
^ domain_hash
;
452 static unsigned long hash_client_socket(int socket
)
454 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
457 static unsigned long hash_client_id(notification_client_id id
)
459 return hash_key_u64(&id
, lttng_ht_seed
);
463 * Get the type of object to which a given condition applies. Bindings let
464 * the notification system evaluate a trigger's condition when a given
465 * object's state is updated.
467 * For instance, a condition bound to a channel will be evaluated everytime
468 * the channel's state is changed by a channel monitoring sample.
470 static enum lttng_object_type
get_condition_binding_object(const struct lttng_condition
*condition
)
472 switch (lttng_condition_get_type(condition
)) {
473 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
474 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
475 return LTTNG_OBJECT_TYPE_CHANNEL
;
476 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
477 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
478 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
479 return LTTNG_OBJECT_TYPE_SESSION
;
480 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
481 return LTTNG_OBJECT_TYPE_NONE
;
483 return LTTNG_OBJECT_TYPE_UNKNOWN
;
487 static void free_channel_info_rcu(struct rcu_head
*node
)
489 free(lttng::utils::container_of(node
, &channel_info::rcu_node
));
492 static void channel_info_destroy(struct channel_info
*channel_info
)
498 if (channel_info
->session_info
) {
499 session_info_remove_channel(channel_info
->session_info
, channel_info
);
500 session_info_put(channel_info
->session_info
);
502 if (channel_info
->name
) {
503 free(channel_info
->name
);
505 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
508 static void free_session_info_rcu(struct rcu_head
*node
)
510 free(lttng::utils::container_of(node
, &session_info::rcu_node
));
513 /* Don't call directly, use the ref-counting mechanism. */
514 static void session_info_destroy(void *_data
)
516 struct session_info
*session_info
= (struct session_info
*) _data
;
519 LTTNG_ASSERT(session_info
);
520 if (session_info
->channel_infos_ht
) {
521 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, nullptr);
523 ERR("Failed to destroy channel information hash table");
526 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
529 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
)
598 cds_lfht_add(session_info
->channel_infos_ht
,
599 hash_channel_key(&channel_info
->key
),
600 &channel_info
->session_info_channels_ht_node
);
604 static void session_info_remove_channel(struct session_info
*session_info
,
605 struct channel_info
*channel_info
)
608 cds_lfht_del(session_info
->channel_infos_ht
, &channel_info
->session_info_channels_ht_node
);
612 static struct channel_info
*channel_info_create(const char *channel_name
,
613 struct channel_key
*channel_key
,
614 uint64_t channel_capacity
,
615 struct session_info
*session_info
)
617 struct channel_info
*channel_info
= zmalloc
<struct channel_info
>();
623 cds_lfht_node_init(&channel_info
->channels_ht_node
);
624 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
625 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
626 channel_info
->capacity
= channel_capacity
;
628 channel_info
->name
= strdup(channel_name
);
629 if (!channel_info
->name
) {
634 * Set the references between session and channel infos:
635 * - channel_info holds a strong reference to session_info
636 * - session_info holds a weak reference to channel_info
638 session_info_get(session_info
);
639 session_info_add_channel(session_info
, channel_info
);
640 channel_info
->session_info
= session_info
;
644 channel_info_destroy(channel_info
);
648 bool notification_client_list_get(struct notification_client_list
*list
)
650 return urcu_ref_get_unless_zero(&list
->ref
);
653 static void free_notification_client_list_rcu(struct rcu_head
*node
)
655 free(caa_container_of(node
, struct notification_client_list
, rcu_node
));
658 static void notification_client_list_release(struct urcu_ref
*list_ref
)
660 struct notification_client_list
*list
=
661 lttng::utils::container_of(list_ref
, ¬ification_client_list::ref
);
662 struct notification_client_list_element
*client_list_element
, *tmp
;
664 lttng_condition_put(list
->condition
);
666 if (list
->notification_trigger_clients_ht
) {
669 cds_lfht_del(list
->notification_trigger_clients_ht
,
670 &list
->notification_trigger_clients_ht_node
);
672 list
->notification_trigger_clients_ht
= nullptr;
674 cds_list_for_each_entry_safe (client_list_element
, tmp
, &list
->clients_list
, node
) {
675 free(client_list_element
);
678 LTTNG_ASSERT(cds_list_empty(&list
->triggers_list
));
680 pthread_mutex_destroy(&list
->lock
);
681 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
684 static bool condition_applies_to_client(const struct lttng_condition
*condition
,
685 struct notification_client
*client
)
687 bool applies
= false;
688 struct lttng_condition_list_element
*condition_list_element
;
690 cds_list_for_each_entry (condition_list_element
, &client
->condition_list
, node
) {
691 applies
= lttng_condition_is_equal(condition_list_element
->condition
, condition
);
700 static struct notification_client_list
*
701 notification_client_list_create(struct notification_thread_state
*state
,
702 const struct lttng_condition
*condition
)
704 struct notification_client
*client
;
705 struct cds_lfht_iter iter
;
706 struct notification_client_list
*client_list
;
708 client_list
= zmalloc
<notification_client_list
>();
710 PERROR("Failed to allocate notification client list");
714 pthread_mutex_init(&client_list
->lock
, nullptr);
716 * The trigger that owns the condition has the first reference to this
719 urcu_ref_init(&client_list
->ref
);
720 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
721 CDS_INIT_LIST_HEAD(&client_list
->clients_list
);
722 CDS_INIT_LIST_HEAD(&client_list
->triggers_list
);
725 * Create a copy of the condition so that it's independent of any
726 * trigger. The client list may outlive the trigger object (which owns
727 * the condition) that is used to create it.
729 client_list
->condition
= lttng_condition_copy(condition
);
731 /* Build a list of clients to which this new condition applies. */
732 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
, client_socket_ht_node
) {
733 struct notification_client_list_element
*client_list_element
;
735 if (!condition_applies_to_client(condition
, client
)) {
739 client_list_element
= zmalloc
<notification_client_list_element
>();
740 if (!client_list_element
) {
741 goto error_put_client_list
;
744 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
745 client_list_element
->client
= client
;
746 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
749 client_list
->notification_trigger_clients_ht
= state
->notification_trigger_clients_ht
;
753 * Add the client list to the global list of client list.
755 cds_lfht_add_unique(state
->notification_trigger_clients_ht
,
756 lttng_condition_hash(client_list
->condition
),
757 match_client_list_condition
,
758 client_list
->condition
,
759 &client_list
->notification_trigger_clients_ht_node
);
763 error_put_client_list
:
764 notification_client_list_put(client_list
);
765 client_list
= nullptr;
771 void notification_client_list_put(struct notification_client_list
*list
)
776 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
779 /* Provides a reference to the returned list. */
780 static struct notification_client_list
*
781 get_client_list_from_condition(struct notification_thread_state
*state
,
782 const struct lttng_condition
*condition
)
784 struct cds_lfht_node
*node
;
785 struct cds_lfht_iter iter
;
786 struct notification_client_list
*list
= nullptr;
789 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
790 lttng_condition_hash(condition
),
791 match_client_list_condition
,
794 node
= cds_lfht_iter_get_node(&iter
);
796 list
= lttng::utils::container_of(
797 node
, ¬ification_client_list::notification_trigger_clients_ht_node
);
798 list
= notification_client_list_get(list
) ? list
: nullptr;
805 static int evaluate_channel_condition_for_client(const struct lttng_condition
*condition
,
806 struct notification_thread_state
*state
,
807 struct lttng_evaluation
**evaluation
,
812 struct cds_lfht_iter iter
;
813 struct cds_lfht_node
*node
;
814 struct channel_info
*channel_info
= nullptr;
815 struct channel_key
*channel_key
= nullptr;
816 struct channel_state_sample
*last_sample
= nullptr;
817 struct lttng_channel_trigger_list
*channel_trigger_list
= nullptr;
821 /* Find the channel associated with the condition. */
822 cds_lfht_for_each_entry (
823 state
->channel_triggers_ht
, &iter
, channel_trigger_list
, channel_triggers_ht_node
) {
824 struct lttng_trigger_list_element
*element
;
826 cds_list_for_each_entry (element
, &channel_trigger_list
->list
, node
) {
827 const struct lttng_condition
*current_condition
=
828 lttng_trigger_get_const_condition(element
->trigger
);
830 LTTNG_ASSERT(current_condition
);
831 if (!lttng_condition_is_equal(condition
, current_condition
)) {
835 /* Found the trigger, save the channel key. */
836 channel_key
= &channel_trigger_list
->channel_key
;
840 /* The channel key was found stop iteration. */
846 /* No channel found; normal exit. */
847 DBG("No known channel associated with newly subscribed-to condition");
852 /* Fetch channel info for the matching channel. */
853 cds_lfht_lookup(state
->channels_ht
,
854 hash_channel_key(channel_key
),
858 node
= cds_lfht_iter_get_node(&iter
);
860 channel_info
= caa_container_of(node
, struct channel_info
, channels_ht_node
);
862 /* Retrieve the channel's last sample, if it exists. */
863 cds_lfht_lookup(state
->channel_state_ht
,
864 hash_channel_key(channel_key
),
865 match_channel_state_sample
,
868 node
= cds_lfht_iter_get_node(&iter
);
871 caa_container_of(node
, struct channel_state_sample
, channel_state_ht_node
);
873 /* Nothing to evaluate, no sample was ever taken. Normal exit */
874 DBG("No channel sample associated with newly subscribed-to condition");
879 ret
= evaluate_buffer_condition(
880 condition
, evaluation
, state
, nullptr, last_sample
, channel_info
);
882 WARN("Fatal error occurred while evaluating a newly subscribed-to condition");
886 *session_uid
= channel_info
->session_info
->uid
;
887 *session_gid
= channel_info
->session_info
->gid
;
893 static const char *get_condition_session_name(const struct lttng_condition
*condition
)
895 const char *session_name
= nullptr;
896 enum lttng_condition_status status
;
898 switch (lttng_condition_get_type(condition
)) {
899 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
900 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
901 status
= lttng_condition_buffer_usage_get_session_name(condition
, &session_name
);
903 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
904 status
= lttng_condition_session_consumed_size_get_session_name(condition
,
907 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
908 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
910 lttng_condition_session_rotation_get_session_name(condition
, &session_name
);
915 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
916 ERR("Failed to retrieve session rotation condition's session name");
923 static bool evaluate_session_rotation_ongoing_condition(const struct lttng_condition
*condition
924 __attribute__((unused
)),
925 const struct session_state_sample
*sample
)
927 return sample
->rotation
.ongoing
;
930 static bool evaluate_session_consumed_size_condition(const struct lttng_condition
*condition
,
931 const struct session_state_sample
*sample
)
934 const struct lttng_condition_session_consumed_size
*size_condition
=
935 lttng::utils::container_of(condition
,
936 <tng_condition_session_consumed_size::parent
);
938 threshold
= size_condition
->consumed_threshold_bytes
.value
;
939 DBG("Session consumed size condition being evaluated: threshold = %" PRIu64
940 ", current size = %" PRIu64
,
942 sample
->consumed_data_size
);
943 return sample
->consumed_data_size
>= threshold
;
947 * `new_state` can be NULL to indicate that we are not evaluating a
948 * state transition. A client subscribed or a trigger was registered and
949 * we wish to perform an initial evaluation.
951 static int evaluate_session_condition(const struct lttng_condition
*condition
,
952 const struct session_info
*session_info
,
953 const struct session_state_sample
*new_state
,
954 struct lttng_evaluation
**evaluation
)
957 bool previous_result
, newest_result
;
959 switch (lttng_condition_get_type(condition
)) {
960 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
962 previous_result
= evaluate_session_rotation_ongoing_condition(
963 condition
, &session_info
->last_state_sample
);
965 evaluate_session_rotation_ongoing_condition(condition
, new_state
);
967 previous_result
= false;
968 newest_result
= evaluate_session_rotation_ongoing_condition(
969 condition
, &session_info
->last_state_sample
);
972 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
974 previous_result
= evaluate_session_consumed_size_condition(
975 condition
, &session_info
->last_state_sample
);
977 evaluate_session_consumed_size_condition(condition
, new_state
);
979 previous_result
= false;
980 newest_result
= evaluate_session_consumed_size_condition(
981 condition
, &session_info
->last_state_sample
);
984 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
986 * Note that LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED is
987 * evaluated differently to only consider state transitions without regard for the
988 * initial state. This is a deliberate choice as it is unlikely that a user would
989 * expect an action to occur for a rotation that occurred long before the trigger or
990 * subscription occurred.
997 previous_result
= !session_info
->last_state_sample
.rotation
.ongoing
;
998 newest_result
= !new_state
->rotation
.ongoing
;
1005 if (!newest_result
|| (previous_result
== newest_result
)) {
1006 /* Not a state transition, evaluate to false. */
1011 switch (lttng_condition_get_type(condition
)) {
1012 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1014 const auto rotation_id
= new_state
? new_state
->rotation
.id
:
1015 session_info
->last_state_sample
.rotation
.id
;
1017 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(rotation_id
);
1020 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1022 const auto& sample
= new_state
? *new_state
: session_info
->last_state_sample
;
1023 const auto rotation_id
= sample
.rotation
.id
;
1025 /* Callee acquires a reference to location. */
1026 *evaluation
= lttng_evaluation_session_rotation_completed_create(
1027 rotation_id
, sample
.rotation
.location
);
1030 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1032 const auto latest_session_consumed_total
= new_state
?
1033 new_state
->consumed_data_size
:
1034 session_info
->last_state_sample
.consumed_data_size
;
1036 *evaluation
= lttng_evaluation_session_consumed_size_create(
1037 latest_session_consumed_total
);
1046 ERR("Failed to create session condition evaluation: session name = `%s`",
1047 session_info
->name
);
1057 static int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1058 const struct lttng_condition
*condition
,
1059 struct notification_client
*client
,
1060 struct notification_thread_state
*state
)
1063 struct lttng_evaluation
*evaluation
= nullptr;
1064 struct notification_client_list client_list
= {
1065 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1067 .condition
= nullptr,
1068 .triggers_list
= {},
1070 .notification_trigger_clients_ht
= nullptr,
1071 .notification_trigger_clients_ht_node
= {},
1074 struct notification_client_list_element client_list_element
= {};
1075 uid_t object_uid
= 0;
1076 gid_t object_gid
= 0;
1078 LTTNG_ASSERT(trigger
);
1079 LTTNG_ASSERT(condition
);
1080 LTTNG_ASSERT(client
);
1081 LTTNG_ASSERT(state
);
1083 switch (get_condition_binding_object(condition
)) {
1084 case LTTNG_OBJECT_TYPE_SESSION
:
1086 /* Find the session associated with the condition. */
1087 const auto *session_name
= get_condition_session_name(condition
);
1088 auto session_info
= get_session_info_by_name(state
, session_name
);
1089 if (!session_info
) {
1090 /* Not an error, the session doesn't exist yet. */
1091 DBG("Session not found while evaluating session condition for client: session name = `%s`",
1097 object_uid
= session_info
->uid
;
1098 object_gid
= session_info
->gid
;
1100 ret
= evaluate_session_condition(condition
, session_info
, nullptr, &evaluation
);
1101 session_info_put(session_info
);
1104 case LTTNG_OBJECT_TYPE_CHANNEL
:
1105 ret
= evaluate_channel_condition_for_client(
1106 condition
, state
, &evaluation
, &object_uid
, &object_gid
);
1108 case LTTNG_OBJECT_TYPE_NONE
:
1109 DBG("Newly subscribed-to condition not bound to object, nothing to evaluate");
1112 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1122 /* Evaluation yielded nothing. Normal exit. */
1123 DBG("Newly subscribed-to condition evaluated to false, nothing to report to client");
1129 * Create a temporary client list with the client currently
1132 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1133 CDS_INIT_LIST_HEAD(&client_list
.clients_list
);
1135 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1136 client_list_element
.client
= client
;
1137 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1139 /* Send evaluation result to the newly-subscribed client. */
1140 DBG("Newly subscribed-to condition evaluated to true, notifying client");
1141 ret
= send_evaluation_to_clients(
1142 trigger
, evaluation
, &client_list
, state
, object_uid
, object_gid
);
1148 static int notification_thread_client_subscribe(struct notification_client
*client
,
1149 struct lttng_condition
*condition
,
1150 struct notification_thread_state
*state
,
1151 enum lttng_notification_channel_status
*_status
)
1154 struct notification_client_list
*client_list
= nullptr;
1155 struct lttng_condition_list_element
*condition_list_element
= nullptr;
1156 struct notification_client_list_element
*client_list_element
= nullptr;
1157 struct lttng_trigger_ht_element
*trigger_ht_element
;
1158 enum lttng_notification_channel_status status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1161 * Ensure that the client has not already subscribed to this condition
1164 cds_list_for_each_entry (condition_list_element
, &client
->condition_list
, node
) {
1165 if (lttng_condition_is_equal(condition_list_element
->condition
, condition
)) {
1166 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1171 condition_list_element
= zmalloc
<lttng_condition_list_element
>();
1172 if (!condition_list_element
) {
1176 client_list_element
= zmalloc
<notification_client_list_element
>();
1177 if (!client_list_element
) {
1183 * Add the newly-subscribed condition to the client's subscription list.
1185 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1186 condition_list_element
->condition
= condition
;
1187 condition
= nullptr;
1188 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1190 client_list
= get_client_list_from_condition(state
, condition_list_element
->condition
);
1193 * No notification-emiting trigger registered with this
1194 * condition. We don't evaluate the condition right away
1195 * since this trigger is not registered yet.
1197 free(client_list_element
);
1202 * The condition to which the client just subscribed is evaluated
1203 * at this point so that conditions that are already TRUE result
1204 * in a notification being sent out.
1206 * Note the iteration on all triggers which share an identical
1207 * `condition` than the one to which the client is registering. This is
1208 * done to ensure that the client receives a distinct notification for
1209 * all triggers that have a `notify` action that have this condition.
1211 pthread_mutex_lock(&client_list
->lock
);
1212 cds_list_for_each_entry (
1213 trigger_ht_element
, &client_list
->triggers_list
, client_list_trigger_node
) {
1214 if (evaluate_condition_for_client(trigger_ht_element
->trigger
,
1215 condition_list_element
->condition
,
1218 WARN("Evaluation of a condition on client subscription failed, aborting.");
1220 free(client_list_element
);
1221 pthread_mutex_unlock(&client_list
->lock
);
1225 pthread_mutex_unlock(&client_list
->lock
);
1228 * Add the client to the list of clients interested in a given trigger
1229 * if a "notification" trigger with a corresponding condition was
1232 client_list_element
->client
= client
;
1233 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1235 pthread_mutex_lock(&client_list
->lock
);
1236 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1237 pthread_mutex_unlock(&client_list
->lock
);
1243 notification_client_list_put(client_list
);
1245 lttng_condition_destroy(condition
);
1248 free(condition_list_element
);
1249 free(client_list_element
);
1250 lttng_condition_destroy(condition
);
1254 static int notification_thread_client_unsubscribe(struct notification_client
*client
,
1255 struct lttng_condition
*condition
,
1256 struct notification_thread_state
*state
,
1257 enum lttng_notification_channel_status
*_status
)
1259 struct notification_client_list
*client_list
;
1260 struct lttng_condition_list_element
*condition_list_element
, *condition_tmp
;
1261 struct notification_client_list_element
*client_list_element
, *client_tmp
;
1262 bool condition_found
= false;
1263 enum lttng_notification_channel_status status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1265 /* Remove the condition from the client's condition list. */
1266 cds_list_for_each_entry_safe (
1267 condition_list_element
, condition_tmp
, &client
->condition_list
, node
) {
1268 if (!lttng_condition_is_equal(condition_list_element
->condition
, condition
)) {
1272 cds_list_del(&condition_list_element
->node
);
1274 * The caller may be iterating on the client's conditions to
1275 * tear down a client's connection. In this case, the condition
1276 * will be destroyed at the end.
1278 if (condition
!= condition_list_element
->condition
) {
1279 lttng_condition_destroy(condition_list_element
->condition
);
1281 free(condition_list_element
);
1282 condition_found
= true;
1286 if (!condition_found
) {
1287 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1292 * Remove the client from the list of clients interested the trigger
1293 * matching the condition.
1295 client_list
= get_client_list_from_condition(state
, condition
);
1300 pthread_mutex_lock(&client_list
->lock
);
1301 cds_list_for_each_entry_safe (
1302 client_list_element
, client_tmp
, &client_list
->clients_list
, node
) {
1303 if (client_list_element
->client
->id
!= client
->id
) {
1306 cds_list_del(&client_list_element
->node
);
1307 free(client_list_element
);
1310 pthread_mutex_unlock(&client_list
->lock
);
1311 notification_client_list_put(client_list
);
1312 client_list
= nullptr;
1314 lttng_condition_destroy(condition
);
1321 static void free_notification_client_rcu(struct rcu_head
*node
)
1323 free(lttng::utils::container_of(node
, ¬ification_client::rcu_node
));
1326 static void notification_client_destroy(struct notification_client
*client
)
1333 * The client object is not reachable by other threads, no need to lock
1336 if (client
->socket
>= 0) {
1337 (void) lttcomm_close_unix_sock(client
->socket
);
1338 client
->socket
= -1;
1340 client
->communication
.active
= false;
1341 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1342 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1343 pthread_mutex_destroy(&client
->lock
);
1344 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1348 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1351 static struct notification_client
*get_client_from_socket(int socket
,
1352 struct notification_thread_state
*state
)
1354 struct cds_lfht_iter iter
;
1355 struct cds_lfht_node
*node
;
1356 struct notification_client
*client
= nullptr;
1358 ASSERT_RCU_READ_LOCKED();
1360 cds_lfht_lookup(state
->client_socket_ht
,
1361 hash_client_socket(socket
),
1362 match_client_socket
,
1363 (void *) (unsigned long) socket
,
1365 node
= cds_lfht_iter_get_node(&iter
);
1370 client
= caa_container_of(node
, struct notification_client
, client_socket_ht_node
);
1376 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1379 static struct notification_client
*get_client_from_id(notification_client_id id
,
1380 struct notification_thread_state
*state
)
1382 struct cds_lfht_iter iter
;
1383 struct cds_lfht_node
*node
;
1384 struct notification_client
*client
= nullptr;
1386 ASSERT_RCU_READ_LOCKED();
1388 cds_lfht_lookup(state
->client_id_ht
, hash_client_id(id
), match_client_id
, &id
, &iter
);
1389 node
= cds_lfht_iter_get_node(&iter
);
1394 client
= caa_container_of(node
, struct notification_client
, client_id_ht_node
);
1399 static bool buffer_usage_condition_applies_to_channel(const struct lttng_condition
*condition
,
1400 const struct channel_info
*channel_info
)
1402 enum lttng_condition_status status
;
1403 enum lttng_domain_type condition_domain
;
1404 const char *condition_session_name
= nullptr;
1405 const char *condition_channel_name
= nullptr;
1407 status
= lttng_condition_buffer_usage_get_domain_type(condition
, &condition_domain
);
1408 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
1409 if (channel_info
->key
.domain
!= condition_domain
) {
1413 status
= lttng_condition_buffer_usage_get_session_name(condition
, &condition_session_name
);
1414 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1416 status
= lttng_condition_buffer_usage_get_channel_name(condition
, &condition_channel_name
);
1417 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1419 if (strcmp(channel_info
->session_info
->name
, condition_session_name
) != 0) {
1422 if (strcmp(channel_info
->name
, condition_channel_name
) != 0) {
1431 static bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1432 const struct channel_info
*channel_info
)
1434 const struct lttng_condition
*condition
;
1435 bool trigger_applies
;
1437 condition
= lttng_trigger_get_const_condition(trigger
);
1442 switch (lttng_condition_get_type(condition
)) {
1443 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1444 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1446 buffer_usage_condition_applies_to_channel(condition
, channel_info
);
1452 return trigger_applies
;
1457 /* Must be called with RCU read lock held. */
1458 static struct lttng_session_trigger_list
*
1459 get_session_trigger_list(struct notification_thread_state
*state
, const char *session_name
)
1461 struct lttng_session_trigger_list
*list
= nullptr;
1462 struct cds_lfht_node
*node
;
1463 struct cds_lfht_iter iter
;
1465 ASSERT_RCU_READ_LOCKED();
1467 cds_lfht_lookup(state
->session_triggers_ht
,
1468 hash_key_str(session_name
, lttng_ht_seed
),
1469 match_session_trigger_list
,
1472 node
= cds_lfht_iter_get_node(&iter
);
1475 * Not an error, the list of triggers applying to that session
1476 * will be initialized when the session is created.
1478 DBG("No trigger list found for session \"%s\" as it is not yet known to the notification system",
1483 list
= caa_container_of(node
, struct lttng_session_trigger_list
, session_triggers_ht_node
);
1489 * Allocate an empty lttng_session_trigger_list for the session named
1492 static struct lttng_session_trigger_list
*
1493 lttng_session_trigger_list_create(const char *session_name
, struct cds_lfht
*session_triggers_ht
)
1495 struct lttng_session_trigger_list
*list
= nullptr;
1496 char *session_name_copy
= strdup(session_name
);
1498 if (!session_name_copy
) {
1499 PERROR("Failed to allocate session name while building trigger list");
1503 list
= zmalloc
<lttng_session_trigger_list
>();
1505 PERROR("Failed to allocate session trigger list while building trigger list");
1509 list
->session_name
= session_name_copy
;
1510 CDS_INIT_LIST_HEAD(&list
->list
);
1511 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1512 list
->session_triggers_ht
= session_triggers_ht
;
1515 /* Publish the list through the session_triggers_ht. */
1516 cds_lfht_add(session_triggers_ht
,
1517 hash_key_str(session_name
, lttng_ht_seed
),
1518 &list
->session_triggers_ht_node
);
1524 static void free_session_trigger_list_rcu(struct rcu_head
*node
)
1526 struct lttng_session_trigger_list
*list
=
1527 caa_container_of(node
, struct lttng_session_trigger_list
, rcu_node
);
1529 free(list
->session_name
);
1533 static void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1535 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1537 /* Empty the list element by element, and then free the list itself. */
1538 cds_list_for_each_entry_safe (trigger_list_element
, tmp
, &list
->list
, node
) {
1539 cds_list_del(&trigger_list_element
->node
);
1540 free(trigger_list_element
);
1543 /* Unpublish the list from the session_triggers_ht. */
1544 cds_lfht_del(list
->session_triggers_ht
, &list
->session_triggers_ht_node
);
1546 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1549 static int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1550 struct lttng_trigger
*trigger
)
1553 struct lttng_trigger_list_element
*new_element
= zmalloc
<lttng_trigger_list_element
>();
1559 CDS_INIT_LIST_HEAD(&new_element
->node
);
1560 new_element
->trigger
= trigger
;
1561 cds_list_add(&new_element
->node
, &list
->list
);
1566 static bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1567 const char *session_name
)
1569 bool applies
= false;
1570 const struct lttng_condition
*condition
;
1572 condition
= lttng_trigger_get_const_condition(trigger
);
1573 switch (lttng_condition_get_type(condition
)) {
1574 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1575 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1576 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1578 const char *condition_session_name
;
1580 condition_session_name
= get_condition_session_name(condition
);
1581 LTTNG_ASSERT(condition_session_name
);
1582 applies
= !strcmp(condition_session_name
, session_name
);
1593 * Allocate and initialize an lttng_session_trigger_list which contains
1594 * all triggers that apply to the session named 'session_name'.
1596 static struct lttng_session_trigger_list
*
1597 lttng_session_trigger_list_build(const struct notification_thread_state
*state
,
1598 const char *session_name
)
1600 int trigger_count
= 0;
1601 struct lttng_session_trigger_list
*session_trigger_list
= nullptr;
1602 struct lttng_trigger_ht_element
*trigger_ht_element
= nullptr;
1603 struct cds_lfht_iter iter
;
1605 session_trigger_list
=
1606 lttng_session_trigger_list_create(session_name
, state
->session_triggers_ht
);
1608 /* Add all triggers applying to the session named 'session_name'. */
1609 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
1612 if (!trigger_applies_to_session(trigger_ht_element
->trigger
, session_name
)) {
1616 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1617 trigger_ht_element
->trigger
);
1625 DBG("Found %i triggers that apply to newly created session", trigger_count
);
1626 return session_trigger_list
;
1628 lttng_session_trigger_list_destroy(session_trigger_list
);
1632 static struct session_info
*create_and_publish_session_info(struct notification_thread_state
*state
,
1638 struct session_info
*session
= nullptr;
1639 struct lttng_session_trigger_list
*trigger_list
;
1642 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1643 if (!trigger_list
) {
1647 session
= session_info_create(id
, name
, uid
, gid
, trigger_list
, state
->sessions_ht
);
1649 ERR("Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1653 lttng_session_trigger_list_destroy(trigger_list
);
1657 /* Transferred ownership to the new session. */
1658 trigger_list
= nullptr;
1660 if (cds_lfht_add_unique(state
->sessions_ht
,
1661 hash_session_info(session
),
1664 &session
->sessions_ht_node
) != &session
->sessions_ht_node
) {
1665 ERR("Duplicate session found: name = `%s`, id = %" PRIu64
, name
, id
);
1673 session_info_put(session
);
1677 static int handle_notification_thread_command_add_channel(struct notification_thread_state
*state
,
1678 uint64_t session_id
,
1679 const char *channel_name
,
1680 enum lttng_domain_type channel_domain
,
1681 uint64_t channel_key_int
,
1682 uint64_t channel_capacity
,
1683 enum lttng_error_code
*cmd_result
)
1685 struct cds_list_head trigger_list
;
1686 struct channel_info
*new_channel_info
= nullptr;
1687 struct channel_key channel_key
= {
1688 .key
= channel_key_int
,
1689 .domain
= channel_domain
,
1691 struct lttng_channel_trigger_list
*channel_trigger_list
= nullptr;
1692 struct lttng_trigger_ht_element
*trigger_ht_element
= nullptr;
1693 int trigger_count
= 0;
1694 struct cds_lfht_iter iter
;
1695 struct session_info
*session_info
= nullptr;
1697 DBG("Adding channel: channel name = `%s`, session id = %" PRIu64
", channel key = %" PRIu64
1702 lttng_domain_type_str(channel_domain
));
1704 CDS_INIT_LIST_HEAD(&trigger_list
);
1706 session_info
= get_session_info_by_id(state
, session_id
);
1707 if (!session_info
) {
1708 /* Fatal logic error. */
1709 ERR("Failed to find session while adding channel: session id = %" PRIu64
,
1715 channel_info_create(channel_name
, &channel_key
, channel_capacity
, session_info
);
1716 if (!new_channel_info
) {
1721 /* Build a list of all triggers applying to the new channel. */
1722 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
1723 struct lttng_trigger_list_element
*new_element
;
1725 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
, new_channel_info
)) {
1729 new_element
= zmalloc
<lttng_trigger_list_element
>();
1734 CDS_INIT_LIST_HEAD(&new_element
->node
);
1735 new_element
->trigger
= trigger_ht_element
->trigger
;
1736 cds_list_add(&new_element
->node
, &trigger_list
);
1741 DBG("Found %i triggers that apply to newly added channel", trigger_count
);
1742 channel_trigger_list
= zmalloc
<lttng_channel_trigger_list
>();
1743 if (!channel_trigger_list
) {
1746 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1747 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1748 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1749 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1752 /* Add channel to the channel_ht which owns the channel_infos. */
1753 cds_lfht_add(state
->channels_ht
,
1754 hash_channel_key(&new_channel_info
->key
),
1755 &new_channel_info
->channels_ht_node
);
1757 * Add the list of triggers associated with this channel to the
1758 * channel_triggers_ht.
1760 cds_lfht_add(state
->channel_triggers_ht
,
1761 hash_channel_key(&new_channel_info
->key
),
1762 &channel_trigger_list
->channel_triggers_ht_node
);
1764 session_info_put(session_info
);
1765 *cmd_result
= LTTNG_OK
;
1768 channel_info_destroy(new_channel_info
);
1769 session_info_put(session_info
);
1773 static int handle_notification_thread_command_add_session(struct notification_thread_state
*state
,
1774 uint64_t session_id
,
1775 const char *session_name
,
1778 enum lttng_error_code
*cmd_result
)
1782 DBG("Adding session: session name = `%s`, session id = %" PRIu64
1783 ", session uid = %d, session gid = %d",
1789 auto session
= create_and_publish_session_info(
1790 state
, session_id
, session_name
, session_uid
, session_gid
);
1792 PERROR("Failed to add session: session name = `%s`, session id = %" PRIu64
1793 ", session uid = %d, session gid = %d",
1799 *cmd_result
= LTTNG_ERR_NOMEM
;
1804 * Note that the reference to `session` is not released; this reference is
1805 * the "global" reference that is used to allow look-ups. This reference will
1806 * only be released when the session is removed. See
1807 * handle_notification_thread_command_remove_session.
1810 *cmd_result
= LTTNG_OK
;
1816 handle_notification_thread_command_remove_session(struct notification_thread_state
*state
,
1817 uint64_t session_id
,
1818 enum lttng_error_code
*cmd_result
)
1822 DBG("Removing session: session id = %" PRIu64
, session_id
);
1824 auto session
= get_session_info_by_id(state
, session_id
);
1826 ERR("Failed to remove session: session id = %" PRIu64
, session_id
);
1828 *cmd_result
= LTTNG_ERR_NO_SESSION
;
1832 /* Release the reference returned by the look-up, and then release the global reference. */
1833 session_info_put(session
);
1834 session_info_put(session
);
1836 *cmd_result
= LTTNG_OK
;
1841 static void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1843 free(caa_container_of(node
, struct lttng_channel_trigger_list
, rcu_node
));
1846 static void free_channel_state_sample_rcu(struct rcu_head
*node
)
1848 free(caa_container_of(node
, struct channel_state_sample
, rcu_node
));
1852 handle_notification_thread_command_remove_channel(struct notification_thread_state
*state
,
1853 uint64_t channel_key
,
1854 enum lttng_domain_type domain
,
1855 enum lttng_error_code
*cmd_result
)
1857 struct cds_lfht_node
*node
;
1858 struct cds_lfht_iter iter
;
1859 struct lttng_channel_trigger_list
*trigger_list
;
1860 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1861 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1862 struct channel_info
*channel_info
;
1864 DBG("Removing channel key = %" PRIu64
" in %s domain",
1866 lttng_domain_type_str(domain
));
1870 cds_lfht_lookup(state
->channel_triggers_ht
,
1871 hash_channel_key(&key
),
1872 match_channel_trigger_list
,
1875 node
= cds_lfht_iter_get_node(&iter
);
1877 * There is a severe internal error if we are being asked to remove a
1878 * channel that doesn't exist.
1881 ERR("Channel being removed is unknown to the notification thread");
1885 /* Free the list of triggers associated with this channel. */
1887 caa_container_of(node
, struct lttng_channel_trigger_list
, channel_triggers_ht_node
);
1888 cds_list_for_each_entry_safe (trigger_list_element
, tmp
, &trigger_list
->list
, node
) {
1889 cds_list_del(&trigger_list_element
->node
);
1890 free(trigger_list_element
);
1892 cds_lfht_del(state
->channel_triggers_ht
, node
);
1893 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1895 /* Free sampled channel state. */
1896 cds_lfht_lookup(state
->channel_state_ht
,
1897 hash_channel_key(&key
),
1898 match_channel_state_sample
,
1901 node
= cds_lfht_iter_get_node(&iter
);
1903 * This is expected to be NULL if the channel is destroyed before we
1904 * received a sample.
1907 struct channel_state_sample
*sample
=
1908 caa_container_of(node
, struct channel_state_sample
, channel_state_ht_node
);
1910 cds_lfht_del(state
->channel_state_ht
, node
);
1911 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1914 /* Remove the channel from the channels_ht and free it. */
1916 state
->channels_ht
, hash_channel_key(&key
), match_channel_info
, &key
, &iter
);
1917 node
= cds_lfht_iter_get_node(&iter
);
1919 channel_info
= caa_container_of(node
, struct channel_info
, channels_ht_node
);
1920 cds_lfht_del(state
->channels_ht
, node
);
1921 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
;
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
;
2050 handle_notification_thread_command_add_tracer_event_source(struct notification_thread_state
*state
,
2051 int tracer_event_source_fd
,
2052 enum lttng_domain_type domain_type
,
2053 enum lttng_error_code
*_cmd_result
)
2056 enum lttng_error_code cmd_result
= LTTNG_OK
;
2057 struct notification_event_tracer_event_source_element
*element
= nullptr;
2059 element
= zmalloc
<notification_event_tracer_event_source_element
>();
2061 cmd_result
= LTTNG_ERR_NOMEM
;
2066 element
->fd
= tracer_event_source_fd
;
2067 element
->domain
= domain_type
;
2069 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2071 DBG3("Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2072 tracer_event_source_fd
,
2073 lttng_domain_type_str(domain_type
));
2075 /* Adding the read side pipe to the event poll. */
2076 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLPRI
| LPOLLIN
);
2078 ERR("Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2079 tracer_event_source_fd
,
2080 lttng_domain_type_str(element
->domain
));
2081 cds_list_del(&element
->node
);
2086 element
->is_fd_in_poll_set
= true;
2089 *_cmd_result
= cmd_result
;
2093 static int drain_event_notifier_notification_pipe(struct notification_thread_state
*state
,
2095 enum lttng_domain_type domain
)
2097 struct lttng_poll_event events
= {};
2100 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2102 ERR("Error creating lttng_poll_event");
2106 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2108 ERR("Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2115 * Continue to consume notifications as long as there are new
2116 * ones coming in. The tracer has been asked to stop producing
2119 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2120 * monitored (on Linux, at least) and will be returned when
2121 * the pipe is closed but empty.
2123 ret
= lttng_poll_wait_interruptible(&events
, 0);
2124 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2125 /* No more notification to be read on this pipe. */
2128 } else if (ret
< 0) {
2129 PERROR("Failed on lttng_poll_wait_interruptible() call");
2134 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2136 ERR("Error consuming an event notifier notification from pipe: fd = %d",
2141 lttng_poll_clean(&events
);
2145 static struct notification_event_tracer_event_source_element
*
2146 find_tracer_event_source_element(struct notification_thread_state
*state
,
2147 int tracer_event_source_fd
)
2149 struct notification_event_tracer_event_source_element
*source_element
;
2151 cds_list_for_each_entry (source_element
, &state
->tracer_event_sources_list
, node
) {
2152 if (source_element
->fd
== tracer_event_source_fd
) {
2157 source_element
= nullptr;
2159 return source_element
;
2162 static int remove_tracer_event_source_from_pollset(
2163 struct notification_thread_state
*state
,
2164 struct notification_event_tracer_event_source_element
*source_element
)
2168 LTTNG_ASSERT(source_element
->is_fd_in_poll_set
);
2170 DBG3("Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2172 lttng_domain_type_str(source_element
->domain
));
2174 /* Removing the fd from the event poll set. */
2175 ret
= lttng_poll_del(&state
->events
, source_element
->fd
);
2177 ERR("Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2179 lttng_domain_type_str(source_element
->domain
));
2184 source_element
->is_fd_in_poll_set
= false;
2187 * Force the notification thread to restart the poll() loop to ensure
2188 * that any events from the removed fd are removed.
2190 state
->restart_poll
= true;
2192 ret
= drain_event_notifier_notification_pipe(
2193 state
, source_element
->fd
, source_element
->domain
);
2195 ERR("Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2197 lttng_domain_type_str(source_element
->domain
));
2206 int handle_notification_thread_tracer_event_source_died(struct notification_thread_state
*state
,
2207 int tracer_event_source_fd
)
2210 struct notification_event_tracer_event_source_element
*source_element
;
2212 source_element
= find_tracer_event_source_element(state
, tracer_event_source_fd
);
2214 LTTNG_ASSERT(source_element
);
2216 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2218 ERR("Failed to remove dead tracer event source from poll set");
2224 static int handle_notification_thread_command_remove_tracer_event_source(
2225 struct notification_thread_state
*state
,
2226 int tracer_event_source_fd
,
2227 enum lttng_error_code
*_cmd_result
)
2230 enum lttng_error_code cmd_result
= LTTNG_OK
;
2231 struct notification_event_tracer_event_source_element
*source_element
= nullptr;
2233 source_element
= find_tracer_event_source_element(state
, tracer_event_source_fd
);
2235 LTTNG_ASSERT(source_element
);
2237 /* Remove the tracer source from the list. */
2238 cds_list_del(&source_element
->node
);
2240 if (!source_element
->is_fd_in_poll_set
) {
2241 /* Skip the poll set removal. */
2245 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2247 ERR("Failed to remove tracer event source from poll set");
2248 cmd_result
= LTTNG_ERR_FATAL
;
2252 free(source_element
);
2253 *_cmd_result
= cmd_result
;
2258 handle_notification_thread_command_list_triggers(struct notification_thread_handle
*handle
2259 __attribute__((unused
)),
2260 struct notification_thread_state
*state
,
2262 struct lttng_triggers
**triggers
,
2263 enum lttng_error_code
*_cmd_result
)
2266 enum lttng_error_code cmd_result
= LTTNG_OK
;
2267 struct cds_lfht_iter iter
;
2268 struct lttng_trigger_ht_element
*trigger_ht_element
;
2269 struct lttng_triggers
*local_triggers
= nullptr;
2270 const struct lttng_credentials
*creds
;
2274 local_triggers
= lttng_triggers_create();
2275 if (!local_triggers
) {
2276 /* Not a fatal error. */
2277 cmd_result
= LTTNG_ERR_NOMEM
;
2281 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2283 * Only return the triggers to which the client has access.
2284 * The root user has visibility over all triggers.
2286 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2287 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2291 ret
= lttng_triggers_add(local_triggers
, trigger_ht_element
->trigger
);
2293 /* Not a fatal error. */
2295 cmd_result
= LTTNG_ERR_NOMEM
;
2300 /* Transferring ownership to the caller. */
2301 *triggers
= local_triggers
;
2302 local_triggers
= nullptr;
2306 lttng_triggers_destroy(local_triggers
);
2307 *_cmd_result
= cmd_result
;
2311 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
2312 const char **trigger_name
,
2313 uid_t
*trigger_owner_uid
)
2315 enum lttng_trigger_status trigger_status
;
2317 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
2318 switch (trigger_status
) {
2319 case LTTNG_TRIGGER_STATUS_OK
:
2321 case LTTNG_TRIGGER_STATUS_UNSET
:
2322 *trigger_name
= "(anonymous)";
2328 trigger_status
= lttng_trigger_get_owner_uid(trigger
, trigger_owner_uid
);
2329 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2332 static int handle_notification_thread_command_get_trigger(struct notification_thread_state
*state
,
2333 const struct lttng_trigger
*trigger
,
2334 struct lttng_trigger
**registered_trigger
,
2335 enum lttng_error_code
*_cmd_result
)
2338 struct cds_lfht_iter iter
;
2339 struct lttng_trigger_ht_element
*trigger_ht_element
;
2340 enum lttng_error_code cmd_result
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2341 const char *trigger_name
;
2342 uid_t trigger_owner_uid
;
2346 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2347 if (lttng_trigger_is_equal(trigger
, trigger_ht_element
->trigger
)) {
2348 /* Take one reference on the return trigger. */
2349 *registered_trigger
= trigger_ht_element
->trigger
;
2350 lttng_trigger_get(*registered_trigger
);
2352 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
);
2367 *_cmd_result
= cmd_result
;
2371 static bool condition_is_supported(struct lttng_condition
*condition
)
2375 switch (lttng_condition_get_type(condition
)) {
2376 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2377 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2380 enum lttng_domain_type domain
;
2382 ret
= lttng_condition_buffer_usage_get_domain_type(condition
, &domain
);
2383 LTTNG_ASSERT(ret
== 0);
2385 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2386 is_supported
= true;
2391 * Older kernel tracers don't expose the API to monitor their
2392 * buffers. Therefore, we reject triggers that require that
2393 * mechanism to be available to be evaluated.
2395 * Assume unsupported on error.
2397 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2400 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
2402 const struct lttng_event_rule
*event_rule
;
2403 enum lttng_domain_type domain
;
2404 const enum lttng_condition_status status
=
2405 lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2407 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
2409 domain
= lttng_event_rule_get_domain_type(event_rule
);
2410 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2411 is_supported
= true;
2416 * Older kernel tracers can't emit notification. Therefore, we
2417 * reject triggers that require that mechanism to be available
2420 * Assume unsupported on error.
2422 is_supported
= kernel_supports_event_notifiers() == 1;
2426 is_supported
= true;
2429 return is_supported
;
2432 /* Must be called with RCU read lock held. */
2433 static int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2434 struct notification_thread_state
*state
)
2437 const struct lttng_condition
*condition
;
2438 const char *session_name
;
2439 struct lttng_session_trigger_list
*trigger_list
;
2441 ASSERT_RCU_READ_LOCKED();
2443 condition
= lttng_trigger_get_const_condition(trigger
);
2444 session_name
= get_condition_session_name(condition
);
2446 trigger_list
= get_session_trigger_list(state
, session_name
);
2447 if (!trigger_list
) {
2448 DBG("Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2453 DBG("Newly registered trigger bound to session \"%s\"", session_name
);
2454 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2459 /* Must be called with RCU read lock held. */
2460 static int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2461 struct notification_thread_state
*state
)
2464 struct cds_lfht_node
*node
;
2465 struct cds_lfht_iter iter
;
2466 struct channel_info
*channel
;
2468 ASSERT_RCU_READ_LOCKED();
2470 cds_lfht_for_each_entry (state
->channels_ht
, &iter
, channel
, channels_ht_node
) {
2471 struct lttng_trigger_list_element
*trigger_list_element
;
2472 struct lttng_channel_trigger_list
*trigger_list
;
2473 struct cds_lfht_iter lookup_iter
;
2475 if (!trigger_applies_to_channel(trigger
, channel
)) {
2479 cds_lfht_lookup(state
->channel_triggers_ht
,
2480 hash_channel_key(&channel
->key
),
2481 match_channel_trigger_list
,
2484 node
= cds_lfht_iter_get_node(&lookup_iter
);
2486 trigger_list
= caa_container_of(
2487 node
, struct lttng_channel_trigger_list
, channel_triggers_ht_node
);
2489 trigger_list_element
= zmalloc
<lttng_trigger_list_element
>();
2490 if (!trigger_list_element
) {
2494 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2495 trigger_list_element
->trigger
= trigger
;
2496 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2497 DBG("Newly registered trigger bound to channel \"%s\"", channel
->name
);
2503 static bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2505 bool is_notify
= false;
2506 unsigned int i
, count
;
2507 enum lttng_action_status action_status
;
2508 const struct lttng_action
*action
= lttng_trigger_get_const_action(trigger
);
2509 enum lttng_action_type action_type
;
2511 LTTNG_ASSERT(action
);
2512 action_type
= lttng_action_get_type(action
);
2513 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2516 } else if (action_type
!= LTTNG_ACTION_TYPE_LIST
) {
2520 action_status
= lttng_action_list_get_count(action
, &count
);
2521 LTTNG_ASSERT(action_status
== LTTNG_ACTION_STATUS_OK
);
2523 for (i
= 0; i
< count
; i
++) {
2524 const struct lttng_action
*inner_action
= lttng_action_list_get_at_index(action
, i
);
2526 action_type
= lttng_action_get_type(inner_action
);
2527 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2537 static bool trigger_name_taken(struct notification_thread_state
*state
,
2538 const struct lttng_trigger
*trigger
)
2540 struct cds_lfht_iter iter
;
2543 * No duplicata is allowed in the triggers_by_name_uid_ht.
2544 * The match is done against the trigger name and uid.
2546 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2547 hash_trigger_by_name_uid(trigger
),
2548 match_trigger_by_name_uid
,
2551 return !!cds_lfht_iter_get_node(&iter
);
2554 static enum lttng_error_code
generate_trigger_name(struct notification_thread_state
*state
,
2555 struct lttng_trigger
*trigger
,
2558 enum lttng_error_code ret_code
= LTTNG_OK
;
2560 enum lttng_trigger_status status
;
2564 lttng_trigger_generate_name(trigger
, state
->trigger_id
.name_offset
++);
2566 /* The only reason this can fail right now. */
2567 ret_code
= LTTNG_ERR_NOMEM
;
2571 status
= lttng_trigger_get_name(trigger
, name
);
2572 LTTNG_ASSERT(status
== LTTNG_TRIGGER_STATUS_OK
);
2574 taken
= trigger_name_taken(state
, trigger
);
2575 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2581 notif_thread_state_remove_trigger_ht_elem(struct notification_thread_state
*state
,
2582 struct lttng_trigger_ht_element
*trigger_ht_element
)
2584 LTTNG_ASSERT(state
);
2585 LTTNG_ASSERT(trigger_ht_element
);
2587 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2588 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2591 static enum lttng_error_code
setup_tracer_notifier(struct notification_thread_state
*state
,
2592 struct lttng_trigger
*trigger
)
2594 enum lttng_error_code ret
;
2595 enum event_notifier_error_accounting_status error_accounting_status
;
2596 struct cds_lfht_node
*node
;
2597 uint64_t error_counter_index
= 0;
2598 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2599 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= nullptr;
2601 trigger_tokens_ht_element
= zmalloc
<notification_trigger_tokens_ht_element
>();
2602 if (!trigger_tokens_ht_element
) {
2603 ret
= LTTNG_ERR_NOMEM
;
2607 /* Add trigger token to the trigger_tokens_ht. */
2608 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2609 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2610 trigger_tokens_ht_element
->trigger
= trigger
;
2612 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2613 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2614 match_trigger_token
,
2615 &trigger_tokens_ht_element
->token
,
2616 &trigger_tokens_ht_element
->node
);
2617 if (node
!= &trigger_tokens_ht_element
->node
) {
2618 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2619 goto error_free_ht_element
;
2622 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2623 trigger
, &error_counter_index
);
2624 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2625 if (error_accounting_status
==
2626 EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2627 DBG("Trigger list error accounting counter full.");
2628 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2630 ERR("Error registering trigger for error accounting");
2631 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2634 goto error_remove_ht_element
;
2637 lttng_condition_event_rule_matches_set_error_counter_index(condition
, error_counter_index
);
2642 error_remove_ht_element
:
2643 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2644 error_free_ht_element
:
2645 free(trigger_tokens_ht_element
);
2651 * FIXME A client's credentials are not checked when registering a trigger.
2653 * The effects of this are benign since:
2654 * - The client will succeed in registering the trigger, as it is valid,
2655 * - The trigger will, internally, be bound to the channel/session,
2656 * - The notifications will not be sent since the client's credentials
2657 * are checked against the channel at that moment.
2659 * If this function returns a non-zero value, it means something is
2660 * fundamentally broken and the whole subsystem/thread will be torn down.
2662 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2666 handle_notification_thread_command_register_trigger(struct notification_thread_state
*state
,
2667 struct lttng_trigger
*trigger
,
2668 bool is_trigger_anonymous
,
2669 enum lttng_error_code
*cmd_result
)
2672 struct lttng_condition
*condition
;
2673 struct notification_client_list
*client_list
= nullptr;
2674 struct lttng_trigger_ht_element
*trigger_ht_element
= nullptr;
2675 struct cds_lfht_node
*node
;
2676 const char *trigger_name
;
2677 bool free_trigger
= true;
2678 struct lttng_evaluation
*evaluation
= nullptr;
2679 struct lttng_credentials object_creds
;
2682 enum action_executor_status executor_status
;
2683 const uint64_t trigger_tracer_token
= state
->trigger_id
.next_tracer_token
++;
2687 /* Set the trigger's tracer token. */
2688 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2690 if (!is_trigger_anonymous
) {
2691 if (lttng_trigger_get_name(trigger
, &trigger_name
) == LTTNG_TRIGGER_STATUS_UNSET
) {
2692 const enum lttng_error_code ret_code
=
2693 generate_trigger_name(state
, trigger
, &trigger_name
);
2695 if (ret_code
!= LTTNG_OK
) {
2698 *cmd_result
= ret_code
;
2701 } else if (trigger_name_taken(state
, trigger
)) {
2702 /* Not a fatal error. */
2703 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2708 trigger_name
= "(anonymous)";
2711 condition
= lttng_trigger_get_condition(trigger
);
2712 LTTNG_ASSERT(condition
);
2714 /* Some conditions require tracers to implement a minimal ABI version. */
2715 if (!condition_is_supported(condition
)) {
2716 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2720 trigger_ht_element
= zmalloc
<lttng_trigger_ht_element
>();
2721 if (!trigger_ht_element
) {
2726 /* Add trigger to the trigger_ht. */
2727 cds_lfht_node_init(&trigger_ht_element
->node
);
2728 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2729 trigger_ht_element
->trigger
= trigger
;
2731 node
= cds_lfht_add_unique(state
->triggers_ht
,
2732 lttng_condition_hash(condition
),
2735 &trigger_ht_element
->node
);
2736 if (node
!= &trigger_ht_element
->node
) {
2737 /* Not a fatal error, simply report it to the client. */
2738 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2739 goto error_free_ht_element
;
2742 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2743 hash_trigger_by_name_uid(trigger
),
2744 match_trigger_by_name_uid
,
2746 &trigger_ht_element
->node_by_name_uid
);
2747 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2748 /* Internal error: add to triggers_ht should have failed. */
2750 goto error_free_ht_element
;
2753 /* From this point consider the trigger registered. */
2754 lttng_trigger_set_as_registered(trigger
);
2757 * Some triggers might need a tracer notifier depending on its
2758 * condition and actions.
2760 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2761 enum lttng_error_code error_code
;
2763 error_code
= setup_tracer_notifier(state
, trigger
);
2764 if (error_code
!= LTTNG_OK
) {
2765 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
2766 if (error_code
== LTTNG_ERR_NOMEM
) {
2769 *cmd_result
= error_code
;
2773 goto error_free_ht_element
;
2778 * The rest only applies to triggers that have a "notify" action.
2779 * It is not skipped as this is the only action type currently
2782 if (is_trigger_action_notify(trigger
)) {
2784 * Find or create the client list of this condition. It may
2785 * already be present if another trigger is already registered
2786 * with the same condition.
2788 client_list
= get_client_list_from_condition(state
, condition
);
2791 * No client list for this condition yet. We create new
2792 * one and build it up.
2794 client_list
= notification_client_list_create(state
, condition
);
2796 ERR("Error creating notification client list for trigger %s",
2799 goto error_free_ht_element
;
2803 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2805 pthread_mutex_lock(&client_list
->lock
);
2806 cds_list_add(&trigger_ht_element
->client_list_trigger_node
,
2807 &client_list
->triggers_list
);
2808 pthread_mutex_unlock(&client_list
->lock
);
2812 * Ownership of the trigger and of its wrapper was transfered to
2813 * the triggers_ht. Same for token ht element if necessary.
2815 trigger_ht_element
= nullptr;
2816 free_trigger
= false;
2818 switch (get_condition_binding_object(condition
)) {
2819 case LTTNG_OBJECT_TYPE_SESSION
:
2820 /* Add the trigger to the list if it matches a known session. */
2821 ret
= bind_trigger_to_matching_session(trigger
, state
);
2823 goto error_free_ht_element
;
2826 case LTTNG_OBJECT_TYPE_CHANNEL
:
2828 * Add the trigger to list of triggers bound to the channels
2831 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2833 goto error_free_ht_element
;
2836 case LTTNG_OBJECT_TYPE_NONE
:
2839 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2841 goto error_free_ht_element
;
2845 * The new trigger's condition must be evaluated against the current
2848 * In the case of `notify` action, nothing preventing clients from
2849 * subscribing to a condition before the corresponding trigger is
2850 * registered, we have to evaluate this new condition right away.
2852 * At some point, we were waiting for the next "evaluation" (e.g. on
2853 * reception of a channel sample) to evaluate this new condition, but
2856 * The reason it was broken is that waiting for the next sample
2857 * does not allow us to properly handle transitions for edge-triggered
2860 * Consider this example: when we handle a new channel sample, we
2861 * evaluate each conditions twice: once with the previous state, and
2862 * again with the newest state. We then use those two results to
2863 * determine whether a state change happened: a condition was false and
2864 * became true. If a state change happened, we have to notify clients.
2866 * Now, if a client subscribes to a given notification and registers
2867 * a trigger *after* that subscription, we have to make sure the
2868 * condition is evaluated at this point while considering only the
2869 * current state. Otherwise, the next evaluation cycle may only see
2870 * that the evaluations remain the same (true for samples n-1 and n) and
2871 * the client will never know that the condition has been met.
2873 switch (get_condition_binding_object(condition
)) {
2874 case LTTNG_OBJECT_TYPE_SESSION
:
2876 /* Find the session associated with the condition. */
2877 const auto *session_name
= get_condition_session_name(condition
);
2878 auto session_info
= get_session_info_by_name(state
, session_name
);
2879 if (!session_info
) {
2880 /* Not an error, the session doesn't exist yet. */
2881 DBG("Session not found while evaluating session condition during registration of trigger: session name = `%s`",
2887 LTTNG_OPTIONAL_SET(&object_creds
.uid
, session_info
->uid
);
2888 LTTNG_OPTIONAL_SET(&object_creds
.gid
, session_info
->gid
);
2890 ret
= evaluate_session_condition(condition
, session_info
, nullptr, &evaluation
);
2891 session_info_put(session_info
);
2894 case LTTNG_OBJECT_TYPE_CHANNEL
:
2895 ret
= evaluate_channel_condition_for_client(
2896 condition
, state
, &evaluation
, &object_uid
, &object_gid
);
2897 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2898 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2900 case LTTNG_OBJECT_TYPE_NONE
:
2903 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2911 goto error_free_ht_element
;
2914 DBG("Newly registered trigger's condition evaluated to %s", evaluation
? "true" : "false");
2916 /* Evaluation yielded nothing. Normal exit. */
2922 * Ownership of `evaluation` transferred to the action executor
2923 * no matter the result.
2925 executor_status
= action_executor_enqueue_trigger(
2926 state
->executor
, trigger
, evaluation
, &object_creds
, client_list
);
2927 evaluation
= nullptr;
2928 switch (executor_status
) {
2929 case ACTION_EXECUTOR_STATUS_OK
:
2931 case ACTION_EXECUTOR_STATUS_ERROR
:
2932 case ACTION_EXECUTOR_STATUS_INVALID
:
2934 * TODO Add trigger identification (name/id) when
2935 * it is added to the API.
2937 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2939 goto error_free_ht_element
;
2940 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2942 * TODO Add trigger identification (name/id) when
2943 * it is added to the API.
2945 * Not a fatal error.
2947 WARN("No space left when enqueuing action associated to newly registered trigger");
2955 *cmd_result
= LTTNG_OK
;
2956 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2958 trigger_tracer_token
);
2961 error_free_ht_element
:
2962 if (trigger_ht_element
) {
2963 /* Delayed removal due to RCU constraint on delete. */
2964 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2969 * Other objects might have a reference to the trigger, mark it
2972 lttng_trigger_set_as_unregistered(trigger
);
2973 lttng_trigger_destroy(trigger
);
2980 static void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2982 free(caa_container_of(node
, struct lttng_trigger_ht_element
, rcu_node
));
2985 static void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2987 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
, rcu_node
));
2990 static void teardown_tracer_notifier(struct notification_thread_state
*state
,
2991 const struct lttng_trigger
*trigger
)
2993 struct cds_lfht_iter iter
;
2994 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2996 cds_lfht_for_each_entry (state
->trigger_tokens_ht
, &iter
, trigger_tokens_ht_element
, node
) {
2997 if (!lttng_trigger_is_equal(trigger
, trigger_tokens_ht_element
->trigger
)) {
3001 event_notifier_error_accounting_unregister_event_notifier(
3002 trigger_tokens_ht_element
->trigger
);
3004 /* TODO talk to all app and remove it */
3005 DBG("Removed trigger from tokens_ht");
3006 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
3008 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
3009 free_notification_trigger_tokens_ht_element_rcu
);
3016 remove_trigger_from_session_trigger_list(struct lttng_session_trigger_list
*trigger_list
,
3017 const struct lttng_trigger
*trigger
)
3020 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3022 cds_list_for_each_entry_safe (trigger_element
, tmp
, &trigger_list
->list
, node
) {
3023 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3027 DBG("Removed trigger from session_triggers_ht");
3028 cds_list_del(&trigger_element
->node
);
3029 free(trigger_element
);
3030 /* A trigger can only appear once per session. */
3036 ERR("Failed to find trigger associated with session: session name = `%s`",
3037 trigger_list
->session_name
);
3040 LTTNG_ASSERT(found
);
3044 handle_notification_thread_command_unregister_trigger(struct notification_thread_state
*state
,
3045 const struct lttng_trigger
*trigger
,
3046 enum lttng_error_code
*_cmd_reply
)
3048 struct cds_lfht_iter iter
;
3049 struct cds_lfht_node
*triggers_ht_node
;
3050 struct notification_client_list
*client_list
;
3051 struct lttng_trigger_ht_element
*trigger_ht_element
= nullptr;
3052 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
3053 enum lttng_error_code cmd_reply
;
3058 state
->triggers_ht
, lttng_condition_hash(condition
), match_trigger
, trigger
, &iter
);
3059 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3060 if (!triggers_ht_node
) {
3061 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3064 cmd_reply
= LTTNG_OK
;
3067 trigger_ht_element
=
3068 caa_container_of(triggers_ht_node
, struct lttng_trigger_ht_element
, node
);
3070 switch (get_condition_binding_object(condition
)) {
3071 case LTTNG_OBJECT_TYPE_CHANNEL
:
3073 struct lttng_channel_trigger_list
*trigger_list
;
3076 * Remove trigger from channel_triggers_ht.
3078 * Note that multiple channels may have matched the trigger's
3079 * condition (e.g. all instances of a given channel in per-pid buffering
3082 * Iterate on all lists since we don't know the target channels' keys.
3084 cds_lfht_for_each_entry (
3085 state
->channel_triggers_ht
, &iter
, trigger_list
, channel_triggers_ht_node
) {
3086 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3088 cds_list_for_each_entry_safe (
3089 trigger_element
, tmp
, &trigger_list
->list
, node
) {
3090 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3094 DBG("Removed trigger from channel_triggers_ht");
3095 cds_list_del(&trigger_element
->node
);
3096 free(trigger_element
);
3097 /* A trigger can only appear once per channel */
3103 case LTTNG_OBJECT_TYPE_SESSION
:
3106 get_session_info_by_name(state
, get_condition_session_name(condition
));
3108 /* Session doesn't exist, no trigger to remove. */
3113 auto session_trigger_list
= get_session_trigger_list(state
, session
->name
);
3114 remove_trigger_from_session_trigger_list(session_trigger_list
, trigger
);
3115 session_info_put(session
);
3117 case LTTNG_OBJECT_TYPE_NONE
:
3123 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3124 teardown_tracer_notifier(state
, trigger
);
3127 if (is_trigger_action_notify(trigger
)) {
3129 * Remove and release the client list from
3130 * notification_trigger_clients_ht.
3132 client_list
= get_client_list_from_condition(state
, condition
);
3133 LTTNG_ASSERT(client_list
);
3135 pthread_mutex_lock(&client_list
->lock
);
3136 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3137 pthread_mutex_unlock(&client_list
->lock
);
3139 /* Put new reference and the hashtable's reference. */
3140 notification_client_list_put(client_list
);
3141 notification_client_list_put(client_list
);
3142 client_list
= nullptr;
3145 /* Remove trigger from triggers_ht. */
3146 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3148 /* Release the ownership of the trigger. */
3149 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3150 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3154 *_cmd_reply
= cmd_reply
;
3159 static int pop_cmd_queue(struct notification_thread_handle
*handle
,
3160 struct notification_thread_command
**cmd
)
3165 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3166 ret
= lttng_read(handle
->cmd_queue
.event_fd
, &counter
, sizeof(counter
));
3167 if (ret
!= sizeof(counter
)) {
3172 *cmd
= cds_list_first_entry(
3173 &handle
->cmd_queue
.list
, struct notification_thread_command
, cmd_list_node
);
3174 cds_list_del(&((*cmd
)->cmd_list_node
));
3178 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3182 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3183 int handle_notification_thread_command(struct notification_thread_handle
*handle
,
3184 struct notification_thread_state
*state
)
3187 struct notification_thread_command
*cmd
;
3189 ret
= pop_cmd_queue(handle
, &cmd
);
3194 DBG("Received `%s` command", notification_command_type_str(cmd
->type
));
3195 switch (cmd
->type
) {
3196 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3197 ret
= handle_notification_thread_command_register_trigger(
3199 cmd
->parameters
.register_trigger
.trigger
,
3200 cmd
->parameters
.register_trigger
.is_trigger_anonymous
,
3203 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3204 ret
= handle_notification_thread_command_unregister_trigger(
3205 state
, cmd
->parameters
.unregister_trigger
.trigger
, &cmd
->reply_code
);
3207 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3208 ret
= handle_notification_thread_command_add_channel(
3210 cmd
->parameters
.add_channel
.session
.id
,
3211 cmd
->parameters
.add_channel
.channel
.name
,
3212 cmd
->parameters
.add_channel
.channel
.domain
,
3213 cmd
->parameters
.add_channel
.channel
.key
,
3214 cmd
->parameters
.add_channel
.channel
.capacity
,
3217 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3218 ret
= handle_notification_thread_command_remove_channel(
3220 cmd
->parameters
.remove_channel
.key
,
3221 cmd
->parameters
.remove_channel
.domain
,
3224 case NOTIFICATION_COMMAND_TYPE_ADD_SESSION
:
3225 ret
= handle_notification_thread_command_add_session(
3227 cmd
->parameters
.add_session
.session_id
,
3228 cmd
->parameters
.add_session
.session_name
,
3229 cmd
->parameters
.add_session
.session_uid
,
3230 cmd
->parameters
.add_session
.session_gid
,
3233 case NOTIFICATION_COMMAND_TYPE_REMOVE_SESSION
:
3234 ret
= handle_notification_thread_command_remove_session(
3235 state
, cmd
->parameters
.remove_session
.session_id
, &cmd
->reply_code
);
3237 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3238 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3239 ret
= handle_notification_thread_command_session_rotation(
3242 cmd
->parameters
.session_rotation
.session_id
,
3243 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3244 cmd
->parameters
.session_rotation
.location
,
3247 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3248 ret
= handle_notification_thread_command_add_tracer_event_source(
3250 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3251 cmd
->parameters
.tracer_event_source
.domain
,
3254 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3255 ret
= handle_notification_thread_command_remove_tracer_event_source(
3257 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3260 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3262 struct lttng_triggers
*triggers
= nullptr;
3264 ret
= handle_notification_thread_command_list_triggers(
3267 cmd
->parameters
.list_triggers
.uid
,
3270 cmd
->reply
.list_triggers
.triggers
= triggers
;
3274 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3275 cmd
->reply_code
= LTTNG_OK
;
3278 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3280 struct lttng_trigger
*trigger
= nullptr;
3282 ret
= handle_notification_thread_command_get_trigger(
3283 state
, cmd
->parameters
.get_trigger
.trigger
, &trigger
, &cmd
->reply_code
);
3284 cmd
->reply
.get_trigger
.trigger
= trigger
;
3287 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3289 const enum client_transmission_status client_status
=
3290 cmd
->parameters
.client_communication_update
.status
;
3291 const notification_client_id client_id
=
3292 cmd
->parameters
.client_communication_update
.id
;
3293 struct notification_client
*client
;
3296 client
= get_client_from_id(client_id
, state
);
3300 * Client error was probably already picked-up by the
3301 * notification thread or it has disconnected
3302 * gracefully while this command was queued.
3304 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3308 ret
= client_handle_transmission_status(client
, client_status
, state
);
3314 ERR("Unknown internal command received");
3322 if (cmd
->is_async
) {
3326 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3330 /* Wake-up and return a fatal error to the calling thread. */
3331 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3332 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3334 /* Indicate a fatal error to the caller. */
3338 static int socket_set_non_blocking(int socket
)
3342 /* Set the pipe as non-blocking. */
3343 ret
= fcntl(socket
, F_GETFL
, 0);
3345 PERROR("fcntl get socket flags");
3350 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3352 PERROR("fcntl set O_NONBLOCK socket flag");
3355 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3360 static int client_reset_inbound_state(struct notification_client
*client
)
3364 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3366 client
->communication
.inbound
.bytes_to_receive
=
3367 sizeof(struct lttng_notification_channel_message
);
3368 client
->communication
.inbound
.msg_type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3369 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3370 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3371 ret
= lttng_dynamic_buffer_set_size(&client
->communication
.inbound
.payload
.buffer
,
3372 client
->communication
.inbound
.bytes_to_receive
);
3377 int handle_notification_thread_client_connect(struct notification_thread_state
*state
)
3380 struct notification_client
*client
;
3382 DBG("Handling new notification channel client connection");
3384 client
= zmalloc
<notification_client
>();
3391 pthread_mutex_init(&client
->lock
, nullptr);
3392 client
->id
= state
->next_notification_client_id
++;
3393 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3394 lttng_payload_init(&client
->communication
.inbound
.payload
);
3395 lttng_payload_init(&client
->communication
.outbound
.payload
);
3396 client
->communication
.inbound
.expect_creds
= true;
3398 ret
= client_reset_inbound_state(client
);
3400 ERR("Failed to reset client communication's inbound state");
3405 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3407 ERR("Failed to accept new notification channel client connection");
3412 client
->socket
= ret
;
3414 ret
= socket_set_non_blocking(client
->socket
);
3416 ERR("Failed to set new notification channel client connection socket as non-blocking");
3420 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3422 ERR("Failed to set socket options on new notification channel client socket");
3427 client
->communication
.current_poll_events
= CLIENT_POLL_EVENTS_IN
;
3428 ret
= lttng_poll_add(
3429 &state
->events
, client
->socket
, client
->communication
.current_poll_events
);
3431 ERR("Failed to add notification channel client socket to poll set");
3435 DBG("Added new notification channel client socket (%i) to poll set", client
->socket
);
3438 cds_lfht_add(state
->client_socket_ht
,
3439 hash_client_socket(client
->socket
),
3440 &client
->client_socket_ht_node
);
3441 cds_lfht_add(state
->client_id_ht
, hash_client_id(client
->id
), &client
->client_id_ht_node
);
3447 notification_client_destroy(client
);
3452 * RCU read-lock must be held by the caller.
3453 * Client lock must _not_ be held by the caller.
3455 static int notification_thread_client_disconnect(struct notification_client
*client
,
3456 struct notification_thread_state
*state
)
3459 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3461 ASSERT_RCU_READ_LOCKED();
3463 /* Acquire the client lock to disable its communication atomically. */
3464 pthread_mutex_lock(&client
->lock
);
3465 client
->communication
.active
= false;
3466 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3467 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3468 pthread_mutex_unlock(&client
->lock
);
3470 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3472 ERR("Failed to remove client socket %d from poll set", client
->socket
);
3475 /* Release all conditions to which the client was subscribed. */
3476 cds_list_for_each_entry_safe (condition_list_element
, tmp
, &client
->condition_list
, node
) {
3477 (void) notification_thread_client_unsubscribe(
3478 client
, condition_list_element
->condition
, state
, nullptr);
3482 * Client no longer accessible to other threads (through the
3485 notification_client_destroy(client
);
3489 int handle_notification_thread_client_disconnect(int client_socket
,
3490 struct notification_thread_state
*state
)
3493 struct notification_client
*client
;
3496 DBG("Closing client connection (socket fd = %i)", client_socket
);
3497 client
= get_client_from_socket(client_socket
, state
);
3499 /* Internal state corruption, fatal error. */
3500 ERR("Unable to find client (socket fd = %i)", client_socket
);
3505 ret
= notification_thread_client_disconnect(client
, state
);
3511 int handle_notification_thread_client_disconnect_all(struct notification_thread_state
*state
)
3513 struct cds_lfht_iter iter
;
3514 struct notification_client
*client
;
3515 bool error_encoutered
= false;
3518 DBG("Closing all client connections");
3519 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
, client_socket_ht_node
) {
3522 ret
= notification_thread_client_disconnect(client
, state
);
3524 error_encoutered
= true;
3528 return error_encoutered
? 1 : 0;
3531 int handle_notification_thread_trigger_unregister_all(struct notification_thread_state
*state
)
3533 bool error_occurred
= false;
3534 struct cds_lfht_iter iter
;
3535 struct lttng_trigger_ht_element
*trigger_ht_element
;
3538 cds_lfht_for_each_entry (state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
3539 int ret
= handle_notification_thread_command_unregister_trigger(
3540 state
, trigger_ht_element
->trigger
, nullptr);
3542 error_occurred
= true;
3546 return error_occurred
? -1 : 0;
3549 static bool client_has_outbound_data_left(const struct notification_client
*client
)
3551 const struct lttng_payload_view pv
=
3552 lttng_payload_view_from_payload(&client
->communication
.outbound
.payload
, 0, -1);
3553 const bool has_data
= pv
.buffer
.size
!= 0;
3554 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3556 return has_data
|| has_fds
;
3559 static int client_handle_transmission_status(struct notification_client
*client
,
3560 enum client_transmission_status transmission_status
,
3561 struct notification_thread_state
*state
)
3565 switch (transmission_status
) {
3566 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3567 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3569 int current_poll_events
;
3570 int new_poll_events
;
3572 * We want to be notified whenever there is buffer space
3573 * available to send the rest of the payload if we are
3574 * waiting to send data to the client.
3576 * The state of the outbound queue being sampled here is
3578 * - it is okay to wake-up "for nothing" in case we see
3579 * that data is left, but another thread succeeds in
3580 * flushing it before us when handling the client "out"
3581 * event. We will simply stop monitoring that event the next
3582 * time it wakes us up and we see no data left to be sent,
3583 * - if another thread fails to flush the entire client
3584 * outgoing queue, it will issue a "communication update"
3585 * command and cause the client's (e)poll mask to be
3588 * The situation we seek to avoid would be to disable the
3589 * monitoring of "out" client events indefinitely when there is
3590 * data to be sent, which can't happen because of the
3591 * aforementioned "communication update" mechanism.
3593 pthread_mutex_lock(&client
->lock
);
3594 current_poll_events
= client
->communication
.current_poll_events
;
3595 new_poll_events
= client_has_outbound_data_left(client
) ?
3596 CLIENT_POLL_EVENTS_IN_OUT
:
3597 CLIENT_POLL_EVENTS_IN
;
3598 client
->communication
.current_poll_events
= new_poll_events
;
3599 pthread_mutex_unlock(&client
->lock
);
3601 /* Update the monitored event set only if it changed. */
3602 if (current_poll_events
!= new_poll_events
) {
3603 ret
= lttng_poll_mod(&state
->events
, client
->socket
, new_poll_events
);
3611 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3612 ret
= notification_thread_client_disconnect(client
, state
);
3617 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3627 /* Client lock must be acquired by caller. */
3628 static enum client_transmission_status
3629 client_flush_outgoing_queue(struct notification_client
*client
)
3632 size_t to_send_count
;
3633 enum client_transmission_status status
;
3634 struct lttng_payload_view pv
=
3635 lttng_payload_view_from_payload(&client
->communication
.outbound
.payload
, 0, -1);
3636 const int fds_to_send_count
= lttng_payload_view_get_fd_handle_count(&pv
);
3638 ASSERT_LOCKED(client
->lock
);
3640 if (!client
->communication
.active
) {
3641 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3645 if (pv
.buffer
.size
== 0) {
3647 * If both data and fds are equal to zero, we are in an invalid
3650 LTTNG_ASSERT(fds_to_send_count
!= 0);
3655 to_send_count
= pv
.buffer
.size
;
3656 DBG("Flushing client (socket fd = %i) outgoing queue", client
->socket
);
3658 ret
= lttcomm_send_unix_sock_non_block(client
->socket
, pv
.buffer
.data
, to_send_count
);
3659 if ((ret
>= 0 && ret
< to_send_count
)) {
3660 DBG("Client (socket fd = %i) outgoing queue could not be completely flushed",
3662 to_send_count
-= std::max(ret
, (ssize_t
) 0);
3664 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3665 pv
.buffer
.data
+ pv
.buffer
.size
- to_send_count
,
3667 ret
= lttng_dynamic_buffer_set_size(&client
->communication
.outbound
.payload
.buffer
,
3673 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3675 } else if (ret
< 0) {
3676 /* Generic error, disable the client's communication. */
3677 ERR("Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3679 client
->communication
.active
= false;
3680 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3684 * No error and flushed the queue completely.
3686 * The payload buffer size is used later to
3687 * check if there is notifications queued. So albeit that the
3688 * direct caller knows that the transmission is complete, we
3689 * need to set the buffer size to zero.
3691 ret
= lttng_dynamic_buffer_set_size(&client
->communication
.outbound
.payload
.buffer
,
3699 /* No fds to send, transmission is complete. */
3700 if (fds_to_send_count
== 0) {
3701 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3705 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(client
->socket
, &pv
);
3707 /* Generic error, disable the client's communication. */
3708 ERR("Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3710 client
->communication
.active
= false;
3711 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3713 } else if (ret
== 0) {
3714 /* Nothing could be sent. */
3715 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3717 /* Fd passing is an all or nothing kind of thing. */
3718 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3720 * The payload _fd_array count is used later to
3721 * check if there is notifications queued. So although the
3722 * direct caller knows that the transmission is complete, we
3723 * need to clear the _fd_array for the queuing check.
3725 lttng_dynamic_pointer_array_clear(
3726 &client
->communication
.outbound
.payload
._fd_handles
);
3730 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3731 client
->communication
.outbound
.queued_command_reply
= false;
3732 client
->communication
.outbound
.dropped_notification
= false;
3733 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3738 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3741 /* Client lock must _not_ be held by the caller. */
3742 static int client_send_command_reply(struct notification_client
*client
,
3743 struct notification_thread_state
*state
,
3744 enum lttng_notification_channel_status status
)
3747 struct lttng_notification_channel_command_reply reply
= {
3748 .status
= (int8_t) status
,
3750 struct lttng_notification_channel_message msg
;
3751 char buffer
[sizeof(msg
) + sizeof(reply
)];
3752 enum client_transmission_status transmission_status
;
3754 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
;
3755 msg
.size
= sizeof(reply
);
3758 memcpy(buffer
, &msg
, sizeof(msg
));
3759 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3760 DBG("Send command reply (%i)", (int) status
);
3762 pthread_mutex_lock(&client
->lock
);
3763 if (client
->communication
.outbound
.queued_command_reply
) {
3764 /* Protocol error. */
3768 /* Enqueue buffer to outgoing queue and flush it. */
3769 ret
= lttng_dynamic_buffer_append(
3770 &client
->communication
.outbound
.payload
.buffer
, buffer
, sizeof(buffer
));
3775 transmission_status
= client_flush_outgoing_queue(client
);
3777 if (client_has_outbound_data_left(client
)) {
3778 /* Queue could not be emptied. */
3779 client
->communication
.outbound
.queued_command_reply
= true;
3782 pthread_mutex_unlock(&client
->lock
);
3783 ret
= client_handle_transmission_status(client
, transmission_status
, state
);
3790 pthread_mutex_unlock(&client
->lock
);
3795 static int client_handle_message_unknown(struct notification_client
*client
,
3796 struct notification_thread_state
*state
3797 __attribute__((unused
)))
3801 * Receiving message header. The function will be called again
3802 * once the rest of the message as been received and can be
3805 const struct lttng_notification_channel_message
*msg
;
3807 LTTNG_ASSERT(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3808 msg
= (const struct lttng_notification_channel_message
*)
3809 client
->communication
.inbound
.payload
.buffer
.data
;
3811 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3812 ERR("Invalid notification channel message: length = %u", msg
->size
);
3817 switch (msg
->type
) {
3818 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3819 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3820 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3824 ERR("Invalid notification channel message: unexpected message type");
3828 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3829 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3830 client
->communication
.inbound
.msg_type
=
3831 (enum lttng_notification_channel_message_type
) msg
->type
;
3832 ret
= lttng_dynamic_buffer_set_size(&client
->communication
.inbound
.payload
.buffer
,
3835 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3841 static int client_handle_message_handshake(struct notification_client
*client
,
3842 struct notification_thread_state
*state
)
3845 struct lttng_notification_channel_command_handshake
*handshake_client
;
3846 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3847 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3848 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3850 struct lttng_notification_channel_message msg_header
;
3851 enum lttng_notification_channel_status status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3852 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3854 msg_header
.type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
;
3855 msg_header
.size
= sizeof(handshake_reply
);
3858 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3859 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
, sizeof(handshake_reply
));
3861 handshake_client
= (struct lttng_notification_channel_command_handshake
*)
3862 client
->communication
.inbound
.payload
.buffer
.data
;
3863 client
->major
= handshake_client
->major
;
3864 client
->minor
= handshake_client
->minor
;
3865 if (!client
->communication
.inbound
.creds_received
) {
3866 ERR("No credentials received from client");
3871 client
->uid
= LTTNG_SOCK_GET_UID_CRED(&client
->communication
.inbound
.creds
);
3872 client
->gid
= LTTNG_SOCK_GET_GID_CRED(&client
->communication
.inbound
.creds
);
3873 client
->is_sessiond
= LTTNG_SOCK_GET_PID_CRED(&client
->communication
.inbound
.creds
) ==
3875 DBG("Received handshake from client: uid = %u, gid = %u, protocol version = %i.%i, client is sessiond = %s",
3878 (int) client
->major
,
3879 (int) client
->minor
,
3880 client
->is_sessiond
? "true" : "false");
3882 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3883 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3886 pthread_mutex_lock(&client
->lock
);
3887 /* Outgoing queue will be flushed when the command reply is sent. */
3888 ret
= lttng_dynamic_buffer_append(
3889 &client
->communication
.outbound
.payload
.buffer
, send_buffer
, sizeof(send_buffer
));
3891 ERR("Failed to send protocol version to notification channel client");
3895 client
->validated
= true;
3896 client
->communication
.active
= true;
3897 pthread_mutex_unlock(&client
->lock
);
3899 /* Set reception state to receive the next message header. */
3900 ret
= client_reset_inbound_state(client
);
3902 ERR("Failed to reset client communication's inbound state");
3906 /* Flushes the outgoing queue. */
3907 ret
= client_send_command_reply(client
, state
, status
);
3909 ERR("Failed to send reply to notification channel client");
3915 pthread_mutex_unlock(&client
->lock
);
3920 static int client_handle_message_subscription(struct notification_client
*client
,
3921 enum lttng_notification_channel_message_type msg_type
,
3922 struct notification_thread_state
*state
)
3925 struct lttng_condition
*condition
;
3926 enum lttng_notification_channel_status status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3927 struct lttng_payload_view condition_view
=
3928 lttng_payload_view_from_payload(&client
->communication
.inbound
.payload
, 0, -1);
3929 size_t expected_condition_size
;
3932 * No need to lock client to sample the inbound state as the only
3933 * other thread accessing clients (action executor) only uses the
3936 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3937 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3938 if (ret
!= expected_condition_size
) {
3939 ERR("Malformed condition received from client");
3943 /* Ownership of condition is always transferred. */
3944 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3945 ret
= notification_thread_client_subscribe(client
, condition
, state
, &status
);
3947 ret
= notification_thread_client_unsubscribe(client
, condition
, state
, &status
);
3954 /* Set reception state to receive the next message header. */
3955 ret
= client_reset_inbound_state(client
);
3957 ERR("Failed to reset client communication's inbound state");
3961 ret
= client_send_command_reply(client
, state
, status
);
3963 ERR("Failed to send reply to notification channel client");
3971 static int client_dispatch_message(struct notification_client
*client
,
3972 struct notification_thread_state
*state
)
3976 if (client
->communication
.inbound
.msg_type
!=
3977 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3978 client
->communication
.inbound
.msg_type
!=
3979 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3980 !client
->validated
) {
3981 WARN("client attempted a command before handshake");
3986 switch (client
->communication
.inbound
.msg_type
) {
3987 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3989 ret
= client_handle_message_unknown(client
, state
);
3992 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3994 ret
= client_handle_message_handshake(client
, state
);
3997 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3998 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
4000 ret
= client_handle_message_subscription(
4001 client
, client
->communication
.inbound
.msg_type
, state
);
4011 /* Incoming data from client. */
4012 int handle_notification_thread_client_in(struct notification_thread_state
*state
, int socket
)
4015 struct notification_client
*client
;
4020 client
= get_client_from_socket(socket
, state
);
4022 /* Internal error, abort. */
4027 if (client
->communication
.inbound
.bytes_to_receive
== 0 &&
4028 client
->communication
.inbound
.fds_to_receive
!= 0) {
4029 /* Only FDs left to receive. */
4033 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
4034 client
->communication
.inbound
.bytes_to_receive
;
4035 if (client
->communication
.inbound
.expect_creds
) {
4036 recv_ret
= lttcomm_recv_creds_unix_sock(
4038 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
4039 client
->communication
.inbound
.bytes_to_receive
,
4040 &client
->communication
.inbound
.creds
);
4042 client
->communication
.inbound
.expect_creds
= false;
4043 client
->communication
.inbound
.creds_received
= true;
4046 recv_ret
= lttcomm_recv_unix_sock_non_block(
4048 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
4049 client
->communication
.inbound
.bytes_to_receive
);
4051 if (recv_ret
>= 0) {
4052 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
4054 goto error_disconnect_client
;
4057 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
4058 /* Message incomplete wait for more data. */
4064 LTTNG_ASSERT(client
->communication
.inbound
.bytes_to_receive
== 0);
4067 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
4068 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
4070 client
->communication
.inbound
.fds_to_receive
,
4071 &client
->communication
.inbound
.payload
);
4074 * Fds received. non blocking fds passing is all
4077 ssize_t expected_size
;
4079 expected_size
= sizeof(int) * client
->communication
.inbound
.fds_to_receive
;
4080 LTTNG_ASSERT(ret
== expected_size
);
4081 client
->communication
.inbound
.fds_to_receive
= 0;
4082 } else if (ret
== 0) {
4083 /* Received nothing. */
4087 goto error_disconnect_client
;
4091 /* At this point the message is complete.*/
4092 LTTNG_ASSERT(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4093 client
->communication
.inbound
.fds_to_receive
== 0);
4094 ret
= client_dispatch_message(client
, state
);
4097 * Only returns an error if this client must be
4100 goto error_disconnect_client
;
4107 error_disconnect_client
:
4108 ret
= notification_thread_client_disconnect(client
, state
);
4112 /* Client ready to receive outgoing data. */
4113 int handle_notification_thread_client_out(struct notification_thread_state
*state
, int socket
)
4116 struct notification_client
*client
;
4117 enum client_transmission_status transmission_status
;
4120 client
= get_client_from_socket(socket
, state
);
4122 /* Internal error, abort. */
4127 pthread_mutex_lock(&client
->lock
);
4128 if (!client_has_outbound_data_left(client
)) {
4130 * A client "out" event can be received when no payload is left
4131 * to send under some circumstances.
4133 * Many threads can flush a client's outgoing queue and, if they
4134 * had to queue their message (socket was full), will use the
4135 * "communication update" command to signal the (e)poll thread
4136 * to monitor for space being made available in the socket.
4138 * Commands are sent over an internal pipe serviced by the same
4139 * thread as the client sockets.
4141 * When space is made available in the socket, there is a race
4142 * between the (e)poll thread and the other threads that may
4143 * wish to use the client's socket to flush its outgoing queue.
4145 * A non-(e)poll thread may attempt (and succeed) in flushing
4146 * the queue before the (e)poll thread gets a chance to service
4147 * the client's "out" event.
4149 * In this situation, the (e)poll thread processing the client
4150 * out event will see an empty payload: there is nothing to do
4151 * except unsubscribing (e)poll "out" events.
4153 * Note that this thread is the (e)poll thread so it can modify
4154 * the (e)poll mask directly without using a communication
4155 * update command. Other threads that flush the outgoing queue
4156 * will use the "communication update" command to wake up this
4157 * thread and force it to monitor "out" events.
4159 * When other threads succeed in emptying the outgoing queue,
4160 * they don't need to update the (e)poll mask: if the "out"
4161 * event is monitored, it will fire once and the (e)poll
4162 * thread will reach this condition, causing the event to
4163 * stop being monitored.
4165 transmission_status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
4167 transmission_status
= client_flush_outgoing_queue(client
);
4169 pthread_mutex_unlock(&client
->lock
);
4171 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. */
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
);
4768 static int handle_one_event_notifier_notification(struct notification_thread_state
*state
,
4770 enum lttng_domain_type domain
)
4773 struct lttng_event_notifier_notification
*notification
= nullptr;
4775 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4776 if (notification
== nullptr) {
4777 /* Reception failed, don't consider it fatal. */
4778 ERR("Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4780 lttng_domain_type_str(domain
));
4784 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4786 ERR("Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4788 lttng_domain_type_str(domain
));
4793 lttng_event_notifier_notification_destroy(notification
);
4797 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4799 enum lttng_domain_type domain
)
4801 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4804 int handle_notification_thread_channel_sample(struct notification_thread_state
*state
,
4806 enum lttng_domain_type domain
)
4809 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4810 struct channel_info
*channel_info
= nullptr;
4811 struct cds_lfht_node
*node
;
4812 struct cds_lfht_iter iter
;
4813 struct lttng_channel_trigger_list
*channel_trigger_list
;
4814 struct lttng_session_trigger_list
*session_trigger_list
;
4815 struct lttng_trigger_list_element
*trigger_list_element
;
4816 bool previous_sample_available
= false;
4817 struct channel_state_sample channel_previous_sample
, channel_new_sample
;
4818 struct session_state_sample session_new_sample
;
4819 struct lttng_credentials channel_creds
= {};
4820 struct lttng_credentials session_creds
= {};
4821 struct session_info
*session
;
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
;
4842 session
= get_session_info_by_id(state
, sample_msg
.session_id
);
4844 DBG("Received a sample for an unknown session from consumerd: session id = %" PRIu64
,
4845 sample_msg
.session_id
);
4849 session_new_sample
= session
->last_state_sample
;
4850 session_new_sample
.consumed_data_size
+= sample_msg
.consumed_since_last_sample
;
4852 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session
->uid
),
4853 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session
->gid
),
4856 session_trigger_list
= get_session_trigger_list(state
, session
->name
);
4857 LTTNG_ASSERT(session_trigger_list
);
4858 cds_list_for_each_entry (trigger_list_element
, &session_trigger_list
->list
, node
) {
4859 const struct lttng_condition
*condition
;
4860 struct lttng_trigger
*trigger
;
4861 struct notification_client_list
*client_list
= nullptr;
4862 struct lttng_evaluation
*evaluation
= nullptr;
4863 enum action_executor_status executor_status
;
4866 trigger
= trigger_list_element
->trigger
;
4867 condition
= lttng_trigger_get_const_condition(trigger
);
4868 LTTNG_ASSERT(condition
);
4870 ret
= evaluate_session_condition(
4871 condition
, session
, &session_new_sample
, &evaluation
);
4872 if (caa_unlikely(ret
)) {
4876 if (caa_likely(!evaluation
)) {
4881 * Ownership of `evaluation` transferred to the action executor
4882 * no matter the result. The callee acquires a reference to the
4883 * client list: we can release our own.
4885 client_list
= get_client_list_from_condition(state
, condition
);
4886 executor_status
= action_executor_enqueue_trigger(
4887 state
->executor
, trigger
, evaluation
, &session_creds
, client_list
);
4888 notification_client_list_put(client_list
);
4889 evaluation
= nullptr;
4890 switch (executor_status
) {
4891 case ACTION_EXECUTOR_STATUS_OK
:
4893 case ACTION_EXECUTOR_STATUS_ERROR
:
4894 case ACTION_EXECUTOR_STATUS_INVALID
:
4896 * TODO Add trigger identification (name/id) when
4897 * it is added to the API.
4899 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4902 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4904 * TODO Add trigger identification (name/id) when
4905 * it is added to the API.
4907 * Not a fatal error.
4909 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4917 /* Retrieve the channel's informations */
4918 cds_lfht_lookup(state
->channels_ht
,
4919 hash_channel_key(&channel_new_sample
.key
),
4921 &channel_new_sample
.key
,
4923 node
= cds_lfht_iter_get_node(&iter
);
4924 if (caa_unlikely(!node
)) {
4926 * Not an error since the consumer can push a sample to the pipe
4927 * and the rest of the session daemon could notify us of the
4928 * channel's destruction before we get a chance to process that
4931 DBG("Received a sample for an unknown channel from consumerd, key = %" PRIu64
4933 channel_new_sample
.key
.key
,
4934 lttng_domain_type_str(domain
));
4938 channel_info
= caa_container_of(node
, struct channel_info
, channels_ht_node
);
4939 DBG("Handling channel sample for channel %s (key = %" PRIu64
4940 ") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
4941 ", consumed since last sample = %" PRIu64
")",
4943 channel_new_sample
.key
.key
,
4944 channel_info
->session_info
->name
,
4945 channel_new_sample
.highest_usage
,
4946 channel_new_sample
.lowest_usage
,
4947 sample_msg
.consumed_since_last_sample
);
4949 /* Retrieve the channel's last sample, if it exists, and update it. */
4950 cds_lfht_lookup(state
->channel_state_ht
,
4951 hash_channel_key(&channel_new_sample
.key
),
4952 match_channel_state_sample
,
4953 &channel_new_sample
.key
,
4955 node
= cds_lfht_iter_get_node(&iter
);
4956 if (caa_likely(node
)) {
4957 struct channel_state_sample
*stored_sample
;
4959 /* Update the sample stored. */
4961 caa_container_of(node
, struct channel_state_sample
, channel_state_ht_node
);
4963 memcpy(&channel_previous_sample
, stored_sample
, sizeof(channel_previous_sample
));
4964 stored_sample
->highest_usage
= channel_new_sample
.highest_usage
;
4965 stored_sample
->lowest_usage
= channel_new_sample
.lowest_usage
;
4966 previous_sample_available
= true;
4969 * This is the channel's first sample, allocate space for and
4970 * store the new sample.
4972 struct channel_state_sample
*stored_sample
;
4974 stored_sample
= zmalloc
<channel_state_sample
>();
4975 if (!stored_sample
) {
4980 memcpy(stored_sample
, &channel_new_sample
, sizeof(*stored_sample
));
4981 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4982 cds_lfht_add(state
->channel_state_ht
,
4983 hash_channel_key(&stored_sample
->key
),
4984 &stored_sample
->channel_state_ht_node
);
4987 /* Find triggers associated with this channel. */
4988 cds_lfht_lookup(state
->channel_triggers_ht
,
4989 hash_channel_key(&channel_new_sample
.key
),
4990 match_channel_trigger_list
,
4991 &channel_new_sample
.key
,
4993 node
= cds_lfht_iter_get_node(&iter
);
4996 channel_creds
= (typeof(channel_creds
)){
4997 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4998 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
5001 channel_trigger_list
=
5002 caa_container_of(node
, struct lttng_channel_trigger_list
, channel_triggers_ht_node
);
5003 cds_list_for_each_entry (trigger_list_element
, &channel_trigger_list
->list
, node
) {
5004 const struct lttng_condition
*condition
;
5005 struct lttng_trigger
*trigger
;
5006 struct notification_client_list
*client_list
= nullptr;
5007 struct lttng_evaluation
*evaluation
= nullptr;
5008 enum action_executor_status executor_status
;
5011 trigger
= trigger_list_element
->trigger
;
5012 condition
= lttng_trigger_get_const_condition(trigger
);
5013 LTTNG_ASSERT(condition
);
5015 ret
= evaluate_buffer_condition(
5019 previous_sample_available
? &channel_previous_sample
: nullptr,
5020 &channel_new_sample
,
5022 if (caa_unlikely(ret
)) {
5026 if (caa_likely(!evaluation
)) {
5031 * Ownership of `evaluation` transferred to the action executor
5032 * no matter the result. The callee acquires a reference to the
5033 * client list: we can release our own.
5035 client_list
= get_client_list_from_condition(state
, condition
);
5036 executor_status
= action_executor_enqueue_trigger(
5037 state
->executor
, trigger
, evaluation
, &channel_creds
, client_list
);
5038 notification_client_list_put(client_list
);
5039 evaluation
= nullptr;
5040 switch (executor_status
) {
5041 case ACTION_EXECUTOR_STATUS_OK
:
5043 case ACTION_EXECUTOR_STATUS_ERROR
:
5044 case ACTION_EXECUTOR_STATUS_INVALID
:
5046 * TODO Add trigger identification (name/id) when
5047 * it is added to the API.
5049 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
5052 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
5054 * TODO Add trigger identification (name/id) when
5055 * it is added to the API.
5057 * Not a fatal error.
5059 WARN("No space left when enqueuing action associated with buffer-condition trigger");
5068 session
->last_state_sample
= session_new_sample
;
5070 session_info_put(session
);