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 "notification-thread.h"
23 #include "notification-thread-events.h"
24 #include "notification-thread-commands.h"
25 #include <common/defaults.h>
26 #include <common/error.h>
27 #include <common/futex.h>
28 #include <common/unix.h>
29 #include <common/dynamic-buffer.h>
30 #include <common/hashtable/utils.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/macros.h>
33 #include <lttng/condition/condition.h>
34 #include <lttng/action/action.h>
35 #include <lttng/notification/notification-internal.h>
36 #include <lttng/condition/condition-internal.h>
37 #include <lttng/condition/buffer-usage-internal.h>
38 #include <lttng/notification/channel-internal.h>
45 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
46 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
48 struct lttng_trigger_list_element
{
49 struct lttng_trigger
*trigger
;
50 struct cds_list_head node
;
53 struct lttng_channel_trigger_list
{
54 struct channel_key channel_key
;
55 struct cds_list_head list
;
56 struct cds_lfht_node channel_triggers_ht_node
;
59 struct lttng_trigger_ht_element
{
60 struct lttng_trigger
*trigger
;
61 struct cds_lfht_node node
;
64 struct lttng_condition_list_element
{
65 struct lttng_condition
*condition
;
66 struct cds_list_head node
;
69 struct notification_client_list_element
{
70 struct notification_client
*client
;
71 struct cds_list_head node
;
74 struct notification_client_list
{
75 struct lttng_trigger
*trigger
;
76 struct cds_list_head list
;
77 struct cds_lfht_node notification_trigger_ht_node
;
80 struct notification_client
{
82 /* Client protocol version. */
87 * Indicates if the credentials and versions of the client has been
92 * Conditions to which the client's notification channel is subscribed.
93 * List of struct lttng_condition_list_node. The condition member is
94 * owned by the client.
96 struct cds_list_head condition_list
;
97 struct cds_lfht_node client_socket_ht_node
;
100 struct lttng_dynamic_buffer buffer
;
101 /* Bytes left to receive for the current message. */
102 size_t bytes_to_receive
;
103 /* Type of the message being received. */
104 enum lttng_notification_channel_message_type msg_type
;
106 * Indicates whether or not credentials are expected
111 * Indicates whether or not credentials were received
115 lttng_sock_cred creds
;
119 * Indicates whether or not a notification addressed to
120 * this client was dropped because a command reply was
123 * A notification is dropped whenever the buffer is not
126 bool dropped_notification
;
128 * Indicates whether or not a command reply is already
129 * buffered. In this case, it means that the client is
130 * not consuming command replies before emitting a new
131 * one. This could be caused by a protocol error or a
132 * misbehaving/malicious client.
134 bool queued_command_reply
;
135 struct lttng_dynamic_buffer buffer
;
140 struct channel_state_sample
{
141 struct channel_key key
;
142 struct cds_lfht_node channel_state_ht_node
;
143 uint64_t highest_usage
;
144 uint64_t lowest_usage
;
148 int match_client(struct cds_lfht_node
*node
, const void *key
)
150 /* This double-cast is intended to supress pointer-to-cast warning. */
151 int socket
= (int) (intptr_t) key
;
152 struct notification_client
*client
;
154 client
= caa_container_of(node
, struct notification_client
,
155 client_socket_ht_node
);
157 return !!(client
->socket
== socket
);
161 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
163 struct channel_key
*channel_key
= (struct channel_key
*) key
;
164 struct lttng_channel_trigger_list
*trigger_list
;
166 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
167 channel_triggers_ht_node
);
169 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
170 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
174 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
176 struct channel_key
*channel_key
= (struct channel_key
*) key
;
177 struct channel_state_sample
*sample
;
179 sample
= caa_container_of(node
, struct channel_state_sample
,
180 channel_state_ht_node
);
182 return !!((channel_key
->key
== sample
->key
.key
) &&
183 (channel_key
->domain
== sample
->key
.domain
));
187 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
189 struct channel_key
*channel_key
= (struct channel_key
*) key
;
190 struct channel_info
*channel_info
;
192 channel_info
= caa_container_of(node
, struct channel_info
,
195 return !!((channel_key
->key
== channel_info
->key
.key
) &&
196 (channel_key
->domain
== channel_info
->key
.domain
));
200 int match_condition(struct cds_lfht_node
*node
, const void *key
)
202 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
203 struct lttng_trigger_ht_element
*trigger
;
204 struct lttng_condition
*condition
;
206 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
208 condition
= lttng_trigger_get_condition(trigger
->trigger
);
211 return !!lttng_condition_is_equal(condition_key
, condition
);
215 int match_client_list(struct cds_lfht_node
*node
, const void *key
)
217 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
218 struct notification_client_list
*client_list
;
219 struct lttng_condition
*condition
;
220 struct lttng_condition
*condition_key
= lttng_trigger_get_condition(
223 assert(condition_key
);
225 client_list
= caa_container_of(node
, struct notification_client_list
,
226 notification_trigger_ht_node
);
227 condition
= lttng_trigger_get_condition(client_list
->trigger
);
229 return !!lttng_condition_is_equal(condition_key
, condition
);
233 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
235 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
236 struct notification_client_list
*client_list
;
237 struct lttng_condition
*condition
;
239 assert(condition_key
);
241 client_list
= caa_container_of(node
, struct notification_client_list
,
242 notification_trigger_ht_node
);
243 condition
= lttng_trigger_get_condition(client_list
->trigger
);
245 return !!lttng_condition_is_equal(condition_key
, condition
);
249 unsigned long lttng_condition_buffer_usage_hash(
250 struct lttng_condition
*_condition
)
252 unsigned long hash
= 0;
253 struct lttng_condition_buffer_usage
*condition
;
255 condition
= container_of(_condition
,
256 struct lttng_condition_buffer_usage
, parent
);
258 if (condition
->session_name
) {
259 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
261 if (condition
->channel_name
) {
262 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
264 if (condition
->domain
.set
) {
265 hash
^= hash_key_ulong(
266 (void *) condition
->domain
.type
,
269 if (condition
->threshold_ratio
.set
) {
272 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
273 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
274 } else if (condition
->threshold_ratio
.set
) {
277 val
= condition
->threshold_bytes
.value
;
278 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
284 * The lttng_condition hashing code is kept in this file (rather than
285 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
286 * don't want to link in liblttng-ctl.
289 unsigned long lttng_condition_hash(struct lttng_condition
*condition
)
291 switch (condition
->type
) {
292 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
293 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
294 return lttng_condition_buffer_usage_hash(condition
);
296 ERR("[notification-thread] Unexpected condition type caught");
302 void channel_info_destroy(struct channel_info
*channel_info
)
308 if (channel_info
->session_name
) {
309 free(channel_info
->session_name
);
311 if (channel_info
->channel_name
) {
312 free(channel_info
->channel_name
);
318 struct channel_info
*channel_info_copy(struct channel_info
*channel_info
)
320 struct channel_info
*copy
= zmalloc(sizeof(*channel_info
));
322 assert(channel_info
);
323 assert(channel_info
->session_name
);
324 assert(channel_info
->channel_name
);
330 memcpy(copy
, channel_info
, sizeof(*channel_info
));
331 copy
->session_name
= NULL
;
332 copy
->channel_name
= NULL
;
334 copy
->session_name
= strdup(channel_info
->session_name
);
335 if (!copy
->session_name
) {
338 copy
->channel_name
= strdup(channel_info
->channel_name
);
339 if (!copy
->channel_name
) {
342 cds_lfht_node_init(&channel_info
->channels_ht_node
);
346 channel_info_destroy(copy
);
351 int notification_thread_client_subscribe(struct notification_client
*client
,
352 struct lttng_condition
*condition
,
353 struct notification_thread_state
*state
,
354 enum lttng_notification_channel_status
*_status
)
357 struct cds_lfht_iter iter
;
358 struct cds_lfht_node
*node
;
359 struct notification_client_list
*client_list
;
360 struct lttng_condition_list_element
*condition_list_element
= NULL
;
361 struct notification_client_list_element
*client_list_element
= NULL
;
362 enum lttng_notification_channel_status status
=
363 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
366 * Ensure that the client has not already subscribed to this condition
369 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
370 if (lttng_condition_is_equal(condition_list_element
->condition
,
372 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
377 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
378 if (!condition_list_element
) {
382 client_list_element
= zmalloc(sizeof(*client_list_element
));
383 if (!client_list_element
) {
391 * Add the newly-subscribed condition to the client's subscription list.
393 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
394 condition_list_element
->condition
= condition
;
395 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
398 * Add the client to the list of clients interested in a given trigger
399 * if a "notification" trigger with a corresponding condition was
402 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
403 lttng_condition_hash(condition
),
404 match_client_list_condition
,
407 node
= cds_lfht_iter_get_node(&iter
);
409 free(client_list_element
);
413 client_list
= caa_container_of(node
, struct notification_client_list
,
414 notification_trigger_ht_node
);
415 client_list_element
->client
= client
;
416 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
417 cds_list_add(&client_list_element
->node
, &client_list
->list
);
426 free(condition_list_element
);
427 free(client_list_element
);
432 int notification_thread_client_unsubscribe(
433 struct notification_client
*client
,
434 struct lttng_condition
*condition
,
435 struct notification_thread_state
*state
,
436 enum lttng_notification_channel_status
*_status
)
438 struct cds_lfht_iter iter
;
439 struct cds_lfht_node
*node
;
440 struct notification_client_list
*client_list
;
441 struct lttng_condition_list_element
*condition_list_element
,
443 struct notification_client_list_element
*client_list_element
,
445 bool condition_found
= false;
446 enum lttng_notification_channel_status status
=
447 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
449 /* Remove the condition from the client's condition list. */
450 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
451 &client
->condition_list
, node
) {
452 if (!lttng_condition_is_equal(condition_list_element
->condition
,
457 cds_list_del(&condition_list_element
->node
);
459 * The caller may be iterating on the client's conditions to
460 * tear down a client's connection. In this case, the condition
461 * will be destroyed at the end.
463 if (condition
!= condition_list_element
->condition
) {
464 lttng_condition_destroy(
465 condition_list_element
->condition
);
467 free(condition_list_element
);
468 condition_found
= true;
472 if (!condition_found
) {
473 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
478 * Remove the client from the list of clients interested the trigger
479 * matching the condition.
482 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
483 lttng_condition_hash(condition
),
484 match_client_list_condition
,
487 node
= cds_lfht_iter_get_node(&iter
);
492 client_list
= caa_container_of(node
, struct notification_client_list
,
493 notification_trigger_ht_node
);
494 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
495 &client_list
->list
, node
) {
496 if (client_list_element
->client
->socket
!= client
->socket
) {
499 cds_list_del(&client_list_element
->node
);
500 free(client_list_element
);
506 lttng_condition_destroy(condition
);
514 void notification_client_destroy(struct notification_client
*client
,
515 struct notification_thread_state
*state
)
517 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
523 /* Release all conditions to which the client was subscribed. */
524 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
525 &client
->condition_list
, node
) {
526 (void) notification_thread_client_unsubscribe(client
,
527 condition_list_element
->condition
, state
, NULL
);
530 if (client
->socket
>= 0) {
531 (void) lttcomm_close_unix_sock(client
->socket
);
533 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
534 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
539 * Call with rcu_read_lock held (and hold for the lifetime of the returned
543 struct notification_client
*get_client_from_socket(int socket
,
544 struct notification_thread_state
*state
)
546 struct cds_lfht_iter iter
;
547 struct cds_lfht_node
*node
;
548 struct notification_client
*client
= NULL
;
550 cds_lfht_lookup(state
->client_socket_ht
,
551 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
553 (void *) (unsigned long) socket
,
555 node
= cds_lfht_iter_get_node(&iter
);
560 client
= caa_container_of(node
, struct notification_client
,
561 client_socket_ht_node
);
567 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
568 struct channel_info
*info
)
570 enum lttng_condition_status status
;
571 struct lttng_condition
*condition
;
572 const char *trigger_session_name
= NULL
;
573 const char *trigger_channel_name
= NULL
;
574 enum lttng_domain_type trigger_domain
;
576 condition
= lttng_trigger_get_condition(trigger
);
581 switch (lttng_condition_get_type(condition
)) {
582 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
583 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
589 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
591 assert(status
== LTTNG_CONDITION_STATUS_OK
);
592 if (info
->key
.domain
!= trigger_domain
) {
596 status
= lttng_condition_buffer_usage_get_session_name(
597 condition
, &trigger_session_name
);
598 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_session_name
);
600 status
= lttng_condition_buffer_usage_get_channel_name(
601 condition
, &trigger_channel_name
);
602 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_channel_name
);
604 if (strcmp(info
->session_name
, trigger_session_name
)) {
607 if (strcmp(info
->channel_name
, trigger_channel_name
)) {
617 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
618 struct notification_client
*client
)
620 bool applies
= false;
621 struct lttng_condition_list_element
*condition_list_element
;
623 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
625 applies
= lttng_condition_is_equal(
626 condition_list_element
->condition
,
627 lttng_trigger_get_condition(trigger
));
636 unsigned long hash_channel_key(struct channel_key
*key
)
638 return hash_key_u64(&key
->key
, lttng_ht_seed
) ^ hash_key_ulong(
639 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
643 int handle_notification_thread_command_add_channel(
644 struct notification_thread_state
*state
,
645 struct channel_info
*channel_info
,
646 enum lttng_error_code
*cmd_result
)
648 struct cds_list_head trigger_list
;
649 struct channel_info
*new_channel_info
;
650 struct channel_key
*channel_key
;
651 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
652 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
653 int trigger_count
= 0;
654 struct cds_lfht_iter iter
;
656 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
657 channel_info
->channel_name
, channel_info
->session_name
,
658 channel_info
->key
.key
, channel_info
->key
.domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
660 CDS_INIT_LIST_HEAD(&trigger_list
);
662 new_channel_info
= channel_info_copy(channel_info
);
663 if (!new_channel_info
) {
667 channel_key
= &new_channel_info
->key
;
669 /* Build a list of all triggers applying to the new channel. */
670 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
672 struct lttng_trigger_list_element
*new_element
;
674 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
679 new_element
= zmalloc(sizeof(*new_element
));
683 CDS_INIT_LIST_HEAD(&new_element
->node
);
684 new_element
->trigger
= trigger_ht_element
->trigger
;
685 cds_list_add(&new_element
->node
, &trigger_list
);
689 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
691 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
692 if (!channel_trigger_list
) {
695 channel_trigger_list
->channel_key
= *channel_key
;
696 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
697 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
698 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
701 /* Add channel to the channel_ht which owns the channel_infos. */
702 cds_lfht_add(state
->channels_ht
,
703 hash_channel_key(channel_key
),
704 &new_channel_info
->channels_ht_node
);
706 * Add the list of triggers associated with this channel to the
707 * channel_triggers_ht.
709 cds_lfht_add(state
->channel_triggers_ht
,
710 hash_channel_key(channel_key
),
711 &channel_trigger_list
->channel_triggers_ht_node
);
713 *cmd_result
= LTTNG_OK
;
716 /* Empty trigger list */
717 channel_info_destroy(new_channel_info
);
722 int handle_notification_thread_command_remove_channel(
723 struct notification_thread_state
*state
,
724 uint64_t channel_key
, enum lttng_domain_type domain
,
725 enum lttng_error_code
*cmd_result
)
727 struct cds_lfht_node
*node
;
728 struct cds_lfht_iter iter
;
729 struct lttng_channel_trigger_list
*trigger_list
;
730 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
731 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
732 struct channel_info
*channel_info
;
734 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
735 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
739 cds_lfht_lookup(state
->channel_triggers_ht
,
740 hash_channel_key(&key
),
741 match_channel_trigger_list
,
744 node
= cds_lfht_iter_get_node(&iter
);
746 * There is a severe internal error if we are being asked to remove a
747 * channel that doesn't exist.
750 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
754 /* Free the list of triggers associated with this channel. */
755 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
756 channel_triggers_ht_node
);
757 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
758 &trigger_list
->list
, node
) {
759 cds_list_del(&trigger_list_element
->node
);
760 free(trigger_list_element
);
762 cds_lfht_del(state
->channel_triggers_ht
, node
);
765 /* Free sampled channel state. */
766 cds_lfht_lookup(state
->channel_state_ht
,
767 hash_channel_key(&key
),
768 match_channel_state_sample
,
771 node
= cds_lfht_iter_get_node(&iter
);
773 * This is expected to be NULL if the channel is destroyed before we
777 struct channel_state_sample
*sample
= caa_container_of(node
,
778 struct channel_state_sample
,
779 channel_state_ht_node
);
781 cds_lfht_del(state
->channel_state_ht
, node
);
785 /* Remove the channel from the channels_ht and free it. */
786 cds_lfht_lookup(state
->channels_ht
,
787 hash_channel_key(&key
),
791 node
= cds_lfht_iter_get_node(&iter
);
793 channel_info
= caa_container_of(node
, struct channel_info
,
795 cds_lfht_del(state
->channels_ht
, node
);
796 channel_info_destroy(channel_info
);
799 *cmd_result
= LTTNG_OK
;
804 * FIXME A client's credentials are not checked when registering a trigger, nor
805 * are they stored alongside with the trigger.
807 * The effects of this are benign:
808 * - The client will succeed in registering the trigger, as it is valid,
809 * - The trigger will, internally, be bound to the channel,
810 * - The notifications will not be sent since the client's credentials
811 * are checked against the channel at that moment.
814 int handle_notification_thread_command_register_trigger(
815 struct notification_thread_state
*state
,
816 struct lttng_trigger
*trigger
,
817 enum lttng_error_code
*cmd_result
)
820 struct lttng_condition
*condition
;
821 struct notification_client
*client
;
822 struct notification_client_list
*client_list
= NULL
;
823 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
824 struct notification_client_list_element
*client_list_element
, *tmp
;
825 struct cds_lfht_node
*node
;
826 struct cds_lfht_iter iter
;
827 struct channel_info
*channel
;
828 bool free_trigger
= true;
832 condition
= lttng_trigger_get_condition(trigger
);
833 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
834 if (!trigger_ht_element
) {
839 /* Add trigger to the trigger_ht. */
840 cds_lfht_node_init(&trigger_ht_element
->node
);
841 trigger_ht_element
->trigger
= trigger
;
843 node
= cds_lfht_add_unique(state
->triggers_ht
,
844 lttng_condition_hash(condition
),
847 &trigger_ht_element
->node
);
848 if (node
!= &trigger_ht_element
->node
) {
849 /* Not a fatal error, simply report it to the client. */
850 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
851 goto error_free_ht_element
;
855 * Ownership of the trigger and of its wrapper was transfered to
858 trigger_ht_element
= NULL
;
859 free_trigger
= false;
862 * The rest only applies to triggers that have a "notify" action.
863 * It is not skipped as this is the only action type currently
866 client_list
= zmalloc(sizeof(*client_list
));
869 goto error_free_ht_element
;
871 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
872 CDS_INIT_LIST_HEAD(&client_list
->list
);
873 client_list
->trigger
= trigger
;
875 /* Build a list of clients to which this new trigger applies. */
876 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
877 client_socket_ht_node
) {
878 if (!trigger_applies_to_client(trigger
, client
)) {
882 client_list_element
= zmalloc(sizeof(*client_list_element
));
883 if (!client_list_element
) {
885 goto error_free_client_list
;
887 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
888 client_list_element
->client
= client
;
889 cds_list_add(&client_list_element
->node
, &client_list
->list
);
892 cds_lfht_add(state
->notification_trigger_clients_ht
,
893 lttng_condition_hash(condition
),
894 &client_list
->notification_trigger_ht_node
);
896 * Client list ownership transferred to the
897 * notification_trigger_clients_ht.
902 * Add the trigger to list of triggers bound to the channels currently
905 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
907 struct lttng_trigger_list_element
*trigger_list_element
;
908 struct lttng_channel_trigger_list
*trigger_list
;
910 if (!trigger_applies_to_channel(trigger
, channel
)) {
914 cds_lfht_lookup(state
->channel_triggers_ht
,
915 hash_channel_key(&channel
->key
),
916 match_channel_trigger_list
,
919 node
= cds_lfht_iter_get_node(&iter
);
921 /* Free the list of triggers associated with this channel. */
922 trigger_list
= caa_container_of(node
,
923 struct lttng_channel_trigger_list
,
924 channel_triggers_ht_node
);
926 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
927 if (!trigger_list_element
) {
929 goto error_free_client_list
;
931 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
932 trigger_list_element
->trigger
= trigger
;
933 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
934 /* A trigger can only apply to one channel. */
938 *cmd_result
= LTTNG_OK
;
939 error_free_client_list
:
941 cds_list_for_each_entry_safe(client_list_element
, tmp
,
942 &client_list
->list
, node
) {
943 free(client_list_element
);
947 error_free_ht_element
:
948 free(trigger_ht_element
);
951 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
953 lttng_condition_destroy(condition
);
954 lttng_action_destroy(action
);
955 lttng_trigger_destroy(trigger
);
961 int handle_notification_thread_command_unregister_trigger(
962 struct notification_thread_state
*state
,
963 struct lttng_trigger
*trigger
,
964 enum lttng_error_code
*_cmd_reply
)
966 struct cds_lfht_iter iter
;
967 struct cds_lfht_node
*node
, *triggers_ht_node
;
968 struct lttng_channel_trigger_list
*trigger_list
;
969 struct notification_client_list
*client_list
;
970 struct notification_client_list_element
*client_list_element
, *tmp
;
971 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
972 struct lttng_condition
*condition
= lttng_trigger_get_condition(
974 struct lttng_action
*action
;
975 enum lttng_error_code cmd_reply
;
979 cds_lfht_lookup(state
->triggers_ht
,
980 lttng_condition_hash(condition
),
984 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
985 if (!triggers_ht_node
) {
986 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
989 cmd_reply
= LTTNG_OK
;
992 /* Remove trigger from channel_triggers_ht. */
993 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
994 channel_triggers_ht_node
) {
995 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
997 cds_list_for_each_entry_safe(trigger_element
, tmp
,
998 &trigger_list
->list
, node
) {
999 struct lttng_condition
*current_condition
=
1000 lttng_trigger_get_condition(
1001 trigger_element
->trigger
);
1003 assert(current_condition
);
1004 if (!lttng_condition_is_equal(condition
,
1005 current_condition
)) {
1009 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1010 cds_list_del(&trigger_element
->node
);
1015 * Remove and release the client list from
1016 * notification_trigger_clients_ht.
1018 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1019 lttng_condition_hash(condition
),
1023 node
= cds_lfht_iter_get_node(&iter
);
1025 client_list
= caa_container_of(node
, struct notification_client_list
,
1026 notification_trigger_ht_node
);
1027 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1028 &client_list
->list
, node
) {
1029 free(client_list_element
);
1031 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1034 /* Remove trigger from triggers_ht. */
1035 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1036 struct lttng_trigger_ht_element
, node
);
1037 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1039 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1040 lttng_condition_destroy(condition
);
1041 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1042 lttng_action_destroy(action
);
1043 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1044 free(trigger_ht_element
);
1048 *_cmd_reply
= cmd_reply
;
1053 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1054 int handle_notification_thread_command(
1055 struct notification_thread_handle
*handle
,
1056 struct notification_thread_state
*state
)
1060 struct notification_thread_command
*cmd
;
1062 /* Read event_fd to put it back into a quiescent state. */
1063 ret
= read(handle
->cmd_queue
.event_fd
, &counter
, sizeof(counter
));
1068 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1069 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1070 struct notification_thread_command
, cmd_list_node
);
1071 switch (cmd
->type
) {
1072 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1073 DBG("[notification-thread] Received register trigger command");
1074 ret
= handle_notification_thread_command_register_trigger(
1075 state
, cmd
->parameters
.trigger
,
1078 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1079 DBG("[notification-thread] Received unregister trigger command");
1080 ret
= handle_notification_thread_command_unregister_trigger(
1081 state
, cmd
->parameters
.trigger
,
1084 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1085 DBG("[notification-thread] Received add channel command");
1086 ret
= handle_notification_thread_command_add_channel(
1087 state
, &cmd
->parameters
.add_channel
,
1090 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1091 DBG("[notification-thread] Received remove channel command");
1092 ret
= handle_notification_thread_command_remove_channel(
1093 state
, cmd
->parameters
.remove_channel
.key
,
1094 cmd
->parameters
.remove_channel
.domain
,
1097 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1098 DBG("[notification-thread] Received quit command");
1099 cmd
->reply_code
= LTTNG_OK
;
1103 ERR("[notification-thread] Unknown internal command received");
1111 cds_list_del(&cmd
->cmd_list_node
);
1112 futex_nto1_wake(&cmd
->reply_futex
);
1113 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1116 /* Wake-up and return a fatal error to the calling thread. */
1117 futex_nto1_wake(&cmd
->reply_futex
);
1118 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1119 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1121 /* Indicate a fatal error to the caller. */
1126 unsigned long hash_client_socket(int socket
)
1128 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1132 int socket_set_non_blocking(int socket
)
1136 /* Set the pipe as non-blocking. */
1137 ret
= fcntl(socket
, F_GETFL
, 0);
1139 PERROR("fcntl get socket flags");
1144 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1146 PERROR("fcntl set O_NONBLOCK socket flag");
1149 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1155 void client_reset_inbound_state(struct notification_client
*client
)
1159 ret
= lttng_dynamic_buffer_set_size(
1160 &client
->communication
.inbound
.buffer
, 0);
1163 client
->communication
.inbound
.bytes_to_receive
=
1164 sizeof(struct lttng_notification_channel_message
);
1165 client
->communication
.inbound
.msg_type
=
1166 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1167 client
->communication
.inbound
.receive_creds
= false;
1168 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1169 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1172 int handle_notification_thread_client_connect(
1173 struct notification_thread_state
*state
)
1176 struct notification_client
*client
;
1178 DBG("[notification-thread] Handling new notification channel client connection");
1180 client
= zmalloc(sizeof(*client
));
1186 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1187 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1188 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1189 client_reset_inbound_state(client
);
1191 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1193 ERR("[notification-thread] Failed to accept new notification channel client connection");
1198 client
->socket
= ret
;
1200 ret
= socket_set_non_blocking(client
->socket
);
1202 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1206 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1208 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1213 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1214 LPOLLIN
| LPOLLERR
|
1215 LPOLLHUP
| LPOLLRDHUP
);
1217 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1221 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1226 cds_lfht_add(state
->client_socket_ht
,
1227 hash_client_socket(client
->socket
),
1228 &client
->client_socket_ht_node
);
1233 notification_client_destroy(client
, state
);
1237 int handle_notification_thread_client_disconnect(
1239 struct notification_thread_state
*state
)
1242 struct notification_client
*client
;
1245 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1247 client
= get_client_from_socket(client_socket
, state
);
1249 /* Internal state corruption, fatal error. */
1250 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1256 ret
= lttng_poll_del(&state
->events
, client_socket
);
1258 ERR("[notification-thread] Failed to remove client socket from poll set");
1260 cds_lfht_del(state
->client_socket_ht
,
1261 &client
->client_socket_ht_node
);
1262 notification_client_destroy(client
, state
);
1268 int handle_notification_thread_client_disconnect_all(
1269 struct notification_thread_state
*state
)
1271 struct cds_lfht_iter iter
;
1272 struct notification_client
*client
;
1273 bool error_encoutered
= false;
1276 DBG("[notification-thread] Closing all client connections");
1277 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1278 client_socket_ht_node
) {
1281 ret
= handle_notification_thread_client_disconnect(
1282 client
->socket
, state
);
1284 error_encoutered
= true;
1288 return error_encoutered
? 1 : 0;
1291 int handle_notification_thread_trigger_unregister_all(
1292 struct notification_thread_state
*state
)
1294 bool error_occured
= false;
1295 struct cds_lfht_iter iter
;
1296 struct lttng_trigger_ht_element
*trigger_ht_element
;
1298 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1300 int ret
= handle_notification_thread_command_unregister_trigger(
1301 state
, trigger_ht_element
->trigger
, NULL
);
1303 error_occured
= true;
1306 return error_occured
? -1 : 0;
1310 int client_flush_outgoing_queue(struct notification_client
*client
,
1311 struct notification_thread_state
*state
)
1314 size_t to_send_count
;
1316 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1317 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1318 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1321 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1322 client
->communication
.outbound
.buffer
.data
,
1324 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1325 (ret
> 0 && ret
< to_send_count
)) {
1326 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1328 to_send_count
-= max(ret
, 0);
1330 memcpy(client
->communication
.outbound
.buffer
.data
,
1331 client
->communication
.outbound
.buffer
.data
+
1332 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1334 ret
= lttng_dynamic_buffer_set_size(
1335 &client
->communication
.outbound
.buffer
,
1342 * We want to be notified whenever there is buffer space
1343 * available to send the rest of the payload.
1345 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1346 CLIENT_POLL_MASK_IN_OUT
);
1350 } else if (ret
< 0) {
1351 /* Generic error, disconnect the client. */
1352 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1354 ret
= handle_notification_thread_client_disconnect(
1355 client
->socket
, state
);
1360 /* No error and flushed the queue completely. */
1361 ret
= lttng_dynamic_buffer_set_size(
1362 &client
->communication
.outbound
.buffer
, 0);
1366 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1367 CLIENT_POLL_MASK_IN
);
1372 client
->communication
.outbound
.queued_command_reply
= false;
1373 client
->communication
.outbound
.dropped_notification
= false;
1382 int client_send_command_reply(struct notification_client
*client
,
1383 struct notification_thread_state
*state
,
1384 enum lttng_notification_channel_status status
)
1387 struct lttng_notification_channel_command_reply reply
= {
1388 .status
= (int8_t) status
,
1390 struct lttng_notification_channel_message msg
= {
1391 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1392 .size
= sizeof(reply
),
1394 char buffer
[sizeof(msg
) + sizeof(reply
)];
1396 if (client
->communication
.outbound
.queued_command_reply
) {
1397 /* Protocol error. */
1401 memcpy(buffer
, &msg
, sizeof(msg
));
1402 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1403 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1405 /* Enqueue buffer to outgoing queue and flush it. */
1406 ret
= lttng_dynamic_buffer_append(
1407 &client
->communication
.outbound
.buffer
,
1408 buffer
, sizeof(buffer
));
1413 ret
= client_flush_outgoing_queue(client
, state
);
1418 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1419 /* Queue could not be emptied. */
1420 client
->communication
.outbound
.queued_command_reply
= true;
1429 int client_dispatch_message(struct notification_client
*client
,
1430 struct notification_thread_state
*state
)
1434 if (client
->communication
.inbound
.msg_type
!=
1435 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1436 client
->communication
.inbound
.msg_type
!=
1437 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1438 !client
->validated
) {
1439 WARN("[notification-thread] client attempted a command before handshake");
1444 switch (client
->communication
.inbound
.msg_type
) {
1445 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1448 * Receiving message header. The function will be called again
1449 * once the rest of the message as been received and can be
1452 const struct lttng_notification_channel_message
*msg
;
1454 assert(sizeof(*msg
) ==
1455 client
->communication
.inbound
.buffer
.size
);
1456 msg
= (const struct lttng_notification_channel_message
*)
1457 client
->communication
.inbound
.buffer
.data
;
1459 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1460 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1465 switch (msg
->type
) {
1466 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1467 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1468 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1472 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1476 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
1477 client
->communication
.inbound
.msg_type
=
1478 (enum lttng_notification_channel_message_type
) msg
->type
;
1479 if (client
->communication
.inbound
.msg_type
==
1480 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
) {
1481 client
->communication
.inbound
.receive_creds
= true;
1483 ret
= lttng_dynamic_buffer_set_size(
1484 &client
->communication
.inbound
.buffer
, 0);
1490 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1492 struct lttng_notification_channel_command_handshake
*handshake_client
;
1493 struct lttng_notification_channel_command_handshake handshake_reply
= {
1494 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
1495 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
1497 struct lttng_notification_channel_message msg_header
= {
1498 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
1499 .size
= sizeof(handshake_reply
),
1501 enum lttng_notification_channel_status status
=
1502 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1503 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
1505 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
1506 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
1507 sizeof(handshake_reply
));
1510 (struct lttng_notification_channel_command_handshake
*)
1511 client
->communication
.inbound
.buffer
.data
;
1512 client
->major
= handshake_client
->major
;
1513 client
->minor
= handshake_client
->minor
;
1514 if (!client
->communication
.inbound
.creds_received
) {
1515 ERR("[notification-thread] No credentials received from client");
1520 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
1521 &client
->communication
.inbound
.creds
);
1522 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
1523 &client
->communication
.inbound
.creds
);
1524 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
1525 client
->uid
, client
->gid
, (int) client
->major
,
1526 (int) client
->minor
);
1528 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
1529 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
1532 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
1533 send_buffer
, sizeof(send_buffer
));
1535 ERR("[notification-thread] Failed to send protocol version to notification channel client");
1539 ret
= client_flush_outgoing_queue(client
, state
);
1544 ret
= client_send_command_reply(client
, state
, status
);
1546 ERR("[notification-thread] Failed to send reply to notification channel client");
1550 /* Set reception state to receive the next message header. */
1551 client_reset_inbound_state(client
);
1552 client
->validated
= true;
1555 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1556 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1558 struct lttng_condition
*condition
;
1559 enum lttng_notification_channel_status status
=
1560 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1561 const struct lttng_buffer_view condition_view
=
1562 lttng_buffer_view_from_dynamic_buffer(
1563 &client
->communication
.inbound
.buffer
,
1565 size_t expected_condition_size
=
1566 client
->communication
.inbound
.buffer
.size
;
1568 ret
= lttng_condition_create_from_buffer(&condition_view
,
1570 if (ret
!= expected_condition_size
) {
1571 ERR("[notification-thread] Malformed condition received from client");
1575 if (client
->communication
.inbound
.msg_type
==
1576 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
1578 * FIXME The current state should be evaluated on
1581 ret
= notification_thread_client_subscribe(client
,
1582 condition
, state
, &status
);
1584 ret
= notification_thread_client_unsubscribe(client
,
1585 condition
, state
, &status
);
1591 ret
= client_send_command_reply(client
, state
, status
);
1593 ERR("[notification-thread] Failed to send reply to notification channel client");
1597 /* Set reception state to receive the next message header. */
1598 client_reset_inbound_state(client
);
1608 /* Incoming data from client. */
1609 int handle_notification_thread_client_in(
1610 struct notification_thread_state
*state
, int socket
)
1613 struct notification_client
*client
;
1617 client
= get_client_from_socket(socket
, state
);
1619 /* Internal error, abort. */
1624 offset
= client
->communication
.inbound
.buffer
.size
;
1625 ret
= lttng_dynamic_buffer_set_size(
1626 &client
->communication
.inbound
.buffer
,
1627 client
->communication
.inbound
.bytes_to_receive
);
1632 if (client
->communication
.inbound
.receive_creds
) {
1633 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
1634 client
->communication
.inbound
.buffer
.data
+ offset
,
1635 client
->communication
.inbound
.bytes_to_receive
,
1636 &client
->communication
.inbound
.creds
);
1638 client
->communication
.inbound
.receive_creds
= false;
1639 client
->communication
.inbound
.creds_received
= true;
1642 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
1643 client
->communication
.inbound
.buffer
.data
+ offset
,
1644 client
->communication
.inbound
.bytes_to_receive
);
1647 goto error_disconnect_client
;
1650 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
1651 ret
= lttng_dynamic_buffer_set_size(
1652 &client
->communication
.inbound
.buffer
,
1653 client
->communication
.inbound
.buffer
.size
-
1654 client
->communication
.inbound
.bytes_to_receive
);
1659 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
1660 ret
= client_dispatch_message(client
, state
);
1663 * Only returns an error if this client must be
1666 goto error_disconnect_client
;
1673 error_disconnect_client
:
1674 ret
= handle_notification_thread_client_disconnect(socket
, state
);
1678 /* Client ready to receive outgoing data. */
1679 int handle_notification_thread_client_out(
1680 struct notification_thread_state
*state
, int socket
)
1683 struct notification_client
*client
;
1685 client
= get_client_from_socket(socket
, state
);
1687 /* Internal error, abort. */
1692 ret
= client_flush_outgoing_queue(client
, state
);
1701 bool evaluate_buffer_usage_condition(struct lttng_condition
*condition
,
1702 struct channel_state_sample
*sample
, uint64_t buffer_capacity
)
1704 bool result
= false;
1706 enum lttng_condition_type condition_type
;
1707 struct lttng_condition_buffer_usage
*use_condition
= container_of(
1708 condition
, struct lttng_condition_buffer_usage
,
1715 if (use_condition
->threshold_bytes
.set
) {
1716 threshold
= use_condition
->threshold_bytes
.value
;
1719 * Threshold was expressed as a ratio.
1721 * TODO the threshold (in bytes) of conditions expressed
1722 * as a ratio of total buffer size could be cached to
1723 * forego this double-multiplication or it could be performed
1724 * as fixed-point math.
1726 * Note that caching should accomodate the case where the
1727 * condition applies to multiple channels (i.e. don't assume
1728 * that all channels matching my_chann* have the same size...)
1730 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
1731 (double) buffer_capacity
);
1734 condition_type
= lttng_condition_get_type(condition
);
1735 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
1736 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1737 threshold
, sample
->highest_usage
);
1740 * The low condition should only be triggered once _all_ of the
1741 * streams in a channel have gone below the "low" threshold.
1743 if (sample
->highest_usage
<= threshold
) {
1747 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1748 threshold
, sample
->highest_usage
);
1751 * For high buffer usage scenarios, we want to trigger whenever
1752 * _any_ of the streams has reached the "high" threshold.
1754 if (sample
->highest_usage
>= threshold
) {
1763 int evaluate_condition(struct lttng_condition
*condition
,
1764 struct lttng_evaluation
**evaluation
,
1765 struct notification_thread_state
*state
,
1766 struct channel_state_sample
*previous_sample
,
1767 struct channel_state_sample
*latest_sample
,
1768 uint64_t buffer_capacity
)
1771 enum lttng_condition_type condition_type
;
1772 bool previous_sample_result
;
1773 bool latest_sample_result
;
1775 condition_type
= lttng_condition_get_type(condition
);
1776 /* No other condition type supported for the moment. */
1777 assert(condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
1778 condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
1780 previous_sample_result
= evaluate_buffer_usage_condition(condition
,
1781 previous_sample
, buffer_capacity
);
1782 latest_sample_result
= evaluate_buffer_usage_condition(condition
,
1783 latest_sample
, buffer_capacity
);
1785 if (!latest_sample_result
||
1786 (previous_sample_result
== latest_sample_result
)) {
1788 * Only trigger on a condition evaluation transition.
1790 * NOTE: This edge-triggered logic may not be appropriate for
1791 * future condition types.
1796 if (evaluation
&& latest_sample_result
) {
1797 *evaluation
= lttng_evaluation_buffer_usage_create(
1799 latest_sample
->highest_usage
,
1811 int client_enqueue_dropped_notification(struct notification_client
*client
,
1812 struct notification_thread_state
*state
)
1815 struct lttng_notification_channel_message msg
= {
1816 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
1820 ret
= lttng_dynamic_buffer_append(
1821 &client
->communication
.outbound
.buffer
, &msg
,
1827 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
1828 struct lttng_evaluation
*evaluation
,
1829 struct notification_client_list
* client_list
,
1830 struct notification_thread_state
*state
,
1831 uid_t channel_uid
, gid_t channel_gid
)
1834 struct lttng_dynamic_buffer msg_buffer
;
1835 struct notification_client_list_element
*client_list_element
, *tmp
;
1836 struct lttng_notification
*notification
;
1837 struct lttng_condition
*condition
;
1838 ssize_t expected_notification_size
, notification_size
;
1839 struct lttng_notification_channel_message msg
;
1841 lttng_dynamic_buffer_init(&msg_buffer
);
1843 condition
= lttng_trigger_get_condition(trigger
);
1846 notification
= lttng_notification_create(condition
, evaluation
);
1847 if (!notification
) {
1852 expected_notification_size
= lttng_notification_serialize(notification
,
1854 if (expected_notification_size
< 0) {
1855 ERR("[notification-thread] Failed to get size of serialized notification");
1860 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
1861 msg
.size
= (uint32_t) expected_notification_size
;
1862 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg
, sizeof(msg
));
1867 ret
= lttng_dynamic_buffer_set_size(&msg_buffer
,
1868 msg_buffer
.size
+ expected_notification_size
);
1873 notification_size
= lttng_notification_serialize(notification
,
1874 msg_buffer
.data
+ sizeof(msg
));
1875 if (notification_size
!= expected_notification_size
) {
1876 ERR("[notification-thread] Failed to serialize notification");
1881 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1882 &client_list
->list
, node
) {
1883 struct notification_client
*client
=
1884 client_list_element
->client
;
1886 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
1888 /* Client is not allowed to monitor this channel. */
1889 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
1893 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
1894 client
->socket
, msg_buffer
.size
);
1895 if (client
->communication
.outbound
.buffer
.size
) {
1897 * Outgoing data is already buffered for this client;
1898 * drop the notification and enqueue a "dropped
1899 * notification" message if this is the first dropped
1900 * notification since the socket spilled-over to the
1903 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
1905 if (!client
->communication
.outbound
.dropped_notification
) {
1906 client
->communication
.outbound
.dropped_notification
= true;
1907 ret
= client_enqueue_dropped_notification(
1916 ret
= lttng_dynamic_buffer_append_buffer(
1917 &client
->communication
.outbound
.buffer
,
1923 ret
= client_flush_outgoing_queue(client
, state
);
1930 lttng_notification_destroy(notification
);
1931 lttng_dynamic_buffer_reset(&msg_buffer
);
1935 int handle_notification_thread_channel_sample(
1936 struct notification_thread_state
*state
, int pipe
,
1937 enum lttng_domain_type domain
)
1940 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
1941 struct channel_state_sample previous_sample
, latest_sample
;
1942 struct channel_info
*channel_info
;
1943 struct cds_lfht_node
*node
;
1944 struct cds_lfht_iter iter
;
1945 struct lttng_channel_trigger_list
*trigger_list
;
1946 struct lttng_trigger_list_element
*trigger_list_element
;
1947 bool previous_sample_available
= false;
1950 * The monitoring pipe only holds messages smaller than PIPE_BUF,
1951 * ensuring that read/write of sampling messages are atomic.
1953 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
1954 if (ret
!= sizeof(sample_msg
)) {
1955 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
1962 latest_sample
.key
.key
= sample_msg
.key
;
1963 latest_sample
.key
.domain
= domain
;
1964 latest_sample
.highest_usage
= sample_msg
.highest
;
1965 latest_sample
.lowest_usage
= sample_msg
.lowest
;
1969 /* Retrieve the channel's informations */
1970 cds_lfht_lookup(state
->channels_ht
,
1971 hash_channel_key(&latest_sample
.key
),
1975 node
= cds_lfht_iter_get_node(&iter
);
1978 * Not an error since the consumer can push a sample to the pipe
1979 * and the rest of the session daemon could notify us of the
1980 * channel's destruction before we get a chance to process that
1983 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
1984 latest_sample
.key
.key
,
1985 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
1989 channel_info
= caa_container_of(node
, struct channel_info
,
1991 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
")",
1992 channel_info
->channel_name
,
1993 latest_sample
.key
.key
,
1994 channel_info
->session_name
,
1995 latest_sample
.highest_usage
,
1996 latest_sample
.lowest_usage
);
1998 /* Retrieve the channel's last sample, if it exists, and update it. */
1999 cds_lfht_lookup(state
->channel_state_ht
,
2000 hash_channel_key(&latest_sample
.key
),
2001 match_channel_state_sample
,
2004 node
= cds_lfht_iter_get_node(&iter
);
2006 struct channel_state_sample
*stored_sample
;
2008 /* Update the sample stored. */
2009 stored_sample
= caa_container_of(node
,
2010 struct channel_state_sample
,
2011 channel_state_ht_node
);
2012 memcpy(&previous_sample
, stored_sample
,
2013 sizeof(previous_sample
));
2014 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2015 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2016 previous_sample_available
= true;
2019 * This is the channel's first sample, allocate space for and
2020 * store the new sample.
2022 struct channel_state_sample
*stored_sample
;
2024 stored_sample
= zmalloc(sizeof(*stored_sample
));
2025 if (!stored_sample
) {
2030 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2031 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2032 cds_lfht_add(state
->channel_state_ht
,
2033 hash_channel_key(&stored_sample
->key
),
2034 &stored_sample
->channel_state_ht_node
);
2037 /* Find triggers associated with this channel. */
2038 cds_lfht_lookup(state
->channel_triggers_ht
,
2039 hash_channel_key(&latest_sample
.key
),
2040 match_channel_trigger_list
,
2043 node
= cds_lfht_iter_get_node(&iter
);
2048 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2049 channel_triggers_ht_node
);
2050 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2052 struct lttng_condition
*condition
;
2053 struct lttng_action
*action
;
2054 struct lttng_trigger
*trigger
;
2055 struct notification_client_list
*client_list
;
2056 struct lttng_evaluation
*evaluation
= NULL
;
2058 trigger
= trigger_list_element
->trigger
;
2059 condition
= lttng_trigger_get_condition(trigger
);
2061 action
= lttng_trigger_get_action(trigger
);
2063 /* Notify actions are the only type currently supported. */
2064 assert(lttng_action_get_type(action
) ==
2065 LTTNG_ACTION_TYPE_NOTIFY
);
2068 * Check if any client is subscribed to the result of this
2071 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2072 lttng_condition_hash(condition
),
2076 node
= cds_lfht_iter_get_node(&iter
);
2079 client_list
= caa_container_of(node
,
2080 struct notification_client_list
,
2081 notification_trigger_ht_node
);
2082 if (cds_list_empty(&client_list
->list
)) {
2084 * No clients interested in the evaluation's result,
2090 ret
= evaluate_condition(condition
, &evaluation
, state
,
2091 previous_sample_available
? &previous_sample
: NULL
,
2092 &latest_sample
, channel_info
->capacity
);
2101 /* Dispatch evaluation result to all clients. */
2102 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2103 evaluation
, client_list
, state
,
2104 channel_info
->uid
, channel_info
->gid
);