2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <urcu/rculfhash.h>
22 #include <common/defaults.h>
23 #include <common/error.h>
24 #include <common/futex.h>
25 #include <common/unix.h>
26 #include <common/dynamic-buffer.h>
27 #include <common/hashtable/utils.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/macros.h>
30 #include <lttng/condition/condition.h>
31 #include <lttng/action/action.h>
32 #include <lttng/notification/notification-internal.h>
33 #include <lttng/condition/condition-internal.h>
34 #include <lttng/condition/buffer-usage-internal.h>
35 #include <lttng/notification/channel-internal.h>
43 #include "notification-thread.h"
44 #include "notification-thread-events.h"
45 #include "notification-thread-commands.h"
46 #include "lttng-sessiond.h"
49 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
50 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
52 struct lttng_trigger_list_element
{
53 struct lttng_trigger
*trigger
;
54 struct cds_list_head node
;
57 struct lttng_channel_trigger_list
{
58 struct channel_key channel_key
;
59 struct cds_list_head list
;
60 struct cds_lfht_node channel_triggers_ht_node
;
63 struct lttng_trigger_ht_element
{
64 struct lttng_trigger
*trigger
;
65 struct cds_lfht_node node
;
68 struct lttng_condition_list_element
{
69 struct lttng_condition
*condition
;
70 struct cds_list_head node
;
73 struct notification_client_list_element
{
74 struct notification_client
*client
;
75 struct cds_list_head node
;
78 struct notification_client_list
{
79 struct lttng_trigger
*trigger
;
80 struct cds_list_head list
;
81 struct cds_lfht_node notification_trigger_ht_node
;
84 struct notification_client
{
86 /* Client protocol version. */
91 * Indicates if the credentials and versions of the client have been
96 * Conditions to which the client's notification channel is subscribed.
97 * List of struct lttng_condition_list_node. The condition member is
98 * owned by the client.
100 struct cds_list_head condition_list
;
101 struct cds_lfht_node client_socket_ht_node
;
105 * During the reception of a message, the reception
106 * buffers' "size" is set to contain the current
107 * message's complete payload.
109 struct lttng_dynamic_buffer buffer
;
110 /* Bytes left to receive for the current message. */
111 size_t bytes_to_receive
;
112 /* Type of the message being received. */
113 enum lttng_notification_channel_message_type msg_type
;
115 * Indicates whether or not credentials are expected
120 * Indicates whether or not credentials were received
124 /* Only used during credentials reception. */
125 lttng_sock_cred creds
;
129 * Indicates whether or not a notification addressed to
130 * this client was dropped because a command reply was
133 * A notification is dropped whenever the buffer is not
136 bool dropped_notification
;
138 * Indicates whether or not a command reply is already
139 * buffered. In this case, it means that the client is
140 * not consuming command replies before emitting a new
141 * one. This could be caused by a protocol error or a
142 * misbehaving/malicious client.
144 bool queued_command_reply
;
145 struct lttng_dynamic_buffer buffer
;
150 struct channel_state_sample
{
151 struct channel_key key
;
152 struct cds_lfht_node channel_state_ht_node
;
153 uint64_t highest_usage
;
154 uint64_t lowest_usage
;
157 static unsigned long hash_channel_key(struct channel_key
*key
);
158 static int evaluate_condition(struct lttng_condition
*condition
,
159 struct lttng_evaluation
**evaluation
,
160 struct notification_thread_state
*state
,
161 struct channel_state_sample
*previous_sample
,
162 struct channel_state_sample
*latest_sample
,
163 uint64_t buffer_capacity
);
165 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
166 struct lttng_evaluation
*evaluation
,
167 struct notification_client_list
*client_list
,
168 struct notification_thread_state
*state
,
169 uid_t channel_uid
, gid_t channel_gid
);
172 int match_client(struct cds_lfht_node
*node
, const void *key
)
174 /* This double-cast is intended to supress pointer-to-cast warning. */
175 int socket
= (int) (intptr_t) key
;
176 struct notification_client
*client
;
178 client
= caa_container_of(node
, struct notification_client
,
179 client_socket_ht_node
);
181 return !!(client
->socket
== socket
);
185 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
187 struct channel_key
*channel_key
= (struct channel_key
*) key
;
188 struct lttng_channel_trigger_list
*trigger_list
;
190 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
191 channel_triggers_ht_node
);
193 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
194 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
198 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
200 struct channel_key
*channel_key
= (struct channel_key
*) key
;
201 struct channel_state_sample
*sample
;
203 sample
= caa_container_of(node
, struct channel_state_sample
,
204 channel_state_ht_node
);
206 return !!((channel_key
->key
== sample
->key
.key
) &&
207 (channel_key
->domain
== sample
->key
.domain
));
211 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
213 struct channel_key
*channel_key
= (struct channel_key
*) key
;
214 struct channel_info
*channel_info
;
216 channel_info
= caa_container_of(node
, struct channel_info
,
219 return !!((channel_key
->key
== channel_info
->key
.key
) &&
220 (channel_key
->domain
== channel_info
->key
.domain
));
224 int match_condition(struct cds_lfht_node
*node
, const void *key
)
226 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
227 struct lttng_trigger_ht_element
*trigger
;
228 struct lttng_condition
*condition
;
230 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
232 condition
= lttng_trigger_get_condition(trigger
->trigger
);
235 return !!lttng_condition_is_equal(condition_key
, condition
);
239 int match_client_list(struct cds_lfht_node
*node
, const void *key
)
241 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
242 struct notification_client_list
*client_list
;
243 struct lttng_condition
*condition
;
244 struct lttng_condition
*condition_key
= lttng_trigger_get_condition(
247 assert(condition_key
);
249 client_list
= caa_container_of(node
, struct notification_client_list
,
250 notification_trigger_ht_node
);
251 condition
= lttng_trigger_get_condition(client_list
->trigger
);
253 return !!lttng_condition_is_equal(condition_key
, condition
);
257 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
259 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
260 struct notification_client_list
*client_list
;
261 struct lttng_condition
*condition
;
263 assert(condition_key
);
265 client_list
= caa_container_of(node
, struct notification_client_list
,
266 notification_trigger_ht_node
);
267 condition
= lttng_trigger_get_condition(client_list
->trigger
);
269 return !!lttng_condition_is_equal(condition_key
, condition
);
273 unsigned long lttng_condition_buffer_usage_hash(
274 struct lttng_condition
*_condition
)
276 unsigned long hash
= 0;
277 struct lttng_condition_buffer_usage
*condition
;
279 condition
= container_of(_condition
,
280 struct lttng_condition_buffer_usage
, parent
);
282 if (condition
->session_name
) {
283 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
285 if (condition
->channel_name
) {
286 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
288 if (condition
->domain
.set
) {
289 hash
^= hash_key_ulong(
290 (void *) condition
->domain
.type
,
293 if (condition
->threshold_ratio
.set
) {
296 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
297 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
298 } else if (condition
->threshold_ratio
.set
) {
301 val
= condition
->threshold_bytes
.value
;
302 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
308 * The lttng_condition hashing code is kept in this file (rather than
309 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
310 * don't want to link in liblttng-ctl.
313 unsigned long lttng_condition_hash(struct lttng_condition
*condition
)
315 switch (condition
->type
) {
316 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
317 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
318 return lttng_condition_buffer_usage_hash(condition
);
320 ERR("[notification-thread] Unexpected condition type caught");
326 unsigned long hash_channel_key(struct channel_key
*key
)
328 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
329 unsigned long domain_hash
= hash_key_ulong(
330 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
332 return key_hash
^ domain_hash
;
336 void channel_info_destroy(struct channel_info
*channel_info
)
342 if (channel_info
->session_name
) {
343 free(channel_info
->session_name
);
345 if (channel_info
->channel_name
) {
346 free(channel_info
->channel_name
);
352 struct channel_info
*channel_info_copy(struct channel_info
*channel_info
)
354 struct channel_info
*copy
= zmalloc(sizeof(*channel_info
));
356 assert(channel_info
);
357 assert(channel_info
->session_name
);
358 assert(channel_info
->channel_name
);
364 memcpy(copy
, channel_info
, sizeof(*channel_info
));
365 copy
->session_name
= NULL
;
366 copy
->channel_name
= NULL
;
368 copy
->session_name
= strdup(channel_info
->session_name
);
369 if (!copy
->session_name
) {
372 copy
->channel_name
= strdup(channel_info
->channel_name
);
373 if (!copy
->channel_name
) {
376 cds_lfht_node_init(&channel_info
->channels_ht_node
);
380 channel_info_destroy(copy
);
384 /* This function must be called with the RCU read lock held. */
386 int evaluate_condition_for_client(struct lttng_trigger
*trigger
,
387 struct lttng_condition
*condition
,
388 struct notification_client
*client
,
389 struct notification_thread_state
*state
)
392 struct cds_lfht_iter iter
;
393 struct cds_lfht_node
*node
;
394 struct channel_info
*channel_info
= NULL
;
395 struct channel_key
*channel_key
= NULL
;
396 struct channel_state_sample
*last_sample
= NULL
;
397 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
398 struct lttng_evaluation
*evaluation
= NULL
;
399 struct notification_client_list client_list
= { 0 };
400 struct notification_client_list_element client_list_element
= { 0 };
407 /* Find the channel associated with the trigger. */
408 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
409 channel_trigger_list
, channel_triggers_ht_node
) {
410 struct lttng_trigger_list_element
*element
;
412 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
413 struct lttng_condition
*current_condition
=
414 lttng_trigger_get_condition(
417 assert(current_condition
);
418 if (!lttng_condition_is_equal(condition
,
419 current_condition
)) {
423 /* Found the trigger, save the channel key. */
424 channel_key
= &channel_trigger_list
->channel_key
;
428 /* The channel key was found stop iteration. */
434 /* No channel found; normal exit. */
435 DBG("[notification-thread] No channel associated with newly subscribed-to condition");
440 /* Fetch channel info for the matching channel. */
441 cds_lfht_lookup(state
->channels_ht
,
442 hash_channel_key(channel_key
),
446 node
= cds_lfht_iter_get_node(&iter
);
448 channel_info
= caa_container_of(node
, struct channel_info
,
451 /* Retrieve the channel's last sample, if it exists. */
452 cds_lfht_lookup(state
->channel_state_ht
,
453 hash_channel_key(channel_key
),
454 match_channel_state_sample
,
457 node
= cds_lfht_iter_get_node(&iter
);
459 last_sample
= caa_container_of(node
,
460 struct channel_state_sample
,
461 channel_state_ht_node
);
463 /* Nothing to evaluate, no sample was ever taken. Normal exit */
464 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
469 ret
= evaluate_condition(condition
, &evaluation
, state
, NULL
,
470 last_sample
, channel_info
->capacity
);
472 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
477 /* Evaluation yielded nothing. Normal exit. */
478 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
484 * Create a temporary client list with the client currently
487 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
488 CDS_INIT_LIST_HEAD(&client_list
.list
);
489 client_list
.trigger
= trigger
;
491 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
492 client_list_element
.client
= client
;
493 cds_list_add(&client_list_element
.node
, &client_list
.list
);
495 /* Send evaluation result to the newly-subscribed client. */
496 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
497 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
498 state
, channel_info
->uid
, channel_info
->gid
);
505 int notification_thread_client_subscribe(struct notification_client
*client
,
506 struct lttng_condition
*condition
,
507 struct notification_thread_state
*state
,
508 enum lttng_notification_channel_status
*_status
)
511 struct cds_lfht_iter iter
;
512 struct cds_lfht_node
*node
;
513 struct notification_client_list
*client_list
;
514 struct lttng_condition_list_element
*condition_list_element
= NULL
;
515 struct notification_client_list_element
*client_list_element
= NULL
;
516 enum lttng_notification_channel_status status
=
517 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
520 * Ensure that the client has not already subscribed to this condition
523 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
524 if (lttng_condition_is_equal(condition_list_element
->condition
,
526 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
531 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
532 if (!condition_list_element
) {
536 client_list_element
= zmalloc(sizeof(*client_list_element
));
537 if (!client_list_element
) {
545 * Add the newly-subscribed condition to the client's subscription list.
547 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
548 condition_list_element
->condition
= condition
;
549 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
551 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
552 lttng_condition_hash(condition
),
553 match_client_list_condition
,
556 node
= cds_lfht_iter_get_node(&iter
);
559 * No notification-emiting trigger registered with this
560 * condition. We don't evaluate the condition right away
561 * since this trigger is not registered yet.
563 free(client_list_element
);
567 client_list
= caa_container_of(node
, struct notification_client_list
,
568 notification_trigger_ht_node
);
570 * The condition to which the client just subscribed is evaluated
571 * at this point so that conditions that are already TRUE result
572 * in a notification being sent out.
574 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
576 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
582 * Add the client to the list of clients interested in a given trigger
583 * if a "notification" trigger with a corresponding condition was
586 client_list_element
->client
= client
;
587 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
588 cds_list_add(&client_list_element
->node
, &client_list
->list
);
597 free(condition_list_element
);
598 free(client_list_element
);
603 int notification_thread_client_unsubscribe(
604 struct notification_client
*client
,
605 struct lttng_condition
*condition
,
606 struct notification_thread_state
*state
,
607 enum lttng_notification_channel_status
*_status
)
609 struct cds_lfht_iter iter
;
610 struct cds_lfht_node
*node
;
611 struct notification_client_list
*client_list
;
612 struct lttng_condition_list_element
*condition_list_element
,
614 struct notification_client_list_element
*client_list_element
,
616 bool condition_found
= false;
617 enum lttng_notification_channel_status status
=
618 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
620 /* Remove the condition from the client's condition list. */
621 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
622 &client
->condition_list
, node
) {
623 if (!lttng_condition_is_equal(condition_list_element
->condition
,
628 cds_list_del(&condition_list_element
->node
);
630 * The caller may be iterating on the client's conditions to
631 * tear down a client's connection. In this case, the condition
632 * will be destroyed at the end.
634 if (condition
!= condition_list_element
->condition
) {
635 lttng_condition_destroy(
636 condition_list_element
->condition
);
638 free(condition_list_element
);
639 condition_found
= true;
643 if (!condition_found
) {
644 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
649 * Remove the client from the list of clients interested the trigger
650 * matching the condition.
653 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
654 lttng_condition_hash(condition
),
655 match_client_list_condition
,
658 node
= cds_lfht_iter_get_node(&iter
);
663 client_list
= caa_container_of(node
, struct notification_client_list
,
664 notification_trigger_ht_node
);
665 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
666 &client_list
->list
, node
) {
667 if (client_list_element
->client
->socket
!= client
->socket
) {
670 cds_list_del(&client_list_element
->node
);
671 free(client_list_element
);
677 lttng_condition_destroy(condition
);
685 void notification_client_destroy(struct notification_client
*client
,
686 struct notification_thread_state
*state
)
688 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
694 /* Release all conditions to which the client was subscribed. */
695 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
696 &client
->condition_list
, node
) {
697 (void) notification_thread_client_unsubscribe(client
,
698 condition_list_element
->condition
, state
, NULL
);
701 if (client
->socket
>= 0) {
702 (void) lttcomm_close_unix_sock(client
->socket
);
704 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
705 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
710 * Call with rcu_read_lock held (and hold for the lifetime of the returned
714 struct notification_client
*get_client_from_socket(int socket
,
715 struct notification_thread_state
*state
)
717 struct cds_lfht_iter iter
;
718 struct cds_lfht_node
*node
;
719 struct notification_client
*client
= NULL
;
721 cds_lfht_lookup(state
->client_socket_ht
,
722 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
724 (void *) (unsigned long) socket
,
726 node
= cds_lfht_iter_get_node(&iter
);
731 client
= caa_container_of(node
, struct notification_client
,
732 client_socket_ht_node
);
738 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
739 struct channel_info
*info
)
741 enum lttng_condition_status status
;
742 struct lttng_condition
*condition
;
743 const char *trigger_session_name
= NULL
;
744 const char *trigger_channel_name
= NULL
;
745 enum lttng_domain_type trigger_domain
;
747 condition
= lttng_trigger_get_condition(trigger
);
752 switch (lttng_condition_get_type(condition
)) {
753 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
754 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
760 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
762 assert(status
== LTTNG_CONDITION_STATUS_OK
);
763 if (info
->key
.domain
!= trigger_domain
) {
767 status
= lttng_condition_buffer_usage_get_session_name(
768 condition
, &trigger_session_name
);
769 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_session_name
);
771 status
= lttng_condition_buffer_usage_get_channel_name(
772 condition
, &trigger_channel_name
);
773 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_channel_name
);
775 if (strcmp(info
->session_name
, trigger_session_name
)) {
778 if (strcmp(info
->channel_name
, trigger_channel_name
)) {
788 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
789 struct notification_client
*client
)
791 bool applies
= false;
792 struct lttng_condition_list_element
*condition_list_element
;
794 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
796 applies
= lttng_condition_is_equal(
797 condition_list_element
->condition
,
798 lttng_trigger_get_condition(trigger
));
807 int handle_notification_thread_command_add_channel(
808 struct notification_thread_state
*state
,
809 struct channel_info
*channel_info
,
810 enum lttng_error_code
*cmd_result
)
812 struct cds_list_head trigger_list
;
813 struct channel_info
*new_channel_info
;
814 struct channel_key
*channel_key
;
815 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
816 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
817 int trigger_count
= 0;
818 struct cds_lfht_iter iter
;
820 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
821 channel_info
->channel_name
, channel_info
->session_name
,
822 channel_info
->key
.key
, channel_info
->key
.domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
824 CDS_INIT_LIST_HEAD(&trigger_list
);
826 new_channel_info
= channel_info_copy(channel_info
);
827 if (!new_channel_info
) {
831 channel_key
= &new_channel_info
->key
;
833 /* Build a list of all triggers applying to the new channel. */
834 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
836 struct lttng_trigger_list_element
*new_element
;
838 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
843 new_element
= zmalloc(sizeof(*new_element
));
847 CDS_INIT_LIST_HEAD(&new_element
->node
);
848 new_element
->trigger
= trigger_ht_element
->trigger
;
849 cds_list_add(&new_element
->node
, &trigger_list
);
853 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
855 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
856 if (!channel_trigger_list
) {
859 channel_trigger_list
->channel_key
= *channel_key
;
860 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
861 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
862 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
865 /* Add channel to the channel_ht which owns the channel_infos. */
866 cds_lfht_add(state
->channels_ht
,
867 hash_channel_key(channel_key
),
868 &new_channel_info
->channels_ht_node
);
870 * Add the list of triggers associated with this channel to the
871 * channel_triggers_ht.
873 cds_lfht_add(state
->channel_triggers_ht
,
874 hash_channel_key(channel_key
),
875 &channel_trigger_list
->channel_triggers_ht_node
);
877 *cmd_result
= LTTNG_OK
;
880 /* Empty trigger list */
881 channel_info_destroy(new_channel_info
);
886 int handle_notification_thread_command_remove_channel(
887 struct notification_thread_state
*state
,
888 uint64_t channel_key
, enum lttng_domain_type domain
,
889 enum lttng_error_code
*cmd_result
)
891 struct cds_lfht_node
*node
;
892 struct cds_lfht_iter iter
;
893 struct lttng_channel_trigger_list
*trigger_list
;
894 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
895 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
896 struct channel_info
*channel_info
;
898 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
899 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
903 cds_lfht_lookup(state
->channel_triggers_ht
,
904 hash_channel_key(&key
),
905 match_channel_trigger_list
,
908 node
= cds_lfht_iter_get_node(&iter
);
910 * There is a severe internal error if we are being asked to remove a
911 * channel that doesn't exist.
914 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
918 /* Free the list of triggers associated with this channel. */
919 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
920 channel_triggers_ht_node
);
921 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
922 &trigger_list
->list
, node
) {
923 cds_list_del(&trigger_list_element
->node
);
924 free(trigger_list_element
);
926 cds_lfht_del(state
->channel_triggers_ht
, node
);
929 /* Free sampled channel state. */
930 cds_lfht_lookup(state
->channel_state_ht
,
931 hash_channel_key(&key
),
932 match_channel_state_sample
,
935 node
= cds_lfht_iter_get_node(&iter
);
937 * This is expected to be NULL if the channel is destroyed before we
941 struct channel_state_sample
*sample
= caa_container_of(node
,
942 struct channel_state_sample
,
943 channel_state_ht_node
);
945 cds_lfht_del(state
->channel_state_ht
, node
);
949 /* Remove the channel from the channels_ht and free it. */
950 cds_lfht_lookup(state
->channels_ht
,
951 hash_channel_key(&key
),
955 node
= cds_lfht_iter_get_node(&iter
);
957 channel_info
= caa_container_of(node
, struct channel_info
,
959 cds_lfht_del(state
->channels_ht
, node
);
960 channel_info_destroy(channel_info
);
963 *cmd_result
= LTTNG_OK
;
968 int condition_is_supported(struct lttng_condition
*condition
)
972 switch (lttng_condition_get_type(condition
)) {
973 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
974 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
976 enum lttng_domain_type domain
;
978 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
985 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
991 * Older kernel tracers don't expose the API to monitor their
992 * buffers. Therefore, we reject triggers that require that
993 * mechanism to be available to be evaluated.
995 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1007 * FIXME A client's credentials are not checked when registering a trigger, nor
1008 * are they stored alongside with the trigger.
1010 * The effects of this are benign since:
1011 * - The client will succeed in registering the trigger, as it is valid,
1012 * - The trigger will, internally, be bound to the channel,
1013 * - The notifications will not be sent since the client's credentials
1014 * are checked against the channel at that moment.
1016 * If this function returns a non-zero value, it means something is
1017 * fundamentally broken and the whole subsystem/thread will be torn down.
1019 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1023 int handle_notification_thread_command_register_trigger(
1024 struct notification_thread_state
*state
,
1025 struct lttng_trigger
*trigger
,
1026 enum lttng_error_code
*cmd_result
)
1029 struct lttng_condition
*condition
;
1030 struct notification_client
*client
;
1031 struct notification_client_list
*client_list
= NULL
;
1032 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1033 struct notification_client_list_element
*client_list_element
, *tmp
;
1034 struct cds_lfht_node
*node
;
1035 struct cds_lfht_iter iter
;
1036 struct channel_info
*channel
;
1037 bool free_trigger
= true;
1041 condition
= lttng_trigger_get_condition(trigger
);
1044 ret
= condition_is_supported(condition
);
1047 } else if (ret
== 0) {
1048 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1051 /* Feature is supported, continue. */
1055 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1056 if (!trigger_ht_element
) {
1061 /* Add trigger to the trigger_ht. */
1062 cds_lfht_node_init(&trigger_ht_element
->node
);
1063 trigger_ht_element
->trigger
= trigger
;
1065 node
= cds_lfht_add_unique(state
->triggers_ht
,
1066 lttng_condition_hash(condition
),
1069 &trigger_ht_element
->node
);
1070 if (node
!= &trigger_ht_element
->node
) {
1071 /* Not a fatal error, simply report it to the client. */
1072 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1073 goto error_free_ht_element
;
1077 * Ownership of the trigger and of its wrapper was transfered to
1080 trigger_ht_element
= NULL
;
1081 free_trigger
= false;
1084 * The rest only applies to triggers that have a "notify" action.
1085 * It is not skipped as this is the only action type currently
1088 client_list
= zmalloc(sizeof(*client_list
));
1091 goto error_free_ht_element
;
1093 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
1094 CDS_INIT_LIST_HEAD(&client_list
->list
);
1095 client_list
->trigger
= trigger
;
1097 /* Build a list of clients to which this new trigger applies. */
1098 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1099 client_socket_ht_node
) {
1100 if (!trigger_applies_to_client(trigger
, client
)) {
1104 client_list_element
= zmalloc(sizeof(*client_list_element
));
1105 if (!client_list_element
) {
1107 goto error_free_client_list
;
1109 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1110 client_list_element
->client
= client
;
1111 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1114 cds_lfht_add(state
->notification_trigger_clients_ht
,
1115 lttng_condition_hash(condition
),
1116 &client_list
->notification_trigger_ht_node
);
1119 * Add the trigger to list of triggers bound to the channels currently
1122 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1124 struct lttng_trigger_list_element
*trigger_list_element
;
1125 struct lttng_channel_trigger_list
*trigger_list
;
1127 if (!trigger_applies_to_channel(trigger
, channel
)) {
1131 cds_lfht_lookup(state
->channel_triggers_ht
,
1132 hash_channel_key(&channel
->key
),
1133 match_channel_trigger_list
,
1136 node
= cds_lfht_iter_get_node(&iter
);
1138 trigger_list
= caa_container_of(node
,
1139 struct lttng_channel_trigger_list
,
1140 channel_triggers_ht_node
);
1142 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1143 if (!trigger_list_element
) {
1145 goto error_free_client_list
;
1147 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1148 trigger_list_element
->trigger
= trigger
;
1149 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1151 /* A trigger can only apply to one channel. */
1156 * Since there is nothing preventing clients from subscribing to a
1157 * condition before the corresponding trigger is registered, we have
1158 * to evaluate this new condition right away.
1160 * At some point, we were waiting for the next "evaluation" (e.g. on
1161 * reception of a channel sample) to evaluate this new condition, but
1164 * The reason it was broken is that waiting for the next sample
1165 * does not allow us to properly handle transitions for edge-triggered
1168 * Consider this example: when we handle a new channel sample, we
1169 * evaluate each conditions twice: once with the previous state, and
1170 * again with the newest state. We then use those two results to
1171 * determine whether a state change happened: a condition was false and
1172 * became true. If a state change happened, we have to notify clients.
1174 * Now, if a client subscribes to a given notification and registers
1175 * a trigger *after* that subscription, we have to make sure the
1176 * condition is evaluated at this point while considering only the
1177 * current state. Otherwise, the next evaluation cycle may only see
1178 * that the evaluations remain the same (true for samples n-1 and n) and
1179 * the client will never know that the condition has been met.
1181 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1182 &client_list
->list
, node
) {
1183 ret
= evaluate_condition_for_client(trigger
, condition
,
1184 client_list_element
->client
, state
);
1186 goto error_free_client_list
;
1191 * Client list ownership transferred to the
1192 * notification_trigger_clients_ht.
1196 *cmd_result
= LTTNG_OK
;
1197 error_free_client_list
:
1199 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1200 &client_list
->list
, node
) {
1201 free(client_list_element
);
1205 error_free_ht_element
:
1206 free(trigger_ht_element
);
1209 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1211 lttng_condition_destroy(condition
);
1212 lttng_action_destroy(action
);
1213 lttng_trigger_destroy(trigger
);
1220 int handle_notification_thread_command_unregister_trigger(
1221 struct notification_thread_state
*state
,
1222 struct lttng_trigger
*trigger
,
1223 enum lttng_error_code
*_cmd_reply
)
1225 struct cds_lfht_iter iter
;
1226 struct cds_lfht_node
*node
, *triggers_ht_node
;
1227 struct lttng_channel_trigger_list
*trigger_list
;
1228 struct notification_client_list
*client_list
;
1229 struct notification_client_list_element
*client_list_element
, *tmp
;
1230 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1231 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1233 struct lttng_action
*action
;
1234 enum lttng_error_code cmd_reply
;
1238 cds_lfht_lookup(state
->triggers_ht
,
1239 lttng_condition_hash(condition
),
1243 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1244 if (!triggers_ht_node
) {
1245 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1248 cmd_reply
= LTTNG_OK
;
1251 /* Remove trigger from channel_triggers_ht. */
1252 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1253 channel_triggers_ht_node
) {
1254 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1256 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1257 &trigger_list
->list
, node
) {
1258 struct lttng_condition
*current_condition
=
1259 lttng_trigger_get_condition(
1260 trigger_element
->trigger
);
1262 assert(current_condition
);
1263 if (!lttng_condition_is_equal(condition
,
1264 current_condition
)) {
1268 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1269 cds_list_del(&trigger_element
->node
);
1270 /* A trigger can only appear once per channel */
1276 * Remove and release the client list from
1277 * notification_trigger_clients_ht.
1279 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1280 lttng_condition_hash(condition
),
1284 node
= cds_lfht_iter_get_node(&iter
);
1286 client_list
= caa_container_of(node
, struct notification_client_list
,
1287 notification_trigger_ht_node
);
1288 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1289 &client_list
->list
, node
) {
1290 free(client_list_element
);
1292 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1295 /* Remove trigger from triggers_ht. */
1296 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1297 struct lttng_trigger_ht_element
, node
);
1298 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1300 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1301 lttng_condition_destroy(condition
);
1302 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1303 lttng_action_destroy(action
);
1304 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1305 free(trigger_ht_element
);
1309 *_cmd_reply
= cmd_reply
;
1314 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1315 int handle_notification_thread_command(
1316 struct notification_thread_handle
*handle
,
1317 struct notification_thread_state
*state
)
1321 struct notification_thread_command
*cmd
;
1323 /* Read event_fd to put it back into a quiescent state. */
1324 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
, sizeof(counter
));
1329 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1330 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1331 struct notification_thread_command
, cmd_list_node
);
1332 switch (cmd
->type
) {
1333 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1334 DBG("[notification-thread] Received register trigger command");
1335 ret
= handle_notification_thread_command_register_trigger(
1336 state
, cmd
->parameters
.trigger
,
1339 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1340 DBG("[notification-thread] Received unregister trigger command");
1341 ret
= handle_notification_thread_command_unregister_trigger(
1342 state
, cmd
->parameters
.trigger
,
1345 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1346 DBG("[notification-thread] Received add channel command");
1347 ret
= handle_notification_thread_command_add_channel(
1348 state
, &cmd
->parameters
.add_channel
,
1351 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1352 DBG("[notification-thread] Received remove channel command");
1353 ret
= handle_notification_thread_command_remove_channel(
1354 state
, cmd
->parameters
.remove_channel
.key
,
1355 cmd
->parameters
.remove_channel
.domain
,
1358 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1359 DBG("[notification-thread] Received quit command");
1360 cmd
->reply_code
= LTTNG_OK
;
1364 ERR("[notification-thread] Unknown internal command received");
1372 cds_list_del(&cmd
->cmd_list_node
);
1373 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1374 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1377 /* Wake-up and return a fatal error to the calling thread. */
1378 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1379 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1380 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1382 /* Indicate a fatal error to the caller. */
1387 unsigned long hash_client_socket(int socket
)
1389 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1393 int socket_set_non_blocking(int socket
)
1397 /* Set the pipe as non-blocking. */
1398 ret
= fcntl(socket
, F_GETFL
, 0);
1400 PERROR("fcntl get socket flags");
1405 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1407 PERROR("fcntl set O_NONBLOCK socket flag");
1410 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1416 int client_reset_inbound_state(struct notification_client
*client
)
1420 ret
= lttng_dynamic_buffer_set_size(
1421 &client
->communication
.inbound
.buffer
, 0);
1424 client
->communication
.inbound
.bytes_to_receive
=
1425 sizeof(struct lttng_notification_channel_message
);
1426 client
->communication
.inbound
.msg_type
=
1427 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1428 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1429 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1430 ret
= lttng_dynamic_buffer_set_size(
1431 &client
->communication
.inbound
.buffer
,
1432 client
->communication
.inbound
.bytes_to_receive
);
1436 int handle_notification_thread_client_connect(
1437 struct notification_thread_state
*state
)
1440 struct notification_client
*client
;
1442 DBG("[notification-thread] Handling new notification channel client connection");
1444 client
= zmalloc(sizeof(*client
));
1450 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1451 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1452 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1453 client
->communication
.inbound
.expect_creds
= true;
1454 ret
= client_reset_inbound_state(client
);
1456 ERR("[notification-thread] Failed to reset client communication's inbound state");
1461 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1463 ERR("[notification-thread] Failed to accept new notification channel client connection");
1468 client
->socket
= ret
;
1470 ret
= socket_set_non_blocking(client
->socket
);
1472 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1476 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1478 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1483 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1484 LPOLLIN
| LPOLLERR
|
1485 LPOLLHUP
| LPOLLRDHUP
);
1487 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1491 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1495 cds_lfht_add(state
->client_socket_ht
,
1496 hash_client_socket(client
->socket
),
1497 &client
->client_socket_ht_node
);
1502 notification_client_destroy(client
, state
);
1506 int handle_notification_thread_client_disconnect(
1508 struct notification_thread_state
*state
)
1511 struct notification_client
*client
;
1514 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1516 client
= get_client_from_socket(client_socket
, state
);
1518 /* Internal state corruption, fatal error. */
1519 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1525 ret
= lttng_poll_del(&state
->events
, client_socket
);
1527 ERR("[notification-thread] Failed to remove client socket from poll set");
1529 cds_lfht_del(state
->client_socket_ht
,
1530 &client
->client_socket_ht_node
);
1531 notification_client_destroy(client
, state
);
1537 int handle_notification_thread_client_disconnect_all(
1538 struct notification_thread_state
*state
)
1540 struct cds_lfht_iter iter
;
1541 struct notification_client
*client
;
1542 bool error_encoutered
= false;
1545 DBG("[notification-thread] Closing all client connections");
1546 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1547 client_socket_ht_node
) {
1550 ret
= handle_notification_thread_client_disconnect(
1551 client
->socket
, state
);
1553 error_encoutered
= true;
1557 return error_encoutered
? 1 : 0;
1560 int handle_notification_thread_trigger_unregister_all(
1561 struct notification_thread_state
*state
)
1563 bool error_occurred
= false;
1564 struct cds_lfht_iter iter
;
1565 struct lttng_trigger_ht_element
*trigger_ht_element
;
1567 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1569 int ret
= handle_notification_thread_command_unregister_trigger(
1570 state
, trigger_ht_element
->trigger
, NULL
);
1572 error_occurred
= true;
1575 return error_occurred
? -1 : 0;
1579 int client_flush_outgoing_queue(struct notification_client
*client
,
1580 struct notification_thread_state
*state
)
1583 size_t to_send_count
;
1585 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1586 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1587 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1590 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1591 client
->communication
.outbound
.buffer
.data
,
1593 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1594 (ret
> 0 && ret
< to_send_count
)) {
1595 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1597 to_send_count
-= max(ret
, 0);
1599 memcpy(client
->communication
.outbound
.buffer
.data
,
1600 client
->communication
.outbound
.buffer
.data
+
1601 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1603 ret
= lttng_dynamic_buffer_set_size(
1604 &client
->communication
.outbound
.buffer
,
1611 * We want to be notified whenever there is buffer space
1612 * available to send the rest of the payload.
1614 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1615 CLIENT_POLL_MASK_IN_OUT
);
1619 } else if (ret
< 0) {
1620 /* Generic error, disconnect the client. */
1621 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1623 ret
= handle_notification_thread_client_disconnect(
1624 client
->socket
, state
);
1629 /* No error and flushed the queue completely. */
1630 ret
= lttng_dynamic_buffer_set_size(
1631 &client
->communication
.outbound
.buffer
, 0);
1635 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1636 CLIENT_POLL_MASK_IN
);
1641 client
->communication
.outbound
.queued_command_reply
= false;
1642 client
->communication
.outbound
.dropped_notification
= false;
1651 int client_send_command_reply(struct notification_client
*client
,
1652 struct notification_thread_state
*state
,
1653 enum lttng_notification_channel_status status
)
1656 struct lttng_notification_channel_command_reply reply
= {
1657 .status
= (int8_t) status
,
1659 struct lttng_notification_channel_message msg
= {
1660 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1661 .size
= sizeof(reply
),
1663 char buffer
[sizeof(msg
) + sizeof(reply
)];
1665 if (client
->communication
.outbound
.queued_command_reply
) {
1666 /* Protocol error. */
1670 memcpy(buffer
, &msg
, sizeof(msg
));
1671 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1672 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1674 /* Enqueue buffer to outgoing queue and flush it. */
1675 ret
= lttng_dynamic_buffer_append(
1676 &client
->communication
.outbound
.buffer
,
1677 buffer
, sizeof(buffer
));
1682 ret
= client_flush_outgoing_queue(client
, state
);
1687 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1688 /* Queue could not be emptied. */
1689 client
->communication
.outbound
.queued_command_reply
= true;
1698 int client_dispatch_message(struct notification_client
*client
,
1699 struct notification_thread_state
*state
)
1703 if (client
->communication
.inbound
.msg_type
!=
1704 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1705 client
->communication
.inbound
.msg_type
!=
1706 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1707 !client
->validated
) {
1708 WARN("[notification-thread] client attempted a command before handshake");
1713 switch (client
->communication
.inbound
.msg_type
) {
1714 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1717 * Receiving message header. The function will be called again
1718 * once the rest of the message as been received and can be
1721 const struct lttng_notification_channel_message
*msg
;
1723 assert(sizeof(*msg
) ==
1724 client
->communication
.inbound
.buffer
.size
);
1725 msg
= (const struct lttng_notification_channel_message
*)
1726 client
->communication
.inbound
.buffer
.data
;
1728 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1729 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1734 switch (msg
->type
) {
1735 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1736 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1737 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1741 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1745 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
1746 client
->communication
.inbound
.msg_type
=
1747 (enum lttng_notification_channel_message_type
) msg
->type
;
1748 ret
= lttng_dynamic_buffer_set_size(
1749 &client
->communication
.inbound
.buffer
, msg
->size
);
1755 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1757 struct lttng_notification_channel_command_handshake
*handshake_client
;
1758 struct lttng_notification_channel_command_handshake handshake_reply
= {
1759 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
1760 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
1762 struct lttng_notification_channel_message msg_header
= {
1763 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
1764 .size
= sizeof(handshake_reply
),
1766 enum lttng_notification_channel_status status
=
1767 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1768 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
1770 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
1771 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
1772 sizeof(handshake_reply
));
1775 (struct lttng_notification_channel_command_handshake
*)
1776 client
->communication
.inbound
.buffer
.data
;
1777 client
->major
= handshake_client
->major
;
1778 client
->minor
= handshake_client
->minor
;
1779 if (!client
->communication
.inbound
.creds_received
) {
1780 ERR("[notification-thread] No credentials received from client");
1785 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
1786 &client
->communication
.inbound
.creds
);
1787 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
1788 &client
->communication
.inbound
.creds
);
1789 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
1790 client
->uid
, client
->gid
, (int) client
->major
,
1791 (int) client
->minor
);
1793 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
1794 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
1797 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
1798 send_buffer
, sizeof(send_buffer
));
1800 ERR("[notification-thread] Failed to send protocol version to notification channel client");
1804 ret
= client_flush_outgoing_queue(client
, state
);
1809 ret
= client_send_command_reply(client
, state
, status
);
1811 ERR("[notification-thread] Failed to send reply to notification channel client");
1815 /* Set reception state to receive the next message header. */
1816 ret
= client_reset_inbound_state(client
);
1818 ERR("[notification-thread] Failed to reset client communication's inbound state");
1821 client
->validated
= true;
1824 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1825 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1827 struct lttng_condition
*condition
;
1828 enum lttng_notification_channel_status status
=
1829 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1830 const struct lttng_buffer_view condition_view
=
1831 lttng_buffer_view_from_dynamic_buffer(
1832 &client
->communication
.inbound
.buffer
,
1834 size_t expected_condition_size
=
1835 client
->communication
.inbound
.buffer
.size
;
1837 ret
= lttng_condition_create_from_buffer(&condition_view
,
1839 if (ret
!= expected_condition_size
) {
1840 ERR("[notification-thread] Malformed condition received from client");
1844 if (client
->communication
.inbound
.msg_type
==
1845 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
1846 ret
= notification_thread_client_subscribe(client
,
1847 condition
, state
, &status
);
1849 ret
= notification_thread_client_unsubscribe(client
,
1850 condition
, state
, &status
);
1856 ret
= client_send_command_reply(client
, state
, status
);
1858 ERR("[notification-thread] Failed to send reply to notification channel client");
1862 /* Set reception state to receive the next message header. */
1863 ret
= client_reset_inbound_state(client
);
1865 ERR("[notification-thread] Failed to reset client communication's inbound state");
1877 /* Incoming data from client. */
1878 int handle_notification_thread_client_in(
1879 struct notification_thread_state
*state
, int socket
)
1882 struct notification_client
*client
;
1886 client
= get_client_from_socket(socket
, state
);
1888 /* Internal error, abort. */
1893 offset
= client
->communication
.inbound
.buffer
.size
-
1894 client
->communication
.inbound
.bytes_to_receive
;
1895 if (client
->communication
.inbound
.expect_creds
) {
1896 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
1897 client
->communication
.inbound
.buffer
.data
+ offset
,
1898 client
->communication
.inbound
.bytes_to_receive
,
1899 &client
->communication
.inbound
.creds
);
1901 client
->communication
.inbound
.expect_creds
= false;
1902 client
->communication
.inbound
.creds_received
= true;
1905 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
1906 client
->communication
.inbound
.buffer
.data
+ offset
,
1907 client
->communication
.inbound
.bytes_to_receive
);
1910 goto error_disconnect_client
;
1913 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
1914 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
1915 ret
= client_dispatch_message(client
, state
);
1918 * Only returns an error if this client must be
1921 goto error_disconnect_client
;
1928 error_disconnect_client
:
1929 ret
= handle_notification_thread_client_disconnect(socket
, state
);
1933 /* Client ready to receive outgoing data. */
1934 int handle_notification_thread_client_out(
1935 struct notification_thread_state
*state
, int socket
)
1938 struct notification_client
*client
;
1940 client
= get_client_from_socket(socket
, state
);
1942 /* Internal error, abort. */
1947 ret
= client_flush_outgoing_queue(client
, state
);
1956 bool evaluate_buffer_usage_condition(struct lttng_condition
*condition
,
1957 struct channel_state_sample
*sample
, uint64_t buffer_capacity
)
1959 bool result
= false;
1961 enum lttng_condition_type condition_type
;
1962 struct lttng_condition_buffer_usage
*use_condition
= container_of(
1963 condition
, struct lttng_condition_buffer_usage
,
1970 if (use_condition
->threshold_bytes
.set
) {
1971 threshold
= use_condition
->threshold_bytes
.value
;
1974 * Threshold was expressed as a ratio.
1976 * TODO the threshold (in bytes) of conditions expressed
1977 * as a ratio of total buffer size could be cached to
1978 * forego this double-multiplication or it could be performed
1979 * as fixed-point math.
1981 * Note that caching should accomodate the case where the
1982 * condition applies to multiple channels (i.e. don't assume
1983 * that all channels matching my_chann* have the same size...)
1985 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
1986 (double) buffer_capacity
);
1989 condition_type
= lttng_condition_get_type(condition
);
1990 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
1991 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1992 threshold
, sample
->highest_usage
);
1995 * The low condition should only be triggered once _all_ of the
1996 * streams in a channel have gone below the "low" threshold.
1998 if (sample
->highest_usage
<= threshold
) {
2002 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2003 threshold
, sample
->highest_usage
);
2006 * For high buffer usage scenarios, we want to trigger whenever
2007 * _any_ of the streams has reached the "high" threshold.
2009 if (sample
->highest_usage
>= threshold
) {
2018 int evaluate_condition(struct lttng_condition
*condition
,
2019 struct lttng_evaluation
**evaluation
,
2020 struct notification_thread_state
*state
,
2021 struct channel_state_sample
*previous_sample
,
2022 struct channel_state_sample
*latest_sample
,
2023 uint64_t buffer_capacity
)
2026 enum lttng_condition_type condition_type
;
2027 bool previous_sample_result
;
2028 bool latest_sample_result
;
2030 condition_type
= lttng_condition_get_type(condition
);
2031 /* No other condition type supported for the moment. */
2032 assert(condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
2033 condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
2035 previous_sample_result
= evaluate_buffer_usage_condition(condition
,
2036 previous_sample
, buffer_capacity
);
2037 latest_sample_result
= evaluate_buffer_usage_condition(condition
,
2038 latest_sample
, buffer_capacity
);
2040 if (!latest_sample_result
||
2041 (previous_sample_result
== latest_sample_result
)) {
2043 * Only trigger on a condition evaluation transition.
2045 * NOTE: This edge-triggered logic may not be appropriate for
2046 * future condition types.
2051 if (evaluation
&& latest_sample_result
) {
2052 *evaluation
= lttng_evaluation_buffer_usage_create(
2054 latest_sample
->highest_usage
,
2066 int client_enqueue_dropped_notification(struct notification_client
*client
,
2067 struct notification_thread_state
*state
)
2070 struct lttng_notification_channel_message msg
= {
2071 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
2075 ret
= lttng_dynamic_buffer_append(
2076 &client
->communication
.outbound
.buffer
, &msg
,
2082 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
2083 struct lttng_evaluation
*evaluation
,
2084 struct notification_client_list
* client_list
,
2085 struct notification_thread_state
*state
,
2086 uid_t channel_uid
, gid_t channel_gid
)
2089 struct lttng_dynamic_buffer msg_buffer
;
2090 struct notification_client_list_element
*client_list_element
, *tmp
;
2091 struct lttng_notification
*notification
;
2092 struct lttng_condition
*condition
;
2093 ssize_t expected_notification_size
, notification_size
;
2094 struct lttng_notification_channel_message msg
;
2096 lttng_dynamic_buffer_init(&msg_buffer
);
2098 condition
= lttng_trigger_get_condition(trigger
);
2101 notification
= lttng_notification_create(condition
, evaluation
);
2102 if (!notification
) {
2107 expected_notification_size
= lttng_notification_serialize(notification
,
2109 if (expected_notification_size
< 0) {
2110 ERR("[notification-thread] Failed to get size of serialized notification");
2115 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
2116 msg
.size
= (uint32_t) expected_notification_size
;
2117 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg
, sizeof(msg
));
2122 ret
= lttng_dynamic_buffer_set_size(&msg_buffer
,
2123 msg_buffer
.size
+ expected_notification_size
);
2128 notification_size
= lttng_notification_serialize(notification
,
2129 msg_buffer
.data
+ sizeof(msg
));
2130 if (notification_size
!= expected_notification_size
) {
2131 ERR("[notification-thread] Failed to serialize notification");
2136 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2137 &client_list
->list
, node
) {
2138 struct notification_client
*client
=
2139 client_list_element
->client
;
2141 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
2143 /* Client is not allowed to monitor this channel. */
2144 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
2148 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
2149 client
->socket
, msg_buffer
.size
);
2150 if (client
->communication
.outbound
.buffer
.size
) {
2152 * Outgoing data is already buffered for this client;
2153 * drop the notification and enqueue a "dropped
2154 * notification" message if this is the first dropped
2155 * notification since the socket spilled-over to the
2158 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
2160 if (!client
->communication
.outbound
.dropped_notification
) {
2161 client
->communication
.outbound
.dropped_notification
= true;
2162 ret
= client_enqueue_dropped_notification(
2171 ret
= lttng_dynamic_buffer_append_buffer(
2172 &client
->communication
.outbound
.buffer
,
2178 ret
= client_flush_outgoing_queue(client
, state
);
2185 lttng_notification_destroy(notification
);
2186 lttng_dynamic_buffer_reset(&msg_buffer
);
2190 int handle_notification_thread_channel_sample(
2191 struct notification_thread_state
*state
, int pipe
,
2192 enum lttng_domain_type domain
)
2195 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2196 struct channel_state_sample previous_sample
, latest_sample
;
2197 struct channel_info
*channel_info
;
2198 struct cds_lfht_node
*node
;
2199 struct cds_lfht_iter iter
;
2200 struct lttng_channel_trigger_list
*trigger_list
;
2201 struct lttng_trigger_list_element
*trigger_list_element
;
2202 bool previous_sample_available
= false;
2205 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2206 * ensuring that read/write of sampling messages are atomic.
2208 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2209 if (ret
!= sizeof(sample_msg
)) {
2210 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2217 latest_sample
.key
.key
= sample_msg
.key
;
2218 latest_sample
.key
.domain
= domain
;
2219 latest_sample
.highest_usage
= sample_msg
.highest
;
2220 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2224 /* Retrieve the channel's informations */
2225 cds_lfht_lookup(state
->channels_ht
,
2226 hash_channel_key(&latest_sample
.key
),
2230 node
= cds_lfht_iter_get_node(&iter
);
2233 * Not an error since the consumer can push a sample to the pipe
2234 * and the rest of the session daemon could notify us of the
2235 * channel's destruction before we get a chance to process that
2238 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2239 latest_sample
.key
.key
,
2240 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2244 channel_info
= caa_container_of(node
, struct channel_info
,
2246 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
")",
2247 channel_info
->channel_name
,
2248 latest_sample
.key
.key
,
2249 channel_info
->session_name
,
2250 latest_sample
.highest_usage
,
2251 latest_sample
.lowest_usage
);
2253 /* Retrieve the channel's last sample, if it exists, and update it. */
2254 cds_lfht_lookup(state
->channel_state_ht
,
2255 hash_channel_key(&latest_sample
.key
),
2256 match_channel_state_sample
,
2259 node
= cds_lfht_iter_get_node(&iter
);
2261 struct channel_state_sample
*stored_sample
;
2263 /* Update the sample stored. */
2264 stored_sample
= caa_container_of(node
,
2265 struct channel_state_sample
,
2266 channel_state_ht_node
);
2267 memcpy(&previous_sample
, stored_sample
,
2268 sizeof(previous_sample
));
2269 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2270 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2271 previous_sample_available
= true;
2274 * This is the channel's first sample, allocate space for and
2275 * store the new sample.
2277 struct channel_state_sample
*stored_sample
;
2279 stored_sample
= zmalloc(sizeof(*stored_sample
));
2280 if (!stored_sample
) {
2285 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2286 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2287 cds_lfht_add(state
->channel_state_ht
,
2288 hash_channel_key(&stored_sample
->key
),
2289 &stored_sample
->channel_state_ht_node
);
2292 /* Find triggers associated with this channel. */
2293 cds_lfht_lookup(state
->channel_triggers_ht
,
2294 hash_channel_key(&latest_sample
.key
),
2295 match_channel_trigger_list
,
2298 node
= cds_lfht_iter_get_node(&iter
);
2303 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2304 channel_triggers_ht_node
);
2305 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2307 struct lttng_condition
*condition
;
2308 struct lttng_action
*action
;
2309 struct lttng_trigger
*trigger
;
2310 struct notification_client_list
*client_list
;
2311 struct lttng_evaluation
*evaluation
= NULL
;
2313 trigger
= trigger_list_element
->trigger
;
2314 condition
= lttng_trigger_get_condition(trigger
);
2316 action
= lttng_trigger_get_action(trigger
);
2318 /* Notify actions are the only type currently supported. */
2319 assert(lttng_action_get_type(action
) ==
2320 LTTNG_ACTION_TYPE_NOTIFY
);
2323 * Check if any client is subscribed to the result of this
2326 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2327 lttng_condition_hash(condition
),
2331 node
= cds_lfht_iter_get_node(&iter
);
2334 client_list
= caa_container_of(node
,
2335 struct notification_client_list
,
2336 notification_trigger_ht_node
);
2337 if (cds_list_empty(&client_list
->list
)) {
2339 * No clients interested in the evaluation's result,
2345 ret
= evaluate_condition(condition
, &evaluation
, state
,
2346 previous_sample_available
? &previous_sample
: NULL
,
2347 &latest_sample
, channel_info
->capacity
);
2356 /* Dispatch evaluation result to all clients. */
2357 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2358 evaluation
, client_list
, state
,
2359 channel_info
->uid
, channel_info
->gid
);