2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <urcu/rculfhash.h>
22 #include <common/defaults.h>
23 #include <common/error.h>
24 #include <common/futex.h>
25 #include <common/unix.h>
26 #include <common/dynamic-buffer.h>
27 #include <common/hashtable/utils.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/macros.h>
30 #include <lttng/condition/condition.h>
31 #include <lttng/action/action-internal.h>
32 #include <lttng/notification/notification-internal.h>
33 #include <lttng/condition/condition-internal.h>
34 #include <lttng/condition/buffer-usage-internal.h>
35 #include <lttng/condition/session-consumed-size-internal.h>
36 #include <lttng/condition/session-rotation-internal.h>
37 #include <lttng/notification/channel-internal.h>
45 #include "notification-thread.h"
46 #include "notification-thread-events.h"
47 #include "notification-thread-commands.h"
48 #include "lttng-sessiond.h"
51 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
52 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
54 enum lttng_object_type
{
55 LTTNG_OBJECT_TYPE_UNKNOWN
,
56 LTTNG_OBJECT_TYPE_NONE
,
57 LTTNG_OBJECT_TYPE_CHANNEL
,
58 LTTNG_OBJECT_TYPE_SESSION
,
61 struct lttng_trigger_list_element
{
62 /* No ownership of the trigger object is assumed. */
63 const struct lttng_trigger
*trigger
;
64 struct cds_list_head node
;
67 struct lttng_channel_trigger_list
{
68 struct channel_key channel_key
;
69 /* List of struct lttng_trigger_list_element. */
70 struct cds_list_head list
;
71 /* Node in the channel_triggers_ht */
72 struct cds_lfht_node channel_triggers_ht_node
;
76 * List of triggers applying to a given session.
79 * - lttng_session_trigger_list_create()
80 * - lttng_session_trigger_list_build()
81 * - lttng_session_trigger_list_destroy()
82 * - lttng_session_trigger_list_add()
84 struct lttng_session_trigger_list
{
86 * Not owned by this; points to the session_info structure's
89 const char *session_name
;
90 /* List of struct lttng_trigger_list_element. */
91 struct cds_list_head list
;
92 /* Node in the session_triggers_ht */
93 struct cds_lfht_node session_triggers_ht_node
;
95 * Weak reference to the notification system's session triggers
98 * The session trigger list structure structure is owned by
99 * the session's session_info.
101 * The session_info is kept alive the the channel_infos holding a
102 * reference to it (reference counting). When those channels are
103 * destroyed (at runtime or on teardown), the reference they hold
104 * to the session_info are released. On destruction of session_info,
105 * session_info_destroy() will remove the list of triggers applying
106 * to this session from the notification system's state.
108 * This implies that the session_triggers_ht must be destroyed
109 * after the channels.
111 struct cds_lfht
*session_triggers_ht
;
112 /* Used for delayed RCU reclaim. */
113 struct rcu_head rcu_node
;
116 struct lttng_trigger_ht_element
{
117 struct lttng_trigger
*trigger
;
118 struct cds_lfht_node node
;
121 struct lttng_condition_list_element
{
122 struct lttng_condition
*condition
;
123 struct cds_list_head node
;
126 struct notification_client_list_element
{
127 struct notification_client
*client
;
128 struct cds_list_head node
;
131 struct notification_client_list
{
132 const struct lttng_trigger
*trigger
;
133 struct cds_list_head list
;
134 struct cds_lfht_node notification_trigger_ht_node
;
137 struct notification_client
{
139 /* Client protocol version. */
140 uint8_t major
, minor
;
144 * Indicates if the credentials and versions of the client have been
149 * Conditions to which the client's notification channel is subscribed.
150 * List of struct lttng_condition_list_node. The condition member is
151 * owned by the client.
153 struct cds_list_head condition_list
;
154 struct cds_lfht_node client_socket_ht_node
;
158 * During the reception of a message, the reception
159 * buffers' "size" is set to contain the current
160 * message's complete payload.
162 struct lttng_dynamic_buffer buffer
;
163 /* Bytes left to receive for the current message. */
164 size_t bytes_to_receive
;
165 /* Type of the message being received. */
166 enum lttng_notification_channel_message_type msg_type
;
168 * Indicates whether or not credentials are expected
173 * Indicates whether or not credentials were received
177 /* Only used during credentials reception. */
178 lttng_sock_cred creds
;
182 * Indicates whether or not a notification addressed to
183 * this client was dropped because a command reply was
186 * A notification is dropped whenever the buffer is not
189 bool dropped_notification
;
191 * Indicates whether or not a command reply is already
192 * buffered. In this case, it means that the client is
193 * not consuming command replies before emitting a new
194 * one. This could be caused by a protocol error or a
195 * misbehaving/malicious client.
197 bool queued_command_reply
;
198 struct lttng_dynamic_buffer buffer
;
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
;
211 static unsigned long hash_channel_key(struct channel_key
*key
);
212 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
213 struct lttng_evaluation
**evaluation
,
214 const struct notification_thread_state
*state
,
215 const struct channel_state_sample
*previous_sample
,
216 const struct channel_state_sample
*latest_sample
,
217 uint64_t previous_session_consumed_total
,
218 uint64_t latest_session_consumed_total
,
219 struct channel_info
*channel_info
);
221 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
222 const struct lttng_evaluation
*evaluation
,
223 struct notification_client_list
*client_list
,
224 struct notification_thread_state
*state
,
225 uid_t channel_uid
, gid_t channel_gid
);
228 /* session_info API */
230 void session_info_destroy(void *_data
);
232 void session_info_get(struct session_info
*session_info
);
234 void session_info_put(struct session_info
*session_info
);
236 struct session_info
*session_info_create(const char *name
,
237 uid_t uid
, gid_t gid
,
238 struct lttng_session_trigger_list
*trigger_list
,
239 struct cds_lfht
*sessions_ht
);
241 void session_info_add_channel(struct session_info
*session_info
,
242 struct channel_info
*channel_info
);
244 void session_info_remove_channel(struct session_info
*session_info
,
245 struct channel_info
*channel_info
);
247 /* lttng_session_trigger_list API */
249 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
250 const char *session_name
,
251 struct cds_lfht
*session_triggers_ht
);
253 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
254 const struct notification_thread_state
*state
,
255 const char *session_name
);
257 void lttng_session_trigger_list_destroy(
258 struct lttng_session_trigger_list
*list
);
260 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
261 const struct lttng_trigger
*trigger
);
265 int match_client(struct cds_lfht_node
*node
, const void *key
)
267 /* This double-cast is intended to supress pointer-to-cast warning. */
268 int socket
= (int) (intptr_t) key
;
269 struct notification_client
*client
;
271 client
= caa_container_of(node
, struct notification_client
,
272 client_socket_ht_node
);
274 return !!(client
->socket
== socket
);
278 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
280 struct channel_key
*channel_key
= (struct channel_key
*) key
;
281 struct lttng_channel_trigger_list
*trigger_list
;
283 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
284 channel_triggers_ht_node
);
286 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
287 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
291 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
293 const char *session_name
= (const char *) key
;
294 struct lttng_session_trigger_list
*trigger_list
;
296 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
297 session_triggers_ht_node
);
299 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
303 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
305 struct channel_key
*channel_key
= (struct channel_key
*) key
;
306 struct channel_state_sample
*sample
;
308 sample
= caa_container_of(node
, struct channel_state_sample
,
309 channel_state_ht_node
);
311 return !!((channel_key
->key
== sample
->key
.key
) &&
312 (channel_key
->domain
== sample
->key
.domain
));
316 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
318 struct channel_key
*channel_key
= (struct channel_key
*) key
;
319 struct channel_info
*channel_info
;
321 channel_info
= caa_container_of(node
, struct channel_info
,
324 return !!((channel_key
->key
== channel_info
->key
.key
) &&
325 (channel_key
->domain
== channel_info
->key
.domain
));
329 int match_condition(struct cds_lfht_node
*node
, const void *key
)
331 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
332 struct lttng_trigger_ht_element
*trigger
;
333 struct lttng_condition
*condition
;
335 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
337 condition
= lttng_trigger_get_condition(trigger
->trigger
);
340 return !!lttng_condition_is_equal(condition_key
, condition
);
344 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
346 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
347 struct notification_client_list
*client_list
;
348 const struct lttng_condition
*condition
;
350 assert(condition_key
);
352 client_list
= caa_container_of(node
, struct notification_client_list
,
353 notification_trigger_ht_node
);
354 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
356 return !!lttng_condition_is_equal(condition_key
, condition
);
360 int match_session(struct cds_lfht_node
*node
, const void *key
)
362 const char *name
= key
;
363 struct session_info
*session_info
= caa_container_of(
364 node
, struct session_info
, sessions_ht_node
);
366 return !strcmp(session_info
->name
, name
);
370 unsigned long lttng_condition_buffer_usage_hash(
371 const struct lttng_condition
*_condition
)
374 unsigned long condition_type
;
375 struct lttng_condition_buffer_usage
*condition
;
377 condition
= container_of(_condition
,
378 struct lttng_condition_buffer_usage
, parent
);
380 condition_type
= (unsigned long) condition
->parent
.type
;
381 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
382 if (condition
->session_name
) {
383 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
385 if (condition
->channel_name
) {
386 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
388 if (condition
->domain
.set
) {
389 hash
^= hash_key_ulong(
390 (void *) condition
->domain
.type
,
393 if (condition
->threshold_ratio
.set
) {
396 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
397 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
398 } else if (condition
->threshold_bytes
.set
) {
401 val
= condition
->threshold_bytes
.value
;
402 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
408 unsigned long lttng_condition_session_consumed_size_hash(
409 const struct lttng_condition
*_condition
)
412 unsigned long condition_type
=
413 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
414 struct lttng_condition_session_consumed_size
*condition
;
417 condition
= container_of(_condition
,
418 struct lttng_condition_session_consumed_size
, parent
);
420 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
421 if (condition
->session_name
) {
422 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
424 val
= condition
->consumed_threshold_bytes
.value
;
425 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
430 unsigned long lttng_condition_session_rotation_hash(
431 const struct lttng_condition
*_condition
)
433 unsigned long hash
, condition_type
;
434 struct lttng_condition_session_rotation
*condition
;
436 condition
= container_of(_condition
,
437 struct lttng_condition_session_rotation
, parent
);
438 condition_type
= (unsigned long) condition
->parent
.type
;
439 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
440 assert(condition
->session_name
);
441 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
446 * The lttng_condition hashing code is kept in this file (rather than
447 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
448 * don't want to link in liblttng-ctl.
451 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
453 switch (condition
->type
) {
454 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
455 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
456 return lttng_condition_buffer_usage_hash(condition
);
457 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
458 return lttng_condition_session_consumed_size_hash(condition
);
459 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
460 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
461 return lttng_condition_session_rotation_hash(condition
);
463 ERR("[notification-thread] Unexpected condition type caught");
469 unsigned long hash_channel_key(struct channel_key
*key
)
471 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
472 unsigned long domain_hash
= hash_key_ulong(
473 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
475 return key_hash
^ domain_hash
;
479 * Get the type of object to which a given condition applies. Bindings let
480 * the notification system evaluate a trigger's condition when a given
481 * object's state is updated.
483 * For instance, a condition bound to a channel will be evaluated everytime
484 * the channel's state is changed by a channel monitoring sample.
487 enum lttng_object_type
get_condition_binding_object(
488 const struct lttng_condition
*condition
)
490 switch (lttng_condition_get_type(condition
)) {
491 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
492 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
493 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
494 return LTTNG_OBJECT_TYPE_CHANNEL
;
495 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
496 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
497 return LTTNG_OBJECT_TYPE_SESSION
;
499 return LTTNG_OBJECT_TYPE_UNKNOWN
;
504 void channel_info_destroy(struct channel_info
*channel_info
)
510 if (channel_info
->session_info
) {
511 session_info_remove_channel(channel_info
->session_info
,
513 session_info_put(channel_info
->session_info
);
515 if (channel_info
->name
) {
516 free(channel_info
->name
);
521 /* Don't call directly, use the ref-counting mechanism. */
523 void session_info_destroy(void *_data
)
525 struct session_info
*session_info
= _data
;
528 assert(session_info
);
529 if (session_info
->channel_infos_ht
) {
530 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
532 ERR("[notification-thread] Failed to destroy channel information hash table");
535 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
538 cds_lfht_del(session_info
->sessions_ht
,
539 &session_info
->sessions_ht_node
);
541 free(session_info
->name
);
546 void session_info_get(struct session_info
*session_info
)
551 lttng_ref_get(&session_info
->ref
);
555 void session_info_put(struct session_info
*session_info
)
560 lttng_ref_put(&session_info
->ref
);
564 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
565 struct lttng_session_trigger_list
*trigger_list
,
566 struct cds_lfht
*sessions_ht
)
568 struct session_info
*session_info
;
572 session_info
= zmalloc(sizeof(*session_info
));
576 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
578 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
579 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
580 if (!session_info
->channel_infos_ht
) {
584 cds_lfht_node_init(&session_info
->sessions_ht_node
);
585 session_info
->name
= strdup(name
);
586 if (!session_info
->name
) {
589 session_info
->uid
= uid
;
590 session_info
->gid
= gid
;
591 session_info
->trigger_list
= trigger_list
;
592 session_info
->sessions_ht
= sessions_ht
;
596 session_info_put(session_info
);
601 void session_info_add_channel(struct session_info
*session_info
,
602 struct channel_info
*channel_info
)
605 cds_lfht_add(session_info
->channel_infos_ht
,
606 hash_channel_key(&channel_info
->key
),
607 &channel_info
->session_info_channels_ht_node
);
612 void session_info_remove_channel(struct session_info
*session_info
,
613 struct channel_info
*channel_info
)
616 cds_lfht_del(session_info
->channel_infos_ht
,
617 &channel_info
->session_info_channels_ht_node
);
622 struct channel_info
*channel_info_create(const char *channel_name
,
623 struct channel_key
*channel_key
, uint64_t channel_capacity
,
624 struct session_info
*session_info
)
626 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
632 cds_lfht_node_init(&channel_info
->channels_ht_node
);
633 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
634 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
635 channel_info
->capacity
= channel_capacity
;
637 channel_info
->name
= strdup(channel_name
);
638 if (!channel_info
->name
) {
643 * Set the references between session and channel infos:
644 * - channel_info holds a strong reference to session_info
645 * - session_info holds a weak reference to channel_info
647 session_info_get(session_info
);
648 session_info_add_channel(session_info
, channel_info
);
649 channel_info
->session_info
= session_info
;
653 channel_info_destroy(channel_info
);
657 /* RCU read lock must be held by the caller. */
659 struct notification_client_list
*get_client_list_from_condition(
660 struct notification_thread_state
*state
,
661 const struct lttng_condition
*condition
)
663 struct cds_lfht_node
*node
;
664 struct cds_lfht_iter iter
;
666 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
667 lttng_condition_hash(condition
),
668 match_client_list_condition
,
671 node
= cds_lfht_iter_get_node(&iter
);
673 return node
? caa_container_of(node
,
674 struct notification_client_list
,
675 notification_trigger_ht_node
) : NULL
;
678 /* This function must be called with the RCU read lock held. */
680 int evaluate_channel_condition_for_client(
681 const struct lttng_condition
*condition
,
682 struct notification_thread_state
*state
,
683 struct lttng_evaluation
**evaluation
,
684 uid_t
*session_uid
, gid_t
*session_gid
)
687 struct cds_lfht_iter iter
;
688 struct cds_lfht_node
*node
;
689 struct channel_info
*channel_info
= NULL
;
690 struct channel_key
*channel_key
= NULL
;
691 struct channel_state_sample
*last_sample
= NULL
;
692 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
694 /* Find the channel associated with the condition. */
695 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
696 channel_trigger_list
, channel_triggers_ht_node
) {
697 struct lttng_trigger_list_element
*element
;
699 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
700 const struct lttng_condition
*current_condition
=
701 lttng_trigger_get_const_condition(
704 assert(current_condition
);
705 if (!lttng_condition_is_equal(condition
,
706 current_condition
)) {
710 /* Found the trigger, save the channel key. */
711 channel_key
= &channel_trigger_list
->channel_key
;
715 /* The channel key was found stop iteration. */
721 /* No channel found; normal exit. */
722 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
727 /* Fetch channel info for the matching channel. */
728 cds_lfht_lookup(state
->channels_ht
,
729 hash_channel_key(channel_key
),
733 node
= cds_lfht_iter_get_node(&iter
);
735 channel_info
= caa_container_of(node
, struct channel_info
,
738 /* Retrieve the channel's last sample, if it exists. */
739 cds_lfht_lookup(state
->channel_state_ht
,
740 hash_channel_key(channel_key
),
741 match_channel_state_sample
,
744 node
= cds_lfht_iter_get_node(&iter
);
746 last_sample
= caa_container_of(node
,
747 struct channel_state_sample
,
748 channel_state_ht_node
);
750 /* Nothing to evaluate, no sample was ever taken. Normal exit */
751 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
756 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
758 0, channel_info
->session_info
->consumed_data_size
,
761 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
765 *session_uid
= channel_info
->session_info
->uid
;
766 *session_gid
= channel_info
->session_info
->gid
;
772 const char *get_condition_session_name(const struct lttng_condition
*condition
)
774 const char *session_name
= NULL
;
775 enum lttng_condition_status status
;
777 switch (lttng_condition_get_type(condition
)) {
778 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
779 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
780 status
= lttng_condition_buffer_usage_get_session_name(
781 condition
, &session_name
);
783 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
784 status
= lttng_condition_session_consumed_size_get_session_name(
785 condition
, &session_name
);
787 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
788 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
789 status
= lttng_condition_session_rotation_get_session_name(
790 condition
, &session_name
);
795 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
796 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
803 /* This function must be called with the RCU read lock held. */
805 int evaluate_session_condition_for_client(
806 const struct lttng_condition
*condition
,
807 struct notification_thread_state
*state
,
808 struct lttng_evaluation
**evaluation
,
809 uid_t
*session_uid
, gid_t
*session_gid
)
812 struct cds_lfht_iter iter
;
813 struct cds_lfht_node
*node
;
814 const char *session_name
;
815 struct session_info
*session_info
= NULL
;
817 session_name
= get_condition_session_name(condition
);
819 /* Find the session associated with the trigger. */
820 cds_lfht_lookup(state
->sessions_ht
,
821 hash_key_str(session_name
, lttng_ht_seed
),
825 node
= cds_lfht_iter_get_node(&iter
);
827 DBG("[notification-thread] No known session matching name \"%s\"",
833 session_info
= caa_container_of(node
, struct session_info
,
835 session_info_get(session_info
);
838 * Evaluation is performed in-line here since only one type of
839 * session-bound condition is handled for the moment.
841 switch (lttng_condition_get_type(condition
)) {
842 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
843 if (!session_info
->rotation
.ongoing
) {
845 goto end_session_put
;
848 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
849 session_info
->rotation
.id
);
852 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
855 goto end_session_put
;
861 goto end_session_put
;
864 *session_uid
= session_info
->uid
;
865 *session_gid
= session_info
->gid
;
868 session_info_put(session_info
);
873 /* This function must be called with the RCU read lock held. */
875 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
876 const struct lttng_condition
*condition
,
877 struct notification_client
*client
,
878 struct notification_thread_state
*state
)
881 struct lttng_evaluation
*evaluation
= NULL
;
882 struct notification_client_list client_list
= { 0 };
883 struct notification_client_list_element client_list_element
= { 0 };
884 uid_t object_uid
= 0;
885 gid_t object_gid
= 0;
892 switch (get_condition_binding_object(condition
)) {
893 case LTTNG_OBJECT_TYPE_SESSION
:
894 ret
= evaluate_session_condition_for_client(condition
, state
,
895 &evaluation
, &object_uid
, &object_gid
);
897 case LTTNG_OBJECT_TYPE_CHANNEL
:
898 ret
= evaluate_channel_condition_for_client(condition
, state
,
899 &evaluation
, &object_uid
, &object_gid
);
901 case LTTNG_OBJECT_TYPE_NONE
:
904 case LTTNG_OBJECT_TYPE_UNKNOWN
:
911 /* Evaluation yielded nothing. Normal exit. */
912 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
918 * Create a temporary client list with the client currently
921 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
922 CDS_INIT_LIST_HEAD(&client_list
.list
);
923 client_list
.trigger
= trigger
;
925 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
926 client_list_element
.client
= client
;
927 cds_list_add(&client_list_element
.node
, &client_list
.list
);
929 /* Send evaluation result to the newly-subscribed client. */
930 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
931 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
932 state
, object_uid
, object_gid
);
939 int notification_thread_client_subscribe(struct notification_client
*client
,
940 struct lttng_condition
*condition
,
941 struct notification_thread_state
*state
,
942 enum lttng_notification_channel_status
*_status
)
945 struct notification_client_list
*client_list
;
946 struct lttng_condition_list_element
*condition_list_element
= NULL
;
947 struct notification_client_list_element
*client_list_element
= NULL
;
948 enum lttng_notification_channel_status status
=
949 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
952 * Ensure that the client has not already subscribed to this condition
955 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
956 if (lttng_condition_is_equal(condition_list_element
->condition
,
958 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
963 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
964 if (!condition_list_element
) {
968 client_list_element
= zmalloc(sizeof(*client_list_element
));
969 if (!client_list_element
) {
977 * Add the newly-subscribed condition to the client's subscription list.
979 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
980 condition_list_element
->condition
= condition
;
981 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
983 client_list
= get_client_list_from_condition(state
, condition
);
986 * No notification-emiting trigger registered with this
987 * condition. We don't evaluate the condition right away
988 * since this trigger is not registered yet.
990 free(client_list_element
);
995 * The condition to which the client just subscribed is evaluated
996 * at this point so that conditions that are already TRUE result
997 * in a notification being sent out.
999 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1001 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1003 free(client_list_element
);
1008 * Add the client to the list of clients interested in a given trigger
1009 * if a "notification" trigger with a corresponding condition was
1012 client_list_element
->client
= client
;
1013 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1014 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1023 free(condition_list_element
);
1024 free(client_list_element
);
1029 int notification_thread_client_unsubscribe(
1030 struct notification_client
*client
,
1031 struct lttng_condition
*condition
,
1032 struct notification_thread_state
*state
,
1033 enum lttng_notification_channel_status
*_status
)
1035 struct notification_client_list
*client_list
;
1036 struct lttng_condition_list_element
*condition_list_element
,
1038 struct notification_client_list_element
*client_list_element
,
1040 bool condition_found
= false;
1041 enum lttng_notification_channel_status status
=
1042 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1044 /* Remove the condition from the client's condition list. */
1045 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1046 &client
->condition_list
, node
) {
1047 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1052 cds_list_del(&condition_list_element
->node
);
1054 * The caller may be iterating on the client's conditions to
1055 * tear down a client's connection. In this case, the condition
1056 * will be destroyed at the end.
1058 if (condition
!= condition_list_element
->condition
) {
1059 lttng_condition_destroy(
1060 condition_list_element
->condition
);
1062 free(condition_list_element
);
1063 condition_found
= true;
1067 if (!condition_found
) {
1068 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1073 * Remove the client from the list of clients interested the trigger
1074 * matching the condition.
1077 client_list
= get_client_list_from_condition(state
, condition
);
1082 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1083 &client_list
->list
, node
) {
1084 if (client_list_element
->client
->socket
!= client
->socket
) {
1087 cds_list_del(&client_list_element
->node
);
1088 free(client_list_element
);
1094 lttng_condition_destroy(condition
);
1102 void notification_client_destroy(struct notification_client
*client
,
1103 struct notification_thread_state
*state
)
1105 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
1111 /* Release all conditions to which the client was subscribed. */
1112 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
1113 &client
->condition_list
, node
) {
1114 (void) notification_thread_client_unsubscribe(client
,
1115 condition_list_element
->condition
, state
, NULL
);
1118 if (client
->socket
>= 0) {
1119 (void) lttcomm_close_unix_sock(client
->socket
);
1121 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
1122 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
1127 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1131 struct notification_client
*get_client_from_socket(int socket
,
1132 struct notification_thread_state
*state
)
1134 struct cds_lfht_iter iter
;
1135 struct cds_lfht_node
*node
;
1136 struct notification_client
*client
= NULL
;
1138 cds_lfht_lookup(state
->client_socket_ht
,
1139 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
1141 (void *) (unsigned long) socket
,
1143 node
= cds_lfht_iter_get_node(&iter
);
1148 client
= caa_container_of(node
, struct notification_client
,
1149 client_socket_ht_node
);
1155 bool buffer_usage_condition_applies_to_channel(
1156 const struct lttng_condition
*condition
,
1157 const struct channel_info
*channel_info
)
1159 enum lttng_condition_status status
;
1160 enum lttng_domain_type condition_domain
;
1161 const char *condition_session_name
= NULL
;
1162 const char *condition_channel_name
= NULL
;
1164 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1166 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1167 if (channel_info
->key
.domain
!= condition_domain
) {
1171 status
= lttng_condition_buffer_usage_get_session_name(
1172 condition
, &condition_session_name
);
1173 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1175 status
= lttng_condition_buffer_usage_get_channel_name(
1176 condition
, &condition_channel_name
);
1177 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1179 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1182 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1192 bool session_consumed_size_condition_applies_to_channel(
1193 const struct lttng_condition
*condition
,
1194 const struct channel_info
*channel_info
)
1196 enum lttng_condition_status status
;
1197 const char *condition_session_name
= NULL
;
1199 status
= lttng_condition_session_consumed_size_get_session_name(
1200 condition
, &condition_session_name
);
1201 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1203 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1213 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1214 const struct channel_info
*channel_info
)
1216 const struct lttng_condition
*condition
;
1217 bool trigger_applies
;
1219 condition
= lttng_trigger_get_const_condition(trigger
);
1224 switch (lttng_condition_get_type(condition
)) {
1225 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1226 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1227 trigger_applies
= buffer_usage_condition_applies_to_channel(
1228 condition
, channel_info
);
1230 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1231 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1232 condition
, channel_info
);
1238 return trigger_applies
;
1244 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1245 struct notification_client
*client
)
1247 bool applies
= false;
1248 struct lttng_condition_list_element
*condition_list_element
;
1250 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1252 applies
= lttng_condition_is_equal(
1253 condition_list_element
->condition
,
1254 lttng_trigger_get_condition(trigger
));
1262 /* Must be called with RCU read lock held. */
1264 struct lttng_session_trigger_list
*get_session_trigger_list(
1265 struct notification_thread_state
*state
,
1266 const char *session_name
)
1268 struct lttng_session_trigger_list
*list
= NULL
;
1269 struct cds_lfht_node
*node
;
1270 struct cds_lfht_iter iter
;
1272 cds_lfht_lookup(state
->session_triggers_ht
,
1273 hash_key_str(session_name
, lttng_ht_seed
),
1274 match_session_trigger_list
,
1277 node
= cds_lfht_iter_get_node(&iter
);
1280 * Not an error, the list of triggers applying to that session
1281 * will be initialized when the session is created.
1283 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1288 list
= caa_container_of(node
,
1289 struct lttng_session_trigger_list
,
1290 session_triggers_ht_node
);
1296 * Allocate an empty lttng_session_trigger_list for the session named
1299 * No ownership of 'session_name' is assumed by the session trigger list.
1300 * It is the caller's responsability to ensure the session name is alive
1301 * for as long as this list is.
1304 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1305 const char *session_name
,
1306 struct cds_lfht
*session_triggers_ht
)
1308 struct lttng_session_trigger_list
*list
;
1310 list
= zmalloc(sizeof(*list
));
1314 list
->session_name
= session_name
;
1315 CDS_INIT_LIST_HEAD(&list
->list
);
1316 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1317 list
->session_triggers_ht
= session_triggers_ht
;
1320 /* Publish the list through the session_triggers_ht. */
1321 cds_lfht_add(session_triggers_ht
,
1322 hash_key_str(session_name
, lttng_ht_seed
),
1323 &list
->session_triggers_ht_node
);
1330 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1332 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1337 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1339 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1341 /* Empty the list element by element, and then free the list itself. */
1342 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1343 &list
->list
, node
) {
1344 cds_list_del(&trigger_list_element
->node
);
1345 free(trigger_list_element
);
1348 /* Unpublish the list from the session_triggers_ht. */
1349 cds_lfht_del(list
->session_triggers_ht
,
1350 &list
->session_triggers_ht_node
);
1352 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1356 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1357 const struct lttng_trigger
*trigger
)
1360 struct lttng_trigger_list_element
*new_element
=
1361 zmalloc(sizeof(*new_element
));
1367 CDS_INIT_LIST_HEAD(&new_element
->node
);
1368 new_element
->trigger
= trigger
;
1369 cds_list_add(&new_element
->node
, &list
->list
);
1375 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1376 const char *session_name
)
1378 bool applies
= false;
1379 const struct lttng_condition
*condition
;
1381 condition
= lttng_trigger_get_const_condition(trigger
);
1382 switch (lttng_condition_get_type(condition
)) {
1383 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1384 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1386 enum lttng_condition_status condition_status
;
1387 const char *condition_session_name
;
1389 condition_status
= lttng_condition_session_rotation_get_session_name(
1390 condition
, &condition_session_name
);
1391 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1392 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1396 assert(condition_session_name
);
1397 applies
= !strcmp(condition_session_name
, session_name
);
1408 * Allocate and initialize an lttng_session_trigger_list which contains
1409 * all triggers that apply to the session named 'session_name'.
1411 * No ownership of 'session_name' is assumed by the session trigger list.
1412 * It is the caller's responsability to ensure the session name is alive
1413 * for as long as this list is.
1416 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1417 const struct notification_thread_state
*state
,
1418 const char *session_name
)
1420 int trigger_count
= 0;
1421 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1422 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1423 struct cds_lfht_iter iter
;
1425 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1426 state
->session_triggers_ht
);
1428 /* Add all triggers applying to the session named 'session_name'. */
1429 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1433 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1438 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1439 trigger_ht_element
->trigger
);
1447 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1449 return session_trigger_list
;
1451 lttng_session_trigger_list_destroy(session_trigger_list
);
1456 struct session_info
*find_or_create_session_info(
1457 struct notification_thread_state
*state
,
1458 const char *name
, uid_t uid
, gid_t gid
)
1460 struct session_info
*session
= NULL
;
1461 struct cds_lfht_node
*node
;
1462 struct cds_lfht_iter iter
;
1463 struct lttng_session_trigger_list
*trigger_list
;
1466 cds_lfht_lookup(state
->sessions_ht
,
1467 hash_key_str(name
, lttng_ht_seed
),
1471 node
= cds_lfht_iter_get_node(&iter
);
1473 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1475 session
= caa_container_of(node
, struct session_info
,
1477 assert(session
->uid
== uid
);
1478 assert(session
->gid
== gid
);
1479 session_info_get(session
);
1483 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1484 if (!trigger_list
) {
1488 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1489 state
->sessions_ht
);
1491 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1495 trigger_list
= NULL
;
1497 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1498 &session
->sessions_ht_node
);
1504 session_info_put(session
);
1509 int handle_notification_thread_command_add_channel(
1510 struct notification_thread_state
*state
,
1511 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1512 const char *channel_name
, enum lttng_domain_type channel_domain
,
1513 uint64_t channel_key_int
, uint64_t channel_capacity
,
1514 enum lttng_error_code
*cmd_result
)
1516 struct cds_list_head trigger_list
;
1517 struct channel_info
*new_channel_info
= NULL
;
1518 struct channel_key channel_key
= {
1519 .key
= channel_key_int
,
1520 .domain
= channel_domain
,
1522 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1523 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1524 int trigger_count
= 0;
1525 struct cds_lfht_iter iter
;
1526 struct session_info
*session_info
= NULL
;
1528 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1529 channel_name
, session_name
, channel_key_int
,
1530 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1532 CDS_INIT_LIST_HEAD(&trigger_list
);
1534 session_info
= find_or_create_session_info(state
, session_name
,
1535 session_uid
, session_gid
);
1536 if (!session_info
) {
1537 /* Allocation error or an internal error occurred. */
1541 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1542 channel_capacity
, session_info
);
1543 if (!new_channel_info
) {
1547 /* Build a list of all triggers applying to the new channel. */
1548 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1550 struct lttng_trigger_list_element
*new_element
;
1552 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1553 new_channel_info
)) {
1557 new_element
= zmalloc(sizeof(*new_element
));
1561 CDS_INIT_LIST_HEAD(&new_element
->node
);
1562 new_element
->trigger
= trigger_ht_element
->trigger
;
1563 cds_list_add(&new_element
->node
, &trigger_list
);
1567 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1569 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1570 if (!channel_trigger_list
) {
1573 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1574 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1575 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1576 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1579 /* Add channel to the channel_ht which owns the channel_infos. */
1580 cds_lfht_add(state
->channels_ht
,
1581 hash_channel_key(&new_channel_info
->key
),
1582 &new_channel_info
->channels_ht_node
);
1584 * Add the list of triggers associated with this channel to the
1585 * channel_triggers_ht.
1587 cds_lfht_add(state
->channel_triggers_ht
,
1588 hash_channel_key(&new_channel_info
->key
),
1589 &channel_trigger_list
->channel_triggers_ht_node
);
1591 session_info_put(session_info
);
1592 *cmd_result
= LTTNG_OK
;
1595 channel_info_destroy(new_channel_info
);
1596 session_info_put(session_info
);
1601 int handle_notification_thread_command_remove_channel(
1602 struct notification_thread_state
*state
,
1603 uint64_t channel_key
, enum lttng_domain_type domain
,
1604 enum lttng_error_code
*cmd_result
)
1606 struct cds_lfht_node
*node
;
1607 struct cds_lfht_iter iter
;
1608 struct lttng_channel_trigger_list
*trigger_list
;
1609 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1610 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1611 struct channel_info
*channel_info
;
1613 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1614 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1618 cds_lfht_lookup(state
->channel_triggers_ht
,
1619 hash_channel_key(&key
),
1620 match_channel_trigger_list
,
1623 node
= cds_lfht_iter_get_node(&iter
);
1625 * There is a severe internal error if we are being asked to remove a
1626 * channel that doesn't exist.
1629 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1633 /* Free the list of triggers associated with this channel. */
1634 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1635 channel_triggers_ht_node
);
1636 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1637 &trigger_list
->list
, node
) {
1638 cds_list_del(&trigger_list_element
->node
);
1639 free(trigger_list_element
);
1641 cds_lfht_del(state
->channel_triggers_ht
, node
);
1644 /* Free sampled channel state. */
1645 cds_lfht_lookup(state
->channel_state_ht
,
1646 hash_channel_key(&key
),
1647 match_channel_state_sample
,
1650 node
= cds_lfht_iter_get_node(&iter
);
1652 * This is expected to be NULL if the channel is destroyed before we
1653 * received a sample.
1656 struct channel_state_sample
*sample
= caa_container_of(node
,
1657 struct channel_state_sample
,
1658 channel_state_ht_node
);
1660 cds_lfht_del(state
->channel_state_ht
, node
);
1664 /* Remove the channel from the channels_ht and free it. */
1665 cds_lfht_lookup(state
->channels_ht
,
1666 hash_channel_key(&key
),
1670 node
= cds_lfht_iter_get_node(&iter
);
1672 channel_info
= caa_container_of(node
, struct channel_info
,
1674 cds_lfht_del(state
->channels_ht
, node
);
1675 channel_info_destroy(channel_info
);
1678 *cmd_result
= LTTNG_OK
;
1683 int handle_notification_thread_command_session_rotation(
1684 struct notification_thread_state
*state
,
1685 enum notification_thread_command_type cmd_type
,
1686 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1687 uint64_t trace_archive_chunk_id
,
1688 struct lttng_trace_archive_location
*location
,
1689 enum lttng_error_code
*_cmd_result
)
1692 enum lttng_error_code cmd_result
= LTTNG_OK
;
1693 struct lttng_session_trigger_list
*trigger_list
;
1694 struct lttng_trigger_list_element
*trigger_list_element
;
1695 struct session_info
*session_info
;
1699 session_info
= find_or_create_session_info(state
, session_name
,
1700 session_uid
, session_gid
);
1701 if (!session_info
) {
1702 /* Allocation error or an internal error occurred. */
1704 cmd_result
= LTTNG_ERR_NOMEM
;
1708 session_info
->rotation
.ongoing
=
1709 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1710 session_info
->rotation
.id
= trace_archive_chunk_id
;
1711 trigger_list
= get_session_trigger_list(state
, session_name
);
1712 if (!trigger_list
) {
1713 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1718 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1720 const struct lttng_condition
*condition
;
1721 const struct lttng_action
*action
;
1722 const struct lttng_trigger
*trigger
;
1723 struct notification_client_list
*client_list
;
1724 struct lttng_evaluation
*evaluation
= NULL
;
1725 enum lttng_condition_type condition_type
;
1727 trigger
= trigger_list_element
->trigger
;
1728 condition
= lttng_trigger_get_const_condition(trigger
);
1730 condition_type
= lttng_condition_get_type(condition
);
1732 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1733 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1735 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1736 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1740 action
= lttng_trigger_get_const_action(trigger
);
1742 /* Notify actions are the only type currently supported. */
1743 assert(lttng_action_get_type_const(action
) ==
1744 LTTNG_ACTION_TYPE_NOTIFY
);
1746 client_list
= get_client_list_from_condition(state
, condition
);
1747 assert(client_list
);
1749 if (cds_list_empty(&client_list
->list
)) {
1751 * No clients interested in the evaluation's result,
1757 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1758 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1759 trace_archive_chunk_id
);
1761 evaluation
= lttng_evaluation_session_rotation_completed_create(
1762 trace_archive_chunk_id
, location
);
1766 /* Internal error */
1768 cmd_result
= LTTNG_ERR_UNK
;
1772 /* Dispatch evaluation result to all clients. */
1773 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
1774 evaluation
, client_list
, state
,
1777 lttng_evaluation_destroy(evaluation
);
1778 if (caa_unlikely(ret
)) {
1783 session_info_put(session_info
);
1784 *_cmd_result
= cmd_result
;
1790 int condition_is_supported(struct lttng_condition
*condition
)
1794 switch (lttng_condition_get_type(condition
)) {
1795 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1796 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1798 enum lttng_domain_type domain
;
1800 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1807 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1813 * Older kernel tracers don't expose the API to monitor their
1814 * buffers. Therefore, we reject triggers that require that
1815 * mechanism to be available to be evaluated.
1817 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1828 /* Must be called with RCU read lock held. */
1830 int bind_trigger_to_matching_session(const struct lttng_trigger
*trigger
,
1831 struct notification_thread_state
*state
)
1834 const struct lttng_condition
*condition
;
1835 const char *session_name
;
1836 struct lttng_session_trigger_list
*trigger_list
;
1838 condition
= lttng_trigger_get_const_condition(trigger
);
1839 switch (lttng_condition_get_type(condition
)) {
1840 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1841 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1843 enum lttng_condition_status status
;
1845 status
= lttng_condition_session_rotation_get_session_name(
1846 condition
, &session_name
);
1847 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1848 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
1859 trigger_list
= get_session_trigger_list(state
, session_name
);
1860 if (!trigger_list
) {
1861 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
1867 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
1869 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
1874 /* Must be called with RCU read lock held. */
1876 int bind_trigger_to_matching_channels(const struct lttng_trigger
*trigger
,
1877 struct notification_thread_state
*state
)
1880 struct cds_lfht_node
*node
;
1881 struct cds_lfht_iter iter
;
1882 struct channel_info
*channel
;
1884 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1886 struct lttng_trigger_list_element
*trigger_list_element
;
1887 struct lttng_channel_trigger_list
*trigger_list
;
1889 if (!trigger_applies_to_channel(trigger
, channel
)) {
1893 cds_lfht_lookup(state
->channel_triggers_ht
,
1894 hash_channel_key(&channel
->key
),
1895 match_channel_trigger_list
,
1898 node
= cds_lfht_iter_get_node(&iter
);
1900 trigger_list
= caa_container_of(node
,
1901 struct lttng_channel_trigger_list
,
1902 channel_triggers_ht_node
);
1904 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1905 if (!trigger_list_element
) {
1909 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1910 trigger_list_element
->trigger
= trigger
;
1911 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1912 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
1920 * FIXME A client's credentials are not checked when registering a trigger, nor
1921 * are they stored alongside with the trigger.
1923 * The effects of this are benign since:
1924 * - The client will succeed in registering the trigger, as it is valid,
1925 * - The trigger will, internally, be bound to the channel/session,
1926 * - The notifications will not be sent since the client's credentials
1927 * are checked against the channel at that moment.
1929 * If this function returns a non-zero value, it means something is
1930 * fundamentally broken and the whole subsystem/thread will be torn down.
1932 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1936 int handle_notification_thread_command_register_trigger(
1937 struct notification_thread_state
*state
,
1938 struct lttng_trigger
*trigger
,
1939 enum lttng_error_code
*cmd_result
)
1942 struct lttng_condition
*condition
;
1943 struct notification_client
*client
;
1944 struct notification_client_list
*client_list
= NULL
;
1945 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1946 struct notification_client_list_element
*client_list_element
, *tmp
;
1947 struct cds_lfht_node
*node
;
1948 struct cds_lfht_iter iter
;
1949 bool free_trigger
= true;
1953 condition
= lttng_trigger_get_condition(trigger
);
1956 ret
= condition_is_supported(condition
);
1959 } else if (ret
== 0) {
1960 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1963 /* Feature is supported, continue. */
1967 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1968 if (!trigger_ht_element
) {
1973 /* Add trigger to the trigger_ht. */
1974 cds_lfht_node_init(&trigger_ht_element
->node
);
1975 trigger_ht_element
->trigger
= trigger
;
1977 node
= cds_lfht_add_unique(state
->triggers_ht
,
1978 lttng_condition_hash(condition
),
1981 &trigger_ht_element
->node
);
1982 if (node
!= &trigger_ht_element
->node
) {
1983 /* Not a fatal error, simply report it to the client. */
1984 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1985 goto error_free_ht_element
;
1989 * Ownership of the trigger and of its wrapper was transfered to
1992 trigger_ht_element
= NULL
;
1993 free_trigger
= false;
1996 * The rest only applies to triggers that have a "notify" action.
1997 * It is not skipped as this is the only action type currently
2000 client_list
= zmalloc(sizeof(*client_list
));
2003 goto error_free_ht_element
;
2005 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
2006 CDS_INIT_LIST_HEAD(&client_list
->list
);
2007 client_list
->trigger
= trigger
;
2009 /* Build a list of clients to which this new trigger applies. */
2010 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2011 client_socket_ht_node
) {
2012 if (!trigger_applies_to_client(trigger
, client
)) {
2016 client_list_element
= zmalloc(sizeof(*client_list_element
));
2017 if (!client_list_element
) {
2019 goto error_free_client_list
;
2021 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2022 client_list_element
->client
= client
;
2023 cds_list_add(&client_list_element
->node
, &client_list
->list
);
2026 cds_lfht_add(state
->notification_trigger_clients_ht
,
2027 lttng_condition_hash(condition
),
2028 &client_list
->notification_trigger_ht_node
);
2030 switch (get_condition_binding_object(condition
)) {
2031 case LTTNG_OBJECT_TYPE_SESSION
:
2032 /* Add the trigger to the list if it matches a known session. */
2033 ret
= bind_trigger_to_matching_session(trigger
, state
);
2035 goto error_free_client_list
;
2038 case LTTNG_OBJECT_TYPE_CHANNEL
:
2040 * Add the trigger to list of triggers bound to the channels
2043 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2045 goto error_free_client_list
;
2048 case LTTNG_OBJECT_TYPE_NONE
:
2051 ERR("[notification-thread] Unknown object type on which to bind a newly registered trigger was encountered");
2053 goto error_free_client_list
;
2057 * Since there is nothing preventing clients from subscribing to a
2058 * condition before the corresponding trigger is registered, we have
2059 * to evaluate this new condition right away.
2061 * At some point, we were waiting for the next "evaluation" (e.g. on
2062 * reception of a channel sample) to evaluate this new condition, but
2065 * The reason it was broken is that waiting for the next sample
2066 * does not allow us to properly handle transitions for edge-triggered
2069 * Consider this example: when we handle a new channel sample, we
2070 * evaluate each conditions twice: once with the previous state, and
2071 * again with the newest state. We then use those two results to
2072 * determine whether a state change happened: a condition was false and
2073 * became true. If a state change happened, we have to notify clients.
2075 * Now, if a client subscribes to a given notification and registers
2076 * a trigger *after* that subscription, we have to make sure the
2077 * condition is evaluated at this point while considering only the
2078 * current state. Otherwise, the next evaluation cycle may only see
2079 * that the evaluations remain the same (true for samples n-1 and n) and
2080 * the client will never know that the condition has been met.
2082 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2083 &client_list
->list
, node
) {
2084 ret
= evaluate_condition_for_client(trigger
, condition
,
2085 client_list_element
->client
, state
);
2087 goto error_free_client_list
;
2092 * Client list ownership transferred to the
2093 * notification_trigger_clients_ht.
2097 *cmd_result
= LTTNG_OK
;
2098 error_free_client_list
:
2100 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2101 &client_list
->list
, node
) {
2102 free(client_list_element
);
2106 error_free_ht_element
:
2107 free(trigger_ht_element
);
2110 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
2112 lttng_condition_destroy(condition
);
2113 lttng_action_destroy(action
);
2114 lttng_trigger_destroy(trigger
);
2121 int handle_notification_thread_command_unregister_trigger(
2122 struct notification_thread_state
*state
,
2123 struct lttng_trigger
*trigger
,
2124 enum lttng_error_code
*_cmd_reply
)
2126 struct cds_lfht_iter iter
;
2127 struct cds_lfht_node
*triggers_ht_node
;
2128 struct lttng_channel_trigger_list
*trigger_list
;
2129 struct notification_client_list
*client_list
;
2130 struct notification_client_list_element
*client_list_element
, *tmp
;
2131 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2132 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2134 struct lttng_action
*action
;
2135 enum lttng_error_code cmd_reply
;
2139 cds_lfht_lookup(state
->triggers_ht
,
2140 lttng_condition_hash(condition
),
2144 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2145 if (!triggers_ht_node
) {
2146 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2149 cmd_reply
= LTTNG_OK
;
2152 /* Remove trigger from channel_triggers_ht. */
2153 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2154 channel_triggers_ht_node
) {
2155 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2157 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2158 &trigger_list
->list
, node
) {
2159 const struct lttng_condition
*current_condition
=
2160 lttng_trigger_get_const_condition(
2161 trigger_element
->trigger
);
2163 assert(current_condition
);
2164 if (!lttng_condition_is_equal(condition
,
2165 current_condition
)) {
2169 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2170 cds_list_del(&trigger_element
->node
);
2171 /* A trigger can only appear once per channel */
2177 * Remove and release the client list from
2178 * notification_trigger_clients_ht.
2180 client_list
= get_client_list_from_condition(state
, condition
);
2181 assert(client_list
);
2183 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2184 &client_list
->list
, node
) {
2185 free(client_list_element
);
2187 cds_lfht_del(state
->notification_trigger_clients_ht
,
2188 &client_list
->notification_trigger_ht_node
);
2191 /* Remove trigger from triggers_ht. */
2192 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2193 struct lttng_trigger_ht_element
, node
);
2194 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2196 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
2197 lttng_condition_destroy(condition
);
2198 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
2199 lttng_action_destroy(action
);
2200 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2201 free(trigger_ht_element
);
2205 *_cmd_reply
= cmd_reply
;
2210 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2211 int handle_notification_thread_command(
2212 struct notification_thread_handle
*handle
,
2213 struct notification_thread_state
*state
)
2217 struct notification_thread_command
*cmd
;
2219 /* Read the event pipe to put it back into a quiescent state. */
2220 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2226 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2227 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2228 struct notification_thread_command
, cmd_list_node
);
2229 switch (cmd
->type
) {
2230 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2231 DBG("[notification-thread] Received register trigger command");
2232 ret
= handle_notification_thread_command_register_trigger(
2233 state
, cmd
->parameters
.trigger
,
2236 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2237 DBG("[notification-thread] Received unregister trigger command");
2238 ret
= handle_notification_thread_command_unregister_trigger(
2239 state
, cmd
->parameters
.trigger
,
2242 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2243 DBG("[notification-thread] Received add channel command");
2244 ret
= handle_notification_thread_command_add_channel(
2246 cmd
->parameters
.add_channel
.session
.name
,
2247 cmd
->parameters
.add_channel
.session
.uid
,
2248 cmd
->parameters
.add_channel
.session
.gid
,
2249 cmd
->parameters
.add_channel
.channel
.name
,
2250 cmd
->parameters
.add_channel
.channel
.domain
,
2251 cmd
->parameters
.add_channel
.channel
.key
,
2252 cmd
->parameters
.add_channel
.channel
.capacity
,
2255 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2256 DBG("[notification-thread] Received remove channel command");
2257 ret
= handle_notification_thread_command_remove_channel(
2258 state
, cmd
->parameters
.remove_channel
.key
,
2259 cmd
->parameters
.remove_channel
.domain
,
2262 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2263 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2264 DBG("[notification-thread] Received session rotation %s command",
2265 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2266 "ongoing" : "completed");
2267 ret
= handle_notification_thread_command_session_rotation(
2270 cmd
->parameters
.session_rotation
.session_name
,
2271 cmd
->parameters
.session_rotation
.uid
,
2272 cmd
->parameters
.session_rotation
.gid
,
2273 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2274 cmd
->parameters
.session_rotation
.location
,
2277 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2278 DBG("[notification-thread] Received quit command");
2279 cmd
->reply_code
= LTTNG_OK
;
2283 ERR("[notification-thread] Unknown internal command received");
2291 cds_list_del(&cmd
->cmd_list_node
);
2292 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2293 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2296 /* Wake-up and return a fatal error to the calling thread. */
2297 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2298 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2299 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2301 /* Indicate a fatal error to the caller. */
2306 unsigned long hash_client_socket(int socket
)
2308 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
2312 int socket_set_non_blocking(int socket
)
2316 /* Set the pipe as non-blocking. */
2317 ret
= fcntl(socket
, F_GETFL
, 0);
2319 PERROR("fcntl get socket flags");
2324 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2326 PERROR("fcntl set O_NONBLOCK socket flag");
2329 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2335 int client_reset_inbound_state(struct notification_client
*client
)
2339 ret
= lttng_dynamic_buffer_set_size(
2340 &client
->communication
.inbound
.buffer
, 0);
2343 client
->communication
.inbound
.bytes_to_receive
=
2344 sizeof(struct lttng_notification_channel_message
);
2345 client
->communication
.inbound
.msg_type
=
2346 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2347 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2348 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2349 ret
= lttng_dynamic_buffer_set_size(
2350 &client
->communication
.inbound
.buffer
,
2351 client
->communication
.inbound
.bytes_to_receive
);
2355 int handle_notification_thread_client_connect(
2356 struct notification_thread_state
*state
)
2359 struct notification_client
*client
;
2361 DBG("[notification-thread] Handling new notification channel client connection");
2363 client
= zmalloc(sizeof(*client
));
2369 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2370 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
2371 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
2372 client
->communication
.inbound
.expect_creds
= true;
2373 ret
= client_reset_inbound_state(client
);
2375 ERR("[notification-thread] Failed to reset client communication's inbound state");
2380 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2382 ERR("[notification-thread] Failed to accept new notification channel client connection");
2387 client
->socket
= ret
;
2389 ret
= socket_set_non_blocking(client
->socket
);
2391 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2395 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2397 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2402 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2403 LPOLLIN
| LPOLLERR
|
2404 LPOLLHUP
| LPOLLRDHUP
);
2406 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2410 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2414 cds_lfht_add(state
->client_socket_ht
,
2415 hash_client_socket(client
->socket
),
2416 &client
->client_socket_ht_node
);
2421 notification_client_destroy(client
, state
);
2425 int handle_notification_thread_client_disconnect(
2427 struct notification_thread_state
*state
)
2430 struct notification_client
*client
;
2433 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2435 client
= get_client_from_socket(client_socket
, state
);
2437 /* Internal state corruption, fatal error. */
2438 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2444 ret
= lttng_poll_del(&state
->events
, client_socket
);
2446 ERR("[notification-thread] Failed to remove client socket from poll set");
2448 cds_lfht_del(state
->client_socket_ht
,
2449 &client
->client_socket_ht_node
);
2450 notification_client_destroy(client
, state
);
2456 int handle_notification_thread_client_disconnect_all(
2457 struct notification_thread_state
*state
)
2459 struct cds_lfht_iter iter
;
2460 struct notification_client
*client
;
2461 bool error_encoutered
= false;
2464 DBG("[notification-thread] Closing all client connections");
2465 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2466 client_socket_ht_node
) {
2469 ret
= handle_notification_thread_client_disconnect(
2470 client
->socket
, state
);
2472 error_encoutered
= true;
2476 return error_encoutered
? 1 : 0;
2479 int handle_notification_thread_trigger_unregister_all(
2480 struct notification_thread_state
*state
)
2482 bool error_occurred
= false;
2483 struct cds_lfht_iter iter
;
2484 struct lttng_trigger_ht_element
*trigger_ht_element
;
2486 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2488 int ret
= handle_notification_thread_command_unregister_trigger(
2489 state
, trigger_ht_element
->trigger
, NULL
);
2491 error_occurred
= true;
2494 return error_occurred
? -1 : 0;
2498 int client_flush_outgoing_queue(struct notification_client
*client
,
2499 struct notification_thread_state
*state
)
2502 size_t to_send_count
;
2504 assert(client
->communication
.outbound
.buffer
.size
!= 0);
2505 to_send_count
= client
->communication
.outbound
.buffer
.size
;
2506 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
2509 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
2510 client
->communication
.outbound
.buffer
.data
,
2512 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
2513 (ret
> 0 && ret
< to_send_count
)) {
2514 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
2516 to_send_count
-= max(ret
, 0);
2518 memcpy(client
->communication
.outbound
.buffer
.data
,
2519 client
->communication
.outbound
.buffer
.data
+
2520 client
->communication
.outbound
.buffer
.size
- to_send_count
,
2522 ret
= lttng_dynamic_buffer_set_size(
2523 &client
->communication
.outbound
.buffer
,
2530 * We want to be notified whenever there is buffer space
2531 * available to send the rest of the payload.
2533 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2534 CLIENT_POLL_MASK_IN_OUT
);
2538 } else if (ret
< 0) {
2539 /* Generic error, disconnect the client. */
2540 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
2542 ret
= handle_notification_thread_client_disconnect(
2543 client
->socket
, state
);
2548 /* No error and flushed the queue completely. */
2549 ret
= lttng_dynamic_buffer_set_size(
2550 &client
->communication
.outbound
.buffer
, 0);
2554 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2555 CLIENT_POLL_MASK_IN
);
2560 client
->communication
.outbound
.queued_command_reply
= false;
2561 client
->communication
.outbound
.dropped_notification
= false;
2570 int client_send_command_reply(struct notification_client
*client
,
2571 struct notification_thread_state
*state
,
2572 enum lttng_notification_channel_status status
)
2575 struct lttng_notification_channel_command_reply reply
= {
2576 .status
= (int8_t) status
,
2578 struct lttng_notification_channel_message msg
= {
2579 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
2580 .size
= sizeof(reply
),
2582 char buffer
[sizeof(msg
) + sizeof(reply
)];
2584 if (client
->communication
.outbound
.queued_command_reply
) {
2585 /* Protocol error. */
2589 memcpy(buffer
, &msg
, sizeof(msg
));
2590 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
2591 DBG("[notification-thread] Send command reply (%i)", (int) status
);
2593 /* Enqueue buffer to outgoing queue and flush it. */
2594 ret
= lttng_dynamic_buffer_append(
2595 &client
->communication
.outbound
.buffer
,
2596 buffer
, sizeof(buffer
));
2601 ret
= client_flush_outgoing_queue(client
, state
);
2606 if (client
->communication
.outbound
.buffer
.size
!= 0) {
2607 /* Queue could not be emptied. */
2608 client
->communication
.outbound
.queued_command_reply
= true;
2617 int client_dispatch_message(struct notification_client
*client
,
2618 struct notification_thread_state
*state
)
2622 if (client
->communication
.inbound
.msg_type
!=
2623 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
2624 client
->communication
.inbound
.msg_type
!=
2625 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
2626 !client
->validated
) {
2627 WARN("[notification-thread] client attempted a command before handshake");
2632 switch (client
->communication
.inbound
.msg_type
) {
2633 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
2636 * Receiving message header. The function will be called again
2637 * once the rest of the message as been received and can be
2640 const struct lttng_notification_channel_message
*msg
;
2642 assert(sizeof(*msg
) ==
2643 client
->communication
.inbound
.buffer
.size
);
2644 msg
= (const struct lttng_notification_channel_message
*)
2645 client
->communication
.inbound
.buffer
.data
;
2647 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
2648 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
2653 switch (msg
->type
) {
2654 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2655 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2656 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2660 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
2664 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
2665 client
->communication
.inbound
.msg_type
=
2666 (enum lttng_notification_channel_message_type
) msg
->type
;
2667 ret
= lttng_dynamic_buffer_set_size(
2668 &client
->communication
.inbound
.buffer
, msg
->size
);
2674 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2676 struct lttng_notification_channel_command_handshake
*handshake_client
;
2677 struct lttng_notification_channel_command_handshake handshake_reply
= {
2678 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
2679 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2681 struct lttng_notification_channel_message msg_header
= {
2682 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2683 .size
= sizeof(handshake_reply
),
2685 enum lttng_notification_channel_status status
=
2686 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2687 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2689 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2690 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2691 sizeof(handshake_reply
));
2694 (struct lttng_notification_channel_command_handshake
*)
2695 client
->communication
.inbound
.buffer
.data
;
2696 client
->major
= handshake_client
->major
;
2697 client
->minor
= handshake_client
->minor
;
2698 if (!client
->communication
.inbound
.creds_received
) {
2699 ERR("[notification-thread] No credentials received from client");
2704 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2705 &client
->communication
.inbound
.creds
);
2706 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2707 &client
->communication
.inbound
.creds
);
2708 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2709 client
->uid
, client
->gid
, (int) client
->major
,
2710 (int) client
->minor
);
2712 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2713 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2716 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2717 send_buffer
, sizeof(send_buffer
));
2719 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2723 ret
= client_flush_outgoing_queue(client
, state
);
2728 ret
= client_send_command_reply(client
, state
, status
);
2730 ERR("[notification-thread] Failed to send reply to notification channel client");
2734 /* Set reception state to receive the next message header. */
2735 ret
= client_reset_inbound_state(client
);
2737 ERR("[notification-thread] Failed to reset client communication's inbound state");
2740 client
->validated
= true;
2743 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2744 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2746 struct lttng_condition
*condition
;
2747 enum lttng_notification_channel_status status
=
2748 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2749 const struct lttng_buffer_view condition_view
=
2750 lttng_buffer_view_from_dynamic_buffer(
2751 &client
->communication
.inbound
.buffer
,
2753 size_t expected_condition_size
=
2754 client
->communication
.inbound
.buffer
.size
;
2756 ret
= lttng_condition_create_from_buffer(&condition_view
,
2758 if (ret
!= expected_condition_size
) {
2759 ERR("[notification-thread] Malformed condition received from client");
2763 if (client
->communication
.inbound
.msg_type
==
2764 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2765 ret
= notification_thread_client_subscribe(client
,
2766 condition
, state
, &status
);
2768 ret
= notification_thread_client_unsubscribe(client
,
2769 condition
, state
, &status
);
2775 ret
= client_send_command_reply(client
, state
, status
);
2777 ERR("[notification-thread] Failed to send reply to notification channel client");
2781 /* Set reception state to receive the next message header. */
2782 ret
= client_reset_inbound_state(client
);
2784 ERR("[notification-thread] Failed to reset client communication's inbound state");
2796 /* Incoming data from client. */
2797 int handle_notification_thread_client_in(
2798 struct notification_thread_state
*state
, int socket
)
2801 struct notification_client
*client
;
2805 client
= get_client_from_socket(socket
, state
);
2807 /* Internal error, abort. */
2812 offset
= client
->communication
.inbound
.buffer
.size
-
2813 client
->communication
.inbound
.bytes_to_receive
;
2814 if (client
->communication
.inbound
.expect_creds
) {
2815 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2816 client
->communication
.inbound
.buffer
.data
+ offset
,
2817 client
->communication
.inbound
.bytes_to_receive
,
2818 &client
->communication
.inbound
.creds
);
2820 client
->communication
.inbound
.expect_creds
= false;
2821 client
->communication
.inbound
.creds_received
= true;
2824 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2825 client
->communication
.inbound
.buffer
.data
+ offset
,
2826 client
->communication
.inbound
.bytes_to_receive
);
2829 goto error_disconnect_client
;
2832 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2833 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2834 ret
= client_dispatch_message(client
, state
);
2837 * Only returns an error if this client must be
2840 goto error_disconnect_client
;
2847 error_disconnect_client
:
2848 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2852 /* Client ready to receive outgoing data. */
2853 int handle_notification_thread_client_out(
2854 struct notification_thread_state
*state
, int socket
)
2857 struct notification_client
*client
;
2859 client
= get_client_from_socket(socket
, state
);
2861 /* Internal error, abort. */
2866 ret
= client_flush_outgoing_queue(client
, state
);
2875 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2876 const struct channel_state_sample
*sample
,
2877 uint64_t buffer_capacity
)
2879 bool result
= false;
2881 enum lttng_condition_type condition_type
;
2882 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2883 condition
, struct lttng_condition_buffer_usage
,
2886 if (use_condition
->threshold_bytes
.set
) {
2887 threshold
= use_condition
->threshold_bytes
.value
;
2890 * Threshold was expressed as a ratio.
2892 * TODO the threshold (in bytes) of conditions expressed
2893 * as a ratio of total buffer size could be cached to
2894 * forego this double-multiplication or it could be performed
2895 * as fixed-point math.
2897 * Note that caching should accomodate the case where the
2898 * condition applies to multiple channels (i.e. don't assume
2899 * that all channels matching my_chann* have the same size...)
2901 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
2902 (double) buffer_capacity
);
2905 condition_type
= lttng_condition_get_type(condition
);
2906 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
2907 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2908 threshold
, sample
->highest_usage
);
2911 * The low condition should only be triggered once _all_ of the
2912 * streams in a channel have gone below the "low" threshold.
2914 if (sample
->highest_usage
<= threshold
) {
2918 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2919 threshold
, sample
->highest_usage
);
2922 * For high buffer usage scenarios, we want to trigger whenever
2923 * _any_ of the streams has reached the "high" threshold.
2925 if (sample
->highest_usage
>= threshold
) {
2934 bool evaluate_session_consumed_size_condition(
2935 const struct lttng_condition
*condition
,
2936 uint64_t session_consumed_size
)
2939 const struct lttng_condition_session_consumed_size
*size_condition
=
2940 container_of(condition
,
2941 struct lttng_condition_session_consumed_size
,
2944 threshold
= size_condition
->consumed_threshold_bytes
.value
;
2945 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
2946 threshold
, session_consumed_size
);
2947 return session_consumed_size
>= threshold
;
2951 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
2952 struct lttng_evaluation
**evaluation
,
2953 const struct notification_thread_state
*state
,
2954 const struct channel_state_sample
*previous_sample
,
2955 const struct channel_state_sample
*latest_sample
,
2956 uint64_t previous_session_consumed_total
,
2957 uint64_t latest_session_consumed_total
,
2958 struct channel_info
*channel_info
)
2961 enum lttng_condition_type condition_type
;
2962 const bool previous_sample_available
= !!previous_sample
;
2963 bool previous_sample_result
= false;
2964 bool latest_sample_result
;
2966 condition_type
= lttng_condition_get_type(condition
);
2968 switch (condition_type
) {
2969 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2970 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2971 if (caa_likely(previous_sample_available
)) {
2972 previous_sample_result
=
2973 evaluate_buffer_usage_condition(condition
,
2974 previous_sample
, channel_info
->capacity
);
2976 latest_sample_result
= evaluate_buffer_usage_condition(
2977 condition
, latest_sample
,
2978 channel_info
->capacity
);
2980 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2981 if (caa_likely(previous_sample_available
)) {
2982 previous_sample_result
=
2983 evaluate_session_consumed_size_condition(
2985 previous_session_consumed_total
);
2987 latest_sample_result
=
2988 evaluate_session_consumed_size_condition(
2990 latest_session_consumed_total
);
2993 /* Unknown condition type; internal error. */
2997 if (!latest_sample_result
||
2998 (previous_sample_result
== latest_sample_result
)) {
3000 * Only trigger on a condition evaluation transition.
3002 * NOTE: This edge-triggered logic may not be appropriate for
3003 * future condition types.
3008 if (!evaluation
|| !latest_sample_result
) {
3012 switch (condition_type
) {
3013 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3014 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3015 *evaluation
= lttng_evaluation_buffer_usage_create(
3017 latest_sample
->highest_usage
,
3018 channel_info
->capacity
);
3020 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3021 *evaluation
= lttng_evaluation_session_consumed_size_create(
3022 latest_session_consumed_total
);
3037 int client_enqueue_dropped_notification(struct notification_client
*client
,
3038 struct notification_thread_state
*state
)
3041 struct lttng_notification_channel_message msg
= {
3042 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3046 ret
= lttng_dynamic_buffer_append(
3047 &client
->communication
.outbound
.buffer
, &msg
,
3053 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3054 const struct lttng_evaluation
*evaluation
,
3055 struct notification_client_list
* client_list
,
3056 struct notification_thread_state
*state
,
3057 uid_t channel_uid
, gid_t channel_gid
)
3060 struct lttng_dynamic_buffer msg_buffer
;
3061 struct notification_client_list_element
*client_list_element
, *tmp
;
3062 const struct lttng_notification notification
= {
3063 .condition
= (struct lttng_condition
*) lttng_trigger_get_const_condition(trigger
),
3064 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3066 struct lttng_notification_channel_message msg_header
= {
3067 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3070 lttng_dynamic_buffer_init(&msg_buffer
);
3072 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg_header
,
3073 sizeof(msg_header
));
3078 ret
= lttng_notification_serialize(¬ification
, &msg_buffer
);
3080 ERR("[notification-thread] Failed to serialize notification");
3085 /* Update payload size. */
3086 ((struct lttng_notification_channel_message
* ) msg_buffer
.data
)->size
=
3087 (uint32_t) (msg_buffer
.size
- sizeof(msg_header
));
3089 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3090 &client_list
->list
, node
) {
3091 struct notification_client
*client
=
3092 client_list_element
->client
;
3094 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
3096 /* Client is not allowed to monitor this channel. */
3097 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
3101 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3102 client
->socket
, msg_buffer
.size
);
3103 if (client
->communication
.outbound
.buffer
.size
) {
3105 * Outgoing data is already buffered for this client;
3106 * drop the notification and enqueue a "dropped
3107 * notification" message if this is the first dropped
3108 * notification since the socket spilled-over to the
3111 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
3113 if (!client
->communication
.outbound
.dropped_notification
) {
3114 client
->communication
.outbound
.dropped_notification
= true;
3115 ret
= client_enqueue_dropped_notification(
3124 ret
= lttng_dynamic_buffer_append_buffer(
3125 &client
->communication
.outbound
.buffer
,
3131 ret
= client_flush_outgoing_queue(client
, state
);
3138 lttng_dynamic_buffer_reset(&msg_buffer
);
3142 int handle_notification_thread_channel_sample(
3143 struct notification_thread_state
*state
, int pipe
,
3144 enum lttng_domain_type domain
)
3147 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
3148 struct channel_info
*channel_info
;
3149 struct cds_lfht_node
*node
;
3150 struct cds_lfht_iter iter
;
3151 struct lttng_channel_trigger_list
*trigger_list
;
3152 struct lttng_trigger_list_element
*trigger_list_element
;
3153 bool previous_sample_available
= false;
3154 struct channel_state_sample previous_sample
, latest_sample
;
3155 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
3158 * The monitoring pipe only holds messages smaller than PIPE_BUF,
3159 * ensuring that read/write of sampling messages are atomic.
3161 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
3162 if (ret
!= sizeof(sample_msg
)) {
3163 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
3170 latest_sample
.key
.key
= sample_msg
.key
;
3171 latest_sample
.key
.domain
= domain
;
3172 latest_sample
.highest_usage
= sample_msg
.highest
;
3173 latest_sample
.lowest_usage
= sample_msg
.lowest
;
3174 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
3178 /* Retrieve the channel's informations */
3179 cds_lfht_lookup(state
->channels_ht
,
3180 hash_channel_key(&latest_sample
.key
),
3184 node
= cds_lfht_iter_get_node(&iter
);
3185 if (caa_unlikely(!node
)) {
3187 * Not an error since the consumer can push a sample to the pipe
3188 * and the rest of the session daemon could notify us of the
3189 * channel's destruction before we get a chance to process that
3192 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
3193 latest_sample
.key
.key
,
3194 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
3198 channel_info
= caa_container_of(node
, struct channel_info
,
3200 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
3202 latest_sample
.key
.key
,
3203 channel_info
->session_info
->name
,
3204 latest_sample
.highest_usage
,
3205 latest_sample
.lowest_usage
,
3206 latest_sample
.channel_total_consumed
);
3208 previous_session_consumed_total
=
3209 channel_info
->session_info
->consumed_data_size
;
3211 /* Retrieve the channel's last sample, if it exists, and update it. */
3212 cds_lfht_lookup(state
->channel_state_ht
,
3213 hash_channel_key(&latest_sample
.key
),
3214 match_channel_state_sample
,
3217 node
= cds_lfht_iter_get_node(&iter
);
3218 if (caa_likely(node
)) {
3219 struct channel_state_sample
*stored_sample
;
3221 /* Update the sample stored. */
3222 stored_sample
= caa_container_of(node
,
3223 struct channel_state_sample
,
3224 channel_state_ht_node
);
3226 memcpy(&previous_sample
, stored_sample
,
3227 sizeof(previous_sample
));
3228 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
3229 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
3230 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
3231 previous_sample_available
= true;
3233 latest_session_consumed_total
=
3234 previous_session_consumed_total
+
3235 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
3238 * This is the channel's first sample, allocate space for and
3239 * store the new sample.
3241 struct channel_state_sample
*stored_sample
;
3243 stored_sample
= zmalloc(sizeof(*stored_sample
));
3244 if (!stored_sample
) {
3249 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
3250 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
3251 cds_lfht_add(state
->channel_state_ht
,
3252 hash_channel_key(&stored_sample
->key
),
3253 &stored_sample
->channel_state_ht_node
);
3255 latest_session_consumed_total
=
3256 previous_session_consumed_total
+
3257 latest_sample
.channel_total_consumed
;
3260 channel_info
->session_info
->consumed_data_size
=
3261 latest_session_consumed_total
;
3263 /* Find triggers associated with this channel. */
3264 cds_lfht_lookup(state
->channel_triggers_ht
,
3265 hash_channel_key(&latest_sample
.key
),
3266 match_channel_trigger_list
,
3269 node
= cds_lfht_iter_get_node(&iter
);
3270 if (caa_likely(!node
)) {
3274 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
3275 channel_triggers_ht_node
);
3276 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
3278 const struct lttng_condition
*condition
;
3279 const struct lttng_action
*action
;
3280 const struct lttng_trigger
*trigger
;
3281 struct notification_client_list
*client_list
;
3282 struct lttng_evaluation
*evaluation
= NULL
;
3284 trigger
= trigger_list_element
->trigger
;
3285 condition
= lttng_trigger_get_const_condition(trigger
);
3287 action
= lttng_trigger_get_const_action(trigger
);
3289 /* Notify actions are the only type currently supported. */
3290 assert(lttng_action_get_type_const(action
) ==
3291 LTTNG_ACTION_TYPE_NOTIFY
);
3294 * Check if any client is subscribed to the result of this
3297 client_list
= get_client_list_from_condition(state
, condition
);
3298 assert(client_list
);
3299 if (cds_list_empty(&client_list
->list
)) {
3301 * No clients interested in the evaluation's result,
3307 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
3308 previous_sample_available
? &previous_sample
: NULL
,
3310 previous_session_consumed_total
,
3311 latest_session_consumed_total
,
3313 if (caa_unlikely(ret
)) {
3317 if (caa_likely(!evaluation
)) {
3321 /* Dispatch evaluation result to all clients. */
3322 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
3323 evaluation
, client_list
, state
,
3324 channel_info
->session_info
->uid
,
3325 channel_info
->session_info
->gid
);
3326 lttng_evaluation_destroy(evaluation
);
3327 if (caa_unlikely(ret
)) {