2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.h"
12 #include <urcu/rculfhash.h>
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/futex.h>
17 #include <common/unix.h>
18 #include <common/dynamic-buffer.h>
19 #include <common/hashtable/utils.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/macros.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.h>
24 #include <lttng/action/group-internal.h>
25 #include <lttng/domain-internal.h>
26 #include <lttng/notification/notification-internal.h>
27 #include <lttng/condition/condition-internal.h>
28 #include <lttng/condition/buffer-usage-internal.h>
29 #include <lttng/condition/session-consumed-size-internal.h>
30 #include <lttng/condition/session-rotation-internal.h>
31 #include <lttng/condition/on-event-internal.h>
32 #include <lttng/domain-internal.h>
33 #include <lttng/notification/channel-internal.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <lttng/event-rule/event-rule-internal.h>
43 #include "condition-internal.h"
44 #include "event-notifier-error-accounting.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 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
55 #define MAX_CAPTURE_SIZE (PIPE_BUF)
57 enum lttng_object_type
{
58 LTTNG_OBJECT_TYPE_UNKNOWN
,
59 LTTNG_OBJECT_TYPE_NONE
,
60 LTTNG_OBJECT_TYPE_CHANNEL
,
61 LTTNG_OBJECT_TYPE_SESSION
,
64 struct lttng_trigger_list_element
{
65 /* No ownership of the trigger object is assumed. */
66 struct lttng_trigger
*trigger
;
67 struct cds_list_head node
;
70 struct lttng_channel_trigger_list
{
71 struct channel_key channel_key
;
72 /* List of struct lttng_trigger_list_element. */
73 struct cds_list_head list
;
74 /* Node in the channel_triggers_ht */
75 struct cds_lfht_node channel_triggers_ht_node
;
76 /* call_rcu delayed reclaim. */
77 struct rcu_head rcu_node
;
81 * List of triggers applying to a given session.
84 * - lttng_session_trigger_list_create()
85 * - lttng_session_trigger_list_build()
86 * - lttng_session_trigger_list_destroy()
87 * - lttng_session_trigger_list_add()
89 struct lttng_session_trigger_list
{
91 * Not owned by this; points to the session_info structure's
94 const char *session_name
;
95 /* List of struct lttng_trigger_list_element. */
96 struct cds_list_head list
;
97 /* Node in the session_triggers_ht */
98 struct cds_lfht_node session_triggers_ht_node
;
100 * Weak reference to the notification system's session triggers
103 * The session trigger list structure structure is owned by
104 * the session's session_info.
106 * The session_info is kept alive the the channel_infos holding a
107 * reference to it (reference counting). When those channels are
108 * destroyed (at runtime or on teardown), the reference they hold
109 * to the session_info are released. On destruction of session_info,
110 * session_info_destroy() will remove the list of triggers applying
111 * to this session from the notification system's state.
113 * This implies that the session_triggers_ht must be destroyed
114 * after the channels.
116 struct cds_lfht
*session_triggers_ht
;
117 /* Used for delayed RCU reclaim. */
118 struct rcu_head rcu_node
;
121 struct lttng_trigger_ht_element
{
122 struct lttng_trigger
*trigger
;
123 struct cds_lfht_node node
;
124 struct cds_lfht_node node_by_name_uid
;
125 struct cds_list_head client_list_trigger_node
;
126 /* call_rcu delayed reclaim. */
127 struct rcu_head rcu_node
;
130 struct lttng_condition_list_element
{
131 struct lttng_condition
*condition
;
132 struct cds_list_head node
;
135 struct channel_state_sample
{
136 struct channel_key key
;
137 struct cds_lfht_node channel_state_ht_node
;
138 uint64_t highest_usage
;
139 uint64_t lowest_usage
;
140 uint64_t channel_total_consumed
;
141 /* call_rcu delayed reclaim. */
142 struct rcu_head rcu_node
;
145 static unsigned long hash_channel_key(struct channel_key
*key
);
146 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
147 struct lttng_evaluation
**evaluation
,
148 const struct notification_thread_state
*state
,
149 const struct channel_state_sample
*previous_sample
,
150 const struct channel_state_sample
*latest_sample
,
151 uint64_t previous_session_consumed_total
,
152 uint64_t latest_session_consumed_total
,
153 struct channel_info
*channel_info
);
155 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
156 const struct lttng_evaluation
*evaluation
,
157 struct notification_client_list
*client_list
,
158 struct notification_thread_state
*state
,
159 uid_t channel_uid
, gid_t channel_gid
);
162 /* session_info API */
164 void session_info_destroy(void *_data
);
166 void session_info_get(struct session_info
*session_info
);
168 void session_info_put(struct session_info
*session_info
);
170 struct session_info
*session_info_create(const char *name
,
171 uid_t uid
, gid_t gid
,
172 struct lttng_session_trigger_list
*trigger_list
,
173 struct cds_lfht
*sessions_ht
);
175 void session_info_add_channel(struct session_info
*session_info
,
176 struct channel_info
*channel_info
);
178 void session_info_remove_channel(struct session_info
*session_info
,
179 struct channel_info
*channel_info
);
181 /* lttng_session_trigger_list API */
183 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
184 const char *session_name
,
185 struct cds_lfht
*session_triggers_ht
);
187 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
188 const struct notification_thread_state
*state
,
189 const char *session_name
);
191 void lttng_session_trigger_list_destroy(
192 struct lttng_session_trigger_list
*list
);
194 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
195 struct lttng_trigger
*trigger
);
198 int client_handle_transmission_status(
199 struct notification_client
*client
,
200 enum client_transmission_status transmission_status
,
201 struct notification_thread_state
*state
);
204 int handle_one_event_notifier_notification(
205 struct notification_thread_state
*state
,
206 int pipe
, enum lttng_domain_type domain
);
209 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
212 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
214 /* This double-cast is intended to supress pointer-to-cast warning. */
215 const int socket
= (int) (intptr_t) key
;
216 const struct notification_client
*client
= caa_container_of(node
,
217 struct notification_client
, client_socket_ht_node
);
219 return client
->socket
== socket
;
223 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
225 /* This double-cast is intended to supress pointer-to-cast warning. */
226 const notification_client_id id
= *((notification_client_id
*) key
);
227 const struct notification_client
*client
= caa_container_of(
228 node
, struct notification_client
, client_id_ht_node
);
230 return client
->id
== id
;
234 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
236 struct channel_key
*channel_key
= (struct channel_key
*) key
;
237 struct lttng_channel_trigger_list
*trigger_list
;
239 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
240 channel_triggers_ht_node
);
242 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
243 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
247 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
249 const char *session_name
= (const char *) key
;
250 struct lttng_session_trigger_list
*trigger_list
;
252 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
253 session_triggers_ht_node
);
255 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
259 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
261 struct channel_key
*channel_key
= (struct channel_key
*) key
;
262 struct channel_state_sample
*sample
;
264 sample
= caa_container_of(node
, struct channel_state_sample
,
265 channel_state_ht_node
);
267 return !!((channel_key
->key
== sample
->key
.key
) &&
268 (channel_key
->domain
== sample
->key
.domain
));
272 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
274 struct channel_key
*channel_key
= (struct channel_key
*) key
;
275 struct channel_info
*channel_info
;
277 channel_info
= caa_container_of(node
, struct channel_info
,
280 return !!((channel_key
->key
== channel_info
->key
.key
) &&
281 (channel_key
->domain
== channel_info
->key
.domain
));
285 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
287 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
288 struct lttng_trigger_ht_element
*trigger_ht_element
;
290 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
293 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
297 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
299 const uint64_t *_key
= key
;
300 struct notification_trigger_tokens_ht_element
*element
;
302 element
= caa_container_of(node
,
303 struct notification_trigger_tokens_ht_element
, node
);
304 return *_key
== element
->token
;
308 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
310 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
311 struct notification_client_list
*client_list
;
312 const struct lttng_condition
*condition
;
314 assert(condition_key
);
316 client_list
= caa_container_of(node
, struct notification_client_list
,
317 notification_trigger_clients_ht_node
);
318 condition
= client_list
->condition
;
320 return !!lttng_condition_is_equal(condition_key
, condition
);
324 int match_session(struct cds_lfht_node
*node
, const void *key
)
326 const char *name
= key
;
327 struct session_info
*session_info
= caa_container_of(
328 node
, struct session_info
, sessions_ht_node
);
330 return !strcmp(session_info
->name
, name
);
334 const char *notification_command_type_str(
335 enum notification_thread_command_type type
)
338 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
339 return "REGISTER_TRIGGER";
340 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
341 return "UNREGISTER_TRIGGER";
342 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
343 return "ADD_CHANNEL";
344 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
345 return "REMOVE_CHANNEL";
346 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
347 return "SESSION_ROTATION_ONGOING";
348 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
349 return "SESSION_ROTATION_COMPLETED";
350 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
351 return "ADD_TRACER_EVENT_SOURCE";
352 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
353 return "REMOVE_TRACER_EVENT_SOURCE";
354 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
355 return "LIST_TRIGGERS";
356 case NOTIFICATION_COMMAND_TYPE_QUIT
:
358 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
359 return "CLIENT_COMMUNICATION_UPDATE";
366 * Match trigger based on name and credentials only.
367 * Name duplication is NOT allowed for the same uid.
370 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
375 const char *key_name
;
376 enum lttng_trigger_status status
;
377 const struct lttng_credentials
*key_creds
;
378 const struct lttng_credentials
*node_creds
;
379 const struct lttng_trigger
*trigger_key
=
380 (const struct lttng_trigger
*) key
;
381 const struct lttng_trigger_ht_element
*trigger_ht_element
=
382 caa_container_of(node
,
383 struct lttng_trigger_ht_element
,
386 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
387 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
389 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
390 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
392 /* Compare the names. */
393 if (strcmp(name
, key_name
) != 0) {
397 /* Compare the owners' UIDs. */
398 key_creds
= lttng_trigger_get_credentials(trigger_key
);
399 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
401 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
408 * Hash trigger based on name and credentials only.
411 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
413 unsigned long hash
= 0;
414 const struct lttng_credentials
*trigger_creds
;
415 const char *trigger_name
;
416 enum lttng_trigger_status status
;
418 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
419 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
420 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
423 trigger_creds
= lttng_trigger_get_credentials(trigger
);
424 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
431 unsigned long hash_channel_key(struct channel_key
*key
)
433 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
434 unsigned long domain_hash
= hash_key_ulong(
435 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
437 return key_hash
^ domain_hash
;
441 unsigned long hash_client_socket(int socket
)
443 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
447 unsigned long hash_client_id(notification_client_id id
)
449 return hash_key_u64(&id
, lttng_ht_seed
);
453 * Get the type of object to which a given condition applies. Bindings let
454 * the notification system evaluate a trigger's condition when a given
455 * object's state is updated.
457 * For instance, a condition bound to a channel will be evaluated everytime
458 * the channel's state is changed by a channel monitoring sample.
461 enum lttng_object_type
get_condition_binding_object(
462 const struct lttng_condition
*condition
)
464 switch (lttng_condition_get_type(condition
)) {
465 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
466 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
467 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
468 return LTTNG_OBJECT_TYPE_CHANNEL
;
469 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
470 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
471 return LTTNG_OBJECT_TYPE_SESSION
;
472 case LTTNG_CONDITION_TYPE_ON_EVENT
:
473 return LTTNG_OBJECT_TYPE_NONE
;
475 return LTTNG_OBJECT_TYPE_UNKNOWN
;
480 void free_channel_info_rcu(struct rcu_head
*node
)
482 free(caa_container_of(node
, struct channel_info
, rcu_node
));
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
);
500 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
504 void free_session_info_rcu(struct rcu_head
*node
)
506 free(caa_container_of(node
, struct session_info
, rcu_node
));
509 /* Don't call directly, use the ref-counting mechanism. */
511 void session_info_destroy(void *_data
)
513 struct session_info
*session_info
= _data
;
516 assert(session_info
);
517 if (session_info
->channel_infos_ht
) {
518 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
520 ERR("[notification-thread] Failed to destroy channel information hash table");
523 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
526 cds_lfht_del(session_info
->sessions_ht
,
527 &session_info
->sessions_ht_node
);
529 free(session_info
->name
);
530 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
534 void session_info_get(struct session_info
*session_info
)
539 lttng_ref_get(&session_info
->ref
);
543 void session_info_put(struct session_info
*session_info
)
548 lttng_ref_put(&session_info
->ref
);
552 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
553 struct lttng_session_trigger_list
*trigger_list
,
554 struct cds_lfht
*sessions_ht
)
556 struct session_info
*session_info
;
560 session_info
= zmalloc(sizeof(*session_info
));
564 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
566 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
567 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
568 if (!session_info
->channel_infos_ht
) {
572 cds_lfht_node_init(&session_info
->sessions_ht_node
);
573 session_info
->name
= strdup(name
);
574 if (!session_info
->name
) {
577 session_info
->uid
= uid
;
578 session_info
->gid
= gid
;
579 session_info
->trigger_list
= trigger_list
;
580 session_info
->sessions_ht
= sessions_ht
;
584 session_info_put(session_info
);
589 void session_info_add_channel(struct session_info
*session_info
,
590 struct channel_info
*channel_info
)
593 cds_lfht_add(session_info
->channel_infos_ht
,
594 hash_channel_key(&channel_info
->key
),
595 &channel_info
->session_info_channels_ht_node
);
600 void session_info_remove_channel(struct session_info
*session_info
,
601 struct channel_info
*channel_info
)
604 cds_lfht_del(session_info
->channel_infos_ht
,
605 &channel_info
->session_info_channels_ht_node
);
610 struct channel_info
*channel_info_create(const char *channel_name
,
611 struct channel_key
*channel_key
, uint64_t channel_capacity
,
612 struct session_info
*session_info
)
614 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
620 cds_lfht_node_init(&channel_info
->channels_ht_node
);
621 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
622 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
623 channel_info
->capacity
= channel_capacity
;
625 channel_info
->name
= strdup(channel_name
);
626 if (!channel_info
->name
) {
631 * Set the references between session and channel infos:
632 * - channel_info holds a strong reference to session_info
633 * - session_info holds a weak reference to channel_info
635 session_info_get(session_info
);
636 session_info_add_channel(session_info
, channel_info
);
637 channel_info
->session_info
= session_info
;
641 channel_info_destroy(channel_info
);
646 bool notification_client_list_get(struct notification_client_list
*list
)
648 return urcu_ref_get_unless_zero(&list
->ref
);
652 void free_notification_client_list_rcu(struct rcu_head
*node
)
654 free(caa_container_of(node
, struct notification_client_list
,
659 void notification_client_list_release(struct urcu_ref
*list_ref
)
661 struct notification_client_list
*list
=
662 container_of(list_ref
, typeof(*list
), ref
);
663 struct notification_client_list_element
*client_list_element
, *tmp
;
665 lttng_condition_put(list
->condition
);
667 if (list
->notification_trigger_clients_ht
) {
670 cds_lfht_del(list
->notification_trigger_clients_ht
,
671 &list
->notification_trigger_clients_ht_node
);
673 list
->notification_trigger_clients_ht
= NULL
;
675 cds_list_for_each_entry_safe(client_list_element
, tmp
,
676 &list
->clients_list
, node
) {
677 free(client_list_element
);
680 assert(cds_list_empty(&list
->triggers_list
));
682 pthread_mutex_destroy(&list
->lock
);
683 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
687 bool condition_applies_to_client(const struct lttng_condition
*condition
,
688 struct notification_client
*client
)
690 bool applies
= false;
691 struct lttng_condition_list_element
*condition_list_element
;
693 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
695 applies
= lttng_condition_is_equal(
696 condition_list_element
->condition
,
707 struct notification_client_list
*notification_client_list_create(
708 struct notification_thread_state
*state
,
709 const struct lttng_condition
*condition
)
711 struct notification_client
*client
;
712 struct cds_lfht_iter iter
;
713 struct notification_client_list
*client_list
;
715 client_list
= zmalloc(sizeof(*client_list
));
717 PERROR("Failed to allocate notification client list");
721 pthread_mutex_init(&client_list
->lock
, NULL
);
723 * The trigger that owns the condition has the first reference to this
726 urcu_ref_init(&client_list
->ref
);
727 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
728 CDS_INIT_LIST_HEAD(&client_list
->clients_list
);
729 CDS_INIT_LIST_HEAD(&client_list
->triggers_list
);
732 * Create a copy of the condition so that it's independent of any
733 * trigger. The client list may outlive the trigger object (which owns
734 * the condition) that is used to create it.
736 client_list
->condition
= lttng_condition_copy(condition
);
738 /* Build a list of clients to which this new condition applies. */
739 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
740 client_socket_ht_node
) {
741 struct notification_client_list_element
*client_list_element
;
743 if (!condition_applies_to_client(condition
, client
)) {
747 client_list_element
= zmalloc(sizeof(*client_list_element
));
748 if (!client_list_element
) {
749 goto error_put_client_list
;
752 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
753 client_list_element
->client
= client
;
754 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
757 client_list
->notification_trigger_clients_ht
=
758 state
->notification_trigger_clients_ht
;
762 * Add the client list to the global list of client list.
764 cds_lfht_add_unique(state
->notification_trigger_clients_ht
,
765 lttng_condition_hash(client_list
->condition
),
766 match_client_list_condition
,
767 client_list
->condition
,
768 &client_list
->notification_trigger_clients_ht_node
);
772 error_put_client_list
:
773 notification_client_list_put(client_list
);
780 void notification_client_list_put(struct notification_client_list
*list
)
785 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
788 /* Provides a reference to the returned list. */
790 struct notification_client_list
*get_client_list_from_condition(
791 struct notification_thread_state
*state
,
792 const struct lttng_condition
*condition
)
794 struct cds_lfht_node
*node
;
795 struct cds_lfht_iter iter
;
796 struct notification_client_list
*list
= NULL
;
799 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
800 lttng_condition_hash(condition
),
801 match_client_list_condition
,
804 node
= cds_lfht_iter_get_node(&iter
);
806 list
= container_of(node
, struct notification_client_list
,
807 notification_trigger_clients_ht_node
);
808 list
= notification_client_list_get(list
) ? list
: NULL
;
816 int evaluate_channel_condition_for_client(
817 const struct lttng_condition
*condition
,
818 struct notification_thread_state
*state
,
819 struct lttng_evaluation
**evaluation
,
820 uid_t
*session_uid
, gid_t
*session_gid
)
823 struct cds_lfht_iter iter
;
824 struct cds_lfht_node
*node
;
825 struct channel_info
*channel_info
= NULL
;
826 struct channel_key
*channel_key
= NULL
;
827 struct channel_state_sample
*last_sample
= NULL
;
828 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
832 /* Find the channel associated with the condition. */
833 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
834 channel_trigger_list
, channel_triggers_ht_node
) {
835 struct lttng_trigger_list_element
*element
;
837 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
838 const struct lttng_condition
*current_condition
=
839 lttng_trigger_get_const_condition(
842 assert(current_condition
);
843 if (!lttng_condition_is_equal(condition
,
844 current_condition
)) {
848 /* Found the trigger, save the channel key. */
849 channel_key
= &channel_trigger_list
->channel_key
;
853 /* The channel key was found stop iteration. */
859 /* No channel found; normal exit. */
860 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
865 /* Fetch channel info for the matching channel. */
866 cds_lfht_lookup(state
->channels_ht
,
867 hash_channel_key(channel_key
),
871 node
= cds_lfht_iter_get_node(&iter
);
873 channel_info
= caa_container_of(node
, struct channel_info
,
876 /* Retrieve the channel's last sample, if it exists. */
877 cds_lfht_lookup(state
->channel_state_ht
,
878 hash_channel_key(channel_key
),
879 match_channel_state_sample
,
882 node
= cds_lfht_iter_get_node(&iter
);
884 last_sample
= caa_container_of(node
,
885 struct channel_state_sample
,
886 channel_state_ht_node
);
888 /* Nothing to evaluate, no sample was ever taken. Normal exit */
889 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
894 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
896 0, channel_info
->session_info
->consumed_data_size
,
899 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
903 *session_uid
= channel_info
->session_info
->uid
;
904 *session_gid
= channel_info
->session_info
->gid
;
911 const char *get_condition_session_name(const struct lttng_condition
*condition
)
913 const char *session_name
= NULL
;
914 enum lttng_condition_status status
;
916 switch (lttng_condition_get_type(condition
)) {
917 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
918 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
919 status
= lttng_condition_buffer_usage_get_session_name(
920 condition
, &session_name
);
922 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
923 status
= lttng_condition_session_consumed_size_get_session_name(
924 condition
, &session_name
);
926 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
927 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
928 status
= lttng_condition_session_rotation_get_session_name(
929 condition
, &session_name
);
934 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
935 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
943 int evaluate_session_condition_for_client(
944 const struct lttng_condition
*condition
,
945 struct notification_thread_state
*state
,
946 struct lttng_evaluation
**evaluation
,
947 uid_t
*session_uid
, gid_t
*session_gid
)
950 struct cds_lfht_iter iter
;
951 struct cds_lfht_node
*node
;
952 const char *session_name
;
953 struct session_info
*session_info
= NULL
;
956 session_name
= get_condition_session_name(condition
);
958 /* Find the session associated with the trigger. */
959 cds_lfht_lookup(state
->sessions_ht
,
960 hash_key_str(session_name
, lttng_ht_seed
),
964 node
= cds_lfht_iter_get_node(&iter
);
966 DBG("[notification-thread] No known session matching name \"%s\"",
972 session_info
= caa_container_of(node
, struct session_info
,
974 session_info_get(session_info
);
977 * Evaluation is performed in-line here since only one type of
978 * session-bound condition is handled for the moment.
980 switch (lttng_condition_get_type(condition
)) {
981 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
982 if (!session_info
->rotation
.ongoing
) {
984 goto end_session_put
;
987 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
988 session_info
->rotation
.id
);
991 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
994 goto end_session_put
;
1000 goto end_session_put
;
1003 *session_uid
= session_info
->uid
;
1004 *session_gid
= session_info
->gid
;
1007 session_info_put(session_info
);
1014 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1015 const struct lttng_condition
*condition
,
1016 struct notification_client
*client
,
1017 struct notification_thread_state
*state
)
1020 struct lttng_evaluation
*evaluation
= NULL
;
1021 struct notification_client_list client_list
= {
1022 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1024 struct notification_client_list_element client_list_element
= { 0 };
1025 uid_t object_uid
= 0;
1026 gid_t object_gid
= 0;
1033 switch (get_condition_binding_object(condition
)) {
1034 case LTTNG_OBJECT_TYPE_SESSION
:
1035 ret
= evaluate_session_condition_for_client(condition
, state
,
1036 &evaluation
, &object_uid
, &object_gid
);
1038 case LTTNG_OBJECT_TYPE_CHANNEL
:
1039 ret
= evaluate_channel_condition_for_client(condition
, state
,
1040 &evaluation
, &object_uid
, &object_gid
);
1042 case LTTNG_OBJECT_TYPE_NONE
:
1043 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
1046 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1056 /* Evaluation yielded nothing. Normal exit. */
1057 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
1063 * Create a temporary client list with the client currently
1066 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1067 CDS_INIT_LIST_HEAD(&client_list
.clients_list
);
1069 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1070 client_list_element
.client
= client
;
1071 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1073 /* Send evaluation result to the newly-subscribed client. */
1074 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1075 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1076 state
, object_uid
, object_gid
);
1083 int notification_thread_client_subscribe(struct notification_client
*client
,
1084 struct lttng_condition
*condition
,
1085 struct notification_thread_state
*state
,
1086 enum lttng_notification_channel_status
*_status
)
1089 struct notification_client_list
*client_list
= NULL
;
1090 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1091 struct notification_client_list_element
*client_list_element
= NULL
;
1092 struct lttng_trigger_ht_element
*trigger_ht_element
;
1093 enum lttng_notification_channel_status status
=
1094 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1097 * Ensure that the client has not already subscribed to this condition
1100 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1101 if (lttng_condition_is_equal(condition_list_element
->condition
,
1103 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1108 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1109 if (!condition_list_element
) {
1113 client_list_element
= zmalloc(sizeof(*client_list_element
));
1114 if (!client_list_element
) {
1120 * Add the newly-subscribed condition to the client's subscription list.
1122 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1123 condition_list_element
->condition
= condition
;
1124 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1126 client_list
= get_client_list_from_condition(state
, condition
);
1129 * No notification-emiting trigger registered with this
1130 * condition. We don't evaluate the condition right away
1131 * since this trigger is not registered yet.
1133 free(client_list_element
);
1138 * The condition to which the client just subscribed is evaluated
1139 * at this point so that conditions that are already TRUE result
1140 * in a notification being sent out.
1142 * Note the iteration on all triggers which share an identical
1143 * `condition` than the one to which the client is registering. This is
1144 * done to ensure that the client receives a distinct notification for
1145 * all triggers that have a `notify` action that have this condition.
1147 pthread_mutex_lock(&client_list
->lock
);
1148 cds_list_for_each_entry(trigger_ht_element
,
1149 &client_list
->triggers_list
, client_list_trigger_node
) {
1150 if (evaluate_condition_for_client(trigger_ht_element
->trigger
, condition
,
1152 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1154 free(client_list_element
);
1158 pthread_mutex_unlock(&client_list
->lock
);
1161 * Add the client to the list of clients interested in a given trigger
1162 * if a "notification" trigger with a corresponding condition was
1165 client_list_element
->client
= client
;
1166 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1168 pthread_mutex_lock(&client_list
->lock
);
1169 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1170 pthread_mutex_unlock(&client_list
->lock
);
1176 notification_client_list_put(client_list
);
1180 free(condition_list_element
);
1181 free(client_list_element
);
1186 int notification_thread_client_unsubscribe(
1187 struct notification_client
*client
,
1188 struct lttng_condition
*condition
,
1189 struct notification_thread_state
*state
,
1190 enum lttng_notification_channel_status
*_status
)
1192 struct notification_client_list
*client_list
;
1193 struct lttng_condition_list_element
*condition_list_element
,
1195 struct notification_client_list_element
*client_list_element
,
1197 bool condition_found
= false;
1198 enum lttng_notification_channel_status status
=
1199 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1201 /* Remove the condition from the client's condition list. */
1202 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1203 &client
->condition_list
, node
) {
1204 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1209 cds_list_del(&condition_list_element
->node
);
1211 * The caller may be iterating on the client's conditions to
1212 * tear down a client's connection. In this case, the condition
1213 * will be destroyed at the end.
1215 if (condition
!= condition_list_element
->condition
) {
1216 lttng_condition_destroy(
1217 condition_list_element
->condition
);
1219 free(condition_list_element
);
1220 condition_found
= true;
1224 if (!condition_found
) {
1225 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1230 * Remove the client from the list of clients interested the trigger
1231 * matching the condition.
1233 client_list
= get_client_list_from_condition(state
, condition
);
1238 pthread_mutex_lock(&client_list
->lock
);
1239 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1240 &client_list
->clients_list
, node
) {
1241 if (client_list_element
->client
->id
!= client
->id
) {
1244 cds_list_del(&client_list_element
->node
);
1245 free(client_list_element
);
1248 pthread_mutex_unlock(&client_list
->lock
);
1249 notification_client_list_put(client_list
);
1252 lttng_condition_destroy(condition
);
1260 void free_notification_client_rcu(struct rcu_head
*node
)
1262 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1266 void notification_client_destroy(struct notification_client
*client
,
1267 struct notification_thread_state
*state
)
1274 * The client object is not reachable by other threads, no need to lock
1277 if (client
->socket
>= 0) {
1278 (void) lttcomm_close_unix_sock(client
->socket
);
1279 client
->socket
= -1;
1281 client
->communication
.active
= false;
1282 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1283 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1284 pthread_mutex_destroy(&client
->lock
);
1285 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1289 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1293 struct notification_client
*get_client_from_socket(int socket
,
1294 struct notification_thread_state
*state
)
1296 struct cds_lfht_iter iter
;
1297 struct cds_lfht_node
*node
;
1298 struct notification_client
*client
= NULL
;
1300 cds_lfht_lookup(state
->client_socket_ht
,
1301 hash_client_socket(socket
),
1302 match_client_socket
,
1303 (void *) (unsigned long) socket
,
1305 node
= cds_lfht_iter_get_node(&iter
);
1310 client
= caa_container_of(node
, struct notification_client
,
1311 client_socket_ht_node
);
1317 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1321 struct notification_client
*get_client_from_id(notification_client_id id
,
1322 struct notification_thread_state
*state
)
1324 struct cds_lfht_iter iter
;
1325 struct cds_lfht_node
*node
;
1326 struct notification_client
*client
= NULL
;
1328 cds_lfht_lookup(state
->client_id_ht
,
1333 node
= cds_lfht_iter_get_node(&iter
);
1338 client
= caa_container_of(node
, struct notification_client
,
1345 bool buffer_usage_condition_applies_to_channel(
1346 const struct lttng_condition
*condition
,
1347 const struct channel_info
*channel_info
)
1349 enum lttng_condition_status status
;
1350 enum lttng_domain_type condition_domain
;
1351 const char *condition_session_name
= NULL
;
1352 const char *condition_channel_name
= NULL
;
1354 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1356 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1357 if (channel_info
->key
.domain
!= condition_domain
) {
1361 status
= lttng_condition_buffer_usage_get_session_name(
1362 condition
, &condition_session_name
);
1363 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1365 status
= lttng_condition_buffer_usage_get_channel_name(
1366 condition
, &condition_channel_name
);
1367 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1369 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1372 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1382 bool session_consumed_size_condition_applies_to_channel(
1383 const struct lttng_condition
*condition
,
1384 const struct channel_info
*channel_info
)
1386 enum lttng_condition_status status
;
1387 const char *condition_session_name
= NULL
;
1389 status
= lttng_condition_session_consumed_size_get_session_name(
1390 condition
, &condition_session_name
);
1391 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1393 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1403 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1404 const struct channel_info
*channel_info
)
1406 const struct lttng_condition
*condition
;
1407 bool trigger_applies
;
1409 condition
= lttng_trigger_get_const_condition(trigger
);
1414 switch (lttng_condition_get_type(condition
)) {
1415 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1416 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1417 trigger_applies
= buffer_usage_condition_applies_to_channel(
1418 condition
, channel_info
);
1420 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1421 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1422 condition
, channel_info
);
1428 return trigger_applies
;
1433 /* Must be called with RCU read lock held. */
1435 struct lttng_session_trigger_list
*get_session_trigger_list(
1436 struct notification_thread_state
*state
,
1437 const char *session_name
)
1439 struct lttng_session_trigger_list
*list
= NULL
;
1440 struct cds_lfht_node
*node
;
1441 struct cds_lfht_iter iter
;
1443 cds_lfht_lookup(state
->session_triggers_ht
,
1444 hash_key_str(session_name
, lttng_ht_seed
),
1445 match_session_trigger_list
,
1448 node
= cds_lfht_iter_get_node(&iter
);
1451 * Not an error, the list of triggers applying to that session
1452 * will be initialized when the session is created.
1454 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1459 list
= caa_container_of(node
,
1460 struct lttng_session_trigger_list
,
1461 session_triggers_ht_node
);
1467 * Allocate an empty lttng_session_trigger_list for the session named
1470 * No ownership of 'session_name' is assumed by the session trigger list.
1471 * It is the caller's responsability to ensure the session name is alive
1472 * for as long as this list is.
1475 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1476 const char *session_name
,
1477 struct cds_lfht
*session_triggers_ht
)
1479 struct lttng_session_trigger_list
*list
;
1481 list
= zmalloc(sizeof(*list
));
1485 list
->session_name
= session_name
;
1486 CDS_INIT_LIST_HEAD(&list
->list
);
1487 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1488 list
->session_triggers_ht
= session_triggers_ht
;
1491 /* Publish the list through the session_triggers_ht. */
1492 cds_lfht_add(session_triggers_ht
,
1493 hash_key_str(session_name
, lttng_ht_seed
),
1494 &list
->session_triggers_ht_node
);
1501 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1503 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1508 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1510 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1512 /* Empty the list element by element, and then free the list itself. */
1513 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1514 &list
->list
, node
) {
1515 cds_list_del(&trigger_list_element
->node
);
1516 free(trigger_list_element
);
1519 /* Unpublish the list from the session_triggers_ht. */
1520 cds_lfht_del(list
->session_triggers_ht
,
1521 &list
->session_triggers_ht_node
);
1523 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1527 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1528 struct lttng_trigger
*trigger
)
1531 struct lttng_trigger_list_element
*new_element
=
1532 zmalloc(sizeof(*new_element
));
1538 CDS_INIT_LIST_HEAD(&new_element
->node
);
1539 new_element
->trigger
= trigger
;
1540 cds_list_add(&new_element
->node
, &list
->list
);
1546 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1547 const char *session_name
)
1549 bool applies
= false;
1550 const struct lttng_condition
*condition
;
1552 condition
= lttng_trigger_get_const_condition(trigger
);
1553 switch (lttng_condition_get_type(condition
)) {
1554 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1555 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1557 enum lttng_condition_status condition_status
;
1558 const char *condition_session_name
;
1560 condition_status
= lttng_condition_session_rotation_get_session_name(
1561 condition
, &condition_session_name
);
1562 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1563 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1567 assert(condition_session_name
);
1568 applies
= !strcmp(condition_session_name
, session_name
);
1579 * Allocate and initialize an lttng_session_trigger_list which contains
1580 * all triggers that apply to the session named 'session_name'.
1582 * No ownership of 'session_name' is assumed by the session trigger list.
1583 * It is the caller's responsability to ensure the session name is alive
1584 * for as long as this list is.
1587 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1588 const struct notification_thread_state
*state
,
1589 const char *session_name
)
1591 int trigger_count
= 0;
1592 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1593 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1594 struct cds_lfht_iter iter
;
1596 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1597 state
->session_triggers_ht
);
1599 /* Add all triggers applying to the session named 'session_name'. */
1600 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1604 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1609 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1610 trigger_ht_element
->trigger
);
1618 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1620 return session_trigger_list
;
1622 lttng_session_trigger_list_destroy(session_trigger_list
);
1627 struct session_info
*find_or_create_session_info(
1628 struct notification_thread_state
*state
,
1629 const char *name
, uid_t uid
, gid_t gid
)
1631 struct session_info
*session
= NULL
;
1632 struct cds_lfht_node
*node
;
1633 struct cds_lfht_iter iter
;
1634 struct lttng_session_trigger_list
*trigger_list
;
1637 cds_lfht_lookup(state
->sessions_ht
,
1638 hash_key_str(name
, lttng_ht_seed
),
1642 node
= cds_lfht_iter_get_node(&iter
);
1644 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1646 session
= caa_container_of(node
, struct session_info
,
1648 assert(session
->uid
== uid
);
1649 assert(session
->gid
== gid
);
1650 session_info_get(session
);
1654 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1655 if (!trigger_list
) {
1659 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1660 state
->sessions_ht
);
1662 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1664 lttng_session_trigger_list_destroy(trigger_list
);
1667 trigger_list
= NULL
;
1669 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1670 &session
->sessions_ht_node
);
1676 session_info_put(session
);
1681 int handle_notification_thread_command_add_channel(
1682 struct notification_thread_state
*state
,
1683 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1684 const char *channel_name
, enum lttng_domain_type channel_domain
,
1685 uint64_t channel_key_int
, uint64_t channel_capacity
,
1686 enum lttng_error_code
*cmd_result
)
1688 struct cds_list_head trigger_list
;
1689 struct channel_info
*new_channel_info
= NULL
;
1690 struct channel_key channel_key
= {
1691 .key
= channel_key_int
,
1692 .domain
= channel_domain
,
1694 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1695 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1696 int trigger_count
= 0;
1697 struct cds_lfht_iter iter
;
1698 struct session_info
*session_info
= NULL
;
1700 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1701 channel_name
, session_name
, channel_key_int
,
1702 lttng_domain_type_str(channel_domain
));
1704 CDS_INIT_LIST_HEAD(&trigger_list
);
1706 session_info
= find_or_create_session_info(state
, session_name
,
1707 session_uid
, session_gid
);
1708 if (!session_info
) {
1709 /* Allocation error or an internal error occurred. */
1713 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1714 channel_capacity
, session_info
);
1715 if (!new_channel_info
) {
1720 /* Build a list of all triggers applying to the new channel. */
1721 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1723 struct lttng_trigger_list_element
*new_element
;
1725 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1726 new_channel_info
)) {
1730 new_element
= zmalloc(sizeof(*new_element
));
1735 CDS_INIT_LIST_HEAD(&new_element
->node
);
1736 new_element
->trigger
= trigger_ht_element
->trigger
;
1737 cds_list_add(&new_element
->node
, &trigger_list
);
1742 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1744 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1745 if (!channel_trigger_list
) {
1748 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1749 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1750 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1751 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1754 /* Add channel to the channel_ht which owns the channel_infos. */
1755 cds_lfht_add(state
->channels_ht
,
1756 hash_channel_key(&new_channel_info
->key
),
1757 &new_channel_info
->channels_ht_node
);
1759 * Add the list of triggers associated with this channel to the
1760 * channel_triggers_ht.
1762 cds_lfht_add(state
->channel_triggers_ht
,
1763 hash_channel_key(&new_channel_info
->key
),
1764 &channel_trigger_list
->channel_triggers_ht_node
);
1766 session_info_put(session_info
);
1767 *cmd_result
= LTTNG_OK
;
1770 channel_info_destroy(new_channel_info
);
1771 session_info_put(session_info
);
1776 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1778 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1783 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1785 free(caa_container_of(node
, struct channel_state_sample
,
1790 int handle_notification_thread_command_remove_channel(
1791 struct notification_thread_state
*state
,
1792 uint64_t channel_key
, enum lttng_domain_type domain
,
1793 enum lttng_error_code
*cmd_result
)
1795 struct cds_lfht_node
*node
;
1796 struct cds_lfht_iter iter
;
1797 struct lttng_channel_trigger_list
*trigger_list
;
1798 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1799 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1800 struct channel_info
*channel_info
;
1802 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1803 channel_key
, lttng_domain_type_str(domain
));
1807 cds_lfht_lookup(state
->channel_triggers_ht
,
1808 hash_channel_key(&key
),
1809 match_channel_trigger_list
,
1812 node
= cds_lfht_iter_get_node(&iter
);
1814 * There is a severe internal error if we are being asked to remove a
1815 * channel that doesn't exist.
1818 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1822 /* Free the list of triggers associated with this channel. */
1823 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1824 channel_triggers_ht_node
);
1825 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1826 &trigger_list
->list
, node
) {
1827 cds_list_del(&trigger_list_element
->node
);
1828 free(trigger_list_element
);
1830 cds_lfht_del(state
->channel_triggers_ht
, node
);
1831 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1833 /* Free sampled channel state. */
1834 cds_lfht_lookup(state
->channel_state_ht
,
1835 hash_channel_key(&key
),
1836 match_channel_state_sample
,
1839 node
= cds_lfht_iter_get_node(&iter
);
1841 * This is expected to be NULL if the channel is destroyed before we
1842 * received a sample.
1845 struct channel_state_sample
*sample
= caa_container_of(node
,
1846 struct channel_state_sample
,
1847 channel_state_ht_node
);
1849 cds_lfht_del(state
->channel_state_ht
, node
);
1850 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1853 /* Remove the channel from the channels_ht and free it. */
1854 cds_lfht_lookup(state
->channels_ht
,
1855 hash_channel_key(&key
),
1859 node
= cds_lfht_iter_get_node(&iter
);
1861 channel_info
= caa_container_of(node
, struct channel_info
,
1863 cds_lfht_del(state
->channels_ht
, node
);
1864 channel_info_destroy(channel_info
);
1867 *cmd_result
= LTTNG_OK
;
1872 int handle_notification_thread_command_session_rotation(
1873 struct notification_thread_state
*state
,
1874 enum notification_thread_command_type cmd_type
,
1875 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1876 uint64_t trace_archive_chunk_id
,
1877 struct lttng_trace_archive_location
*location
,
1878 enum lttng_error_code
*_cmd_result
)
1881 enum lttng_error_code cmd_result
= LTTNG_OK
;
1882 struct lttng_session_trigger_list
*trigger_list
;
1883 struct lttng_trigger_list_element
*trigger_list_element
;
1884 struct session_info
*session_info
;
1885 const struct lttng_credentials session_creds
= {
1886 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1887 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1892 session_info
= find_or_create_session_info(state
, session_name
,
1893 session_uid
, session_gid
);
1894 if (!session_info
) {
1895 /* Allocation error or an internal error occurred. */
1897 cmd_result
= LTTNG_ERR_NOMEM
;
1901 session_info
->rotation
.ongoing
=
1902 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1903 session_info
->rotation
.id
= trace_archive_chunk_id
;
1904 trigger_list
= get_session_trigger_list(state
, session_name
);
1905 if (!trigger_list
) {
1906 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1911 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1913 const struct lttng_condition
*condition
;
1914 struct lttng_trigger
*trigger
;
1915 struct notification_client_list
*client_list
;
1916 struct lttng_evaluation
*evaluation
= NULL
;
1917 enum lttng_condition_type condition_type
;
1918 enum action_executor_status executor_status
;
1920 trigger
= trigger_list_element
->trigger
;
1921 condition
= lttng_trigger_get_const_condition(trigger
);
1923 condition_type
= lttng_condition_get_type(condition
);
1925 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1926 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1928 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1929 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1933 client_list
= get_client_list_from_condition(state
, condition
);
1934 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1935 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1936 trace_archive_chunk_id
);
1938 evaluation
= lttng_evaluation_session_rotation_completed_create(
1939 trace_archive_chunk_id
, location
);
1943 /* Internal error */
1945 cmd_result
= LTTNG_ERR_UNK
;
1950 * Ownership of `evaluation` transferred to the action executor
1951 * no matter the result.
1953 executor_status
= action_executor_enqueue(state
->executor
,
1954 trigger
, evaluation
, &session_creds
,
1957 switch (executor_status
) {
1958 case ACTION_EXECUTOR_STATUS_OK
:
1960 case ACTION_EXECUTOR_STATUS_ERROR
:
1961 case ACTION_EXECUTOR_STATUS_INVALID
:
1963 * TODO Add trigger identification (name/id) when
1964 * it is added to the API.
1966 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1969 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1971 * TODO Add trigger identification (name/id) when
1972 * it is added to the API.
1974 * Not a fatal error.
1976 WARN("No space left when enqueuing action associated with session rotation trigger");
1984 notification_client_list_put(client_list
);
1985 if (caa_unlikely(ret
)) {
1990 session_info_put(session_info
);
1991 *_cmd_result
= cmd_result
;
1997 int handle_notification_thread_command_add_tracer_event_source(
1998 struct notification_thread_state
*state
,
1999 int tracer_event_source_fd
,
2000 enum lttng_domain_type domain_type
,
2001 enum lttng_error_code
*_cmd_result
)
2004 enum lttng_error_code cmd_result
= LTTNG_OK
;
2005 struct notification_event_tracer_event_source_element
*element
= NULL
;
2007 element
= zmalloc(sizeof(*element
));
2009 cmd_result
= LTTNG_ERR_NOMEM
;
2014 element
->fd
= tracer_event_source_fd
;
2015 element
->domain
= domain_type
;
2017 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2019 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2020 tracer_event_source_fd
,
2021 lttng_domain_type_str(domain_type
));
2023 /* Adding the read side pipe to the event poll. */
2024 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
2026 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2027 tracer_event_source_fd
,
2028 lttng_domain_type_str(element
->domain
));
2029 cds_list_del(&element
->node
);
2034 element
->is_fd_in_poll_set
= true;
2037 *_cmd_result
= cmd_result
;
2042 int drain_event_notifier_notification_pipe(
2043 struct notification_thread_state
*state
,
2044 int pipe
, enum lttng_domain_type domain
)
2046 struct lttng_poll_event events
= {0};
2049 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2051 ERR("[notification-thread] Error creating lttng_poll_event");
2055 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2057 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2064 * Continue to consume notifications as long as there are new
2065 * ones coming in. The tracer has been asked to stop producing
2068 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2069 * monitored (on Linux, at least) and will be returned when
2070 * the pipe is closed but empty.
2072 ret
= lttng_poll_wait_interruptible(&events
, 0);
2073 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2074 /* No more notification to be read on this pipe. */
2077 } else if (ret
< 0) {
2078 PERROR("Failed on lttng_poll_wait_interruptible() call");
2083 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2085 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2090 lttng_poll_clean(&events
);
2095 int handle_notification_thread_command_remove_tracer_event_source(
2096 struct notification_thread_state
*state
,
2097 int tracer_event_source_fd
,
2098 enum lttng_error_code
*_cmd_result
)
2102 enum lttng_error_code cmd_result
= LTTNG_OK
;
2103 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
2105 cds_list_for_each_entry_safe(source_element
, tmp
,
2106 &state
->tracer_event_sources_list
, node
) {
2107 if (source_element
->fd
!= tracer_event_source_fd
) {
2111 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2112 tracer_event_source_fd
,
2113 lttng_domain_type_str(source_element
->domain
));
2114 cds_list_del(&source_element
->node
);
2121 * This is temporarily allowed since the poll activity set is
2122 * not properly cleaned-up for the moment. This is adressed in
2125 source_element
= NULL
;
2129 if (!source_element
->is_fd_in_poll_set
) {
2130 /* Skip the poll set removal. */
2134 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2135 tracer_event_source_fd
,
2136 lttng_domain_type_str(source_element
->domain
));
2138 /* Removing the fd from the event poll set. */
2139 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2141 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2142 tracer_event_source_fd
,
2143 lttng_domain_type_str(source_element
->domain
));
2144 cmd_result
= LTTNG_ERR_FATAL
;
2148 source_element
->is_fd_in_poll_set
= false;
2150 ret
= drain_event_notifier_notification_pipe(state
, tracer_event_source_fd
,
2151 source_element
->domain
);
2153 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2154 tracer_event_source_fd
,
2155 lttng_domain_type_str(source_element
->domain
));
2156 cmd_result
= LTTNG_ERR_FATAL
;
2161 * The drain_event_notifier_notification_pipe() call might have read
2162 * data from an fd that we received in event in the latest _poll_wait()
2163 * call. Make sure the thread call poll_wait() again to ensure we have
2166 state
->restart_poll
= true;
2169 free(source_element
);
2170 *_cmd_result
= cmd_result
;
2175 int condition_on_event_update_error_count(struct lttng_trigger
*trigger
)
2178 uint64_t error_count
= 0;
2179 struct lttng_condition
*condition
;
2180 enum event_notifier_error_accounting_status status
;
2182 condition
= lttng_trigger_get_condition(trigger
);
2183 assert(lttng_condition_get_type(condition
) ==
2184 LTTNG_CONDITION_TYPE_ON_EVENT
);
2186 status
= event_notifier_error_accounting_get_count(trigger
, &error_count
);
2187 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2188 uid_t trigger_owner_uid
;
2189 const char *trigger_name
;
2190 const enum lttng_trigger_status trigger_status
=
2191 lttng_trigger_get_owner_uid(
2192 trigger
, &trigger_owner_uid
);
2194 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2195 if (lttng_trigger_get_name(trigger
, &trigger_name
) != LTTNG_TRIGGER_STATUS_OK
) {
2196 trigger_name
= "(unnamed)";
2199 ERR("Failed to get event notifier error count of trigger for update: trigger owner = %d, trigger name = '%s'",
2200 trigger_owner_uid
, trigger_name
);
2204 lttng_condition_on_event_set_error_count(condition
, error_count
);
2208 int handle_notification_thread_remove_tracer_event_source_no_result(
2209 struct notification_thread_state
*state
,
2210 int tracer_event_source_fd
)
2213 enum lttng_error_code cmd_result
;
2215 ret
= handle_notification_thread_command_remove_tracer_event_source(
2216 state
, tracer_event_source_fd
, &cmd_result
);
2221 static int handle_notification_thread_command_list_triggers(
2222 struct notification_thread_handle
*handle
,
2223 struct notification_thread_state
*state
,
2225 struct lttng_triggers
**triggers
,
2226 enum lttng_error_code
*_cmd_result
)
2229 enum lttng_error_code cmd_result
= LTTNG_OK
;
2230 struct cds_lfht_iter iter
;
2231 struct lttng_trigger_ht_element
*trigger_ht_element
;
2232 struct lttng_triggers
*local_triggers
= NULL
;
2233 const struct lttng_credentials
*creds
;
2237 local_triggers
= lttng_triggers_create();
2238 if (!local_triggers
) {
2239 /* Not a fatal error. */
2240 cmd_result
= LTTNG_ERR_NOMEM
;
2244 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2245 trigger_ht_element
, node
) {
2247 * Only return the triggers to which the client has access.
2248 * The root user has visibility over all triggers.
2250 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2251 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2255 if (lttng_trigger_needs_tracer_notifier(trigger_ht_element
->trigger
)) {
2256 ret
= condition_on_event_update_error_count(
2257 trigger_ht_element
->trigger
);
2261 ret
= lttng_triggers_add(local_triggers
,
2262 trigger_ht_element
->trigger
);
2264 /* Not a fatal error. */
2266 cmd_result
= LTTNG_ERR_NOMEM
;
2271 /* Transferring ownership to the caller. */
2272 *triggers
= local_triggers
;
2273 local_triggers
= NULL
;
2277 lttng_triggers_destroy(local_triggers
);
2278 *_cmd_result
= cmd_result
;
2283 bool condition_is_supported(struct lttng_condition
*condition
)
2287 switch (lttng_condition_get_type(condition
)) {
2288 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2289 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2292 enum lttng_domain_type domain
;
2294 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2298 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2299 is_supported
= true;
2304 * Older kernel tracers don't expose the API to monitor their
2305 * buffers. Therefore, we reject triggers that require that
2306 * mechanism to be available to be evaluated.
2308 * Assume unsupported on error.
2310 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2313 case LTTNG_CONDITION_TYPE_ON_EVENT
:
2315 const struct lttng_event_rule
*event_rule
;
2316 enum lttng_domain_type domain
;
2317 const enum lttng_condition_status status
=
2318 lttng_condition_on_event_get_rule(
2319 condition
, &event_rule
);
2321 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2323 domain
= lttng_event_rule_get_domain_type(event_rule
);
2324 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2325 is_supported
= true;
2330 * Older kernel tracers can't emit notification. Therefore, we
2331 * reject triggers that require that mechanism to be available
2334 * Assume unsupported on error.
2336 is_supported
= kernel_supports_event_notifiers() == 1;
2340 is_supported
= true;
2343 return is_supported
;
2346 /* Must be called with RCU read lock held. */
2348 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2349 struct notification_thread_state
*state
)
2352 const struct lttng_condition
*condition
;
2353 const char *session_name
;
2354 struct lttng_session_trigger_list
*trigger_list
;
2356 condition
= lttng_trigger_get_const_condition(trigger
);
2357 switch (lttng_condition_get_type(condition
)) {
2358 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2359 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2361 enum lttng_condition_status status
;
2363 status
= lttng_condition_session_rotation_get_session_name(
2364 condition
, &session_name
);
2365 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2366 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2377 trigger_list
= get_session_trigger_list(state
, session_name
);
2378 if (!trigger_list
) {
2379 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2385 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2387 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2392 /* Must be called with RCU read lock held. */
2394 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2395 struct notification_thread_state
*state
)
2398 struct cds_lfht_node
*node
;
2399 struct cds_lfht_iter iter
;
2400 struct channel_info
*channel
;
2402 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2404 struct lttng_trigger_list_element
*trigger_list_element
;
2405 struct lttng_channel_trigger_list
*trigger_list
;
2406 struct cds_lfht_iter lookup_iter
;
2408 if (!trigger_applies_to_channel(trigger
, channel
)) {
2412 cds_lfht_lookup(state
->channel_triggers_ht
,
2413 hash_channel_key(&channel
->key
),
2414 match_channel_trigger_list
,
2417 node
= cds_lfht_iter_get_node(&lookup_iter
);
2419 trigger_list
= caa_container_of(node
,
2420 struct lttng_channel_trigger_list
,
2421 channel_triggers_ht_node
);
2423 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2424 if (!trigger_list_element
) {
2428 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2429 trigger_list_element
->trigger
= trigger
;
2430 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2431 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2439 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2441 bool is_notify
= false;
2442 unsigned int i
, count
;
2443 enum lttng_action_status action_status
;
2444 const struct lttng_action
*action
=
2445 lttng_trigger_get_const_action(trigger
);
2446 enum lttng_action_type action_type
;
2449 action_type
= lttng_action_get_type(action
);
2450 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2453 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2457 action_status
= lttng_action_group_get_count(action
, &count
);
2458 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2460 for (i
= 0; i
< count
; i
++) {
2461 const struct lttng_action
*inner_action
=
2462 lttng_action_group_get_at_index(
2465 action_type
= lttng_action_get_type(inner_action
);
2466 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2476 static bool trigger_name_taken(struct notification_thread_state
*state
,
2477 const struct lttng_trigger
*trigger
)
2479 struct cds_lfht_iter iter
;
2482 * No duplicata is allowed in the triggers_by_name_uid_ht.
2483 * The match is done against the trigger name and uid.
2485 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2486 hash_trigger_by_name_uid(trigger
),
2487 match_trigger_by_name_uid
,
2490 return !!cds_lfht_iter_get_node(&iter
);
2494 enum lttng_error_code
generate_trigger_name(
2495 struct notification_thread_state
*state
,
2496 struct lttng_trigger
*trigger
, const char **name
)
2498 enum lttng_error_code ret_code
= LTTNG_OK
;
2500 enum lttng_trigger_status status
;
2503 const int ret
= lttng_trigger_generate_name(trigger
,
2504 state
->trigger_id
.name_offset
++);
2506 /* The only reason this can fail right now. */
2507 ret_code
= LTTNG_ERR_NOMEM
;
2511 status
= lttng_trigger_get_name(trigger
, name
);
2512 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2514 taken
= trigger_name_taken(state
, trigger
);
2515 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2521 void notif_thread_state_remove_trigger_ht_elem(
2522 struct notification_thread_state
*state
,
2523 struct lttng_trigger_ht_element
*trigger_ht_element
)
2526 assert(trigger_ht_element
);
2528 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2529 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2533 enum lttng_error_code
setup_tracer_notifier(
2534 struct notification_thread_state
*state
,
2535 struct lttng_trigger
*trigger
)
2537 enum lttng_error_code ret
;
2538 enum event_notifier_error_accounting_status error_accounting_status
;
2539 struct cds_lfht_node
*node
;
2540 uint64_t error_counter_index
= 0;
2541 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2542 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2544 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2545 if (!trigger_tokens_ht_element
) {
2546 ret
= LTTNG_ERR_NOMEM
;
2550 /* Add trigger token to the trigger_tokens_ht. */
2551 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2552 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2553 trigger_tokens_ht_element
->trigger
= trigger
;
2555 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2556 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2557 match_trigger_token
,
2558 &trigger_tokens_ht_element
->token
,
2559 &trigger_tokens_ht_element
->node
);
2560 if (node
!= &trigger_tokens_ht_element
->node
) {
2561 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2562 goto error_free_ht_element
;
2565 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2566 trigger
, &error_counter_index
);
2567 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2568 if (error_accounting_status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2569 DBG("Trigger group error accounting counter full.");
2570 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2572 ERR("Error registering trigger for error accounting");
2573 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2576 goto error_remove_ht_element
;
2579 lttng_condition_on_event_set_error_counter_index(
2580 condition
, error_counter_index
);
2585 error_remove_ht_element
:
2586 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2587 error_free_ht_element
:
2588 free(trigger_tokens_ht_element
);
2594 * FIXME A client's credentials are not checked when registering a trigger.
2596 * The effects of this are benign since:
2597 * - The client will succeed in registering the trigger, as it is valid,
2598 * - The trigger will, internally, be bound to the channel/session,
2599 * - The notifications will not be sent since the client's credentials
2600 * are checked against the channel at that moment.
2602 * If this function returns a non-zero value, it means something is
2603 * fundamentally broken and the whole subsystem/thread will be torn down.
2605 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2609 int handle_notification_thread_command_register_trigger(
2610 struct notification_thread_state
*state
,
2611 struct lttng_trigger
*trigger
,
2612 enum lttng_error_code
*cmd_result
)
2615 struct lttng_condition
*condition
;
2616 struct notification_client_list
*client_list
= NULL
;
2617 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2618 struct cds_lfht_node
*node
;
2619 const char* trigger_name
;
2620 bool free_trigger
= true;
2621 struct lttng_evaluation
*evaluation
= NULL
;
2622 struct lttng_credentials object_creds
;
2625 enum action_executor_status executor_status
;
2626 const uint64_t trigger_tracer_token
=
2627 state
->trigger_id
.next_tracer_token
++;
2631 /* Set the trigger's tracer token. */
2632 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2634 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2635 LTTNG_TRIGGER_STATUS_UNSET
) {
2636 const enum lttng_error_code ret_code
= generate_trigger_name(
2637 state
, trigger
, &trigger_name
);
2639 if (ret_code
!= LTTNG_OK
) {
2642 *cmd_result
= ret_code
;
2645 } else if (trigger_name_taken(state
, trigger
)) {
2646 /* Not a fatal error. */
2647 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2652 condition
= lttng_trigger_get_condition(trigger
);
2655 /* Some conditions require tracers to implement a minimal ABI version. */
2656 if (!condition_is_supported(condition
)) {
2657 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2661 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2662 if (!trigger_ht_element
) {
2667 /* Add trigger to the trigger_ht. */
2668 cds_lfht_node_init(&trigger_ht_element
->node
);
2669 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2670 trigger_ht_element
->trigger
= trigger
;
2672 node
= cds_lfht_add_unique(state
->triggers_ht
,
2673 lttng_condition_hash(condition
),
2676 &trigger_ht_element
->node
);
2677 if (node
!= &trigger_ht_element
->node
) {
2678 /* Not a fatal error, simply report it to the client. */
2679 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2680 goto error_free_ht_element
;
2683 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2684 hash_trigger_by_name_uid(trigger
),
2685 match_trigger_by_name_uid
,
2687 &trigger_ht_element
->node_by_name_uid
);
2688 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2689 /* Not a fatal error, simply report it to the client. */
2690 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2691 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2692 goto error_free_ht_element
;
2696 * Some triggers might need a tracer notifier depending on its
2697 * condition and actions.
2699 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2700 enum lttng_error_code error_code
;
2702 error_code
= setup_tracer_notifier(state
, trigger
);
2703 if (error_code
!= LTTNG_OK
) {
2704 notif_thread_state_remove_trigger_ht_elem(state
,
2705 trigger_ht_element
);
2706 if (error_code
== LTTNG_ERR_NOMEM
) {
2709 *cmd_result
= error_code
;
2713 goto error_free_ht_element
;
2718 * The rest only applies to triggers that have a "notify" action.
2719 * It is not skipped as this is the only action type currently
2722 if (is_trigger_action_notify(trigger
)) {
2724 * Find or create the client list of this condition. It may
2725 * already be present if another trigger is already registered
2726 * with the same condition.
2728 client_list
= get_client_list_from_condition(state
, condition
);
2731 * No client list for this condition yet. We create new
2732 * one and build it up.
2734 client_list
= notification_client_list_create(state
, condition
);
2736 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2737 goto error_free_ht_element
;
2741 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2743 pthread_mutex_lock(&client_list
->lock
);
2744 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2745 pthread_mutex_unlock(&client_list
->lock
);
2749 * Ownership of the trigger and of its wrapper was transfered to
2750 * the triggers_ht. Same for token ht element if necessary.
2752 trigger_ht_element
= NULL
;
2753 free_trigger
= false;
2755 switch (get_condition_binding_object(condition
)) {
2756 case LTTNG_OBJECT_TYPE_SESSION
:
2757 /* Add the trigger to the list if it matches a known session. */
2758 ret
= bind_trigger_to_matching_session(trigger
, state
);
2760 goto error_free_ht_element
;
2763 case LTTNG_OBJECT_TYPE_CHANNEL
:
2765 * Add the trigger to list of triggers bound to the channels
2768 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2770 goto error_free_ht_element
;
2773 case LTTNG_OBJECT_TYPE_NONE
:
2776 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2778 goto error_free_ht_element
;
2782 * The new trigger's condition must be evaluated against the current
2785 * In the case of `notify` action, nothing preventing clients from
2786 * subscribing to a condition before the corresponding trigger is
2787 * registered, we have to evaluate this new condition right away.
2789 * At some point, we were waiting for the next "evaluation" (e.g. on
2790 * reception of a channel sample) to evaluate this new condition, but
2793 * The reason it was broken is that waiting for the next sample
2794 * does not allow us to properly handle transitions for edge-triggered
2797 * Consider this example: when we handle a new channel sample, we
2798 * evaluate each conditions twice: once with the previous state, and
2799 * again with the newest state. We then use those two results to
2800 * determine whether a state change happened: a condition was false and
2801 * became true. If a state change happened, we have to notify clients.
2803 * Now, if a client subscribes to a given notification and registers
2804 * a trigger *after* that subscription, we have to make sure the
2805 * condition is evaluated at this point while considering only the
2806 * current state. Otherwise, the next evaluation cycle may only see
2807 * that the evaluations remain the same (true for samples n-1 and n) and
2808 * the client will never know that the condition has been met.
2810 switch (get_condition_binding_object(condition
)) {
2811 case LTTNG_OBJECT_TYPE_SESSION
:
2812 ret
= evaluate_session_condition_for_client(condition
, state
,
2813 &evaluation
, &object_uid
,
2815 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2816 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2818 case LTTNG_OBJECT_TYPE_CHANNEL
:
2819 ret
= evaluate_channel_condition_for_client(condition
, state
,
2820 &evaluation
, &object_uid
,
2822 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2823 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2825 case LTTNG_OBJECT_TYPE_NONE
:
2828 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2836 goto error_free_ht_element
;
2839 DBG("Newly registered trigger's condition evaluated to %s",
2840 evaluation
? "true" : "false");
2842 /* Evaluation yielded nothing. Normal exit. */
2848 * Ownership of `evaluation` transferred to the action executor
2849 * no matter the result.
2851 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2852 evaluation
, &object_creds
, client_list
);
2854 switch (executor_status
) {
2855 case ACTION_EXECUTOR_STATUS_OK
:
2857 case ACTION_EXECUTOR_STATUS_ERROR
:
2858 case ACTION_EXECUTOR_STATUS_INVALID
:
2860 * TODO Add trigger identification (name/id) when
2861 * it is added to the API.
2863 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2865 goto error_free_ht_element
;
2866 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2868 * TODO Add trigger identification (name/id) when
2869 * it is added to the API.
2871 * Not a fatal error.
2873 WARN("No space left when enqueuing action associated to newly registered trigger");
2881 *cmd_result
= LTTNG_OK
;
2882 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2883 trigger_name
, trigger_tracer_token
);
2886 error_free_ht_element
:
2887 if (trigger_ht_element
) {
2888 /* Delayed removal due to RCU constraint on delete. */
2889 call_rcu(&trigger_ht_element
->rcu_node
,
2890 free_lttng_trigger_ht_element_rcu
);
2894 lttng_trigger_destroy(trigger
);
2902 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2904 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2909 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2911 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2916 void teardown_tracer_notifier(struct notification_thread_state
*state
,
2917 const struct lttng_trigger
*trigger
)
2919 struct cds_lfht_iter iter
;
2920 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2922 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
2923 trigger_tokens_ht_element
, node
) {
2925 if (!lttng_trigger_is_equal(trigger
,
2926 trigger_tokens_ht_element
->trigger
)) {
2930 event_notifier_error_accounting_unregister_event_notifier(
2931 trigger_tokens_ht_element
->trigger
);
2933 /* TODO talk to all app and remove it */
2934 DBG("[notification-thread] Removed trigger from tokens_ht");
2935 cds_lfht_del(state
->trigger_tokens_ht
,
2936 &trigger_tokens_ht_element
->node
);
2938 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2939 free_notification_trigger_tokens_ht_element_rcu
);
2946 int handle_notification_thread_command_unregister_trigger(
2947 struct notification_thread_state
*state
,
2948 const struct lttng_trigger
*trigger
,
2949 enum lttng_error_code
*_cmd_reply
)
2951 struct cds_lfht_iter iter
;
2952 struct cds_lfht_node
*triggers_ht_node
;
2953 struct lttng_channel_trigger_list
*trigger_list
;
2954 struct notification_client_list
*client_list
;
2955 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2956 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
2958 enum lttng_error_code cmd_reply
;
2962 cds_lfht_lookup(state
->triggers_ht
,
2963 lttng_condition_hash(condition
),
2967 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2968 if (!triggers_ht_node
) {
2969 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2972 cmd_reply
= LTTNG_OK
;
2975 /* Remove trigger from channel_triggers_ht. */
2976 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2977 channel_triggers_ht_node
) {
2978 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2980 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2981 &trigger_list
->list
, node
) {
2982 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2986 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2987 cds_list_del(&trigger_element
->node
);
2988 /* A trigger can only appear once per channel */
2993 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2994 teardown_tracer_notifier(state
, trigger
);
2997 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2998 struct lttng_trigger_ht_element
, node
);
3000 if (is_trigger_action_notify(trigger
)) {
3002 * Remove and release the client list from
3003 * notification_trigger_clients_ht.
3005 client_list
= get_client_list_from_condition(state
, condition
);
3006 assert(client_list
);
3008 pthread_mutex_lock(&client_list
->lock
);
3009 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3010 pthread_mutex_unlock(&client_list
->lock
);
3012 /* Put new reference and the hashtable's reference. */
3013 notification_client_list_put(client_list
);
3014 notification_client_list_put(client_list
);
3018 /* Remove trigger from triggers_ht. */
3019 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3021 /* Release the ownership of the trigger. */
3022 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3023 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3027 *_cmd_reply
= cmd_reply
;
3032 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3033 int handle_notification_thread_command(
3034 struct notification_thread_handle
*handle
,
3035 struct notification_thread_state
*state
)
3039 struct notification_thread_command
*cmd
;
3041 /* Read the event pipe to put it back into a quiescent state. */
3042 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
3044 if (ret
!= sizeof(counter
)) {
3048 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3049 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3050 struct notification_thread_command
, cmd_list_node
);
3051 cds_list_del(&cmd
->cmd_list_node
);
3052 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3054 DBG("[notification-thread] Received `%s` command",
3055 notification_command_type_str(cmd
->type
));
3056 switch (cmd
->type
) {
3057 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3058 ret
= handle_notification_thread_command_register_trigger(state
,
3059 cmd
->parameters
.register_trigger
.trigger
,
3062 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3063 ret
= handle_notification_thread_command_unregister_trigger(
3065 cmd
->parameters
.unregister_trigger
.trigger
,
3068 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3069 ret
= handle_notification_thread_command_add_channel(
3071 cmd
->parameters
.add_channel
.session
.name
,
3072 cmd
->parameters
.add_channel
.session
.uid
,
3073 cmd
->parameters
.add_channel
.session
.gid
,
3074 cmd
->parameters
.add_channel
.channel
.name
,
3075 cmd
->parameters
.add_channel
.channel
.domain
,
3076 cmd
->parameters
.add_channel
.channel
.key
,
3077 cmd
->parameters
.add_channel
.channel
.capacity
,
3080 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3081 ret
= handle_notification_thread_command_remove_channel(
3082 state
, cmd
->parameters
.remove_channel
.key
,
3083 cmd
->parameters
.remove_channel
.domain
,
3086 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3087 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3088 ret
= handle_notification_thread_command_session_rotation(
3091 cmd
->parameters
.session_rotation
.session_name
,
3092 cmd
->parameters
.session_rotation
.uid
,
3093 cmd
->parameters
.session_rotation
.gid
,
3094 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3095 cmd
->parameters
.session_rotation
.location
,
3098 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3099 ret
= handle_notification_thread_command_add_tracer_event_source(
3101 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3102 cmd
->parameters
.tracer_event_source
.domain
,
3105 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3106 ret
= handle_notification_thread_command_remove_tracer_event_source(
3108 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3111 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3113 struct lttng_triggers
*triggers
= NULL
;
3115 ret
= handle_notification_thread_command_list_triggers(
3118 cmd
->parameters
.list_triggers
.uid
,
3121 cmd
->reply
.list_triggers
.triggers
= triggers
;
3125 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3126 cmd
->reply_code
= LTTNG_OK
;
3129 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3131 const enum client_transmission_status client_status
=
3132 cmd
->parameters
.client_communication_update
3134 const notification_client_id client_id
=
3135 cmd
->parameters
.client_communication_update
.id
;
3136 struct notification_client
*client
;
3139 client
= get_client_from_id(client_id
, state
);
3143 * Client error was probably already picked-up by the
3144 * notification thread or it has disconnected
3145 * gracefully while this command was queued.
3147 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3151 ret
= client_handle_transmission_status(
3152 client
, client_status
, state
);
3158 ERR("[notification-thread] Unknown internal command received");
3166 if (cmd
->is_async
) {
3170 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3174 /* Wake-up and return a fatal error to the calling thread. */
3175 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3176 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3178 /* Indicate a fatal error to the caller. */
3183 int socket_set_non_blocking(int socket
)
3187 /* Set the pipe as non-blocking. */
3188 ret
= fcntl(socket
, F_GETFL
, 0);
3190 PERROR("fcntl get socket flags");
3195 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3197 PERROR("fcntl set O_NONBLOCK socket flag");
3200 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3206 int client_reset_inbound_state(struct notification_client
*client
)
3211 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3213 client
->communication
.inbound
.bytes_to_receive
=
3214 sizeof(struct lttng_notification_channel_message
);
3215 client
->communication
.inbound
.msg_type
=
3216 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3217 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3218 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3219 ret
= lttng_dynamic_buffer_set_size(
3220 &client
->communication
.inbound
.payload
.buffer
,
3221 client
->communication
.inbound
.bytes_to_receive
);
3226 int handle_notification_thread_client_connect(
3227 struct notification_thread_state
*state
)
3230 struct notification_client
*client
;
3232 DBG("[notification-thread] Handling new notification channel client connection");
3234 client
= zmalloc(sizeof(*client
));
3241 pthread_mutex_init(&client
->lock
, NULL
);
3242 client
->id
= state
->next_notification_client_id
++;
3243 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3244 lttng_payload_init(&client
->communication
.inbound
.payload
);
3245 lttng_payload_init(&client
->communication
.outbound
.payload
);
3246 client
->communication
.inbound
.expect_creds
= true;
3248 ret
= client_reset_inbound_state(client
);
3250 ERR("[notification-thread] Failed to reset client communication's inbound state");
3255 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3257 ERR("[notification-thread] Failed to accept new notification channel client connection");
3262 client
->socket
= ret
;
3264 ret
= socket_set_non_blocking(client
->socket
);
3266 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3270 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3272 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3277 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3278 LPOLLIN
| LPOLLERR
|
3279 LPOLLHUP
| LPOLLRDHUP
);
3281 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3285 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3289 cds_lfht_add(state
->client_socket_ht
,
3290 hash_client_socket(client
->socket
),
3291 &client
->client_socket_ht_node
);
3292 cds_lfht_add(state
->client_id_ht
,
3293 hash_client_id(client
->id
),
3294 &client
->client_id_ht_node
);
3300 notification_client_destroy(client
, state
);
3305 * RCU read-lock must be held by the caller.
3306 * Client lock must _not_ be held by the caller.
3309 int notification_thread_client_disconnect(
3310 struct notification_client
*client
,
3311 struct notification_thread_state
*state
)
3314 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3316 /* Acquire the client lock to disable its communication atomically. */
3317 pthread_mutex_lock(&client
->lock
);
3318 client
->communication
.active
= false;
3319 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3320 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3321 pthread_mutex_unlock(&client
->lock
);
3323 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3325 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3329 /* Release all conditions to which the client was subscribed. */
3330 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3331 &client
->condition_list
, node
) {
3332 (void) notification_thread_client_unsubscribe(client
,
3333 condition_list_element
->condition
, state
, NULL
);
3337 * Client no longer accessible to other threads (through the
3340 notification_client_destroy(client
, state
);
3344 int handle_notification_thread_client_disconnect(
3345 int client_socket
, struct notification_thread_state
*state
)
3348 struct notification_client
*client
;
3351 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3353 client
= get_client_from_socket(client_socket
, state
);
3355 /* Internal state corruption, fatal error. */
3356 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3362 ret
= notification_thread_client_disconnect(client
, state
);
3368 int handle_notification_thread_client_disconnect_all(
3369 struct notification_thread_state
*state
)
3371 struct cds_lfht_iter iter
;
3372 struct notification_client
*client
;
3373 bool error_encoutered
= false;
3376 DBG("[notification-thread] Closing all client connections");
3377 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3378 client_socket_ht_node
) {
3381 ret
= notification_thread_client_disconnect(
3384 error_encoutered
= true;
3388 return error_encoutered
? 1 : 0;
3391 int handle_notification_thread_trigger_unregister_all(
3392 struct notification_thread_state
*state
)
3394 bool error_occurred
= false;
3395 struct cds_lfht_iter iter
;
3396 struct lttng_trigger_ht_element
*trigger_ht_element
;
3399 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3401 int ret
= handle_notification_thread_command_unregister_trigger(
3402 state
, trigger_ht_element
->trigger
, NULL
);
3404 error_occurred
= true;
3408 return error_occurred
? -1 : 0;
3412 int client_handle_transmission_status(
3413 struct notification_client
*client
,
3414 enum client_transmission_status transmission_status
,
3415 struct notification_thread_state
*state
)
3419 switch (transmission_status
) {
3420 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3421 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3422 CLIENT_POLL_MASK_IN
);
3428 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3430 * We want to be notified whenever there is buffer space
3431 * available to send the rest of the payload.
3433 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3434 CLIENT_POLL_MASK_IN_OUT
);
3439 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3440 ret
= notification_thread_client_disconnect(client
, state
);
3445 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3455 /* Client lock must be acquired by caller. */
3457 enum client_transmission_status
client_flush_outgoing_queue(
3458 struct notification_client
*client
)
3461 size_t to_send_count
;
3462 enum client_transmission_status status
;
3463 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3464 &client
->communication
.outbound
.payload
, 0, -1);
3465 const int fds_to_send_count
=
3466 lttng_payload_view_get_fd_handle_count(&pv
);
3468 ASSERT_LOCKED(client
->lock
);
3470 if (!client
->communication
.active
) {
3471 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3475 if (pv
.buffer
.size
== 0) {
3477 * If both data and fds are equal to zero, we are in an invalid
3480 assert(fds_to_send_count
!= 0);
3485 to_send_count
= pv
.buffer
.size
;
3486 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3489 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3492 if ((ret
>= 0 && ret
< to_send_count
)) {
3493 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3495 to_send_count
-= max(ret
, 0);
3497 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3499 pv
.buffer
.size
- to_send_count
,
3501 ret
= lttng_dynamic_buffer_set_size(
3502 &client
->communication
.outbound
.payload
.buffer
,
3508 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3510 } else if (ret
< 0) {
3511 /* Generic error, disable the client's communication. */
3512 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3514 client
->communication
.active
= false;
3515 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3519 * No error and flushed the queue completely.
3521 * The payload buffer size is used later to
3522 * check if there is notifications queued. So albeit that the
3523 * direct caller knows that the transmission is complete, we
3524 * need to set the buffer size to zero.
3526 ret
= lttng_dynamic_buffer_set_size(
3527 &client
->communication
.outbound
.payload
.buffer
, 0);
3534 /* No fds to send, transmission is complete. */
3535 if (fds_to_send_count
== 0) {
3536 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3540 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3541 client
->socket
, &pv
);
3543 /* Generic error, disable the client's communication. */
3544 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3546 client
->communication
.active
= false;
3547 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3549 } else if (ret
== 0) {
3550 /* Nothing could be sent. */
3551 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3553 /* Fd passing is an all or nothing kind of thing. */
3554 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3556 * The payload _fd_array count is used later to
3557 * check if there is notifications queued. So although the
3558 * direct caller knows that the transmission is complete, we
3559 * need to clear the _fd_array for the queuing check.
3561 lttng_dynamic_pointer_array_clear(
3562 &client
->communication
.outbound
.payload
3567 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3568 client
->communication
.outbound
.queued_command_reply
= false;
3569 client
->communication
.outbound
.dropped_notification
= false;
3570 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3575 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3579 bool client_has_outbound_data_left(
3580 const struct notification_client
*client
)
3582 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3583 &client
->communication
.outbound
.payload
, 0, -1);
3584 const bool has_data
= pv
.buffer
.size
!= 0;
3585 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3587 return has_data
|| has_fds
;
3590 /* Client lock must _not_ be held by the caller. */
3592 int client_send_command_reply(struct notification_client
*client
,
3593 struct notification_thread_state
*state
,
3594 enum lttng_notification_channel_status status
)
3597 struct lttng_notification_channel_command_reply reply
= {
3598 .status
= (int8_t) status
,
3600 struct lttng_notification_channel_message msg
= {
3601 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3602 .size
= sizeof(reply
),
3604 char buffer
[sizeof(msg
) + sizeof(reply
)];
3605 enum client_transmission_status transmission_status
;
3607 memcpy(buffer
, &msg
, sizeof(msg
));
3608 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3609 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3611 pthread_mutex_lock(&client
->lock
);
3612 if (client
->communication
.outbound
.queued_command_reply
) {
3613 /* Protocol error. */
3617 /* Enqueue buffer to outgoing queue and flush it. */
3618 ret
= lttng_dynamic_buffer_append(
3619 &client
->communication
.outbound
.payload
.buffer
,
3620 buffer
, sizeof(buffer
));
3625 transmission_status
= client_flush_outgoing_queue(client
);
3627 if (client_has_outbound_data_left(client
)) {
3628 /* Queue could not be emptied. */
3629 client
->communication
.outbound
.queued_command_reply
= true;
3632 pthread_mutex_unlock(&client
->lock
);
3633 ret
= client_handle_transmission_status(
3634 client
, transmission_status
, state
);
3641 pthread_mutex_unlock(&client
->lock
);
3647 int client_handle_message_unknown(struct notification_client
*client
,
3648 struct notification_thread_state
*state
)
3652 * Receiving message header. The function will be called again
3653 * once the rest of the message as been received and can be
3656 const struct lttng_notification_channel_message
*msg
;
3658 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3659 msg
= (const struct lttng_notification_channel_message
*)
3660 client
->communication
.inbound
.payload
.buffer
.data
;
3662 if (msg
->size
== 0 ||
3663 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3664 ERR("[notification-thread] Invalid notification channel message: length = %u",
3670 switch (msg
->type
) {
3671 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3672 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3673 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3677 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3681 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3682 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3683 client
->communication
.inbound
.msg_type
=
3684 (enum lttng_notification_channel_message_type
) msg
->type
;
3685 ret
= lttng_dynamic_buffer_set_size(
3686 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3688 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3695 int client_handle_message_handshake(struct notification_client
*client
,
3696 struct notification_thread_state
*state
)
3699 struct lttng_notification_channel_command_handshake
*handshake_client
;
3700 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3701 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3702 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3704 const struct lttng_notification_channel_message msg_header
= {
3705 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3706 .size
= sizeof(handshake_reply
),
3708 enum lttng_notification_channel_status status
=
3709 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3710 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3712 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3713 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3714 sizeof(handshake_reply
));
3717 (struct lttng_notification_channel_command_handshake
*)
3718 client
->communication
.inbound
.payload
.buffer
3720 client
->major
= handshake_client
->major
;
3721 client
->minor
= handshake_client
->minor
;
3722 if (!client
->communication
.inbound
.creds_received
) {
3723 ERR("[notification-thread] No credentials received from client");
3728 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3729 &client
->communication
.inbound
.creds
);
3730 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3731 &client
->communication
.inbound
.creds
);
3732 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3733 client
->uid
, client
->gid
, (int) client
->major
,
3734 (int) client
->minor
);
3736 if (handshake_client
->major
!=
3737 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3738 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3741 pthread_mutex_lock(&client
->lock
);
3742 /* Outgoing queue will be flushed when the command reply is sent. */
3743 ret
= lttng_dynamic_buffer_append(
3744 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3745 sizeof(send_buffer
));
3747 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3751 client
->validated
= true;
3752 client
->communication
.active
= true;
3753 pthread_mutex_unlock(&client
->lock
);
3755 /* Set reception state to receive the next message header. */
3756 ret
= client_reset_inbound_state(client
);
3758 ERR("[notification-thread] Failed to reset client communication's inbound state");
3762 /* Flushes the outgoing queue. */
3763 ret
= client_send_command_reply(client
, state
, status
);
3765 ERR("[notification-thread] Failed to send reply to notification channel client");
3771 pthread_mutex_unlock(&client
->lock
);
3777 int client_handle_message_subscription(
3778 struct notification_client
*client
,
3779 enum lttng_notification_channel_message_type msg_type
,
3780 struct notification_thread_state
*state
)
3783 struct lttng_condition
*condition
;
3784 enum lttng_notification_channel_status status
=
3785 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3786 struct lttng_payload_view condition_view
=
3787 lttng_payload_view_from_payload(
3788 &client
->communication
.inbound
.payload
,
3790 size_t expected_condition_size
;
3793 * No need to lock client to sample the inbound state as the only
3794 * other thread accessing clients (action executor) only uses the
3797 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3798 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3799 if (ret
!= expected_condition_size
) {
3800 ERR("[notification-thread] Malformed condition received from client");
3804 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3805 ret
= notification_thread_client_subscribe(
3806 client
, condition
, state
, &status
);
3808 ret
= notification_thread_client_unsubscribe(
3809 client
, condition
, state
, &status
);
3816 /* Set reception state to receive the next message header. */
3817 ret
= client_reset_inbound_state(client
);
3819 ERR("[notification-thread] Failed to reset client communication's inbound state");
3823 ret
= client_send_command_reply(client
, state
, status
);
3825 ERR("[notification-thread] Failed to send reply to notification channel client");
3834 int client_dispatch_message(struct notification_client
*client
,
3835 struct notification_thread_state
*state
)
3839 if (client
->communication
.inbound
.msg_type
!=
3840 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3841 client
->communication
.inbound
.msg_type
!=
3842 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3843 !client
->validated
) {
3844 WARN("[notification-thread] client attempted a command before handshake");
3849 switch (client
->communication
.inbound
.msg_type
) {
3850 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3852 ret
= client_handle_message_unknown(client
, state
);
3855 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3857 ret
= client_handle_message_handshake(client
, state
);
3860 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3861 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3863 ret
= client_handle_message_subscription(client
,
3864 client
->communication
.inbound
.msg_type
, state
);
3874 /* Incoming data from client. */
3875 int handle_notification_thread_client_in(
3876 struct notification_thread_state
*state
, int socket
)
3879 struct notification_client
*client
;
3884 client
= get_client_from_socket(socket
, state
);
3886 /* Internal error, abort. */
3891 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3892 client
->communication
.inbound
.bytes_to_receive
;
3893 if (client
->communication
.inbound
.expect_creds
) {
3894 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3895 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3896 client
->communication
.inbound
.bytes_to_receive
,
3897 &client
->communication
.inbound
.creds
);
3899 client
->communication
.inbound
.expect_creds
= false;
3900 client
->communication
.inbound
.creds_received
= true;
3903 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3904 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3905 client
->communication
.inbound
.bytes_to_receive
);
3907 if (recv_ret
>= 0) {
3908 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3910 goto error_disconnect_client
;
3913 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3914 /* Message incomplete wait for more data. */
3919 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3922 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3923 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3925 client
->communication
.inbound
.fds_to_receive
,
3926 &client
->communication
.inbound
.payload
);
3929 * Fds received. non blocking fds passing is all
3932 ssize_t expected_size
;
3934 expected_size
= sizeof(int) *
3935 client
->communication
.inbound
3937 assert(ret
== expected_size
);
3938 client
->communication
.inbound
.fds_to_receive
= 0;
3939 } else if (ret
== 0) {
3940 /* Received nothing. */
3944 goto error_disconnect_client
;
3948 /* At this point the message is complete.*/
3949 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3950 client
->communication
.inbound
.fds_to_receive
== 0);
3951 ret
= client_dispatch_message(client
, state
);
3954 * Only returns an error if this client must be
3957 goto error_disconnect_client
;
3964 error_disconnect_client
:
3965 ret
= notification_thread_client_disconnect(client
, state
);
3969 /* Client ready to receive outgoing data. */
3970 int handle_notification_thread_client_out(
3971 struct notification_thread_state
*state
, int socket
)
3974 struct notification_client
*client
;
3975 enum client_transmission_status transmission_status
;
3978 client
= get_client_from_socket(socket
, state
);
3980 /* Internal error, abort. */
3985 pthread_mutex_lock(&client
->lock
);
3986 transmission_status
= client_flush_outgoing_queue(client
);
3987 pthread_mutex_unlock(&client
->lock
);
3989 ret
= client_handle_transmission_status(
3990 client
, transmission_status
, state
);
4000 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4001 const struct channel_state_sample
*sample
,
4002 uint64_t buffer_capacity
)
4004 bool result
= false;
4006 enum lttng_condition_type condition_type
;
4007 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4008 condition
, struct lttng_condition_buffer_usage
,
4011 if (use_condition
->threshold_bytes
.set
) {
4012 threshold
= use_condition
->threshold_bytes
.value
;
4015 * Threshold was expressed as a ratio.
4017 * TODO the threshold (in bytes) of conditions expressed
4018 * as a ratio of total buffer size could be cached to
4019 * forego this double-multiplication or it could be performed
4020 * as fixed-point math.
4022 * Note that caching should accommodates the case where the
4023 * condition applies to multiple channels (i.e. don't assume
4024 * that all channels matching my_chann* have the same size...)
4026 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4027 (double) buffer_capacity
);
4030 condition_type
= lttng_condition_get_type(condition
);
4031 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4032 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4033 threshold
, sample
->highest_usage
);
4036 * The low condition should only be triggered once _all_ of the
4037 * streams in a channel have gone below the "low" threshold.
4039 if (sample
->highest_usage
<= threshold
) {
4043 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4044 threshold
, sample
->highest_usage
);
4047 * For high buffer usage scenarios, we want to trigger whenever
4048 * _any_ of the streams has reached the "high" threshold.
4050 if (sample
->highest_usage
>= threshold
) {
4059 bool evaluate_session_consumed_size_condition(
4060 const struct lttng_condition
*condition
,
4061 uint64_t session_consumed_size
)
4064 const struct lttng_condition_session_consumed_size
*size_condition
=
4065 container_of(condition
,
4066 struct lttng_condition_session_consumed_size
,
4069 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4070 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4071 threshold
, session_consumed_size
);
4072 return session_consumed_size
>= threshold
;
4076 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4077 struct lttng_evaluation
**evaluation
,
4078 const struct notification_thread_state
*state
,
4079 const struct channel_state_sample
*previous_sample
,
4080 const struct channel_state_sample
*latest_sample
,
4081 uint64_t previous_session_consumed_total
,
4082 uint64_t latest_session_consumed_total
,
4083 struct channel_info
*channel_info
)
4086 enum lttng_condition_type condition_type
;
4087 const bool previous_sample_available
= !!previous_sample
;
4088 bool previous_sample_result
= false;
4089 bool latest_sample_result
;
4091 condition_type
= lttng_condition_get_type(condition
);
4093 switch (condition_type
) {
4094 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4095 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4096 if (caa_likely(previous_sample_available
)) {
4097 previous_sample_result
=
4098 evaluate_buffer_usage_condition(condition
,
4099 previous_sample
, channel_info
->capacity
);
4101 latest_sample_result
= evaluate_buffer_usage_condition(
4102 condition
, latest_sample
,
4103 channel_info
->capacity
);
4105 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4106 if (caa_likely(previous_sample_available
)) {
4107 previous_sample_result
=
4108 evaluate_session_consumed_size_condition(
4110 previous_session_consumed_total
);
4112 latest_sample_result
=
4113 evaluate_session_consumed_size_condition(
4115 latest_session_consumed_total
);
4118 /* Unknown condition type; internal error. */
4122 if (!latest_sample_result
||
4123 (previous_sample_result
== latest_sample_result
)) {
4125 * Only trigger on a condition evaluation transition.
4127 * NOTE: This edge-triggered logic may not be appropriate for
4128 * future condition types.
4133 if (!evaluation
|| !latest_sample_result
) {
4137 switch (condition_type
) {
4138 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4139 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4140 *evaluation
= lttng_evaluation_buffer_usage_create(
4142 latest_sample
->highest_usage
,
4143 channel_info
->capacity
);
4145 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4146 *evaluation
= lttng_evaluation_session_consumed_size_create(
4147 latest_session_consumed_total
);
4162 int client_notification_overflow(struct notification_client
*client
)
4165 const struct lttng_notification_channel_message msg
= {
4166 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4169 ASSERT_LOCKED(client
->lock
);
4171 DBG("Dropping notification addressed to client (socket fd = %i)",
4173 if (client
->communication
.outbound
.dropped_notification
) {
4175 * The client already has a "notification dropped" message
4176 * in its outgoing queue. Nothing to do since all
4177 * of those messages are coalesced.
4182 client
->communication
.outbound
.dropped_notification
= true;
4183 ret
= lttng_dynamic_buffer_append(
4184 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4187 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4194 static int client_handle_transmission_status_wrapper(
4195 struct notification_client
*client
,
4196 enum client_transmission_status status
,
4199 return client_handle_transmission_status(client
, status
,
4200 (struct notification_thread_state
*) user_data
);
4204 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4205 const struct lttng_evaluation
*evaluation
,
4206 struct notification_client_list
* client_list
,
4207 struct notification_thread_state
*state
,
4208 uid_t object_uid
, gid_t object_gid
)
4210 const struct lttng_credentials creds
= {
4211 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4212 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4215 return notification_client_list_send_evaluation(client_list
,
4216 trigger
, evaluation
,
4218 client_handle_transmission_status_wrapper
, state
);
4222 * Permission checks relative to notification channel clients are performed
4223 * here. Notice how object, client, and trigger credentials are involved in
4226 * The `object` credentials are the credentials associated with the "subject"
4227 * of a condition. For instance, a `rotation completed` condition applies
4228 * to a session. When that condition is met, it will produce an evaluation
4229 * against a session. Hence, in this case, the `object` credentials are the
4230 * credentials of the "subject" session.
4232 * The `trigger` credentials are the credentials of the user that registered the
4235 * The `client` credentials are the credentials of the user that created a given
4236 * notification channel.
4238 * In terms of visibility, it is expected that non-privilieged users can only
4239 * register triggers against "their" objects (their own sessions and
4240 * applications they are allowed to interact with). They can then open a
4241 * notification channel and subscribe to notifications associated with those
4244 * As for privilieged users, they can register triggers against the objects of
4245 * other users. They can then subscribe to the notifications associated to their
4246 * triggers. Privilieged users _can't_ subscribe to the notifications of
4247 * triggers owned by other users; they must create their own triggers.
4249 * This is more a concern of usability than security. It would be difficult for
4250 * a root user reliably subscribe to a specific set of conditions without
4251 * interference from external users (those could, for instance, unregister
4255 int notification_client_list_send_evaluation(
4256 struct notification_client_list
*client_list
,
4257 const struct lttng_trigger
*trigger
,
4258 const struct lttng_evaluation
*evaluation
,
4259 const struct lttng_credentials
*source_object_creds
,
4260 report_client_transmission_result_cb client_report
,
4264 struct lttng_payload msg_payload
;
4265 struct notification_client_list_element
*client_list_element
, *tmp
;
4266 const struct lttng_notification notification
= {
4267 .trigger
= (struct lttng_trigger
*) trigger
,
4268 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4270 struct lttng_notification_channel_message msg_header
= {
4271 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4273 const struct lttng_credentials
*trigger_creds
=
4274 lttng_trigger_get_credentials(trigger
);
4276 lttng_payload_init(&msg_payload
);
4278 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4279 sizeof(msg_header
));
4284 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4286 ERR("[notification-thread] Failed to serialize notification");
4291 /* Update payload size. */
4292 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4293 ->size
= (uint32_t)(
4294 msg_payload
.buffer
.size
- sizeof(msg_header
));
4296 /* Update the payload number of fds. */
4298 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4299 &msg_payload
, 0, -1);
4301 ((struct lttng_notification_channel_message
*)
4302 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4303 lttng_payload_view_get_fd_handle_count(&pv
);
4306 pthread_mutex_lock(&client_list
->lock
);
4307 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4308 &client_list
->clients_list
, node
) {
4309 enum client_transmission_status transmission_status
;
4310 struct notification_client
*client
=
4311 client_list_element
->client
;
4314 pthread_mutex_lock(&client
->lock
);
4315 if (!client
->communication
.active
) {
4317 * Skip inactive client (protocol error or
4320 DBG("Skipping client at it is marked as inactive");
4324 if (source_object_creds
) {
4325 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4326 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4329 * Client is not allowed to monitor this
4332 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4337 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
4338 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4342 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4343 client
->socket
, msg_payload
.buffer
.size
);
4345 if (client_has_outbound_data_left(client
)) {
4347 * Outgoing data is already buffered for this client;
4348 * drop the notification and enqueue a "dropped
4349 * notification" message if this is the first dropped
4350 * notification since the socket spilled-over to the
4353 ret
= client_notification_overflow(client
);
4360 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4366 transmission_status
= client_flush_outgoing_queue(client
);
4367 pthread_mutex_unlock(&client
->lock
);
4368 ret
= client_report(client
, transmission_status
, user_data
);
4371 goto end_unlock_list
;
4377 pthread_mutex_unlock(&client
->lock
);
4380 goto end_unlock_list
;
4386 pthread_mutex_unlock(&client_list
->lock
);
4388 lttng_payload_reset(&msg_payload
);
4393 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4394 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4398 struct lttng_event_notifier_notification
*notification
= NULL
;
4399 char *capture_buffer
= NULL
;
4400 size_t capture_buffer_size
;
4401 void *reception_buffer
;
4402 size_t reception_size
;
4404 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4405 struct lttng_kernel_event_notifier_notification kernel_notification
;
4407 /* Init lttng_event_notifier_notification */
4409 case LTTNG_DOMAIN_UST
:
4410 reception_buffer
= (void *) &ust_notification
;
4411 reception_size
= sizeof(ust_notification
);
4413 case LTTNG_DOMAIN_KERNEL
:
4414 reception_buffer
= (void *) &kernel_notification
;
4415 reception_size
= sizeof(kernel_notification
);
4422 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4423 * ensuring that read/write of tracer notifications are atomic.
4425 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4427 if (ret
!= reception_size
) {
4428 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4429 notification_pipe_read_fd
, reception_size
, ret
);
4435 case LTTNG_DOMAIN_UST
:
4436 token
= ust_notification
.token
;
4437 capture_buffer_size
= ust_notification
.capture_buf_size
;
4439 case LTTNG_DOMAIN_KERNEL
:
4440 token
= kernel_notification
.token
;
4441 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4447 if (capture_buffer_size
== 0) {
4448 capture_buffer
= NULL
;
4452 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4453 ERR("[notification-thread] Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4454 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4458 capture_buffer
= zmalloc(capture_buffer_size
);
4459 if (!capture_buffer
) {
4460 ERR("[notification-thread] Failed to allocate capture buffer");
4464 /* Fetch additional payload (capture). */
4465 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4466 if (ret
!= capture_buffer_size
) {
4467 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4468 notification_pipe_read_fd
);
4473 notification
= lttng_event_notifier_notification_create(token
, domain
,
4474 capture_buffer
, capture_buffer_size
);
4475 if (notification
== NULL
) {
4480 * Ownership transfered to the lttng_event_notifier_notification object.
4482 capture_buffer
= NULL
;
4485 free(capture_buffer
);
4486 return notification
;
4490 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4491 struct lttng_event_notifier_notification
*notification
)
4493 struct cds_lfht_node
*node
;
4494 struct cds_lfht_iter iter
;
4495 struct notification_trigger_tokens_ht_element
*element
;
4496 enum lttng_trigger_status trigger_status
;
4497 struct lttng_evaluation
*evaluation
= NULL
;
4498 enum action_executor_status executor_status
;
4499 struct notification_client_list
*client_list
= NULL
;
4500 const char *trigger_name
;
4502 unsigned int capture_count
= 0;
4504 /* Find triggers associated with this token. */
4506 cds_lfht_lookup(state
->trigger_tokens_ht
,
4507 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4508 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4509 node
= cds_lfht_iter_get_node(&iter
);
4510 if (caa_unlikely(!node
)) {
4512 * This is not an error, slow consumption of the tracer
4513 * notifications can lead to situations where a trigger is
4514 * removed but we still get tracer notifications matching a
4515 * trigger that no longer exists.
4521 element
= caa_container_of(node
,
4522 struct notification_trigger_tokens_ht_element
,
4525 if (!lttng_trigger_should_fire(element
->trigger
)) {
4530 lttng_trigger_fire(element
->trigger
);
4532 trigger_status
= lttng_trigger_get_name(element
->trigger
, &trigger_name
);
4533 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4535 if (lttng_condition_on_event_get_capture_descriptor_count(
4536 lttng_trigger_get_const_condition(element
->trigger
),
4537 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4538 ERR("Failed to get capture count");
4543 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4544 ERR("Expected capture but capture buffer is null");
4549 evaluation
= lttng_evaluation_on_event_create(
4550 container_of(lttng_trigger_get_const_condition(
4552 struct lttng_condition_on_event
,
4554 notification
->capture_buffer
,
4555 notification
->capture_buf_size
, false);
4557 if (evaluation
== NULL
) {
4558 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
4562 client_list
= get_client_list_from_condition(state
,
4563 lttng_trigger_get_const_condition(element
->trigger
));
4564 executor_status
= action_executor_enqueue(state
->executor
,
4565 element
->trigger
, evaluation
, NULL
, client_list
);
4566 switch (executor_status
) {
4567 case ACTION_EXECUTOR_STATUS_OK
:
4570 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4572 struct notification_client_list_element
*client_list_element
,
4576 * Not a fatal error; this is expected and simply means the
4577 * executor has too much work queued already.
4581 /* No clients subscribed to notifications for this trigger. */
4586 /* Warn clients that a notification (or more) was dropped. */
4587 pthread_mutex_lock(&client_list
->lock
);
4588 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4589 &client_list
->clients_list
, node
) {
4590 enum client_transmission_status transmission_status
;
4591 struct notification_client
*client
=
4592 client_list_element
->client
;
4594 pthread_mutex_lock(&client
->lock
);
4595 ret
= client_notification_overflow(client
);
4601 transmission_status
=
4602 client_flush_outgoing_queue(client
);
4603 ret
= client_handle_transmission_status(
4604 client
, transmission_status
, state
);
4610 pthread_mutex_unlock(&client
->lock
);
4616 pthread_mutex_unlock(&client_list
->lock
);
4619 case ACTION_EXECUTOR_STATUS_INVALID
:
4620 case ACTION_EXECUTOR_STATUS_ERROR
:
4621 /* Fatal error, shut down everything. */
4622 ERR("Fatal error encoutered while enqueuing action to the action executor");
4626 /* Unhandled error. */
4631 notification_client_list_put(client_list
);
4638 int handle_one_event_notifier_notification(
4639 struct notification_thread_state
*state
,
4640 int pipe
, enum lttng_domain_type domain
)
4643 struct lttng_event_notifier_notification
*notification
= NULL
;
4645 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4646 if (notification
== NULL
) {
4647 /* Reception failed, don't consider it fatal. */
4648 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4649 pipe
, lttng_domain_type_str(domain
));
4653 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4655 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4656 pipe
, lttng_domain_type_str(domain
));
4661 lttng_event_notifier_notification_destroy(notification
);
4665 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4666 int pipe
, enum lttng_domain_type domain
)
4668 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4671 int handle_notification_thread_channel_sample(
4672 struct notification_thread_state
*state
, int pipe
,
4673 enum lttng_domain_type domain
)
4676 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4677 struct channel_info
*channel_info
;
4678 struct cds_lfht_node
*node
;
4679 struct cds_lfht_iter iter
;
4680 struct lttng_channel_trigger_list
*trigger_list
;
4681 struct lttng_trigger_list_element
*trigger_list_element
;
4682 bool previous_sample_available
= false;
4683 struct channel_state_sample previous_sample
, latest_sample
;
4684 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4685 struct lttng_credentials channel_creds
;
4688 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4689 * ensuring that read/write of sampling messages are atomic.
4691 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4692 if (ret
!= sizeof(sample_msg
)) {
4693 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4700 latest_sample
.key
.key
= sample_msg
.key
;
4701 latest_sample
.key
.domain
= domain
;
4702 latest_sample
.highest_usage
= sample_msg
.highest
;
4703 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4704 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4708 /* Retrieve the channel's informations */
4709 cds_lfht_lookup(state
->channels_ht
,
4710 hash_channel_key(&latest_sample
.key
),
4714 node
= cds_lfht_iter_get_node(&iter
);
4715 if (caa_unlikely(!node
)) {
4717 * Not an error since the consumer can push a sample to the pipe
4718 * and the rest of the session daemon could notify us of the
4719 * channel's destruction before we get a chance to process that
4722 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4723 latest_sample
.key
.key
,
4724 lttng_domain_type_str(domain
));
4727 channel_info
= caa_container_of(node
, struct channel_info
,
4729 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4731 latest_sample
.key
.key
,
4732 channel_info
->session_info
->name
,
4733 latest_sample
.highest_usage
,
4734 latest_sample
.lowest_usage
,
4735 latest_sample
.channel_total_consumed
);
4737 previous_session_consumed_total
=
4738 channel_info
->session_info
->consumed_data_size
;
4740 /* Retrieve the channel's last sample, if it exists, and update it. */
4741 cds_lfht_lookup(state
->channel_state_ht
,
4742 hash_channel_key(&latest_sample
.key
),
4743 match_channel_state_sample
,
4746 node
= cds_lfht_iter_get_node(&iter
);
4747 if (caa_likely(node
)) {
4748 struct channel_state_sample
*stored_sample
;
4750 /* Update the sample stored. */
4751 stored_sample
= caa_container_of(node
,
4752 struct channel_state_sample
,
4753 channel_state_ht_node
);
4755 memcpy(&previous_sample
, stored_sample
,
4756 sizeof(previous_sample
));
4757 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4758 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4759 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4760 previous_sample_available
= true;
4762 latest_session_consumed_total
=
4763 previous_session_consumed_total
+
4764 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4767 * This is the channel's first sample, allocate space for and
4768 * store the new sample.
4770 struct channel_state_sample
*stored_sample
;
4772 stored_sample
= zmalloc(sizeof(*stored_sample
));
4773 if (!stored_sample
) {
4778 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4779 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4780 cds_lfht_add(state
->channel_state_ht
,
4781 hash_channel_key(&stored_sample
->key
),
4782 &stored_sample
->channel_state_ht_node
);
4784 latest_session_consumed_total
=
4785 previous_session_consumed_total
+
4786 latest_sample
.channel_total_consumed
;
4789 channel_info
->session_info
->consumed_data_size
=
4790 latest_session_consumed_total
;
4792 /* Find triggers associated with this channel. */
4793 cds_lfht_lookup(state
->channel_triggers_ht
,
4794 hash_channel_key(&latest_sample
.key
),
4795 match_channel_trigger_list
,
4798 node
= cds_lfht_iter_get_node(&iter
);
4799 if (caa_likely(!node
)) {
4803 channel_creds
= (typeof(channel_creds
)) {
4804 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4805 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4808 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4809 channel_triggers_ht_node
);
4810 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4812 const struct lttng_condition
*condition
;
4813 struct lttng_trigger
*trigger
;
4814 struct notification_client_list
*client_list
= NULL
;
4815 struct lttng_evaluation
*evaluation
= NULL
;
4816 enum action_executor_status executor_status
;
4819 trigger
= trigger_list_element
->trigger
;
4820 condition
= lttng_trigger_get_const_condition(trigger
);
4824 * Check if any client is subscribed to the result of this
4827 client_list
= get_client_list_from_condition(state
, condition
);
4829 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4830 previous_sample_available
? &previous_sample
: NULL
,
4832 previous_session_consumed_total
,
4833 latest_session_consumed_total
,
4835 if (caa_unlikely(ret
)) {
4839 if (caa_likely(!evaluation
)) {
4843 if (!lttng_trigger_should_fire(trigger
)) {
4847 lttng_trigger_fire(trigger
);
4850 * Ownership of `evaluation` transferred to the action executor
4851 * no matter the result.
4853 executor_status
= action_executor_enqueue(state
->executor
,
4854 trigger
, evaluation
, &channel_creds
,
4857 switch (executor_status
) {
4858 case ACTION_EXECUTOR_STATUS_OK
:
4860 case ACTION_EXECUTOR_STATUS_ERROR
:
4861 case ACTION_EXECUTOR_STATUS_INVALID
:
4863 * TODO Add trigger identification (name/id) when
4864 * it is added to the API.
4866 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4869 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4871 * TODO Add trigger identification (name/id) when
4872 * it is added to the API.
4874 * Not a fatal error.
4876 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4884 notification_client_list_put(client_list
);
4885 if (caa_unlikely(ret
)) {