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>
44 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
45 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
47 struct lttng_trigger_list_element
{
48 struct lttng_trigger
*trigger
;
49 struct cds_list_head node
;
52 struct lttng_channel_trigger_list
{
53 struct channel_key channel_key
;
54 struct cds_list_head list
;
55 struct cds_lfht_node channel_triggers_ht_node
;
58 struct lttng_trigger_ht_element
{
59 struct lttng_trigger
*trigger
;
60 struct cds_lfht_node node
;
63 struct lttng_condition_list_element
{
64 struct lttng_condition
*condition
;
65 struct cds_list_head node
;
68 struct notification_client_list_element
{
69 struct notification_client
*client
;
70 struct cds_list_head node
;
73 struct notification_client_list
{
74 struct lttng_trigger
*trigger
;
75 struct cds_list_head list
;
76 struct cds_lfht_node notification_trigger_ht_node
;
79 struct notification_client
{
81 /* Client protocol version. */
86 * Indicates if the credentials and versions of the client has been
91 * Conditions to which the client's notification channel is subscribed.
92 * List of struct lttng_condition_list_node. The condition member is
93 * owned by the client.
95 struct cds_list_head condition_list
;
96 struct cds_lfht_node client_socket_ht_node
;
99 struct lttng_dynamic_buffer buffer
;
100 /* Bytes left to receive for the current message. */
101 size_t bytes_to_receive
;
102 /* Type of the message being received. */
103 enum lttng_notification_channel_message_type msg_type
;
105 * Indicates whether or not credentials are expected
110 * Indicates whether or not credentials were received
114 lttng_sock_cred creds
;
118 * Indicates whether or not a notification addressed to
119 * this client was dropped because a command reply was
122 * A notification is dropped whenever the buffer is not
125 bool dropped_notification
;
127 * Indicates whether or not a command reply is already
128 * buffered. In this case, it means that the client is
129 * not consuming command replies before emitting a new
130 * one. This could be caused by a protocol error or a
131 * misbehaving/malicious client.
133 bool queued_command_reply
;
134 struct lttng_dynamic_buffer buffer
;
139 struct channel_state_sample
{
140 struct channel_key key
;
141 struct cds_lfht_node channel_state_ht_node
;
142 uint64_t highest_usage
;
143 uint64_t lowest_usage
;
147 int match_client(struct cds_lfht_node
*node
, const void *key
)
149 /* This double-cast is intended to supress pointer-to-cast warning. */
150 int socket
= (int) (intptr_t) key
;
151 struct notification_client
*client
;
153 client
= caa_container_of(node
, struct notification_client
,
154 client_socket_ht_node
);
156 return !!(client
->socket
== socket
);
160 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
162 struct channel_key
*channel_key
= (struct channel_key
*) key
;
163 struct lttng_channel_trigger_list
*trigger_list
;
165 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
166 channel_triggers_ht_node
);
168 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
169 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
173 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
175 struct channel_key
*channel_key
= (struct channel_key
*) key
;
176 struct channel_state_sample
*sample
;
178 sample
= caa_container_of(node
, struct channel_state_sample
,
179 channel_state_ht_node
);
181 return !!((channel_key
->key
== sample
->key
.key
) &&
182 (channel_key
->domain
== sample
->key
.domain
));
186 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
188 struct channel_key
*channel_key
= (struct channel_key
*) key
;
189 struct channel_info
*channel_info
;
191 channel_info
= caa_container_of(node
, struct channel_info
,
194 return !!((channel_key
->key
== channel_info
->key
.key
) &&
195 (channel_key
->domain
== channel_info
->key
.domain
));
199 int match_condition(struct cds_lfht_node
*node
, const void *key
)
201 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
202 struct lttng_trigger_ht_element
*trigger
;
203 struct lttng_condition
*condition
;
205 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
207 condition
= lttng_trigger_get_condition(trigger
->trigger
);
210 return !!lttng_condition_is_equal(condition_key
, condition
);
214 int match_client_list(struct cds_lfht_node
*node
, const void *key
)
216 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
217 struct notification_client_list
*client_list
;
218 struct lttng_condition
*condition
;
219 struct lttng_condition
*condition_key
= lttng_trigger_get_condition(
222 assert(condition_key
);
224 client_list
= caa_container_of(node
, struct notification_client_list
,
225 notification_trigger_ht_node
);
226 condition
= lttng_trigger_get_condition(client_list
->trigger
);
228 return !!lttng_condition_is_equal(condition_key
, condition
);
232 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
234 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
235 struct notification_client_list
*client_list
;
236 struct lttng_condition
*condition
;
238 assert(condition_key
);
240 client_list
= caa_container_of(node
, struct notification_client_list
,
241 notification_trigger_ht_node
);
242 condition
= lttng_trigger_get_condition(client_list
->trigger
);
244 return !!lttng_condition_is_equal(condition_key
, condition
);
248 unsigned long lttng_condition_buffer_usage_hash(
249 struct lttng_condition
*_condition
)
251 unsigned long hash
= 0;
252 struct lttng_condition_buffer_usage
*condition
;
254 condition
= container_of(_condition
,
255 struct lttng_condition_buffer_usage
, parent
);
257 if (condition
->session_name
) {
258 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
260 if (condition
->channel_name
) {
261 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
263 if (condition
->domain
.set
) {
264 hash
^= hash_key_ulong(
265 (void *) condition
->domain
.type
,
268 if (condition
->threshold_ratio
.set
) {
271 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
272 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
273 } else if (condition
->threshold_ratio
.set
) {
276 val
= condition
->threshold_bytes
.value
;
277 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
283 * The lttng_condition hashing code is kept in this file (rather than
284 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
285 * don't want to link in liblttng-ctl.
288 unsigned long lttng_condition_hash(struct lttng_condition
*condition
)
290 switch (condition
->type
) {
291 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
292 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
293 return lttng_condition_buffer_usage_hash(condition
);
295 ERR("[notification-thread] Unexpected condition type caught");
301 void channel_info_destroy(struct channel_info
*channel_info
)
307 if (channel_info
->session_name
) {
308 free(channel_info
->session_name
);
310 if (channel_info
->channel_name
) {
311 free(channel_info
->channel_name
);
317 struct channel_info
*channel_info_copy(struct channel_info
*channel_info
)
319 struct channel_info
*copy
= zmalloc(sizeof(*channel_info
));
321 assert(channel_info
);
322 assert(channel_info
->session_name
);
323 assert(channel_info
->channel_name
);
329 memcpy(copy
, channel_info
, sizeof(*channel_info
));
330 copy
->session_name
= NULL
;
331 copy
->channel_name
= NULL
;
333 copy
->session_name
= strdup(channel_info
->session_name
);
334 if (!copy
->session_name
) {
337 copy
->channel_name
= strdup(channel_info
->channel_name
);
338 if (!copy
->channel_name
) {
341 cds_lfht_node_init(&channel_info
->channels_ht_node
);
345 channel_info_destroy(copy
);
350 int notification_thread_client_subscribe(struct notification_client
*client
,
351 struct lttng_condition
*condition
,
352 struct notification_thread_state
*state
,
353 enum lttng_notification_channel_status
*_status
)
356 struct cds_lfht_iter iter
;
357 struct cds_lfht_node
*node
;
358 struct notification_client_list
*client_list
;
359 struct lttng_condition_list_element
*condition_list_element
= NULL
;
360 struct notification_client_list_element
*client_list_element
= NULL
;
361 enum lttng_notification_channel_status status
=
362 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
365 * Ensure that the client has not already subscribed to this condition
368 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
369 if (lttng_condition_is_equal(condition_list_element
->condition
,
371 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
376 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
377 if (!condition_list_element
) {
381 client_list_element
= zmalloc(sizeof(*client_list_element
));
382 if (!client_list_element
) {
390 * Add the newly-subscribed condition to the client's subscription list.
392 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
393 condition_list_element
->condition
= condition
;
394 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
397 * Add the client to the list of clients interested in a given trigger
398 * if a "notification" trigger with a corresponding condition was
401 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
402 lttng_condition_hash(condition
),
403 match_client_list_condition
,
406 node
= cds_lfht_iter_get_node(&iter
);
408 free(client_list_element
);
412 client_list
= caa_container_of(node
, struct notification_client_list
,
413 notification_trigger_ht_node
);
414 client_list_element
->client
= client
;
415 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
416 cds_list_add(&client_list_element
->node
, &client_list
->list
);
425 free(condition_list_element
);
426 free(client_list_element
);
431 int notification_thread_client_unsubscribe(
432 struct notification_client
*client
,
433 struct lttng_condition
*condition
,
434 struct notification_thread_state
*state
,
435 enum lttng_notification_channel_status
*_status
)
437 struct cds_lfht_iter iter
;
438 struct cds_lfht_node
*node
;
439 struct notification_client_list
*client_list
;
440 struct lttng_condition_list_element
*condition_list_element
,
442 struct notification_client_list_element
*client_list_element
,
444 bool condition_found
= false;
445 enum lttng_notification_channel_status status
=
446 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
448 /* Remove the condition from the client's condition list. */
449 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
450 &client
->condition_list
, node
) {
451 if (!lttng_condition_is_equal(condition_list_element
->condition
,
456 cds_list_del(&condition_list_element
->node
);
458 * The caller may be iterating on the client's conditions to
459 * tear down a client's connection. In this case, the condition
460 * will be destroyed at the end.
462 if (condition
!= condition_list_element
->condition
) {
463 lttng_condition_destroy(
464 condition_list_element
->condition
);
466 free(condition_list_element
);
467 condition_found
= true;
471 if (!condition_found
) {
472 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
477 * Remove the client from the list of clients interested the trigger
478 * matching the condition.
481 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
482 lttng_condition_hash(condition
),
483 match_client_list_condition
,
486 node
= cds_lfht_iter_get_node(&iter
);
491 client_list
= caa_container_of(node
, struct notification_client_list
,
492 notification_trigger_ht_node
);
493 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
494 &client_list
->list
, node
) {
495 if (client_list_element
->client
->socket
!= client
->socket
) {
498 cds_list_del(&client_list_element
->node
);
499 free(client_list_element
);
505 lttng_condition_destroy(condition
);
513 void notification_client_destroy(struct notification_client
*client
,
514 struct notification_thread_state
*state
)
516 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
522 /* Release all conditions to which the client was subscribed. */
523 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
524 &client
->condition_list
, node
) {
525 (void) notification_thread_client_unsubscribe(client
,
526 condition_list_element
->condition
, state
, NULL
);
529 if (client
->socket
>= 0) {
530 (void) lttcomm_close_unix_sock(client
->socket
);
532 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
533 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
538 * Call with rcu_read_lock held (and hold for the lifetime of the returned
542 struct notification_client
*get_client_from_socket(int socket
,
543 struct notification_thread_state
*state
)
545 struct cds_lfht_iter iter
;
546 struct cds_lfht_node
*node
;
547 struct notification_client
*client
= NULL
;
549 cds_lfht_lookup(state
->client_socket_ht
,
550 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
552 (void *) (unsigned long) socket
,
554 node
= cds_lfht_iter_get_node(&iter
);
559 client
= caa_container_of(node
, struct notification_client
,
560 client_socket_ht_node
);
566 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
567 struct channel_info
*info
)
569 enum lttng_condition_status status
;
570 struct lttng_condition
*condition
;
571 const char *trigger_session_name
= NULL
;
572 const char *trigger_channel_name
= NULL
;
573 enum lttng_domain_type trigger_domain
;
575 condition
= lttng_trigger_get_condition(trigger
);
580 switch (lttng_condition_get_type(condition
)) {
581 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
582 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
588 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
590 assert(status
== LTTNG_CONDITION_STATUS_OK
);
591 if (info
->key
.domain
!= trigger_domain
) {
595 status
= lttng_condition_buffer_usage_get_session_name(
596 condition
, &trigger_session_name
);
597 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_session_name
);
599 status
= lttng_condition_buffer_usage_get_channel_name(
600 condition
, &trigger_channel_name
);
601 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_channel_name
);
603 if (strcmp(info
->session_name
, trigger_session_name
)) {
606 if (strcmp(info
->channel_name
, trigger_channel_name
)) {
616 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
617 struct notification_client
*client
)
619 bool applies
= false;
620 struct lttng_condition_list_element
*condition_list_element
;
622 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
624 applies
= lttng_condition_is_equal(
625 condition_list_element
->condition
,
626 lttng_trigger_get_condition(trigger
));
635 unsigned long hash_channel_key(struct channel_key
*key
)
637 return hash_key_u64(&key
->key
, lttng_ht_seed
) ^ hash_key_ulong(
638 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
642 int handle_notification_thread_command_add_channel(
643 struct notification_thread_state
*state
,
644 struct channel_info
*channel_info
,
645 enum lttng_error_code
*cmd_result
)
647 struct cds_list_head trigger_list
;
648 struct channel_info
*new_channel_info
;
649 struct channel_key
*channel_key
;
650 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
651 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
652 int trigger_count
= 0;
653 struct cds_lfht_iter iter
;
655 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
656 channel_info
->channel_name
, channel_info
->session_name
,
657 channel_info
->key
.key
, channel_info
->key
.domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
659 CDS_INIT_LIST_HEAD(&trigger_list
);
661 new_channel_info
= channel_info_copy(channel_info
);
662 if (!new_channel_info
) {
666 channel_key
= &new_channel_info
->key
;
668 /* Build a list of all triggers applying to the new channel. */
669 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
671 struct lttng_trigger_list_element
*new_element
;
673 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
678 new_element
= zmalloc(sizeof(*new_element
));
682 CDS_INIT_LIST_HEAD(&new_element
->node
);
683 new_element
->trigger
= trigger_ht_element
->trigger
;
684 cds_list_add(&new_element
->node
, &trigger_list
);
688 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
690 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
691 if (!channel_trigger_list
) {
694 channel_trigger_list
->channel_key
= *channel_key
;
695 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
696 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
697 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
700 /* Add channel to the channel_ht which owns the channel_infos. */
701 cds_lfht_add(state
->channels_ht
,
702 hash_channel_key(channel_key
),
703 &new_channel_info
->channels_ht_node
);
705 * Add the list of triggers associated with this channel to the
706 * channel_triggers_ht.
708 cds_lfht_add(state
->channel_triggers_ht
,
709 hash_channel_key(channel_key
),
710 &channel_trigger_list
->channel_triggers_ht_node
);
712 *cmd_result
= LTTNG_OK
;
715 /* Empty trigger list */
716 channel_info_destroy(new_channel_info
);
721 int handle_notification_thread_command_remove_channel(
722 struct notification_thread_state
*state
,
723 uint64_t channel_key
, enum lttng_domain_type domain
,
724 enum lttng_error_code
*cmd_result
)
726 struct cds_lfht_node
*node
;
727 struct cds_lfht_iter iter
;
728 struct lttng_channel_trigger_list
*trigger_list
;
729 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
730 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
731 struct channel_info
*channel_info
;
733 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
734 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
738 cds_lfht_lookup(state
->channel_triggers_ht
,
739 hash_channel_key(&key
),
740 match_channel_trigger_list
,
743 node
= cds_lfht_iter_get_node(&iter
);
745 * There is a severe internal error if we are being asked to remove a
746 * channel that doesn't exist.
749 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
753 /* Free the list of triggers associated with this channel. */
754 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
755 channel_triggers_ht_node
);
756 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
757 &trigger_list
->list
, node
) {
758 cds_list_del(&trigger_list_element
->node
);
759 free(trigger_list_element
);
761 cds_lfht_del(state
->channel_triggers_ht
, node
);
764 /* Free sampled channel state. */
765 cds_lfht_lookup(state
->channel_state_ht
,
766 hash_channel_key(&key
),
767 match_channel_state_sample
,
770 node
= cds_lfht_iter_get_node(&iter
);
772 * This is expected to be NULL if the channel is destroyed before we
776 struct channel_state_sample
*sample
= caa_container_of(node
,
777 struct channel_state_sample
,
778 channel_state_ht_node
);
780 cds_lfht_del(state
->channel_state_ht
, node
);
784 /* Remove the channel from the channels_ht and free it. */
785 cds_lfht_lookup(state
->channels_ht
,
786 hash_channel_key(&key
),
790 node
= cds_lfht_iter_get_node(&iter
);
792 channel_info
= caa_container_of(node
, struct channel_info
,
794 cds_lfht_del(state
->channels_ht
, node
);
795 channel_info_destroy(channel_info
);
798 *cmd_result
= LTTNG_OK
;
803 * FIXME A client's credentials are not checked when registering a trigger, nor
804 * are they stored alongside with the trigger.
806 * The effects of this are benign:
807 * - The client will succeed in registering the trigger, as it is valid,
808 * - The trigger will, internally, be bound to the channel,
809 * - The notifications will not be sent since the client's credentials
810 * are checked against the channel at that moment.
813 int handle_notification_thread_command_register_trigger(
814 struct notification_thread_state
*state
,
815 struct lttng_trigger
*trigger
,
816 enum lttng_error_code
*cmd_result
)
819 struct lttng_condition
*condition
;
820 struct notification_client
*client
;
821 struct notification_client_list
*client_list
= NULL
;
822 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
823 struct notification_client_list_element
*client_list_element
, *tmp
;
824 struct cds_lfht_node
*node
;
825 struct cds_lfht_iter iter
;
826 struct channel_info
*channel
;
827 bool free_trigger
= true;
831 condition
= lttng_trigger_get_condition(trigger
);
832 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
833 if (!trigger_ht_element
) {
838 /* Add trigger to the trigger_ht. */
839 cds_lfht_node_init(&trigger_ht_element
->node
);
840 trigger_ht_element
->trigger
= trigger
;
842 node
= cds_lfht_add_unique(state
->triggers_ht
,
843 lttng_condition_hash(condition
),
846 &trigger_ht_element
->node
);
847 if (node
!= &trigger_ht_element
->node
) {
848 /* Not a fatal error, simply report it to the client. */
849 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
850 goto error_free_ht_element
;
854 * Ownership of the trigger and of its wrapper was transfered to
857 trigger_ht_element
= NULL
;
858 free_trigger
= false;
861 * The rest only applies to triggers that have a "notify" action.
862 * It is not skipped as this is the only action type currently
865 client_list
= zmalloc(sizeof(*client_list
));
868 goto error_free_ht_element
;
870 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
871 CDS_INIT_LIST_HEAD(&client_list
->list
);
872 client_list
->trigger
= trigger
;
874 /* Build a list of clients to which this new trigger applies. */
875 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
876 client_socket_ht_node
) {
877 if (!trigger_applies_to_client(trigger
, client
)) {
881 client_list_element
= zmalloc(sizeof(*client_list_element
));
882 if (!client_list_element
) {
884 goto error_free_client_list
;
886 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
887 client_list_element
->client
= client
;
888 cds_list_add(&client_list_element
->node
, &client_list
->list
);
891 cds_lfht_add(state
->notification_trigger_clients_ht
,
892 lttng_condition_hash(condition
),
893 &client_list
->notification_trigger_ht_node
);
895 * Client list ownership transferred to the
896 * notification_trigger_clients_ht.
901 * Add the trigger to list of triggers bound to the channels currently
904 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
906 struct lttng_trigger_list_element
*trigger_list_element
;
907 struct lttng_channel_trigger_list
*trigger_list
;
909 if (!trigger_applies_to_channel(trigger
, channel
)) {
913 cds_lfht_lookup(state
->channel_triggers_ht
,
914 hash_channel_key(&channel
->key
),
915 match_channel_trigger_list
,
918 node
= cds_lfht_iter_get_node(&iter
);
920 /* Free the list of triggers associated with this channel. */
921 trigger_list
= caa_container_of(node
,
922 struct lttng_channel_trigger_list
,
923 channel_triggers_ht_node
);
925 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
926 if (!trigger_list_element
) {
928 goto error_free_client_list
;
930 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
931 trigger_list_element
->trigger
= trigger
;
932 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
933 /* A trigger can only apply to one channel. */
937 *cmd_result
= LTTNG_OK
;
938 error_free_client_list
:
940 cds_list_for_each_entry_safe(client_list_element
, tmp
,
941 &client_list
->list
, node
) {
942 free(client_list_element
);
946 error_free_ht_element
:
947 free(trigger_ht_element
);
950 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
952 lttng_condition_destroy(condition
);
953 lttng_action_destroy(action
);
954 lttng_trigger_destroy(trigger
);
960 int handle_notification_thread_command_unregister_trigger(
961 struct notification_thread_state
*state
,
962 struct lttng_trigger
*trigger
,
963 enum lttng_error_code
*_cmd_reply
)
965 struct cds_lfht_iter iter
;
966 struct cds_lfht_node
*node
, *triggers_ht_node
;
967 struct lttng_channel_trigger_list
*trigger_list
;
968 struct notification_client_list
*client_list
;
969 struct notification_client_list_element
*client_list_element
, *tmp
;
970 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
971 struct lttng_condition
*condition
= lttng_trigger_get_condition(
973 struct lttng_action
*action
;
974 enum lttng_error_code cmd_reply
;
978 cds_lfht_lookup(state
->triggers_ht
,
979 lttng_condition_hash(condition
),
983 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
984 if (!triggers_ht_node
) {
985 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
988 cmd_reply
= LTTNG_OK
;
991 /* Remove trigger from channel_triggers_ht. */
992 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
993 channel_triggers_ht_node
) {
994 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
996 cds_list_for_each_entry_safe(trigger_element
, tmp
,
997 &trigger_list
->list
, node
) {
998 struct lttng_condition
*current_condition
=
999 lttng_trigger_get_condition(
1000 trigger_element
->trigger
);
1002 assert(current_condition
);
1003 if (!lttng_condition_is_equal(condition
,
1004 current_condition
)) {
1008 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1009 cds_list_del(&trigger_element
->node
);
1014 * Remove and release the client list from
1015 * notification_trigger_clients_ht.
1017 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1018 lttng_condition_hash(condition
),
1022 node
= cds_lfht_iter_get_node(&iter
);
1024 client_list
= caa_container_of(node
, struct notification_client_list
,
1025 notification_trigger_ht_node
);
1026 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1027 &client_list
->list
, node
) {
1028 free(client_list_element
);
1030 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1033 /* Remove trigger from triggers_ht. */
1034 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1035 struct lttng_trigger_ht_element
, node
);
1036 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1038 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1039 lttng_condition_destroy(condition
);
1040 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1041 lttng_action_destroy(action
);
1042 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1043 free(trigger_ht_element
);
1047 *_cmd_reply
= cmd_reply
;
1052 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1053 int handle_notification_thread_command(
1054 struct notification_thread_handle
*handle
,
1055 struct notification_thread_state
*state
)
1059 struct notification_thread_command
*cmd
;
1061 /* Read event_fd to put it back into a quiescent state. */
1062 ret
= read(handle
->cmd_queue
.event_fd
, &counter
, sizeof(counter
));
1067 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1068 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1069 struct notification_thread_command
, cmd_list_node
);
1070 switch (cmd
->type
) {
1071 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1072 DBG("[notification-thread] Received register trigger command");
1073 ret
= handle_notification_thread_command_register_trigger(
1074 state
, cmd
->parameters
.trigger
,
1077 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1078 DBG("[notification-thread] Received unregister trigger command");
1079 ret
= handle_notification_thread_command_unregister_trigger(
1080 state
, cmd
->parameters
.trigger
,
1083 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1084 DBG("[notification-thread] Received add channel command");
1085 ret
= handle_notification_thread_command_add_channel(
1086 state
, &cmd
->parameters
.add_channel
,
1089 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1090 DBG("[notification-thread] Received remove channel command");
1091 ret
= handle_notification_thread_command_remove_channel(
1092 state
, cmd
->parameters
.remove_channel
.key
,
1093 cmd
->parameters
.remove_channel
.domain
,
1096 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1097 DBG("[notification-thread] Received quit command");
1098 cmd
->reply_code
= LTTNG_OK
;
1102 ERR("[notification-thread] Unknown internal command received");
1110 cds_list_del(&cmd
->cmd_list_node
);
1111 futex_nto1_wake(&cmd
->reply_futex
);
1112 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1115 /* Wake-up and return a fatal error to the calling thread. */
1116 futex_nto1_wake(&cmd
->reply_futex
);
1117 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1118 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1120 /* Indicate a fatal error to the caller. */
1125 unsigned long hash_client_socket(int socket
)
1127 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1131 int socket_set_non_blocking(int socket
)
1135 /* Set the pipe as non-blocking. */
1136 ret
= fcntl(socket
, F_GETFL
, 0);
1138 PERROR("fcntl get socket flags");
1143 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1145 PERROR("fcntl set O_NONBLOCK socket flag");
1148 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1154 void client_reset_inbound_state(struct notification_client
*client
)
1158 ret
= lttng_dynamic_buffer_set_size(
1159 &client
->communication
.inbound
.buffer
, 0);
1162 client
->communication
.inbound
.bytes_to_receive
=
1163 sizeof(struct lttng_notification_channel_message
);
1164 client
->communication
.inbound
.msg_type
=
1165 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1166 client
->communication
.inbound
.receive_creds
= false;
1167 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1168 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1171 int handle_notification_thread_client_connect(
1172 struct notification_thread_state
*state
)
1175 struct notification_client
*client
;
1177 DBG("[notification-thread] Handling new notification channel client connection");
1179 client
= zmalloc(sizeof(*client
));
1185 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1186 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1187 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1188 client_reset_inbound_state(client
);
1190 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1192 ERR("[notification-thread] Failed to accept new notification channel client connection");
1197 client
->socket
= ret
;
1199 ret
= socket_set_non_blocking(client
->socket
);
1201 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1205 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1207 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1212 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1213 LPOLLIN
| LPOLLERR
|
1214 LPOLLHUP
| LPOLLRDHUP
);
1216 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1220 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1225 cds_lfht_add(state
->client_socket_ht
,
1226 hash_client_socket(client
->socket
),
1227 &client
->client_socket_ht_node
);
1232 notification_client_destroy(client
, state
);
1236 int handle_notification_thread_client_disconnect(
1238 struct notification_thread_state
*state
)
1241 struct notification_client
*client
;
1244 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1246 client
= get_client_from_socket(client_socket
, state
);
1248 /* Internal state corruption, fatal error. */
1249 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1255 ret
= lttng_poll_del(&state
->events
, client_socket
);
1257 ERR("[notification-thread] Failed to remove client socket from poll set");
1259 cds_lfht_del(state
->client_socket_ht
,
1260 &client
->client_socket_ht_node
);
1261 notification_client_destroy(client
, state
);
1267 int handle_notification_thread_client_disconnect_all(
1268 struct notification_thread_state
*state
)
1270 struct cds_lfht_iter iter
;
1271 struct notification_client
*client
;
1272 bool error_encoutered
= false;
1275 DBG("[notification-thread] Closing all client connections");
1276 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1277 client_socket_ht_node
) {
1280 ret
= handle_notification_thread_client_disconnect(
1281 client
->socket
, state
);
1283 error_encoutered
= true;
1287 return error_encoutered
? 1 : 0;
1290 int handle_notification_thread_trigger_unregister_all(
1291 struct notification_thread_state
*state
)
1293 bool error_occured
= false;
1294 struct cds_lfht_iter iter
;
1295 struct lttng_trigger_ht_element
*trigger_ht_element
;
1297 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1299 int ret
= handle_notification_thread_command_unregister_trigger(
1300 state
, trigger_ht_element
->trigger
, NULL
);
1302 error_occured
= true;
1305 return error_occured
? -1 : 0;
1309 int client_flush_outgoing_queue(struct notification_client
*client
,
1310 struct notification_thread_state
*state
)
1313 size_t to_send_count
;
1315 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1316 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1317 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1320 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1321 client
->communication
.outbound
.buffer
.data
,
1323 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1324 (ret
> 0 && ret
< to_send_count
)) {
1325 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1327 to_send_count
-= max(ret
, 0);
1329 memcpy(client
->communication
.outbound
.buffer
.data
,
1330 client
->communication
.outbound
.buffer
.data
+
1331 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1333 ret
= lttng_dynamic_buffer_set_size(
1334 &client
->communication
.outbound
.buffer
,
1341 * We want to be notified whenever there is buffer space
1342 * available to send the rest of the payload.
1344 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1345 CLIENT_POLL_MASK_IN_OUT
);
1349 } else if (ret
< 0) {
1350 /* Generic error, disconnect the client. */
1351 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1353 ret
= handle_notification_thread_client_disconnect(
1354 client
->socket
, state
);
1359 /* No error and flushed the queue completely. */
1360 ret
= lttng_dynamic_buffer_set_size(
1361 &client
->communication
.outbound
.buffer
, 0);
1365 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1366 CLIENT_POLL_MASK_IN
);
1371 client
->communication
.outbound
.queued_command_reply
= false;
1372 client
->communication
.outbound
.dropped_notification
= false;
1381 int client_send_command_reply(struct notification_client
*client
,
1382 struct notification_thread_state
*state
,
1383 enum lttng_notification_channel_status status
)
1386 struct lttng_notification_channel_command_reply reply
= {
1387 .status
= (int8_t) status
,
1389 struct lttng_notification_channel_message msg
= {
1390 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1391 .size
= sizeof(reply
),
1393 char buffer
[sizeof(msg
) + sizeof(reply
)];
1395 if (client
->communication
.outbound
.queued_command_reply
) {
1396 /* Protocol error. */
1400 memcpy(buffer
, &msg
, sizeof(msg
));
1401 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1402 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1404 /* Enqueue buffer to outgoing queue and flush it. */
1405 ret
= lttng_dynamic_buffer_append(
1406 &client
->communication
.outbound
.buffer
,
1407 buffer
, sizeof(buffer
));
1412 ret
= client_flush_outgoing_queue(client
, state
);
1417 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1418 /* Queue could not be emptied. */
1419 client
->communication
.outbound
.queued_command_reply
= true;
1428 int client_dispatch_message(struct notification_client
*client
,
1429 struct notification_thread_state
*state
)
1433 if (client
->communication
.inbound
.msg_type
!=
1434 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1435 client
->communication
.inbound
.msg_type
!=
1436 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1437 !client
->validated
) {
1438 WARN("[notification-thread] client attempted a command before handshake");
1443 switch (client
->communication
.inbound
.msg_type
) {
1444 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1447 * Receiving message header. The function will be called again
1448 * once the rest of the message as been received and can be
1451 const struct lttng_notification_channel_message
*msg
;
1453 assert(sizeof(*msg
) ==
1454 client
->communication
.inbound
.buffer
.size
);
1455 msg
= (const struct lttng_notification_channel_message
*)
1456 client
->communication
.inbound
.buffer
.data
;
1458 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1459 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1464 switch (msg
->type
) {
1465 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1466 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1467 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1471 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1475 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
1476 client
->communication
.inbound
.msg_type
=
1477 (enum lttng_notification_channel_message_type
) msg
->type
;
1478 if (client
->communication
.inbound
.msg_type
==
1479 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
) {
1480 client
->communication
.inbound
.receive_creds
= true;
1482 ret
= lttng_dynamic_buffer_set_size(
1483 &client
->communication
.inbound
.buffer
, 0);
1489 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1491 struct lttng_notification_channel_command_handshake
*handshake_client
;
1492 struct lttng_notification_channel_command_handshake handshake_reply
= {
1493 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
1494 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
1496 struct lttng_notification_channel_message msg_header
= {
1497 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
1498 .size
= sizeof(handshake_reply
),
1500 enum lttng_notification_channel_status status
=
1501 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1502 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
1504 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
1505 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
1506 sizeof(handshake_reply
));
1509 (struct lttng_notification_channel_command_handshake
*)
1510 client
->communication
.inbound
.buffer
.data
;
1511 client
->major
= handshake_client
->major
;
1512 client
->minor
= handshake_client
->minor
;
1513 if (!client
->communication
.inbound
.creds_received
) {
1514 ERR("[notification-thread] No credentials received from client");
1519 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
1520 &client
->communication
.inbound
.creds
);
1521 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
1522 &client
->communication
.inbound
.creds
);
1523 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
1524 client
->uid
, client
->gid
, (int) client
->major
,
1525 (int) client
->minor
);
1527 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
1528 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
1531 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
1532 send_buffer
, sizeof(send_buffer
));
1534 ERR("[notification-thread] Failed to send protocol version to notification channel client");
1538 ret
= client_flush_outgoing_queue(client
, state
);
1543 ret
= client_send_command_reply(client
, state
, status
);
1545 ERR("[notification-thread] Failed to send reply to notification channel client");
1549 /* Set reception state to receive the next message header. */
1550 client_reset_inbound_state(client
);
1551 client
->validated
= true;
1554 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1555 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1557 struct lttng_condition
*condition
;
1558 enum lttng_notification_channel_status status
=
1559 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1560 const struct lttng_buffer_view condition_view
=
1561 lttng_buffer_view_from_dynamic_buffer(
1562 &client
->communication
.inbound
.buffer
,
1564 size_t expected_condition_size
=
1565 client
->communication
.inbound
.buffer
.size
;
1567 ret
= lttng_condition_create_from_buffer(&condition_view
,
1569 if (ret
!= expected_condition_size
) {
1570 ERR("[notification-thread] Malformed condition received from client");
1574 if (client
->communication
.inbound
.msg_type
==
1575 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
1577 * FIXME The current state should be evaluated on
1580 ret
= notification_thread_client_subscribe(client
,
1581 condition
, state
, &status
);
1583 ret
= notification_thread_client_unsubscribe(client
,
1584 condition
, state
, &status
);
1590 ret
= client_send_command_reply(client
, state
, status
);
1592 ERR("[notification-thread] Failed to send reply to notification channel client");
1596 /* Set reception state to receive the next message header. */
1597 client_reset_inbound_state(client
);
1607 /* Incoming data from client. */
1608 int handle_notification_thread_client_in(
1609 struct notification_thread_state
*state
, int socket
)
1612 struct notification_client
*client
;
1616 client
= get_client_from_socket(socket
, state
);
1618 /* Internal error, abort. */
1623 offset
= client
->communication
.inbound
.buffer
.size
;
1624 ret
= lttng_dynamic_buffer_set_size(
1625 &client
->communication
.inbound
.buffer
,
1626 client
->communication
.inbound
.bytes_to_receive
);
1631 if (client
->communication
.inbound
.receive_creds
) {
1632 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
1633 client
->communication
.inbound
.buffer
.data
+ offset
,
1634 client
->communication
.inbound
.bytes_to_receive
,
1635 &client
->communication
.inbound
.creds
);
1637 client
->communication
.inbound
.receive_creds
= false;
1638 client
->communication
.inbound
.creds_received
= true;
1641 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
1642 client
->communication
.inbound
.buffer
.data
+ offset
,
1643 client
->communication
.inbound
.bytes_to_receive
);
1646 goto error_disconnect_client
;
1649 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
1650 ret
= lttng_dynamic_buffer_set_size(
1651 &client
->communication
.inbound
.buffer
,
1652 client
->communication
.inbound
.buffer
.size
-
1653 client
->communication
.inbound
.bytes_to_receive
);
1658 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
1659 ret
= client_dispatch_message(client
, state
);
1662 * Only returns an error if this client must be
1665 goto error_disconnect_client
;
1672 error_disconnect_client
:
1673 ret
= handle_notification_thread_client_disconnect(socket
, state
);
1677 /* Client ready to receive outgoing data. */
1678 int handle_notification_thread_client_out(
1679 struct notification_thread_state
*state
, int socket
)
1682 struct notification_client
*client
;
1684 client
= get_client_from_socket(socket
, state
);
1686 /* Internal error, abort. */
1691 ret
= client_flush_outgoing_queue(client
, state
);
1700 bool evaluate_buffer_usage_condition(struct lttng_condition
*condition
,
1701 struct channel_state_sample
*sample
, uint64_t buffer_capacity
)
1703 bool result
= false;
1705 enum lttng_condition_type condition_type
;
1706 struct lttng_condition_buffer_usage
*use_condition
= container_of(
1707 condition
, struct lttng_condition_buffer_usage
,
1714 if (use_condition
->threshold_bytes
.set
) {
1715 threshold
= use_condition
->threshold_bytes
.value
;
1718 * Threshold was expressed as a ratio.
1720 * TODO the threshold (in bytes) of conditions expressed
1721 * as a ratio of total buffer size could be cached to
1722 * forego this double-multiplication or it could be performed
1723 * as fixed-point math.
1725 * Note that caching should accomodate the case where the
1726 * condition applies to multiple channels (i.e. don't assume
1727 * that all channels matching my_chann* have the same size...)
1729 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
1730 (double) buffer_capacity
);
1733 condition_type
= lttng_condition_get_type(condition
);
1734 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
1735 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1736 threshold
, sample
->highest_usage
);
1739 * The low condition should only be triggered once _all_ of the
1740 * streams in a channel have gone below the "low" threshold.
1742 if (sample
->highest_usage
<= threshold
) {
1746 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1747 threshold
, sample
->highest_usage
);
1750 * For high buffer usage scenarios, we want to trigger whenever
1751 * _any_ of the streams has reached the "high" threshold.
1753 if (sample
->highest_usage
>= threshold
) {
1762 int evaluate_condition(struct lttng_condition
*condition
,
1763 struct lttng_evaluation
**evaluation
,
1764 struct notification_thread_state
*state
,
1765 struct channel_state_sample
*previous_sample
,
1766 struct channel_state_sample
*latest_sample
,
1767 uint64_t buffer_capacity
)
1770 enum lttng_condition_type condition_type
;
1771 bool previous_sample_result
;
1772 bool latest_sample_result
;
1774 condition_type
= lttng_condition_get_type(condition
);
1775 /* No other condition type supported for the moment. */
1776 assert(condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
1777 condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
1779 previous_sample_result
= evaluate_buffer_usage_condition(condition
,
1780 previous_sample
, buffer_capacity
);
1781 latest_sample_result
= evaluate_buffer_usage_condition(condition
,
1782 latest_sample
, buffer_capacity
);
1784 if (!latest_sample_result
||
1785 (previous_sample_result
== latest_sample_result
)) {
1787 * Only trigger on a condition evaluation transition.
1789 * NOTE: This edge-triggered logic may not be appropriate for
1790 * future condition types.
1795 if (evaluation
&& latest_sample_result
) {
1796 *evaluation
= lttng_evaluation_buffer_usage_create(
1798 latest_sample
->highest_usage
,
1810 int client_enqueue_dropped_notification(struct notification_client
*client
,
1811 struct notification_thread_state
*state
)
1814 struct lttng_notification_channel_message msg
= {
1815 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
1819 ret
= lttng_dynamic_buffer_append(
1820 &client
->communication
.outbound
.buffer
, &msg
,
1826 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
1827 struct lttng_evaluation
*evaluation
,
1828 struct notification_client_list
* client_list
,
1829 struct notification_thread_state
*state
,
1830 uid_t channel_uid
, gid_t channel_gid
)
1833 struct lttng_dynamic_buffer msg_buffer
;
1834 struct notification_client_list_element
*client_list_element
, *tmp
;
1835 struct lttng_notification
*notification
;
1836 struct lttng_condition
*condition
;
1837 ssize_t expected_notification_size
, notification_size
;
1838 struct lttng_notification_channel_message msg
;
1840 lttng_dynamic_buffer_init(&msg_buffer
);
1842 condition
= lttng_trigger_get_condition(trigger
);
1845 notification
= lttng_notification_create(condition
, evaluation
);
1846 if (!notification
) {
1851 expected_notification_size
= lttng_notification_serialize(notification
,
1853 if (expected_notification_size
< 0) {
1854 ERR("[notification-thread] Failed to get size of serialized notification");
1859 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
1860 msg
.size
= (uint32_t) expected_notification_size
;
1861 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg
, sizeof(msg
));
1866 ret
= lttng_dynamic_buffer_set_size(&msg_buffer
,
1867 msg_buffer
.size
+ expected_notification_size
);
1872 notification_size
= lttng_notification_serialize(notification
,
1873 msg_buffer
.data
+ sizeof(msg
));
1874 if (notification_size
!= expected_notification_size
) {
1875 ERR("[notification-thread] Failed to serialize notification");
1880 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1881 &client_list
->list
, node
) {
1882 struct notification_client
*client
=
1883 client_list_element
->client
;
1885 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
1887 /* Client is not allowed to monitor this channel. */
1888 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
1892 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
1893 client
->socket
, msg_buffer
.size
);
1894 if (client
->communication
.outbound
.buffer
.size
) {
1896 * Outgoing data is already buffered for this client;
1897 * drop the notification and enqueue a "dropped
1898 * notification" message if this is the first dropped
1899 * notification since the socket spilled-over to the
1902 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
1904 if (!client
->communication
.outbound
.dropped_notification
) {
1905 client
->communication
.outbound
.dropped_notification
= true;
1906 ret
= client_enqueue_dropped_notification(
1915 ret
= lttng_dynamic_buffer_append_buffer(
1916 &client
->communication
.outbound
.buffer
,
1922 ret
= client_flush_outgoing_queue(client
, state
);
1929 lttng_notification_destroy(notification
);
1930 lttng_dynamic_buffer_reset(&msg_buffer
);
1934 int handle_notification_thread_channel_sample(
1935 struct notification_thread_state
*state
, int pipe
,
1936 enum lttng_domain_type domain
)
1939 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
1940 struct channel_state_sample previous_sample
, latest_sample
;
1941 struct channel_info
*channel_info
;
1942 struct cds_lfht_node
*node
;
1943 struct cds_lfht_iter iter
;
1944 struct lttng_channel_trigger_list
*trigger_list
;
1945 struct lttng_trigger_list_element
*trigger_list_element
;
1946 bool previous_sample_available
= false;
1949 * The monitoring pipe only holds messages smaller than PIPE_BUF,
1950 * ensuring that read/write of sampling messages are atomic.
1953 ret
= read(pipe
, &sample_msg
, sizeof(sample_msg
));
1954 } while (ret
== -1 && errno
== EINTR
);
1955 if (ret
!= sizeof(sample_msg
)) {
1956 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
1963 latest_sample
.key
.key
= sample_msg
.key
;
1964 latest_sample
.key
.domain
= domain
;
1965 latest_sample
.highest_usage
= sample_msg
.highest
;
1966 latest_sample
.lowest_usage
= sample_msg
.lowest
;
1970 /* Retrieve the channel's informations */
1971 cds_lfht_lookup(state
->channels_ht
,
1972 hash_channel_key(&latest_sample
.key
),
1976 node
= cds_lfht_iter_get_node(&iter
);
1979 * Not an error since the consumer can push a sample to the pipe
1980 * and the rest of the session daemon could notify us of the
1981 * channel's destruction before we get a chance to process that
1984 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
1985 latest_sample
.key
.key
,
1986 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
1990 channel_info
= caa_container_of(node
, struct channel_info
,
1992 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
")",
1993 channel_info
->channel_name
,
1994 latest_sample
.key
.key
,
1995 channel_info
->session_name
,
1996 latest_sample
.highest_usage
,
1997 latest_sample
.lowest_usage
);
1999 /* Retrieve the channel's last sample, if it exists, and update it. */
2000 cds_lfht_lookup(state
->channel_state_ht
,
2001 hash_channel_key(&latest_sample
.key
),
2002 match_channel_state_sample
,
2005 node
= cds_lfht_iter_get_node(&iter
);
2007 struct channel_state_sample
*stored_sample
;
2009 /* Update the sample stored. */
2010 stored_sample
= caa_container_of(node
,
2011 struct channel_state_sample
,
2012 channel_state_ht_node
);
2013 memcpy(&previous_sample
, stored_sample
,
2014 sizeof(previous_sample
));
2015 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2016 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2017 previous_sample_available
= true;
2020 * This is the channel's first sample, allocate space for and
2021 * store the new sample.
2023 struct channel_state_sample
*stored_sample
;
2025 stored_sample
= zmalloc(sizeof(*stored_sample
));
2026 if (!stored_sample
) {
2031 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2032 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2033 cds_lfht_add(state
->channel_state_ht
,
2034 hash_channel_key(&stored_sample
->key
),
2035 &stored_sample
->channel_state_ht_node
);
2038 /* Find triggers associated with this channel. */
2039 cds_lfht_lookup(state
->channel_triggers_ht
,
2040 hash_channel_key(&latest_sample
.key
),
2041 match_channel_trigger_list
,
2044 node
= cds_lfht_iter_get_node(&iter
);
2049 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2050 channel_triggers_ht_node
);
2051 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2053 struct lttng_condition
*condition
;
2054 struct lttng_action
*action
;
2055 struct lttng_trigger
*trigger
;
2056 struct notification_client_list
*client_list
;
2057 struct lttng_evaluation
*evaluation
= NULL
;
2059 trigger
= trigger_list_element
->trigger
;
2060 condition
= lttng_trigger_get_condition(trigger
);
2062 action
= lttng_trigger_get_action(trigger
);
2064 /* Notify actions are the only type currently supported. */
2065 assert(lttng_action_get_type(action
) ==
2066 LTTNG_ACTION_TYPE_NOTIFY
);
2069 * Check if any client is subscribed to the result of this
2072 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2073 lttng_condition_hash(condition
),
2077 node
= cds_lfht_iter_get_node(&iter
);
2080 client_list
= caa_container_of(node
,
2081 struct notification_client_list
,
2082 notification_trigger_ht_node
);
2083 if (cds_list_empty(&client_list
->list
)) {
2085 * No clients interested in the evaluation's result,
2091 ret
= evaluate_condition(condition
, &evaluation
, state
,
2092 previous_sample_available
? &previous_sample
: NULL
,
2093 &latest_sample
, channel_info
->capacity
);
2102 /* Dispatch evaluation result to all clients. */
2103 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2104 evaluation
, client_list
, state
,
2105 channel_info
->uid
, channel_info
->gid
);