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
;
1122 struct cds_lfht_iter lookup_iter
;
1124 if (!trigger_applies_to_channel(trigger
, channel
)) {
1128 cds_lfht_lookup(state
->channel_triggers_ht
,
1129 hash_channel_key(&channel
->key
),
1130 match_channel_trigger_list
,
1133 node
= cds_lfht_iter_get_node(&lookup_iter
);
1135 trigger_list
= caa_container_of(node
,
1136 struct lttng_channel_trigger_list
,
1137 channel_triggers_ht_node
);
1139 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1140 if (!trigger_list_element
) {
1142 goto error_free_client_list
;
1144 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1145 trigger_list_element
->trigger
= trigger
;
1146 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1148 /* A trigger can only apply to one channel. */
1152 *cmd_result
= LTTNG_OK
;
1153 error_free_client_list
:
1155 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1156 &client_list
->list
, node
) {
1157 free(client_list_element
);
1161 error_free_ht_element
:
1162 free(trigger_ht_element
);
1165 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1167 lttng_condition_destroy(condition
);
1168 lttng_action_destroy(action
);
1169 lttng_trigger_destroy(trigger
);
1176 int handle_notification_thread_command_unregister_trigger(
1177 struct notification_thread_state
*state
,
1178 struct lttng_trigger
*trigger
,
1179 enum lttng_error_code
*_cmd_reply
)
1181 struct cds_lfht_iter iter
;
1182 struct cds_lfht_node
*node
, *triggers_ht_node
;
1183 struct lttng_channel_trigger_list
*trigger_list
;
1184 struct notification_client_list
*client_list
;
1185 struct notification_client_list_element
*client_list_element
, *tmp
;
1186 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1187 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1189 struct lttng_action
*action
;
1190 enum lttng_error_code cmd_reply
;
1194 cds_lfht_lookup(state
->triggers_ht
,
1195 lttng_condition_hash(condition
),
1199 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1200 if (!triggers_ht_node
) {
1201 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1204 cmd_reply
= LTTNG_OK
;
1207 /* Remove trigger from channel_triggers_ht. */
1208 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1209 channel_triggers_ht_node
) {
1210 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1212 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1213 &trigger_list
->list
, node
) {
1214 struct lttng_condition
*current_condition
=
1215 lttng_trigger_get_condition(
1216 trigger_element
->trigger
);
1218 assert(current_condition
);
1219 if (!lttng_condition_is_equal(condition
,
1220 current_condition
)) {
1224 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1225 cds_list_del(&trigger_element
->node
);
1226 /* A trigger can only appear once per channel */
1232 * Remove and release the client list from
1233 * notification_trigger_clients_ht.
1235 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1236 lttng_condition_hash(condition
),
1240 node
= cds_lfht_iter_get_node(&iter
);
1242 client_list
= caa_container_of(node
, struct notification_client_list
,
1243 notification_trigger_ht_node
);
1244 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1245 &client_list
->list
, node
) {
1246 free(client_list_element
);
1248 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1251 /* Remove trigger from triggers_ht. */
1252 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1253 struct lttng_trigger_ht_element
, node
);
1254 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1256 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1257 lttng_condition_destroy(condition
);
1258 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1259 lttng_action_destroy(action
);
1260 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1261 free(trigger_ht_element
);
1265 *_cmd_reply
= cmd_reply
;
1270 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1271 int handle_notification_thread_command(
1272 struct notification_thread_handle
*handle
,
1273 struct notification_thread_state
*state
)
1277 struct notification_thread_command
*cmd
;
1279 /* Read event_fd to put it back into a quiescent state. */
1280 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
, sizeof(counter
));
1285 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1286 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1287 struct notification_thread_command
, cmd_list_node
);
1288 switch (cmd
->type
) {
1289 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1290 DBG("[notification-thread] Received register trigger command");
1291 ret
= handle_notification_thread_command_register_trigger(
1292 state
, cmd
->parameters
.trigger
,
1295 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1296 DBG("[notification-thread] Received unregister trigger command");
1297 ret
= handle_notification_thread_command_unregister_trigger(
1298 state
, cmd
->parameters
.trigger
,
1301 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1302 DBG("[notification-thread] Received add channel command");
1303 ret
= handle_notification_thread_command_add_channel(
1304 state
, &cmd
->parameters
.add_channel
,
1307 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1308 DBG("[notification-thread] Received remove channel command");
1309 ret
= handle_notification_thread_command_remove_channel(
1310 state
, cmd
->parameters
.remove_channel
.key
,
1311 cmd
->parameters
.remove_channel
.domain
,
1314 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1315 DBG("[notification-thread] Received quit command");
1316 cmd
->reply_code
= LTTNG_OK
;
1320 ERR("[notification-thread] Unknown internal command received");
1328 cds_list_del(&cmd
->cmd_list_node
);
1329 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1330 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1333 /* Wake-up and return a fatal error to the calling thread. */
1334 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1335 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1336 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1338 /* Indicate a fatal error to the caller. */
1343 unsigned long hash_client_socket(int socket
)
1345 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1349 int socket_set_non_blocking(int socket
)
1353 /* Set the pipe as non-blocking. */
1354 ret
= fcntl(socket
, F_GETFL
, 0);
1356 PERROR("fcntl get socket flags");
1361 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1363 PERROR("fcntl set O_NONBLOCK socket flag");
1366 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1372 int client_reset_inbound_state(struct notification_client
*client
)
1376 ret
= lttng_dynamic_buffer_set_size(
1377 &client
->communication
.inbound
.buffer
, 0);
1380 client
->communication
.inbound
.bytes_to_receive
=
1381 sizeof(struct lttng_notification_channel_message
);
1382 client
->communication
.inbound
.msg_type
=
1383 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1384 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1385 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1386 ret
= lttng_dynamic_buffer_set_size(
1387 &client
->communication
.inbound
.buffer
,
1388 client
->communication
.inbound
.bytes_to_receive
);
1392 int handle_notification_thread_client_connect(
1393 struct notification_thread_state
*state
)
1396 struct notification_client
*client
;
1398 DBG("[notification-thread] Handling new notification channel client connection");
1400 client
= zmalloc(sizeof(*client
));
1406 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1407 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1408 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1409 client
->communication
.inbound
.expect_creds
= true;
1410 ret
= client_reset_inbound_state(client
);
1412 ERR("[notification-thread] Failed to reset client communication's inbound state");
1417 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1419 ERR("[notification-thread] Failed to accept new notification channel client connection");
1424 client
->socket
= ret
;
1426 ret
= socket_set_non_blocking(client
->socket
);
1428 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1432 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1434 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1439 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1440 LPOLLIN
| LPOLLERR
|
1441 LPOLLHUP
| LPOLLRDHUP
);
1443 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1447 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1451 cds_lfht_add(state
->client_socket_ht
,
1452 hash_client_socket(client
->socket
),
1453 &client
->client_socket_ht_node
);
1458 notification_client_destroy(client
, state
);
1462 int handle_notification_thread_client_disconnect(
1464 struct notification_thread_state
*state
)
1467 struct notification_client
*client
;
1470 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1472 client
= get_client_from_socket(client_socket
, state
);
1474 /* Internal state corruption, fatal error. */
1475 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1481 ret
= lttng_poll_del(&state
->events
, client_socket
);
1483 ERR("[notification-thread] Failed to remove client socket from poll set");
1485 cds_lfht_del(state
->client_socket_ht
,
1486 &client
->client_socket_ht_node
);
1487 notification_client_destroy(client
, state
);
1493 int handle_notification_thread_client_disconnect_all(
1494 struct notification_thread_state
*state
)
1496 struct cds_lfht_iter iter
;
1497 struct notification_client
*client
;
1498 bool error_encoutered
= false;
1501 DBG("[notification-thread] Closing all client connections");
1502 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1503 client_socket_ht_node
) {
1506 ret
= handle_notification_thread_client_disconnect(
1507 client
->socket
, state
);
1509 error_encoutered
= true;
1513 return error_encoutered
? 1 : 0;
1516 int handle_notification_thread_trigger_unregister_all(
1517 struct notification_thread_state
*state
)
1519 bool error_occurred
= false;
1520 struct cds_lfht_iter iter
;
1521 struct lttng_trigger_ht_element
*trigger_ht_element
;
1523 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1525 int ret
= handle_notification_thread_command_unregister_trigger(
1526 state
, trigger_ht_element
->trigger
, NULL
);
1528 error_occurred
= true;
1531 return error_occurred
? -1 : 0;
1535 int client_flush_outgoing_queue(struct notification_client
*client
,
1536 struct notification_thread_state
*state
)
1539 size_t to_send_count
;
1541 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1542 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1543 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1546 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1547 client
->communication
.outbound
.buffer
.data
,
1549 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1550 (ret
> 0 && ret
< to_send_count
)) {
1551 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1553 to_send_count
-= max(ret
, 0);
1555 memcpy(client
->communication
.outbound
.buffer
.data
,
1556 client
->communication
.outbound
.buffer
.data
+
1557 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1559 ret
= lttng_dynamic_buffer_set_size(
1560 &client
->communication
.outbound
.buffer
,
1567 * We want to be notified whenever there is buffer space
1568 * available to send the rest of the payload.
1570 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1571 CLIENT_POLL_MASK_IN_OUT
);
1575 } else if (ret
< 0) {
1576 /* Generic error, disconnect the client. */
1577 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1579 ret
= handle_notification_thread_client_disconnect(
1580 client
->socket
, state
);
1585 /* No error and flushed the queue completely. */
1586 ret
= lttng_dynamic_buffer_set_size(
1587 &client
->communication
.outbound
.buffer
, 0);
1591 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1592 CLIENT_POLL_MASK_IN
);
1597 client
->communication
.outbound
.queued_command_reply
= false;
1598 client
->communication
.outbound
.dropped_notification
= false;
1607 int client_send_command_reply(struct notification_client
*client
,
1608 struct notification_thread_state
*state
,
1609 enum lttng_notification_channel_status status
)
1612 struct lttng_notification_channel_command_reply reply
= {
1613 .status
= (int8_t) status
,
1615 struct lttng_notification_channel_message msg
= {
1616 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1617 .size
= sizeof(reply
),
1619 char buffer
[sizeof(msg
) + sizeof(reply
)];
1621 if (client
->communication
.outbound
.queued_command_reply
) {
1622 /* Protocol error. */
1626 memcpy(buffer
, &msg
, sizeof(msg
));
1627 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1628 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1630 /* Enqueue buffer to outgoing queue and flush it. */
1631 ret
= lttng_dynamic_buffer_append(
1632 &client
->communication
.outbound
.buffer
,
1633 buffer
, sizeof(buffer
));
1638 ret
= client_flush_outgoing_queue(client
, state
);
1643 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1644 /* Queue could not be emptied. */
1645 client
->communication
.outbound
.queued_command_reply
= true;
1654 int client_dispatch_message(struct notification_client
*client
,
1655 struct notification_thread_state
*state
)
1659 if (client
->communication
.inbound
.msg_type
!=
1660 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1661 client
->communication
.inbound
.msg_type
!=
1662 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1663 !client
->validated
) {
1664 WARN("[notification-thread] client attempted a command before handshake");
1669 switch (client
->communication
.inbound
.msg_type
) {
1670 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1673 * Receiving message header. The function will be called again
1674 * once the rest of the message as been received and can be
1677 const struct lttng_notification_channel_message
*msg
;
1679 assert(sizeof(*msg
) ==
1680 client
->communication
.inbound
.buffer
.size
);
1681 msg
= (const struct lttng_notification_channel_message
*)
1682 client
->communication
.inbound
.buffer
.data
;
1684 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1685 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1690 switch (msg
->type
) {
1691 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1692 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1693 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1697 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1701 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
1702 client
->communication
.inbound
.msg_type
=
1703 (enum lttng_notification_channel_message_type
) msg
->type
;
1704 ret
= lttng_dynamic_buffer_set_size(
1705 &client
->communication
.inbound
.buffer
, msg
->size
);
1711 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1713 struct lttng_notification_channel_command_handshake
*handshake_client
;
1714 struct lttng_notification_channel_command_handshake handshake_reply
= {
1715 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
1716 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
1718 struct lttng_notification_channel_message msg_header
= {
1719 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
1720 .size
= sizeof(handshake_reply
),
1722 enum lttng_notification_channel_status status
=
1723 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1724 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
1726 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
1727 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
1728 sizeof(handshake_reply
));
1731 (struct lttng_notification_channel_command_handshake
*)
1732 client
->communication
.inbound
.buffer
.data
;
1733 client
->major
= handshake_client
->major
;
1734 client
->minor
= handshake_client
->minor
;
1735 if (!client
->communication
.inbound
.creds_received
) {
1736 ERR("[notification-thread] No credentials received from client");
1741 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
1742 &client
->communication
.inbound
.creds
);
1743 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
1744 &client
->communication
.inbound
.creds
);
1745 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
1746 client
->uid
, client
->gid
, (int) client
->major
,
1747 (int) client
->minor
);
1749 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
1750 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
1753 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
1754 send_buffer
, sizeof(send_buffer
));
1756 ERR("[notification-thread] Failed to send protocol version to notification channel client");
1760 ret
= client_flush_outgoing_queue(client
, state
);
1765 ret
= client_send_command_reply(client
, state
, status
);
1767 ERR("[notification-thread] Failed to send reply to notification channel client");
1771 /* Set reception state to receive the next message header. */
1772 ret
= client_reset_inbound_state(client
);
1774 ERR("[notification-thread] Failed to reset client communication's inbound state");
1777 client
->validated
= true;
1780 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1781 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1783 struct lttng_condition
*condition
;
1784 enum lttng_notification_channel_status status
=
1785 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1786 const struct lttng_buffer_view condition_view
=
1787 lttng_buffer_view_from_dynamic_buffer(
1788 &client
->communication
.inbound
.buffer
,
1790 size_t expected_condition_size
=
1791 client
->communication
.inbound
.buffer
.size
;
1793 ret
= lttng_condition_create_from_buffer(&condition_view
,
1795 if (ret
!= expected_condition_size
) {
1796 ERR("[notification-thread] Malformed condition received from client");
1800 if (client
->communication
.inbound
.msg_type
==
1801 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
1803 * FIXME The current state should be evaluated on
1806 ret
= notification_thread_client_subscribe(client
,
1807 condition
, state
, &status
);
1809 ret
= notification_thread_client_unsubscribe(client
,
1810 condition
, state
, &status
);
1816 ret
= client_send_command_reply(client
, state
, status
);
1818 ERR("[notification-thread] Failed to send reply to notification channel client");
1822 /* Set reception state to receive the next message header. */
1823 ret
= client_reset_inbound_state(client
);
1825 ERR("[notification-thread] Failed to reset client communication's inbound state");
1837 /* Incoming data from client. */
1838 int handle_notification_thread_client_in(
1839 struct notification_thread_state
*state
, int socket
)
1842 struct notification_client
*client
;
1846 client
= get_client_from_socket(socket
, state
);
1848 /* Internal error, abort. */
1853 offset
= client
->communication
.inbound
.buffer
.size
-
1854 client
->communication
.inbound
.bytes_to_receive
;
1855 if (client
->communication
.inbound
.expect_creds
) {
1856 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
1857 client
->communication
.inbound
.buffer
.data
+ offset
,
1858 client
->communication
.inbound
.bytes_to_receive
,
1859 &client
->communication
.inbound
.creds
);
1861 client
->communication
.inbound
.expect_creds
= false;
1862 client
->communication
.inbound
.creds_received
= true;
1865 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
1866 client
->communication
.inbound
.buffer
.data
+ offset
,
1867 client
->communication
.inbound
.bytes_to_receive
);
1870 goto error_disconnect_client
;
1873 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
1874 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
1875 ret
= client_dispatch_message(client
, state
);
1878 * Only returns an error if this client must be
1881 goto error_disconnect_client
;
1888 error_disconnect_client
:
1889 ret
= handle_notification_thread_client_disconnect(socket
, state
);
1893 /* Client ready to receive outgoing data. */
1894 int handle_notification_thread_client_out(
1895 struct notification_thread_state
*state
, int socket
)
1898 struct notification_client
*client
;
1900 client
= get_client_from_socket(socket
, state
);
1902 /* Internal error, abort. */
1907 ret
= client_flush_outgoing_queue(client
, state
);
1916 bool evaluate_buffer_usage_condition(struct lttng_condition
*condition
,
1917 struct channel_state_sample
*sample
, uint64_t buffer_capacity
)
1919 bool result
= false;
1921 enum lttng_condition_type condition_type
;
1922 struct lttng_condition_buffer_usage
*use_condition
= container_of(
1923 condition
, struct lttng_condition_buffer_usage
,
1930 if (use_condition
->threshold_bytes
.set
) {
1931 threshold
= use_condition
->threshold_bytes
.value
;
1934 * Threshold was expressed as a ratio.
1936 * TODO the threshold (in bytes) of conditions expressed
1937 * as a ratio of total buffer size could be cached to
1938 * forego this double-multiplication or it could be performed
1939 * as fixed-point math.
1941 * Note that caching should accomodate the case where the
1942 * condition applies to multiple channels (i.e. don't assume
1943 * that all channels matching my_chann* have the same size...)
1945 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
1946 (double) buffer_capacity
);
1949 condition_type
= lttng_condition_get_type(condition
);
1950 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
1951 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1952 threshold
, sample
->highest_usage
);
1955 * The low condition should only be triggered once _all_ of the
1956 * streams in a channel have gone below the "low" threshold.
1958 if (sample
->highest_usage
<= threshold
) {
1962 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1963 threshold
, sample
->highest_usage
);
1966 * For high buffer usage scenarios, we want to trigger whenever
1967 * _any_ of the streams has reached the "high" threshold.
1969 if (sample
->highest_usage
>= threshold
) {
1978 int evaluate_condition(struct lttng_condition
*condition
,
1979 struct lttng_evaluation
**evaluation
,
1980 struct notification_thread_state
*state
,
1981 struct channel_state_sample
*previous_sample
,
1982 struct channel_state_sample
*latest_sample
,
1983 uint64_t buffer_capacity
)
1986 enum lttng_condition_type condition_type
;
1987 bool previous_sample_result
;
1988 bool latest_sample_result
;
1990 condition_type
= lttng_condition_get_type(condition
);
1991 /* No other condition type supported for the moment. */
1992 assert(condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
1993 condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
1995 previous_sample_result
= evaluate_buffer_usage_condition(condition
,
1996 previous_sample
, buffer_capacity
);
1997 latest_sample_result
= evaluate_buffer_usage_condition(condition
,
1998 latest_sample
, buffer_capacity
);
2000 if (!latest_sample_result
||
2001 (previous_sample_result
== latest_sample_result
)) {
2003 * Only trigger on a condition evaluation transition.
2005 * NOTE: This edge-triggered logic may not be appropriate for
2006 * future condition types.
2011 if (evaluation
&& latest_sample_result
) {
2012 *evaluation
= lttng_evaluation_buffer_usage_create(
2014 latest_sample
->highest_usage
,
2026 int client_enqueue_dropped_notification(struct notification_client
*client
,
2027 struct notification_thread_state
*state
)
2030 struct lttng_notification_channel_message msg
= {
2031 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
2035 ret
= lttng_dynamic_buffer_append(
2036 &client
->communication
.outbound
.buffer
, &msg
,
2042 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
2043 struct lttng_evaluation
*evaluation
,
2044 struct notification_client_list
* client_list
,
2045 struct notification_thread_state
*state
,
2046 uid_t channel_uid
, gid_t channel_gid
)
2049 struct lttng_dynamic_buffer msg_buffer
;
2050 struct notification_client_list_element
*client_list_element
, *tmp
;
2051 struct lttng_notification
*notification
;
2052 struct lttng_condition
*condition
;
2053 ssize_t expected_notification_size
, notification_size
;
2054 struct lttng_notification_channel_message msg
;
2056 lttng_dynamic_buffer_init(&msg_buffer
);
2058 condition
= lttng_trigger_get_condition(trigger
);
2061 notification
= lttng_notification_create(condition
, evaluation
);
2062 if (!notification
) {
2067 expected_notification_size
= lttng_notification_serialize(notification
,
2069 if (expected_notification_size
< 0) {
2070 ERR("[notification-thread] Failed to get size of serialized notification");
2075 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
2076 msg
.size
= (uint32_t) expected_notification_size
;
2077 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg
, sizeof(msg
));
2082 ret
= lttng_dynamic_buffer_set_size(&msg_buffer
,
2083 msg_buffer
.size
+ expected_notification_size
);
2088 notification_size
= lttng_notification_serialize(notification
,
2089 msg_buffer
.data
+ sizeof(msg
));
2090 if (notification_size
!= expected_notification_size
) {
2091 ERR("[notification-thread] Failed to serialize notification");
2096 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2097 &client_list
->list
, node
) {
2098 struct notification_client
*client
=
2099 client_list_element
->client
;
2101 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
2103 /* Client is not allowed to monitor this channel. */
2104 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
2108 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
2109 client
->socket
, msg_buffer
.size
);
2110 if (client
->communication
.outbound
.buffer
.size
) {
2112 * Outgoing data is already buffered for this client;
2113 * drop the notification and enqueue a "dropped
2114 * notification" message if this is the first dropped
2115 * notification since the socket spilled-over to the
2118 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
2120 if (!client
->communication
.outbound
.dropped_notification
) {
2121 client
->communication
.outbound
.dropped_notification
= true;
2122 ret
= client_enqueue_dropped_notification(
2131 ret
= lttng_dynamic_buffer_append_buffer(
2132 &client
->communication
.outbound
.buffer
,
2138 ret
= client_flush_outgoing_queue(client
, state
);
2145 lttng_notification_destroy(notification
);
2146 lttng_dynamic_buffer_reset(&msg_buffer
);
2150 int handle_notification_thread_channel_sample(
2151 struct notification_thread_state
*state
, int pipe
,
2152 enum lttng_domain_type domain
)
2155 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2156 struct channel_state_sample previous_sample
, latest_sample
;
2157 struct channel_info
*channel_info
;
2158 struct cds_lfht_node
*node
;
2159 struct cds_lfht_iter iter
;
2160 struct lttng_channel_trigger_list
*trigger_list
;
2161 struct lttng_trigger_list_element
*trigger_list_element
;
2162 bool previous_sample_available
= false;
2165 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2166 * ensuring that read/write of sampling messages are atomic.
2168 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2169 if (ret
!= sizeof(sample_msg
)) {
2170 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2177 latest_sample
.key
.key
= sample_msg
.key
;
2178 latest_sample
.key
.domain
= domain
;
2179 latest_sample
.highest_usage
= sample_msg
.highest
;
2180 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2184 /* Retrieve the channel's informations */
2185 cds_lfht_lookup(state
->channels_ht
,
2186 hash_channel_key(&latest_sample
.key
),
2190 node
= cds_lfht_iter_get_node(&iter
);
2193 * Not an error since the consumer can push a sample to the pipe
2194 * and the rest of the session daemon could notify us of the
2195 * channel's destruction before we get a chance to process that
2198 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2199 latest_sample
.key
.key
,
2200 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2204 channel_info
= caa_container_of(node
, struct channel_info
,
2206 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
")",
2207 channel_info
->channel_name
,
2208 latest_sample
.key
.key
,
2209 channel_info
->session_name
,
2210 latest_sample
.highest_usage
,
2211 latest_sample
.lowest_usage
);
2213 /* Retrieve the channel's last sample, if it exists, and update it. */
2214 cds_lfht_lookup(state
->channel_state_ht
,
2215 hash_channel_key(&latest_sample
.key
),
2216 match_channel_state_sample
,
2219 node
= cds_lfht_iter_get_node(&iter
);
2221 struct channel_state_sample
*stored_sample
;
2223 /* Update the sample stored. */
2224 stored_sample
= caa_container_of(node
,
2225 struct channel_state_sample
,
2226 channel_state_ht_node
);
2227 memcpy(&previous_sample
, stored_sample
,
2228 sizeof(previous_sample
));
2229 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2230 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2231 previous_sample_available
= true;
2234 * This is the channel's first sample, allocate space for and
2235 * store the new sample.
2237 struct channel_state_sample
*stored_sample
;
2239 stored_sample
= zmalloc(sizeof(*stored_sample
));
2240 if (!stored_sample
) {
2245 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2246 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2247 cds_lfht_add(state
->channel_state_ht
,
2248 hash_channel_key(&stored_sample
->key
),
2249 &stored_sample
->channel_state_ht_node
);
2252 /* Find triggers associated with this channel. */
2253 cds_lfht_lookup(state
->channel_triggers_ht
,
2254 hash_channel_key(&latest_sample
.key
),
2255 match_channel_trigger_list
,
2258 node
= cds_lfht_iter_get_node(&iter
);
2263 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2264 channel_triggers_ht_node
);
2265 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2267 struct lttng_condition
*condition
;
2268 struct lttng_action
*action
;
2269 struct lttng_trigger
*trigger
;
2270 struct notification_client_list
*client_list
;
2271 struct lttng_evaluation
*evaluation
= NULL
;
2273 trigger
= trigger_list_element
->trigger
;
2274 condition
= lttng_trigger_get_condition(trigger
);
2276 action
= lttng_trigger_get_action(trigger
);
2278 /* Notify actions are the only type currently supported. */
2279 assert(lttng_action_get_type(action
) ==
2280 LTTNG_ACTION_TYPE_NOTIFY
);
2283 * Check if any client is subscribed to the result of this
2286 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2287 lttng_condition_hash(condition
),
2291 node
= cds_lfht_iter_get_node(&iter
);
2294 client_list
= caa_container_of(node
,
2295 struct notification_client_list
,
2296 notification_trigger_ht_node
);
2297 if (cds_list_empty(&client_list
->list
)) {
2299 * No clients interested in the evaluation's result,
2305 ret
= evaluate_condition(condition
, &evaluation
, state
,
2306 previous_sample_available
? &previous_sample
: NULL
,
2307 &latest_sample
, channel_info
->capacity
);
2316 /* Dispatch evaluation result to all clients. */
2317 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2318 evaluation
, client_list
, state
,
2319 channel_info
->uid
, channel_info
->gid
);