2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include <urcu/rculfhash.h>
12 #include <common/defaults.h>
13 #include <common/error.h>
14 #include <common/futex.h>
15 #include <common/unix.h>
16 #include <common/dynamic-buffer.h>
17 #include <common/hashtable/utils.h>
18 #include <common/sessiond-comm/sessiond-comm.h>
19 #include <common/macros.h>
20 #include <lttng/condition/condition.h>
21 #include <lttng/action/action-internal.h>
22 #include <lttng/notification/notification-internal.h>
23 #include <lttng/condition/condition-internal.h>
24 #include <lttng/condition/buffer-usage-internal.h>
25 #include <lttng/condition/session-consumed-size-internal.h>
26 #include <lttng/condition/session-rotation-internal.h>
27 #include <lttng/notification/channel-internal.h>
35 #include "notification-thread.h"
36 #include "notification-thread-events.h"
37 #include "notification-thread-commands.h"
38 #include "lttng-sessiond.h"
41 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
42 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
44 enum lttng_object_type
{
45 LTTNG_OBJECT_TYPE_UNKNOWN
,
46 LTTNG_OBJECT_TYPE_NONE
,
47 LTTNG_OBJECT_TYPE_CHANNEL
,
48 LTTNG_OBJECT_TYPE_SESSION
,
51 struct lttng_trigger_list_element
{
52 /* No ownership of the trigger object is assumed. */
53 const struct lttng_trigger
*trigger
;
54 struct cds_list_head node
;
57 struct lttng_channel_trigger_list
{
58 struct channel_key channel_key
;
59 /* List of struct lttng_trigger_list_element. */
60 struct cds_list_head list
;
61 /* Node in the channel_triggers_ht */
62 struct cds_lfht_node channel_triggers_ht_node
;
63 /* call_rcu delayed reclaim. */
64 struct rcu_head rcu_node
;
68 * List of triggers applying to a given session.
71 * - lttng_session_trigger_list_create()
72 * - lttng_session_trigger_list_build()
73 * - lttng_session_trigger_list_destroy()
74 * - lttng_session_trigger_list_add()
76 struct lttng_session_trigger_list
{
78 * Not owned by this; points to the session_info structure's
81 const char *session_name
;
82 /* List of struct lttng_trigger_list_element. */
83 struct cds_list_head list
;
84 /* Node in the session_triggers_ht */
85 struct cds_lfht_node session_triggers_ht_node
;
87 * Weak reference to the notification system's session triggers
90 * The session trigger list structure structure is owned by
91 * the session's session_info.
93 * The session_info is kept alive the the channel_infos holding a
94 * reference to it (reference counting). When those channels are
95 * destroyed (at runtime or on teardown), the reference they hold
96 * to the session_info are released. On destruction of session_info,
97 * session_info_destroy() will remove the list of triggers applying
98 * to this session from the notification system's state.
100 * This implies that the session_triggers_ht must be destroyed
101 * after the channels.
103 struct cds_lfht
*session_triggers_ht
;
104 /* Used for delayed RCU reclaim. */
105 struct rcu_head rcu_node
;
108 struct lttng_trigger_ht_element
{
109 struct lttng_trigger
*trigger
;
110 struct cds_lfht_node node
;
111 /* call_rcu delayed reclaim. */
112 struct rcu_head rcu_node
;
115 struct lttng_condition_list_element
{
116 struct lttng_condition
*condition
;
117 struct cds_list_head node
;
120 struct notification_client_list_element
{
121 struct notification_client
*client
;
122 struct cds_list_head node
;
125 struct notification_client_list
{
126 const struct lttng_trigger
*trigger
;
127 struct cds_list_head list
;
128 struct cds_lfht_node notification_trigger_ht_node
;
129 /* call_rcu delayed reclaim. */
130 struct rcu_head rcu_node
;
133 struct notification_client
{
134 notification_client_id id
;
136 /* Client protocol version. */
137 uint8_t major
, minor
;
141 * Indicates if the credentials and versions of the client have been
146 * Conditions to which the client's notification channel is subscribed.
147 * List of struct lttng_condition_list_node. The condition member is
148 * owned by the client.
150 struct cds_list_head condition_list
;
151 struct cds_lfht_node client_socket_ht_node
;
152 struct cds_lfht_node client_id_ht_node
;
156 * During the reception of a message, the reception
157 * buffers' "size" is set to contain the current
158 * message's complete payload.
160 struct lttng_dynamic_buffer buffer
;
161 /* Bytes left to receive for the current message. */
162 size_t bytes_to_receive
;
163 /* Type of the message being received. */
164 enum lttng_notification_channel_message_type msg_type
;
166 * Indicates whether or not credentials are expected
171 * Indicates whether or not credentials were received
175 /* Only used during credentials reception. */
176 lttng_sock_cred creds
;
180 * Indicates whether or not a notification addressed to
181 * this client was dropped because a command reply was
184 * A notification is dropped whenever the buffer is not
187 bool dropped_notification
;
189 * Indicates whether or not a command reply is already
190 * buffered. In this case, it means that the client is
191 * not consuming command replies before emitting a new
192 * one. This could be caused by a protocol error or a
193 * misbehaving/malicious client.
195 bool queued_command_reply
;
196 struct lttng_dynamic_buffer buffer
;
199 /* call_rcu delayed reclaim. */
200 struct rcu_head rcu_node
;
203 struct channel_state_sample
{
204 struct channel_key key
;
205 struct cds_lfht_node channel_state_ht_node
;
206 uint64_t highest_usage
;
207 uint64_t lowest_usage
;
208 uint64_t channel_total_consumed
;
209 /* call_rcu delayed reclaim. */
210 struct rcu_head rcu_node
;
213 static unsigned long hash_channel_key(struct channel_key
*key
);
214 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
215 struct lttng_evaluation
**evaluation
,
216 const struct notification_thread_state
*state
,
217 const struct channel_state_sample
*previous_sample
,
218 const struct channel_state_sample
*latest_sample
,
219 uint64_t previous_session_consumed_total
,
220 uint64_t latest_session_consumed_total
,
221 struct channel_info
*channel_info
);
223 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
224 const struct lttng_evaluation
*evaluation
,
225 struct notification_client_list
*client_list
,
226 struct notification_thread_state
*state
,
227 uid_t channel_uid
, gid_t channel_gid
);
230 /* session_info API */
232 void session_info_destroy(void *_data
);
234 void session_info_get(struct session_info
*session_info
);
236 void session_info_put(struct session_info
*session_info
);
238 struct session_info
*session_info_create(const char *name
,
239 uid_t uid
, gid_t gid
,
240 struct lttng_session_trigger_list
*trigger_list
,
241 struct cds_lfht
*sessions_ht
);
243 void session_info_add_channel(struct session_info
*session_info
,
244 struct channel_info
*channel_info
);
246 void session_info_remove_channel(struct session_info
*session_info
,
247 struct channel_info
*channel_info
);
249 /* lttng_session_trigger_list API */
251 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
252 const char *session_name
,
253 struct cds_lfht
*session_triggers_ht
);
255 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
256 const struct notification_thread_state
*state
,
257 const char *session_name
);
259 void lttng_session_trigger_list_destroy(
260 struct lttng_session_trigger_list
*list
);
262 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
263 const struct lttng_trigger
*trigger
);
267 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
269 /* This double-cast is intended to supress pointer-to-cast warning. */
270 const int socket
= (int) (intptr_t) key
;
271 const struct notification_client
*client
= caa_container_of(node
,
272 struct notification_client
, client_socket_ht_node
);
274 return client
->socket
== socket
;
278 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
280 /* This double-cast is intended to supress pointer-to-cast warning. */
281 const notification_client_id id
= *((notification_client_id
*) key
);
282 const struct notification_client
*client
= caa_container_of(
283 node
, struct notification_client
, client_id_ht_node
);
285 return client
->id
== id
;
289 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
291 struct channel_key
*channel_key
= (struct channel_key
*) key
;
292 struct lttng_channel_trigger_list
*trigger_list
;
294 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
295 channel_triggers_ht_node
);
297 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
298 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
302 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
304 const char *session_name
= (const char *) key
;
305 struct lttng_session_trigger_list
*trigger_list
;
307 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
308 session_triggers_ht_node
);
310 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
314 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
316 struct channel_key
*channel_key
= (struct channel_key
*) key
;
317 struct channel_state_sample
*sample
;
319 sample
= caa_container_of(node
, struct channel_state_sample
,
320 channel_state_ht_node
);
322 return !!((channel_key
->key
== sample
->key
.key
) &&
323 (channel_key
->domain
== sample
->key
.domain
));
327 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
329 struct channel_key
*channel_key
= (struct channel_key
*) key
;
330 struct channel_info
*channel_info
;
332 channel_info
= caa_container_of(node
, struct channel_info
,
335 return !!((channel_key
->key
== channel_info
->key
.key
) &&
336 (channel_key
->domain
== channel_info
->key
.domain
));
340 int match_condition(struct cds_lfht_node
*node
, const void *key
)
342 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
343 struct lttng_trigger_ht_element
*trigger
;
344 struct lttng_condition
*condition
;
346 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
348 condition
= lttng_trigger_get_condition(trigger
->trigger
);
351 return !!lttng_condition_is_equal(condition_key
, condition
);
355 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
357 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
358 struct notification_client_list
*client_list
;
359 const struct lttng_condition
*condition
;
361 assert(condition_key
);
363 client_list
= caa_container_of(node
, struct notification_client_list
,
364 notification_trigger_ht_node
);
365 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
367 return !!lttng_condition_is_equal(condition_key
, condition
);
371 int match_session(struct cds_lfht_node
*node
, const void *key
)
373 const char *name
= key
;
374 struct session_info
*session_info
= caa_container_of(
375 node
, struct session_info
, sessions_ht_node
);
377 return !strcmp(session_info
->name
, name
);
381 unsigned long lttng_condition_buffer_usage_hash(
382 const struct lttng_condition
*_condition
)
385 unsigned long condition_type
;
386 struct lttng_condition_buffer_usage
*condition
;
388 condition
= container_of(_condition
,
389 struct lttng_condition_buffer_usage
, parent
);
391 condition_type
= (unsigned long) condition
->parent
.type
;
392 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
393 if (condition
->session_name
) {
394 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
396 if (condition
->channel_name
) {
397 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
399 if (condition
->domain
.set
) {
400 hash
^= hash_key_ulong(
401 (void *) condition
->domain
.type
,
404 if (condition
->threshold_ratio
.set
) {
407 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
408 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
409 } else if (condition
->threshold_bytes
.set
) {
412 val
= condition
->threshold_bytes
.value
;
413 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
419 unsigned long lttng_condition_session_consumed_size_hash(
420 const struct lttng_condition
*_condition
)
423 unsigned long condition_type
=
424 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
425 struct lttng_condition_session_consumed_size
*condition
;
428 condition
= container_of(_condition
,
429 struct lttng_condition_session_consumed_size
, parent
);
431 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
432 if (condition
->session_name
) {
433 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
435 val
= condition
->consumed_threshold_bytes
.value
;
436 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
441 unsigned long lttng_condition_session_rotation_hash(
442 const struct lttng_condition
*_condition
)
444 unsigned long hash
, condition_type
;
445 struct lttng_condition_session_rotation
*condition
;
447 condition
= container_of(_condition
,
448 struct lttng_condition_session_rotation
, parent
);
449 condition_type
= (unsigned long) condition
->parent
.type
;
450 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
451 assert(condition
->session_name
);
452 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
457 * The lttng_condition hashing code is kept in this file (rather than
458 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
459 * don't want to link in liblttng-ctl.
462 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
464 switch (condition
->type
) {
465 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
466 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
467 return lttng_condition_buffer_usage_hash(condition
);
468 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
469 return lttng_condition_session_consumed_size_hash(condition
);
470 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
471 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
472 return lttng_condition_session_rotation_hash(condition
);
474 ERR("[notification-thread] Unexpected condition type caught");
480 unsigned long hash_channel_key(struct channel_key
*key
)
482 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
483 unsigned long domain_hash
= hash_key_ulong(
484 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
486 return key_hash
^ domain_hash
;
490 unsigned long hash_client_socket(int socket
)
492 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
496 unsigned long hash_client_id(notification_client_id id
)
498 return hash_key_u64(&id
, lttng_ht_seed
);
502 * Get the type of object to which a given condition applies. Bindings let
503 * the notification system evaluate a trigger's condition when a given
504 * object's state is updated.
506 * For instance, a condition bound to a channel will be evaluated everytime
507 * the channel's state is changed by a channel monitoring sample.
510 enum lttng_object_type
get_condition_binding_object(
511 const struct lttng_condition
*condition
)
513 switch (lttng_condition_get_type(condition
)) {
514 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
515 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
516 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
517 return LTTNG_OBJECT_TYPE_CHANNEL
;
518 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
519 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
520 return LTTNG_OBJECT_TYPE_SESSION
;
522 return LTTNG_OBJECT_TYPE_UNKNOWN
;
527 void free_channel_info_rcu(struct rcu_head
*node
)
529 free(caa_container_of(node
, struct channel_info
, rcu_node
));
533 void channel_info_destroy(struct channel_info
*channel_info
)
539 if (channel_info
->session_info
) {
540 session_info_remove_channel(channel_info
->session_info
,
542 session_info_put(channel_info
->session_info
);
544 if (channel_info
->name
) {
545 free(channel_info
->name
);
547 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
551 void free_session_info_rcu(struct rcu_head
*node
)
553 free(caa_container_of(node
, struct session_info
, rcu_node
));
556 /* Don't call directly, use the ref-counting mechanism. */
558 void session_info_destroy(void *_data
)
560 struct session_info
*session_info
= _data
;
563 assert(session_info
);
564 if (session_info
->channel_infos_ht
) {
565 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
567 ERR("[notification-thread] Failed to destroy channel information hash table");
570 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
573 cds_lfht_del(session_info
->sessions_ht
,
574 &session_info
->sessions_ht_node
);
576 free(session_info
->name
);
577 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
581 void session_info_get(struct session_info
*session_info
)
586 lttng_ref_get(&session_info
->ref
);
590 void session_info_put(struct session_info
*session_info
)
595 lttng_ref_put(&session_info
->ref
);
599 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
600 struct lttng_session_trigger_list
*trigger_list
,
601 struct cds_lfht
*sessions_ht
)
603 struct session_info
*session_info
;
607 session_info
= zmalloc(sizeof(*session_info
));
611 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
613 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
614 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
615 if (!session_info
->channel_infos_ht
) {
619 cds_lfht_node_init(&session_info
->sessions_ht_node
);
620 session_info
->name
= strdup(name
);
621 if (!session_info
->name
) {
624 session_info
->uid
= uid
;
625 session_info
->gid
= gid
;
626 session_info
->trigger_list
= trigger_list
;
627 session_info
->sessions_ht
= sessions_ht
;
631 session_info_put(session_info
);
636 void session_info_add_channel(struct session_info
*session_info
,
637 struct channel_info
*channel_info
)
640 cds_lfht_add(session_info
->channel_infos_ht
,
641 hash_channel_key(&channel_info
->key
),
642 &channel_info
->session_info_channels_ht_node
);
647 void session_info_remove_channel(struct session_info
*session_info
,
648 struct channel_info
*channel_info
)
651 cds_lfht_del(session_info
->channel_infos_ht
,
652 &channel_info
->session_info_channels_ht_node
);
657 struct channel_info
*channel_info_create(const char *channel_name
,
658 struct channel_key
*channel_key
, uint64_t channel_capacity
,
659 struct session_info
*session_info
)
661 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
667 cds_lfht_node_init(&channel_info
->channels_ht_node
);
668 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
669 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
670 channel_info
->capacity
= channel_capacity
;
672 channel_info
->name
= strdup(channel_name
);
673 if (!channel_info
->name
) {
678 * Set the references between session and channel infos:
679 * - channel_info holds a strong reference to session_info
680 * - session_info holds a weak reference to channel_info
682 session_info_get(session_info
);
683 session_info_add_channel(session_info
, channel_info
);
684 channel_info
->session_info
= session_info
;
688 channel_info_destroy(channel_info
);
692 /* RCU read lock must be held by the caller. */
694 struct notification_client_list
*get_client_list_from_condition(
695 struct notification_thread_state
*state
,
696 const struct lttng_condition
*condition
)
698 struct cds_lfht_node
*node
;
699 struct cds_lfht_iter iter
;
701 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
702 lttng_condition_hash(condition
),
703 match_client_list_condition
,
706 node
= cds_lfht_iter_get_node(&iter
);
708 return node
? caa_container_of(node
,
709 struct notification_client_list
,
710 notification_trigger_ht_node
) : NULL
;
713 /* This function must be called with the RCU read lock held. */
715 int evaluate_channel_condition_for_client(
716 const struct lttng_condition
*condition
,
717 struct notification_thread_state
*state
,
718 struct lttng_evaluation
**evaluation
,
719 uid_t
*session_uid
, gid_t
*session_gid
)
722 struct cds_lfht_iter iter
;
723 struct cds_lfht_node
*node
;
724 struct channel_info
*channel_info
= NULL
;
725 struct channel_key
*channel_key
= NULL
;
726 struct channel_state_sample
*last_sample
= NULL
;
727 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
729 /* Find the channel associated with the condition. */
730 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
731 channel_trigger_list
, channel_triggers_ht_node
) {
732 struct lttng_trigger_list_element
*element
;
734 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
735 const struct lttng_condition
*current_condition
=
736 lttng_trigger_get_const_condition(
739 assert(current_condition
);
740 if (!lttng_condition_is_equal(condition
,
741 current_condition
)) {
745 /* Found the trigger, save the channel key. */
746 channel_key
= &channel_trigger_list
->channel_key
;
750 /* The channel key was found stop iteration. */
756 /* No channel found; normal exit. */
757 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
762 /* Fetch channel info for the matching channel. */
763 cds_lfht_lookup(state
->channels_ht
,
764 hash_channel_key(channel_key
),
768 node
= cds_lfht_iter_get_node(&iter
);
770 channel_info
= caa_container_of(node
, struct channel_info
,
773 /* Retrieve the channel's last sample, if it exists. */
774 cds_lfht_lookup(state
->channel_state_ht
,
775 hash_channel_key(channel_key
),
776 match_channel_state_sample
,
779 node
= cds_lfht_iter_get_node(&iter
);
781 last_sample
= caa_container_of(node
,
782 struct channel_state_sample
,
783 channel_state_ht_node
);
785 /* Nothing to evaluate, no sample was ever taken. Normal exit */
786 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
791 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
793 0, channel_info
->session_info
->consumed_data_size
,
796 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
800 *session_uid
= channel_info
->session_info
->uid
;
801 *session_gid
= channel_info
->session_info
->gid
;
807 const char *get_condition_session_name(const struct lttng_condition
*condition
)
809 const char *session_name
= NULL
;
810 enum lttng_condition_status status
;
812 switch (lttng_condition_get_type(condition
)) {
813 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
814 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
815 status
= lttng_condition_buffer_usage_get_session_name(
816 condition
, &session_name
);
818 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
819 status
= lttng_condition_session_consumed_size_get_session_name(
820 condition
, &session_name
);
822 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
823 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
824 status
= lttng_condition_session_rotation_get_session_name(
825 condition
, &session_name
);
830 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
831 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
838 /* This function must be called with the RCU read lock held. */
840 int evaluate_session_condition_for_client(
841 const struct lttng_condition
*condition
,
842 struct notification_thread_state
*state
,
843 struct lttng_evaluation
**evaluation
,
844 uid_t
*session_uid
, gid_t
*session_gid
)
847 struct cds_lfht_iter iter
;
848 struct cds_lfht_node
*node
;
849 const char *session_name
;
850 struct session_info
*session_info
= NULL
;
852 session_name
= get_condition_session_name(condition
);
854 /* Find the session associated with the trigger. */
855 cds_lfht_lookup(state
->sessions_ht
,
856 hash_key_str(session_name
, lttng_ht_seed
),
860 node
= cds_lfht_iter_get_node(&iter
);
862 DBG("[notification-thread] No known session matching name \"%s\"",
868 session_info
= caa_container_of(node
, struct session_info
,
870 session_info_get(session_info
);
873 * Evaluation is performed in-line here since only one type of
874 * session-bound condition is handled for the moment.
876 switch (lttng_condition_get_type(condition
)) {
877 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
878 if (!session_info
->rotation
.ongoing
) {
880 goto end_session_put
;
883 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
884 session_info
->rotation
.id
);
887 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
890 goto end_session_put
;
896 goto end_session_put
;
899 *session_uid
= session_info
->uid
;
900 *session_gid
= session_info
->gid
;
903 session_info_put(session_info
);
908 /* This function must be called with the RCU read lock held. */
910 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
911 const struct lttng_condition
*condition
,
912 struct notification_client
*client
,
913 struct notification_thread_state
*state
)
916 struct lttng_evaluation
*evaluation
= NULL
;
917 struct notification_client_list client_list
= { 0 };
918 struct notification_client_list_element client_list_element
= { 0 };
919 uid_t object_uid
= 0;
920 gid_t object_gid
= 0;
927 switch (get_condition_binding_object(condition
)) {
928 case LTTNG_OBJECT_TYPE_SESSION
:
929 ret
= evaluate_session_condition_for_client(condition
, state
,
930 &evaluation
, &object_uid
, &object_gid
);
932 case LTTNG_OBJECT_TYPE_CHANNEL
:
933 ret
= evaluate_channel_condition_for_client(condition
, state
,
934 &evaluation
, &object_uid
, &object_gid
);
936 case LTTNG_OBJECT_TYPE_NONE
:
939 case LTTNG_OBJECT_TYPE_UNKNOWN
:
949 /* Evaluation yielded nothing. Normal exit. */
950 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
956 * Create a temporary client list with the client currently
959 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
960 CDS_INIT_LIST_HEAD(&client_list
.list
);
961 client_list
.trigger
= trigger
;
963 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
964 client_list_element
.client
= client
;
965 cds_list_add(&client_list_element
.node
, &client_list
.list
);
967 /* Send evaluation result to the newly-subscribed client. */
968 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
969 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
970 state
, object_uid
, object_gid
);
977 int notification_thread_client_subscribe(struct notification_client
*client
,
978 struct lttng_condition
*condition
,
979 struct notification_thread_state
*state
,
980 enum lttng_notification_channel_status
*_status
)
983 struct notification_client_list
*client_list
;
984 struct lttng_condition_list_element
*condition_list_element
= NULL
;
985 struct notification_client_list_element
*client_list_element
= NULL
;
986 enum lttng_notification_channel_status status
=
987 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
990 * Ensure that the client has not already subscribed to this condition
993 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
994 if (lttng_condition_is_equal(condition_list_element
->condition
,
996 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1001 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1002 if (!condition_list_element
) {
1006 client_list_element
= zmalloc(sizeof(*client_list_element
));
1007 if (!client_list_element
) {
1015 * Add the newly-subscribed condition to the client's subscription list.
1017 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1018 condition_list_element
->condition
= condition
;
1019 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1021 client_list
= get_client_list_from_condition(state
, condition
);
1024 * No notification-emiting trigger registered with this
1025 * condition. We don't evaluate the condition right away
1026 * since this trigger is not registered yet.
1028 free(client_list_element
);
1033 * The condition to which the client just subscribed is evaluated
1034 * at this point so that conditions that are already TRUE result
1035 * in a notification being sent out.
1037 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1039 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1041 free(client_list_element
);
1046 * Add the client to the list of clients interested in a given trigger
1047 * if a "notification" trigger with a corresponding condition was
1050 client_list_element
->client
= client
;
1051 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1052 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1061 free(condition_list_element
);
1062 free(client_list_element
);
1067 int notification_thread_client_unsubscribe(
1068 struct notification_client
*client
,
1069 struct lttng_condition
*condition
,
1070 struct notification_thread_state
*state
,
1071 enum lttng_notification_channel_status
*_status
)
1073 struct notification_client_list
*client_list
;
1074 struct lttng_condition_list_element
*condition_list_element
,
1076 struct notification_client_list_element
*client_list_element
,
1078 bool condition_found
= false;
1079 enum lttng_notification_channel_status status
=
1080 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1082 /* Remove the condition from the client's condition list. */
1083 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1084 &client
->condition_list
, node
) {
1085 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1090 cds_list_del(&condition_list_element
->node
);
1092 * The caller may be iterating on the client's conditions to
1093 * tear down a client's connection. In this case, the condition
1094 * will be destroyed at the end.
1096 if (condition
!= condition_list_element
->condition
) {
1097 lttng_condition_destroy(
1098 condition_list_element
->condition
);
1100 free(condition_list_element
);
1101 condition_found
= true;
1105 if (!condition_found
) {
1106 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1111 * Remove the client from the list of clients interested the trigger
1112 * matching the condition.
1115 client_list
= get_client_list_from_condition(state
, condition
);
1120 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1121 &client_list
->list
, node
) {
1122 if (client_list_element
->client
->socket
!= client
->socket
) {
1125 cds_list_del(&client_list_element
->node
);
1126 free(client_list_element
);
1132 lttng_condition_destroy(condition
);
1140 void free_notification_client_rcu(struct rcu_head
*node
)
1142 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1146 void notification_client_destroy(struct notification_client
*client
,
1147 struct notification_thread_state
*state
)
1149 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
1155 /* Release all conditions to which the client was subscribed. */
1156 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
1157 &client
->condition_list
, node
) {
1158 (void) notification_thread_client_unsubscribe(client
,
1159 condition_list_element
->condition
, state
, NULL
);
1162 if (client
->socket
>= 0) {
1163 (void) lttcomm_close_unix_sock(client
->socket
);
1164 client
->socket
= -1;
1166 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
1167 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
1168 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1172 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1176 struct notification_client
*get_client_from_socket(int socket
,
1177 struct notification_thread_state
*state
)
1179 struct cds_lfht_iter iter
;
1180 struct cds_lfht_node
*node
;
1181 struct notification_client
*client
= NULL
;
1183 cds_lfht_lookup(state
->client_socket_ht
,
1184 hash_client_socket(socket
),
1185 match_client_socket
,
1186 (void *) (unsigned long) socket
,
1188 node
= cds_lfht_iter_get_node(&iter
);
1193 client
= caa_container_of(node
, struct notification_client
,
1194 client_socket_ht_node
);
1200 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1204 struct notification_client
*get_client_from_id(notification_client_id id
,
1205 struct notification_thread_state
*state
)
1207 struct cds_lfht_iter iter
;
1208 struct cds_lfht_node
*node
;
1209 struct notification_client
*client
= NULL
;
1211 cds_lfht_lookup(state
->client_id_ht
,
1216 node
= cds_lfht_iter_get_node(&iter
);
1221 client
= caa_container_of(node
, struct notification_client
,
1228 bool buffer_usage_condition_applies_to_channel(
1229 const struct lttng_condition
*condition
,
1230 const struct channel_info
*channel_info
)
1232 enum lttng_condition_status status
;
1233 enum lttng_domain_type condition_domain
;
1234 const char *condition_session_name
= NULL
;
1235 const char *condition_channel_name
= NULL
;
1237 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1239 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1240 if (channel_info
->key
.domain
!= condition_domain
) {
1244 status
= lttng_condition_buffer_usage_get_session_name(
1245 condition
, &condition_session_name
);
1246 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1248 status
= lttng_condition_buffer_usage_get_channel_name(
1249 condition
, &condition_channel_name
);
1250 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1252 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1255 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1265 bool session_consumed_size_condition_applies_to_channel(
1266 const struct lttng_condition
*condition
,
1267 const struct channel_info
*channel_info
)
1269 enum lttng_condition_status status
;
1270 const char *condition_session_name
= NULL
;
1272 status
= lttng_condition_session_consumed_size_get_session_name(
1273 condition
, &condition_session_name
);
1274 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1276 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1286 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1287 const struct channel_info
*channel_info
)
1289 const struct lttng_condition
*condition
;
1290 bool trigger_applies
;
1292 condition
= lttng_trigger_get_const_condition(trigger
);
1297 switch (lttng_condition_get_type(condition
)) {
1298 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1299 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1300 trigger_applies
= buffer_usage_condition_applies_to_channel(
1301 condition
, channel_info
);
1303 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1304 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1305 condition
, channel_info
);
1311 return trigger_applies
;
1317 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1318 struct notification_client
*client
)
1320 bool applies
= false;
1321 struct lttng_condition_list_element
*condition_list_element
;
1323 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1325 applies
= lttng_condition_is_equal(
1326 condition_list_element
->condition
,
1327 lttng_trigger_get_condition(trigger
));
1335 /* Must be called with RCU read lock held. */
1337 struct lttng_session_trigger_list
*get_session_trigger_list(
1338 struct notification_thread_state
*state
,
1339 const char *session_name
)
1341 struct lttng_session_trigger_list
*list
= NULL
;
1342 struct cds_lfht_node
*node
;
1343 struct cds_lfht_iter iter
;
1345 cds_lfht_lookup(state
->session_triggers_ht
,
1346 hash_key_str(session_name
, lttng_ht_seed
),
1347 match_session_trigger_list
,
1350 node
= cds_lfht_iter_get_node(&iter
);
1353 * Not an error, the list of triggers applying to that session
1354 * will be initialized when the session is created.
1356 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1361 list
= caa_container_of(node
,
1362 struct lttng_session_trigger_list
,
1363 session_triggers_ht_node
);
1369 * Allocate an empty lttng_session_trigger_list for the session named
1372 * No ownership of 'session_name' is assumed by the session trigger list.
1373 * It is the caller's responsability to ensure the session name is alive
1374 * for as long as this list is.
1377 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1378 const char *session_name
,
1379 struct cds_lfht
*session_triggers_ht
)
1381 struct lttng_session_trigger_list
*list
;
1383 list
= zmalloc(sizeof(*list
));
1387 list
->session_name
= session_name
;
1388 CDS_INIT_LIST_HEAD(&list
->list
);
1389 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1390 list
->session_triggers_ht
= session_triggers_ht
;
1393 /* Publish the list through the session_triggers_ht. */
1394 cds_lfht_add(session_triggers_ht
,
1395 hash_key_str(session_name
, lttng_ht_seed
),
1396 &list
->session_triggers_ht_node
);
1403 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1405 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1410 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1412 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1414 /* Empty the list element by element, and then free the list itself. */
1415 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1416 &list
->list
, node
) {
1417 cds_list_del(&trigger_list_element
->node
);
1418 free(trigger_list_element
);
1421 /* Unpublish the list from the session_triggers_ht. */
1422 cds_lfht_del(list
->session_triggers_ht
,
1423 &list
->session_triggers_ht_node
);
1425 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1429 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1430 const struct lttng_trigger
*trigger
)
1433 struct lttng_trigger_list_element
*new_element
=
1434 zmalloc(sizeof(*new_element
));
1440 CDS_INIT_LIST_HEAD(&new_element
->node
);
1441 new_element
->trigger
= trigger
;
1442 cds_list_add(&new_element
->node
, &list
->list
);
1448 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1449 const char *session_name
)
1451 bool applies
= false;
1452 const struct lttng_condition
*condition
;
1454 condition
= lttng_trigger_get_const_condition(trigger
);
1455 switch (lttng_condition_get_type(condition
)) {
1456 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1457 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1459 enum lttng_condition_status condition_status
;
1460 const char *condition_session_name
;
1462 condition_status
= lttng_condition_session_rotation_get_session_name(
1463 condition
, &condition_session_name
);
1464 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1465 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1469 assert(condition_session_name
);
1470 applies
= !strcmp(condition_session_name
, session_name
);
1481 * Allocate and initialize an lttng_session_trigger_list which contains
1482 * all triggers that apply to the session named 'session_name'.
1484 * No ownership of 'session_name' is assumed by the session trigger list.
1485 * It is the caller's responsability to ensure the session name is alive
1486 * for as long as this list is.
1489 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1490 const struct notification_thread_state
*state
,
1491 const char *session_name
)
1493 int trigger_count
= 0;
1494 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1495 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1496 struct cds_lfht_iter iter
;
1498 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1499 state
->session_triggers_ht
);
1501 /* Add all triggers applying to the session named 'session_name'. */
1502 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1506 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1511 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1512 trigger_ht_element
->trigger
);
1520 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1522 return session_trigger_list
;
1524 lttng_session_trigger_list_destroy(session_trigger_list
);
1529 struct session_info
*find_or_create_session_info(
1530 struct notification_thread_state
*state
,
1531 const char *name
, uid_t uid
, gid_t gid
)
1533 struct session_info
*session
= NULL
;
1534 struct cds_lfht_node
*node
;
1535 struct cds_lfht_iter iter
;
1536 struct lttng_session_trigger_list
*trigger_list
;
1539 cds_lfht_lookup(state
->sessions_ht
,
1540 hash_key_str(name
, lttng_ht_seed
),
1544 node
= cds_lfht_iter_get_node(&iter
);
1546 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1548 session
= caa_container_of(node
, struct session_info
,
1550 assert(session
->uid
== uid
);
1551 assert(session
->gid
== gid
);
1552 session_info_get(session
);
1556 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1557 if (!trigger_list
) {
1561 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1562 state
->sessions_ht
);
1564 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1566 lttng_session_trigger_list_destroy(trigger_list
);
1569 trigger_list
= NULL
;
1571 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1572 &session
->sessions_ht_node
);
1578 session_info_put(session
);
1583 int handle_notification_thread_command_add_channel(
1584 struct notification_thread_state
*state
,
1585 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1586 const char *channel_name
, enum lttng_domain_type channel_domain
,
1587 uint64_t channel_key_int
, uint64_t channel_capacity
,
1588 enum lttng_error_code
*cmd_result
)
1590 struct cds_list_head trigger_list
;
1591 struct channel_info
*new_channel_info
= NULL
;
1592 struct channel_key channel_key
= {
1593 .key
= channel_key_int
,
1594 .domain
= channel_domain
,
1596 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1597 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1598 int trigger_count
= 0;
1599 struct cds_lfht_iter iter
;
1600 struct session_info
*session_info
= NULL
;
1602 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1603 channel_name
, session_name
, channel_key_int
,
1604 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1606 CDS_INIT_LIST_HEAD(&trigger_list
);
1608 session_info
= find_or_create_session_info(state
, session_name
,
1609 session_uid
, session_gid
);
1610 if (!session_info
) {
1611 /* Allocation error or an internal error occurred. */
1615 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1616 channel_capacity
, session_info
);
1617 if (!new_channel_info
) {
1622 /* Build a list of all triggers applying to the new channel. */
1623 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1625 struct lttng_trigger_list_element
*new_element
;
1627 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1628 new_channel_info
)) {
1632 new_element
= zmalloc(sizeof(*new_element
));
1637 CDS_INIT_LIST_HEAD(&new_element
->node
);
1638 new_element
->trigger
= trigger_ht_element
->trigger
;
1639 cds_list_add(&new_element
->node
, &trigger_list
);
1644 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1646 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1647 if (!channel_trigger_list
) {
1650 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1651 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1652 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1653 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1656 /* Add channel to the channel_ht which owns the channel_infos. */
1657 cds_lfht_add(state
->channels_ht
,
1658 hash_channel_key(&new_channel_info
->key
),
1659 &new_channel_info
->channels_ht_node
);
1661 * Add the list of triggers associated with this channel to the
1662 * channel_triggers_ht.
1664 cds_lfht_add(state
->channel_triggers_ht
,
1665 hash_channel_key(&new_channel_info
->key
),
1666 &channel_trigger_list
->channel_triggers_ht_node
);
1668 session_info_put(session_info
);
1669 *cmd_result
= LTTNG_OK
;
1672 channel_info_destroy(new_channel_info
);
1673 session_info_put(session_info
);
1678 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1680 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1685 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1687 free(caa_container_of(node
, struct channel_state_sample
,
1692 int handle_notification_thread_command_remove_channel(
1693 struct notification_thread_state
*state
,
1694 uint64_t channel_key
, enum lttng_domain_type domain
,
1695 enum lttng_error_code
*cmd_result
)
1697 struct cds_lfht_node
*node
;
1698 struct cds_lfht_iter iter
;
1699 struct lttng_channel_trigger_list
*trigger_list
;
1700 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1701 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1702 struct channel_info
*channel_info
;
1704 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1705 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1709 cds_lfht_lookup(state
->channel_triggers_ht
,
1710 hash_channel_key(&key
),
1711 match_channel_trigger_list
,
1714 node
= cds_lfht_iter_get_node(&iter
);
1716 * There is a severe internal error if we are being asked to remove a
1717 * channel that doesn't exist.
1720 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1724 /* Free the list of triggers associated with this channel. */
1725 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1726 channel_triggers_ht_node
);
1727 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1728 &trigger_list
->list
, node
) {
1729 cds_list_del(&trigger_list_element
->node
);
1730 free(trigger_list_element
);
1732 cds_lfht_del(state
->channel_triggers_ht
, node
);
1733 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1735 /* Free sampled channel state. */
1736 cds_lfht_lookup(state
->channel_state_ht
,
1737 hash_channel_key(&key
),
1738 match_channel_state_sample
,
1741 node
= cds_lfht_iter_get_node(&iter
);
1743 * This is expected to be NULL if the channel is destroyed before we
1744 * received a sample.
1747 struct channel_state_sample
*sample
= caa_container_of(node
,
1748 struct channel_state_sample
,
1749 channel_state_ht_node
);
1751 cds_lfht_del(state
->channel_state_ht
, node
);
1752 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1755 /* Remove the channel from the channels_ht and free it. */
1756 cds_lfht_lookup(state
->channels_ht
,
1757 hash_channel_key(&key
),
1761 node
= cds_lfht_iter_get_node(&iter
);
1763 channel_info
= caa_container_of(node
, struct channel_info
,
1765 cds_lfht_del(state
->channels_ht
, node
);
1766 channel_info_destroy(channel_info
);
1769 *cmd_result
= LTTNG_OK
;
1774 int handle_notification_thread_command_session_rotation(
1775 struct notification_thread_state
*state
,
1776 enum notification_thread_command_type cmd_type
,
1777 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1778 uint64_t trace_archive_chunk_id
,
1779 struct lttng_trace_archive_location
*location
,
1780 enum lttng_error_code
*_cmd_result
)
1783 enum lttng_error_code cmd_result
= LTTNG_OK
;
1784 struct lttng_session_trigger_list
*trigger_list
;
1785 struct lttng_trigger_list_element
*trigger_list_element
;
1786 struct session_info
*session_info
;
1790 session_info
= find_or_create_session_info(state
, session_name
,
1791 session_uid
, session_gid
);
1792 if (!session_info
) {
1793 /* Allocation error or an internal error occurred. */
1795 cmd_result
= LTTNG_ERR_NOMEM
;
1799 session_info
->rotation
.ongoing
=
1800 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1801 session_info
->rotation
.id
= trace_archive_chunk_id
;
1802 trigger_list
= get_session_trigger_list(state
, session_name
);
1803 if (!trigger_list
) {
1804 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1809 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1811 const struct lttng_condition
*condition
;
1812 const struct lttng_action
*action
;
1813 const struct lttng_trigger
*trigger
;
1814 struct notification_client_list
*client_list
;
1815 struct lttng_evaluation
*evaluation
= NULL
;
1816 enum lttng_condition_type condition_type
;
1818 trigger
= trigger_list_element
->trigger
;
1819 condition
= lttng_trigger_get_const_condition(trigger
);
1821 condition_type
= lttng_condition_get_type(condition
);
1823 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1824 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1826 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1827 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1831 action
= lttng_trigger_get_const_action(trigger
);
1833 /* Notify actions are the only type currently supported. */
1834 assert(lttng_action_get_type_const(action
) ==
1835 LTTNG_ACTION_TYPE_NOTIFY
);
1837 client_list
= get_client_list_from_condition(state
, condition
);
1838 assert(client_list
);
1840 if (cds_list_empty(&client_list
->list
)) {
1842 * No clients interested in the evaluation's result,
1848 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1849 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1850 trace_archive_chunk_id
);
1852 evaluation
= lttng_evaluation_session_rotation_completed_create(
1853 trace_archive_chunk_id
, location
);
1857 /* Internal error */
1859 cmd_result
= LTTNG_ERR_UNK
;
1863 /* Dispatch evaluation result to all clients. */
1864 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
1865 evaluation
, client_list
, state
,
1868 lttng_evaluation_destroy(evaluation
);
1869 if (caa_unlikely(ret
)) {
1874 session_info_put(session_info
);
1875 *_cmd_result
= cmd_result
;
1881 int condition_is_supported(struct lttng_condition
*condition
)
1885 switch (lttng_condition_get_type(condition
)) {
1886 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1887 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1889 enum lttng_domain_type domain
;
1891 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1898 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1904 * Older kernel tracers don't expose the API to monitor their
1905 * buffers. Therefore, we reject triggers that require that
1906 * mechanism to be available to be evaluated.
1908 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1918 /* Must be called with RCU read lock held. */
1920 int bind_trigger_to_matching_session(const struct lttng_trigger
*trigger
,
1921 struct notification_thread_state
*state
)
1924 const struct lttng_condition
*condition
;
1925 const char *session_name
;
1926 struct lttng_session_trigger_list
*trigger_list
;
1928 condition
= lttng_trigger_get_const_condition(trigger
);
1929 switch (lttng_condition_get_type(condition
)) {
1930 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1931 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1933 enum lttng_condition_status status
;
1935 status
= lttng_condition_session_rotation_get_session_name(
1936 condition
, &session_name
);
1937 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1938 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
1949 trigger_list
= get_session_trigger_list(state
, session_name
);
1950 if (!trigger_list
) {
1951 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
1957 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
1959 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
1964 /* Must be called with RCU read lock held. */
1966 int bind_trigger_to_matching_channels(const struct lttng_trigger
*trigger
,
1967 struct notification_thread_state
*state
)
1970 struct cds_lfht_node
*node
;
1971 struct cds_lfht_iter iter
;
1972 struct channel_info
*channel
;
1974 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1976 struct lttng_trigger_list_element
*trigger_list_element
;
1977 struct lttng_channel_trigger_list
*trigger_list
;
1978 struct cds_lfht_iter lookup_iter
;
1980 if (!trigger_applies_to_channel(trigger
, channel
)) {
1984 cds_lfht_lookup(state
->channel_triggers_ht
,
1985 hash_channel_key(&channel
->key
),
1986 match_channel_trigger_list
,
1989 node
= cds_lfht_iter_get_node(&lookup_iter
);
1991 trigger_list
= caa_container_of(node
,
1992 struct lttng_channel_trigger_list
,
1993 channel_triggers_ht_node
);
1995 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1996 if (!trigger_list_element
) {
2000 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2001 trigger_list_element
->trigger
= trigger
;
2002 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2003 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2011 * FIXME A client's credentials are not checked when registering a trigger, nor
2012 * are they stored alongside with the trigger.
2014 * The effects of this are benign since:
2015 * - The client will succeed in registering the trigger, as it is valid,
2016 * - The trigger will, internally, be bound to the channel/session,
2017 * - The notifications will not be sent since the client's credentials
2018 * are checked against the channel at that moment.
2020 * If this function returns a non-zero value, it means something is
2021 * fundamentally broken and the whole subsystem/thread will be torn down.
2023 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2027 int handle_notification_thread_command_register_trigger(
2028 struct notification_thread_state
*state
,
2029 struct lttng_trigger
*trigger
,
2030 enum lttng_error_code
*cmd_result
)
2033 struct lttng_condition
*condition
;
2034 struct notification_client
*client
;
2035 struct notification_client_list
*client_list
= NULL
;
2036 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2037 struct notification_client_list_element
*client_list_element
, *tmp
;
2038 struct cds_lfht_node
*node
;
2039 struct cds_lfht_iter iter
;
2040 bool free_trigger
= true;
2044 condition
= lttng_trigger_get_condition(trigger
);
2047 ret
= condition_is_supported(condition
);
2050 } else if (ret
== 0) {
2051 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2054 /* Feature is supported, continue. */
2058 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2059 if (!trigger_ht_element
) {
2064 /* Add trigger to the trigger_ht. */
2065 cds_lfht_node_init(&trigger_ht_element
->node
);
2066 trigger_ht_element
->trigger
= trigger
;
2068 node
= cds_lfht_add_unique(state
->triggers_ht
,
2069 lttng_condition_hash(condition
),
2072 &trigger_ht_element
->node
);
2073 if (node
!= &trigger_ht_element
->node
) {
2074 /* Not a fatal error, simply report it to the client. */
2075 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2076 goto error_free_ht_element
;
2080 * Ownership of the trigger and of its wrapper was transfered to
2083 trigger_ht_element
= NULL
;
2084 free_trigger
= false;
2087 * The rest only applies to triggers that have a "notify" action.
2088 * It is not skipped as this is the only action type currently
2091 client_list
= zmalloc(sizeof(*client_list
));
2094 goto error_free_ht_element
;
2096 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
2097 CDS_INIT_LIST_HEAD(&client_list
->list
);
2098 client_list
->trigger
= trigger
;
2100 /* Build a list of clients to which this new trigger applies. */
2101 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2102 client_socket_ht_node
) {
2103 if (!trigger_applies_to_client(trigger
, client
)) {
2107 client_list_element
= zmalloc(sizeof(*client_list_element
));
2108 if (!client_list_element
) {
2110 goto error_free_client_list
;
2112 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2113 client_list_element
->client
= client
;
2114 cds_list_add(&client_list_element
->node
, &client_list
->list
);
2117 cds_lfht_add(state
->notification_trigger_clients_ht
,
2118 lttng_condition_hash(condition
),
2119 &client_list
->notification_trigger_ht_node
);
2121 switch (get_condition_binding_object(condition
)) {
2122 case LTTNG_OBJECT_TYPE_SESSION
:
2123 /* Add the trigger to the list if it matches a known session. */
2124 ret
= bind_trigger_to_matching_session(trigger
, state
);
2126 goto error_free_client_list
;
2129 case LTTNG_OBJECT_TYPE_CHANNEL
:
2131 * Add the trigger to list of triggers bound to the channels
2134 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2136 goto error_free_client_list
;
2139 case LTTNG_OBJECT_TYPE_NONE
:
2142 ERR("[notification-thread] Unknown object type on which to bind a newly registered trigger was encountered");
2144 goto error_free_client_list
;
2148 * Since there is nothing preventing clients from subscribing to a
2149 * condition before the corresponding trigger is registered, we have
2150 * to evaluate this new condition right away.
2152 * At some point, we were waiting for the next "evaluation" (e.g. on
2153 * reception of a channel sample) to evaluate this new condition, but
2156 * The reason it was broken is that waiting for the next sample
2157 * does not allow us to properly handle transitions for edge-triggered
2160 * Consider this example: when we handle a new channel sample, we
2161 * evaluate each conditions twice: once with the previous state, and
2162 * again with the newest state. We then use those two results to
2163 * determine whether a state change happened: a condition was false and
2164 * became true. If a state change happened, we have to notify clients.
2166 * Now, if a client subscribes to a given notification and registers
2167 * a trigger *after* that subscription, we have to make sure the
2168 * condition is evaluated at this point while considering only the
2169 * current state. Otherwise, the next evaluation cycle may only see
2170 * that the evaluations remain the same (true for samples n-1 and n) and
2171 * the client will never know that the condition has been met.
2173 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2174 &client_list
->list
, node
) {
2175 ret
= evaluate_condition_for_client(trigger
, condition
,
2176 client_list_element
->client
, state
);
2178 goto error_free_client_list
;
2183 * Client list ownership transferred to the
2184 * notification_trigger_clients_ht.
2188 *cmd_result
= LTTNG_OK
;
2189 error_free_client_list
:
2191 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2192 &client_list
->list
, node
) {
2193 free(client_list_element
);
2197 error_free_ht_element
:
2198 free(trigger_ht_element
);
2201 lttng_trigger_destroy(trigger
);
2208 void free_notification_client_list_rcu(struct rcu_head
*node
)
2210 free(caa_container_of(node
, struct notification_client_list
,
2215 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2217 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2222 int handle_notification_thread_command_unregister_trigger(
2223 struct notification_thread_state
*state
,
2224 struct lttng_trigger
*trigger
,
2225 enum lttng_error_code
*_cmd_reply
)
2227 struct cds_lfht_iter iter
;
2228 struct cds_lfht_node
*triggers_ht_node
;
2229 struct lttng_channel_trigger_list
*trigger_list
;
2230 struct notification_client_list
*client_list
;
2231 struct notification_client_list_element
*client_list_element
, *tmp
;
2232 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2233 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2235 enum lttng_error_code cmd_reply
;
2239 cds_lfht_lookup(state
->triggers_ht
,
2240 lttng_condition_hash(condition
),
2244 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2245 if (!triggers_ht_node
) {
2246 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2249 cmd_reply
= LTTNG_OK
;
2252 /* Remove trigger from channel_triggers_ht. */
2253 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2254 channel_triggers_ht_node
) {
2255 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2257 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2258 &trigger_list
->list
, node
) {
2259 const struct lttng_condition
*current_condition
=
2260 lttng_trigger_get_const_condition(
2261 trigger_element
->trigger
);
2263 assert(current_condition
);
2264 if (!lttng_condition_is_equal(condition
,
2265 current_condition
)) {
2269 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2270 cds_list_del(&trigger_element
->node
);
2271 /* A trigger can only appear once per channel */
2277 * Remove and release the client list from
2278 * notification_trigger_clients_ht.
2280 client_list
= get_client_list_from_condition(state
, condition
);
2281 assert(client_list
);
2283 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2284 &client_list
->list
, node
) {
2285 free(client_list_element
);
2287 cds_lfht_del(state
->notification_trigger_clients_ht
,
2288 &client_list
->notification_trigger_ht_node
);
2289 call_rcu(&client_list
->rcu_node
, free_notification_client_list_rcu
);
2291 /* Remove trigger from triggers_ht. */
2292 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2293 struct lttng_trigger_ht_element
, node
);
2294 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2296 /* Release the ownership of the trigger. */
2297 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2298 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2302 *_cmd_reply
= cmd_reply
;
2307 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2308 int handle_notification_thread_command(
2309 struct notification_thread_handle
*handle
,
2310 struct notification_thread_state
*state
)
2314 struct notification_thread_command
*cmd
;
2316 /* Read the event pipe to put it back into a quiescent state. */
2317 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2319 if (ret
!= sizeof(counter
)) {
2323 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2324 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2325 struct notification_thread_command
, cmd_list_node
);
2326 switch (cmd
->type
) {
2327 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2328 DBG("[notification-thread] Received register trigger command");
2329 ret
= handle_notification_thread_command_register_trigger(
2330 state
, cmd
->parameters
.trigger
,
2333 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2334 DBG("[notification-thread] Received unregister trigger command");
2335 ret
= handle_notification_thread_command_unregister_trigger(
2336 state
, cmd
->parameters
.trigger
,
2339 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2340 DBG("[notification-thread] Received add channel command");
2341 ret
= handle_notification_thread_command_add_channel(
2343 cmd
->parameters
.add_channel
.session
.name
,
2344 cmd
->parameters
.add_channel
.session
.uid
,
2345 cmd
->parameters
.add_channel
.session
.gid
,
2346 cmd
->parameters
.add_channel
.channel
.name
,
2347 cmd
->parameters
.add_channel
.channel
.domain
,
2348 cmd
->parameters
.add_channel
.channel
.key
,
2349 cmd
->parameters
.add_channel
.channel
.capacity
,
2352 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2353 DBG("[notification-thread] Received remove channel command");
2354 ret
= handle_notification_thread_command_remove_channel(
2355 state
, cmd
->parameters
.remove_channel
.key
,
2356 cmd
->parameters
.remove_channel
.domain
,
2359 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2360 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2361 DBG("[notification-thread] Received session rotation %s command",
2362 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2363 "ongoing" : "completed");
2364 ret
= handle_notification_thread_command_session_rotation(
2367 cmd
->parameters
.session_rotation
.session_name
,
2368 cmd
->parameters
.session_rotation
.uid
,
2369 cmd
->parameters
.session_rotation
.gid
,
2370 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2371 cmd
->parameters
.session_rotation
.location
,
2374 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2375 DBG("[notification-thread] Received quit command");
2376 cmd
->reply_code
= LTTNG_OK
;
2380 ERR("[notification-thread] Unknown internal command received");
2388 cds_list_del(&cmd
->cmd_list_node
);
2389 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2390 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2393 /* Wake-up and return a fatal error to the calling thread. */
2394 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2395 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2396 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2398 /* Indicate a fatal error to the caller. */
2403 int socket_set_non_blocking(int socket
)
2407 /* Set the pipe as non-blocking. */
2408 ret
= fcntl(socket
, F_GETFL
, 0);
2410 PERROR("fcntl get socket flags");
2415 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2417 PERROR("fcntl set O_NONBLOCK socket flag");
2420 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2426 int client_reset_inbound_state(struct notification_client
*client
)
2430 ret
= lttng_dynamic_buffer_set_size(
2431 &client
->communication
.inbound
.buffer
, 0);
2434 client
->communication
.inbound
.bytes_to_receive
=
2435 sizeof(struct lttng_notification_channel_message
);
2436 client
->communication
.inbound
.msg_type
=
2437 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2438 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2439 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2440 ret
= lttng_dynamic_buffer_set_size(
2441 &client
->communication
.inbound
.buffer
,
2442 client
->communication
.inbound
.bytes_to_receive
);
2446 int handle_notification_thread_client_connect(
2447 struct notification_thread_state
*state
)
2450 struct notification_client
*client
;
2452 DBG("[notification-thread] Handling new notification channel client connection");
2454 client
= zmalloc(sizeof(*client
));
2460 client
->id
= state
->next_notification_client_id
++;
2461 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2462 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
2463 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
2464 client
->communication
.inbound
.expect_creds
= true;
2465 ret
= client_reset_inbound_state(client
);
2467 ERR("[notification-thread] Failed to reset client communication's inbound state");
2472 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2474 ERR("[notification-thread] Failed to accept new notification channel client connection");
2479 client
->socket
= ret
;
2481 ret
= socket_set_non_blocking(client
->socket
);
2483 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2487 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2489 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2494 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2495 LPOLLIN
| LPOLLERR
|
2496 LPOLLHUP
| LPOLLRDHUP
);
2498 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2502 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2506 cds_lfht_add(state
->client_socket_ht
,
2507 hash_client_socket(client
->socket
),
2508 &client
->client_socket_ht_node
);
2509 cds_lfht_add(state
->client_id_ht
,
2510 hash_client_id(client
->id
),
2511 &client
->client_id_ht_node
);
2516 notification_client_destroy(client
, state
);
2520 int handle_notification_thread_client_disconnect(
2522 struct notification_thread_state
*state
)
2525 struct notification_client
*client
;
2528 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2530 client
= get_client_from_socket(client_socket
, state
);
2532 /* Internal state corruption, fatal error. */
2533 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2539 ret
= lttng_poll_del(&state
->events
, client_socket
);
2541 ERR("[notification-thread] Failed to remove client socket from poll set");
2543 cds_lfht_del(state
->client_socket_ht
,
2544 &client
->client_socket_ht_node
);
2545 cds_lfht_del(state
->client_id_ht
,
2546 &client
->client_id_ht_node
);
2547 notification_client_destroy(client
, state
);
2553 int handle_notification_thread_client_disconnect_all(
2554 struct notification_thread_state
*state
)
2556 struct cds_lfht_iter iter
;
2557 struct notification_client
*client
;
2558 bool error_encoutered
= false;
2561 DBG("[notification-thread] Closing all client connections");
2562 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2563 client_socket_ht_node
) {
2566 ret
= handle_notification_thread_client_disconnect(
2567 client
->socket
, state
);
2569 error_encoutered
= true;
2573 return error_encoutered
? 1 : 0;
2576 int handle_notification_thread_trigger_unregister_all(
2577 struct notification_thread_state
*state
)
2579 bool error_occurred
= false;
2580 struct cds_lfht_iter iter
;
2581 struct lttng_trigger_ht_element
*trigger_ht_element
;
2584 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2586 int ret
= handle_notification_thread_command_unregister_trigger(
2587 state
, trigger_ht_element
->trigger
, NULL
);
2589 error_occurred
= true;
2593 return error_occurred
? -1 : 0;
2597 int client_flush_outgoing_queue(struct notification_client
*client
,
2598 struct notification_thread_state
*state
)
2601 size_t to_send_count
;
2603 assert(client
->communication
.outbound
.buffer
.size
!= 0);
2604 to_send_count
= client
->communication
.outbound
.buffer
.size
;
2605 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
2608 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
2609 client
->communication
.outbound
.buffer
.data
,
2611 if ((ret
>= 0 && ret
< to_send_count
)) {
2612 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
2614 to_send_count
-= max(ret
, 0);
2616 memcpy(client
->communication
.outbound
.buffer
.data
,
2617 client
->communication
.outbound
.buffer
.data
+
2618 client
->communication
.outbound
.buffer
.size
- to_send_count
,
2620 ret
= lttng_dynamic_buffer_set_size(
2621 &client
->communication
.outbound
.buffer
,
2628 * We want to be notified whenever there is buffer space
2629 * available to send the rest of the payload.
2631 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2632 CLIENT_POLL_MASK_IN_OUT
);
2636 } else if (ret
< 0) {
2637 /* Generic error, disconnect the client. */
2638 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
2640 ret
= handle_notification_thread_client_disconnect(
2641 client
->socket
, state
);
2646 /* No error and flushed the queue completely. */
2647 ret
= lttng_dynamic_buffer_set_size(
2648 &client
->communication
.outbound
.buffer
, 0);
2652 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2653 CLIENT_POLL_MASK_IN
);
2658 client
->communication
.outbound
.queued_command_reply
= false;
2659 client
->communication
.outbound
.dropped_notification
= false;
2668 int client_send_command_reply(struct notification_client
*client
,
2669 struct notification_thread_state
*state
,
2670 enum lttng_notification_channel_status status
)
2673 struct lttng_notification_channel_command_reply reply
= {
2674 .status
= (int8_t) status
,
2676 struct lttng_notification_channel_message msg
= {
2677 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
2678 .size
= sizeof(reply
),
2680 char buffer
[sizeof(msg
) + sizeof(reply
)];
2682 if (client
->communication
.outbound
.queued_command_reply
) {
2683 /* Protocol error. */
2687 memcpy(buffer
, &msg
, sizeof(msg
));
2688 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
2689 DBG("[notification-thread] Send command reply (%i)", (int) status
);
2691 /* Enqueue buffer to outgoing queue and flush it. */
2692 ret
= lttng_dynamic_buffer_append(
2693 &client
->communication
.outbound
.buffer
,
2694 buffer
, sizeof(buffer
));
2699 ret
= client_flush_outgoing_queue(client
, state
);
2704 if (client
->communication
.outbound
.buffer
.size
!= 0) {
2705 /* Queue could not be emptied. */
2706 client
->communication
.outbound
.queued_command_reply
= true;
2715 int client_dispatch_message(struct notification_client
*client
,
2716 struct notification_thread_state
*state
)
2720 if (client
->communication
.inbound
.msg_type
!=
2721 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
2722 client
->communication
.inbound
.msg_type
!=
2723 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
2724 !client
->validated
) {
2725 WARN("[notification-thread] client attempted a command before handshake");
2730 switch (client
->communication
.inbound
.msg_type
) {
2731 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
2734 * Receiving message header. The function will be called again
2735 * once the rest of the message as been received and can be
2738 const struct lttng_notification_channel_message
*msg
;
2740 assert(sizeof(*msg
) ==
2741 client
->communication
.inbound
.buffer
.size
);
2742 msg
= (const struct lttng_notification_channel_message
*)
2743 client
->communication
.inbound
.buffer
.data
;
2745 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
2746 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
2751 switch (msg
->type
) {
2752 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2753 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2754 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2758 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
2762 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
2763 client
->communication
.inbound
.msg_type
=
2764 (enum lttng_notification_channel_message_type
) msg
->type
;
2765 ret
= lttng_dynamic_buffer_set_size(
2766 &client
->communication
.inbound
.buffer
, msg
->size
);
2772 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2774 struct lttng_notification_channel_command_handshake
*handshake_client
;
2775 struct lttng_notification_channel_command_handshake handshake_reply
= {
2776 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
2777 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2779 struct lttng_notification_channel_message msg_header
= {
2780 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2781 .size
= sizeof(handshake_reply
),
2783 enum lttng_notification_channel_status status
=
2784 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2785 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2787 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2788 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2789 sizeof(handshake_reply
));
2792 (struct lttng_notification_channel_command_handshake
*)
2793 client
->communication
.inbound
.buffer
.data
;
2794 client
->major
= handshake_client
->major
;
2795 client
->minor
= handshake_client
->minor
;
2796 if (!client
->communication
.inbound
.creds_received
) {
2797 ERR("[notification-thread] No credentials received from client");
2802 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2803 &client
->communication
.inbound
.creds
);
2804 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2805 &client
->communication
.inbound
.creds
);
2806 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2807 client
->uid
, client
->gid
, (int) client
->major
,
2808 (int) client
->minor
);
2810 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2811 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2814 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2815 send_buffer
, sizeof(send_buffer
));
2817 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2821 ret
= client_flush_outgoing_queue(client
, state
);
2826 ret
= client_send_command_reply(client
, state
, status
);
2828 ERR("[notification-thread] Failed to send reply to notification channel client");
2832 /* Set reception state to receive the next message header. */
2833 ret
= client_reset_inbound_state(client
);
2835 ERR("[notification-thread] Failed to reset client communication's inbound state");
2838 client
->validated
= true;
2841 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2842 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2844 struct lttng_condition
*condition
;
2845 enum lttng_notification_channel_status status
=
2846 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2847 struct lttng_payload_view condition_view
=
2848 lttng_payload_view_from_dynamic_buffer(
2849 &client
->communication
.inbound
.buffer
,
2851 size_t expected_condition_size
=
2852 client
->communication
.inbound
.buffer
.size
;
2854 ret
= lttng_condition_create_from_payload(&condition_view
,
2856 if (ret
!= expected_condition_size
) {
2857 ERR("[notification-thread] Malformed condition received from client");
2861 if (client
->communication
.inbound
.msg_type
==
2862 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2863 ret
= notification_thread_client_subscribe(client
,
2864 condition
, state
, &status
);
2866 ret
= notification_thread_client_unsubscribe(client
,
2867 condition
, state
, &status
);
2873 ret
= client_send_command_reply(client
, state
, status
);
2875 ERR("[notification-thread] Failed to send reply to notification channel client");
2879 /* Set reception state to receive the next message header. */
2880 ret
= client_reset_inbound_state(client
);
2882 ERR("[notification-thread] Failed to reset client communication's inbound state");
2894 /* Incoming data from client. */
2895 int handle_notification_thread_client_in(
2896 struct notification_thread_state
*state
, int socket
)
2899 struct notification_client
*client
;
2903 client
= get_client_from_socket(socket
, state
);
2905 /* Internal error, abort. */
2910 offset
= client
->communication
.inbound
.buffer
.size
-
2911 client
->communication
.inbound
.bytes_to_receive
;
2912 if (client
->communication
.inbound
.expect_creds
) {
2913 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2914 client
->communication
.inbound
.buffer
.data
+ offset
,
2915 client
->communication
.inbound
.bytes_to_receive
,
2916 &client
->communication
.inbound
.creds
);
2918 client
->communication
.inbound
.expect_creds
= false;
2919 client
->communication
.inbound
.creds_received
= true;
2922 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2923 client
->communication
.inbound
.buffer
.data
+ offset
,
2924 client
->communication
.inbound
.bytes_to_receive
);
2927 goto error_disconnect_client
;
2930 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2931 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2932 ret
= client_dispatch_message(client
, state
);
2935 * Only returns an error if this client must be
2938 goto error_disconnect_client
;
2945 error_disconnect_client
:
2946 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2950 /* Client ready to receive outgoing data. */
2951 int handle_notification_thread_client_out(
2952 struct notification_thread_state
*state
, int socket
)
2955 struct notification_client
*client
;
2957 client
= get_client_from_socket(socket
, state
);
2959 /* Internal error, abort. */
2964 ret
= client_flush_outgoing_queue(client
, state
);
2973 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2974 const struct channel_state_sample
*sample
,
2975 uint64_t buffer_capacity
)
2977 bool result
= false;
2979 enum lttng_condition_type condition_type
;
2980 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2981 condition
, struct lttng_condition_buffer_usage
,
2984 if (use_condition
->threshold_bytes
.set
) {
2985 threshold
= use_condition
->threshold_bytes
.value
;
2988 * Threshold was expressed as a ratio.
2990 * TODO the threshold (in bytes) of conditions expressed
2991 * as a ratio of total buffer size could be cached to
2992 * forego this double-multiplication or it could be performed
2993 * as fixed-point math.
2995 * Note that caching should accommodates the case where the
2996 * condition applies to multiple channels (i.e. don't assume
2997 * that all channels matching my_chann* have the same size...)
2999 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3000 (double) buffer_capacity
);
3003 condition_type
= lttng_condition_get_type(condition
);
3004 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3005 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3006 threshold
, sample
->highest_usage
);
3009 * The low condition should only be triggered once _all_ of the
3010 * streams in a channel have gone below the "low" threshold.
3012 if (sample
->highest_usage
<= threshold
) {
3016 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3017 threshold
, sample
->highest_usage
);
3020 * For high buffer usage scenarios, we want to trigger whenever
3021 * _any_ of the streams has reached the "high" threshold.
3023 if (sample
->highest_usage
>= threshold
) {
3032 bool evaluate_session_consumed_size_condition(
3033 const struct lttng_condition
*condition
,
3034 uint64_t session_consumed_size
)
3037 const struct lttng_condition_session_consumed_size
*size_condition
=
3038 container_of(condition
,
3039 struct lttng_condition_session_consumed_size
,
3042 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3043 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3044 threshold
, session_consumed_size
);
3045 return session_consumed_size
>= threshold
;
3049 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3050 struct lttng_evaluation
**evaluation
,
3051 const struct notification_thread_state
*state
,
3052 const struct channel_state_sample
*previous_sample
,
3053 const struct channel_state_sample
*latest_sample
,
3054 uint64_t previous_session_consumed_total
,
3055 uint64_t latest_session_consumed_total
,
3056 struct channel_info
*channel_info
)
3059 enum lttng_condition_type condition_type
;
3060 const bool previous_sample_available
= !!previous_sample
;
3061 bool previous_sample_result
= false;
3062 bool latest_sample_result
;
3064 condition_type
= lttng_condition_get_type(condition
);
3066 switch (condition_type
) {
3067 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3068 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3069 if (caa_likely(previous_sample_available
)) {
3070 previous_sample_result
=
3071 evaluate_buffer_usage_condition(condition
,
3072 previous_sample
, channel_info
->capacity
);
3074 latest_sample_result
= evaluate_buffer_usage_condition(
3075 condition
, latest_sample
,
3076 channel_info
->capacity
);
3078 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3079 if (caa_likely(previous_sample_available
)) {
3080 previous_sample_result
=
3081 evaluate_session_consumed_size_condition(
3083 previous_session_consumed_total
);
3085 latest_sample_result
=
3086 evaluate_session_consumed_size_condition(
3088 latest_session_consumed_total
);
3091 /* Unknown condition type; internal error. */
3095 if (!latest_sample_result
||
3096 (previous_sample_result
== latest_sample_result
)) {
3098 * Only trigger on a condition evaluation transition.
3100 * NOTE: This edge-triggered logic may not be appropriate for
3101 * future condition types.
3106 if (!evaluation
|| !latest_sample_result
) {
3110 switch (condition_type
) {
3111 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3112 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3113 *evaluation
= lttng_evaluation_buffer_usage_create(
3115 latest_sample
->highest_usage
,
3116 channel_info
->capacity
);
3118 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3119 *evaluation
= lttng_evaluation_session_consumed_size_create(
3120 latest_session_consumed_total
);
3135 int client_enqueue_dropped_notification(struct notification_client
*client
)
3138 struct lttng_notification_channel_message msg
= {
3139 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3143 ret
= lttng_dynamic_buffer_append(
3144 &client
->communication
.outbound
.buffer
, &msg
,
3150 * Permission checks relative to notification channel clients are performed
3151 * here. Notice how object, client, and trigger credentials are involved in
3154 * The `object` credentials are the credentials associated with the "subject"
3155 * of a condition. For instance, a `rotation completed` condition applies
3156 * to a session. When that condition is met, it will produce an evaluation
3157 * against a session. Hence, in this case, the `object` credentials are the
3158 * credentials of the "subject" session.
3160 * The `trigger` credentials are the credentials of the user that registered the
3163 * The `client` credentials are the credentials of the user that created a given
3164 * notification channel.
3166 * In terms of visibility, it is expected that non-privilieged users can only
3167 * register triggers against "their" objects (their own sessions and
3168 * applications they are allowed to interact with). They can then open a
3169 * notification channel and subscribe to notifications associated with those
3172 * As for privilieged users, they can register triggers against the objects of
3173 * other users. They can then subscribe to the notifications associated to their
3174 * triggers. Privilieged users _can't_ subscribe to the notifications of
3175 * triggers owned by other users; they must create their own triggers.
3177 * This is more a concern of usability than security. It would be difficult for
3178 * a root user reliably subscribe to a specific set of conditions without
3179 * interference from external users (those could, for instance, unregister
3183 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3184 const struct lttng_evaluation
*evaluation
,
3185 struct notification_client_list
* client_list
,
3186 struct notification_thread_state
*state
,
3187 uid_t object_uid
, gid_t object_gid
)
3190 struct lttng_payload msg_payload
;
3191 struct notification_client_list_element
*client_list_element
, *tmp
;
3192 const struct lttng_notification notification
= {
3193 .condition
= (struct lttng_condition
*) lttng_trigger_get_const_condition(trigger
),
3194 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3196 struct lttng_notification_channel_message msg_header
= {
3197 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3199 const struct lttng_credentials
*trigger_creds
= lttng_trigger_get_credentials(trigger
);
3201 lttng_payload_init(&msg_payload
);
3203 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
3204 sizeof(msg_header
));
3209 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
3211 ERR("[notification-thread] Failed to serialize notification");
3216 /* Update payload size. */
3217 ((struct lttng_notification_channel_message
* ) msg_payload
.buffer
.data
)->size
=
3218 (uint32_t) (msg_payload
.buffer
.size
- sizeof(msg_header
));
3220 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3221 &client_list
->list
, node
) {
3222 struct notification_client
*client
=
3223 client_list_element
->client
;
3225 if (client
->uid
!= object_uid
&& client
->gid
!= object_gid
&&
3227 /* Client is not allowed to monitor this channel. */
3228 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
3232 if (client
->uid
!= trigger_creds
->uid
&& client
->gid
!= trigger_creds
->gid
) {
3233 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
3237 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3238 client
->socket
, msg_payload
.buffer
.size
);
3239 if (client
->communication
.outbound
.buffer
.size
) {
3241 * Outgoing data is already buffered for this client;
3242 * drop the notification and enqueue a "dropped
3243 * notification" message if this is the first dropped
3244 * notification since the socket spilled-over to the
3247 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
3249 if (!client
->communication
.outbound
.dropped_notification
) {
3250 client
->communication
.outbound
.dropped_notification
= true;
3251 ret
= client_enqueue_dropped_notification(
3260 ret
= lttng_dynamic_buffer_append_buffer(
3261 &client
->communication
.outbound
.buffer
,
3262 &msg_payload
.buffer
);
3267 ret
= client_flush_outgoing_queue(client
, state
);
3274 lttng_payload_reset(&msg_payload
);
3278 int handle_notification_thread_channel_sample(
3279 struct notification_thread_state
*state
, int pipe
,
3280 enum lttng_domain_type domain
)
3283 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
3284 struct channel_info
*channel_info
;
3285 struct cds_lfht_node
*node
;
3286 struct cds_lfht_iter iter
;
3287 struct lttng_channel_trigger_list
*trigger_list
;
3288 struct lttng_trigger_list_element
*trigger_list_element
;
3289 bool previous_sample_available
= false;
3290 struct channel_state_sample previous_sample
, latest_sample
;
3291 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
3294 * The monitoring pipe only holds messages smaller than PIPE_BUF,
3295 * ensuring that read/write of sampling messages are atomic.
3297 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
3298 if (ret
!= sizeof(sample_msg
)) {
3299 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
3306 latest_sample
.key
.key
= sample_msg
.key
;
3307 latest_sample
.key
.domain
= domain
;
3308 latest_sample
.highest_usage
= sample_msg
.highest
;
3309 latest_sample
.lowest_usage
= sample_msg
.lowest
;
3310 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
3314 /* Retrieve the channel's informations */
3315 cds_lfht_lookup(state
->channels_ht
,
3316 hash_channel_key(&latest_sample
.key
),
3320 node
= cds_lfht_iter_get_node(&iter
);
3321 if (caa_unlikely(!node
)) {
3323 * Not an error since the consumer can push a sample to the pipe
3324 * and the rest of the session daemon could notify us of the
3325 * channel's destruction before we get a chance to process that
3328 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
3329 latest_sample
.key
.key
,
3330 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
3334 channel_info
= caa_container_of(node
, struct channel_info
,
3336 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
3338 latest_sample
.key
.key
,
3339 channel_info
->session_info
->name
,
3340 latest_sample
.highest_usage
,
3341 latest_sample
.lowest_usage
,
3342 latest_sample
.channel_total_consumed
);
3344 previous_session_consumed_total
=
3345 channel_info
->session_info
->consumed_data_size
;
3347 /* Retrieve the channel's last sample, if it exists, and update it. */
3348 cds_lfht_lookup(state
->channel_state_ht
,
3349 hash_channel_key(&latest_sample
.key
),
3350 match_channel_state_sample
,
3353 node
= cds_lfht_iter_get_node(&iter
);
3354 if (caa_likely(node
)) {
3355 struct channel_state_sample
*stored_sample
;
3357 /* Update the sample stored. */
3358 stored_sample
= caa_container_of(node
,
3359 struct channel_state_sample
,
3360 channel_state_ht_node
);
3362 memcpy(&previous_sample
, stored_sample
,
3363 sizeof(previous_sample
));
3364 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
3365 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
3366 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
3367 previous_sample_available
= true;
3369 latest_session_consumed_total
=
3370 previous_session_consumed_total
+
3371 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
3374 * This is the channel's first sample, allocate space for and
3375 * store the new sample.
3377 struct channel_state_sample
*stored_sample
;
3379 stored_sample
= zmalloc(sizeof(*stored_sample
));
3380 if (!stored_sample
) {
3385 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
3386 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
3387 cds_lfht_add(state
->channel_state_ht
,
3388 hash_channel_key(&stored_sample
->key
),
3389 &stored_sample
->channel_state_ht_node
);
3391 latest_session_consumed_total
=
3392 previous_session_consumed_total
+
3393 latest_sample
.channel_total_consumed
;
3396 channel_info
->session_info
->consumed_data_size
=
3397 latest_session_consumed_total
;
3399 /* Find triggers associated with this channel. */
3400 cds_lfht_lookup(state
->channel_triggers_ht
,
3401 hash_channel_key(&latest_sample
.key
),
3402 match_channel_trigger_list
,
3405 node
= cds_lfht_iter_get_node(&iter
);
3406 if (caa_likely(!node
)) {
3410 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
3411 channel_triggers_ht_node
);
3412 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
3414 const struct lttng_condition
*condition
;
3415 const struct lttng_action
*action
;
3416 const struct lttng_trigger
*trigger
;
3417 struct notification_client_list
*client_list
;
3418 struct lttng_evaluation
*evaluation
= NULL
;
3420 trigger
= trigger_list_element
->trigger
;
3421 condition
= lttng_trigger_get_const_condition(trigger
);
3423 action
= lttng_trigger_get_const_action(trigger
);
3425 /* Notify actions are the only type currently supported. */
3426 assert(lttng_action_get_type_const(action
) ==
3427 LTTNG_ACTION_TYPE_NOTIFY
);
3430 * Check if any client is subscribed to the result of this
3433 client_list
= get_client_list_from_condition(state
, condition
);
3434 assert(client_list
);
3435 if (cds_list_empty(&client_list
->list
)) {
3437 * No clients interested in the evaluation's result,
3443 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
3444 previous_sample_available
? &previous_sample
: NULL
,
3446 previous_session_consumed_total
,
3447 latest_session_consumed_total
,
3449 if (caa_unlikely(ret
)) {
3453 if (caa_likely(!evaluation
)) {
3457 /* Dispatch evaluation result to all clients. */
3458 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
3459 evaluation
, client_list
, state
,
3460 channel_info
->session_info
->uid
,
3461 channel_info
->session_info
->gid
);
3462 lttng_evaluation_destroy(evaluation
);
3463 if (caa_unlikely(ret
)) {