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-internal.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/condition/session-consumed-size-internal.h>
36 #include <lttng/condition/session-rotation-internal.h>
37 #include <lttng/notification/channel-internal.h>
45 #include "notification-thread.h"
46 #include "notification-thread-events.h"
47 #include "notification-thread-commands.h"
48 #include "lttng-sessiond.h"
51 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
52 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
54 enum lttng_object_type
{
55 LTTNG_OBJECT_TYPE_UNKNOWN
,
56 LTTNG_OBJECT_TYPE_NONE
,
57 LTTNG_OBJECT_TYPE_CHANNEL
,
58 LTTNG_OBJECT_TYPE_SESSION
,
61 struct lttng_trigger_list_element
{
62 /* No ownership of the trigger object is assumed. */
63 const struct lttng_trigger
*trigger
;
64 struct cds_list_head node
;
67 struct lttng_channel_trigger_list
{
68 struct channel_key channel_key
;
69 /* List of struct lttng_trigger_list_element. */
70 struct cds_list_head list
;
71 /* Node in the channel_triggers_ht */
72 struct cds_lfht_node channel_triggers_ht_node
;
76 * List of triggers applying to a given session.
79 * - lttng_session_trigger_list_create()
80 * - lttng_session_trigger_list_build()
81 * - lttng_session_trigger_list_destroy()
82 * - lttng_session_trigger_list_add()
84 struct lttng_session_trigger_list
{
86 * Not owned by this; points to the session_info structure's
89 const char *session_name
;
90 /* List of struct lttng_trigger_list_element. */
91 struct cds_list_head list
;
92 /* Node in the session_triggers_ht */
93 struct cds_lfht_node session_triggers_ht_node
;
95 * Weak reference to the notification system's session triggers
98 * The session trigger list structure structure is owned by
99 * the session's session_info.
101 * The session_info is kept alive the the channel_infos holding a
102 * reference to it (reference counting). When those channels are
103 * destroyed (at runtime or on teardown), the reference they hold
104 * to the session_info are released. On destruction of session_info,
105 * session_info_destroy() will remove the list of triggers applying
106 * to this session from the notification system's state.
108 * This implies that the session_triggers_ht must be destroyed
109 * after the channels.
111 struct cds_lfht
*session_triggers_ht
;
112 /* Used for delayed RCU reclaim. */
113 struct rcu_head rcu_node
;
116 struct lttng_trigger_ht_element
{
117 struct lttng_trigger
*trigger
;
118 struct cds_lfht_node node
;
121 struct lttng_condition_list_element
{
122 struct lttng_condition
*condition
;
123 struct cds_list_head node
;
126 struct notification_client_list_element
{
127 struct notification_client
*client
;
128 struct cds_list_head node
;
131 struct notification_client_list
{
132 struct lttng_trigger
*trigger
;
133 struct cds_list_head list
;
134 struct cds_lfht_node notification_trigger_ht_node
;
137 struct notification_client
{
139 /* Client protocol version. */
140 uint8_t major
, minor
;
144 * Indicates if the credentials and versions of the client have been
149 * Conditions to which the client's notification channel is subscribed.
150 * List of struct lttng_condition_list_node. The condition member is
151 * owned by the client.
153 struct cds_list_head condition_list
;
154 struct cds_lfht_node client_socket_ht_node
;
158 * During the reception of a message, the reception
159 * buffers' "size" is set to contain the current
160 * message's complete payload.
162 struct lttng_dynamic_buffer buffer
;
163 /* Bytes left to receive for the current message. */
164 size_t bytes_to_receive
;
165 /* Type of the message being received. */
166 enum lttng_notification_channel_message_type msg_type
;
168 * Indicates whether or not credentials are expected
173 * Indicates whether or not credentials were received
177 /* Only used during credentials reception. */
178 lttng_sock_cred creds
;
182 * Indicates whether or not a notification addressed to
183 * this client was dropped because a command reply was
186 * A notification is dropped whenever the buffer is not
189 bool dropped_notification
;
191 * Indicates whether or not a command reply is already
192 * buffered. In this case, it means that the client is
193 * not consuming command replies before emitting a new
194 * one. This could be caused by a protocol error or a
195 * misbehaving/malicious client.
197 bool queued_command_reply
;
198 struct lttng_dynamic_buffer buffer
;
203 struct channel_state_sample
{
204 struct channel_key key
;
205 struct cds_lfht_node channel_state_ht_node
;
206 uint64_t highest_usage
;
207 uint64_t lowest_usage
;
208 uint64_t channel_total_consumed
;
211 static unsigned long hash_channel_key(struct channel_key
*key
);
212 static int evaluate_condition(const struct lttng_condition
*condition
,
213 struct lttng_evaluation
**evaluation
,
214 const struct notification_thread_state
*state
,
215 const struct channel_state_sample
*previous_sample
,
216 const struct channel_state_sample
*latest_sample
,
217 uint64_t previous_session_consumed_total
,
218 uint64_t latest_session_consumed_total
,
219 struct channel_info
*channel_info
);
221 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
222 const struct lttng_evaluation
*evaluation
,
223 struct notification_client_list
*client_list
,
224 struct notification_thread_state
*state
,
225 uid_t channel_uid
, gid_t channel_gid
);
228 /* session_info API */
230 void session_info_destroy(void *_data
);
232 void session_info_get(struct session_info
*session_info
);
234 void session_info_put(struct session_info
*session_info
);
236 struct session_info
*session_info_create(const char *name
,
237 uid_t uid
, gid_t gid
,
238 struct lttng_session_trigger_list
*trigger_list
);
240 void session_info_add_channel(struct session_info
*session_info
,
241 struct channel_info
*channel_info
);
243 void session_info_remove_channel(struct session_info
*session_info
,
244 struct channel_info
*channel_info
);
246 /* lttng_session_trigger_list API */
248 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
249 const char *session_name
,
250 struct cds_lfht
*session_triggers_ht
);
252 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
253 const struct notification_thread_state
*state
,
254 const char *session_name
);
256 void lttng_session_trigger_list_destroy(
257 struct lttng_session_trigger_list
*list
);
259 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
260 const struct lttng_trigger
*trigger
);
264 int match_client(struct cds_lfht_node
*node
, const void *key
)
266 /* This double-cast is intended to supress pointer-to-cast warning. */
267 int socket
= (int) (intptr_t) key
;
268 struct notification_client
*client
;
270 client
= caa_container_of(node
, struct notification_client
,
271 client_socket_ht_node
);
273 return !!(client
->socket
== socket
);
277 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
279 struct channel_key
*channel_key
= (struct channel_key
*) key
;
280 struct lttng_channel_trigger_list
*trigger_list
;
282 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
283 channel_triggers_ht_node
);
285 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
286 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
290 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
292 const char *session_name
= (const char *) key
;
293 struct lttng_session_trigger_list
*trigger_list
;
295 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
296 session_triggers_ht_node
);
298 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
302 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
304 struct channel_key
*channel_key
= (struct channel_key
*) key
;
305 struct channel_state_sample
*sample
;
307 sample
= caa_container_of(node
, struct channel_state_sample
,
308 channel_state_ht_node
);
310 return !!((channel_key
->key
== sample
->key
.key
) &&
311 (channel_key
->domain
== sample
->key
.domain
));
315 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
317 struct channel_key
*channel_key
= (struct channel_key
*) key
;
318 struct channel_info
*channel_info
;
320 channel_info
= caa_container_of(node
, struct channel_info
,
323 return !!((channel_key
->key
== channel_info
->key
.key
) &&
324 (channel_key
->domain
== channel_info
->key
.domain
));
328 int match_condition(struct cds_lfht_node
*node
, const void *key
)
330 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
331 struct lttng_trigger_ht_element
*trigger
;
332 struct lttng_condition
*condition
;
334 trigger
= caa_container_of(node
, struct lttng_trigger_ht_element
,
336 condition
= lttng_trigger_get_condition(trigger
->trigger
);
339 return !!lttng_condition_is_equal(condition_key
, condition
);
343 int match_client_list(struct cds_lfht_node
*node
, const void *key
)
345 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
346 struct notification_client_list
*client_list
;
347 struct lttng_condition
*condition
;
348 struct lttng_condition
*condition_key
= lttng_trigger_get_condition(
351 assert(condition_key
);
353 client_list
= caa_container_of(node
, struct notification_client_list
,
354 notification_trigger_ht_node
);
355 condition
= lttng_trigger_get_condition(client_list
->trigger
);
357 return !!lttng_condition_is_equal(condition_key
, condition
);
361 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
363 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
364 struct notification_client_list
*client_list
;
365 struct lttng_condition
*condition
;
367 assert(condition_key
);
369 client_list
= caa_container_of(node
, struct notification_client_list
,
370 notification_trigger_ht_node
);
371 condition
= lttng_trigger_get_condition(client_list
->trigger
);
373 return !!lttng_condition_is_equal(condition_key
, condition
);
377 unsigned long lttng_condition_buffer_usage_hash(
378 const struct lttng_condition
*_condition
)
381 unsigned long condition_type
;
382 struct lttng_condition_buffer_usage
*condition
;
384 condition
= container_of(_condition
,
385 struct lttng_condition_buffer_usage
, parent
);
387 condition_type
= (unsigned long) condition
->parent
.type
;
388 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
389 if (condition
->session_name
) {
390 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
392 if (condition
->channel_name
) {
393 hash
^= hash_key_str(condition
->channel_name
, lttng_ht_seed
);
395 if (condition
->domain
.set
) {
396 hash
^= hash_key_ulong(
397 (void *) condition
->domain
.type
,
400 if (condition
->threshold_ratio
.set
) {
403 val
= condition
->threshold_ratio
.value
* (double) UINT32_MAX
;
404 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
405 } else if (condition
->threshold_bytes
.set
) {
408 val
= condition
->threshold_bytes
.value
;
409 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
415 unsigned long lttng_condition_session_consumed_size_hash(
416 const struct lttng_condition
*_condition
)
419 unsigned long condition_type
=
420 (unsigned long) LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
;
421 struct lttng_condition_session_consumed_size
*condition
;
424 condition
= container_of(_condition
,
425 struct lttng_condition_session_consumed_size
, parent
);
427 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
428 if (condition
->session_name
) {
429 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
431 val
= condition
->consumed_threshold_bytes
.value
;
432 hash
^= hash_key_u64(&val
, lttng_ht_seed
);
437 unsigned long lttng_condition_session_rotation_hash(
438 const struct lttng_condition
*_condition
)
440 unsigned long hash
, condition_type
;
441 struct lttng_condition_session_rotation
*condition
;
443 condition
= container_of(_condition
,
444 struct lttng_condition_session_rotation
, parent
);
445 condition_type
= (unsigned long) condition
->parent
.type
;
446 hash
= hash_key_ulong((void *) condition_type
, lttng_ht_seed
);
447 assert(condition
->session_name
);
448 hash
^= hash_key_str(condition
->session_name
, lttng_ht_seed
);
453 * The lttng_condition hashing code is kept in this file (rather than
454 * condition.c) since it makes use of GPLv2 code (hashtable utils), which we
455 * don't want to link in liblttng-ctl.
458 unsigned long lttng_condition_hash(const struct lttng_condition
*condition
)
460 switch (condition
->type
) {
461 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
462 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
463 return lttng_condition_buffer_usage_hash(condition
);
464 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
465 return lttng_condition_session_consumed_size_hash(condition
);
466 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
467 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
468 return lttng_condition_session_rotation_hash(condition
);
470 ERR("[notification-thread] Unexpected condition type caught");
476 unsigned long hash_channel_key(struct channel_key
*key
)
478 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
479 unsigned long domain_hash
= hash_key_ulong(
480 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
482 return key_hash
^ domain_hash
;
486 void channel_info_destroy(struct channel_info
*channel_info
)
492 if (channel_info
->session_info
) {
493 session_info_remove_channel(channel_info
->session_info
,
495 session_info_put(channel_info
->session_info
);
497 if (channel_info
->name
) {
498 free(channel_info
->name
);
503 /* Don't call directly, use the ref-counting mechanism. */
505 void session_info_destroy(void *_data
)
507 struct session_info
*session_info
= _data
;
510 assert(session_info
);
511 if (session_info
->channel_infos_ht
) {
512 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
514 ERR("[notification-thread] Failed to destroy channel information hash table");
517 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
518 free(session_info
->name
);
523 void session_info_get(struct session_info
*session_info
)
528 lttng_ref_get(&session_info
->ref
);
532 void session_info_put(struct session_info
*session_info
)
537 lttng_ref_put(&session_info
->ref
);
541 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
542 struct lttng_session_trigger_list
*trigger_list
)
544 struct session_info
*session_info
;
548 session_info
= zmalloc(sizeof(*session_info
));
552 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
554 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
555 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
556 if (!session_info
->channel_infos_ht
) {
560 cds_lfht_node_init(&session_info
->sessions_ht_node
);
561 session_info
->name
= strdup(name
);
562 if (!session_info
->name
) {
565 session_info
->uid
= uid
;
566 session_info
->gid
= gid
;
567 session_info
->trigger_list
= trigger_list
;
571 session_info_put(session_info
);
576 void session_info_add_channel(struct session_info
*session_info
,
577 struct channel_info
*channel_info
)
580 cds_lfht_add(session_info
->channel_infos_ht
,
581 hash_channel_key(&channel_info
->key
),
582 &channel_info
->session_info_channels_ht_node
);
587 void session_info_remove_channel(struct session_info
*session_info
,
588 struct channel_info
*channel_info
)
591 cds_lfht_del(session_info
->channel_infos_ht
,
592 &channel_info
->session_info_channels_ht_node
);
597 struct channel_info
*channel_info_create(const char *channel_name
,
598 struct channel_key
*channel_key
, uint64_t channel_capacity
,
599 struct session_info
*session_info
)
601 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
607 cds_lfht_node_init(&channel_info
->channels_ht_node
);
608 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
609 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
610 channel_info
->capacity
= channel_capacity
;
612 channel_info
->name
= strdup(channel_name
);
613 if (!channel_info
->name
) {
618 * Set the references between session and channel infos:
619 * - channel_info holds a strong reference to session_info
620 * - session_info holds a weak reference to channel_info
622 session_info_get(session_info
);
623 session_info_add_channel(session_info
, channel_info
);
624 channel_info
->session_info
= session_info
;
628 channel_info_destroy(channel_info
);
632 /* This function must be called with the RCU read lock held. */
634 int evaluate_condition_for_client(struct lttng_trigger
*trigger
,
635 struct lttng_condition
*condition
,
636 struct notification_client
*client
,
637 struct notification_thread_state
*state
)
640 struct cds_lfht_iter iter
;
641 struct cds_lfht_node
*node
;
642 struct channel_info
*channel_info
= NULL
;
643 struct channel_key
*channel_key
= NULL
;
644 struct channel_state_sample
*last_sample
= NULL
;
645 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
646 struct lttng_evaluation
*evaluation
= NULL
;
647 struct notification_client_list client_list
= { 0 };
648 struct notification_client_list_element client_list_element
= { 0 };
655 /* Find the channel associated with the trigger. */
656 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
657 channel_trigger_list
, channel_triggers_ht_node
) {
658 struct lttng_trigger_list_element
*element
;
660 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
661 const struct lttng_condition
*current_condition
=
662 lttng_trigger_get_const_condition(
665 assert(current_condition
);
666 if (!lttng_condition_is_equal(condition
,
667 current_condition
)) {
671 /* Found the trigger, save the channel key. */
672 channel_key
= &channel_trigger_list
->channel_key
;
676 /* The channel key was found stop iteration. */
682 /* No channel found; normal exit. */
683 DBG("[notification-thread] No channel associated with newly subscribed-to condition");
688 /* Fetch channel info for the matching channel. */
689 cds_lfht_lookup(state
->channels_ht
,
690 hash_channel_key(channel_key
),
694 node
= cds_lfht_iter_get_node(&iter
);
696 channel_info
= caa_container_of(node
, struct channel_info
,
699 /* Retrieve the channel's last sample, if it exists. */
700 cds_lfht_lookup(state
->channel_state_ht
,
701 hash_channel_key(channel_key
),
702 match_channel_state_sample
,
705 node
= cds_lfht_iter_get_node(&iter
);
707 last_sample
= caa_container_of(node
,
708 struct channel_state_sample
,
709 channel_state_ht_node
);
711 /* Nothing to evaluate, no sample was ever taken. Normal exit */
712 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
717 ret
= evaluate_condition(condition
, &evaluation
, state
,
719 0, channel_info
->session_info
->consumed_data_size
,
722 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
727 /* Evaluation yielded nothing. Normal exit. */
728 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
734 * Create a temporary client list with the client currently
737 cds_lfht_node_init(&client_list
.notification_trigger_ht_node
);
738 CDS_INIT_LIST_HEAD(&client_list
.list
);
739 client_list
.trigger
= trigger
;
741 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
742 client_list_element
.client
= client
;
743 cds_list_add(&client_list_element
.node
, &client_list
.list
);
745 /* Send evaluation result to the newly-subscribed client. */
746 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
747 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
748 state
, channel_info
->session_info
->uid
,
749 channel_info
->session_info
->gid
);
756 int notification_thread_client_subscribe(struct notification_client
*client
,
757 struct lttng_condition
*condition
,
758 struct notification_thread_state
*state
,
759 enum lttng_notification_channel_status
*_status
)
762 struct cds_lfht_iter iter
;
763 struct cds_lfht_node
*node
;
764 struct notification_client_list
*client_list
;
765 struct lttng_condition_list_element
*condition_list_element
= NULL
;
766 struct notification_client_list_element
*client_list_element
= NULL
;
767 enum lttng_notification_channel_status status
=
768 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
771 * Ensure that the client has not already subscribed to this condition
774 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
775 if (lttng_condition_is_equal(condition_list_element
->condition
,
777 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
782 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
783 if (!condition_list_element
) {
787 client_list_element
= zmalloc(sizeof(*client_list_element
));
788 if (!client_list_element
) {
796 * Add the newly-subscribed condition to the client's subscription list.
798 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
799 condition_list_element
->condition
= condition
;
800 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
802 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
803 lttng_condition_hash(condition
),
804 match_client_list_condition
,
807 node
= cds_lfht_iter_get_node(&iter
);
810 * No notification-emiting trigger registered with this
811 * condition. We don't evaluate the condition right away
812 * since this trigger is not registered yet.
814 free(client_list_element
);
818 client_list
= caa_container_of(node
, struct notification_client_list
,
819 notification_trigger_ht_node
);
821 * The condition to which the client just subscribed is evaluated
822 * at this point so that conditions that are already TRUE result
823 * in a notification being sent out.
825 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
827 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
829 free(client_list_element
);
834 * Add the client to the list of clients interested in a given trigger
835 * if a "notification" trigger with a corresponding condition was
838 client_list_element
->client
= client
;
839 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
840 cds_list_add(&client_list_element
->node
, &client_list
->list
);
849 free(condition_list_element
);
850 free(client_list_element
);
855 int notification_thread_client_unsubscribe(
856 struct notification_client
*client
,
857 struct lttng_condition
*condition
,
858 struct notification_thread_state
*state
,
859 enum lttng_notification_channel_status
*_status
)
861 struct cds_lfht_iter iter
;
862 struct cds_lfht_node
*node
;
863 struct notification_client_list
*client_list
;
864 struct lttng_condition_list_element
*condition_list_element
,
866 struct notification_client_list_element
*client_list_element
,
868 bool condition_found
= false;
869 enum lttng_notification_channel_status status
=
870 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
872 /* Remove the condition from the client's condition list. */
873 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
874 &client
->condition_list
, node
) {
875 if (!lttng_condition_is_equal(condition_list_element
->condition
,
880 cds_list_del(&condition_list_element
->node
);
882 * The caller may be iterating on the client's conditions to
883 * tear down a client's connection. In this case, the condition
884 * will be destroyed at the end.
886 if (condition
!= condition_list_element
->condition
) {
887 lttng_condition_destroy(
888 condition_list_element
->condition
);
890 free(condition_list_element
);
891 condition_found
= true;
895 if (!condition_found
) {
896 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
901 * Remove the client from the list of clients interested the trigger
902 * matching the condition.
905 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
906 lttng_condition_hash(condition
),
907 match_client_list_condition
,
910 node
= cds_lfht_iter_get_node(&iter
);
915 client_list
= caa_container_of(node
, struct notification_client_list
,
916 notification_trigger_ht_node
);
917 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
918 &client_list
->list
, node
) {
919 if (client_list_element
->client
->socket
!= client
->socket
) {
922 cds_list_del(&client_list_element
->node
);
923 free(client_list_element
);
929 lttng_condition_destroy(condition
);
937 void notification_client_destroy(struct notification_client
*client
,
938 struct notification_thread_state
*state
)
940 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
946 /* Release all conditions to which the client was subscribed. */
947 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
948 &client
->condition_list
, node
) {
949 (void) notification_thread_client_unsubscribe(client
,
950 condition_list_element
->condition
, state
, NULL
);
953 if (client
->socket
>= 0) {
954 (void) lttcomm_close_unix_sock(client
->socket
);
956 lttng_dynamic_buffer_reset(&client
->communication
.inbound
.buffer
);
957 lttng_dynamic_buffer_reset(&client
->communication
.outbound
.buffer
);
962 * Call with rcu_read_lock held (and hold for the lifetime of the returned
966 struct notification_client
*get_client_from_socket(int socket
,
967 struct notification_thread_state
*state
)
969 struct cds_lfht_iter iter
;
970 struct cds_lfht_node
*node
;
971 struct notification_client
*client
= NULL
;
973 cds_lfht_lookup(state
->client_socket_ht
,
974 hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
),
976 (void *) (unsigned long) socket
,
978 node
= cds_lfht_iter_get_node(&iter
);
983 client
= caa_container_of(node
, struct notification_client
,
984 client_socket_ht_node
);
990 bool buffer_usage_condition_applies_to_channel(
991 const struct lttng_condition
*condition
,
992 const struct channel_info
*channel_info
)
994 enum lttng_condition_status status
;
995 enum lttng_domain_type condition_domain
;
996 const char *condition_session_name
= NULL
;
997 const char *condition_channel_name
= NULL
;
999 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1001 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1002 if (channel_info
->key
.domain
!= condition_domain
) {
1006 status
= lttng_condition_buffer_usage_get_session_name(
1007 condition
, &condition_session_name
);
1008 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1010 status
= lttng_condition_buffer_usage_get_channel_name(
1011 condition
, &condition_channel_name
);
1012 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1014 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1017 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1027 bool session_consumed_size_condition_applies_to_channel(
1028 const struct lttng_condition
*condition
,
1029 const struct channel_info
*channel_info
)
1031 enum lttng_condition_status status
;
1032 const char *condition_session_name
= NULL
;
1034 status
= lttng_condition_session_consumed_size_get_session_name(
1035 condition
, &condition_session_name
);
1036 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1038 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1048 * Get the type of object to which a given trigger applies. Binding lets
1049 * the notification system evaluate a trigger's condition when a given
1050 * object's state is updated.
1052 * For instance, a condition bound to a channel will be evaluated everytime
1053 * the channel's state is changed by a channel monitoring sample.
1056 enum lttng_object_type
get_trigger_binding_object(
1057 const struct lttng_trigger
*trigger
)
1059 const struct lttng_condition
*condition
;
1061 condition
= lttng_trigger_get_const_condition(trigger
);
1062 switch (lttng_condition_get_type(condition
)) {
1063 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1064 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1065 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1066 return LTTNG_OBJECT_TYPE_CHANNEL
;
1067 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1068 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1069 return LTTNG_OBJECT_TYPE_SESSION
;
1071 return LTTNG_OBJECT_TYPE_UNKNOWN
;
1076 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1077 const struct channel_info
*channel_info
)
1079 const struct lttng_condition
*condition
;
1080 bool trigger_applies
;
1082 condition
= lttng_trigger_get_const_condition(trigger
);
1087 switch (lttng_condition_get_type(condition
)) {
1088 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1089 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1090 trigger_applies
= buffer_usage_condition_applies_to_channel(
1091 condition
, channel_info
);
1093 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1094 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1095 condition
, channel_info
);
1101 return trigger_applies
;
1107 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1108 struct notification_client
*client
)
1110 bool applies
= false;
1111 struct lttng_condition_list_element
*condition_list_element
;
1113 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1115 applies
= lttng_condition_is_equal(
1116 condition_list_element
->condition
,
1117 lttng_trigger_get_condition(trigger
));
1126 int match_session(struct cds_lfht_node
*node
, const void *key
)
1128 const char *name
= key
;
1129 struct session_info
*session_info
= caa_container_of(
1130 node
, struct session_info
, sessions_ht_node
);
1132 return !strcmp(session_info
->name
, name
);
1136 * Allocate an empty lttng_session_trigger_list for the session named
1139 * No ownership of 'session_name' is assumed by the session trigger list.
1140 * It is the caller's responsability to ensure the session name is alive
1141 * for as long as this list is.
1144 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1145 const char *session_name
,
1146 struct cds_lfht
*session_triggers_ht
)
1148 struct lttng_session_trigger_list
*list
;
1150 list
= zmalloc(sizeof(*list
));
1154 list
->session_name
= session_name
;
1155 CDS_INIT_LIST_HEAD(&list
->list
);
1156 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1157 list
->session_triggers_ht
= session_triggers_ht
;
1160 /* Publish the list through the session_triggers_ht. */
1161 cds_lfht_add(session_triggers_ht
,
1162 hash_key_str(session_name
, lttng_ht_seed
),
1163 &list
->session_triggers_ht_node
);
1170 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1172 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1177 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1179 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1181 /* Empty the list element by element, and then free the list itself. */
1182 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1183 &list
->list
, node
) {
1184 cds_list_del(&trigger_list_element
->node
);
1185 free(trigger_list_element
);
1188 /* Unpublish the list from the session_triggers_ht. */
1189 cds_lfht_del(list
->session_triggers_ht
,
1190 &list
->session_triggers_ht_node
);
1192 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1196 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1197 const struct lttng_trigger
*trigger
)
1200 struct lttng_trigger_list_element
*new_element
=
1201 zmalloc(sizeof(*new_element
));
1207 CDS_INIT_LIST_HEAD(&new_element
->node
);
1208 new_element
->trigger
= trigger
;
1209 cds_list_add(&new_element
->node
, &list
->list
);
1215 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1216 const char *session_name
)
1218 bool applies
= false;
1219 const struct lttng_condition
*condition
;
1221 condition
= lttng_trigger_get_const_condition(trigger
);
1222 switch (lttng_condition_get_type(condition
)) {
1223 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1224 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1226 enum lttng_condition_status condition_status
;
1227 const char *condition_session_name
;
1229 condition_status
= lttng_condition_session_rotation_get_session_name(
1230 condition
, &condition_session_name
);
1231 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1232 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1236 assert(condition_session_name
);
1237 applies
= !strcmp(condition_session_name
, session_name
);
1248 * Allocate and initialize an lttng_session_trigger_list which contains
1249 * all triggers that apply to the session named 'session_name'.
1251 * No ownership of 'session_name' is assumed by the session trigger list.
1252 * It is the caller's responsability to ensure the session name is alive
1253 * for as long as this list is.
1256 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1257 const struct notification_thread_state
*state
,
1258 const char *session_name
)
1260 int trigger_count
= 0;
1261 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1262 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1263 struct cds_lfht_iter iter
;
1265 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1266 state
->session_triggers_ht
);
1268 /* Add all triggers applying to the session named 'session_name'. */
1269 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1273 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1278 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1279 trigger_ht_element
->trigger
);
1287 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1289 return session_trigger_list
;
1291 lttng_session_trigger_list_destroy(session_trigger_list
);
1296 struct session_info
*find_or_create_session_info(
1297 struct notification_thread_state
*state
,
1298 const char *name
, uid_t uid
, gid_t gid
)
1300 struct session_info
*session
= NULL
;
1301 struct cds_lfht_node
*node
;
1302 struct cds_lfht_iter iter
;
1303 struct lttng_session_trigger_list
*trigger_list
;
1306 cds_lfht_lookup(state
->sessions_ht
,
1307 hash_key_str(name
, lttng_ht_seed
),
1311 node
= cds_lfht_iter_get_node(&iter
);
1313 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1315 session
= caa_container_of(node
, struct session_info
,
1317 assert(session
->uid
== uid
);
1318 assert(session
->gid
== gid
);
1322 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1323 if (!trigger_list
) {
1327 session
= session_info_create(name
, uid
, gid
, trigger_list
);
1329 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1333 trigger_list
= NULL
;
1335 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1336 &session
->sessions_ht_node
);
1341 session_info_put(session
);
1346 int handle_notification_thread_command_add_channel(
1347 struct notification_thread_state
*state
,
1348 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1349 const char *channel_name
, enum lttng_domain_type channel_domain
,
1350 uint64_t channel_key_int
, uint64_t channel_capacity
,
1351 enum lttng_error_code
*cmd_result
)
1353 struct cds_list_head trigger_list
;
1354 struct channel_info
*new_channel_info
= NULL
;
1355 struct channel_key channel_key
= {
1356 .key
= channel_key_int
,
1357 .domain
= channel_domain
,
1359 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1360 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1361 int trigger_count
= 0;
1362 struct cds_lfht_iter iter
;
1363 struct session_info
*session_info
= NULL
;
1365 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1366 channel_name
, session_name
, channel_key_int
,
1367 channel_domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1369 CDS_INIT_LIST_HEAD(&trigger_list
);
1371 session_info
= find_or_create_session_info(state
, session_name
,
1372 session_uid
, session_gid
);
1373 if (!session_info
) {
1374 /* Allocation error or an internal error occured. */
1378 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1379 channel_capacity
, session_info
);
1380 if (!new_channel_info
) {
1384 /* Build a list of all triggers applying to the new channel. */
1385 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1387 struct lttng_trigger_list_element
*new_element
;
1389 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1390 new_channel_info
)) {
1394 new_element
= zmalloc(sizeof(*new_element
));
1398 CDS_INIT_LIST_HEAD(&new_element
->node
);
1399 new_element
->trigger
= trigger_ht_element
->trigger
;
1400 cds_list_add(&new_element
->node
, &trigger_list
);
1404 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1406 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1407 if (!channel_trigger_list
) {
1410 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1411 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1412 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1413 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1416 /* Add channel to the channel_ht which owns the channel_infos. */
1417 cds_lfht_add(state
->channels_ht
,
1418 hash_channel_key(&new_channel_info
->key
),
1419 &new_channel_info
->channels_ht_node
);
1421 * Add the list of triggers associated with this channel to the
1422 * channel_triggers_ht.
1424 cds_lfht_add(state
->channel_triggers_ht
,
1425 hash_channel_key(&new_channel_info
->key
),
1426 &channel_trigger_list
->channel_triggers_ht_node
);
1428 *cmd_result
= LTTNG_OK
;
1431 channel_info_destroy(new_channel_info
);
1432 session_info_put(session_info
);
1437 int handle_notification_thread_command_remove_channel(
1438 struct notification_thread_state
*state
,
1439 uint64_t channel_key
, enum lttng_domain_type domain
,
1440 enum lttng_error_code
*cmd_result
)
1442 struct cds_lfht_node
*node
;
1443 struct cds_lfht_iter iter
;
1444 struct lttng_channel_trigger_list
*trigger_list
;
1445 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1446 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1447 struct channel_info
*channel_info
;
1449 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1450 channel_key
, domain
== LTTNG_DOMAIN_KERNEL
? "kernel" : "user space");
1454 cds_lfht_lookup(state
->channel_triggers_ht
,
1455 hash_channel_key(&key
),
1456 match_channel_trigger_list
,
1459 node
= cds_lfht_iter_get_node(&iter
);
1461 * There is a severe internal error if we are being asked to remove a
1462 * channel that doesn't exist.
1465 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1469 /* Free the list of triggers associated with this channel. */
1470 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1471 channel_triggers_ht_node
);
1472 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1473 &trigger_list
->list
, node
) {
1474 cds_list_del(&trigger_list_element
->node
);
1475 free(trigger_list_element
);
1477 cds_lfht_del(state
->channel_triggers_ht
, node
);
1480 /* Free sampled channel state. */
1481 cds_lfht_lookup(state
->channel_state_ht
,
1482 hash_channel_key(&key
),
1483 match_channel_state_sample
,
1486 node
= cds_lfht_iter_get_node(&iter
);
1488 * This is expected to be NULL if the channel is destroyed before we
1489 * received a sample.
1492 struct channel_state_sample
*sample
= caa_container_of(node
,
1493 struct channel_state_sample
,
1494 channel_state_ht_node
);
1496 cds_lfht_del(state
->channel_state_ht
, node
);
1500 /* Remove the channel from the channels_ht and free it. */
1501 cds_lfht_lookup(state
->channels_ht
,
1502 hash_channel_key(&key
),
1506 node
= cds_lfht_iter_get_node(&iter
);
1508 channel_info
= caa_container_of(node
, struct channel_info
,
1510 cds_lfht_del(state
->channels_ht
, node
);
1511 channel_info_destroy(channel_info
);
1514 *cmd_result
= LTTNG_OK
;
1519 int condition_is_supported(struct lttng_condition
*condition
)
1523 switch (lttng_condition_get_type(condition
)) {
1524 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1525 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1527 enum lttng_domain_type domain
;
1529 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
1536 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
1542 * Older kernel tracers don't expose the API to monitor their
1543 * buffers. Therefore, we reject triggers that require that
1544 * mechanism to be available to be evaluated.
1546 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1557 /* Must be called with RCU read lock held. */
1559 int bind_trigger_to_matching_session(const struct lttng_trigger
*trigger
,
1560 struct notification_thread_state
*state
)
1563 struct cds_lfht_node
*node
;
1564 struct cds_lfht_iter iter
;
1565 const struct lttng_condition
*condition
;
1566 const char *session_name
;
1567 struct lttng_session_trigger_list
*trigger_list
;
1569 condition
= lttng_trigger_get_const_condition(trigger
);
1570 switch (lttng_condition_get_type(condition
)) {
1571 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1572 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1574 enum lttng_condition_status status
;
1576 status
= lttng_condition_session_rotation_get_session_name(
1577 condition
, &session_name
);
1578 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
1579 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
1590 cds_lfht_lookup(state
->session_triggers_ht
,
1591 hash_key_str(session_name
, lttng_ht_seed
),
1592 match_session_trigger_list
,
1595 node
= cds_lfht_iter_get_node(&iter
);
1598 * Not an error, the list of triggers applying to that session
1599 * will be initialized when the session is created.
1601 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
1606 trigger_list
= caa_container_of(node
,
1607 struct lttng_session_trigger_list
,
1608 session_triggers_ht_node
);
1610 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
1612 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
1617 /* Must be called with RCU read lock held. */
1619 int bind_trigger_to_matching_channels(const struct lttng_trigger
*trigger
,
1620 struct notification_thread_state
*state
)
1623 struct cds_lfht_node
*node
;
1624 struct cds_lfht_iter iter
;
1625 struct channel_info
*channel
;
1627 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
1629 struct lttng_trigger_list_element
*trigger_list_element
;
1630 struct lttng_channel_trigger_list
*trigger_list
;
1632 if (!trigger_applies_to_channel(trigger
, channel
)) {
1636 cds_lfht_lookup(state
->channel_triggers_ht
,
1637 hash_channel_key(&channel
->key
),
1638 match_channel_trigger_list
,
1641 node
= cds_lfht_iter_get_node(&iter
);
1643 trigger_list
= caa_container_of(node
,
1644 struct lttng_channel_trigger_list
,
1645 channel_triggers_ht_node
);
1647 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
1648 if (!trigger_list_element
) {
1652 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
1653 trigger_list_element
->trigger
= trigger
;
1654 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
1655 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
1663 * FIXME A client's credentials are not checked when registering a trigger, nor
1664 * are they stored alongside with the trigger.
1666 * The effects of this are benign since:
1667 * - The client will succeed in registering the trigger, as it is valid,
1668 * - The trigger will, internally, be bound to the channel/session,
1669 * - The notifications will not be sent since the client's credentials
1670 * are checked against the channel at that moment.
1672 * If this function returns a non-zero value, it means something is
1673 * fundamentally broken and the whole subsystem/thread will be torn down.
1675 * If a non-fatal error occurs, just set the cmd_result to the appropriate
1679 int handle_notification_thread_command_register_trigger(
1680 struct notification_thread_state
*state
,
1681 struct lttng_trigger
*trigger
,
1682 enum lttng_error_code
*cmd_result
)
1685 struct lttng_condition
*condition
;
1686 struct notification_client
*client
;
1687 struct notification_client_list
*client_list
= NULL
;
1688 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1689 struct notification_client_list_element
*client_list_element
, *tmp
;
1690 struct cds_lfht_node
*node
;
1691 struct cds_lfht_iter iter
;
1692 bool free_trigger
= true;
1696 condition
= lttng_trigger_get_condition(trigger
);
1699 ret
= condition_is_supported(condition
);
1702 } else if (ret
== 0) {
1703 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
1706 /* Feature is supported, continue. */
1710 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
1711 if (!trigger_ht_element
) {
1716 /* Add trigger to the trigger_ht. */
1717 cds_lfht_node_init(&trigger_ht_element
->node
);
1718 trigger_ht_element
->trigger
= trigger
;
1720 node
= cds_lfht_add_unique(state
->triggers_ht
,
1721 lttng_condition_hash(condition
),
1724 &trigger_ht_element
->node
);
1725 if (node
!= &trigger_ht_element
->node
) {
1726 /* Not a fatal error, simply report it to the client. */
1727 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
1728 goto error_free_ht_element
;
1732 * Ownership of the trigger and of its wrapper was transfered to
1735 trigger_ht_element
= NULL
;
1736 free_trigger
= false;
1739 * The rest only applies to triggers that have a "notify" action.
1740 * It is not skipped as this is the only action type currently
1743 client_list
= zmalloc(sizeof(*client_list
));
1746 goto error_free_ht_element
;
1748 cds_lfht_node_init(&client_list
->notification_trigger_ht_node
);
1749 CDS_INIT_LIST_HEAD(&client_list
->list
);
1750 client_list
->trigger
= trigger
;
1752 /* Build a list of clients to which this new trigger applies. */
1753 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
1754 client_socket_ht_node
) {
1755 if (!trigger_applies_to_client(trigger
, client
)) {
1759 client_list_element
= zmalloc(sizeof(*client_list_element
));
1760 if (!client_list_element
) {
1762 goto error_free_client_list
;
1764 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1765 client_list_element
->client
= client
;
1766 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1769 cds_lfht_add(state
->notification_trigger_clients_ht
,
1770 lttng_condition_hash(condition
),
1771 &client_list
->notification_trigger_ht_node
);
1773 switch (get_trigger_binding_object(trigger
)) {
1774 case LTTNG_OBJECT_TYPE_SESSION
:
1775 /* Add the trigger to the list if it matches a known session. */
1776 ret
= bind_trigger_to_matching_session(trigger
, state
);
1778 goto error_free_client_list
;
1780 case LTTNG_OBJECT_TYPE_CHANNEL
:
1782 * Add the trigger to list of triggers bound to the channels
1785 ret
= bind_trigger_to_matching_channels(trigger
, state
);
1787 goto error_free_client_list
;
1790 case LTTNG_OBJECT_TYPE_NONE
:
1793 ERR("[notification-thread] Unknown object type on which to bind a newly registered trigger was encountered");
1795 goto error_free_client_list
;
1799 * Since there is nothing preventing clients from subscribing to a
1800 * condition before the corresponding trigger is registered, we have
1801 * to evaluate this new condition right away.
1803 * At some point, we were waiting for the next "evaluation" (e.g. on
1804 * reception of a channel sample) to evaluate this new condition, but
1807 * The reason it was broken is that waiting for the next sample
1808 * does not allow us to properly handle transitions for edge-triggered
1811 * Consider this example: when we handle a new channel sample, we
1812 * evaluate each conditions twice: once with the previous state, and
1813 * again with the newest state. We then use those two results to
1814 * determine whether a state change happened: a condition was false and
1815 * became true. If a state change happened, we have to notify clients.
1817 * Now, if a client subscribes to a given notification and registers
1818 * a trigger *after* that subscription, we have to make sure the
1819 * condition is evaluated at this point while considering only the
1820 * current state. Otherwise, the next evaluation cycle may only see
1821 * that the evaluations remain the same (true for samples n-1 and n) and
1822 * the client will never know that the condition has been met.
1824 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1825 &client_list
->list
, node
) {
1826 ret
= evaluate_condition_for_client(trigger
, condition
,
1827 client_list_element
->client
, state
);
1829 goto error_free_client_list
;
1834 * Client list ownership transferred to the
1835 * notification_trigger_clients_ht.
1839 *cmd_result
= LTTNG_OK
;
1840 error_free_client_list
:
1842 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1843 &client_list
->list
, node
) {
1844 free(client_list_element
);
1848 error_free_ht_element
:
1849 free(trigger_ht_element
);
1852 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
1854 lttng_condition_destroy(condition
);
1855 lttng_action_destroy(action
);
1856 lttng_trigger_destroy(trigger
);
1863 int handle_notification_thread_command_unregister_trigger(
1864 struct notification_thread_state
*state
,
1865 struct lttng_trigger
*trigger
,
1866 enum lttng_error_code
*_cmd_reply
)
1868 struct cds_lfht_iter iter
;
1869 struct cds_lfht_node
*node
, *triggers_ht_node
;
1870 struct lttng_channel_trigger_list
*trigger_list
;
1871 struct notification_client_list
*client_list
;
1872 struct notification_client_list_element
*client_list_element
, *tmp
;
1873 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1874 struct lttng_condition
*condition
= lttng_trigger_get_condition(
1876 struct lttng_action
*action
;
1877 enum lttng_error_code cmd_reply
;
1881 cds_lfht_lookup(state
->triggers_ht
,
1882 lttng_condition_hash(condition
),
1886 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
1887 if (!triggers_ht_node
) {
1888 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
1891 cmd_reply
= LTTNG_OK
;
1894 /* Remove trigger from channel_triggers_ht. */
1895 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
1896 channel_triggers_ht_node
) {
1897 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
1899 cds_list_for_each_entry_safe(trigger_element
, tmp
,
1900 &trigger_list
->list
, node
) {
1901 const struct lttng_condition
*current_condition
=
1902 lttng_trigger_get_const_condition(
1903 trigger_element
->trigger
);
1905 assert(current_condition
);
1906 if (!lttng_condition_is_equal(condition
,
1907 current_condition
)) {
1911 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
1912 cds_list_del(&trigger_element
->node
);
1913 /* A trigger can only appear once per channel */
1919 * Remove and release the client list from
1920 * notification_trigger_clients_ht.
1922 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
1923 lttng_condition_hash(condition
),
1927 node
= cds_lfht_iter_get_node(&iter
);
1929 client_list
= caa_container_of(node
, struct notification_client_list
,
1930 notification_trigger_ht_node
);
1931 cds_list_for_each_entry_safe(client_list_element
, tmp
,
1932 &client_list
->list
, node
) {
1933 free(client_list_element
);
1935 cds_lfht_del(state
->notification_trigger_clients_ht
, node
);
1938 /* Remove trigger from triggers_ht. */
1939 trigger_ht_element
= caa_container_of(triggers_ht_node
,
1940 struct lttng_trigger_ht_element
, node
);
1941 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
1943 condition
= lttng_trigger_get_condition(trigger_ht_element
->trigger
);
1944 lttng_condition_destroy(condition
);
1945 action
= lttng_trigger_get_action(trigger_ht_element
->trigger
);
1946 lttng_action_destroy(action
);
1947 lttng_trigger_destroy(trigger_ht_element
->trigger
);
1948 free(trigger_ht_element
);
1952 *_cmd_reply
= cmd_reply
;
1957 /* Returns 0 on success, 1 on exit requested, negative value on error. */
1958 int handle_notification_thread_command(
1959 struct notification_thread_handle
*handle
,
1960 struct notification_thread_state
*state
)
1964 struct notification_thread_command
*cmd
;
1966 /* Read the event pipe to put it back into a quiescent state. */
1967 ret
= read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
1973 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
1974 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
1975 struct notification_thread_command
, cmd_list_node
);
1976 switch (cmd
->type
) {
1977 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
1978 DBG("[notification-thread] Received register trigger command");
1979 ret
= handle_notification_thread_command_register_trigger(
1980 state
, cmd
->parameters
.trigger
,
1983 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
1984 DBG("[notification-thread] Received unregister trigger command");
1985 ret
= handle_notification_thread_command_unregister_trigger(
1986 state
, cmd
->parameters
.trigger
,
1989 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
1990 DBG("[notification-thread] Received add channel command");
1991 ret
= handle_notification_thread_command_add_channel(
1993 cmd
->parameters
.add_channel
.session
.name
,
1994 cmd
->parameters
.add_channel
.session
.uid
,
1995 cmd
->parameters
.add_channel
.session
.gid
,
1996 cmd
->parameters
.add_channel
.channel
.name
,
1997 cmd
->parameters
.add_channel
.channel
.domain
,
1998 cmd
->parameters
.add_channel
.channel
.key
,
1999 cmd
->parameters
.add_channel
.channel
.capacity
,
2002 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2003 DBG("[notification-thread] Received remove channel command");
2004 ret
= handle_notification_thread_command_remove_channel(
2005 state
, cmd
->parameters
.remove_channel
.key
,
2006 cmd
->parameters
.remove_channel
.domain
,
2009 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2010 DBG("[notification-thread] Received quit command");
2011 cmd
->reply_code
= LTTNG_OK
;
2015 ERR("[notification-thread] Unknown internal command received");
2023 cds_list_del(&cmd
->cmd_list_node
);
2024 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2025 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2028 /* Wake-up and return a fatal error to the calling thread. */
2029 lttng_waiter_wake_up(&cmd
->reply_waiter
);
2030 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2031 cmd
->reply_code
= LTTNG_ERR_FATAL
;
2033 /* Indicate a fatal error to the caller. */
2038 unsigned long hash_client_socket(int socket
)
2040 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
2044 int socket_set_non_blocking(int socket
)
2048 /* Set the pipe as non-blocking. */
2049 ret
= fcntl(socket
, F_GETFL
, 0);
2051 PERROR("fcntl get socket flags");
2056 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
2058 PERROR("fcntl set O_NONBLOCK socket flag");
2061 DBG("Client socket (fd = %i) set as non-blocking", socket
);
2067 int client_reset_inbound_state(struct notification_client
*client
)
2071 ret
= lttng_dynamic_buffer_set_size(
2072 &client
->communication
.inbound
.buffer
, 0);
2075 client
->communication
.inbound
.bytes_to_receive
=
2076 sizeof(struct lttng_notification_channel_message
);
2077 client
->communication
.inbound
.msg_type
=
2078 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
2079 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
2080 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
2081 ret
= lttng_dynamic_buffer_set_size(
2082 &client
->communication
.inbound
.buffer
,
2083 client
->communication
.inbound
.bytes_to_receive
);
2087 int handle_notification_thread_client_connect(
2088 struct notification_thread_state
*state
)
2091 struct notification_client
*client
;
2093 DBG("[notification-thread] Handling new notification channel client connection");
2095 client
= zmalloc(sizeof(*client
));
2101 CDS_INIT_LIST_HEAD(&client
->condition_list
);
2102 lttng_dynamic_buffer_init(&client
->communication
.inbound
.buffer
);
2103 lttng_dynamic_buffer_init(&client
->communication
.outbound
.buffer
);
2104 client
->communication
.inbound
.expect_creds
= true;
2105 ret
= client_reset_inbound_state(client
);
2107 ERR("[notification-thread] Failed to reset client communication's inbound state");
2112 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
2114 ERR("[notification-thread] Failed to accept new notification channel client connection");
2119 client
->socket
= ret
;
2121 ret
= socket_set_non_blocking(client
->socket
);
2123 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
2127 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
2129 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
2134 ret
= lttng_poll_add(&state
->events
, client
->socket
,
2135 LPOLLIN
| LPOLLERR
|
2136 LPOLLHUP
| LPOLLRDHUP
);
2138 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
2142 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
2146 cds_lfht_add(state
->client_socket_ht
,
2147 hash_client_socket(client
->socket
),
2148 &client
->client_socket_ht_node
);
2153 notification_client_destroy(client
, state
);
2157 int handle_notification_thread_client_disconnect(
2159 struct notification_thread_state
*state
)
2162 struct notification_client
*client
;
2165 DBG("[notification-thread] Closing client connection (socket fd = %i)",
2167 client
= get_client_from_socket(client_socket
, state
);
2169 /* Internal state corruption, fatal error. */
2170 ERR("[notification-thread] Unable to find client (socket fd = %i)",
2176 ret
= lttng_poll_del(&state
->events
, client_socket
);
2178 ERR("[notification-thread] Failed to remove client socket from poll set");
2180 cds_lfht_del(state
->client_socket_ht
,
2181 &client
->client_socket_ht_node
);
2182 notification_client_destroy(client
, state
);
2188 int handle_notification_thread_client_disconnect_all(
2189 struct notification_thread_state
*state
)
2191 struct cds_lfht_iter iter
;
2192 struct notification_client
*client
;
2193 bool error_encoutered
= false;
2196 DBG("[notification-thread] Closing all client connections");
2197 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
2198 client_socket_ht_node
) {
2201 ret
= handle_notification_thread_client_disconnect(
2202 client
->socket
, state
);
2204 error_encoutered
= true;
2208 return error_encoutered
? 1 : 0;
2211 int handle_notification_thread_trigger_unregister_all(
2212 struct notification_thread_state
*state
)
2214 bool error_occurred
= false;
2215 struct cds_lfht_iter iter
;
2216 struct lttng_trigger_ht_element
*trigger_ht_element
;
2218 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
2220 int ret
= handle_notification_thread_command_unregister_trigger(
2221 state
, trigger_ht_element
->trigger
, NULL
);
2223 error_occurred
= true;
2226 return error_occurred
? -1 : 0;
2230 int client_flush_outgoing_queue(struct notification_client
*client
,
2231 struct notification_thread_state
*state
)
2234 size_t to_send_count
;
2236 assert(client
->communication
.outbound
.buffer
.size
!= 0);
2237 to_send_count
= client
->communication
.outbound
.buffer
.size
;
2238 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
2241 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
2242 client
->communication
.outbound
.buffer
.data
,
2244 if ((ret
< 0 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) ||
2245 (ret
> 0 && ret
< to_send_count
)) {
2246 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
2248 to_send_count
-= max(ret
, 0);
2250 memcpy(client
->communication
.outbound
.buffer
.data
,
2251 client
->communication
.outbound
.buffer
.data
+
2252 client
->communication
.outbound
.buffer
.size
- to_send_count
,
2254 ret
= lttng_dynamic_buffer_set_size(
2255 &client
->communication
.outbound
.buffer
,
2262 * We want to be notified whenever there is buffer space
2263 * available to send the rest of the payload.
2265 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2266 CLIENT_POLL_MASK_IN_OUT
);
2270 } else if (ret
< 0) {
2271 /* Generic error, disconnect the client. */
2272 ERR("[notification-thread] Failed to send flush outgoing queue, disconnecting client (socket fd = %i)",
2274 ret
= handle_notification_thread_client_disconnect(
2275 client
->socket
, state
);
2280 /* No error and flushed the queue completely. */
2281 ret
= lttng_dynamic_buffer_set_size(
2282 &client
->communication
.outbound
.buffer
, 0);
2286 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
2287 CLIENT_POLL_MASK_IN
);
2292 client
->communication
.outbound
.queued_command_reply
= false;
2293 client
->communication
.outbound
.dropped_notification
= false;
2302 int client_send_command_reply(struct notification_client
*client
,
2303 struct notification_thread_state
*state
,
2304 enum lttng_notification_channel_status status
)
2307 struct lttng_notification_channel_command_reply reply
= {
2308 .status
= (int8_t) status
,
2310 struct lttng_notification_channel_message msg
= {
2311 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
2312 .size
= sizeof(reply
),
2314 char buffer
[sizeof(msg
) + sizeof(reply
)];
2316 if (client
->communication
.outbound
.queued_command_reply
) {
2317 /* Protocol error. */
2321 memcpy(buffer
, &msg
, sizeof(msg
));
2322 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
2323 DBG("[notification-thread] Send command reply (%i)", (int) status
);
2325 /* Enqueue buffer to outgoing queue and flush it. */
2326 ret
= lttng_dynamic_buffer_append(
2327 &client
->communication
.outbound
.buffer
,
2328 buffer
, sizeof(buffer
));
2333 ret
= client_flush_outgoing_queue(client
, state
);
2338 if (client
->communication
.outbound
.buffer
.size
!= 0) {
2339 /* Queue could not be emptied. */
2340 client
->communication
.outbound
.queued_command_reply
= true;
2349 int client_dispatch_message(struct notification_client
*client
,
2350 struct notification_thread_state
*state
)
2354 if (client
->communication
.inbound
.msg_type
!=
2355 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
2356 client
->communication
.inbound
.msg_type
!=
2357 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
2358 !client
->validated
) {
2359 WARN("[notification-thread] client attempted a command before handshake");
2364 switch (client
->communication
.inbound
.msg_type
) {
2365 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
2368 * Receiving message header. The function will be called again
2369 * once the rest of the message as been received and can be
2372 const struct lttng_notification_channel_message
*msg
;
2374 assert(sizeof(*msg
) ==
2375 client
->communication
.inbound
.buffer
.size
);
2376 msg
= (const struct lttng_notification_channel_message
*)
2377 client
->communication
.inbound
.buffer
.data
;
2379 if (msg
->size
== 0 || msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
2380 ERR("[notification-thread] Invalid notification channel message: length = %u", msg
->size
);
2385 switch (msg
->type
) {
2386 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2387 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2388 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2392 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
2396 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
2397 client
->communication
.inbound
.msg_type
=
2398 (enum lttng_notification_channel_message_type
) msg
->type
;
2399 ret
= lttng_dynamic_buffer_set_size(
2400 &client
->communication
.inbound
.buffer
, msg
->size
);
2406 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
2408 struct lttng_notification_channel_command_handshake
*handshake_client
;
2409 struct lttng_notification_channel_command_handshake handshake_reply
= {
2410 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
2411 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
2413 struct lttng_notification_channel_message msg_header
= {
2414 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
2415 .size
= sizeof(handshake_reply
),
2417 enum lttng_notification_channel_status status
=
2418 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2419 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
2421 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
2422 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
2423 sizeof(handshake_reply
));
2426 (struct lttng_notification_channel_command_handshake
*)
2427 client
->communication
.inbound
.buffer
.data
;
2428 client
->major
= handshake_client
->major
;
2429 client
->minor
= handshake_client
->minor
;
2430 if (!client
->communication
.inbound
.creds_received
) {
2431 ERR("[notification-thread] No credentials received from client");
2436 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
2437 &client
->communication
.inbound
.creds
);
2438 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
2439 &client
->communication
.inbound
.creds
);
2440 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
2441 client
->uid
, client
->gid
, (int) client
->major
,
2442 (int) client
->minor
);
2444 if (handshake_client
->major
!= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
2445 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
2448 ret
= lttng_dynamic_buffer_append(&client
->communication
.outbound
.buffer
,
2449 send_buffer
, sizeof(send_buffer
));
2451 ERR("[notification-thread] Failed to send protocol version to notification channel client");
2455 ret
= client_flush_outgoing_queue(client
, state
);
2460 ret
= client_send_command_reply(client
, state
, status
);
2462 ERR("[notification-thread] Failed to send reply to notification channel client");
2466 /* Set reception state to receive the next message header. */
2467 ret
= client_reset_inbound_state(client
);
2469 ERR("[notification-thread] Failed to reset client communication's inbound state");
2472 client
->validated
= true;
2475 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
2476 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
2478 struct lttng_condition
*condition
;
2479 enum lttng_notification_channel_status status
=
2480 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
2481 const struct lttng_buffer_view condition_view
=
2482 lttng_buffer_view_from_dynamic_buffer(
2483 &client
->communication
.inbound
.buffer
,
2485 size_t expected_condition_size
=
2486 client
->communication
.inbound
.buffer
.size
;
2488 ret
= lttng_condition_create_from_buffer(&condition_view
,
2490 if (ret
!= expected_condition_size
) {
2491 ERR("[notification-thread] Malformed condition received from client");
2495 if (client
->communication
.inbound
.msg_type
==
2496 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
2497 ret
= notification_thread_client_subscribe(client
,
2498 condition
, state
, &status
);
2500 ret
= notification_thread_client_unsubscribe(client
,
2501 condition
, state
, &status
);
2507 ret
= client_send_command_reply(client
, state
, status
);
2509 ERR("[notification-thread] Failed to send reply to notification channel client");
2513 /* Set reception state to receive the next message header. */
2514 ret
= client_reset_inbound_state(client
);
2516 ERR("[notification-thread] Failed to reset client communication's inbound state");
2528 /* Incoming data from client. */
2529 int handle_notification_thread_client_in(
2530 struct notification_thread_state
*state
, int socket
)
2533 struct notification_client
*client
;
2537 client
= get_client_from_socket(socket
, state
);
2539 /* Internal error, abort. */
2544 offset
= client
->communication
.inbound
.buffer
.size
-
2545 client
->communication
.inbound
.bytes_to_receive
;
2546 if (client
->communication
.inbound
.expect_creds
) {
2547 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
2548 client
->communication
.inbound
.buffer
.data
+ offset
,
2549 client
->communication
.inbound
.bytes_to_receive
,
2550 &client
->communication
.inbound
.creds
);
2552 client
->communication
.inbound
.expect_creds
= false;
2553 client
->communication
.inbound
.creds_received
= true;
2556 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
2557 client
->communication
.inbound
.buffer
.data
+ offset
,
2558 client
->communication
.inbound
.bytes_to_receive
);
2561 goto error_disconnect_client
;
2564 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
2565 if (client
->communication
.inbound
.bytes_to_receive
== 0) {
2566 ret
= client_dispatch_message(client
, state
);
2569 * Only returns an error if this client must be
2572 goto error_disconnect_client
;
2579 error_disconnect_client
:
2580 ret
= handle_notification_thread_client_disconnect(socket
, state
);
2584 /* Client ready to receive outgoing data. */
2585 int handle_notification_thread_client_out(
2586 struct notification_thread_state
*state
, int socket
)
2589 struct notification_client
*client
;
2591 client
= get_client_from_socket(socket
, state
);
2593 /* Internal error, abort. */
2598 ret
= client_flush_outgoing_queue(client
, state
);
2607 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
2608 const struct channel_state_sample
*sample
,
2609 uint64_t buffer_capacity
)
2611 bool result
= false;
2613 enum lttng_condition_type condition_type
;
2614 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
2615 condition
, struct lttng_condition_buffer_usage
,
2618 if (use_condition
->threshold_bytes
.set
) {
2619 threshold
= use_condition
->threshold_bytes
.value
;
2622 * Threshold was expressed as a ratio.
2624 * TODO the threshold (in bytes) of conditions expressed
2625 * as a ratio of total buffer size could be cached to
2626 * forego this double-multiplication or it could be performed
2627 * as fixed-point math.
2629 * Note that caching should accomodate the case where the
2630 * condition applies to multiple channels (i.e. don't assume
2631 * that all channels matching my_chann* have the same size...)
2633 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
2634 (double) buffer_capacity
);
2637 condition_type
= lttng_condition_get_type(condition
);
2638 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
2639 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2640 threshold
, sample
->highest_usage
);
2643 * The low condition should only be triggered once _all_ of the
2644 * streams in a channel have gone below the "low" threshold.
2646 if (sample
->highest_usage
<= threshold
) {
2650 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
2651 threshold
, sample
->highest_usage
);
2654 * For high buffer usage scenarios, we want to trigger whenever
2655 * _any_ of the streams has reached the "high" threshold.
2657 if (sample
->highest_usage
>= threshold
) {
2666 bool evaluate_session_consumed_size_condition(
2667 const struct lttng_condition
*condition
,
2668 uint64_t session_consumed_size
)
2671 const struct lttng_condition_session_consumed_size
*size_condition
=
2672 container_of(condition
,
2673 struct lttng_condition_session_consumed_size
,
2676 threshold
= size_condition
->consumed_threshold_bytes
.value
;
2677 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
2678 threshold
, session_consumed_size
);
2679 return session_consumed_size
>= threshold
;
2683 int evaluate_condition(const struct lttng_condition
*condition
,
2684 struct lttng_evaluation
**evaluation
,
2685 const struct notification_thread_state
*state
,
2686 const struct channel_state_sample
*previous_sample
,
2687 const struct channel_state_sample
*latest_sample
,
2688 uint64_t previous_session_consumed_total
,
2689 uint64_t latest_session_consumed_total
,
2690 struct channel_info
*channel_info
)
2693 enum lttng_condition_type condition_type
;
2694 const bool previous_sample_available
= !!previous_sample
;
2695 bool previous_sample_result
= false;
2696 bool latest_sample_result
;
2698 condition_type
= lttng_condition_get_type(condition
);
2700 switch (condition_type
) {
2701 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2702 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2703 if (caa_likely(previous_sample_available
)) {
2704 previous_sample_result
=
2705 evaluate_buffer_usage_condition(condition
,
2706 previous_sample
, channel_info
->capacity
);
2708 latest_sample_result
= evaluate_buffer_usage_condition(
2709 condition
, latest_sample
,
2710 channel_info
->capacity
);
2712 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2713 if (caa_likely(previous_sample_available
)) {
2714 previous_sample_result
=
2715 evaluate_session_consumed_size_condition(
2717 previous_session_consumed_total
);
2719 latest_sample_result
=
2720 evaluate_session_consumed_size_condition(
2722 latest_session_consumed_total
);
2725 /* Unknown condition type; internal error. */
2729 if (!latest_sample_result
||
2730 (previous_sample_result
== latest_sample_result
)) {
2732 * Only trigger on a condition evaluation transition.
2734 * NOTE: This edge-triggered logic may not be appropriate for
2735 * future condition types.
2740 if (!evaluation
|| !latest_sample_result
) {
2744 switch (condition_type
) {
2745 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2746 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2747 *evaluation
= lttng_evaluation_buffer_usage_create(
2749 latest_sample
->highest_usage
,
2750 channel_info
->capacity
);
2752 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
2753 *evaluation
= lttng_evaluation_session_consumed_size_create(
2754 latest_session_consumed_total
);
2769 int client_enqueue_dropped_notification(struct notification_client
*client
,
2770 struct notification_thread_state
*state
)
2773 struct lttng_notification_channel_message msg
= {
2774 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
2778 ret
= lttng_dynamic_buffer_append(
2779 &client
->communication
.outbound
.buffer
, &msg
,
2785 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
2786 const struct lttng_evaluation
*evaluation
,
2787 struct notification_client_list
* client_list
,
2788 struct notification_thread_state
*state
,
2789 uid_t channel_uid
, gid_t channel_gid
)
2792 struct lttng_dynamic_buffer msg_buffer
;
2793 struct notification_client_list_element
*client_list_element
, *tmp
;
2794 const struct lttng_notification notification
= {
2795 .condition
= (struct lttng_condition
*) lttng_trigger_get_const_condition(trigger
),
2796 .evaluation
= (struct lttng_evaluation
*) evaluation
,
2798 struct lttng_notification_channel_message msg_header
= {
2799 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
2802 lttng_dynamic_buffer_init(&msg_buffer
);
2804 ret
= lttng_dynamic_buffer_append(&msg_buffer
, &msg_header
,
2805 sizeof(msg_header
));
2810 ret
= lttng_notification_serialize(¬ification
, &msg_buffer
);
2812 ERR("[notification-thread] Failed to serialize notification");
2817 /* Update payload size. */
2818 ((struct lttng_notification_channel_message
* ) msg_buffer
.data
)->size
=
2819 (uint32_t) (msg_buffer
.size
- sizeof(msg_header
));
2821 cds_list_for_each_entry_safe(client_list_element
, tmp
,
2822 &client_list
->list
, node
) {
2823 struct notification_client
*client
=
2824 client_list_element
->client
;
2826 if (client
->uid
!= channel_uid
&& client
->gid
!= channel_gid
&&
2828 /* Client is not allowed to monitor this channel. */
2829 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this channel");
2833 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
2834 client
->socket
, msg_buffer
.size
);
2835 if (client
->communication
.outbound
.buffer
.size
) {
2837 * Outgoing data is already buffered for this client;
2838 * drop the notification and enqueue a "dropped
2839 * notification" message if this is the first dropped
2840 * notification since the socket spilled-over to the
2843 DBG("[notification-thread] Dropping notification addressed to client (socket fd = %i)",
2845 if (!client
->communication
.outbound
.dropped_notification
) {
2846 client
->communication
.outbound
.dropped_notification
= true;
2847 ret
= client_enqueue_dropped_notification(
2856 ret
= lttng_dynamic_buffer_append_buffer(
2857 &client
->communication
.outbound
.buffer
,
2863 ret
= client_flush_outgoing_queue(client
, state
);
2870 lttng_dynamic_buffer_reset(&msg_buffer
);
2874 int handle_notification_thread_channel_sample(
2875 struct notification_thread_state
*state
, int pipe
,
2876 enum lttng_domain_type domain
)
2879 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
2880 struct channel_info
*channel_info
;
2881 struct cds_lfht_node
*node
;
2882 struct cds_lfht_iter iter
;
2883 struct lttng_channel_trigger_list
*trigger_list
;
2884 struct lttng_trigger_list_element
*trigger_list_element
;
2885 bool previous_sample_available
= false;
2886 struct channel_state_sample previous_sample
, latest_sample
;
2887 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
2890 * The monitoring pipe only holds messages smaller than PIPE_BUF,
2891 * ensuring that read/write of sampling messages are atomic.
2893 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
2894 if (ret
!= sizeof(sample_msg
)) {
2895 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
2902 latest_sample
.key
.key
= sample_msg
.key
;
2903 latest_sample
.key
.domain
= domain
;
2904 latest_sample
.highest_usage
= sample_msg
.highest
;
2905 latest_sample
.lowest_usage
= sample_msg
.lowest
;
2906 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
2910 /* Retrieve the channel's informations */
2911 cds_lfht_lookup(state
->channels_ht
,
2912 hash_channel_key(&latest_sample
.key
),
2916 node
= cds_lfht_iter_get_node(&iter
);
2917 if (caa_unlikely(!node
)) {
2919 * Not an error since the consumer can push a sample to the pipe
2920 * and the rest of the session daemon could notify us of the
2921 * channel's destruction before we get a chance to process that
2924 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
2925 latest_sample
.key
.key
,
2926 domain
== LTTNG_DOMAIN_KERNEL
? "kernel" :
2930 channel_info
= caa_container_of(node
, struct channel_info
,
2932 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
2934 latest_sample
.key
.key
,
2935 channel_info
->session_info
->name
,
2936 latest_sample
.highest_usage
,
2937 latest_sample
.lowest_usage
,
2938 latest_sample
.channel_total_consumed
);
2940 previous_session_consumed_total
=
2941 channel_info
->session_info
->consumed_data_size
;
2943 /* Retrieve the channel's last sample, if it exists, and update it. */
2944 cds_lfht_lookup(state
->channel_state_ht
,
2945 hash_channel_key(&latest_sample
.key
),
2946 match_channel_state_sample
,
2949 node
= cds_lfht_iter_get_node(&iter
);
2950 if (caa_likely(node
)) {
2951 struct channel_state_sample
*stored_sample
;
2953 /* Update the sample stored. */
2954 stored_sample
= caa_container_of(node
,
2955 struct channel_state_sample
,
2956 channel_state_ht_node
);
2958 memcpy(&previous_sample
, stored_sample
,
2959 sizeof(previous_sample
));
2960 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
2961 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
2962 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
2963 previous_sample_available
= true;
2965 latest_session_consumed_total
=
2966 previous_session_consumed_total
+
2967 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
2970 * This is the channel's first sample, allocate space for and
2971 * store the new sample.
2973 struct channel_state_sample
*stored_sample
;
2975 stored_sample
= zmalloc(sizeof(*stored_sample
));
2976 if (!stored_sample
) {
2981 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
2982 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
2983 cds_lfht_add(state
->channel_state_ht
,
2984 hash_channel_key(&stored_sample
->key
),
2985 &stored_sample
->channel_state_ht_node
);
2987 latest_session_consumed_total
=
2988 previous_session_consumed_total
+
2989 latest_sample
.channel_total_consumed
;
2992 channel_info
->session_info
->consumed_data_size
=
2993 latest_session_consumed_total
;
2995 /* Find triggers associated with this channel. */
2996 cds_lfht_lookup(state
->channel_triggers_ht
,
2997 hash_channel_key(&latest_sample
.key
),
2998 match_channel_trigger_list
,
3001 node
= cds_lfht_iter_get_node(&iter
);
3002 if (caa_likely(!node
)) {
3006 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
3007 channel_triggers_ht_node
);
3008 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
3010 const struct lttng_condition
*condition
;
3011 const struct lttng_action
*action
;
3012 const struct lttng_trigger
*trigger
;
3013 struct notification_client_list
*client_list
;
3014 struct lttng_evaluation
*evaluation
= NULL
;
3016 trigger
= trigger_list_element
->trigger
;
3017 condition
= lttng_trigger_get_const_condition(trigger
);
3019 action
= lttng_trigger_get_const_action(trigger
);
3021 /* Notify actions are the only type currently supported. */
3022 assert(lttng_action_get_type_const(action
) ==
3023 LTTNG_ACTION_TYPE_NOTIFY
);
3026 * Check if any client is subscribed to the result of this
3029 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
3030 lttng_condition_hash(condition
),
3034 node
= cds_lfht_iter_get_node(&iter
);
3037 client_list
= caa_container_of(node
,
3038 struct notification_client_list
,
3039 notification_trigger_ht_node
);
3040 if (cds_list_empty(&client_list
->list
)) {
3042 * No clients interested in the evaluation's result,
3048 ret
= evaluate_condition(condition
, &evaluation
, state
,
3049 previous_sample_available
? &previous_sample
: NULL
,
3051 previous_session_consumed_total
,
3052 latest_session_consumed_total
,
3054 if (caa_unlikely(ret
)) {
3058 if (caa_likely(!evaluation
)) {
3062 /* Dispatch evaluation result to all clients. */
3063 ret
= send_evaluation_to_clients(trigger_list_element
->trigger
,
3064 evaluation
, client_list
, state
,
3065 channel_info
->session_info
->uid
,
3066 channel_info
->session_info
->gid
);
3067 lttng_evaluation_destroy(evaluation
);
3068 if (caa_unlikely(ret
)) {