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.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/notification/channel-internal.h>
43 #include "notification-thread.h"
44 #include "notification-thread-events.h"
45 #include "notification-thread-commands.h"
46 #include "lttng-sessiond.h"
49 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
50 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
52 struct lttng_trigger_list_element
{
53 struct lttng_trigger
*trigger
;
54 struct cds_list_head node
;
57 struct lttng_channel_trigger_list
{
58 struct channel_key channel_key
;
59 struct cds_list_head list
;
60 struct cds_lfht_node channel_triggers_ht_node
;
63 struct lttng_trigger_ht_element
{
64 struct lttng_trigger
*trigger
;
65 struct cds_lfht_node node
;
68 struct lttng_condition_list_element
{
69 struct lttng_condition
*condition
;
70 struct cds_list_head node
;
73 struct notification_client_list_element
{
74 struct notification_client
*client
;
75 struct cds_list_head node
;
78 struct notification_client_list
{
79 struct lttng_trigger
*trigger
;
80 struct cds_list_head list
;
81 struct cds_lfht_node notification_trigger_ht_node
;
84 struct notification_client
{
86 /* Client protocol version. */
91 * Indicates if the credentials and versions of the client have been
96 * Conditions to which the client's notification channel is subscribed.
97 * List of struct lttng_condition_list_node. The condition member is
98 * owned by the client.
100 struct cds_list_head condition_list
;
101 struct cds_lfht_node client_socket_ht_node
;
105 * During the reception of a message, the reception
106 * buffers' "size" is set to contain the current
107 * message's complete payload.
109 struct lttng_dynamic_buffer buffer
;
110 /* Bytes left to receive for the current message. */
111 size_t bytes_to_receive
;
112 /* Type of the message being received. */
113 enum lttng_notification_channel_message_type msg_type
;
115 * Indicates whether or not credentials are expected
120 * Indicates whether or not credentials were received
124 /* Only used during credentials reception. */
125 lttng_sock_cred creds
;
129 * Indicates whether or not a notification addressed to
130 * this client was dropped because a command reply was
133 * A notification is dropped whenever the buffer is not
136 bool dropped_notification
;
138 * Indicates whether or not a command reply is already
139 * buffered. In this case, it means that the client is
140 * not consuming command replies before emitting a new
141 * one. This could be caused by a protocol error or a
142 * misbehaving/malicious client.
144 bool queued_command_reply
;
145 struct lttng_dynamic_buffer buffer
;
150 struct channel_state_sample
{
151 struct channel_key key
;
152 struct cds_lfht_node channel_state_ht_node
;
153 uint64_t highest_usage
;
154 uint64_t lowest_usage
;
157 static unsigned long hash_channel_key(struct channel_key
*key
);
158 static int evaluate_condition(struct lttng_condition
*condition
,
159 struct lttng_evaluation
**evaluation
,
160 struct notification_thread_state
*state
,
161 struct channel_state_sample
*previous_sample
,
162 struct channel_state_sample
*latest_sample
,
163 uint64_t buffer_capacity
);
165 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
166 struct lttng_evaluation
*evaluation
,
167 struct notification_client_list
*client_list
,
168 struct notification_thread_state
*state
,
169 uid_t channel_uid
, gid_t channel_gid
);
172 int match_client(struct cds_lfht_node
*node
, const void *key
)
174 /* This double-cast is intended to supress pointer-to-cast warning. */
175 int socket
= (int) (intptr_t) key
;
176 struct notification_client
*client
;
178 client
= caa_container_of(node
, struct notification_client
,
179 client_socket_ht_node
);
181 return !!(client
->socket
== socket
);
185 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
187 struct channel_key
*channel_key
= (struct channel_key
*) key
;
188 struct lttng_channel_trigger_list
*trigger_list
;
190 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
191 channel_triggers_ht_node
);
193 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
194 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
198 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
200 struct channel_key
*channel_key
= (struct channel_key
*) key
;
201 struct channel_state_sample
*sample
;
203 sample
= caa_container_of(node
, struct channel_state_sample
,
204 channel_state_ht_node
);
206 return !!((channel_key
->key
== sample
->key
.key
) &&
207 (channel_key
->domain
== sample
->key
.domain
));
211 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
213 struct channel_key
*channel_key
= (struct channel_key
*) key
;
214 struct channel_info
*channel_info
;
216 channel_info
= caa_container_of(node
, struct channel_info
,
219 return !!((channel_key
->key
== channel_info
->key
.key
) &&
220 (channel_key
->domain
== channel_info
->key
.domain
));
224 int match_condition(struct cds_lfht_node
*node
, const void *key
)
226 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
227 struct lttng_trigger_ht_element
*trigger
;
228 struct lttng_condition
*condition
;
230 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
232 condition
= lttng_trigger_get_condition(trigger
->trigger
);
235 return !!lttng_condition_is_equal(condition_key
, condition
);
239 int match_client_list(struct cds_lfht_node
*node
, const void *key
)
241 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
242 struct notification_client_list
*client_list
;
243 struct lttng_condition
*condition
;
244 struct lttng_condition
*condition_key
= lttng_trigger_get_condition(
247 assert(condition_key
);
249 client_list
= caa_container_of(node
, struct notification_client_list
,
250 notification_trigger_ht_node
);
251 condition
= lttng_trigger_get_condition(client_list
->trigger
);
253 return !!lttng_condition_is_equal(condition_key
, condition
);
257 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
259 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
260 struct notification_client_list
*client_list
;
261 struct lttng_condition
*condition
;
263 assert(condition_key
);
265 client_list
= caa_container_of(node
, struct notification_client_list
,
266 notification_trigger_ht_node
);
267 condition
= lttng_trigger_get_condition(client_list
->trigger
);
269 return !!lttng_condition_is_equal(condition_key
, condition
);
273 unsigned long lttng_condition_buffer_usage_hash(
274 struct lttng_condition
*_condition
)
276 unsigned long hash
= 0;
277 struct lttng_condition_buffer_usage
*condition
;
279 condition
= container_of(_condition
,
280 struct lttng_condition_buffer_usage
, parent
);
282 if (condition
->session_name
) {
283 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
285 if (condition
->channel_name
) {
286 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
288 if (condition
->domain
.set
) {
289 hash
^= hash_key_ulong(
290 (void *) condition
->domain
.type
,
293 if (condition
->threshold_ratio
.set
) {
296 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
297 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
298 } else if (condition
->threshold_bytes
.set
) {
301 val
= condition
->threshold_bytes
.value
;
302 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
308 * The lttng_condition hashing code is kept in this file (rather than
309 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
310 * don't want to link in liblttng-ctl.
313 unsigned long lttng_condition_hash(struct lttng_condition
*condition
)
315 switch (condition
->type
) {
316 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
317 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
318 return lttng_condition_buffer_usage_hash(condition
);
320 ERR("[notification-thread] Unexpected condition type caught");
326 unsigned long hash_channel_key(struct channel_key
*key
)
328 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
329 unsigned long domain_hash
= hash_key_ulong(
330 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
332 return key_hash
^ domain_hash
;
336 void channel_info_destroy(struct channel_info
*channel_info
)
342 if (channel_info
->session_name
) {
343 free(channel_info
->session_name
);
345 if (channel_info
->channel_name
) {
346 free(channel_info
->channel_name
);
352 struct channel_info
*channel_info_copy(struct channel_info
*channel_info
)
354 struct channel_info
*copy
= zmalloc(sizeof(*channel_info
));
356 assert(channel_info
);
357 assert(channel_info
->session_name
);
358 assert(channel_info
->channel_name
);
364 memcpy(copy
, channel_info
, sizeof(*channel_info
));
365 copy
->session_name
= NULL
;
366 copy
->channel_name
= NULL
;
368 copy
->session_name
= strdup(channel_info
->session_name
);
369 if (!copy
->session_name
) {
372 copy
->channel_name
= strdup(channel_info
->channel_name
);
373 if (!copy
->channel_name
) {
376 cds_lfht_node_init(&channel_info
->channels_ht_node
);
380 channel_info_destroy(copy
);
384 /* This function must be called with the RCU read lock held. */
386 int evaluate_condition_for_client(struct lttng_trigger
*trigger
,
387 struct lttng_condition
*condition
,
388 struct notification_client
*client
,
389 struct notification_thread_state
*state
)
392 struct cds_lfht_iter iter
;
393 struct cds_lfht_node
*node
;
394 struct channel_info
*channel_info
= NULL
;
395 struct channel_key
*channel_key
= NULL
;
396 struct channel_state_sample
*last_sample
= NULL
;
397 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
398 struct lttng_evaluation
*evaluation
= NULL
;
399 struct notification_client_list client_list
= { 0 };
400 struct notification_client_list_element client_list_element
= { 0 };
407 /* Find the channel associated with the trigger. */
408 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
409 channel_trigger_list
, channel_triggers_ht_node
) {
410 struct lttng_trigger_list_element
*element
;
412 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
413 struct lttng_condition
*current_condition
=
414 lttng_trigger_get_condition(
417 assert(current_condition
);
418 if (!lttng_condition_is_equal(condition
,
419 current_condition
)) {
423 /* Found the trigger, save the channel key. */
424 channel_key
= &channel_trigger_list
->channel_key
;
428 /* The channel key was found stop iteration. */
434 /* No channel found; normal exit. */
435 DBG("[notification-thread] No channel associated with newly subscribed-to condition");
440 /* Fetch channel info for the matching channel. */
441 cds_lfht_lookup(state
->channels_ht
,
442 hash_channel_key(channel_key
),
446 node
= cds_lfht_iter_get_node(&iter
);
448 channel_info
= caa_container_of(node
, struct channel_info
,
451 /* Retrieve the channel's last sample, if it exists. */
452 cds_lfht_lookup(state
->channel_state_ht
,
453 hash_channel_key(channel_key
),
454 match_channel_state_sample
,
457 node
= cds_lfht_iter_get_node(&iter
);
459 last_sample
= caa_container_of(node
,
460 struct channel_state_sample
,
461 channel_state_ht_node
);
463 /* Nothing to evaluate, no sample was ever taken. Normal exit */
464 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
469 ret
= evaluate_condition(condition
, &evaluation
, state
, NULL
,
470 last_sample
, channel_info
->capacity
);
472 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
477 /* Evaluation yielded nothing. Normal exit. */
478 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
484 * Create a temporary client list with the client currently
487 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
488 CDS_INIT_LIST_HEAD(&client_list
.list
);
489 client_list
.trigger
= trigger
;
491 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
492 client_list_element
.client
= client
;
493 cds_list_add(&client_list_element
.node
, &client_list
.list
);
495 /* Send evaluation result to the newly-subscribed client. */
496 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
497 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
498 state
, channel_info
->uid
, channel_info
->gid
);
505 int notification_thread_client_subscribe(struct notification_client
*client
,
506 struct lttng_condition
*condition
,
507 struct notification_thread_state
*state
,
508 enum lttng_notification_channel_status
*_status
)
511 struct cds_lfht_iter iter
;
512 struct cds_lfht_node
*node
;
513 struct notification_client_list
*client_list
;
514 struct lttng_condition_list_element
*condition_list_element
= NULL
;
515 struct notification_client_list_element
*client_list_element
= NULL
;
516 enum lttng_notification_channel_status status
=
517 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
520 * Ensure that the client has not already subscribed to this condition
523 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
524 if (lttng_condition_is_equal(condition_list_element
->condition
,
526 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
531 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
532 if (!condition_list_element
) {
536 client_list_element
= zmalloc(sizeof(*client_list_element
));
537 if (!client_list_element
) {
545 * Add the newly-subscribed condition to the client's subscription list.
547 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
548 condition_list_element
->condition
= condition
;
549 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
551 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
552 lttng_condition_hash(condition
),
553 match_client_list_condition
,
556 node
= cds_lfht_iter_get_node(&iter
);
558 free(client_list_element
);
562 client_list
= caa_container_of(node
, struct notification_client_list
,
563 notification_trigger_ht_node
);
564 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
566 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
568 free(client_list_element
);
573 * Add the client to the list of clients interested in a given trigger
574 * if a "notification" trigger with a corresponding condition was
577 client_list_element
->client
= client
;
578 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
579 cds_list_add(&client_list_element
->node
, &client_list
->list
);
588 free(condition_list_element
);
589 free(client_list_element
);
594 int notification_thread_client_unsubscribe(
595 struct notification_client
*client
,
596 struct lttng_condition
*condition
,
597 struct notification_thread_state
*state
,
598 enum lttng_notification_channel_status
*_status
)
600 struct cds_lfht_iter iter
;
601 struct cds_lfht_node
*node
;
602 struct notification_client_list
*client_list
;
603 struct lttng_condition_list_element
*condition_list_element
,
605 struct notification_client_list_element
*client_list_element
,
607 bool condition_found
= false;
608 enum lttng_notification_channel_status status
=
609 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
611 /* Remove the condition from the client's condition list. */
612 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
613 &client
->condition_list
, node
) {
614 if (!lttng_condition_is_equal(condition_list_element
->condition
,
619 cds_list_del(&condition_list_element
->node
);
621 * The caller may be iterating on the client's conditions to
622 * tear down a client's connection. In this case, the condition
623 * will be destroyed at the end.
625 if (condition
!= condition_list_element
->condition
) {
626 lttng_condition_destroy(
627 condition_list_element
->condition
);
629 free(condition_list_element
);
630 condition_found
= true;
634 if (!condition_found
) {
635 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
640 * Remove the client from the list of clients interested the trigger
641 * matching the condition.
644 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
645 lttng_condition_hash(condition
),
646 match_client_list_condition
,
649 node
= cds_lfht_iter_get_node(&iter
);
654 client_list
= caa_container_of(node
, struct notification_client_list
,
655 notification_trigger_ht_node
);
656 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
657 &client_list
->list
, node
) {
658 if (client_list_element
->client
->socket
!= client
->socket
) {
661 cds_list_del(&client_list_element
->node
);
662 free(client_list_element
);
668 lttng_condition_destroy(condition
);
676 void notification_client_destroy(struct notification_client
*client
,
677 struct notification_thread_state
*state
)
679 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
685 /* Release all conditions to which the client was subscribed. */
686 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
687 &client
->condition_list
, node
) {
688 (void) notification_thread_client_unsubscribe(client
,
689 condition_list_element
->condition
, state
, NULL
);
692 if (client
->socket
>= 0) {
693 (void) lttcomm_close_unix_sock(client
->socket
);
695 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
696 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
701 * Call with rcu_read_lock held (and hold for the lifetime of the returned
705 struct notification_client
*get_client_from_socket(int socket
,
706 struct notification_thread_state
*state
)
708 struct cds_lfht_iter iter
;
709 struct cds_lfht_node
*node
;
710 struct notification_client
*client
= NULL
;
712 cds_lfht_lookup(state
->client_socket_ht
,
713 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
715 (void *) (unsigned long) socket
,
717 node
= cds_lfht_iter_get_node(&iter
);
722 client
= caa_container_of(node
, struct notification_client
,
723 client_socket_ht_node
);
729 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
730 struct channel_info
*info
)
732 enum lttng_condition_status status
;
733 struct lttng_condition
*condition
;
734 const char *trigger_session_name
= NULL
;
735 const char *trigger_channel_name
= NULL
;
736 enum lttng_domain_type trigger_domain
;
738 condition
= lttng_trigger_get_condition(trigger
);
743 switch (lttng_condition_get_type(condition
)) {
744 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
745 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
751 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
753 assert(status
== LTTNG_CONDITION_STATUS_OK
);
754 if (info
->key
.domain
!= trigger_domain
) {
758 status
= lttng_condition_buffer_usage_get_session_name(
759 condition
, &trigger_session_name
);
760 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_session_name
);
762 status
= lttng_condition_buffer_usage_get_channel_name(
763 condition
, &trigger_channel_name
);
764 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_channel_name
);
766 if (strcmp(info
->session_name
, trigger_session_name
)) {
769 if (strcmp(info
->channel_name
, trigger_channel_name
)) {
779 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
780 struct notification_client
*client
)
782 bool applies
= false;
783 struct lttng_condition_list_element
*condition_list_element
;
785 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
787 applies
= lttng_condition_is_equal(
788 condition_list_element
->condition
,
789 lttng_trigger_get_condition(trigger
));
798 int handle_notification_thread_command_add_channel(
799 struct notification_thread_state
*state
,
800 struct channel_info
*channel_info
,
801 enum lttng_error_code
*cmd_result
)
803 struct cds_list_head trigger_list
;
804 struct channel_info
*new_channel_info
;
805 struct channel_key
*channel_key
;
806 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
807 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
808 int trigger_count
= 0;
809 struct cds_lfht_iter iter
;
811 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
812 channel_info
->channel_name
, channel_info
->session_name
,
813 channel_info
->key
.key
, channel_info
->key
.domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
815 CDS_INIT_LIST_HEAD(&trigger_list
);
817 new_channel_info
= channel_info_copy(channel_info
);
818 if (!new_channel_info
) {
822 channel_key
= &new_channel_info
->key
;
824 /* Build a list of all triggers applying to the new channel. */
825 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
827 struct lttng_trigger_list_element
*new_element
;
829 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
834 new_element
= zmalloc(sizeof(*new_element
));
838 CDS_INIT_LIST_HEAD(&new_element
->node
);
839 new_element
->trigger
= trigger_ht_element
->trigger
;
840 cds_list_add(&new_element
->node
, &trigger_list
);
844 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
846 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
847 if (!channel_trigger_list
) {
850 channel_trigger_list
->channel_key
= *channel_key
;
851 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
852 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
853 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
856 /* Add channel to the channel_ht which owns the channel_infos. */
857 cds_lfht_add(state
->channels_ht
,
858 hash_channel_key(channel_key
),
859 &new_channel_info
->channels_ht_node
);
861 * Add the list of triggers associated with this channel to the
862 * channel_triggers_ht.
864 cds_lfht_add(state
->channel_triggers_ht
,
865 hash_channel_key(channel_key
),
866 &channel_trigger_list
->channel_triggers_ht_node
);
868 *cmd_result
= LTTNG_OK
;
871 /* Empty trigger list */
872 channel_info_destroy(new_channel_info
);
877 int handle_notification_thread_command_remove_channel(
878 struct notification_thread_state
*state
,
879 uint64_t channel_key
, enum lttng_domain_type domain
,
880 enum lttng_error_code
*cmd_result
)
882 struct cds_lfht_node
*node
;
883 struct cds_lfht_iter iter
;
884 struct lttng_channel_trigger_list
*trigger_list
;
885 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
886 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
887 struct channel_info
*channel_info
;
889 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
890 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
894 cds_lfht_lookup(state
->channel_triggers_ht
,
895 hash_channel_key(&key
),
896 match_channel_trigger_list
,
899 node
= cds_lfht_iter_get_node(&iter
);
901 * There is a severe internal error if we are being asked to remove a
902 * channel that doesn't exist.
905 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
909 /* Free the list of triggers associated with this channel. */
910 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
911 channel_triggers_ht_node
);
912 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
913 &trigger_list
->list
, node
) {
914 cds_list_del(&trigger_list_element
->node
);
915 free(trigger_list_element
);
917 cds_lfht_del(state
->channel_triggers_ht
, node
);
920 /* Free sampled channel state. */
921 cds_lfht_lookup(state
->channel_state_ht
,
922 hash_channel_key(&key
),
923 match_channel_state_sample
,
926 node
= cds_lfht_iter_get_node(&iter
);
928 * This is expected to be NULL if the channel is destroyed before we
932 struct channel_state_sample
*sample
= caa_container_of(node
,
933 struct channel_state_sample
,
934 channel_state_ht_node
);
936 cds_lfht_del(state
->channel_state_ht
, node
);
940 /* Remove the channel from the channels_ht and free it. */
941 cds_lfht_lookup(state
->channels_ht
,
942 hash_channel_key(&key
),
946 node
= cds_lfht_iter_get_node(&iter
);
948 channel_info
= caa_container_of(node
, struct channel_info
,
950 cds_lfht_del(state
->channels_ht
, node
);
951 channel_info_destroy(channel_info
);
954 *cmd_result
= LTTNG_OK
;
959 int condition_is_supported(struct lttng_condition
*condition
)
963 switch (lttng_condition_get_type(condition
)) {
964 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
965 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
967 enum lttng_domain_type domain
;
969 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
976 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
982 * Older kernel tracers don't expose the API to monitor their
983 * buffers. Therefore, we reject triggers that require that
984 * mechanism to be available to be evaluated.
986 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
998 * FIXME A client's credentials are not checked when registering a trigger, nor
999 * are they stored alongside with the trigger.
1001 * The effects of this are benign since:
1002 * - The client will succeed in registering the trigger, as it is valid,
1003 * - The trigger will, internally, be bound to the channel,
1004 * - The notifications will not be sent since the client's credentials
1005 * are checked against the channel at that moment.
1007 * If this function returns a non-zero value, it means something is
1008 * fundamentally and the whole subsystem/thread will be torn down.
1010 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1014 int handle_notification_thread_command_register_trigger(
1015 struct notification_thread_state
*state
,
1016 struct lttng_trigger
*trigger
,
1017 enum lttng_error_code
*cmd_result
)
1020 struct lttng_condition
*condition
;
1021 struct notification_client
*client
;
1022 struct notification_client_list
*client_list
= NULL
;
1023 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1024 struct notification_client_list_element
*client_list_element
, *tmp
;
1025 struct cds_lfht_node
*node
;
1026 struct cds_lfht_iter iter
;
1027 struct channel_info
*channel
;
1028 bool free_trigger
= true;
1032 condition
= lttng_trigger_get_condition(trigger
);
1035 ret
= condition_is_supported(condition
);
1038 } else if (ret
== 0) {
1039 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1042 /* Feature is supported, continue. */
1046 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1047 if (!trigger_ht_element
) {
1052 /* Add trigger to the trigger_ht. */
1053 cds_lfht_node_init(&trigger_ht_element
->node
);
1054 trigger_ht_element
->trigger
= trigger
;
1056 node
= cds_lfht_add_unique(state
->triggers_ht
,
1057 lttng_condition_hash(condition
),
1060 &trigger_ht_element
->node
);
1061 if (node
!= &trigger_ht_element
->node
) {
1062 /* Not a fatal error, simply report it to the client. */
1063 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1064 goto error_free_ht_element
;
1068 * Ownership of the trigger and of its wrapper was transfered to
1071 trigger_ht_element
= NULL
;
1072 free_trigger
= false;
1075 * The rest only applies to triggers that have a "notify" action.
1076 * It is not skipped as this is the only action type currently
1079 client_list
= zmalloc(sizeof(*client_list
));
1082 goto error_free_ht_element
;
1084 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
1085 CDS_INIT_LIST_HEAD(&client_list
->list
);
1086 client_list
->trigger
= trigger
;
1088 /* Build a list of clients to which this new trigger applies. */
1089 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1090 client_socket_ht_node
) {
1091 if (!trigger_applies_to_client(trigger
, client
)) {
1095 client_list_element
= zmalloc(sizeof(*client_list_element
));
1096 if (!client_list_element
) {
1098 goto error_free_client_list
;
1100 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1101 client_list_element
->client
= client
;
1102 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1105 cds_lfht_add(state
->notification_trigger_clients_ht
,
1106 lttng_condition_hash(condition
),
1107 &client_list
->notification_trigger_ht_node
);
1109 * Client list ownership transferred to the
1110 * notification_trigger_clients_ht.
1115 * Add the trigger to list of triggers bound to the channels currently
1118 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1120 struct lttng_trigger_list_element
*trigger_list_element
;
1121 struct lttng_channel_trigger_list
*trigger_list
;
1123 if (!trigger_applies_to_channel(trigger
, channel
)) {
1127 cds_lfht_lookup(state
->channel_triggers_ht
,
1128 hash_channel_key(&channel
->key
),
1129 match_channel_trigger_list
,
1132 node
= cds_lfht_iter_get_node(&iter
);
1134 trigger_list
= caa_container_of(node
,
1135 struct lttng_channel_trigger_list
,
1136 channel_triggers_ht_node
);
1138 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1139 if (!trigger_list_element
) {
1141 goto error_free_client_list
;
1143 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1144 trigger_list_element
->trigger
= trigger
;
1145 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1147 /* A trigger can only apply to one channel. */
1151 *cmd_result
= LTTNG_OK
;
1152 error_free_client_list
:
1154 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1155 &client_list
->list
, node
) {
1156 free(client_list_element
);
1160 error_free_ht_element
:
1161 free(trigger_ht_element
);
1164 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1166 lttng_condition_destroy(condition
);
1167 lttng_action_destroy(action
);
1168 lttng_trigger_destroy(trigger
);
1175 int handle_notification_thread_command_unregister_trigger(
1176 struct notification_thread_state
*state
,
1177 struct lttng_trigger
*trigger
,
1178 enum lttng_error_code
*_cmd_reply
)
1180 struct cds_lfht_iter iter
;
1181 struct cds_lfht_node
*node
, *triggers_ht_node
;
1182 struct lttng_channel_trigger_list
*trigger_list
;
1183 struct notification_client_list
*client_list
;
1184 struct notification_client_list_element
*client_list_element
, *tmp
;
1185 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1186 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1188 struct lttng_action
*action
;
1189 enum lttng_error_code cmd_reply
;
1193 cds_lfht_lookup(state
->triggers_ht
,
1194 lttng_condition_hash(condition
),
1198 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1199 if (!triggers_ht_node
) {
1200 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1203 cmd_reply
= LTTNG_OK
;
1206 /* Remove trigger from channel_triggers_ht. */
1207 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1208 channel_triggers_ht_node
) {
1209 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1211 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1212 &trigger_list
->list
, node
) {
1213 struct lttng_condition
*current_condition
=
1214 lttng_trigger_get_condition(
1215 trigger_element
->trigger
);
1217 assert(current_condition
);
1218 if (!lttng_condition_is_equal(condition
,
1219 current_condition
)) {
1223 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1224 cds_list_del(&trigger_element
->node
);
1225 /* A trigger can only appear once per channel */
1231 * Remove and release the client list from
1232 * notification_trigger_clients_ht.
1234 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1235 lttng_condition_hash(condition
),
1239 node
= cds_lfht_iter_get_node(&iter
);
1241 client_list
= caa_container_of(node
, struct notification_client_list
,
1242 notification_trigger_ht_node
);
1243 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1244 &client_list
->list
, node
) {
1245 free(client_list_element
);
1247 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1250 /* Remove trigger from triggers_ht. */
1251 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1252 struct lttng_trigger_ht_element
, node
);
1253 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1255 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1256 lttng_condition_destroy(condition
);
1257 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1258 lttng_action_destroy(action
);
1259 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1260 free(trigger_ht_element
);
1264 *_cmd_reply
= cmd_reply
;
1269 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1270 int handle_notification_thread_command(
1271 struct notification_thread_handle
*handle
,
1272 struct notification_thread_state
*state
)
1276 struct notification_thread_command
*cmd
;
1278 /* Read event_fd to put it back into a quiescent state. */
1279 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
, sizeof(counter
));
1284 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1285 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1286 struct notification_thread_command
, cmd_list_node
);
1287 switch (cmd
->type
) {
1288 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1289 DBG("[notification-thread] Received register trigger command");
1290 ret
= handle_notification_thread_command_register_trigger(
1291 state
, cmd
->parameters
.trigger
,
1294 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1295 DBG("[notification-thread] Received unregister trigger command");
1296 ret
= handle_notification_thread_command_unregister_trigger(
1297 state
, cmd
->parameters
.trigger
,
1300 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1301 DBG("[notification-thread] Received add channel command");
1302 ret
= handle_notification_thread_command_add_channel(
1303 state
, &cmd
->parameters
.add_channel
,
1306 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1307 DBG("[notification-thread] Received remove channel command");
1308 ret
= handle_notification_thread_command_remove_channel(
1309 state
, cmd
->parameters
.remove_channel
.key
,
1310 cmd
->parameters
.remove_channel
.domain
,
1313 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1314 DBG("[notification-thread] Received quit command");
1315 cmd
->reply_code
= LTTNG_OK
;
1319 ERR("[notification-thread] Unknown internal command received");
1327 cds_list_del(&cmd
->cmd_list_node
);
1328 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1329 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1332 /* Wake-up and return a fatal error to the calling thread. */
1333 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1334 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1335 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1337 /* Indicate a fatal error to the caller. */
1342 unsigned long hash_client_socket(int socket
)
1344 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1348 int socket_set_non_blocking(int socket
)
1352 /* Set the pipe as non-blocking. */
1353 ret
= fcntl(socket
, F_GETFL
, 0);
1355 PERROR("fcntl get socket flags");
1360 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1362 PERROR("fcntl set O_NONBLOCK socket flag");
1365 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1371 int client_reset_inbound_state(struct notification_client
*client
)
1375 ret
= lttng_dynamic_buffer_set_size(
1376 &client
->communication
.inbound
.buffer
, 0);
1379 client
->communication
.inbound
.bytes_to_receive
=
1380 sizeof(struct lttng_notification_channel_message
);
1381 client
->communication
.inbound
.msg_type
=
1382 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1383 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1384 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1385 ret
= lttng_dynamic_buffer_set_size(
1386 &client
->communication
.inbound
.buffer
,
1387 client
->communication
.inbound
.bytes_to_receive
);
1391 int handle_notification_thread_client_connect(
1392 struct notification_thread_state
*state
)
1395 struct notification_client
*client
;
1397 DBG("[notification-thread] Handling new notification channel client connection");
1399 client
= zmalloc(sizeof(*client
));
1405 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1406 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1407 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1408 client
->communication
.inbound
.expect_creds
= true;
1409 ret
= client_reset_inbound_state(client
);
1411 ERR("[notification-thread] Failed to reset client communication's inbound state");
1416 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1418 ERR("[notification-thread] Failed to accept new notification channel client connection");
1423 client
->socket
= ret
;
1425 ret
= socket_set_non_blocking(client
->socket
);
1427 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1431 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1433 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1438 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1439 LPOLLIN
| LPOLLERR
|
1440 LPOLLHUP
| LPOLLRDHUP
);
1442 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1446 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1450 cds_lfht_add(state
->client_socket_ht
,
1451 hash_client_socket(client
->socket
),
1452 &client
->client_socket_ht_node
);
1457 notification_client_destroy(client
, state
);
1461 int handle_notification_thread_client_disconnect(
1463 struct notification_thread_state
*state
)
1466 struct notification_client
*client
;
1469 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1471 client
= get_client_from_socket(client_socket
, state
);
1473 /* Internal state corruption, fatal error. */
1474 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1480 ret
= lttng_poll_del(&state
->events
, client_socket
);
1482 ERR("[notification-thread] Failed to remove client socket from poll set");
1484 cds_lfht_del(state
->client_socket_ht
,
1485 &client
->client_socket_ht_node
);
1486 notification_client_destroy(client
, state
);
1492 int handle_notification_thread_client_disconnect_all(
1493 struct notification_thread_state
*state
)
1495 struct cds_lfht_iter iter
;
1496 struct notification_client
*client
;
1497 bool error_encoutered
= false;
1500 DBG("[notification-thread] Closing all client connections");
1501 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1502 client_socket_ht_node
) {
1505 ret
= handle_notification_thread_client_disconnect(
1506 client
->socket
, state
);
1508 error_encoutered
= true;
1512 return error_encoutered
? 1 : 0;
1515 int handle_notification_thread_trigger_unregister_all(
1516 struct notification_thread_state
*state
)
1518 bool error_occurred
= false;
1519 struct cds_lfht_iter iter
;
1520 struct lttng_trigger_ht_element
*trigger_ht_element
;
1522 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1524 int ret
= handle_notification_thread_command_unregister_trigger(
1525 state
, trigger_ht_element
->trigger
, NULL
);
1527 error_occurred
= true;
1530 return error_occurred
? -1 : 0;
1534 int client_flush_outgoing_queue(struct notification_client
*client
,
1535 struct notification_thread_state
*state
)
1538 size_t to_send_count
;
1540 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1541 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1542 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1545 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1546 client
->communication
.outbound
.buffer
.data
,
1548 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1549 (ret
> 0 && ret
< to_send_count
)) {
1550 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1552 to_send_count
-= max(ret
, 0);
1554 memcpy(client
->communication
.outbound
.buffer
.data
,
1555 client
->communication
.outbound
.buffer
.data
+
1556 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1558 ret
= lttng_dynamic_buffer_set_size(
1559 &client
->communication
.outbound
.buffer
,
1566 * We want to be notified whenever there is buffer space
1567 * available to send the rest of the payload.
1569 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1570 CLIENT_POLL_MASK_IN_OUT
);
1574 } else if (ret
< 0) {
1575 /* Generic error, disconnect the client. */
1576 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1578 ret
= handle_notification_thread_client_disconnect(
1579 client
->socket
, state
);
1584 /* No error and flushed the queue completely. */
1585 ret
= lttng_dynamic_buffer_set_size(
1586 &client
->communication
.outbound
.buffer
, 0);
1590 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1591 CLIENT_POLL_MASK_IN
);
1596 client
->communication
.outbound
.queued_command_reply
= false;
1597 client
->communication
.outbound
.dropped_notification
= false;
1606 int client_send_command_reply(struct notification_client
*client
,
1607 struct notification_thread_state
*state
,
1608 enum lttng_notification_channel_status status
)
1611 struct lttng_notification_channel_command_reply reply
= {
1612 .status
= (int8_t) status
,
1614 struct lttng_notification_channel_message msg
= {
1615 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1616 .size
= sizeof(reply
),
1618 char buffer
[sizeof(msg
) + sizeof(reply
)];
1620 if (client
->communication
.outbound
.queued_command_reply
) {
1621 /* Protocol error. */
1625 memcpy(buffer
, &msg
, sizeof(msg
));
1626 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1627 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1629 /* Enqueue buffer to outgoing queue and flush it. */
1630 ret
= lttng_dynamic_buffer_append(
1631 &client
->communication
.outbound
.buffer
,
1632 buffer
, sizeof(buffer
));
1637 ret
= client_flush_outgoing_queue(client
, state
);
1642 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1643 /* Queue could not be emptied. */
1644 client
->communication
.outbound
.queued_command_reply
= true;
1653 int client_dispatch_message(struct notification_client
*client
,
1654 struct notification_thread_state
*state
)
1658 if (client
->communication
.inbound
.msg_type
!=
1659 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1660 client
->communication
.inbound
.msg_type
!=
1661 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1662 !client
->validated
) {
1663 WARN("[notification-thread] client attempted a command before handshake");
1668 switch (client
->communication
.inbound
.msg_type
) {
1669 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1672 * Receiving message header. The function will be called again
1673 * once the rest of the message as been received and can be
1676 const struct lttng_notification_channel_message
*msg
;
1678 assert(sizeof(*msg
) ==
1679 client
->communication
.inbound
.buffer
.size
);
1680 msg
= (const struct lttng_notification_channel_message
*)
1681 client
->communication
.inbound
.buffer
.data
;
1683 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1684 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1689 switch (msg
->type
) {
1690 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1691 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1692 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1696 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1700 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
1701 client
->communication
.inbound
.msg_type
=
1702 (enum lttng_notification_channel_message_type
) msg
->type
;
1703 ret
= lttng_dynamic_buffer_set_size(
1704 &client
->communication
.inbound
.buffer
, msg
->size
);
1710 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1712 struct lttng_notification_channel_command_handshake
*handshake_client
;
1713 struct lttng_notification_channel_command_handshake handshake_reply
= {
1714 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
1715 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
1717 struct lttng_notification_channel_message msg_header
= {
1718 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
1719 .size
= sizeof(handshake_reply
),
1721 enum lttng_notification_channel_status status
=
1722 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1723 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
1725 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
1726 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
1727 sizeof(handshake_reply
));
1730 (struct lttng_notification_channel_command_handshake
*)
1731 client
->communication
.inbound
.buffer
.data
;
1732 client
->major
= handshake_client
->major
;
1733 client
->minor
= handshake_client
->minor
;
1734 if (!client
->communication
.inbound
.creds_received
) {
1735 ERR("[notification-thread] No credentials received from client");
1740 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
1741 &client
->communication
.inbound
.creds
);
1742 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
1743 &client
->communication
.inbound
.creds
);
1744 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
1745 client
->uid
, client
->gid
, (int) client
->major
,
1746 (int) client
->minor
);
1748 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
1749 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
1752 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
1753 send_buffer
, sizeof(send_buffer
));
1755 ERR("[notification-thread] Failed to send protocol version to notification channel client");
1759 ret
= client_flush_outgoing_queue(client
, state
);
1764 ret
= client_send_command_reply(client
, state
, status
);
1766 ERR("[notification-thread] Failed to send reply to notification channel client");
1770 /* Set reception state to receive the next message header. */
1771 ret
= client_reset_inbound_state(client
);
1773 ERR("[notification-thread] Failed to reset client communication's inbound state");
1776 client
->validated
= true;
1779 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1780 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1782 struct lttng_condition
*condition
;
1783 enum lttng_notification_channel_status status
=
1784 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1785 const struct lttng_buffer_view condition_view
=
1786 lttng_buffer_view_from_dynamic_buffer(
1787 &client
->communication
.inbound
.buffer
,
1789 size_t expected_condition_size
=
1790 client
->communication
.inbound
.buffer
.size
;
1792 ret
= lttng_condition_create_from_buffer(&condition_view
,
1794 if (ret
!= expected_condition_size
) {
1795 ERR("[notification-thread] Malformed condition received from client");
1799 if (client
->communication
.inbound
.msg_type
==
1800 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
1802 * FIXME The current state should be evaluated on
1805 ret
= notification_thread_client_subscribe(client
,
1806 condition
, state
, &status
);
1808 ret
= notification_thread_client_unsubscribe(client
,
1809 condition
, state
, &status
);
1815 ret
= client_send_command_reply(client
, state
, status
);
1817 ERR("[notification-thread] Failed to send reply to notification channel client");
1821 /* Set reception state to receive the next message header. */
1822 ret
= client_reset_inbound_state(client
);
1824 ERR("[notification-thread] Failed to reset client communication's inbound state");
1836 /* Incoming data from client. */
1837 int handle_notification_thread_client_in(
1838 struct notification_thread_state
*state
, int socket
)
1841 struct notification_client
*client
;
1845 client
= get_client_from_socket(socket
, state
);
1847 /* Internal error, abort. */
1852 offset
= client
->communication
.inbound
.buffer
.size
-
1853 client
->communication
.inbound
.bytes_to_receive
;
1854 if (client
->communication
.inbound
.expect_creds
) {
1855 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
1856 client
->communication
.inbound
.buffer
.data
+ offset
,
1857 client
->communication
.inbound
.bytes_to_receive
,
1858 &client
->communication
.inbound
.creds
);
1860 client
->communication
.inbound
.expect_creds
= false;
1861 client
->communication
.inbound
.creds_received
= true;
1864 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
1865 client
->communication
.inbound
.buffer
.data
+ offset
,
1866 client
->communication
.inbound
.bytes_to_receive
);
1869 goto error_disconnect_client
;
1872 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
1873 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
1874 ret
= client_dispatch_message(client
, state
);
1877 * Only returns an error if this client must be
1880 goto error_disconnect_client
;
1887 error_disconnect_client
:
1888 ret
= handle_notification_thread_client_disconnect(socket
, state
);
1892 /* Client ready to receive outgoing data. */
1893 int handle_notification_thread_client_out(
1894 struct notification_thread_state
*state
, int socket
)
1897 struct notification_client
*client
;
1899 client
= get_client_from_socket(socket
, state
);
1901 /* Internal error, abort. */
1906 ret
= client_flush_outgoing_queue(client
, state
);
1915 bool evaluate_buffer_usage_condition(struct lttng_condition
*condition
,
1916 struct channel_state_sample
*sample
, uint64_t buffer_capacity
)
1918 bool result
= false;
1920 enum lttng_condition_type condition_type
;
1921 struct lttng_condition_buffer_usage
*use_condition
= container_of(
1922 condition
, struct lttng_condition_buffer_usage
,
1929 if (use_condition
->threshold_bytes
.set
) {
1930 threshold
= use_condition
->threshold_bytes
.value
;
1933 * Threshold was expressed as a ratio.
1935 * TODO the threshold (in bytes) of conditions expressed
1936 * as a ratio of total buffer size could be cached to
1937 * forego this double-multiplication or it could be performed
1938 * as fixed-point math.
1940 * Note that caching should accomodate the case where the
1941 * condition applies to multiple channels (i.e. don't assume
1942 * that all channels matching my_chann* have the same size...)
1944 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
1945 (double) buffer_capacity
);
1948 condition_type
= lttng_condition_get_type(condition
);
1949 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
1950 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1951 threshold
, sample
->highest_usage
);
1954 * The low condition should only be triggered once _all_ of the
1955 * streams in a channel have gone below the "low" threshold.
1957 if (sample
->highest_usage
<= threshold
) {
1961 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1962 threshold
, sample
->highest_usage
);
1965 * For high buffer usage scenarios, we want to trigger whenever
1966 * _any_ of the streams has reached the "high" threshold.
1968 if (sample
->highest_usage
>= threshold
) {
1977 int evaluate_condition(struct lttng_condition
*condition
,
1978 struct lttng_evaluation
**evaluation
,
1979 struct notification_thread_state
*state
,
1980 struct channel_state_sample
*previous_sample
,
1981 struct channel_state_sample
*latest_sample
,
1982 uint64_t buffer_capacity
)
1985 enum lttng_condition_type condition_type
;
1986 bool previous_sample_result
;
1987 bool latest_sample_result
;
1989 condition_type
= lttng_condition_get_type(condition
);
1990 /* No other condition type supported for the moment. */
1991 assert(condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
1992 condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
1994 previous_sample_result
= evaluate_buffer_usage_condition(condition
,
1995 previous_sample
, buffer_capacity
);
1996 latest_sample_result
= evaluate_buffer_usage_condition(condition
,
1997 latest_sample
, buffer_capacity
);
1999 if (!latest_sample_result
||
2000 (previous_sample_result
== latest_sample_result
)) {
2002 * Only trigger on a condition evaluation transition.
2004 * NOTE: This edge-triggered logic may not be appropriate for
2005 * future condition types.
2010 if (evaluation
&& latest_sample_result
) {
2011 *evaluation
= lttng_evaluation_buffer_usage_create(
2013 latest_sample
->highest_usage
,
2025 int client_enqueue_dropped_notification(struct notification_client
*client
,
2026 struct notification_thread_state
*state
)
2029 struct lttng_notification_channel_message msg
= {
2030 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
2034 ret
= lttng_dynamic_buffer_append(
2035 &client
->communication
.outbound
.buffer
, &msg
,
2041 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
2042 struct lttng_evaluation
*evaluation
,
2043 struct notification_client_list
* client_list
,
2044 struct notification_thread_state
*state
,
2045 uid_t channel_uid
, gid_t channel_gid
)
2048 struct lttng_dynamic_buffer msg_buffer
;
2049 struct notification_client_list_element
*client_list_element
, *tmp
;
2050 struct lttng_notification
*notification
;
2051 struct lttng_condition
*condition
;
2052 ssize_t expected_notification_size
, notification_size
;
2053 struct lttng_notification_channel_message msg
;
2055 lttng_dynamic_buffer_init(&msg_buffer
);
2057 condition
= lttng_trigger_get_condition(trigger
);
2060 notification
= lttng_notification_create(condition
, evaluation
);
2061 if (!notification
) {
2066 expected_notification_size
= lttng_notification_serialize(notification
,
2068 if (expected_notification_size
< 0) {
2069 ERR("[notification-thread] Failed to get size of serialized notification");
2074 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
2075 msg
.size
= (uint32_t) expected_notification_size
;
2076 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg
, sizeof(msg
));
2081 ret
= lttng_dynamic_buffer_set_size(&msg_buffer
,
2082 msg_buffer
.size
+ expected_notification_size
);
2087 notification_size
= lttng_notification_serialize(notification
,
2088 msg_buffer
.data
+ sizeof(msg
));
2089 if (notification_size
!= expected_notification_size
) {
2090 ERR("[notification-thread] Failed to serialize notification");
2095 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2096 &client_list
->list
, node
) {
2097 struct notification_client
*client
=
2098 client_list_element
->client
;
2100 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
2102 /* Client is not allowed to monitor this channel. */
2103 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
2107 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
2108 client
->socket
, msg_buffer
.size
);
2109 if (client
->communication
.outbound
.buffer
.size
) {
2111 * Outgoing data is already buffered for this client;
2112 * drop the notification and enqueue a "dropped
2113 * notification" message if this is the first dropped
2114 * notification since the socket spilled-over to the
2117 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
2119 if (!client
->communication
.outbound
.dropped_notification
) {
2120 client
->communication
.outbound
.dropped_notification
= true;
2121 ret
= client_enqueue_dropped_notification(
2130 ret
= lttng_dynamic_buffer_append_buffer(
2131 &client
->communication
.outbound
.buffer
,
2137 ret
= client_flush_outgoing_queue(client
, state
);
2144 lttng_notification_destroy(notification
);
2145 lttng_dynamic_buffer_reset(&msg_buffer
);
2149 int handle_notification_thread_channel_sample(
2150 struct notification_thread_state
*state
, int pipe
,
2151 enum lttng_domain_type domain
)
2154 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2155 struct channel_state_sample previous_sample
, latest_sample
;
2156 struct channel_info
*channel_info
;
2157 struct cds_lfht_node
*node
;
2158 struct cds_lfht_iter iter
;
2159 struct lttng_channel_trigger_list
*trigger_list
;
2160 struct lttng_trigger_list_element
*trigger_list_element
;
2161 bool previous_sample_available
= false;
2164 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2165 * ensuring that read/write of sampling messages are atomic.
2167 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2168 if (ret
!= sizeof(sample_msg
)) {
2169 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2176 latest_sample
.key
.key
= sample_msg
.key
;
2177 latest_sample
.key
.domain
= domain
;
2178 latest_sample
.highest_usage
= sample_msg
.highest
;
2179 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2183 /* Retrieve the channel's informations */
2184 cds_lfht_lookup(state
->channels_ht
,
2185 hash_channel_key(&latest_sample
.key
),
2189 node
= cds_lfht_iter_get_node(&iter
);
2192 * Not an error since the consumer can push a sample to the pipe
2193 * and the rest of the session daemon could notify us of the
2194 * channel's destruction before we get a chance to process that
2197 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2198 latest_sample
.key
.key
,
2199 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2203 channel_info
= caa_container_of(node
, struct channel_info
,
2205 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
")",
2206 channel_info
->channel_name
,
2207 latest_sample
.key
.key
,
2208 channel_info
->session_name
,
2209 latest_sample
.highest_usage
,
2210 latest_sample
.lowest_usage
);
2212 /* Retrieve the channel's last sample, if it exists, and update it. */
2213 cds_lfht_lookup(state
->channel_state_ht
,
2214 hash_channel_key(&latest_sample
.key
),
2215 match_channel_state_sample
,
2218 node
= cds_lfht_iter_get_node(&iter
);
2220 struct channel_state_sample
*stored_sample
;
2222 /* Update the sample stored. */
2223 stored_sample
= caa_container_of(node
,
2224 struct channel_state_sample
,
2225 channel_state_ht_node
);
2226 memcpy(&previous_sample
, stored_sample
,
2227 sizeof(previous_sample
));
2228 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2229 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2230 previous_sample_available
= true;
2233 * This is the channel's first sample, allocate space for and
2234 * store the new sample.
2236 struct channel_state_sample
*stored_sample
;
2238 stored_sample
= zmalloc(sizeof(*stored_sample
));
2239 if (!stored_sample
) {
2244 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2245 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2246 cds_lfht_add(state
->channel_state_ht
,
2247 hash_channel_key(&stored_sample
->key
),
2248 &stored_sample
->channel_state_ht_node
);
2251 /* Find triggers associated with this channel. */
2252 cds_lfht_lookup(state
->channel_triggers_ht
,
2253 hash_channel_key(&latest_sample
.key
),
2254 match_channel_trigger_list
,
2257 node
= cds_lfht_iter_get_node(&iter
);
2262 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2263 channel_triggers_ht_node
);
2264 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2266 struct lttng_condition
*condition
;
2267 struct lttng_action
*action
;
2268 struct lttng_trigger
*trigger
;
2269 struct notification_client_list
*client_list
;
2270 struct lttng_evaluation
*evaluation
= NULL
;
2272 trigger
= trigger_list_element
->trigger
;
2273 condition
= lttng_trigger_get_condition(trigger
);
2275 action
= lttng_trigger_get_action(trigger
);
2277 /* Notify actions are the only type currently supported. */
2278 assert(lttng_action_get_type(action
) ==
2279 LTTNG_ACTION_TYPE_NOTIFY
);
2282 * Check if any client is subscribed to the result of this
2285 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2286 lttng_condition_hash(condition
),
2290 node
= cds_lfht_iter_get_node(&iter
);
2293 client_list
= caa_container_of(node
,
2294 struct notification_client_list
,
2295 notification_trigger_ht_node
);
2296 if (cds_list_empty(&client_list
->list
)) {
2298 * No clients interested in the evaluation's result,
2304 ret
= evaluate_condition(condition
, &evaluation
, state
,
2305 previous_sample_available
? &previous_sample
: NULL
,
2306 &latest_sample
, channel_info
->capacity
);
2315 /* Dispatch evaluation result to all clients. */
2316 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2317 evaluation
, client_list
, state
,
2318 channel_info
->uid
, channel_info
->gid
);