2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <urcu/rculfhash.h>
22 #include "notification-thread.h"
23 #include "notification-thread-events.h"
24 #include "notification-thread-commands.h"
25 #include <common/defaults.h>
26 #include <common/error.h>
27 #include <common/futex.h>
28 #include <common/unix.h>
29 #include <common/dynamic-buffer.h>
30 #include <common/hashtable/utils.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/macros.h>
33 #include <lttng/condition/condition.h>
34 #include <lttng/action/action.h>
35 #include <lttng/notification/notification-internal.h>
36 #include <lttng/condition/condition-internal.h>
37 #include <lttng/condition/buffer-usage-internal.h>
38 #include <lttng/notification/channel-internal.h>
45 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
46 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
48 struct lttng_trigger_list_element
{
49 struct lttng_trigger
*trigger
;
50 struct cds_list_head node
;
53 struct lttng_channel_trigger_list
{
54 struct channel_key channel_key
;
55 struct cds_list_head list
;
56 struct cds_lfht_node channel_triggers_ht_node
;
59 struct lttng_trigger_ht_element
{
60 struct lttng_trigger
*trigger
;
61 struct cds_lfht_node node
;
64 struct lttng_condition_list_element
{
65 struct lttng_condition
*condition
;
66 struct cds_list_head node
;
69 struct notification_client_list_element
{
70 struct notification_client
*client
;
71 struct cds_list_head node
;
74 struct notification_client_list
{
75 struct lttng_trigger
*trigger
;
76 struct cds_list_head list
;
77 struct cds_lfht_node notification_trigger_ht_node
;
80 struct notification_client
{
82 /* Client protocol version. */
87 * Indicates if the credentials and versions of the client has been
92 * Conditions to which the client's notification channel is subscribed.
93 * List of struct lttng_condition_list_node. The condition member is
94 * owned by the client.
96 struct cds_list_head condition_list
;
97 struct cds_lfht_node client_socket_ht_node
;
101 * During the reception of a message, the reception
102 * buffers' "size" is set to contain the current
103 * message's complete payload.
105 struct lttng_dynamic_buffer buffer
;
106 /* Bytes left to receive for the current message. */
107 size_t bytes_to_receive
;
108 /* Type of the message being received. */
109 enum lttng_notification_channel_message_type msg_type
;
111 * Indicates whether or not credentials are expected
116 * Indicates whether or not credentials were received
120 /* Only used during credentials reception. */
121 lttng_sock_cred creds
;
125 * Indicates whether or not a notification addressed to
126 * this client was dropped because a command reply was
129 * A notification is dropped whenever the buffer is not
132 bool dropped_notification
;
134 * Indicates whether or not a command reply is already
135 * buffered. In this case, it means that the client is
136 * not consuming command replies before emitting a new
137 * one. This could be caused by a protocol error or a
138 * misbehaving/malicious client.
140 bool queued_command_reply
;
141 struct lttng_dynamic_buffer buffer
;
146 struct channel_state_sample
{
147 struct channel_key key
;
148 struct cds_lfht_node channel_state_ht_node
;
149 uint64_t highest_usage
;
150 uint64_t lowest_usage
;
154 int match_client(struct cds_lfht_node
*node
, const void *key
)
156 /* This double-cast is intended to supress pointer-to-cast warning. */
157 int socket
= (int) (intptr_t) key
;
158 struct notification_client
*client
;
160 client
= caa_container_of(node
, struct notification_client
,
161 client_socket_ht_node
);
163 return !!(client
->socket
== socket
);
167 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
169 struct channel_key
*channel_key
= (struct channel_key
*) key
;
170 struct lttng_channel_trigger_list
*trigger_list
;
172 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
173 channel_triggers_ht_node
);
175 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
176 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
180 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
182 struct channel_key
*channel_key
= (struct channel_key
*) key
;
183 struct channel_state_sample
*sample
;
185 sample
= caa_container_of(node
, struct channel_state_sample
,
186 channel_state_ht_node
);
188 return !!((channel_key
->key
== sample
->key
.key
) &&
189 (channel_key
->domain
== sample
->key
.domain
));
193 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
195 struct channel_key
*channel_key
= (struct channel_key
*) key
;
196 struct channel_info
*channel_info
;
198 channel_info
= caa_container_of(node
, struct channel_info
,
201 return !!((channel_key
->key
== channel_info
->key
.key
) &&
202 (channel_key
->domain
== channel_info
->key
.domain
));
206 int match_condition(struct cds_lfht_node
*node
, const void *key
)
208 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
209 struct lttng_trigger_ht_element
*trigger
;
210 struct lttng_condition
*condition
;
212 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
214 condition
= lttng_trigger_get_condition(trigger
->trigger
);
217 return !!lttng_condition_is_equal(condition_key
, condition
);
221 int match_client_list(struct cds_lfht_node
*node
, const void *key
)
223 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
224 struct notification_client_list
*client_list
;
225 struct lttng_condition
*condition
;
226 struct lttng_condition
*condition_key
= lttng_trigger_get_condition(
229 assert(condition_key
);
231 client_list
= caa_container_of(node
, struct notification_client_list
,
232 notification_trigger_ht_node
);
233 condition
= lttng_trigger_get_condition(client_list
->trigger
);
235 return !!lttng_condition_is_equal(condition_key
, condition
);
239 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
241 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
242 struct notification_client_list
*client_list
;
243 struct lttng_condition
*condition
;
245 assert(condition_key
);
247 client_list
= caa_container_of(node
, struct notification_client_list
,
248 notification_trigger_ht_node
);
249 condition
= lttng_trigger_get_condition(client_list
->trigger
);
251 return !!lttng_condition_is_equal(condition_key
, condition
);
255 unsigned long lttng_condition_buffer_usage_hash(
256 struct lttng_condition
*_condition
)
258 unsigned long hash
= 0;
259 struct lttng_condition_buffer_usage
*condition
;
261 condition
= container_of(_condition
,
262 struct lttng_condition_buffer_usage
, parent
);
264 if (condition
->session_name
) {
265 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
267 if (condition
->channel_name
) {
268 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
270 if (condition
->domain
.set
) {
271 hash
^= hash_key_ulong(
272 (void *) condition
->domain
.type
,
275 if (condition
->threshold_ratio
.set
) {
278 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
279 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
280 } else if (condition
->threshold_ratio
.set
) {
283 val
= condition
->threshold_bytes
.value
;
284 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
290 * The lttng_condition hashing code is kept in this file (rather than
291 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
292 * don't want to link in liblttng-ctl.
295 unsigned long lttng_condition_hash(struct lttng_condition
*condition
)
297 switch (condition
->type
) {
298 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
299 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
300 return lttng_condition_buffer_usage_hash(condition
);
302 ERR("[notification-thread] Unexpected condition type caught");
308 void channel_info_destroy(struct channel_info
*channel_info
)
314 if (channel_info
->session_name
) {
315 free(channel_info
->session_name
);
317 if (channel_info
->channel_name
) {
318 free(channel_info
->channel_name
);
324 struct channel_info
*channel_info_copy(struct channel_info
*channel_info
)
326 struct channel_info
*copy
= zmalloc(sizeof(*channel_info
));
328 assert(channel_info
);
329 assert(channel_info
->session_name
);
330 assert(channel_info
->channel_name
);
336 memcpy(copy
, channel_info
, sizeof(*channel_info
));
337 copy
->session_name
= NULL
;
338 copy
->channel_name
= NULL
;
340 copy
->session_name
= strdup(channel_info
->session_name
);
341 if (!copy
->session_name
) {
344 copy
->channel_name
= strdup(channel_info
->channel_name
);
345 if (!copy
->channel_name
) {
348 cds_lfht_node_init(&channel_info
->channels_ht_node
);
352 channel_info_destroy(copy
);
357 int notification_thread_client_subscribe(struct notification_client
*client
,
358 struct lttng_condition
*condition
,
359 struct notification_thread_state
*state
,
360 enum lttng_notification_channel_status
*_status
)
363 struct cds_lfht_iter iter
;
364 struct cds_lfht_node
*node
;
365 struct notification_client_list
*client_list
;
366 struct lttng_condition_list_element
*condition_list_element
= NULL
;
367 struct notification_client_list_element
*client_list_element
= NULL
;
368 enum lttng_notification_channel_status status
=
369 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
372 * Ensure that the client has not already subscribed to this condition
375 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
376 if (lttng_condition_is_equal(condition_list_element
->condition
,
378 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
383 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
384 if (!condition_list_element
) {
388 client_list_element
= zmalloc(sizeof(*client_list_element
));
389 if (!client_list_element
) {
397 * Add the newly-subscribed condition to the client's subscription list.
399 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
400 condition_list_element
->condition
= condition
;
401 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
404 * Add the client to the list of clients interested in a given trigger
405 * if a "notification" trigger with a corresponding condition was
408 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
409 lttng_condition_hash(condition
),
410 match_client_list_condition
,
413 node
= cds_lfht_iter_get_node(&iter
);
415 free(client_list_element
);
419 client_list
= caa_container_of(node
, struct notification_client_list
,
420 notification_trigger_ht_node
);
421 client_list_element
->client
= client
;
422 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
423 cds_list_add(&client_list_element
->node
, &client_list
->list
);
432 free(condition_list_element
);
433 free(client_list_element
);
438 int notification_thread_client_unsubscribe(
439 struct notification_client
*client
,
440 struct lttng_condition
*condition
,
441 struct notification_thread_state
*state
,
442 enum lttng_notification_channel_status
*_status
)
444 struct cds_lfht_iter iter
;
445 struct cds_lfht_node
*node
;
446 struct notification_client_list
*client_list
;
447 struct lttng_condition_list_element
*condition_list_element
,
449 struct notification_client_list_element
*client_list_element
,
451 bool condition_found
= false;
452 enum lttng_notification_channel_status status
=
453 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
455 /* Remove the condition from the client's condition list. */
456 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
457 &client
->condition_list
, node
) {
458 if (!lttng_condition_is_equal(condition_list_element
->condition
,
463 cds_list_del(&condition_list_element
->node
);
465 * The caller may be iterating on the client's conditions to
466 * tear down a client's connection. In this case, the condition
467 * will be destroyed at the end.
469 if (condition
!= condition_list_element
->condition
) {
470 lttng_condition_destroy(
471 condition_list_element
->condition
);
473 free(condition_list_element
);
474 condition_found
= true;
478 if (!condition_found
) {
479 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
484 * Remove the client from the list of clients interested the trigger
485 * matching the condition.
488 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
489 lttng_condition_hash(condition
),
490 match_client_list_condition
,
493 node
= cds_lfht_iter_get_node(&iter
);
498 client_list
= caa_container_of(node
, struct notification_client_list
,
499 notification_trigger_ht_node
);
500 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
501 &client_list
->list
, node
) {
502 if (client_list_element
->client
->socket
!= client
->socket
) {
505 cds_list_del(&client_list_element
->node
);
506 free(client_list_element
);
512 lttng_condition_destroy(condition
);
520 void notification_client_destroy(struct notification_client
*client
,
521 struct notification_thread_state
*state
)
523 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
529 /* Release all conditions to which the client was subscribed. */
530 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
531 &client
->condition_list
, node
) {
532 (void) notification_thread_client_unsubscribe(client
,
533 condition_list_element
->condition
, state
, NULL
);
536 if (client
->socket
>= 0) {
537 (void) lttcomm_close_unix_sock(client
->socket
);
539 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
540 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
545 * Call with rcu_read_lock held (and hold for the lifetime of the returned
549 struct notification_client
*get_client_from_socket(int socket
,
550 struct notification_thread_state
*state
)
552 struct cds_lfht_iter iter
;
553 struct cds_lfht_node
*node
;
554 struct notification_client
*client
= NULL
;
556 cds_lfht_lookup(state
->client_socket_ht
,
557 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
559 (void *) (unsigned long) socket
,
561 node
= cds_lfht_iter_get_node(&iter
);
566 client
= caa_container_of(node
, struct notification_client
,
567 client_socket_ht_node
);
573 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
574 struct channel_info
*info
)
576 enum lttng_condition_status status
;
577 struct lttng_condition
*condition
;
578 const char *trigger_session_name
= NULL
;
579 const char *trigger_channel_name
= NULL
;
580 enum lttng_domain_type trigger_domain
;
582 condition
= lttng_trigger_get_condition(trigger
);
587 switch (lttng_condition_get_type(condition
)) {
588 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
589 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
595 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
597 assert(status
== LTTNG_CONDITION_STATUS_OK
);
598 if (info
->key
.domain
!= trigger_domain
) {
602 status
= lttng_condition_buffer_usage_get_session_name(
603 condition
, &trigger_session_name
);
604 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_session_name
);
606 status
= lttng_condition_buffer_usage_get_channel_name(
607 condition
, &trigger_channel_name
);
608 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_channel_name
);
610 if (strcmp(info
->session_name
, trigger_session_name
)) {
613 if (strcmp(info
->channel_name
, trigger_channel_name
)) {
623 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
624 struct notification_client
*client
)
626 bool applies
= false;
627 struct lttng_condition_list_element
*condition_list_element
;
629 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
631 applies
= lttng_condition_is_equal(
632 condition_list_element
->condition
,
633 lttng_trigger_get_condition(trigger
));
642 unsigned long hash_channel_key(struct channel_key
*key
)
644 return hash_key_u64(&key
->key
, lttng_ht_seed
) ^ hash_key_ulong(
645 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
649 int handle_notification_thread_command_add_channel(
650 struct notification_thread_state
*state
,
651 struct channel_info
*channel_info
,
652 enum lttng_error_code
*cmd_result
)
654 struct cds_list_head trigger_list
;
655 struct channel_info
*new_channel_info
;
656 struct channel_key
*channel_key
;
657 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
658 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
659 int trigger_count
= 0;
660 struct cds_lfht_iter iter
;
662 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
663 channel_info
->channel_name
, channel_info
->session_name
,
664 channel_info
->key
.key
, channel_info
->key
.domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
666 CDS_INIT_LIST_HEAD(&trigger_list
);
668 new_channel_info
= channel_info_copy(channel_info
);
669 if (!new_channel_info
) {
673 channel_key
= &new_channel_info
->key
;
675 /* Build a list of all triggers applying to the new channel. */
676 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
678 struct lttng_trigger_list_element
*new_element
;
680 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
685 new_element
= zmalloc(sizeof(*new_element
));
689 CDS_INIT_LIST_HEAD(&new_element
->node
);
690 new_element
->trigger
= trigger_ht_element
->trigger
;
691 cds_list_add(&new_element
->node
, &trigger_list
);
695 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
697 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
698 if (!channel_trigger_list
) {
701 channel_trigger_list
->channel_key
= *channel_key
;
702 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
703 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
704 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
707 /* Add channel to the channel_ht which owns the channel_infos. */
708 cds_lfht_add(state
->channels_ht
,
709 hash_channel_key(channel_key
),
710 &new_channel_info
->channels_ht_node
);
712 * Add the list of triggers associated with this channel to the
713 * channel_triggers_ht.
715 cds_lfht_add(state
->channel_triggers_ht
,
716 hash_channel_key(channel_key
),
717 &channel_trigger_list
->channel_triggers_ht_node
);
719 *cmd_result
= LTTNG_OK
;
722 /* Empty trigger list */
723 channel_info_destroy(new_channel_info
);
728 int handle_notification_thread_command_remove_channel(
729 struct notification_thread_state
*state
,
730 uint64_t channel_key
, enum lttng_domain_type domain
,
731 enum lttng_error_code
*cmd_result
)
733 struct cds_lfht_node
*node
;
734 struct cds_lfht_iter iter
;
735 struct lttng_channel_trigger_list
*trigger_list
;
736 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
737 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
738 struct channel_info
*channel_info
;
740 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
741 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
745 cds_lfht_lookup(state
->channel_triggers_ht
,
746 hash_channel_key(&key
),
747 match_channel_trigger_list
,
750 node
= cds_lfht_iter_get_node(&iter
);
752 * There is a severe internal error if we are being asked to remove a
753 * channel that doesn't exist.
756 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
760 /* Free the list of triggers associated with this channel. */
761 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
762 channel_triggers_ht_node
);
763 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
764 &trigger_list
->list
, node
) {
765 cds_list_del(&trigger_list_element
->node
);
766 free(trigger_list_element
);
768 cds_lfht_del(state
->channel_triggers_ht
, node
);
771 /* Free sampled channel state. */
772 cds_lfht_lookup(state
->channel_state_ht
,
773 hash_channel_key(&key
),
774 match_channel_state_sample
,
777 node
= cds_lfht_iter_get_node(&iter
);
779 * This is expected to be NULL if the channel is destroyed before we
783 struct channel_state_sample
*sample
= caa_container_of(node
,
784 struct channel_state_sample
,
785 channel_state_ht_node
);
787 cds_lfht_del(state
->channel_state_ht
, node
);
791 /* Remove the channel from the channels_ht and free it. */
792 cds_lfht_lookup(state
->channels_ht
,
793 hash_channel_key(&key
),
797 node
= cds_lfht_iter_get_node(&iter
);
799 channel_info
= caa_container_of(node
, struct channel_info
,
801 cds_lfht_del(state
->channels_ht
, node
);
802 channel_info_destroy(channel_info
);
805 *cmd_result
= LTTNG_OK
;
810 * FIXME A client's credentials are not checked when registering a trigger, nor
811 * are they stored alongside with the trigger.
813 * The effects of this are benign:
814 * - The client will succeed in registering the trigger, as it is valid,
815 * - The trigger will, internally, be bound to the channel,
816 * - The notifications will not be sent since the client's credentials
817 * are checked against the channel at that moment.
820 int handle_notification_thread_command_register_trigger(
821 struct notification_thread_state
*state
,
822 struct lttng_trigger
*trigger
,
823 enum lttng_error_code
*cmd_result
)
826 struct lttng_condition
*condition
;
827 struct notification_client
*client
;
828 struct notification_client_list
*client_list
= NULL
;
829 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
830 struct notification_client_list_element
*client_list_element
, *tmp
;
831 struct cds_lfht_node
*node
;
832 struct cds_lfht_iter iter
;
833 struct channel_info
*channel
;
834 bool free_trigger
= true;
838 condition
= lttng_trigger_get_condition(trigger
);
839 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
840 if (!trigger_ht_element
) {
845 /* Add trigger to the trigger_ht. */
846 cds_lfht_node_init(&trigger_ht_element
->node
);
847 trigger_ht_element
->trigger
= trigger
;
849 node
= cds_lfht_add_unique(state
->triggers_ht
,
850 lttng_condition_hash(condition
),
853 &trigger_ht_element
->node
);
854 if (node
!= &trigger_ht_element
->node
) {
855 /* Not a fatal error, simply report it to the client. */
856 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
857 goto error_free_ht_element
;
861 * Ownership of the trigger and of its wrapper was transfered to
864 trigger_ht_element
= NULL
;
865 free_trigger
= false;
868 * The rest only applies to triggers that have a "notify" action.
869 * It is not skipped as this is the only action type currently
872 client_list
= zmalloc(sizeof(*client_list
));
875 goto error_free_ht_element
;
877 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
878 CDS_INIT_LIST_HEAD(&client_list
->list
);
879 client_list
->trigger
= trigger
;
881 /* Build a list of clients to which this new trigger applies. */
882 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
883 client_socket_ht_node
) {
884 if (!trigger_applies_to_client(trigger
, client
)) {
888 client_list_element
= zmalloc(sizeof(*client_list_element
));
889 if (!client_list_element
) {
891 goto error_free_client_list
;
893 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
894 client_list_element
->client
= client
;
895 cds_list_add(&client_list_element
->node
, &client_list
->list
);
898 cds_lfht_add(state
->notification_trigger_clients_ht
,
899 lttng_condition_hash(condition
),
900 &client_list
->notification_trigger_ht_node
);
902 * Client list ownership transferred to the
903 * notification_trigger_clients_ht.
908 * Add the trigger to list of triggers bound to the channels currently
911 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
913 struct lttng_trigger_list_element
*trigger_list_element
;
914 struct lttng_channel_trigger_list
*trigger_list
;
916 if (!trigger_applies_to_channel(trigger
, channel
)) {
920 cds_lfht_lookup(state
->channel_triggers_ht
,
921 hash_channel_key(&channel
->key
),
922 match_channel_trigger_list
,
925 node
= cds_lfht_iter_get_node(&iter
);
927 /* Free the list of triggers associated with this channel. */
928 trigger_list
= caa_container_of(node
,
929 struct lttng_channel_trigger_list
,
930 channel_triggers_ht_node
);
932 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
933 if (!trigger_list_element
) {
935 goto error_free_client_list
;
937 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
938 trigger_list_element
->trigger
= trigger
;
939 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
940 /* A trigger can only apply to one channel. */
944 *cmd_result
= LTTNG_OK
;
945 error_free_client_list
:
947 cds_list_for_each_entry_safe(client_list_element
, tmp
,
948 &client_list
->list
, node
) {
949 free(client_list_element
);
953 error_free_ht_element
:
954 free(trigger_ht_element
);
957 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
959 lttng_condition_destroy(condition
);
960 lttng_action_destroy(action
);
961 lttng_trigger_destroy(trigger
);
968 int handle_notification_thread_command_unregister_trigger(
969 struct notification_thread_state
*state
,
970 struct lttng_trigger
*trigger
,
971 enum lttng_error_code
*_cmd_reply
)
973 struct cds_lfht_iter iter
;
974 struct cds_lfht_node
*node
, *triggers_ht_node
;
975 struct lttng_channel_trigger_list
*trigger_list
;
976 struct notification_client_list
*client_list
;
977 struct notification_client_list_element
*client_list_element
, *tmp
;
978 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
979 struct lttng_condition
*condition
= lttng_trigger_get_condition(
981 struct lttng_action
*action
;
982 enum lttng_error_code cmd_reply
;
986 cds_lfht_lookup(state
->triggers_ht
,
987 lttng_condition_hash(condition
),
991 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
992 if (!triggers_ht_node
) {
993 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
996 cmd_reply
= LTTNG_OK
;
999 /* Remove trigger from channel_triggers_ht. */
1000 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1001 channel_triggers_ht_node
) {
1002 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1004 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1005 &trigger_list
->list
, node
) {
1006 struct lttng_condition
*current_condition
=
1007 lttng_trigger_get_condition(
1008 trigger_element
->trigger
);
1010 assert(current_condition
);
1011 if (!lttng_condition_is_equal(condition
,
1012 current_condition
)) {
1016 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1017 cds_list_del(&trigger_element
->node
);
1022 * Remove and release the client list from
1023 * notification_trigger_clients_ht.
1025 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1026 lttng_condition_hash(condition
),
1030 node
= cds_lfht_iter_get_node(&iter
);
1032 client_list
= caa_container_of(node
, struct notification_client_list
,
1033 notification_trigger_ht_node
);
1034 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1035 &client_list
->list
, node
) {
1036 free(client_list_element
);
1038 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1041 /* Remove trigger from triggers_ht. */
1042 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1043 struct lttng_trigger_ht_element
, node
);
1044 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1046 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1047 lttng_condition_destroy(condition
);
1048 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1049 lttng_action_destroy(action
);
1050 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1051 free(trigger_ht_element
);
1055 *_cmd_reply
= cmd_reply
;
1060 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1061 int handle_notification_thread_command(
1062 struct notification_thread_handle
*handle
,
1063 struct notification_thread_state
*state
)
1067 struct notification_thread_command
*cmd
;
1069 /* Read event_fd to put it back into a quiescent state. */
1070 ret
= read(handle
->cmd_queue
.event_fd
, &counter
, sizeof(counter
));
1075 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1076 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1077 struct notification_thread_command
, cmd_list_node
);
1078 switch (cmd
->type
) {
1079 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1080 DBG("[notification-thread] Received register trigger command");
1081 ret
= handle_notification_thread_command_register_trigger(
1082 state
, cmd
->parameters
.trigger
,
1085 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1086 DBG("[notification-thread] Received unregister trigger command");
1087 ret
= handle_notification_thread_command_unregister_trigger(
1088 state
, cmd
->parameters
.trigger
,
1091 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1092 DBG("[notification-thread] Received add channel command");
1093 ret
= handle_notification_thread_command_add_channel(
1094 state
, &cmd
->parameters
.add_channel
,
1097 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1098 DBG("[notification-thread] Received remove channel command");
1099 ret
= handle_notification_thread_command_remove_channel(
1100 state
, cmd
->parameters
.remove_channel
.key
,
1101 cmd
->parameters
.remove_channel
.domain
,
1104 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1105 DBG("[notification-thread] Received quit command");
1106 cmd
->reply_code
= LTTNG_OK
;
1110 ERR("[notification-thread] Unknown internal command received");
1118 cds_list_del(&cmd
->cmd_list_node
);
1119 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1120 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1123 /* Wake-up and return a fatal error to the calling thread. */
1124 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1125 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1126 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1128 /* Indicate a fatal error to the caller. */
1133 unsigned long hash_client_socket(int socket
)
1135 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1139 int socket_set_non_blocking(int socket
)
1143 /* Set the pipe as non-blocking. */
1144 ret
= fcntl(socket
, F_GETFL
, 0);
1146 PERROR("fcntl get socket flags");
1151 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1153 PERROR("fcntl set O_NONBLOCK socket flag");
1156 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1162 int client_reset_inbound_state(struct notification_client
*client
)
1166 ret
= lttng_dynamic_buffer_set_size(
1167 &client
->communication
.inbound
.buffer
, 0);
1170 client
->communication
.inbound
.bytes_to_receive
=
1171 sizeof(struct lttng_notification_channel_message
);
1172 client
->communication
.inbound
.msg_type
=
1173 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1174 client
->communication
.inbound
.receive_creds
= false;
1175 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1176 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1177 ret
= lttng_dynamic_buffer_set_size(
1178 &client
->communication
.inbound
.buffer
,
1179 client
->communication
.inbound
.bytes_to_receive
);
1183 int handle_notification_thread_client_connect(
1184 struct notification_thread_state
*state
)
1187 struct notification_client
*client
;
1189 DBG("[notification-thread] Handling new notification channel client connection");
1191 client
= zmalloc(sizeof(*client
));
1197 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1198 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1199 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1200 ret
= client_reset_inbound_state(client
);
1202 ERR("[notification-thread] Failed to reset client communication's inbound state");
1207 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1209 ERR("[notification-thread] Failed to accept new notification channel client connection");
1214 client
->socket
= ret
;
1216 ret
= socket_set_non_blocking(client
->socket
);
1218 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1222 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1224 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1229 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1230 LPOLLIN
| LPOLLERR
|
1231 LPOLLHUP
| LPOLLRDHUP
);
1233 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1237 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1241 cds_lfht_add(state
->client_socket_ht
,
1242 hash_client_socket(client
->socket
),
1243 &client
->client_socket_ht_node
);
1248 notification_client_destroy(client
, state
);
1252 int handle_notification_thread_client_disconnect(
1254 struct notification_thread_state
*state
)
1257 struct notification_client
*client
;
1260 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1262 client
= get_client_from_socket(client_socket
, state
);
1264 /* Internal state corruption, fatal error. */
1265 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1271 ret
= lttng_poll_del(&state
->events
, client_socket
);
1273 ERR("[notification-thread] Failed to remove client socket from poll set");
1275 cds_lfht_del(state
->client_socket_ht
,
1276 &client
->client_socket_ht_node
);
1277 notification_client_destroy(client
, state
);
1283 int handle_notification_thread_client_disconnect_all(
1284 struct notification_thread_state
*state
)
1286 struct cds_lfht_iter iter
;
1287 struct notification_client
*client
;
1288 bool error_encoutered
= false;
1291 DBG("[notification-thread] Closing all client connections");
1292 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1293 client_socket_ht_node
) {
1296 ret
= handle_notification_thread_client_disconnect(
1297 client
->socket
, state
);
1299 error_encoutered
= true;
1303 return error_encoutered
? 1 : 0;
1306 int handle_notification_thread_trigger_unregister_all(
1307 struct notification_thread_state
*state
)
1309 bool error_occured
= false;
1310 struct cds_lfht_iter iter
;
1311 struct lttng_trigger_ht_element
*trigger_ht_element
;
1313 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1315 int ret
= handle_notification_thread_command_unregister_trigger(
1316 state
, trigger_ht_element
->trigger
, NULL
);
1318 error_occured
= true;
1321 return error_occured
? -1 : 0;
1325 int client_flush_outgoing_queue(struct notification_client
*client
,
1326 struct notification_thread_state
*state
)
1329 size_t to_send_count
;
1331 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1332 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1333 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1336 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1337 client
->communication
.outbound
.buffer
.data
,
1339 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1340 (ret
> 0 && ret
< to_send_count
)) {
1341 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1343 to_send_count
-= max(ret
, 0);
1345 memcpy(client
->communication
.outbound
.buffer
.data
,
1346 client
->communication
.outbound
.buffer
.data
+
1347 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1349 ret
= lttng_dynamic_buffer_set_size(
1350 &client
->communication
.outbound
.buffer
,
1357 * We want to be notified whenever there is buffer space
1358 * available to send the rest of the payload.
1360 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1361 CLIENT_POLL_MASK_IN_OUT
);
1365 } else if (ret
< 0) {
1366 /* Generic error, disconnect the client. */
1367 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1369 ret
= handle_notification_thread_client_disconnect(
1370 client
->socket
, state
);
1375 /* No error and flushed the queue completely. */
1376 ret
= lttng_dynamic_buffer_set_size(
1377 &client
->communication
.outbound
.buffer
, 0);
1381 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1382 CLIENT_POLL_MASK_IN
);
1387 client
->communication
.outbound
.queued_command_reply
= false;
1388 client
->communication
.outbound
.dropped_notification
= false;
1397 int client_send_command_reply(struct notification_client
*client
,
1398 struct notification_thread_state
*state
,
1399 enum lttng_notification_channel_status status
)
1402 struct lttng_notification_channel_command_reply reply
= {
1403 .status
= (int8_t) status
,
1405 struct lttng_notification_channel_message msg
= {
1406 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1407 .size
= sizeof(reply
),
1409 char buffer
[sizeof(msg
) + sizeof(reply
)];
1411 if (client
->communication
.outbound
.queued_command_reply
) {
1412 /* Protocol error. */
1416 memcpy(buffer
, &msg
, sizeof(msg
));
1417 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1418 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1420 /* Enqueue buffer to outgoing queue and flush it. */
1421 ret
= lttng_dynamic_buffer_append(
1422 &client
->communication
.outbound
.buffer
,
1423 buffer
, sizeof(buffer
));
1428 ret
= client_flush_outgoing_queue(client
, state
);
1433 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1434 /* Queue could not be emptied. */
1435 client
->communication
.outbound
.queued_command_reply
= true;
1444 int client_dispatch_message(struct notification_client
*client
,
1445 struct notification_thread_state
*state
)
1449 if (client
->communication
.inbound
.msg_type
!=
1450 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1451 client
->communication
.inbound
.msg_type
!=
1452 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1453 !client
->validated
) {
1454 WARN("[notification-thread] client attempted a command before handshake");
1459 switch (client
->communication
.inbound
.msg_type
) {
1460 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1463 * Receiving message header. The function will be called again
1464 * once the rest of the message as been received and can be
1467 const struct lttng_notification_channel_message
*msg
;
1469 assert(sizeof(*msg
) ==
1470 client
->communication
.inbound
.buffer
.size
);
1471 msg
= (const struct lttng_notification_channel_message
*)
1472 client
->communication
.inbound
.buffer
.data
;
1474 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1475 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1480 switch (msg
->type
) {
1481 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1482 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1483 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1487 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1491 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
1492 client
->communication
.inbound
.msg_type
=
1493 (enum lttng_notification_channel_message_type
) msg
->type
;
1494 if (client
->communication
.inbound
.msg_type
==
1495 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
) {
1496 client
->communication
.inbound
.receive_creds
= true;
1498 ret
= lttng_dynamic_buffer_set_size(
1499 &client
->communication
.inbound
.buffer
, msg
->size
);
1505 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1507 struct lttng_notification_channel_command_handshake
*handshake_client
;
1508 struct lttng_notification_channel_command_handshake handshake_reply
= {
1509 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
1510 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
1512 struct lttng_notification_channel_message msg_header
= {
1513 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
1514 .size
= sizeof(handshake_reply
),
1516 enum lttng_notification_channel_status status
=
1517 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1518 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
1520 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
1521 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
1522 sizeof(handshake_reply
));
1525 (struct lttng_notification_channel_command_handshake
*)
1526 client
->communication
.inbound
.buffer
.data
;
1527 client
->major
= handshake_client
->major
;
1528 client
->minor
= handshake_client
->minor
;
1529 if (!client
->communication
.inbound
.creds_received
) {
1530 ERR("[notification-thread] No credentials received from client");
1535 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
1536 &client
->communication
.inbound
.creds
);
1537 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
1538 &client
->communication
.inbound
.creds
);
1539 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
1540 client
->uid
, client
->gid
, (int) client
->major
,
1541 (int) client
->minor
);
1543 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
1544 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
1547 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
1548 send_buffer
, sizeof(send_buffer
));
1550 ERR("[notification-thread] Failed to send protocol version to notification channel client");
1554 ret
= client_flush_outgoing_queue(client
, state
);
1559 ret
= client_send_command_reply(client
, state
, status
);
1561 ERR("[notification-thread] Failed to send reply to notification channel client");
1565 /* Set reception state to receive the next message header. */
1566 ret
= client_reset_inbound_state(client
);
1568 ERR("[notification-thread] Failed to reset client communication's inbound state");
1571 client
->validated
= true;
1574 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1575 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1577 struct lttng_condition
*condition
;
1578 enum lttng_notification_channel_status status
=
1579 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1580 const struct lttng_buffer_view condition_view
=
1581 lttng_buffer_view_from_dynamic_buffer(
1582 &client
->communication
.inbound
.buffer
,
1584 size_t expected_condition_size
=
1585 client
->communication
.inbound
.buffer
.size
;
1587 ret
= lttng_condition_create_from_buffer(&condition_view
,
1589 if (ret
!= expected_condition_size
) {
1590 ERR("[notification-thread] Malformed condition received from client");
1594 if (client
->communication
.inbound
.msg_type
==
1595 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
1597 * FIXME The current state should be evaluated on
1600 ret
= notification_thread_client_subscribe(client
,
1601 condition
, state
, &status
);
1603 ret
= notification_thread_client_unsubscribe(client
,
1604 condition
, state
, &status
);
1610 ret
= client_send_command_reply(client
, state
, status
);
1612 ERR("[notification-thread] Failed to send reply to notification channel client");
1616 /* Set reception state to receive the next message header. */
1617 ret
= client_reset_inbound_state(client
);
1619 ERR("[notification-thread] Failed to reset client communication's inbound state");
1631 /* Incoming data from client. */
1632 int handle_notification_thread_client_in(
1633 struct notification_thread_state
*state
, int socket
)
1636 struct notification_client
*client
;
1640 client
= get_client_from_socket(socket
, state
);
1642 /* Internal error, abort. */
1647 offset
= client
->communication
.inbound
.buffer
.size
-
1648 client
->communication
.inbound
.bytes_to_receive
;
1649 if (client
->communication
.inbound
.receive_creds
) {
1650 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
1651 client
->communication
.inbound
.buffer
.data
+ offset
,
1652 client
->communication
.inbound
.bytes_to_receive
,
1653 &client
->communication
.inbound
.creds
);
1655 client
->communication
.inbound
.receive_creds
= false;
1656 client
->communication
.inbound
.creds_received
= true;
1659 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
1660 client
->communication
.inbound
.buffer
.data
+ offset
,
1661 client
->communication
.inbound
.bytes_to_receive
);
1664 goto error_disconnect_client
;
1667 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
1668 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
1669 ret
= client_dispatch_message(client
, state
);
1672 * Only returns an error if this client must be
1675 goto error_disconnect_client
;
1682 error_disconnect_client
:
1683 ret
= handle_notification_thread_client_disconnect(socket
, state
);
1687 /* Client ready to receive outgoing data. */
1688 int handle_notification_thread_client_out(
1689 struct notification_thread_state
*state
, int socket
)
1692 struct notification_client
*client
;
1694 client
= get_client_from_socket(socket
, state
);
1696 /* Internal error, abort. */
1701 ret
= client_flush_outgoing_queue(client
, state
);
1710 bool evaluate_buffer_usage_condition(struct lttng_condition
*condition
,
1711 struct channel_state_sample
*sample
, uint64_t buffer_capacity
)
1713 bool result
= false;
1715 enum lttng_condition_type condition_type
;
1716 struct lttng_condition_buffer_usage
*use_condition
= container_of(
1717 condition
, struct lttng_condition_buffer_usage
,
1724 if (use_condition
->threshold_bytes
.set
) {
1725 threshold
= use_condition
->threshold_bytes
.value
;
1728 * Threshold was expressed as a ratio.
1730 * TODO the threshold (in bytes) of conditions expressed
1731 * as a ratio of total buffer size could be cached to
1732 * forego this double-multiplication or it could be performed
1733 * as fixed-point math.
1735 * Note that caching should accomodate the case where the
1736 * condition applies to multiple channels (i.e. don't assume
1737 * that all channels matching my_chann* have the same size...)
1739 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
1740 (double) buffer_capacity
);
1743 condition_type
= lttng_condition_get_type(condition
);
1744 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
1745 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1746 threshold
, sample
->highest_usage
);
1749 * The low condition should only be triggered once _all_ of the
1750 * streams in a channel have gone below the "low" threshold.
1752 if (sample
->highest_usage
<= threshold
) {
1756 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1757 threshold
, sample
->highest_usage
);
1760 * For high buffer usage scenarios, we want to trigger whenever
1761 * _any_ of the streams has reached the "high" threshold.
1763 if (sample
->highest_usage
>= threshold
) {
1772 int evaluate_condition(struct lttng_condition
*condition
,
1773 struct lttng_evaluation
**evaluation
,
1774 struct notification_thread_state
*state
,
1775 struct channel_state_sample
*previous_sample
,
1776 struct channel_state_sample
*latest_sample
,
1777 uint64_t buffer_capacity
)
1780 enum lttng_condition_type condition_type
;
1781 bool previous_sample_result
;
1782 bool latest_sample_result
;
1784 condition_type
= lttng_condition_get_type(condition
);
1785 /* No other condition type supported for the moment. */
1786 assert(condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
1787 condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
1789 previous_sample_result
= evaluate_buffer_usage_condition(condition
,
1790 previous_sample
, buffer_capacity
);
1791 latest_sample_result
= evaluate_buffer_usage_condition(condition
,
1792 latest_sample
, buffer_capacity
);
1794 if (!latest_sample_result
||
1795 (previous_sample_result
== latest_sample_result
)) {
1797 * Only trigger on a condition evaluation transition.
1799 * NOTE: This edge-triggered logic may not be appropriate for
1800 * future condition types.
1805 if (evaluation
&& latest_sample_result
) {
1806 *evaluation
= lttng_evaluation_buffer_usage_create(
1808 latest_sample
->highest_usage
,
1820 int client_enqueue_dropped_notification(struct notification_client
*client
,
1821 struct notification_thread_state
*state
)
1824 struct lttng_notification_channel_message msg
= {
1825 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
1829 ret
= lttng_dynamic_buffer_append(
1830 &client
->communication
.outbound
.buffer
, &msg
,
1836 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
1837 struct lttng_evaluation
*evaluation
,
1838 struct notification_client_list
* client_list
,
1839 struct notification_thread_state
*state
,
1840 uid_t channel_uid
, gid_t channel_gid
)
1843 struct lttng_dynamic_buffer msg_buffer
;
1844 struct notification_client_list_element
*client_list_element
, *tmp
;
1845 struct lttng_notification
*notification
;
1846 struct lttng_condition
*condition
;
1847 ssize_t expected_notification_size
, notification_size
;
1848 struct lttng_notification_channel_message msg
;
1850 lttng_dynamic_buffer_init(&msg_buffer
);
1852 condition
= lttng_trigger_get_condition(trigger
);
1855 notification
= lttng_notification_create(condition
, evaluation
);
1856 if (!notification
) {
1861 expected_notification_size
= lttng_notification_serialize(notification
,
1863 if (expected_notification_size
< 0) {
1864 ERR("[notification-thread] Failed to get size of serialized notification");
1869 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
1870 msg
.size
= (uint32_t) expected_notification_size
;
1871 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg
, sizeof(msg
));
1876 ret
= lttng_dynamic_buffer_set_size(&msg_buffer
,
1877 msg_buffer
.size
+ expected_notification_size
);
1882 notification_size
= lttng_notification_serialize(notification
,
1883 msg_buffer
.data
+ sizeof(msg
));
1884 if (notification_size
!= expected_notification_size
) {
1885 ERR("[notification-thread] Failed to serialize notification");
1890 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1891 &client_list
->list
, node
) {
1892 struct notification_client
*client
=
1893 client_list_element
->client
;
1895 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
1897 /* Client is not allowed to monitor this channel. */
1898 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
1902 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
1903 client
->socket
, msg_buffer
.size
);
1904 if (client
->communication
.outbound
.buffer
.size
) {
1906 * Outgoing data is already buffered for this client;
1907 * drop the notification and enqueue a "dropped
1908 * notification" message if this is the first dropped
1909 * notification since the socket spilled-over to the
1912 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
1914 if (!client
->communication
.outbound
.dropped_notification
) {
1915 client
->communication
.outbound
.dropped_notification
= true;
1916 ret
= client_enqueue_dropped_notification(
1925 ret
= lttng_dynamic_buffer_append_buffer(
1926 &client
->communication
.outbound
.buffer
,
1932 ret
= client_flush_outgoing_queue(client
, state
);
1939 lttng_notification_destroy(notification
);
1940 lttng_dynamic_buffer_reset(&msg_buffer
);
1944 int handle_notification_thread_channel_sample(
1945 struct notification_thread_state
*state
, int pipe
,
1946 enum lttng_domain_type domain
)
1949 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
1950 struct channel_state_sample previous_sample
, latest_sample
;
1951 struct channel_info
*channel_info
;
1952 struct cds_lfht_node
*node
;
1953 struct cds_lfht_iter iter
;
1954 struct lttng_channel_trigger_list
*trigger_list
;
1955 struct lttng_trigger_list_element
*trigger_list_element
;
1956 bool previous_sample_available
= false;
1959 * The monitoring pipe only holds messages smaller than PIPE_BUF,
1960 * ensuring that read/write of sampling messages are atomic.
1962 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
1963 if (ret
!= sizeof(sample_msg
)) {
1964 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
1971 latest_sample
.key
.key
= sample_msg
.key
;
1972 latest_sample
.key
.domain
= domain
;
1973 latest_sample
.highest_usage
= sample_msg
.highest
;
1974 latest_sample
.lowest_usage
= sample_msg
.lowest
;
1978 /* Retrieve the channel's informations */
1979 cds_lfht_lookup(state
->channels_ht
,
1980 hash_channel_key(&latest_sample
.key
),
1984 node
= cds_lfht_iter_get_node(&iter
);
1987 * Not an error since the consumer can push a sample to the pipe
1988 * and the rest of the session daemon could notify us of the
1989 * channel's destruction before we get a chance to process that
1992 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
1993 latest_sample
.key
.key
,
1994 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
1998 channel_info
= caa_container_of(node
, struct channel_info
,
2000 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
")",
2001 channel_info
->channel_name
,
2002 latest_sample
.key
.key
,
2003 channel_info
->session_name
,
2004 latest_sample
.highest_usage
,
2005 latest_sample
.lowest_usage
);
2007 /* Retrieve the channel's last sample, if it exists, and update it. */
2008 cds_lfht_lookup(state
->channel_state_ht
,
2009 hash_channel_key(&latest_sample
.key
),
2010 match_channel_state_sample
,
2013 node
= cds_lfht_iter_get_node(&iter
);
2015 struct channel_state_sample
*stored_sample
;
2017 /* Update the sample stored. */
2018 stored_sample
= caa_container_of(node
,
2019 struct channel_state_sample
,
2020 channel_state_ht_node
);
2021 memcpy(&previous_sample
, stored_sample
,
2022 sizeof(previous_sample
));
2023 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2024 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2025 previous_sample_available
= true;
2028 * This is the channel's first sample, allocate space for and
2029 * store the new sample.
2031 struct channel_state_sample
*stored_sample
;
2033 stored_sample
= zmalloc(sizeof(*stored_sample
));
2034 if (!stored_sample
) {
2039 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2040 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2041 cds_lfht_add(state
->channel_state_ht
,
2042 hash_channel_key(&stored_sample
->key
),
2043 &stored_sample
->channel_state_ht_node
);
2046 /* Find triggers associated with this channel. */
2047 cds_lfht_lookup(state
->channel_triggers_ht
,
2048 hash_channel_key(&latest_sample
.key
),
2049 match_channel_trigger_list
,
2052 node
= cds_lfht_iter_get_node(&iter
);
2057 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2058 channel_triggers_ht_node
);
2059 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2061 struct lttng_condition
*condition
;
2062 struct lttng_action
*action
;
2063 struct lttng_trigger
*trigger
;
2064 struct notification_client_list
*client_list
;
2065 struct lttng_evaluation
*evaluation
= NULL
;
2067 trigger
= trigger_list_element
->trigger
;
2068 condition
= lttng_trigger_get_condition(trigger
);
2070 action
= lttng_trigger_get_action(trigger
);
2072 /* Notify actions are the only type currently supported. */
2073 assert(lttng_action_get_type(action
) ==
2074 LTTNG_ACTION_TYPE_NOTIFY
);
2077 * Check if any client is subscribed to the result of this
2080 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2081 lttng_condition_hash(condition
),
2085 node
= cds_lfht_iter_get_node(&iter
);
2088 client_list
= caa_container_of(node
,
2089 struct notification_client_list
,
2090 notification_trigger_ht_node
);
2091 if (cds_list_empty(&client_list
->list
)) {
2093 * No clients interested in the evaluation's result,
2099 ret
= evaluate_condition(condition
, &evaluation
, state
,
2100 previous_sample_available
? &previous_sample
: NULL
,
2101 &latest_sample
, channel_info
->capacity
);
2110 /* Dispatch evaluation result to all clients. */
2111 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2112 evaluation
, client_list
, state
,
2113 channel_info
->uid
, channel_info
->gid
);