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
) {
844 goto end_session_put
;
847 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
848 session_info
->rotation
.id
);
851 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
854 goto end_session_put
;
860 goto end_session_put
;
863 *session_uid
= session_info
->uid
;
864 *session_gid
= session_info
->gid
;
867 session_info_put(session_info
);
872 /* This function must be called with the RCU read lock held. */
874 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
875 const struct lttng_condition
*condition
,
876 struct notification_client
*client
,
877 struct notification_thread_state
*state
)
880 struct lttng_evaluation
*evaluation
= NULL
;
881 struct notification_client_list client_list
= { 0 };
882 struct notification_client_list_element client_list_element
= { 0 };
883 uid_t object_uid
= 0;
884 gid_t object_gid
= 0;
891 switch (get_condition_binding_object(condition
)) {
892 case LTTNG_OBJECT_TYPE_SESSION
:
893 ret
= evaluate_session_condition_for_client(condition
, state
,
894 &evaluation
, &object_uid
, &object_gid
);
896 case LTTNG_OBJECT_TYPE_CHANNEL
:
897 ret
= evaluate_channel_condition_for_client(condition
, state
,
898 &evaluation
, &object_uid
, &object_gid
);
900 case LTTNG_OBJECT_TYPE_NONE
:
903 case LTTNG_OBJECT_TYPE_UNKNOWN
:
910 /* Evaluation yielded nothing. Normal exit. */
911 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
917 * Create a temporary client list with the client currently
920 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
921 CDS_INIT_LIST_HEAD(&client_list
.list
);
922 client_list
.trigger
= trigger
;
924 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
925 client_list_element
.client
= client
;
926 cds_list_add(&client_list_element
.node
, &client_list
.list
);
928 /* Send evaluation result to the newly-subscribed client. */
929 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
930 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
931 state
, object_uid
, object_gid
);
938 int notification_thread_client_subscribe(struct notification_client
*client
,
939 struct lttng_condition
*condition
,
940 struct notification_thread_state
*state
,
941 enum lttng_notification_channel_status
*_status
)
944 struct notification_client_list
*client_list
;
945 struct lttng_condition_list_element
*condition_list_element
= NULL
;
946 struct notification_client_list_element
*client_list_element
= NULL
;
947 enum lttng_notification_channel_status status
=
948 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
951 * Ensure that the client has not already subscribed to this condition
954 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
955 if (lttng_condition_is_equal(condition_list_element
->condition
,
957 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
962 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
963 if (!condition_list_element
) {
967 client_list_element
= zmalloc(sizeof(*client_list_element
));
968 if (!client_list_element
) {
976 * Add the newly-subscribed condition to the client's subscription list.
978 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
979 condition_list_element
->condition
= condition
;
980 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
982 client_list
= get_client_list_from_condition(state
, condition
);
985 * No notification-emiting trigger registered with this
986 * condition. We don't evaluate the condition right away
987 * since this trigger is not registered yet.
989 free(client_list_element
);
994 * The condition to which the client just subscribed is evaluated
995 * at this point so that conditions that are already TRUE result
996 * in a notification being sent out.
998 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1000 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1002 free(client_list_element
);
1007 * Add the client to the list of clients interested in a given trigger
1008 * if a "notification" trigger with a corresponding condition was
1011 client_list_element
->client
= client
;
1012 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1013 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1022 free(condition_list_element
);
1023 free(client_list_element
);
1028 int notification_thread_client_unsubscribe(
1029 struct notification_client
*client
,
1030 struct lttng_condition
*condition
,
1031 struct notification_thread_state
*state
,
1032 enum lttng_notification_channel_status
*_status
)
1034 struct notification_client_list
*client_list
;
1035 struct lttng_condition_list_element
*condition_list_element
,
1037 struct notification_client_list_element
*client_list_element
,
1039 bool condition_found
= false;
1040 enum lttng_notification_channel_status status
=
1041 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1043 /* Remove the condition from the client's condition list. */
1044 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1045 &client
->condition_list
, node
) {
1046 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1051 cds_list_del(&condition_list_element
->node
);
1053 * The caller may be iterating on the client's conditions to
1054 * tear down a client's connection. In this case, the condition
1055 * will be destroyed at the end.
1057 if (condition
!= condition_list_element
->condition
) {
1058 lttng_condition_destroy(
1059 condition_list_element
->condition
);
1061 free(condition_list_element
);
1062 condition_found
= true;
1066 if (!condition_found
) {
1067 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1072 * Remove the client from the list of clients interested the trigger
1073 * matching the condition.
1076 client_list
= get_client_list_from_condition(state
, condition
);
1081 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1082 &client_list
->list
, node
) {
1083 if (client_list_element
->client
->socket
!= client
->socket
) {
1086 cds_list_del(&client_list_element
->node
);
1087 free(client_list_element
);
1093 lttng_condition_destroy(condition
);
1101 void notification_client_destroy(struct notification_client
*client
,
1102 struct notification_thread_state
*state
)
1104 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
1110 /* Release all conditions to which the client was subscribed. */
1111 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
1112 &client
->condition_list
, node
) {
1113 (void) notification_thread_client_unsubscribe(client
,
1114 condition_list_element
->condition
, state
, NULL
);
1117 if (client
->socket
>= 0) {
1118 (void) lttcomm_close_unix_sock(client
->socket
);
1120 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
1121 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
1126 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1130 struct notification_client
*get_client_from_socket(int socket
,
1131 struct notification_thread_state
*state
)
1133 struct cds_lfht_iter iter
;
1134 struct cds_lfht_node
*node
;
1135 struct notification_client
*client
= NULL
;
1137 cds_lfht_lookup(state
->client_socket_ht
,
1138 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
1140 (void *) (unsigned long) socket
,
1142 node
= cds_lfht_iter_get_node(&iter
);
1147 client
= caa_container_of(node
, struct notification_client
,
1148 client_socket_ht_node
);
1154 bool buffer_usage_condition_applies_to_channel(
1155 const struct lttng_condition
*condition
,
1156 const struct channel_info
*channel_info
)
1158 enum lttng_condition_status status
;
1159 enum lttng_domain_type condition_domain
;
1160 const char *condition_session_name
= NULL
;
1161 const char *condition_channel_name
= NULL
;
1163 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1165 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1166 if (channel_info
->key
.domain
!= condition_domain
) {
1170 status
= lttng_condition_buffer_usage_get_session_name(
1171 condition
, &condition_session_name
);
1172 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1174 status
= lttng_condition_buffer_usage_get_channel_name(
1175 condition
, &condition_channel_name
);
1176 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1178 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1181 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1191 bool session_consumed_size_condition_applies_to_channel(
1192 const struct lttng_condition
*condition
,
1193 const struct channel_info
*channel_info
)
1195 enum lttng_condition_status status
;
1196 const char *condition_session_name
= NULL
;
1198 status
= lttng_condition_session_consumed_size_get_session_name(
1199 condition
, &condition_session_name
);
1200 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1202 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1212 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1213 const struct channel_info
*channel_info
)
1215 const struct lttng_condition
*condition
;
1216 bool trigger_applies
;
1218 condition
= lttng_trigger_get_const_condition(trigger
);
1223 switch (lttng_condition_get_type(condition
)) {
1224 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1225 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1226 trigger_applies
= buffer_usage_condition_applies_to_channel(
1227 condition
, channel_info
);
1229 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1230 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1231 condition
, channel_info
);
1237 return trigger_applies
;
1243 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1244 struct notification_client
*client
)
1246 bool applies
= false;
1247 struct lttng_condition_list_element
*condition_list_element
;
1249 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1251 applies
= lttng_condition_is_equal(
1252 condition_list_element
->condition
,
1253 lttng_trigger_get_condition(trigger
));
1261 /* Must be called with RCU read lock held. */
1263 struct lttng_session_trigger_list
*get_session_trigger_list(
1264 struct notification_thread_state
*state
,
1265 const char *session_name
)
1267 struct lttng_session_trigger_list
*list
= NULL
;
1268 struct cds_lfht_node
*node
;
1269 struct cds_lfht_iter iter
;
1271 cds_lfht_lookup(state
->session_triggers_ht
,
1272 hash_key_str(session_name
, lttng_ht_seed
),
1273 match_session_trigger_list
,
1276 node
= cds_lfht_iter_get_node(&iter
);
1279 * Not an error, the list of triggers applying to that session
1280 * will be initialized when the session is created.
1282 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1287 list
= caa_container_of(node
,
1288 struct lttng_session_trigger_list
,
1289 session_triggers_ht_node
);
1295 * Allocate an empty lttng_session_trigger_list for the session named
1298 * No ownership of 'session_name' is assumed by the session trigger list.
1299 * It is the caller's responsability to ensure the session name is alive
1300 * for as long as this list is.
1303 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1304 const char *session_name
,
1305 struct cds_lfht
*session_triggers_ht
)
1307 struct lttng_session_trigger_list
*list
;
1309 list
= zmalloc(sizeof(*list
));
1313 list
->session_name
= session_name
;
1314 CDS_INIT_LIST_HEAD(&list
->list
);
1315 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1316 list
->session_triggers_ht
= session_triggers_ht
;
1319 /* Publish the list through the session_triggers_ht. */
1320 cds_lfht_add(session_triggers_ht
,
1321 hash_key_str(session_name
, lttng_ht_seed
),
1322 &list
->session_triggers_ht_node
);
1329 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1331 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1336 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1338 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1340 /* Empty the list element by element, and then free the list itself. */
1341 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1342 &list
->list
, node
) {
1343 cds_list_del(&trigger_list_element
->node
);
1344 free(trigger_list_element
);
1347 /* Unpublish the list from the session_triggers_ht. */
1348 cds_lfht_del(list
->session_triggers_ht
,
1349 &list
->session_triggers_ht_node
);
1351 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1355 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1356 const struct lttng_trigger
*trigger
)
1359 struct lttng_trigger_list_element
*new_element
=
1360 zmalloc(sizeof(*new_element
));
1366 CDS_INIT_LIST_HEAD(&new_element
->node
);
1367 new_element
->trigger
= trigger
;
1368 cds_list_add(&new_element
->node
, &list
->list
);
1374 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1375 const char *session_name
)
1377 bool applies
= false;
1378 const struct lttng_condition
*condition
;
1380 condition
= lttng_trigger_get_const_condition(trigger
);
1381 switch (lttng_condition_get_type(condition
)) {
1382 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1383 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1385 enum lttng_condition_status condition_status
;
1386 const char *condition_session_name
;
1388 condition_status
= lttng_condition_session_rotation_get_session_name(
1389 condition
, &condition_session_name
);
1390 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1391 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1395 assert(condition_session_name
);
1396 applies
= !strcmp(condition_session_name
, session_name
);
1407 * Allocate and initialize an lttng_session_trigger_list which contains
1408 * all triggers that apply to the session named 'session_name'.
1410 * No ownership of 'session_name' is assumed by the session trigger list.
1411 * It is the caller's responsability to ensure the session name is alive
1412 * for as long as this list is.
1415 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1416 const struct notification_thread_state
*state
,
1417 const char *session_name
)
1419 int trigger_count
= 0;
1420 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1421 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1422 struct cds_lfht_iter iter
;
1424 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1425 state
->session_triggers_ht
);
1427 /* Add all triggers applying to the session named 'session_name'. */
1428 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1432 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1437 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1438 trigger_ht_element
->trigger
);
1446 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1448 return session_trigger_list
;
1450 lttng_session_trigger_list_destroy(session_trigger_list
);
1455 struct session_info
*find_or_create_session_info(
1456 struct notification_thread_state
*state
,
1457 const char *name
, uid_t uid
, gid_t gid
)
1459 struct session_info
*session
= NULL
;
1460 struct cds_lfht_node
*node
;
1461 struct cds_lfht_iter iter
;
1462 struct lttng_session_trigger_list
*trigger_list
;
1465 cds_lfht_lookup(state
->sessions_ht
,
1466 hash_key_str(name
, lttng_ht_seed
),
1470 node
= cds_lfht_iter_get_node(&iter
);
1472 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1474 session
= caa_container_of(node
, struct session_info
,
1476 assert(session
->uid
== uid
);
1477 assert(session
->gid
== gid
);
1478 session_info_get(session
);
1482 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1483 if (!trigger_list
) {
1487 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1488 state
->sessions_ht
);
1490 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1494 trigger_list
= NULL
;
1496 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1497 &session
->sessions_ht_node
);
1503 session_info_put(session
);
1508 int handle_notification_thread_command_add_channel(
1509 struct notification_thread_state
*state
,
1510 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1511 const char *channel_name
, enum lttng_domain_type channel_domain
,
1512 uint64_t channel_key_int
, uint64_t channel_capacity
,
1513 enum lttng_error_code
*cmd_result
)
1515 struct cds_list_head trigger_list
;
1516 struct channel_info
*new_channel_info
= NULL
;
1517 struct channel_key channel_key
= {
1518 .key
= channel_key_int
,
1519 .domain
= channel_domain
,
1521 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1522 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1523 int trigger_count
= 0;
1524 struct cds_lfht_iter iter
;
1525 struct session_info
*session_info
= NULL
;
1527 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1528 channel_name
, session_name
, channel_key_int
,
1529 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1531 CDS_INIT_LIST_HEAD(&trigger_list
);
1533 session_info
= find_or_create_session_info(state
, session_name
,
1534 session_uid
, session_gid
);
1535 if (!session_info
) {
1536 /* Allocation error or an internal error occured. */
1540 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1541 channel_capacity
, session_info
);
1542 if (!new_channel_info
) {
1546 /* Build a list of all triggers applying to the new channel. */
1547 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1549 struct lttng_trigger_list_element
*new_element
;
1551 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1552 new_channel_info
)) {
1556 new_element
= zmalloc(sizeof(*new_element
));
1560 CDS_INIT_LIST_HEAD(&new_element
->node
);
1561 new_element
->trigger
= trigger_ht_element
->trigger
;
1562 cds_list_add(&new_element
->node
, &trigger_list
);
1566 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1568 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1569 if (!channel_trigger_list
) {
1572 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1573 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1574 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1575 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1578 /* Add channel to the channel_ht which owns the channel_infos. */
1579 cds_lfht_add(state
->channels_ht
,
1580 hash_channel_key(&new_channel_info
->key
),
1581 &new_channel_info
->channels_ht_node
);
1583 * Add the list of triggers associated with this channel to the
1584 * channel_triggers_ht.
1586 cds_lfht_add(state
->channel_triggers_ht
,
1587 hash_channel_key(&new_channel_info
->key
),
1588 &channel_trigger_list
->channel_triggers_ht_node
);
1590 session_info_put(session_info
);
1591 *cmd_result
= LTTNG_OK
;
1594 channel_info_destroy(new_channel_info
);
1595 session_info_put(session_info
);
1600 int handle_notification_thread_command_remove_channel(
1601 struct notification_thread_state
*state
,
1602 uint64_t channel_key
, enum lttng_domain_type domain
,
1603 enum lttng_error_code
*cmd_result
)
1605 struct cds_lfht_node
*node
;
1606 struct cds_lfht_iter iter
;
1607 struct lttng_channel_trigger_list
*trigger_list
;
1608 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1609 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1610 struct channel_info
*channel_info
;
1612 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1613 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1617 cds_lfht_lookup(state
->channel_triggers_ht
,
1618 hash_channel_key(&key
),
1619 match_channel_trigger_list
,
1622 node
= cds_lfht_iter_get_node(&iter
);
1624 * There is a severe internal error if we are being asked to remove a
1625 * channel that doesn't exist.
1628 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1632 /* Free the list of triggers associated with this channel. */
1633 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1634 channel_triggers_ht_node
);
1635 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1636 &trigger_list
->list
, node
) {
1637 cds_list_del(&trigger_list_element
->node
);
1638 free(trigger_list_element
);
1640 cds_lfht_del(state
->channel_triggers_ht
, node
);
1643 /* Free sampled channel state. */
1644 cds_lfht_lookup(state
->channel_state_ht
,
1645 hash_channel_key(&key
),
1646 match_channel_state_sample
,
1649 node
= cds_lfht_iter_get_node(&iter
);
1651 * This is expected to be NULL if the channel is destroyed before we
1652 * received a sample.
1655 struct channel_state_sample
*sample
= caa_container_of(node
,
1656 struct channel_state_sample
,
1657 channel_state_ht_node
);
1659 cds_lfht_del(state
->channel_state_ht
, node
);
1663 /* Remove the channel from the channels_ht and free it. */
1664 cds_lfht_lookup(state
->channels_ht
,
1665 hash_channel_key(&key
),
1669 node
= cds_lfht_iter_get_node(&iter
);
1671 channel_info
= caa_container_of(node
, struct channel_info
,
1673 cds_lfht_del(state
->channels_ht
, node
);
1674 channel_info_destroy(channel_info
);
1677 *cmd_result
= LTTNG_OK
;
1682 int handle_notification_thread_command_session_rotation(
1683 struct notification_thread_state
*state
,
1684 enum notification_thread_command_type cmd_type
,
1685 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1686 uint64_t trace_archive_chunk_id
,
1687 struct lttng_trace_archive_location
*location
,
1688 enum lttng_error_code
*_cmd_result
)
1691 enum lttng_error_code cmd_result
= LTTNG_OK
;
1692 struct lttng_session_trigger_list
*trigger_list
;
1693 struct lttng_trigger_list_element
*trigger_list_element
;
1694 struct session_info
*session_info
;
1698 session_info
= find_or_create_session_info(state
, session_name
,
1699 session_uid
, session_gid
);
1700 if (!session_info
) {
1701 /* Allocation error or an internal error occured. */
1703 cmd_result
= LTTNG_ERR_NOMEM
;
1707 session_info
->rotation
.ongoing
=
1708 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1709 session_info
->rotation
.id
= trace_archive_chunk_id
;
1710 trigger_list
= get_session_trigger_list(state
, session_name
);
1711 if (!trigger_list
) {
1712 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1717 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1719 const struct lttng_condition
*condition
;
1720 const struct lttng_action
*action
;
1721 const struct lttng_trigger
*trigger
;
1722 struct notification_client_list
*client_list
;
1723 struct lttng_evaluation
*evaluation
= NULL
;
1724 enum lttng_condition_type condition_type
;
1726 trigger
= trigger_list_element
->trigger
;
1727 condition
= lttng_trigger_get_const_condition(trigger
);
1729 condition_type
= lttng_condition_get_type(condition
);
1731 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1732 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1734 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1735 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1739 action
= lttng_trigger_get_const_action(trigger
);
1741 /* Notify actions are the only type currently supported. */
1742 assert(lttng_action_get_type_const(action
) ==
1743 LTTNG_ACTION_TYPE_NOTIFY
);
1745 client_list
= get_client_list_from_condition(state
, condition
);
1746 assert(client_list
);
1748 if (cds_list_empty(&client_list
->list
)) {
1750 * No clients interested in the evaluation's result,
1756 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1757 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1758 trace_archive_chunk_id
);
1760 evaluation
= lttng_evaluation_session_rotation_completed_create(
1761 trace_archive_chunk_id
, location
);
1765 /* Internal error */
1767 cmd_result
= LTTNG_ERR_UNK
;
1771 /* Dispatch evaluation result to all clients. */
1772 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
1773 evaluation
, client_list
, state
,
1776 lttng_evaluation_destroy(evaluation
);
1777 if (caa_unlikely(ret
)) {
1782 session_info_put(session_info
);
1783 *_cmd_result
= cmd_result
;
1789 int condition_is_supported(struct lttng_condition
*condition
)
1793 switch (lttng_condition_get_type(condition
)) {
1794 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1795 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1797 enum lttng_domain_type domain
;
1799 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1806 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1812 * Older kernel tracers don't expose the API to monitor their
1813 * buffers. Therefore, we reject triggers that require that
1814 * mechanism to be available to be evaluated.
1816 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1827 /* Must be called with RCU read lock held. */
1829 int bind_trigger_to_matching_session(const struct lttng_trigger
*trigger
,
1830 struct notification_thread_state
*state
)
1833 const struct lttng_condition
*condition
;
1834 const char *session_name
;
1835 struct lttng_session_trigger_list
*trigger_list
;
1837 condition
= lttng_trigger_get_const_condition(trigger
);
1838 switch (lttng_condition_get_type(condition
)) {
1839 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1840 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1842 enum lttng_condition_status status
;
1844 status
= lttng_condition_session_rotation_get_session_name(
1845 condition
, &session_name
);
1846 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1847 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
1858 trigger_list
= get_session_trigger_list(state
, session_name
);
1859 if (!trigger_list
) {
1860 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
1866 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
1868 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
1873 /* Must be called with RCU read lock held. */
1875 int bind_trigger_to_matching_channels(const struct lttng_trigger
*trigger
,
1876 struct notification_thread_state
*state
)
1879 struct cds_lfht_node
*node
;
1880 struct cds_lfht_iter iter
;
1881 struct channel_info
*channel
;
1883 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1885 struct lttng_trigger_list_element
*trigger_list_element
;
1886 struct lttng_channel_trigger_list
*trigger_list
;
1888 if (!trigger_applies_to_channel(trigger
, channel
)) {
1892 cds_lfht_lookup(state
->channel_triggers_ht
,
1893 hash_channel_key(&channel
->key
),
1894 match_channel_trigger_list
,
1897 node
= cds_lfht_iter_get_node(&iter
);
1899 trigger_list
= caa_container_of(node
,
1900 struct lttng_channel_trigger_list
,
1901 channel_triggers_ht_node
);
1903 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1904 if (!trigger_list_element
) {
1908 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1909 trigger_list_element
->trigger
= trigger
;
1910 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1911 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
1919 * FIXME A client's credentials are not checked when registering a trigger, nor
1920 * are they stored alongside with the trigger.
1922 * The effects of this are benign since:
1923 * - The client will succeed in registering the trigger, as it is valid,
1924 * - The trigger will, internally, be bound to the channel/session,
1925 * - The notifications will not be sent since the client's credentials
1926 * are checked against the channel at that moment.
1928 * If this function returns a non-zero value, it means something is
1929 * fundamentally broken and the whole subsystem/thread will be torn down.
1931 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1935 int handle_notification_thread_command_register_trigger(
1936 struct notification_thread_state
*state
,
1937 struct lttng_trigger
*trigger
,
1938 enum lttng_error_code
*cmd_result
)
1941 struct lttng_condition
*condition
;
1942 struct notification_client
*client
;
1943 struct notification_client_list
*client_list
= NULL
;
1944 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1945 struct notification_client_list_element
*client_list_element
, *tmp
;
1946 struct cds_lfht_node
*node
;
1947 struct cds_lfht_iter iter
;
1948 bool free_trigger
= true;
1952 condition
= lttng_trigger_get_condition(trigger
);
1955 ret
= condition_is_supported(condition
);
1958 } else if (ret
== 0) {
1959 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1962 /* Feature is supported, continue. */
1966 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1967 if (!trigger_ht_element
) {
1972 /* Add trigger to the trigger_ht. */
1973 cds_lfht_node_init(&trigger_ht_element
->node
);
1974 trigger_ht_element
->trigger
= trigger
;
1976 node
= cds_lfht_add_unique(state
->triggers_ht
,
1977 lttng_condition_hash(condition
),
1980 &trigger_ht_element
->node
);
1981 if (node
!= &trigger_ht_element
->node
) {
1982 /* Not a fatal error, simply report it to the client. */
1983 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1984 goto error_free_ht_element
;
1988 * Ownership of the trigger and of its wrapper was transfered to
1991 trigger_ht_element
= NULL
;
1992 free_trigger
= false;
1995 * The rest only applies to triggers that have a "notify" action.
1996 * It is not skipped as this is the only action type currently
1999 client_list
= zmalloc(sizeof(*client_list
));
2002 goto error_free_ht_element
;
2004 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
2005 CDS_INIT_LIST_HEAD(&client_list
->list
);
2006 client_list
->trigger
= trigger
;
2008 /* Build a list of clients to which this new trigger applies. */
2009 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2010 client_socket_ht_node
) {
2011 if (!trigger_applies_to_client(trigger
, client
)) {
2015 client_list_element
= zmalloc(sizeof(*client_list_element
));
2016 if (!client_list_element
) {
2018 goto error_free_client_list
;
2020 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2021 client_list_element
->client
= client
;
2022 cds_list_add(&client_list_element
->node
, &client_list
->list
);
2025 cds_lfht_add(state
->notification_trigger_clients_ht
,
2026 lttng_condition_hash(condition
),
2027 &client_list
->notification_trigger_ht_node
);
2029 switch (get_condition_binding_object(condition
)) {
2030 case LTTNG_OBJECT_TYPE_SESSION
:
2031 /* Add the trigger to the list if it matches a known session. */
2032 ret
= bind_trigger_to_matching_session(trigger
, state
);
2034 goto error_free_client_list
;
2037 case LTTNG_OBJECT_TYPE_CHANNEL
:
2039 * Add the trigger to list of triggers bound to the channels
2042 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2044 goto error_free_client_list
;
2047 case LTTNG_OBJECT_TYPE_NONE
:
2050 ERR("[notification-thread] Unknown object type on which to bind a newly registered trigger was encountered");
2052 goto error_free_client_list
;
2056 * Since there is nothing preventing clients from subscribing to a
2057 * condition before the corresponding trigger is registered, we have
2058 * to evaluate this new condition right away.
2060 * At some point, we were waiting for the next "evaluation" (e.g. on
2061 * reception of a channel sample) to evaluate this new condition, but
2064 * The reason it was broken is that waiting for the next sample
2065 * does not allow us to properly handle transitions for edge-triggered
2068 * Consider this example: when we handle a new channel sample, we
2069 * evaluate each conditions twice: once with the previous state, and
2070 * again with the newest state. We then use those two results to
2071 * determine whether a state change happened: a condition was false and
2072 * became true. If a state change happened, we have to notify clients.
2074 * Now, if a client subscribes to a given notification and registers
2075 * a trigger *after* that subscription, we have to make sure the
2076 * condition is evaluated at this point while considering only the
2077 * current state. Otherwise, the next evaluation cycle may only see
2078 * that the evaluations remain the same (true for samples n-1 and n) and
2079 * the client will never know that the condition has been met.
2081 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2082 &client_list
->list
, node
) {
2083 ret
= evaluate_condition_for_client(trigger
, condition
,
2084 client_list_element
->client
, state
);
2086 goto error_free_client_list
;
2091 * Client list ownership transferred to the
2092 * notification_trigger_clients_ht.
2096 *cmd_result
= LTTNG_OK
;
2097 error_free_client_list
:
2099 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2100 &client_list
->list
, node
) {
2101 free(client_list_element
);
2105 error_free_ht_element
:
2106 free(trigger_ht_element
);
2109 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
2111 lttng_condition_destroy(condition
);
2112 lttng_action_destroy(action
);
2113 lttng_trigger_destroy(trigger
);
2120 int handle_notification_thread_command_unregister_trigger(
2121 struct notification_thread_state
*state
,
2122 struct lttng_trigger
*trigger
,
2123 enum lttng_error_code
*_cmd_reply
)
2125 struct cds_lfht_iter iter
;
2126 struct cds_lfht_node
*triggers_ht_node
;
2127 struct lttng_channel_trigger_list
*trigger_list
;
2128 struct notification_client_list
*client_list
;
2129 struct notification_client_list_element
*client_list_element
, *tmp
;
2130 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2131 struct lttng_condition
*condition
= lttng_trigger_get_condition(
2133 struct lttng_action
*action
;
2134 enum lttng_error_code cmd_reply
;
2138 cds_lfht_lookup(state
->triggers_ht
,
2139 lttng_condition_hash(condition
),
2143 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2144 if (!triggers_ht_node
) {
2145 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2148 cmd_reply
= LTTNG_OK
;
2151 /* Remove trigger from channel_triggers_ht. */
2152 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2153 channel_triggers_ht_node
) {
2154 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2156 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2157 &trigger_list
->list
, node
) {
2158 const struct lttng_condition
*current_condition
=
2159 lttng_trigger_get_const_condition(
2160 trigger_element
->trigger
);
2162 assert(current_condition
);
2163 if (!lttng_condition_is_equal(condition
,
2164 current_condition
)) {
2168 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2169 cds_list_del(&trigger_element
->node
);
2170 /* A trigger can only appear once per channel */
2176 * Remove and release the client list from
2177 * notification_trigger_clients_ht.
2179 client_list
= get_client_list_from_condition(state
, condition
);
2180 assert(client_list
);
2182 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2183 &client_list
->list
, node
) {
2184 free(client_list_element
);
2186 cds_lfht_del(state
->notification_trigger_clients_ht
,
2187 &client_list
->notification_trigger_ht_node
);
2190 /* Remove trigger from triggers_ht. */
2191 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2192 struct lttng_trigger_ht_element
, node
);
2193 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2195 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
2196 lttng_condition_destroy(condition
);
2197 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
2198 lttng_action_destroy(action
);
2199 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2200 free(trigger_ht_element
);
2204 *_cmd_reply
= cmd_reply
;
2209 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2210 int handle_notification_thread_command(
2211 struct notification_thread_handle
*handle
,
2212 struct notification_thread_state
*state
)
2216 struct notification_thread_command
*cmd
;
2218 /* Read the event pipe to put it back into a quiescent state. */
2219 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2225 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2226 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2227 struct notification_thread_command
, cmd_list_node
);
2228 switch (cmd
->type
) {
2229 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2230 DBG("[notification-thread] Received register trigger command");
2231 ret
= handle_notification_thread_command_register_trigger(
2232 state
, cmd
->parameters
.trigger
,
2235 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2236 DBG("[notification-thread] Received unregister trigger command");
2237 ret
= handle_notification_thread_command_unregister_trigger(
2238 state
, cmd
->parameters
.trigger
,
2241 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2242 DBG("[notification-thread] Received add channel command");
2243 ret
= handle_notification_thread_command_add_channel(
2245 cmd
->parameters
.add_channel
.session
.name
,
2246 cmd
->parameters
.add_channel
.session
.uid
,
2247 cmd
->parameters
.add_channel
.session
.gid
,
2248 cmd
->parameters
.add_channel
.channel
.name
,
2249 cmd
->parameters
.add_channel
.channel
.domain
,
2250 cmd
->parameters
.add_channel
.channel
.key
,
2251 cmd
->parameters
.add_channel
.channel
.capacity
,
2254 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2255 DBG("[notification-thread] Received remove channel command");
2256 ret
= handle_notification_thread_command_remove_channel(
2257 state
, cmd
->parameters
.remove_channel
.key
,
2258 cmd
->parameters
.remove_channel
.domain
,
2261 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2262 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2263 DBG("[notification-thread] Received session rotation %s command",
2264 cmd
->type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
?
2265 "ongoing" : "completed");
2266 ret
= handle_notification_thread_command_session_rotation(
2269 cmd
->parameters
.session_rotation
.session_name
,
2270 cmd
->parameters
.session_rotation
.uid
,
2271 cmd
->parameters
.session_rotation
.gid
,
2272 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2273 cmd
->parameters
.session_rotation
.location
,
2276 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2277 DBG("[notification-thread] Received quit command");
2278 cmd
->reply_code
= LTTNG_OK
;
2282 ERR("[notification-thread] Unknown internal command received");
2290 cds_list_del(&cmd
->cmd_list_node
);
2291 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2292 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2295 /* Wake-up and return a fatal error to the calling thread. */
2296 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2297 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2298 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2300 /* Indicate a fatal error to the caller. */
2305 unsigned long hash_client_socket(int socket
)
2307 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
2311 int socket_set_non_blocking(int socket
)
2315 /* Set the pipe as non-blocking. */
2316 ret
= fcntl(socket
, F_GETFL
, 0);
2318 PERROR("fcntl get socket flags");
2323 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2325 PERROR("fcntl set O_NONBLOCK socket flag");
2328 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2334 int client_reset_inbound_state(struct notification_client
*client
)
2338 ret
= lttng_dynamic_buffer_set_size(
2339 &client
->communication
.inbound
.buffer
, 0);
2342 client
->communication
.inbound
.bytes_to_receive
=
2343 sizeof(struct lttng_notification_channel_message
);
2344 client
->communication
.inbound
.msg_type
=
2345 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2346 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2347 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2348 ret
= lttng_dynamic_buffer_set_size(
2349 &client
->communication
.inbound
.buffer
,
2350 client
->communication
.inbound
.bytes_to_receive
);
2354 int handle_notification_thread_client_connect(
2355 struct notification_thread_state
*state
)
2358 struct notification_client
*client
;
2360 DBG("[notification-thread] Handling new notification channel client connection");
2362 client
= zmalloc(sizeof(*client
));
2368 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2369 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
2370 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
2371 client
->communication
.inbound
.expect_creds
= true;
2372 ret
= client_reset_inbound_state(client
);
2374 ERR("[notification-thread] Failed to reset client communication's inbound state");
2379 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2381 ERR("[notification-thread] Failed to accept new notification channel client connection");
2386 client
->socket
= ret
;
2388 ret
= socket_set_non_blocking(client
->socket
);
2390 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2394 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2396 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2401 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2402 LPOLLIN
| LPOLLERR
|
2403 LPOLLHUP
| LPOLLRDHUP
);
2405 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2409 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2413 cds_lfht_add(state
->client_socket_ht
,
2414 hash_client_socket(client
->socket
),
2415 &client
->client_socket_ht_node
);
2420 notification_client_destroy(client
, state
);
2424 int handle_notification_thread_client_disconnect(
2426 struct notification_thread_state
*state
)
2429 struct notification_client
*client
;
2432 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2434 client
= get_client_from_socket(client_socket
, state
);
2436 /* Internal state corruption, fatal error. */
2437 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2443 ret
= lttng_poll_del(&state
->events
, client_socket
);
2445 ERR("[notification-thread] Failed to remove client socket from poll set");
2447 cds_lfht_del(state
->client_socket_ht
,
2448 &client
->client_socket_ht_node
);
2449 notification_client_destroy(client
, state
);
2455 int handle_notification_thread_client_disconnect_all(
2456 struct notification_thread_state
*state
)
2458 struct cds_lfht_iter iter
;
2459 struct notification_client
*client
;
2460 bool error_encoutered
= false;
2463 DBG("[notification-thread] Closing all client connections");
2464 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2465 client_socket_ht_node
) {
2468 ret
= handle_notification_thread_client_disconnect(
2469 client
->socket
, state
);
2471 error_encoutered
= true;
2475 return error_encoutered
? 1 : 0;
2478 int handle_notification_thread_trigger_unregister_all(
2479 struct notification_thread_state
*state
)
2481 bool error_occurred
= false;
2482 struct cds_lfht_iter iter
;
2483 struct lttng_trigger_ht_element
*trigger_ht_element
;
2485 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2487 int ret
= handle_notification_thread_command_unregister_trigger(
2488 state
, trigger_ht_element
->trigger
, NULL
);
2490 error_occurred
= true;
2493 return error_occurred
? -1 : 0;
2497 int client_flush_outgoing_queue(struct notification_client
*client
,
2498 struct notification_thread_state
*state
)
2501 size_t to_send_count
;
2503 assert(client
->communication
.outbound
.buffer
.size
!= 0);
2504 to_send_count
= client
->communication
.outbound
.buffer
.size
;
2505 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
2508 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
2509 client
->communication
.outbound
.buffer
.data
,
2511 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
2512 (ret
> 0 && ret
< to_send_count
)) {
2513 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
2515 to_send_count
-= max(ret
, 0);
2517 memcpy(client
->communication
.outbound
.buffer
.data
,
2518 client
->communication
.outbound
.buffer
.data
+
2519 client
->communication
.outbound
.buffer
.size
- to_send_count
,
2521 ret
= lttng_dynamic_buffer_set_size(
2522 &client
->communication
.outbound
.buffer
,
2529 * We want to be notified whenever there is buffer space
2530 * available to send the rest of the payload.
2532 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2533 CLIENT_POLL_MASK_IN_OUT
);
2537 } else if (ret
< 0) {
2538 /* Generic error, disconnect the client. */
2539 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
2541 ret
= handle_notification_thread_client_disconnect(
2542 client
->socket
, state
);
2547 /* No error and flushed the queue completely. */
2548 ret
= lttng_dynamic_buffer_set_size(
2549 &client
->communication
.outbound
.buffer
, 0);
2553 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2554 CLIENT_POLL_MASK_IN
);
2559 client
->communication
.outbound
.queued_command_reply
= false;
2560 client
->communication
.outbound
.dropped_notification
= false;
2569 int client_send_command_reply(struct notification_client
*client
,
2570 struct notification_thread_state
*state
,
2571 enum lttng_notification_channel_status status
)
2574 struct lttng_notification_channel_command_reply reply
= {
2575 .status
= (int8_t) status
,
2577 struct lttng_notification_channel_message msg
= {
2578 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
2579 .size
= sizeof(reply
),
2581 char buffer
[sizeof(msg
) + sizeof(reply
)];
2583 if (client
->communication
.outbound
.queued_command_reply
) {
2584 /* Protocol error. */
2588 memcpy(buffer
, &msg
, sizeof(msg
));
2589 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
2590 DBG("[notification-thread] Send command reply (%i)", (int) status
);
2592 /* Enqueue buffer to outgoing queue and flush it. */
2593 ret
= lttng_dynamic_buffer_append(
2594 &client
->communication
.outbound
.buffer
,
2595 buffer
, sizeof(buffer
));
2600 ret
= client_flush_outgoing_queue(client
, state
);
2605 if (client
->communication
.outbound
.buffer
.size
!= 0) {
2606 /* Queue could not be emptied. */
2607 client
->communication
.outbound
.queued_command_reply
= true;
2616 int client_dispatch_message(struct notification_client
*client
,
2617 struct notification_thread_state
*state
)
2621 if (client
->communication
.inbound
.msg_type
!=
2622 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
2623 client
->communication
.inbound
.msg_type
!=
2624 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
2625 !client
->validated
) {
2626 WARN("[notification-thread] client attempted a command before handshake");
2631 switch (client
->communication
.inbound
.msg_type
) {
2632 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
2635 * Receiving message header. The function will be called again
2636 * once the rest of the message as been received and can be
2639 const struct lttng_notification_channel_message
*msg
;
2641 assert(sizeof(*msg
) ==
2642 client
->communication
.inbound
.buffer
.size
);
2643 msg
= (const struct lttng_notification_channel_message
*)
2644 client
->communication
.inbound
.buffer
.data
;
2646 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
2647 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
2652 switch (msg
->type
) {
2653 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2654 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2655 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2659 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
2663 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
2664 client
->communication
.inbound
.msg_type
=
2665 (enum lttng_notification_channel_message_type
) msg
->type
;
2666 ret
= lttng_dynamic_buffer_set_size(
2667 &client
->communication
.inbound
.buffer
, msg
->size
);
2673 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2675 struct lttng_notification_channel_command_handshake
*handshake_client
;
2676 struct lttng_notification_channel_command_handshake handshake_reply
= {
2677 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
2678 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2680 struct lttng_notification_channel_message msg_header
= {
2681 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2682 .size
= sizeof(handshake_reply
),
2684 enum lttng_notification_channel_status status
=
2685 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2686 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2688 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2689 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2690 sizeof(handshake_reply
));
2693 (struct lttng_notification_channel_command_handshake
*)
2694 client
->communication
.inbound
.buffer
.data
;
2695 client
->major
= handshake_client
->major
;
2696 client
->minor
= handshake_client
->minor
;
2697 if (!client
->communication
.inbound
.creds_received
) {
2698 ERR("[notification-thread] No credentials received from client");
2703 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2704 &client
->communication
.inbound
.creds
);
2705 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2706 &client
->communication
.inbound
.creds
);
2707 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2708 client
->uid
, client
->gid
, (int) client
->major
,
2709 (int) client
->minor
);
2711 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2712 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2715 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2716 send_buffer
, sizeof(send_buffer
));
2718 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2722 ret
= client_flush_outgoing_queue(client
, state
);
2727 ret
= client_send_command_reply(client
, state
, status
);
2729 ERR("[notification-thread] Failed to send reply to notification channel client");
2733 /* Set reception state to receive the next message header. */
2734 ret
= client_reset_inbound_state(client
);
2736 ERR("[notification-thread] Failed to reset client communication's inbound state");
2739 client
->validated
= true;
2742 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2743 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2745 struct lttng_condition
*condition
;
2746 enum lttng_notification_channel_status status
=
2747 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2748 const struct lttng_buffer_view condition_view
=
2749 lttng_buffer_view_from_dynamic_buffer(
2750 &client
->communication
.inbound
.buffer
,
2752 size_t expected_condition_size
=
2753 client
->communication
.inbound
.buffer
.size
;
2755 ret
= lttng_condition_create_from_buffer(&condition_view
,
2757 if (ret
!= expected_condition_size
) {
2758 ERR("[notification-thread] Malformed condition received from client");
2762 if (client
->communication
.inbound
.msg_type
==
2763 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2764 ret
= notification_thread_client_subscribe(client
,
2765 condition
, state
, &status
);
2767 ret
= notification_thread_client_unsubscribe(client
,
2768 condition
, state
, &status
);
2774 ret
= client_send_command_reply(client
, state
, status
);
2776 ERR("[notification-thread] Failed to send reply to notification channel client");
2780 /* Set reception state to receive the next message header. */
2781 ret
= client_reset_inbound_state(client
);
2783 ERR("[notification-thread] Failed to reset client communication's inbound state");
2795 /* Incoming data from client. */
2796 int handle_notification_thread_client_in(
2797 struct notification_thread_state
*state
, int socket
)
2800 struct notification_client
*client
;
2804 client
= get_client_from_socket(socket
, state
);
2806 /* Internal error, abort. */
2811 offset
= client
->communication
.inbound
.buffer
.size
-
2812 client
->communication
.inbound
.bytes_to_receive
;
2813 if (client
->communication
.inbound
.expect_creds
) {
2814 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2815 client
->communication
.inbound
.buffer
.data
+ offset
,
2816 client
->communication
.inbound
.bytes_to_receive
,
2817 &client
->communication
.inbound
.creds
);
2819 client
->communication
.inbound
.expect_creds
= false;
2820 client
->communication
.inbound
.creds_received
= true;
2823 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2824 client
->communication
.inbound
.buffer
.data
+ offset
,
2825 client
->communication
.inbound
.bytes_to_receive
);
2828 goto error_disconnect_client
;
2831 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2832 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2833 ret
= client_dispatch_message(client
, state
);
2836 * Only returns an error if this client must be
2839 goto error_disconnect_client
;
2846 error_disconnect_client
:
2847 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2851 /* Client ready to receive outgoing data. */
2852 int handle_notification_thread_client_out(
2853 struct notification_thread_state
*state
, int socket
)
2856 struct notification_client
*client
;
2858 client
= get_client_from_socket(socket
, state
);
2860 /* Internal error, abort. */
2865 ret
= client_flush_outgoing_queue(client
, state
);
2874 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2875 const struct channel_state_sample
*sample
,
2876 uint64_t buffer_capacity
)
2878 bool result
= false;
2880 enum lttng_condition_type condition_type
;
2881 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2882 condition
, struct lttng_condition_buffer_usage
,
2885 if (use_condition
->threshold_bytes
.set
) {
2886 threshold
= use_condition
->threshold_bytes
.value
;
2889 * Threshold was expressed as a ratio.
2891 * TODO the threshold (in bytes) of conditions expressed
2892 * as a ratio of total buffer size could be cached to
2893 * forego this double-multiplication or it could be performed
2894 * as fixed-point math.
2896 * Note that caching should accomodate the case where the
2897 * condition applies to multiple channels (i.e. don't assume
2898 * that all channels matching my_chann* have the same size...)
2900 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
2901 (double) buffer_capacity
);
2904 condition_type
= lttng_condition_get_type(condition
);
2905 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
2906 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2907 threshold
, sample
->highest_usage
);
2910 * The low condition should only be triggered once _all_ of the
2911 * streams in a channel have gone below the "low" threshold.
2913 if (sample
->highest_usage
<= threshold
) {
2917 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2918 threshold
, sample
->highest_usage
);
2921 * For high buffer usage scenarios, we want to trigger whenever
2922 * _any_ of the streams has reached the "high" threshold.
2924 if (sample
->highest_usage
>= threshold
) {
2933 bool evaluate_session_consumed_size_condition(
2934 const struct lttng_condition
*condition
,
2935 uint64_t session_consumed_size
)
2938 const struct lttng_condition_session_consumed_size
*size_condition
=
2939 container_of(condition
,
2940 struct lttng_condition_session_consumed_size
,
2943 threshold
= size_condition
->consumed_threshold_bytes
.value
;
2944 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
2945 threshold
, session_consumed_size
);
2946 return session_consumed_size
>= threshold
;
2950 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
2951 struct lttng_evaluation
**evaluation
,
2952 const struct notification_thread_state
*state
,
2953 const struct channel_state_sample
*previous_sample
,
2954 const struct channel_state_sample
*latest_sample
,
2955 uint64_t previous_session_consumed_total
,
2956 uint64_t latest_session_consumed_total
,
2957 struct channel_info
*channel_info
)
2960 enum lttng_condition_type condition_type
;
2961 const bool previous_sample_available
= !!previous_sample
;
2962 bool previous_sample_result
= false;
2963 bool latest_sample_result
;
2965 condition_type
= lttng_condition_get_type(condition
);
2967 switch (condition_type
) {
2968 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2969 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2970 if (caa_likely(previous_sample_available
)) {
2971 previous_sample_result
=
2972 evaluate_buffer_usage_condition(condition
,
2973 previous_sample
, channel_info
->capacity
);
2975 latest_sample_result
= evaluate_buffer_usage_condition(
2976 condition
, latest_sample
,
2977 channel_info
->capacity
);
2979 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2980 if (caa_likely(previous_sample_available
)) {
2981 previous_sample_result
=
2982 evaluate_session_consumed_size_condition(
2984 previous_session_consumed_total
);
2986 latest_sample_result
=
2987 evaluate_session_consumed_size_condition(
2989 latest_session_consumed_total
);
2992 /* Unknown condition type; internal error. */
2996 if (!latest_sample_result
||
2997 (previous_sample_result
== latest_sample_result
)) {
2999 * Only trigger on a condition evaluation transition.
3001 * NOTE: This edge-triggered logic may not be appropriate for
3002 * future condition types.
3007 if (!evaluation
|| !latest_sample_result
) {
3011 switch (condition_type
) {
3012 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3013 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3014 *evaluation
= lttng_evaluation_buffer_usage_create(
3016 latest_sample
->highest_usage
,
3017 channel_info
->capacity
);
3019 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3020 *evaluation
= lttng_evaluation_session_consumed_size_create(
3021 latest_session_consumed_total
);
3036 int client_enqueue_dropped_notification(struct notification_client
*client
,
3037 struct notification_thread_state
*state
)
3040 struct lttng_notification_channel_message msg
= {
3041 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
3045 ret
= lttng_dynamic_buffer_append(
3046 &client
->communication
.outbound
.buffer
, &msg
,
3052 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
3053 const struct lttng_evaluation
*evaluation
,
3054 struct notification_client_list
* client_list
,
3055 struct notification_thread_state
*state
,
3056 uid_t channel_uid
, gid_t channel_gid
)
3059 struct lttng_dynamic_buffer msg_buffer
;
3060 struct notification_client_list_element
*client_list_element
, *tmp
;
3061 const struct lttng_notification notification
= {
3062 .condition
= (struct lttng_condition
*) lttng_trigger_get_const_condition(trigger
),
3063 .evaluation
= (struct lttng_evaluation
*) evaluation
,
3065 struct lttng_notification_channel_message msg_header
= {
3066 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
3069 lttng_dynamic_buffer_init(&msg_buffer
);
3071 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg_header
,
3072 sizeof(msg_header
));
3077 ret
= lttng_notification_serialize(¬ification
, &msg_buffer
);
3079 ERR("[notification-thread] Failed to serialize notification");
3084 /* Update payload size. */
3085 ((struct lttng_notification_channel_message
* ) msg_buffer
.data
)->size
=
3086 (uint32_t) (msg_buffer
.size
- sizeof(msg_header
));
3088 cds_list_for_each_entry_safe(client_list_element
, tmp
,
3089 &client_list
->list
, node
) {
3090 struct notification_client
*client
=
3091 client_list_element
->client
;
3093 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
3095 /* Client is not allowed to monitor this channel. */
3096 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
3100 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
3101 client
->socket
, msg_buffer
.size
);
3102 if (client
->communication
.outbound
.buffer
.size
) {
3104 * Outgoing data is already buffered for this client;
3105 * drop the notification and enqueue a "dropped
3106 * notification" message if this is the first dropped
3107 * notification since the socket spilled-over to the
3110 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
3112 if (!client
->communication
.outbound
.dropped_notification
) {
3113 client
->communication
.outbound
.dropped_notification
= true;
3114 ret
= client_enqueue_dropped_notification(
3123 ret
= lttng_dynamic_buffer_append_buffer(
3124 &client
->communication
.outbound
.buffer
,
3130 ret
= client_flush_outgoing_queue(client
, state
);
3137 lttng_dynamic_buffer_reset(&msg_buffer
);
3141 int handle_notification_thread_channel_sample(
3142 struct notification_thread_state
*state
, int pipe
,
3143 enum lttng_domain_type domain
)
3146 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
3147 struct channel_info
*channel_info
;
3148 struct cds_lfht_node
*node
;
3149 struct cds_lfht_iter iter
;
3150 struct lttng_channel_trigger_list
*trigger_list
;
3151 struct lttng_trigger_list_element
*trigger_list_element
;
3152 bool previous_sample_available
= false;
3153 struct channel_state_sample previous_sample
, latest_sample
;
3154 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
3157 * The monitoring pipe only holds messages smaller than PIPE_BUF,
3158 * ensuring that read/write of sampling messages are atomic.
3160 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
3161 if (ret
!= sizeof(sample_msg
)) {
3162 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
3169 latest_sample
.key
.key
= sample_msg
.key
;
3170 latest_sample
.key
.domain
= domain
;
3171 latest_sample
.highest_usage
= sample_msg
.highest
;
3172 latest_sample
.lowest_usage
= sample_msg
.lowest
;
3173 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
3177 /* Retrieve the channel's informations */
3178 cds_lfht_lookup(state
->channels_ht
,
3179 hash_channel_key(&latest_sample
.key
),
3183 node
= cds_lfht_iter_get_node(&iter
);
3184 if (caa_unlikely(!node
)) {
3186 * Not an error since the consumer can push a sample to the pipe
3187 * and the rest of the session daemon could notify us of the
3188 * channel's destruction before we get a chance to process that
3191 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
3192 latest_sample
.key
.key
,
3193 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
3197 channel_info
= caa_container_of(node
, struct channel_info
,
3199 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
3201 latest_sample
.key
.key
,
3202 channel_info
->session_info
->name
,
3203 latest_sample
.highest_usage
,
3204 latest_sample
.lowest_usage
,
3205 latest_sample
.channel_total_consumed
);
3207 previous_session_consumed_total
=
3208 channel_info
->session_info
->consumed_data_size
;
3210 /* Retrieve the channel's last sample, if it exists, and update it. */
3211 cds_lfht_lookup(state
->channel_state_ht
,
3212 hash_channel_key(&latest_sample
.key
),
3213 match_channel_state_sample
,
3216 node
= cds_lfht_iter_get_node(&iter
);
3217 if (caa_likely(node
)) {
3218 struct channel_state_sample
*stored_sample
;
3220 /* Update the sample stored. */
3221 stored_sample
= caa_container_of(node
,
3222 struct channel_state_sample
,
3223 channel_state_ht_node
);
3225 memcpy(&previous_sample
, stored_sample
,
3226 sizeof(previous_sample
));
3227 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
3228 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
3229 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
3230 previous_sample_available
= true;
3232 latest_session_consumed_total
=
3233 previous_session_consumed_total
+
3234 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
3237 * This is the channel's first sample, allocate space for and
3238 * store the new sample.
3240 struct channel_state_sample
*stored_sample
;
3242 stored_sample
= zmalloc(sizeof(*stored_sample
));
3243 if (!stored_sample
) {
3248 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
3249 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
3250 cds_lfht_add(state
->channel_state_ht
,
3251 hash_channel_key(&stored_sample
->key
),
3252 &stored_sample
->channel_state_ht_node
);
3254 latest_session_consumed_total
=
3255 previous_session_consumed_total
+
3256 latest_sample
.channel_total_consumed
;
3259 channel_info
->session_info
->consumed_data_size
=
3260 latest_session_consumed_total
;
3262 /* Find triggers associated with this channel. */
3263 cds_lfht_lookup(state
->channel_triggers_ht
,
3264 hash_channel_key(&latest_sample
.key
),
3265 match_channel_trigger_list
,
3268 node
= cds_lfht_iter_get_node(&iter
);
3269 if (caa_likely(!node
)) {
3273 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
3274 channel_triggers_ht_node
);
3275 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
3277 const struct lttng_condition
*condition
;
3278 const struct lttng_action
*action
;
3279 const struct lttng_trigger
*trigger
;
3280 struct notification_client_list
*client_list
;
3281 struct lttng_evaluation
*evaluation
= NULL
;
3283 trigger
= trigger_list_element
->trigger
;
3284 condition
= lttng_trigger_get_const_condition(trigger
);
3286 action
= lttng_trigger_get_const_action(trigger
);
3288 /* Notify actions are the only type currently supported. */
3289 assert(lttng_action_get_type_const(action
) ==
3290 LTTNG_ACTION_TYPE_NOTIFY
);
3293 * Check if any client is subscribed to the result of this
3296 client_list
= get_client_list_from_condition(state
, condition
);
3297 assert(client_list
);
3298 if (cds_list_empty(&client_list
->list
)) {
3300 * No clients interested in the evaluation's result,
3306 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
3307 previous_sample_available
? &previous_sample
: NULL
,
3309 previous_session_consumed_total
,
3310 latest_session_consumed_total
,
3312 if (caa_unlikely(ret
)) {
3316 if (caa_likely(!evaluation
)) {
3320 /* Dispatch evaluation result to all clients. */
3321 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
3322 evaluation
, client_list
, state
,
3323 channel_info
->session_info
->uid
,
3324 channel_info
->session_info
->gid
);
3325 lttng_evaluation_destroy(evaluation
);
3326 if (caa_unlikely(ret
)) {