2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <urcu/rculfhash.h>
22 #include <common/defaults.h>
23 #include <common/error.h>
24 #include <common/futex.h>
25 #include <common/unix.h>
26 #include <common/dynamic-buffer.h>
27 #include <common/hashtable/utils.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/macros.h>
30 #include <lttng/condition/condition.h>
31 #include <lttng/action/action.h>
32 #include <lttng/notification/notification-internal.h>
33 #include <lttng/condition/condition-internal.h>
34 #include <lttng/condition/buffer-usage-internal.h>
35 #include <lttng/notification/channel-internal.h>
43 #include "notification-thread.h"
44 #include "notification-thread-events.h"
45 #include "notification-thread-commands.h"
46 #include "lttng-sessiond.h"
49 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
50 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
52 struct lttng_trigger_list_element
{
53 struct lttng_trigger
*trigger
;
54 struct cds_list_head node
;
57 struct lttng_channel_trigger_list
{
58 struct channel_key channel_key
;
59 struct cds_list_head list
;
60 struct cds_lfht_node channel_triggers_ht_node
;
63 struct lttng_trigger_ht_element
{
64 struct lttng_trigger
*trigger
;
65 struct cds_lfht_node node
;
68 struct lttng_condition_list_element
{
69 struct lttng_condition
*condition
;
70 struct cds_list_head node
;
73 struct notification_client_list_element
{
74 struct notification_client
*client
;
75 struct cds_list_head node
;
78 struct notification_client_list
{
79 struct lttng_trigger
*trigger
;
80 struct cds_list_head list
;
81 struct cds_lfht_node notification_trigger_ht_node
;
84 struct notification_client
{
86 /* Client protocol version. */
91 * Indicates if the credentials and versions of the client has been
96 * Conditions to which the client's notification channel is subscribed.
97 * List of struct lttng_condition_list_node. The condition member is
98 * owned by the client.
100 struct cds_list_head condition_list
;
101 struct cds_lfht_node client_socket_ht_node
;
105 * During the reception of a message, the reception
106 * buffers' "size" is set to contain the current
107 * message's complete payload.
109 struct lttng_dynamic_buffer buffer
;
110 /* Bytes left to receive for the current message. */
111 size_t bytes_to_receive
;
112 /* Type of the message being received. */
113 enum lttng_notification_channel_message_type msg_type
;
115 * Indicates whether or not credentials are expected
120 * Indicates whether or not credentials were received
124 /* Only used during credentials reception. */
125 lttng_sock_cred creds
;
129 * Indicates whether or not a notification addressed to
130 * this client was dropped because a command reply was
133 * A notification is dropped whenever the buffer is not
136 bool dropped_notification
;
138 * Indicates whether or not a command reply is already
139 * buffered. In this case, it means that the client is
140 * not consuming command replies before emitting a new
141 * one. This could be caused by a protocol error or a
142 * misbehaving/malicious client.
144 bool queued_command_reply
;
145 struct lttng_dynamic_buffer buffer
;
150 struct channel_state_sample
{
151 struct channel_key key
;
152 struct cds_lfht_node channel_state_ht_node
;
153 uint64_t highest_usage
;
154 uint64_t lowest_usage
;
158 int match_client(struct cds_lfht_node
*node
, const void *key
)
160 /* This double-cast is intended to supress pointer-to-cast warning. */
161 int socket
= (int) (intptr_t) key
;
162 struct notification_client
*client
;
164 client
= caa_container_of(node
, struct notification_client
,
165 client_socket_ht_node
);
167 return !!(client
->socket
== socket
);
171 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
173 struct channel_key
*channel_key
= (struct channel_key
*) key
;
174 struct lttng_channel_trigger_list
*trigger_list
;
176 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
177 channel_triggers_ht_node
);
179 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
180 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
184 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
186 struct channel_key
*channel_key
= (struct channel_key
*) key
;
187 struct channel_state_sample
*sample
;
189 sample
= caa_container_of(node
, struct channel_state_sample
,
190 channel_state_ht_node
);
192 return !!((channel_key
->key
== sample
->key
.key
) &&
193 (channel_key
->domain
== sample
->key
.domain
));
197 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
199 struct channel_key
*channel_key
= (struct channel_key
*) key
;
200 struct channel_info
*channel_info
;
202 channel_info
= caa_container_of(node
, struct channel_info
,
205 return !!((channel_key
->key
== channel_info
->key
.key
) &&
206 (channel_key
->domain
== channel_info
->key
.domain
));
210 int match_condition(struct cds_lfht_node
*node
, const void *key
)
212 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
213 struct lttng_trigger_ht_element
*trigger
;
214 struct lttng_condition
*condition
;
216 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
218 condition
= lttng_trigger_get_condition(trigger
->trigger
);
221 return !!lttng_condition_is_equal(condition_key
, condition
);
225 int match_client_list(struct cds_lfht_node
*node
, const void *key
)
227 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
228 struct notification_client_list
*client_list
;
229 struct lttng_condition
*condition
;
230 struct lttng_condition
*condition_key
= lttng_trigger_get_condition(
233 assert(condition_key
);
235 client_list
= caa_container_of(node
, struct notification_client_list
,
236 notification_trigger_ht_node
);
237 condition
= lttng_trigger_get_condition(client_list
->trigger
);
239 return !!lttng_condition_is_equal(condition_key
, condition
);
243 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
245 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
246 struct notification_client_list
*client_list
;
247 struct lttng_condition
*condition
;
249 assert(condition_key
);
251 client_list
= caa_container_of(node
, struct notification_client_list
,
252 notification_trigger_ht_node
);
253 condition
= lttng_trigger_get_condition(client_list
->trigger
);
255 return !!lttng_condition_is_equal(condition_key
, condition
);
259 unsigned long lttng_condition_buffer_usage_hash(
260 struct lttng_condition
*_condition
)
262 unsigned long hash
= 0;
263 struct lttng_condition_buffer_usage
*condition
;
265 condition
= container_of(_condition
,
266 struct lttng_condition_buffer_usage
, parent
);
268 if (condition
->session_name
) {
269 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
271 if (condition
->channel_name
) {
272 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
274 if (condition
->domain
.set
) {
275 hash
^= hash_key_ulong(
276 (void *) condition
->domain
.type
,
279 if (condition
->threshold_ratio
.set
) {
282 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
283 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
284 } else if (condition
->threshold_ratio
.set
) {
287 val
= condition
->threshold_bytes
.value
;
288 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
294 * The lttng_condition hashing code is kept in this file (rather than
295 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
296 * don't want to link in liblttng-ctl.
299 unsigned long lttng_condition_hash(struct lttng_condition
*condition
)
301 switch (condition
->type
) {
302 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
303 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
304 return lttng_condition_buffer_usage_hash(condition
);
306 ERR("[notification-thread] Unexpected condition type caught");
312 void channel_info_destroy(struct channel_info
*channel_info
)
318 if (channel_info
->session_name
) {
319 free(channel_info
->session_name
);
321 if (channel_info
->channel_name
) {
322 free(channel_info
->channel_name
);
328 struct channel_info
*channel_info_copy(struct channel_info
*channel_info
)
330 struct channel_info
*copy
= zmalloc(sizeof(*channel_info
));
332 assert(channel_info
);
333 assert(channel_info
->session_name
);
334 assert(channel_info
->channel_name
);
340 memcpy(copy
, channel_info
, sizeof(*channel_info
));
341 copy
->session_name
= NULL
;
342 copy
->channel_name
= NULL
;
344 copy
->session_name
= strdup(channel_info
->session_name
);
345 if (!copy
->session_name
) {
348 copy
->channel_name
= strdup(channel_info
->channel_name
);
349 if (!copy
->channel_name
) {
352 cds_lfht_node_init(&channel_info
->channels_ht_node
);
356 channel_info_destroy(copy
);
361 int notification_thread_client_subscribe(struct notification_client
*client
,
362 struct lttng_condition
*condition
,
363 struct notification_thread_state
*state
,
364 enum lttng_notification_channel_status
*_status
)
367 struct cds_lfht_iter iter
;
368 struct cds_lfht_node
*node
;
369 struct notification_client_list
*client_list
;
370 struct lttng_condition_list_element
*condition_list_element
= NULL
;
371 struct notification_client_list_element
*client_list_element
= NULL
;
372 enum lttng_notification_channel_status status
=
373 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
376 * Ensure that the client has not already subscribed to this condition
379 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
380 if (lttng_condition_is_equal(condition_list_element
->condition
,
382 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
387 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
388 if (!condition_list_element
) {
392 client_list_element
= zmalloc(sizeof(*client_list_element
));
393 if (!client_list_element
) {
401 * Add the newly-subscribed condition to the client's subscription list.
403 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
404 condition_list_element
->condition
= condition
;
405 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
408 * Add the client to the list of clients interested in a given trigger
409 * if a "notification" trigger with a corresponding condition was
412 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
413 lttng_condition_hash(condition
),
414 match_client_list_condition
,
417 node
= cds_lfht_iter_get_node(&iter
);
419 free(client_list_element
);
423 client_list
= caa_container_of(node
, struct notification_client_list
,
424 notification_trigger_ht_node
);
425 client_list_element
->client
= client
;
426 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
427 cds_list_add(&client_list_element
->node
, &client_list
->list
);
436 free(condition_list_element
);
437 free(client_list_element
);
442 int notification_thread_client_unsubscribe(
443 struct notification_client
*client
,
444 struct lttng_condition
*condition
,
445 struct notification_thread_state
*state
,
446 enum lttng_notification_channel_status
*_status
)
448 struct cds_lfht_iter iter
;
449 struct cds_lfht_node
*node
;
450 struct notification_client_list
*client_list
;
451 struct lttng_condition_list_element
*condition_list_element
,
453 struct notification_client_list_element
*client_list_element
,
455 bool condition_found
= false;
456 enum lttng_notification_channel_status status
=
457 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
459 /* Remove the condition from the client's condition list. */
460 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
461 &client
->condition_list
, node
) {
462 if (!lttng_condition_is_equal(condition_list_element
->condition
,
467 cds_list_del(&condition_list_element
->node
);
469 * The caller may be iterating on the client's conditions to
470 * tear down a client's connection. In this case, the condition
471 * will be destroyed at the end.
473 if (condition
!= condition_list_element
->condition
) {
474 lttng_condition_destroy(
475 condition_list_element
->condition
);
477 free(condition_list_element
);
478 condition_found
= true;
482 if (!condition_found
) {
483 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
488 * Remove the client from the list of clients interested the trigger
489 * matching the condition.
492 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
493 lttng_condition_hash(condition
),
494 match_client_list_condition
,
497 node
= cds_lfht_iter_get_node(&iter
);
502 client_list
= caa_container_of(node
, struct notification_client_list
,
503 notification_trigger_ht_node
);
504 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
505 &client_list
->list
, node
) {
506 if (client_list_element
->client
->socket
!= client
->socket
) {
509 cds_list_del(&client_list_element
->node
);
510 free(client_list_element
);
516 lttng_condition_destroy(condition
);
524 void notification_client_destroy(struct notification_client
*client
,
525 struct notification_thread_state
*state
)
527 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
533 /* Release all conditions to which the client was subscribed. */
534 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
535 &client
->condition_list
, node
) {
536 (void) notification_thread_client_unsubscribe(client
,
537 condition_list_element
->condition
, state
, NULL
);
540 if (client
->socket
>= 0) {
541 (void) lttcomm_close_unix_sock(client
->socket
);
543 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
544 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
549 * Call with rcu_read_lock held (and hold for the lifetime of the returned
553 struct notification_client
*get_client_from_socket(int socket
,
554 struct notification_thread_state
*state
)
556 struct cds_lfht_iter iter
;
557 struct cds_lfht_node
*node
;
558 struct notification_client
*client
= NULL
;
560 cds_lfht_lookup(state
->client_socket_ht
,
561 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
563 (void *) (unsigned long) socket
,
565 node
= cds_lfht_iter_get_node(&iter
);
570 client
= caa_container_of(node
, struct notification_client
,
571 client_socket_ht_node
);
577 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
578 struct channel_info
*info
)
580 enum lttng_condition_status status
;
581 struct lttng_condition
*condition
;
582 const char *trigger_session_name
= NULL
;
583 const char *trigger_channel_name
= NULL
;
584 enum lttng_domain_type trigger_domain
;
586 condition
= lttng_trigger_get_condition(trigger
);
591 switch (lttng_condition_get_type(condition
)) {
592 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
593 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
599 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
601 assert(status
== LTTNG_CONDITION_STATUS_OK
);
602 if (info
->key
.domain
!= trigger_domain
) {
606 status
= lttng_condition_buffer_usage_get_session_name(
607 condition
, &trigger_session_name
);
608 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_session_name
);
610 status
= lttng_condition_buffer_usage_get_channel_name(
611 condition
, &trigger_channel_name
);
612 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_channel_name
);
614 if (strcmp(info
->session_name
, trigger_session_name
)) {
617 if (strcmp(info
->channel_name
, trigger_channel_name
)) {
627 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
628 struct notification_client
*client
)
630 bool applies
= false;
631 struct lttng_condition_list_element
*condition_list_element
;
633 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
635 applies
= lttng_condition_is_equal(
636 condition_list_element
->condition
,
637 lttng_trigger_get_condition(trigger
));
646 unsigned long hash_channel_key(struct channel_key
*key
)
648 return hash_key_u64(&key
->key
, lttng_ht_seed
) ^ hash_key_ulong(
649 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
653 int handle_notification_thread_command_add_channel(
654 struct notification_thread_state
*state
,
655 struct channel_info
*channel_info
,
656 enum lttng_error_code
*cmd_result
)
658 struct cds_list_head trigger_list
;
659 struct channel_info
*new_channel_info
;
660 struct channel_key
*channel_key
;
661 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
662 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
663 int trigger_count
= 0;
664 struct cds_lfht_iter iter
;
666 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
667 channel_info
->channel_name
, channel_info
->session_name
,
668 channel_info
->key
.key
, channel_info
->key
.domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
670 CDS_INIT_LIST_HEAD(&trigger_list
);
672 new_channel_info
= channel_info_copy(channel_info
);
673 if (!new_channel_info
) {
677 channel_key
= &new_channel_info
->key
;
679 /* Build a list of all triggers applying to the new channel. */
680 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
682 struct lttng_trigger_list_element
*new_element
;
684 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
689 new_element
= zmalloc(sizeof(*new_element
));
693 CDS_INIT_LIST_HEAD(&new_element
->node
);
694 new_element
->trigger
= trigger_ht_element
->trigger
;
695 cds_list_add(&new_element
->node
, &trigger_list
);
699 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
701 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
702 if (!channel_trigger_list
) {
705 channel_trigger_list
->channel_key
= *channel_key
;
706 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
707 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
708 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
711 /* Add channel to the channel_ht which owns the channel_infos. */
712 cds_lfht_add(state
->channels_ht
,
713 hash_channel_key(channel_key
),
714 &new_channel_info
->channels_ht_node
);
716 * Add the list of triggers associated with this channel to the
717 * channel_triggers_ht.
719 cds_lfht_add(state
->channel_triggers_ht
,
720 hash_channel_key(channel_key
),
721 &channel_trigger_list
->channel_triggers_ht_node
);
723 *cmd_result
= LTTNG_OK
;
726 /* Empty trigger list */
727 channel_info_destroy(new_channel_info
);
732 int handle_notification_thread_command_remove_channel(
733 struct notification_thread_state
*state
,
734 uint64_t channel_key
, enum lttng_domain_type domain
,
735 enum lttng_error_code
*cmd_result
)
737 struct cds_lfht_node
*node
;
738 struct cds_lfht_iter iter
;
739 struct lttng_channel_trigger_list
*trigger_list
;
740 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
741 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
742 struct channel_info
*channel_info
;
744 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
745 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
749 cds_lfht_lookup(state
->channel_triggers_ht
,
750 hash_channel_key(&key
),
751 match_channel_trigger_list
,
754 node
= cds_lfht_iter_get_node(&iter
);
756 * There is a severe internal error if we are being asked to remove a
757 * channel that doesn't exist.
760 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
764 /* Free the list of triggers associated with this channel. */
765 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
766 channel_triggers_ht_node
);
767 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
768 &trigger_list
->list
, node
) {
769 cds_list_del(&trigger_list_element
->node
);
770 free(trigger_list_element
);
772 cds_lfht_del(state
->channel_triggers_ht
, node
);
775 /* Free sampled channel state. */
776 cds_lfht_lookup(state
->channel_state_ht
,
777 hash_channel_key(&key
),
778 match_channel_state_sample
,
781 node
= cds_lfht_iter_get_node(&iter
);
783 * This is expected to be NULL if the channel is destroyed before we
787 struct channel_state_sample
*sample
= caa_container_of(node
,
788 struct channel_state_sample
,
789 channel_state_ht_node
);
791 cds_lfht_del(state
->channel_state_ht
, node
);
795 /* Remove the channel from the channels_ht and free it. */
796 cds_lfht_lookup(state
->channels_ht
,
797 hash_channel_key(&key
),
801 node
= cds_lfht_iter_get_node(&iter
);
803 channel_info
= caa_container_of(node
, struct channel_info
,
805 cds_lfht_del(state
->channels_ht
, node
);
806 channel_info_destroy(channel_info
);
809 *cmd_result
= LTTNG_OK
;
814 int condition_is_supported(struct lttng_condition
*condition
)
818 switch (lttng_condition_get_type(condition
)) {
819 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
820 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
822 enum lttng_domain_type domain
;
824 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
831 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
837 * Older kernel tracers don't expose the API to monitor their
838 * buffers. Therefore, we reject triggers that require that
839 * mechanism to be available to be evaluated.
841 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
853 * FIXME A client's credentials are not checked when registering a trigger, nor
854 * are they stored alongside with the trigger.
856 * The effects of this are benign since:
857 * - The client will succeed in registering the trigger, as it is valid,
858 * - The trigger will, internally, be bound to the channel,
859 * - The notifications will not be sent since the client's credentials
860 * are checked against the channel at that moment.
862 * If this function returns a non-zero value, it means something is
863 * fundamentally and the whole subsystem/thread will be torn down.
865 * If a non-fatal error occurs, just set the cmd_result to the appropriate
869 int handle_notification_thread_command_register_trigger(
870 struct notification_thread_state
*state
,
871 struct lttng_trigger
*trigger
,
872 enum lttng_error_code
*cmd_result
)
875 struct lttng_condition
*condition
;
876 struct notification_client
*client
;
877 struct notification_client_list
*client_list
= NULL
;
878 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
879 struct notification_client_list_element
*client_list_element
, *tmp
;
880 struct cds_lfht_node
*node
;
881 struct cds_lfht_iter iter
;
882 struct channel_info
*channel
;
883 bool free_trigger
= true;
887 condition
= lttng_trigger_get_condition(trigger
);
890 ret
= condition_is_supported(condition
);
893 } else if (ret
== 0) {
894 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
897 /* Feature is supported, continue. */
901 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
902 if (!trigger_ht_element
) {
907 /* Add trigger to the trigger_ht. */
908 cds_lfht_node_init(&trigger_ht_element
->node
);
909 trigger_ht_element
->trigger
= trigger
;
911 node
= cds_lfht_add_unique(state
->triggers_ht
,
912 lttng_condition_hash(condition
),
915 &trigger_ht_element
->node
);
916 if (node
!= &trigger_ht_element
->node
) {
917 /* Not a fatal error, simply report it to the client. */
918 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
919 goto error_free_ht_element
;
923 * Ownership of the trigger and of its wrapper was transfered to
926 trigger_ht_element
= NULL
;
927 free_trigger
= false;
930 * The rest only applies to triggers that have a "notify" action.
931 * It is not skipped as this is the only action type currently
934 client_list
= zmalloc(sizeof(*client_list
));
937 goto error_free_ht_element
;
939 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
940 CDS_INIT_LIST_HEAD(&client_list
->list
);
941 client_list
->trigger
= trigger
;
943 /* Build a list of clients to which this new trigger applies. */
944 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
945 client_socket_ht_node
) {
946 if (!trigger_applies_to_client(trigger
, client
)) {
950 client_list_element
= zmalloc(sizeof(*client_list_element
));
951 if (!client_list_element
) {
953 goto error_free_client_list
;
955 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
956 client_list_element
->client
= client
;
957 cds_list_add(&client_list_element
->node
, &client_list
->list
);
960 cds_lfht_add(state
->notification_trigger_clients_ht
,
961 lttng_condition_hash(condition
),
962 &client_list
->notification_trigger_ht_node
);
964 * Client list ownership transferred to the
965 * notification_trigger_clients_ht.
970 * Add the trigger to list of triggers bound to the channels currently
973 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
975 struct lttng_trigger_list_element
*trigger_list_element
;
976 struct lttng_channel_trigger_list
*trigger_list
;
978 if (!trigger_applies_to_channel(trigger
, channel
)) {
982 cds_lfht_lookup(state
->channel_triggers_ht
,
983 hash_channel_key(&channel
->key
),
984 match_channel_trigger_list
,
987 node
= cds_lfht_iter_get_node(&iter
);
989 /* Free the list of triggers associated with this channel. */
990 trigger_list
= caa_container_of(node
,
991 struct lttng_channel_trigger_list
,
992 channel_triggers_ht_node
);
994 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
995 if (!trigger_list_element
) {
997 goto error_free_client_list
;
999 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1000 trigger_list_element
->trigger
= trigger
;
1001 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1002 /* A trigger can only apply to one channel. */
1006 *cmd_result
= LTTNG_OK
;
1007 error_free_client_list
:
1009 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1010 &client_list
->list
, node
) {
1011 free(client_list_element
);
1015 error_free_ht_element
:
1016 free(trigger_ht_element
);
1019 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1021 lttng_condition_destroy(condition
);
1022 lttng_action_destroy(action
);
1023 lttng_trigger_destroy(trigger
);
1030 int handle_notification_thread_command_unregister_trigger(
1031 struct notification_thread_state
*state
,
1032 struct lttng_trigger
*trigger
,
1033 enum lttng_error_code
*_cmd_reply
)
1035 struct cds_lfht_iter iter
;
1036 struct cds_lfht_node
*node
, *triggers_ht_node
;
1037 struct lttng_channel_trigger_list
*trigger_list
;
1038 struct notification_client_list
*client_list
;
1039 struct notification_client_list_element
*client_list_element
, *tmp
;
1040 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1041 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1043 struct lttng_action
*action
;
1044 enum lttng_error_code cmd_reply
;
1048 cds_lfht_lookup(state
->triggers_ht
,
1049 lttng_condition_hash(condition
),
1053 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1054 if (!triggers_ht_node
) {
1055 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1058 cmd_reply
= LTTNG_OK
;
1061 /* Remove trigger from channel_triggers_ht. */
1062 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1063 channel_triggers_ht_node
) {
1064 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1066 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1067 &trigger_list
->list
, node
) {
1068 struct lttng_condition
*current_condition
=
1069 lttng_trigger_get_condition(
1070 trigger_element
->trigger
);
1072 assert(current_condition
);
1073 if (!lttng_condition_is_equal(condition
,
1074 current_condition
)) {
1078 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1079 cds_list_del(&trigger_element
->node
);
1084 * Remove and release the client list from
1085 * notification_trigger_clients_ht.
1087 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1088 lttng_condition_hash(condition
),
1092 node
= cds_lfht_iter_get_node(&iter
);
1094 client_list
= caa_container_of(node
, struct notification_client_list
,
1095 notification_trigger_ht_node
);
1096 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1097 &client_list
->list
, node
) {
1098 free(client_list_element
);
1100 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1103 /* Remove trigger from triggers_ht. */
1104 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1105 struct lttng_trigger_ht_element
, node
);
1106 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1108 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1109 lttng_condition_destroy(condition
);
1110 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1111 lttng_action_destroy(action
);
1112 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1113 free(trigger_ht_element
);
1117 *_cmd_reply
= cmd_reply
;
1122 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1123 int handle_notification_thread_command(
1124 struct notification_thread_handle
*handle
,
1125 struct notification_thread_state
*state
)
1129 struct notification_thread_command
*cmd
;
1131 /* Read event_fd to put it back into a quiescent state. */
1132 ret
= read(handle
->cmd_queue
.event_fd
, &counter
, sizeof(counter
));
1137 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1138 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1139 struct notification_thread_command
, cmd_list_node
);
1140 switch (cmd
->type
) {
1141 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1142 DBG("[notification-thread] Received register trigger command");
1143 ret
= handle_notification_thread_command_register_trigger(
1144 state
, cmd
->parameters
.trigger
,
1147 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1148 DBG("[notification-thread] Received unregister trigger command");
1149 ret
= handle_notification_thread_command_unregister_trigger(
1150 state
, cmd
->parameters
.trigger
,
1153 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1154 DBG("[notification-thread] Received add channel command");
1155 ret
= handle_notification_thread_command_add_channel(
1156 state
, &cmd
->parameters
.add_channel
,
1159 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1160 DBG("[notification-thread] Received remove channel command");
1161 ret
= handle_notification_thread_command_remove_channel(
1162 state
, cmd
->parameters
.remove_channel
.key
,
1163 cmd
->parameters
.remove_channel
.domain
,
1166 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1167 DBG("[notification-thread] Received quit command");
1168 cmd
->reply_code
= LTTNG_OK
;
1172 ERR("[notification-thread] Unknown internal command received");
1180 cds_list_del(&cmd
->cmd_list_node
);
1181 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1182 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1185 /* Wake-up and return a fatal error to the calling thread. */
1186 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1187 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1188 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1190 /* Indicate a fatal error to the caller. */
1195 unsigned long hash_client_socket(int socket
)
1197 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1201 int socket_set_non_blocking(int socket
)
1205 /* Set the pipe as non-blocking. */
1206 ret
= fcntl(socket
, F_GETFL
, 0);
1208 PERROR("fcntl get socket flags");
1213 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1215 PERROR("fcntl set O_NONBLOCK socket flag");
1218 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1224 int client_reset_inbound_state(struct notification_client
*client
)
1228 ret
= lttng_dynamic_buffer_set_size(
1229 &client
->communication
.inbound
.buffer
, 0);
1232 client
->communication
.inbound
.bytes_to_receive
=
1233 sizeof(struct lttng_notification_channel_message
);
1234 client
->communication
.inbound
.msg_type
=
1235 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1236 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1237 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1238 ret
= lttng_dynamic_buffer_set_size(
1239 &client
->communication
.inbound
.buffer
,
1240 client
->communication
.inbound
.bytes_to_receive
);
1244 int handle_notification_thread_client_connect(
1245 struct notification_thread_state
*state
)
1248 struct notification_client
*client
;
1250 DBG("[notification-thread] Handling new notification channel client connection");
1252 client
= zmalloc(sizeof(*client
));
1258 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1259 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1260 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1261 client
->communication
.inbound
.expect_creds
= true;
1262 ret
= client_reset_inbound_state(client
);
1264 ERR("[notification-thread] Failed to reset client communication's inbound state");
1269 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1271 ERR("[notification-thread] Failed to accept new notification channel client connection");
1276 client
->socket
= ret
;
1278 ret
= socket_set_non_blocking(client
->socket
);
1280 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1284 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1286 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1291 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1292 LPOLLIN
| LPOLLERR
|
1293 LPOLLHUP
| LPOLLRDHUP
);
1295 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1299 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1303 cds_lfht_add(state
->client_socket_ht
,
1304 hash_client_socket(client
->socket
),
1305 &client
->client_socket_ht_node
);
1310 notification_client_destroy(client
, state
);
1314 int handle_notification_thread_client_disconnect(
1316 struct notification_thread_state
*state
)
1319 struct notification_client
*client
;
1322 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1324 client
= get_client_from_socket(client_socket
, state
);
1326 /* Internal state corruption, fatal error. */
1327 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1333 ret
= lttng_poll_del(&state
->events
, client_socket
);
1335 ERR("[notification-thread] Failed to remove client socket from poll set");
1337 cds_lfht_del(state
->client_socket_ht
,
1338 &client
->client_socket_ht_node
);
1339 notification_client_destroy(client
, state
);
1345 int handle_notification_thread_client_disconnect_all(
1346 struct notification_thread_state
*state
)
1348 struct cds_lfht_iter iter
;
1349 struct notification_client
*client
;
1350 bool error_encoutered
= false;
1353 DBG("[notification-thread] Closing all client connections");
1354 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1355 client_socket_ht_node
) {
1358 ret
= handle_notification_thread_client_disconnect(
1359 client
->socket
, state
);
1361 error_encoutered
= true;
1365 return error_encoutered
? 1 : 0;
1368 int handle_notification_thread_trigger_unregister_all(
1369 struct notification_thread_state
*state
)
1371 bool error_occurred
= false;
1372 struct cds_lfht_iter iter
;
1373 struct lttng_trigger_ht_element
*trigger_ht_element
;
1375 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1377 int ret
= handle_notification_thread_command_unregister_trigger(
1378 state
, trigger_ht_element
->trigger
, NULL
);
1380 error_occurred
= true;
1383 return error_occurred
? -1 : 0;
1387 int client_flush_outgoing_queue(struct notification_client
*client
,
1388 struct notification_thread_state
*state
)
1391 size_t to_send_count
;
1393 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1394 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1395 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1398 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1399 client
->communication
.outbound
.buffer
.data
,
1401 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1402 (ret
> 0 && ret
< to_send_count
)) {
1403 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1405 to_send_count
-= max(ret
, 0);
1407 memcpy(client
->communication
.outbound
.buffer
.data
,
1408 client
->communication
.outbound
.buffer
.data
+
1409 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1411 ret
= lttng_dynamic_buffer_set_size(
1412 &client
->communication
.outbound
.buffer
,
1419 * We want to be notified whenever there is buffer space
1420 * available to send the rest of the payload.
1422 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1423 CLIENT_POLL_MASK_IN_OUT
);
1427 } else if (ret
< 0) {
1428 /* Generic error, disconnect the client. */
1429 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1431 ret
= handle_notification_thread_client_disconnect(
1432 client
->socket
, state
);
1437 /* No error and flushed the queue completely. */
1438 ret
= lttng_dynamic_buffer_set_size(
1439 &client
->communication
.outbound
.buffer
, 0);
1443 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1444 CLIENT_POLL_MASK_IN
);
1449 client
->communication
.outbound
.queued_command_reply
= false;
1450 client
->communication
.outbound
.dropped_notification
= false;
1459 int client_send_command_reply(struct notification_client
*client
,
1460 struct notification_thread_state
*state
,
1461 enum lttng_notification_channel_status status
)
1464 struct lttng_notification_channel_command_reply reply
= {
1465 .status
= (int8_t) status
,
1467 struct lttng_notification_channel_message msg
= {
1468 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1469 .size
= sizeof(reply
),
1471 char buffer
[sizeof(msg
) + sizeof(reply
)];
1473 if (client
->communication
.outbound
.queued_command_reply
) {
1474 /* Protocol error. */
1478 memcpy(buffer
, &msg
, sizeof(msg
));
1479 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1480 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1482 /* Enqueue buffer to outgoing queue and flush it. */
1483 ret
= lttng_dynamic_buffer_append(
1484 &client
->communication
.outbound
.buffer
,
1485 buffer
, sizeof(buffer
));
1490 ret
= client_flush_outgoing_queue(client
, state
);
1495 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1496 /* Queue could not be emptied. */
1497 client
->communication
.outbound
.queued_command_reply
= true;
1506 int client_dispatch_message(struct notification_client
*client
,
1507 struct notification_thread_state
*state
)
1511 if (client
->communication
.inbound
.msg_type
!=
1512 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1513 client
->communication
.inbound
.msg_type
!=
1514 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1515 !client
->validated
) {
1516 WARN("[notification-thread] client attempted a command before handshake");
1521 switch (client
->communication
.inbound
.msg_type
) {
1522 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1525 * Receiving message header. The function will be called again
1526 * once the rest of the message as been received and can be
1529 const struct lttng_notification_channel_message
*msg
;
1531 assert(sizeof(*msg
) ==
1532 client
->communication
.inbound
.buffer
.size
);
1533 msg
= (const struct lttng_notification_channel_message
*)
1534 client
->communication
.inbound
.buffer
.data
;
1536 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1537 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1542 switch (msg
->type
) {
1543 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1544 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1545 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1549 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1553 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
1554 client
->communication
.inbound
.msg_type
=
1555 (enum lttng_notification_channel_message_type
) msg
->type
;
1556 ret
= lttng_dynamic_buffer_set_size(
1557 &client
->communication
.inbound
.buffer
, msg
->size
);
1563 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1565 struct lttng_notification_channel_command_handshake
*handshake_client
;
1566 struct lttng_notification_channel_command_handshake handshake_reply
= {
1567 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
1568 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
1570 struct lttng_notification_channel_message msg_header
= {
1571 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
1572 .size
= sizeof(handshake_reply
),
1574 enum lttng_notification_channel_status status
=
1575 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1576 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
1578 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
1579 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
1580 sizeof(handshake_reply
));
1583 (struct lttng_notification_channel_command_handshake
*)
1584 client
->communication
.inbound
.buffer
.data
;
1585 client
->major
= handshake_client
->major
;
1586 client
->minor
= handshake_client
->minor
;
1587 if (!client
->communication
.inbound
.creds_received
) {
1588 ERR("[notification-thread] No credentials received from client");
1593 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
1594 &client
->communication
.inbound
.creds
);
1595 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
1596 &client
->communication
.inbound
.creds
);
1597 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
1598 client
->uid
, client
->gid
, (int) client
->major
,
1599 (int) client
->minor
);
1601 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
1602 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
1605 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
1606 send_buffer
, sizeof(send_buffer
));
1608 ERR("[notification-thread] Failed to send protocol version to notification channel client");
1612 ret
= client_flush_outgoing_queue(client
, state
);
1617 ret
= client_send_command_reply(client
, state
, status
);
1619 ERR("[notification-thread] Failed to send reply to notification channel client");
1623 /* Set reception state to receive the next message header. */
1624 ret
= client_reset_inbound_state(client
);
1626 ERR("[notification-thread] Failed to reset client communication's inbound state");
1629 client
->validated
= true;
1632 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1633 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1635 struct lttng_condition
*condition
;
1636 enum lttng_notification_channel_status status
=
1637 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1638 const struct lttng_buffer_view condition_view
=
1639 lttng_buffer_view_from_dynamic_buffer(
1640 &client
->communication
.inbound
.buffer
,
1642 size_t expected_condition_size
=
1643 client
->communication
.inbound
.buffer
.size
;
1645 ret
= lttng_condition_create_from_buffer(&condition_view
,
1647 if (ret
!= expected_condition_size
) {
1648 ERR("[notification-thread] Malformed condition received from client");
1652 if (client
->communication
.inbound
.msg_type
==
1653 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
1655 * FIXME The current state should be evaluated on
1658 ret
= notification_thread_client_subscribe(client
,
1659 condition
, state
, &status
);
1661 ret
= notification_thread_client_unsubscribe(client
,
1662 condition
, state
, &status
);
1668 ret
= client_send_command_reply(client
, state
, status
);
1670 ERR("[notification-thread] Failed to send reply to notification channel client");
1674 /* Set reception state to receive the next message header. */
1675 ret
= client_reset_inbound_state(client
);
1677 ERR("[notification-thread] Failed to reset client communication's inbound state");
1689 /* Incoming data from client. */
1690 int handle_notification_thread_client_in(
1691 struct notification_thread_state
*state
, int socket
)
1694 struct notification_client
*client
;
1698 client
= get_client_from_socket(socket
, state
);
1700 /* Internal error, abort. */
1705 offset
= client
->communication
.inbound
.buffer
.size
-
1706 client
->communication
.inbound
.bytes_to_receive
;
1707 if (client
->communication
.inbound
.expect_creds
) {
1708 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
1709 client
->communication
.inbound
.buffer
.data
+ offset
,
1710 client
->communication
.inbound
.bytes_to_receive
,
1711 &client
->communication
.inbound
.creds
);
1713 client
->communication
.inbound
.expect_creds
= false;
1714 client
->communication
.inbound
.creds_received
= true;
1717 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
1718 client
->communication
.inbound
.buffer
.data
+ offset
,
1719 client
->communication
.inbound
.bytes_to_receive
);
1722 goto error_disconnect_client
;
1725 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
1726 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
1727 ret
= client_dispatch_message(client
, state
);
1730 * Only returns an error if this client must be
1733 goto error_disconnect_client
;
1740 error_disconnect_client
:
1741 ret
= handle_notification_thread_client_disconnect(socket
, state
);
1745 /* Client ready to receive outgoing data. */
1746 int handle_notification_thread_client_out(
1747 struct notification_thread_state
*state
, int socket
)
1750 struct notification_client
*client
;
1752 client
= get_client_from_socket(socket
, state
);
1754 /* Internal error, abort. */
1759 ret
= client_flush_outgoing_queue(client
, state
);
1768 bool evaluate_buffer_usage_condition(struct lttng_condition
*condition
,
1769 struct channel_state_sample
*sample
, uint64_t buffer_capacity
)
1771 bool result
= false;
1773 enum lttng_condition_type condition_type
;
1774 struct lttng_condition_buffer_usage
*use_condition
= container_of(
1775 condition
, struct lttng_condition_buffer_usage
,
1782 if (use_condition
->threshold_bytes
.set
) {
1783 threshold
= use_condition
->threshold_bytes
.value
;
1786 * Threshold was expressed as a ratio.
1788 * TODO the threshold (in bytes) of conditions expressed
1789 * as a ratio of total buffer size could be cached to
1790 * forego this double-multiplication or it could be performed
1791 * as fixed-point math.
1793 * Note that caching should accomodate the case where the
1794 * condition applies to multiple channels (i.e. don't assume
1795 * that all channels matching my_chann* have the same size...)
1797 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
1798 (double) buffer_capacity
);
1801 condition_type
= lttng_condition_get_type(condition
);
1802 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
1803 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1804 threshold
, sample
->highest_usage
);
1807 * The low condition should only be triggered once _all_ of the
1808 * streams in a channel have gone below the "low" threshold.
1810 if (sample
->highest_usage
<= threshold
) {
1814 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1815 threshold
, sample
->highest_usage
);
1818 * For high buffer usage scenarios, we want to trigger whenever
1819 * _any_ of the streams has reached the "high" threshold.
1821 if (sample
->highest_usage
>= threshold
) {
1830 int evaluate_condition(struct lttng_condition
*condition
,
1831 struct lttng_evaluation
**evaluation
,
1832 struct notification_thread_state
*state
,
1833 struct channel_state_sample
*previous_sample
,
1834 struct channel_state_sample
*latest_sample
,
1835 uint64_t buffer_capacity
)
1838 enum lttng_condition_type condition_type
;
1839 bool previous_sample_result
;
1840 bool latest_sample_result
;
1842 condition_type
= lttng_condition_get_type(condition
);
1843 /* No other condition type supported for the moment. */
1844 assert(condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
1845 condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
1847 previous_sample_result
= evaluate_buffer_usage_condition(condition
,
1848 previous_sample
, buffer_capacity
);
1849 latest_sample_result
= evaluate_buffer_usage_condition(condition
,
1850 latest_sample
, buffer_capacity
);
1852 if (!latest_sample_result
||
1853 (previous_sample_result
== latest_sample_result
)) {
1855 * Only trigger on a condition evaluation transition.
1857 * NOTE: This edge-triggered logic may not be appropriate for
1858 * future condition types.
1863 if (evaluation
&& latest_sample_result
) {
1864 *evaluation
= lttng_evaluation_buffer_usage_create(
1866 latest_sample
->highest_usage
,
1878 int client_enqueue_dropped_notification(struct notification_client
*client
,
1879 struct notification_thread_state
*state
)
1882 struct lttng_notification_channel_message msg
= {
1883 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
1887 ret
= lttng_dynamic_buffer_append(
1888 &client
->communication
.outbound
.buffer
, &msg
,
1894 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
1895 struct lttng_evaluation
*evaluation
,
1896 struct notification_client_list
* client_list
,
1897 struct notification_thread_state
*state
,
1898 uid_t channel_uid
, gid_t channel_gid
)
1901 struct lttng_dynamic_buffer msg_buffer
;
1902 struct notification_client_list_element
*client_list_element
, *tmp
;
1903 struct lttng_notification
*notification
;
1904 struct lttng_condition
*condition
;
1905 ssize_t expected_notification_size
, notification_size
;
1906 struct lttng_notification_channel_message msg
;
1908 lttng_dynamic_buffer_init(&msg_buffer
);
1910 condition
= lttng_trigger_get_condition(trigger
);
1913 notification
= lttng_notification_create(condition
, evaluation
);
1914 if (!notification
) {
1919 expected_notification_size
= lttng_notification_serialize(notification
,
1921 if (expected_notification_size
< 0) {
1922 ERR("[notification-thread] Failed to get size of serialized notification");
1927 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
1928 msg
.size
= (uint32_t) expected_notification_size
;
1929 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg
, sizeof(msg
));
1934 ret
= lttng_dynamic_buffer_set_size(&msg_buffer
,
1935 msg_buffer
.size
+ expected_notification_size
);
1940 notification_size
= lttng_notification_serialize(notification
,
1941 msg_buffer
.data
+ sizeof(msg
));
1942 if (notification_size
!= expected_notification_size
) {
1943 ERR("[notification-thread] Failed to serialize notification");
1948 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1949 &client_list
->list
, node
) {
1950 struct notification_client
*client
=
1951 client_list_element
->client
;
1953 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
1955 /* Client is not allowed to monitor this channel. */
1956 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
1960 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
1961 client
->socket
, msg_buffer
.size
);
1962 if (client
->communication
.outbound
.buffer
.size
) {
1964 * Outgoing data is already buffered for this client;
1965 * drop the notification and enqueue a "dropped
1966 * notification" message if this is the first dropped
1967 * notification since the socket spilled-over to the
1970 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
1972 if (!client
->communication
.outbound
.dropped_notification
) {
1973 client
->communication
.outbound
.dropped_notification
= true;
1974 ret
= client_enqueue_dropped_notification(
1983 ret
= lttng_dynamic_buffer_append_buffer(
1984 &client
->communication
.outbound
.buffer
,
1990 ret
= client_flush_outgoing_queue(client
, state
);
1997 lttng_notification_destroy(notification
);
1998 lttng_dynamic_buffer_reset(&msg_buffer
);
2002 int handle_notification_thread_channel_sample(
2003 struct notification_thread_state
*state
, int pipe
,
2004 enum lttng_domain_type domain
)
2007 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2008 struct channel_state_sample previous_sample
, latest_sample
;
2009 struct channel_info
*channel_info
;
2010 struct cds_lfht_node
*node
;
2011 struct cds_lfht_iter iter
;
2012 struct lttng_channel_trigger_list
*trigger_list
;
2013 struct lttng_trigger_list_element
*trigger_list_element
;
2014 bool previous_sample_available
= false;
2017 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2018 * ensuring that read/write of sampling messages are atomic.
2020 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2021 if (ret
!= sizeof(sample_msg
)) {
2022 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2029 latest_sample
.key
.key
= sample_msg
.key
;
2030 latest_sample
.key
.domain
= domain
;
2031 latest_sample
.highest_usage
= sample_msg
.highest
;
2032 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2036 /* Retrieve the channel's informations */
2037 cds_lfht_lookup(state
->channels_ht
,
2038 hash_channel_key(&latest_sample
.key
),
2042 node
= cds_lfht_iter_get_node(&iter
);
2045 * Not an error since the consumer can push a sample to the pipe
2046 * and the rest of the session daemon could notify us of the
2047 * channel's destruction before we get a chance to process that
2050 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2051 latest_sample
.key
.key
,
2052 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2056 channel_info
= caa_container_of(node
, struct channel_info
,
2058 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
")",
2059 channel_info
->channel_name
,
2060 latest_sample
.key
.key
,
2061 channel_info
->session_name
,
2062 latest_sample
.highest_usage
,
2063 latest_sample
.lowest_usage
);
2065 /* Retrieve the channel's last sample, if it exists, and update it. */
2066 cds_lfht_lookup(state
->channel_state_ht
,
2067 hash_channel_key(&latest_sample
.key
),
2068 match_channel_state_sample
,
2071 node
= cds_lfht_iter_get_node(&iter
);
2073 struct channel_state_sample
*stored_sample
;
2075 /* Update the sample stored. */
2076 stored_sample
= caa_container_of(node
,
2077 struct channel_state_sample
,
2078 channel_state_ht_node
);
2079 memcpy(&previous_sample
, stored_sample
,
2080 sizeof(previous_sample
));
2081 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2082 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2083 previous_sample_available
= true;
2086 * This is the channel's first sample, allocate space for and
2087 * store the new sample.
2089 struct channel_state_sample
*stored_sample
;
2091 stored_sample
= zmalloc(sizeof(*stored_sample
));
2092 if (!stored_sample
) {
2097 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2098 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2099 cds_lfht_add(state
->channel_state_ht
,
2100 hash_channel_key(&stored_sample
->key
),
2101 &stored_sample
->channel_state_ht_node
);
2104 /* Find triggers associated with this channel. */
2105 cds_lfht_lookup(state
->channel_triggers_ht
,
2106 hash_channel_key(&latest_sample
.key
),
2107 match_channel_trigger_list
,
2110 node
= cds_lfht_iter_get_node(&iter
);
2115 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2116 channel_triggers_ht_node
);
2117 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2119 struct lttng_condition
*condition
;
2120 struct lttng_action
*action
;
2121 struct lttng_trigger
*trigger
;
2122 struct notification_client_list
*client_list
;
2123 struct lttng_evaluation
*evaluation
= NULL
;
2125 trigger
= trigger_list_element
->trigger
;
2126 condition
= lttng_trigger_get_condition(trigger
);
2128 action
= lttng_trigger_get_action(trigger
);
2130 /* Notify actions are the only type currently supported. */
2131 assert(lttng_action_get_type(action
) ==
2132 LTTNG_ACTION_TYPE_NOTIFY
);
2135 * Check if any client is subscribed to the result of this
2138 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2139 lttng_condition_hash(condition
),
2143 node
= cds_lfht_iter_get_node(&iter
);
2146 client_list
= caa_container_of(node
,
2147 struct notification_client_list
,
2148 notification_trigger_ht_node
);
2149 if (cds_list_empty(&client_list
->list
)) {
2151 * No clients interested in the evaluation's result,
2157 ret
= evaluate_condition(condition
, &evaluation
, state
,
2158 previous_sample_available
? &previous_sample
: NULL
,
2159 &latest_sample
, channel_info
->capacity
);
2168 /* Dispatch evaluation result to all clients. */
2169 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2170 evaluation
, client_list
, state
,
2171 channel_info
->uid
, channel_info
->gid
);