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 occured 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
);
558 free(client_list_element
);
562 client_list
= caa_container_of(node
, struct notification_client_list
,
563 notification_trigger_ht_node
);
564 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
566 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
572 * Add the client to the list of clients interested in a given trigger
573 * if a "notification" trigger with a corresponding condition was
576 client_list_element
->client
= client
;
577 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
578 cds_list_add(&client_list_element
->node
, &client_list
->list
);
587 free(condition_list_element
);
588 free(client_list_element
);
593 int notification_thread_client_unsubscribe(
594 struct notification_client
*client
,
595 struct lttng_condition
*condition
,
596 struct notification_thread_state
*state
,
597 enum lttng_notification_channel_status
*_status
)
599 struct cds_lfht_iter iter
;
600 struct cds_lfht_node
*node
;
601 struct notification_client_list
*client_list
;
602 struct lttng_condition_list_element
*condition_list_element
,
604 struct notification_client_list_element
*client_list_element
,
606 bool condition_found
= false;
607 enum lttng_notification_channel_status status
=
608 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
610 /* Remove the condition from the client's condition list. */
611 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
612 &client
->condition_list
, node
) {
613 if (!lttng_condition_is_equal(condition_list_element
->condition
,
618 cds_list_del(&condition_list_element
->node
);
620 * The caller may be iterating on the client's conditions to
621 * tear down a client's connection. In this case, the condition
622 * will be destroyed at the end.
624 if (condition
!= condition_list_element
->condition
) {
625 lttng_condition_destroy(
626 condition_list_element
->condition
);
628 free(condition_list_element
);
629 condition_found
= true;
633 if (!condition_found
) {
634 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
639 * Remove the client from the list of clients interested the trigger
640 * matching the condition.
643 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
644 lttng_condition_hash(condition
),
645 match_client_list_condition
,
648 node
= cds_lfht_iter_get_node(&iter
);
653 client_list
= caa_container_of(node
, struct notification_client_list
,
654 notification_trigger_ht_node
);
655 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
656 &client_list
->list
, node
) {
657 if (client_list_element
->client
->socket
!= client
->socket
) {
660 cds_list_del(&client_list_element
->node
);
661 free(client_list_element
);
667 lttng_condition_destroy(condition
);
675 void notification_client_destroy(struct notification_client
*client
,
676 struct notification_thread_state
*state
)
678 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
684 /* Release all conditions to which the client was subscribed. */
685 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
686 &client
->condition_list
, node
) {
687 (void) notification_thread_client_unsubscribe(client
,
688 condition_list_element
->condition
, state
, NULL
);
691 if (client
->socket
>= 0) {
692 (void) lttcomm_close_unix_sock(client
->socket
);
694 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
695 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
700 * Call with rcu_read_lock held (and hold for the lifetime of the returned
704 struct notification_client
*get_client_from_socket(int socket
,
705 struct notification_thread_state
*state
)
707 struct cds_lfht_iter iter
;
708 struct cds_lfht_node
*node
;
709 struct notification_client
*client
= NULL
;
711 cds_lfht_lookup(state
->client_socket_ht
,
712 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
714 (void *) (unsigned long) socket
,
716 node
= cds_lfht_iter_get_node(&iter
);
721 client
= caa_container_of(node
, struct notification_client
,
722 client_socket_ht_node
);
728 bool trigger_applies_to_channel(struct lttng_trigger
*trigger
,
729 struct channel_info
*info
)
731 enum lttng_condition_status status
;
732 struct lttng_condition
*condition
;
733 const char *trigger_session_name
= NULL
;
734 const char *trigger_channel_name
= NULL
;
735 enum lttng_domain_type trigger_domain
;
737 condition
= lttng_trigger_get_condition(trigger
);
742 switch (lttng_condition_get_type(condition
)) {
743 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
744 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
750 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
752 assert(status
== LTTNG_CONDITION_STATUS_OK
);
753 if (info
->key
.domain
!= trigger_domain
) {
757 status
= lttng_condition_buffer_usage_get_session_name(
758 condition
, &trigger_session_name
);
759 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_session_name
);
761 status
= lttng_condition_buffer_usage_get_channel_name(
762 condition
, &trigger_channel_name
);
763 assert((status
== LTTNG_CONDITION_STATUS_OK
) && trigger_channel_name
);
765 if (strcmp(info
->session_name
, trigger_session_name
)) {
768 if (strcmp(info
->channel_name
, trigger_channel_name
)) {
778 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
779 struct notification_client
*client
)
781 bool applies
= false;
782 struct lttng_condition_list_element
*condition_list_element
;
784 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
786 applies
= lttng_condition_is_equal(
787 condition_list_element
->condition
,
788 lttng_trigger_get_condition(trigger
));
797 int handle_notification_thread_command_add_channel(
798 struct notification_thread_state
*state
,
799 struct channel_info
*channel_info
,
800 enum lttng_error_code
*cmd_result
)
802 struct cds_list_head trigger_list
;
803 struct channel_info
*new_channel_info
;
804 struct channel_key
*channel_key
;
805 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
806 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
807 int trigger_count
= 0;
808 struct cds_lfht_iter iter
;
810 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
811 channel_info
->channel_name
, channel_info
->session_name
,
812 channel_info
->key
.key
, channel_info
->key
.domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
814 CDS_INIT_LIST_HEAD(&trigger_list
);
816 new_channel_info
= channel_info_copy(channel_info
);
817 if (!new_channel_info
) {
821 channel_key
= &new_channel_info
->key
;
823 /* Build a list of all triggers applying to the new channel. */
824 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
826 struct lttng_trigger_list_element
*new_element
;
828 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
833 new_element
= zmalloc(sizeof(*new_element
));
837 CDS_INIT_LIST_HEAD(&new_element
->node
);
838 new_element
->trigger
= trigger_ht_element
->trigger
;
839 cds_list_add(&new_element
->node
, &trigger_list
);
843 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
845 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
846 if (!channel_trigger_list
) {
849 channel_trigger_list
->channel_key
= *channel_key
;
850 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
851 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
852 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
855 /* Add channel to the channel_ht which owns the channel_infos. */
856 cds_lfht_add(state
->channels_ht
,
857 hash_channel_key(channel_key
),
858 &new_channel_info
->channels_ht_node
);
860 * Add the list of triggers associated with this channel to the
861 * channel_triggers_ht.
863 cds_lfht_add(state
->channel_triggers_ht
,
864 hash_channel_key(channel_key
),
865 &channel_trigger_list
->channel_triggers_ht_node
);
867 *cmd_result
= LTTNG_OK
;
870 /* Empty trigger list */
871 channel_info_destroy(new_channel_info
);
876 int handle_notification_thread_command_remove_channel(
877 struct notification_thread_state
*state
,
878 uint64_t channel_key
, enum lttng_domain_type domain
,
879 enum lttng_error_code
*cmd_result
)
881 struct cds_lfht_node
*node
;
882 struct cds_lfht_iter iter
;
883 struct lttng_channel_trigger_list
*trigger_list
;
884 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
885 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
886 struct channel_info
*channel_info
;
888 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
889 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
893 cds_lfht_lookup(state
->channel_triggers_ht
,
894 hash_channel_key(&key
),
895 match_channel_trigger_list
,
898 node
= cds_lfht_iter_get_node(&iter
);
900 * There is a severe internal error if we are being asked to remove a
901 * channel that doesn't exist.
904 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
908 /* Free the list of triggers associated with this channel. */
909 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
910 channel_triggers_ht_node
);
911 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
912 &trigger_list
->list
, node
) {
913 cds_list_del(&trigger_list_element
->node
);
914 free(trigger_list_element
);
916 cds_lfht_del(state
->channel_triggers_ht
, node
);
919 /* Free sampled channel state. */
920 cds_lfht_lookup(state
->channel_state_ht
,
921 hash_channel_key(&key
),
922 match_channel_state_sample
,
925 node
= cds_lfht_iter_get_node(&iter
);
927 * This is expected to be NULL if the channel is destroyed before we
931 struct channel_state_sample
*sample
= caa_container_of(node
,
932 struct channel_state_sample
,
933 channel_state_ht_node
);
935 cds_lfht_del(state
->channel_state_ht
, node
);
939 /* Remove the channel from the channels_ht and free it. */
940 cds_lfht_lookup(state
->channels_ht
,
941 hash_channel_key(&key
),
945 node
= cds_lfht_iter_get_node(&iter
);
947 channel_info
= caa_container_of(node
, struct channel_info
,
949 cds_lfht_del(state
->channels_ht
, node
);
950 channel_info_destroy(channel_info
);
953 *cmd_result
= LTTNG_OK
;
958 int condition_is_supported(struct lttng_condition
*condition
)
962 switch (lttng_condition_get_type(condition
)) {
963 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
964 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
966 enum lttng_domain_type domain
;
968 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
975 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
981 * Older kernel tracers don't expose the API to monitor their
982 * buffers. Therefore, we reject triggers that require that
983 * mechanism to be available to be evaluated.
985 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
997 * FIXME A client's credentials are not checked when registering a trigger, nor
998 * are they stored alongside with the trigger.
1000 * The effects of this are benign since:
1001 * - The client will succeed in registering the trigger, as it is valid,
1002 * - The trigger will, internally, be bound to the channel,
1003 * - The notifications will not be sent since the client's credentials
1004 * are checked against the channel at that moment.
1006 * If this function returns a non-zero value, it means something is
1007 * fundamentally and the whole subsystem/thread will be torn down.
1009 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1013 int handle_notification_thread_command_register_trigger(
1014 struct notification_thread_state
*state
,
1015 struct lttng_trigger
*trigger
,
1016 enum lttng_error_code
*cmd_result
)
1019 struct lttng_condition
*condition
;
1020 struct notification_client
*client
;
1021 struct notification_client_list
*client_list
= NULL
;
1022 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1023 struct notification_client_list_element
*client_list_element
, *tmp
;
1024 struct cds_lfht_node
*node
;
1025 struct cds_lfht_iter iter
;
1026 struct channel_info
*channel
;
1027 bool free_trigger
= true;
1031 condition
= lttng_trigger_get_condition(trigger
);
1034 ret
= condition_is_supported(condition
);
1037 } else if (ret
== 0) {
1038 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1041 /* Feature is supported, continue. */
1045 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1046 if (!trigger_ht_element
) {
1051 /* Add trigger to the trigger_ht. */
1052 cds_lfht_node_init(&trigger_ht_element
->node
);
1053 trigger_ht_element
->trigger
= trigger
;
1055 node
= cds_lfht_add_unique(state
->triggers_ht
,
1056 lttng_condition_hash(condition
),
1059 &trigger_ht_element
->node
);
1060 if (node
!= &trigger_ht_element
->node
) {
1061 /* Not a fatal error, simply report it to the client. */
1062 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1063 goto error_free_ht_element
;
1067 * Ownership of the trigger and of its wrapper was transfered to
1070 trigger_ht_element
= NULL
;
1071 free_trigger
= false;
1074 * The rest only applies to triggers that have a "notify" action.
1075 * It is not skipped as this is the only action type currently
1078 client_list
= zmalloc(sizeof(*client_list
));
1081 goto error_free_ht_element
;
1083 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
1084 CDS_INIT_LIST_HEAD(&client_list
->list
);
1085 client_list
->trigger
= trigger
;
1087 /* Build a list of clients to which this new trigger applies. */
1088 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1089 client_socket_ht_node
) {
1090 if (!trigger_applies_to_client(trigger
, client
)) {
1094 client_list_element
= zmalloc(sizeof(*client_list_element
));
1095 if (!client_list_element
) {
1097 goto error_free_client_list
;
1099 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1100 client_list_element
->client
= client
;
1101 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1104 cds_lfht_add(state
->notification_trigger_clients_ht
,
1105 lttng_condition_hash(condition
),
1106 &client_list
->notification_trigger_ht_node
);
1108 * Client list ownership transferred to the
1109 * notification_trigger_clients_ht.
1114 * Add the trigger to list of triggers bound to the channels currently
1117 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1119 struct lttng_trigger_list_element
*trigger_list_element
;
1120 struct lttng_channel_trigger_list
*trigger_list
;
1122 if (!trigger_applies_to_channel(trigger
, channel
)) {
1126 cds_lfht_lookup(state
->channel_triggers_ht
,
1127 hash_channel_key(&channel
->key
),
1128 match_channel_trigger_list
,
1131 node
= cds_lfht_iter_get_node(&iter
);
1133 trigger_list
= caa_container_of(node
,
1134 struct lttng_channel_trigger_list
,
1135 channel_triggers_ht_node
);
1137 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1138 if (!trigger_list_element
) {
1140 goto error_free_client_list
;
1142 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1143 trigger_list_element
->trigger
= trigger
;
1144 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1146 /* A trigger can only apply to one channel. */
1150 *cmd_result
= LTTNG_OK
;
1151 error_free_client_list
:
1153 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1154 &client_list
->list
, node
) {
1155 free(client_list_element
);
1159 error_free_ht_element
:
1160 free(trigger_ht_element
);
1163 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1165 lttng_condition_destroy(condition
);
1166 lttng_action_destroy(action
);
1167 lttng_trigger_destroy(trigger
);
1174 int handle_notification_thread_command_unregister_trigger(
1175 struct notification_thread_state
*state
,
1176 struct lttng_trigger
*trigger
,
1177 enum lttng_error_code
*_cmd_reply
)
1179 struct cds_lfht_iter iter
;
1180 struct cds_lfht_node
*node
, *triggers_ht_node
;
1181 struct lttng_channel_trigger_list
*trigger_list
;
1182 struct notification_client_list
*client_list
;
1183 struct notification_client_list_element
*client_list_element
, *tmp
;
1184 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1185 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1187 struct lttng_action
*action
;
1188 enum lttng_error_code cmd_reply
;
1192 cds_lfht_lookup(state
->triggers_ht
,
1193 lttng_condition_hash(condition
),
1197 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1198 if (!triggers_ht_node
) {
1199 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1202 cmd_reply
= LTTNG_OK
;
1205 /* Remove trigger from channel_triggers_ht. */
1206 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1207 channel_triggers_ht_node
) {
1208 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1210 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1211 &trigger_list
->list
, node
) {
1212 struct lttng_condition
*current_condition
=
1213 lttng_trigger_get_condition(
1214 trigger_element
->trigger
);
1216 assert(current_condition
);
1217 if (!lttng_condition_is_equal(condition
,
1218 current_condition
)) {
1222 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1223 cds_list_del(&trigger_element
->node
);
1224 /* A trigger can only appear once per channel */
1230 * Remove and release the client list from
1231 * notification_trigger_clients_ht.
1233 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1234 lttng_condition_hash(condition
),
1238 node
= cds_lfht_iter_get_node(&iter
);
1240 client_list
= caa_container_of(node
, struct notification_client_list
,
1241 notification_trigger_ht_node
);
1242 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1243 &client_list
->list
, node
) {
1244 free(client_list_element
);
1246 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1249 /* Remove trigger from triggers_ht. */
1250 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1251 struct lttng_trigger_ht_element
, node
);
1252 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1254 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1255 lttng_condition_destroy(condition
);
1256 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1257 lttng_action_destroy(action
);
1258 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1259 free(trigger_ht_element
);
1263 *_cmd_reply
= cmd_reply
;
1268 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1269 int handle_notification_thread_command(
1270 struct notification_thread_handle
*handle
,
1271 struct notification_thread_state
*state
)
1275 struct notification_thread_command
*cmd
;
1277 /* Read event_fd to put it back into a quiescent state. */
1278 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
, sizeof(counter
));
1283 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1284 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1285 struct notification_thread_command
, cmd_list_node
);
1286 switch (cmd
->type
) {
1287 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1288 DBG("[notification-thread] Received register trigger command");
1289 ret
= handle_notification_thread_command_register_trigger(
1290 state
, cmd
->parameters
.trigger
,
1293 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1294 DBG("[notification-thread] Received unregister trigger command");
1295 ret
= handle_notification_thread_command_unregister_trigger(
1296 state
, cmd
->parameters
.trigger
,
1299 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1300 DBG("[notification-thread] Received add channel command");
1301 ret
= handle_notification_thread_command_add_channel(
1302 state
, &cmd
->parameters
.add_channel
,
1305 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
1306 DBG("[notification-thread] Received remove channel command");
1307 ret
= handle_notification_thread_command_remove_channel(
1308 state
, cmd
->parameters
.remove_channel
.key
,
1309 cmd
->parameters
.remove_channel
.domain
,
1312 case NOTIFICATION_COMMAND_TYPE_QUIT
:
1313 DBG("[notification-thread] Received quit command");
1314 cmd
->reply_code
= LTTNG_OK
;
1318 ERR("[notification-thread] Unknown internal command received");
1326 cds_list_del(&cmd
->cmd_list_node
);
1327 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1328 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1331 /* Wake-up and return a fatal error to the calling thread. */
1332 lttng_waiter_wake_up(&cmd
->reply_waiter
);
1333 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
1334 cmd
->reply_code
= LTTNG_ERR_FATAL
;
1336 /* Indicate a fatal error to the caller. */
1341 unsigned long hash_client_socket(int socket
)
1343 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
1347 int socket_set_non_blocking(int socket
)
1351 /* Set the pipe as non-blocking. */
1352 ret
= fcntl(socket
, F_GETFL
, 0);
1354 PERROR("fcntl get socket flags");
1359 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
1361 PERROR("fcntl set O_NONBLOCK socket flag");
1364 DBG("Client socket (fd = %i) set as non-blocking", socket
);
1370 int client_reset_inbound_state(struct notification_client
*client
)
1374 ret
= lttng_dynamic_buffer_set_size(
1375 &client
->communication
.inbound
.buffer
, 0);
1378 client
->communication
.inbound
.bytes_to_receive
=
1379 sizeof(struct lttng_notification_channel_message
);
1380 client
->communication
.inbound
.msg_type
=
1381 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
1382 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
1383 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
1384 ret
= lttng_dynamic_buffer_set_size(
1385 &client
->communication
.inbound
.buffer
,
1386 client
->communication
.inbound
.bytes_to_receive
);
1390 int handle_notification_thread_client_connect(
1391 struct notification_thread_state
*state
)
1394 struct notification_client
*client
;
1396 DBG("[notification-thread] Handling new notification channel client connection");
1398 client
= zmalloc(sizeof(*client
));
1404 CDS_INIT_LIST_HEAD(&client
->condition_list
);
1405 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
1406 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
1407 client
->communication
.inbound
.expect_creds
= true;
1408 ret
= client_reset_inbound_state(client
);
1410 ERR("[notification-thread] Failed to reset client communication's inbound state");
1415 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
1417 ERR("[notification-thread] Failed to accept new notification channel client connection");
1422 client
->socket
= ret
;
1424 ret
= socket_set_non_blocking(client
->socket
);
1426 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
1430 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
1432 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
1437 ret
= lttng_poll_add(&state
->events
, client
->socket
,
1438 LPOLLIN
| LPOLLERR
|
1439 LPOLLHUP
| LPOLLRDHUP
);
1441 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
1445 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
1449 cds_lfht_add(state
->client_socket_ht
,
1450 hash_client_socket(client
->socket
),
1451 &client
->client_socket_ht_node
);
1456 notification_client_destroy(client
, state
);
1460 int handle_notification_thread_client_disconnect(
1462 struct notification_thread_state
*state
)
1465 struct notification_client
*client
;
1468 DBG("[notification-thread] Closing client connection (socket fd = %i)",
1470 client
= get_client_from_socket(client_socket
, state
);
1472 /* Internal state corruption, fatal error. */
1473 ERR("[notification-thread] Unable to find client (socket fd = %i)",
1479 ret
= lttng_poll_del(&state
->events
, client_socket
);
1481 ERR("[notification-thread] Failed to remove client socket from poll set");
1483 cds_lfht_del(state
->client_socket_ht
,
1484 &client
->client_socket_ht_node
);
1485 notification_client_destroy(client
, state
);
1491 int handle_notification_thread_client_disconnect_all(
1492 struct notification_thread_state
*state
)
1494 struct cds_lfht_iter iter
;
1495 struct notification_client
*client
;
1496 bool error_encoutered
= false;
1499 DBG("[notification-thread] Closing all client connections");
1500 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1501 client_socket_ht_node
) {
1504 ret
= handle_notification_thread_client_disconnect(
1505 client
->socket
, state
);
1507 error_encoutered
= true;
1511 return error_encoutered
? 1 : 0;
1514 int handle_notification_thread_trigger_unregister_all(
1515 struct notification_thread_state
*state
)
1517 bool error_occurred
= false;
1518 struct cds_lfht_iter iter
;
1519 struct lttng_trigger_ht_element
*trigger_ht_element
;
1521 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1523 int ret
= handle_notification_thread_command_unregister_trigger(
1524 state
, trigger_ht_element
->trigger
, NULL
);
1526 error_occurred
= true;
1529 return error_occurred
? -1 : 0;
1533 int client_flush_outgoing_queue(struct notification_client
*client
,
1534 struct notification_thread_state
*state
)
1537 size_t to_send_count
;
1539 assert(client
->communication
.outbound
.buffer
.size
!= 0);
1540 to_send_count
= client
->communication
.outbound
.buffer
.size
;
1541 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
1544 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
1545 client
->communication
.outbound
.buffer
.data
,
1547 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
1548 (ret
> 0 && ret
< to_send_count
)) {
1549 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
1551 to_send_count
-= max(ret
, 0);
1553 memcpy(client
->communication
.outbound
.buffer
.data
,
1554 client
->communication
.outbound
.buffer
.data
+
1555 client
->communication
.outbound
.buffer
.size
- to_send_count
,
1557 ret
= lttng_dynamic_buffer_set_size(
1558 &client
->communication
.outbound
.buffer
,
1565 * We want to be notified whenever there is buffer space
1566 * available to send the rest of the payload.
1568 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1569 CLIENT_POLL_MASK_IN_OUT
);
1573 } else if (ret
< 0) {
1574 /* Generic error, disconnect the client. */
1575 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
1577 ret
= handle_notification_thread_client_disconnect(
1578 client
->socket
, state
);
1583 /* No error and flushed the queue completely. */
1584 ret
= lttng_dynamic_buffer_set_size(
1585 &client
->communication
.outbound
.buffer
, 0);
1589 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
1590 CLIENT_POLL_MASK_IN
);
1595 client
->communication
.outbound
.queued_command_reply
= false;
1596 client
->communication
.outbound
.dropped_notification
= false;
1605 int client_send_command_reply(struct notification_client
*client
,
1606 struct notification_thread_state
*state
,
1607 enum lttng_notification_channel_status status
)
1610 struct lttng_notification_channel_command_reply reply
= {
1611 .status
= (int8_t) status
,
1613 struct lttng_notification_channel_message msg
= {
1614 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
1615 .size
= sizeof(reply
),
1617 char buffer
[sizeof(msg
) + sizeof(reply
)];
1619 if (client
->communication
.outbound
.queued_command_reply
) {
1620 /* Protocol error. */
1624 memcpy(buffer
, &msg
, sizeof(msg
));
1625 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
1626 DBG("[notification-thread] Send command reply (%i)", (int) status
);
1628 /* Enqueue buffer to outgoing queue and flush it. */
1629 ret
= lttng_dynamic_buffer_append(
1630 &client
->communication
.outbound
.buffer
,
1631 buffer
, sizeof(buffer
));
1636 ret
= client_flush_outgoing_queue(client
, state
);
1641 if (client
->communication
.outbound
.buffer
.size
!= 0) {
1642 /* Queue could not be emptied. */
1643 client
->communication
.outbound
.queued_command_reply
= true;
1652 int client_dispatch_message(struct notification_client
*client
,
1653 struct notification_thread_state
*state
)
1657 if (client
->communication
.inbound
.msg_type
!=
1658 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
1659 client
->communication
.inbound
.msg_type
!=
1660 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
1661 !client
->validated
) {
1662 WARN("[notification-thread] client attempted a command before handshake");
1667 switch (client
->communication
.inbound
.msg_type
) {
1668 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
1671 * Receiving message header. The function will be called again
1672 * once the rest of the message as been received and can be
1675 const struct lttng_notification_channel_message
*msg
;
1677 assert(sizeof(*msg
) ==
1678 client
->communication
.inbound
.buffer
.size
);
1679 msg
= (const struct lttng_notification_channel_message
*)
1680 client
->communication
.inbound
.buffer
.data
;
1682 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
1683 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
1688 switch (msg
->type
) {
1689 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1690 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1691 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1695 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
1699 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
1700 client
->communication
.inbound
.msg_type
=
1701 (enum lttng_notification_channel_message_type
) msg
->type
;
1702 ret
= lttng_dynamic_buffer_set_size(
1703 &client
->communication
.inbound
.buffer
, msg
->size
);
1709 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
1711 struct lttng_notification_channel_command_handshake
*handshake_client
;
1712 struct lttng_notification_channel_command_handshake handshake_reply
= {
1713 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
1714 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
1716 struct lttng_notification_channel_message msg_header
= {
1717 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
1718 .size
= sizeof(handshake_reply
),
1720 enum lttng_notification_channel_status status
=
1721 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1722 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
1724 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
1725 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
1726 sizeof(handshake_reply
));
1729 (struct lttng_notification_channel_command_handshake
*)
1730 client
->communication
.inbound
.buffer
.data
;
1731 client
->major
= handshake_client
->major
;
1732 client
->minor
= handshake_client
->minor
;
1733 if (!client
->communication
.inbound
.creds_received
) {
1734 ERR("[notification-thread] No credentials received from client");
1739 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
1740 &client
->communication
.inbound
.creds
);
1741 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
1742 &client
->communication
.inbound
.creds
);
1743 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
1744 client
->uid
, client
->gid
, (int) client
->major
,
1745 (int) client
->minor
);
1747 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
1748 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
1751 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
1752 send_buffer
, sizeof(send_buffer
));
1754 ERR("[notification-thread] Failed to send protocol version to notification channel client");
1758 ret
= client_flush_outgoing_queue(client
, state
);
1763 ret
= client_send_command_reply(client
, state
, status
);
1765 ERR("[notification-thread] Failed to send reply to notification channel client");
1769 /* Set reception state to receive the next message header. */
1770 ret
= client_reset_inbound_state(client
);
1772 ERR("[notification-thread] Failed to reset client communication's inbound state");
1775 client
->validated
= true;
1778 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
1779 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
1781 struct lttng_condition
*condition
;
1782 enum lttng_notification_channel_status status
=
1783 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1784 const struct lttng_buffer_view condition_view
=
1785 lttng_buffer_view_from_dynamic_buffer(
1786 &client
->communication
.inbound
.buffer
,
1788 size_t expected_condition_size
=
1789 client
->communication
.inbound
.buffer
.size
;
1791 ret
= lttng_condition_create_from_buffer(&condition_view
,
1793 if (ret
!= expected_condition_size
) {
1794 ERR("[notification-thread] Malformed condition received from client");
1798 if (client
->communication
.inbound
.msg_type
==
1799 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
1801 * FIXME The current state should be evaluated on
1804 ret
= notification_thread_client_subscribe(client
,
1805 condition
, state
, &status
);
1807 ret
= notification_thread_client_unsubscribe(client
,
1808 condition
, state
, &status
);
1814 ret
= client_send_command_reply(client
, state
, status
);
1816 ERR("[notification-thread] Failed to send reply to notification channel client");
1820 /* Set reception state to receive the next message header. */
1821 ret
= client_reset_inbound_state(client
);
1823 ERR("[notification-thread] Failed to reset client communication's inbound state");
1835 /* Incoming data from client. */
1836 int handle_notification_thread_client_in(
1837 struct notification_thread_state
*state
, int socket
)
1840 struct notification_client
*client
;
1844 client
= get_client_from_socket(socket
, state
);
1846 /* Internal error, abort. */
1851 offset
= client
->communication
.inbound
.buffer
.size
-
1852 client
->communication
.inbound
.bytes_to_receive
;
1853 if (client
->communication
.inbound
.expect_creds
) {
1854 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
1855 client
->communication
.inbound
.buffer
.data
+ offset
,
1856 client
->communication
.inbound
.bytes_to_receive
,
1857 &client
->communication
.inbound
.creds
);
1859 client
->communication
.inbound
.expect_creds
= false;
1860 client
->communication
.inbound
.creds_received
= true;
1863 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
1864 client
->communication
.inbound
.buffer
.data
+ offset
,
1865 client
->communication
.inbound
.bytes_to_receive
);
1868 goto error_disconnect_client
;
1871 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
1872 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
1873 ret
= client_dispatch_message(client
, state
);
1876 * Only returns an error if this client must be
1879 goto error_disconnect_client
;
1886 error_disconnect_client
:
1887 ret
= handle_notification_thread_client_disconnect(socket
, state
);
1891 /* Client ready to receive outgoing data. */
1892 int handle_notification_thread_client_out(
1893 struct notification_thread_state
*state
, int socket
)
1896 struct notification_client
*client
;
1898 client
= get_client_from_socket(socket
, state
);
1900 /* Internal error, abort. */
1905 ret
= client_flush_outgoing_queue(client
, state
);
1914 bool evaluate_buffer_usage_condition(struct lttng_condition
*condition
,
1915 struct channel_state_sample
*sample
, uint64_t buffer_capacity
)
1917 bool result
= false;
1919 enum lttng_condition_type condition_type
;
1920 struct lttng_condition_buffer_usage
*use_condition
= container_of(
1921 condition
, struct lttng_condition_buffer_usage
,
1928 if (use_condition
->threshold_bytes
.set
) {
1929 threshold
= use_condition
->threshold_bytes
.value
;
1932 * Threshold was expressed as a ratio.
1934 * TODO the threshold (in bytes) of conditions expressed
1935 * as a ratio of total buffer size could be cached to
1936 * forego this double-multiplication or it could be performed
1937 * as fixed-point math.
1939 * Note that caching should accomodate the case where the
1940 * condition applies to multiple channels (i.e. don't assume
1941 * that all channels matching my_chann* have the same size...)
1943 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
1944 (double) buffer_capacity
);
1947 condition_type
= lttng_condition_get_type(condition
);
1948 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
1949 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1950 threshold
, sample
->highest_usage
);
1953 * The low condition should only be triggered once _all_ of the
1954 * streams in a channel have gone below the "low" threshold.
1956 if (sample
->highest_usage
<= threshold
) {
1960 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
1961 threshold
, sample
->highest_usage
);
1964 * For high buffer usage scenarios, we want to trigger whenever
1965 * _any_ of the streams has reached the "high" threshold.
1967 if (sample
->highest_usage
>= threshold
) {
1976 int evaluate_condition(struct lttng_condition
*condition
,
1977 struct lttng_evaluation
**evaluation
,
1978 struct notification_thread_state
*state
,
1979 struct channel_state_sample
*previous_sample
,
1980 struct channel_state_sample
*latest_sample
,
1981 uint64_t buffer_capacity
)
1984 enum lttng_condition_type condition_type
;
1985 bool previous_sample_result
;
1986 bool latest_sample_result
;
1988 condition_type
= lttng_condition_get_type(condition
);
1989 /* No other condition type supported for the moment. */
1990 assert(condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
1991 condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
1993 previous_sample_result
= evaluate_buffer_usage_condition(condition
,
1994 previous_sample
, buffer_capacity
);
1995 latest_sample_result
= evaluate_buffer_usage_condition(condition
,
1996 latest_sample
, buffer_capacity
);
1998 if (!latest_sample_result
||
1999 (previous_sample_result
== latest_sample_result
)) {
2001 * Only trigger on a condition evaluation transition.
2003 * NOTE: This edge-triggered logic may not be appropriate for
2004 * future condition types.
2009 if (evaluation
&& latest_sample_result
) {
2010 *evaluation
= lttng_evaluation_buffer_usage_create(
2012 latest_sample
->highest_usage
,
2024 int client_enqueue_dropped_notification(struct notification_client
*client
,
2025 struct notification_thread_state
*state
)
2028 struct lttng_notification_channel_message msg
= {
2029 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
2033 ret
= lttng_dynamic_buffer_append(
2034 &client
->communication
.outbound
.buffer
, &msg
,
2040 int send_evaluation_to_clients(struct lttng_trigger
*trigger
,
2041 struct lttng_evaluation
*evaluation
,
2042 struct notification_client_list
* client_list
,
2043 struct notification_thread_state
*state
,
2044 uid_t channel_uid
, gid_t channel_gid
)
2047 struct lttng_dynamic_buffer msg_buffer
;
2048 struct notification_client_list_element
*client_list_element
, *tmp
;
2049 struct lttng_notification
*notification
;
2050 struct lttng_condition
*condition
;
2051 ssize_t expected_notification_size
, notification_size
;
2052 struct lttng_notification_channel_message msg
;
2054 lttng_dynamic_buffer_init(&msg_buffer
);
2056 condition
= lttng_trigger_get_condition(trigger
);
2059 notification
= lttng_notification_create(condition
, evaluation
);
2060 if (!notification
) {
2065 expected_notification_size
= lttng_notification_serialize(notification
,
2067 if (expected_notification_size
< 0) {
2068 ERR("[notification-thread] Failed to get size of serialized notification");
2073 msg
.type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
;
2074 msg
.size
= (uint32_t) expected_notification_size
;
2075 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg
, sizeof(msg
));
2080 ret
= lttng_dynamic_buffer_set_size(&msg_buffer
,
2081 msg_buffer
.size
+ expected_notification_size
);
2086 notification_size
= lttng_notification_serialize(notification
,
2087 msg_buffer
.data
+ sizeof(msg
));
2088 if (notification_size
!= expected_notification_size
) {
2089 ERR("[notification-thread] Failed to serialize notification");
2094 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2095 &client_list
->list
, node
) {
2096 struct notification_client
*client
=
2097 client_list_element
->client
;
2099 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
2101 /* Client is not allowed to monitor this channel. */
2102 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
2106 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
2107 client
->socket
, msg_buffer
.size
);
2108 if (client
->communication
.outbound
.buffer
.size
) {
2110 * Outgoing data is already buffered for this client;
2111 * drop the notification and enqueue a "dropped
2112 * notification" message if this is the first dropped
2113 * notification since the socket spilled-over to the
2116 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
2118 if (!client
->communication
.outbound
.dropped_notification
) {
2119 client
->communication
.outbound
.dropped_notification
= true;
2120 ret
= client_enqueue_dropped_notification(
2129 ret
= lttng_dynamic_buffer_append_buffer(
2130 &client
->communication
.outbound
.buffer
,
2136 ret
= client_flush_outgoing_queue(client
, state
);
2143 lttng_notification_destroy(notification
);
2144 lttng_dynamic_buffer_reset(&msg_buffer
);
2148 int handle_notification_thread_channel_sample(
2149 struct notification_thread_state
*state
, int pipe
,
2150 enum lttng_domain_type domain
)
2153 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2154 struct channel_state_sample previous_sample
, latest_sample
;
2155 struct channel_info
*channel_info
;
2156 struct cds_lfht_node
*node
;
2157 struct cds_lfht_iter iter
;
2158 struct lttng_channel_trigger_list
*trigger_list
;
2159 struct lttng_trigger_list_element
*trigger_list_element
;
2160 bool previous_sample_available
= false;
2163 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2164 * ensuring that read/write of sampling messages are atomic.
2166 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2167 if (ret
!= sizeof(sample_msg
)) {
2168 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2175 latest_sample
.key
.key
= sample_msg
.key
;
2176 latest_sample
.key
.domain
= domain
;
2177 latest_sample
.highest_usage
= sample_msg
.highest
;
2178 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2182 /* Retrieve the channel's informations */
2183 cds_lfht_lookup(state
->channels_ht
,
2184 hash_channel_key(&latest_sample
.key
),
2188 node
= cds_lfht_iter_get_node(&iter
);
2191 * Not an error since the consumer can push a sample to the pipe
2192 * and the rest of the session daemon could notify us of the
2193 * channel's destruction before we get a chance to process that
2196 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2197 latest_sample
.key
.key
,
2198 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2202 channel_info
= caa_container_of(node
, struct channel_info
,
2204 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
")",
2205 channel_info
->channel_name
,
2206 latest_sample
.key
.key
,
2207 channel_info
->session_name
,
2208 latest_sample
.highest_usage
,
2209 latest_sample
.lowest_usage
);
2211 /* Retrieve the channel's last sample, if it exists, and update it. */
2212 cds_lfht_lookup(state
->channel_state_ht
,
2213 hash_channel_key(&latest_sample
.key
),
2214 match_channel_state_sample
,
2217 node
= cds_lfht_iter_get_node(&iter
);
2219 struct channel_state_sample
*stored_sample
;
2221 /* Update the sample stored. */
2222 stored_sample
= caa_container_of(node
,
2223 struct channel_state_sample
,
2224 channel_state_ht_node
);
2225 memcpy(&previous_sample
, stored_sample
,
2226 sizeof(previous_sample
));
2227 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2228 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2229 previous_sample_available
= true;
2232 * This is the channel's first sample, allocate space for and
2233 * store the new sample.
2235 struct channel_state_sample
*stored_sample
;
2237 stored_sample
= zmalloc(sizeof(*stored_sample
));
2238 if (!stored_sample
) {
2243 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2244 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2245 cds_lfht_add(state
->channel_state_ht
,
2246 hash_channel_key(&stored_sample
->key
),
2247 &stored_sample
->channel_state_ht_node
);
2250 /* Find triggers associated with this channel. */
2251 cds_lfht_lookup(state
->channel_triggers_ht
,
2252 hash_channel_key(&latest_sample
.key
),
2253 match_channel_trigger_list
,
2256 node
= cds_lfht_iter_get_node(&iter
);
2261 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
2262 channel_triggers_ht_node
);
2263 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
2265 struct lttng_condition
*condition
;
2266 struct lttng_action
*action
;
2267 struct lttng_trigger
*trigger
;
2268 struct notification_client_list
*client_list
;
2269 struct lttng_evaluation
*evaluation
= NULL
;
2271 trigger
= trigger_list_element
->trigger
;
2272 condition
= lttng_trigger_get_condition(trigger
);
2274 action
= lttng_trigger_get_action(trigger
);
2276 /* Notify actions are the only type currently supported. */
2277 assert(lttng_action_get_type(action
) ==
2278 LTTNG_ACTION_TYPE_NOTIFY
);
2281 * Check if any client is subscribed to the result of this
2284 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
2285 lttng_condition_hash(condition
),
2289 node
= cds_lfht_iter_get_node(&iter
);
2292 client_list
= caa_container_of(node
,
2293 struct notification_client_list
,
2294 notification_trigger_ht_node
);
2295 if (cds_list_empty(&client_list
->list
)) {
2297 * No clients interested in the evaluation's result,
2303 ret
= evaluate_condition(condition
, &evaluation
, state
,
2304 previous_sample_available
? &previous_sample
: NULL
,
2305 &latest_sample
, channel_info
->capacity
);
2314 /* Dispatch evaluation result to all clients. */
2315 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
2316 evaluation
, client_list
, state
,
2317 channel_info
->uid
, channel_info
->gid
);