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
);
1155 pthread_mutex_unlock(&client_list
->lock
);
1159 pthread_mutex_unlock(&client_list
->lock
);
1162 * Add the client to the list of clients interested in a given trigger
1163 * if a "notification" trigger with a corresponding condition was
1166 client_list_element
->client
= client
;
1167 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1169 pthread_mutex_lock(&client_list
->lock
);
1170 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1171 pthread_mutex_unlock(&client_list
->lock
);
1177 notification_client_list_put(client_list
);
1181 free(condition_list_element
);
1182 free(client_list_element
);
1187 int notification_thread_client_unsubscribe(
1188 struct notification_client
*client
,
1189 struct lttng_condition
*condition
,
1190 struct notification_thread_state
*state
,
1191 enum lttng_notification_channel_status
*_status
)
1193 struct notification_client_list
*client_list
;
1194 struct lttng_condition_list_element
*condition_list_element
,
1196 struct notification_client_list_element
*client_list_element
,
1198 bool condition_found
= false;
1199 enum lttng_notification_channel_status status
=
1200 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1202 /* Remove the condition from the client's condition list. */
1203 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1204 &client
->condition_list
, node
) {
1205 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1210 cds_list_del(&condition_list_element
->node
);
1212 * The caller may be iterating on the client's conditions to
1213 * tear down a client's connection. In this case, the condition
1214 * will be destroyed at the end.
1216 if (condition
!= condition_list_element
->condition
) {
1217 lttng_condition_destroy(
1218 condition_list_element
->condition
);
1220 free(condition_list_element
);
1221 condition_found
= true;
1225 if (!condition_found
) {
1226 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1231 * Remove the client from the list of clients interested the trigger
1232 * matching the condition.
1234 client_list
= get_client_list_from_condition(state
, condition
);
1239 pthread_mutex_lock(&client_list
->lock
);
1240 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1241 &client_list
->clients_list
, node
) {
1242 if (client_list_element
->client
->id
!= client
->id
) {
1245 cds_list_del(&client_list_element
->node
);
1246 free(client_list_element
);
1249 pthread_mutex_unlock(&client_list
->lock
);
1250 notification_client_list_put(client_list
);
1253 lttng_condition_destroy(condition
);
1261 void free_notification_client_rcu(struct rcu_head
*node
)
1263 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1267 void notification_client_destroy(struct notification_client
*client
,
1268 struct notification_thread_state
*state
)
1275 * The client object is not reachable by other threads, no need to lock
1278 if (client
->socket
>= 0) {
1279 (void) lttcomm_close_unix_sock(client
->socket
);
1280 client
->socket
= -1;
1282 client
->communication
.active
= false;
1283 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1284 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1285 pthread_mutex_destroy(&client
->lock
);
1286 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1290 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1294 struct notification_client
*get_client_from_socket(int socket
,
1295 struct notification_thread_state
*state
)
1297 struct cds_lfht_iter iter
;
1298 struct cds_lfht_node
*node
;
1299 struct notification_client
*client
= NULL
;
1301 cds_lfht_lookup(state
->client_socket_ht
,
1302 hash_client_socket(socket
),
1303 match_client_socket
,
1304 (void *) (unsigned long) socket
,
1306 node
= cds_lfht_iter_get_node(&iter
);
1311 client
= caa_container_of(node
, struct notification_client
,
1312 client_socket_ht_node
);
1318 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1322 struct notification_client
*get_client_from_id(notification_client_id id
,
1323 struct notification_thread_state
*state
)
1325 struct cds_lfht_iter iter
;
1326 struct cds_lfht_node
*node
;
1327 struct notification_client
*client
= NULL
;
1329 cds_lfht_lookup(state
->client_id_ht
,
1334 node
= cds_lfht_iter_get_node(&iter
);
1339 client
= caa_container_of(node
, struct notification_client
,
1346 bool buffer_usage_condition_applies_to_channel(
1347 const struct lttng_condition
*condition
,
1348 const struct channel_info
*channel_info
)
1350 enum lttng_condition_status status
;
1351 enum lttng_domain_type condition_domain
;
1352 const char *condition_session_name
= NULL
;
1353 const char *condition_channel_name
= NULL
;
1355 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1357 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1358 if (channel_info
->key
.domain
!= condition_domain
) {
1362 status
= lttng_condition_buffer_usage_get_session_name(
1363 condition
, &condition_session_name
);
1364 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1366 status
= lttng_condition_buffer_usage_get_channel_name(
1367 condition
, &condition_channel_name
);
1368 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1370 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1373 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1383 bool session_consumed_size_condition_applies_to_channel(
1384 const struct lttng_condition
*condition
,
1385 const struct channel_info
*channel_info
)
1387 enum lttng_condition_status status
;
1388 const char *condition_session_name
= NULL
;
1390 status
= lttng_condition_session_consumed_size_get_session_name(
1391 condition
, &condition_session_name
);
1392 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1394 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1404 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1405 const struct channel_info
*channel_info
)
1407 const struct lttng_condition
*condition
;
1408 bool trigger_applies
;
1410 condition
= lttng_trigger_get_const_condition(trigger
);
1415 switch (lttng_condition_get_type(condition
)) {
1416 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1417 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1418 trigger_applies
= buffer_usage_condition_applies_to_channel(
1419 condition
, channel_info
);
1421 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1422 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1423 condition
, channel_info
);
1429 return trigger_applies
;
1434 /* Must be called with RCU read lock held. */
1436 struct lttng_session_trigger_list
*get_session_trigger_list(
1437 struct notification_thread_state
*state
,
1438 const char *session_name
)
1440 struct lttng_session_trigger_list
*list
= NULL
;
1441 struct cds_lfht_node
*node
;
1442 struct cds_lfht_iter iter
;
1444 cds_lfht_lookup(state
->session_triggers_ht
,
1445 hash_key_str(session_name
, lttng_ht_seed
),
1446 match_session_trigger_list
,
1449 node
= cds_lfht_iter_get_node(&iter
);
1452 * Not an error, the list of triggers applying to that session
1453 * will be initialized when the session is created.
1455 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1460 list
= caa_container_of(node
,
1461 struct lttng_session_trigger_list
,
1462 session_triggers_ht_node
);
1468 * Allocate an empty lttng_session_trigger_list for the session named
1471 * No ownership of 'session_name' is assumed by the session trigger list.
1472 * It is the caller's responsability to ensure the session name is alive
1473 * for as long as this list is.
1476 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1477 const char *session_name
,
1478 struct cds_lfht
*session_triggers_ht
)
1480 struct lttng_session_trigger_list
*list
;
1482 list
= zmalloc(sizeof(*list
));
1486 list
->session_name
= session_name
;
1487 CDS_INIT_LIST_HEAD(&list
->list
);
1488 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1489 list
->session_triggers_ht
= session_triggers_ht
;
1492 /* Publish the list through the session_triggers_ht. */
1493 cds_lfht_add(session_triggers_ht
,
1494 hash_key_str(session_name
, lttng_ht_seed
),
1495 &list
->session_triggers_ht_node
);
1502 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1504 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1509 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1511 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1513 /* Empty the list element by element, and then free the list itself. */
1514 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1515 &list
->list
, node
) {
1516 cds_list_del(&trigger_list_element
->node
);
1517 free(trigger_list_element
);
1520 /* Unpublish the list from the session_triggers_ht. */
1521 cds_lfht_del(list
->session_triggers_ht
,
1522 &list
->session_triggers_ht_node
);
1524 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1528 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1529 struct lttng_trigger
*trigger
)
1532 struct lttng_trigger_list_element
*new_element
=
1533 zmalloc(sizeof(*new_element
));
1539 CDS_INIT_LIST_HEAD(&new_element
->node
);
1540 new_element
->trigger
= trigger
;
1541 cds_list_add(&new_element
->node
, &list
->list
);
1547 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1548 const char *session_name
)
1550 bool applies
= false;
1551 const struct lttng_condition
*condition
;
1553 condition
= lttng_trigger_get_const_condition(trigger
);
1554 switch (lttng_condition_get_type(condition
)) {
1555 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1556 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1558 enum lttng_condition_status condition_status
;
1559 const char *condition_session_name
;
1561 condition_status
= lttng_condition_session_rotation_get_session_name(
1562 condition
, &condition_session_name
);
1563 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1564 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1568 assert(condition_session_name
);
1569 applies
= !strcmp(condition_session_name
, session_name
);
1580 * Allocate and initialize an lttng_session_trigger_list which contains
1581 * all triggers that apply to the session named 'session_name'.
1583 * No ownership of 'session_name' is assumed by the session trigger list.
1584 * It is the caller's responsability to ensure the session name is alive
1585 * for as long as this list is.
1588 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1589 const struct notification_thread_state
*state
,
1590 const char *session_name
)
1592 int trigger_count
= 0;
1593 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1594 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1595 struct cds_lfht_iter iter
;
1597 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1598 state
->session_triggers_ht
);
1600 /* Add all triggers applying to the session named 'session_name'. */
1601 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1605 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1610 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1611 trigger_ht_element
->trigger
);
1619 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1621 return session_trigger_list
;
1623 lttng_session_trigger_list_destroy(session_trigger_list
);
1628 struct session_info
*find_or_create_session_info(
1629 struct notification_thread_state
*state
,
1630 const char *name
, uid_t uid
, gid_t gid
)
1632 struct session_info
*session
= NULL
;
1633 struct cds_lfht_node
*node
;
1634 struct cds_lfht_iter iter
;
1635 struct lttng_session_trigger_list
*trigger_list
;
1638 cds_lfht_lookup(state
->sessions_ht
,
1639 hash_key_str(name
, lttng_ht_seed
),
1643 node
= cds_lfht_iter_get_node(&iter
);
1645 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1647 session
= caa_container_of(node
, struct session_info
,
1649 assert(session
->uid
== uid
);
1650 assert(session
->gid
== gid
);
1651 session_info_get(session
);
1655 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1656 if (!trigger_list
) {
1660 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1661 state
->sessions_ht
);
1663 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1665 lttng_session_trigger_list_destroy(trigger_list
);
1668 trigger_list
= NULL
;
1670 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1671 &session
->sessions_ht_node
);
1677 session_info_put(session
);
1682 int handle_notification_thread_command_add_channel(
1683 struct notification_thread_state
*state
,
1684 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1685 const char *channel_name
, enum lttng_domain_type channel_domain
,
1686 uint64_t channel_key_int
, uint64_t channel_capacity
,
1687 enum lttng_error_code
*cmd_result
)
1689 struct cds_list_head trigger_list
;
1690 struct channel_info
*new_channel_info
= NULL
;
1691 struct channel_key channel_key
= {
1692 .key
= channel_key_int
,
1693 .domain
= channel_domain
,
1695 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1696 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1697 int trigger_count
= 0;
1698 struct cds_lfht_iter iter
;
1699 struct session_info
*session_info
= NULL
;
1701 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1702 channel_name
, session_name
, channel_key_int
,
1703 lttng_domain_type_str(channel_domain
));
1705 CDS_INIT_LIST_HEAD(&trigger_list
);
1707 session_info
= find_or_create_session_info(state
, session_name
,
1708 session_uid
, session_gid
);
1709 if (!session_info
) {
1710 /* Allocation error or an internal error occurred. */
1714 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1715 channel_capacity
, session_info
);
1716 if (!new_channel_info
) {
1721 /* Build a list of all triggers applying to the new channel. */
1722 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1724 struct lttng_trigger_list_element
*new_element
;
1726 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1727 new_channel_info
)) {
1731 new_element
= zmalloc(sizeof(*new_element
));
1736 CDS_INIT_LIST_HEAD(&new_element
->node
);
1737 new_element
->trigger
= trigger_ht_element
->trigger
;
1738 cds_list_add(&new_element
->node
, &trigger_list
);
1743 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1745 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1746 if (!channel_trigger_list
) {
1749 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1750 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1751 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1752 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1755 /* Add channel to the channel_ht which owns the channel_infos. */
1756 cds_lfht_add(state
->channels_ht
,
1757 hash_channel_key(&new_channel_info
->key
),
1758 &new_channel_info
->channels_ht_node
);
1760 * Add the list of triggers associated with this channel to the
1761 * channel_triggers_ht.
1763 cds_lfht_add(state
->channel_triggers_ht
,
1764 hash_channel_key(&new_channel_info
->key
),
1765 &channel_trigger_list
->channel_triggers_ht_node
);
1767 session_info_put(session_info
);
1768 *cmd_result
= LTTNG_OK
;
1771 channel_info_destroy(new_channel_info
);
1772 session_info_put(session_info
);
1777 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1779 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1784 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1786 free(caa_container_of(node
, struct channel_state_sample
,
1791 int handle_notification_thread_command_remove_channel(
1792 struct notification_thread_state
*state
,
1793 uint64_t channel_key
, enum lttng_domain_type domain
,
1794 enum lttng_error_code
*cmd_result
)
1796 struct cds_lfht_node
*node
;
1797 struct cds_lfht_iter iter
;
1798 struct lttng_channel_trigger_list
*trigger_list
;
1799 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1800 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1801 struct channel_info
*channel_info
;
1803 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1804 channel_key
, lttng_domain_type_str(domain
));
1808 cds_lfht_lookup(state
->channel_triggers_ht
,
1809 hash_channel_key(&key
),
1810 match_channel_trigger_list
,
1813 node
= cds_lfht_iter_get_node(&iter
);
1815 * There is a severe internal error if we are being asked to remove a
1816 * channel that doesn't exist.
1819 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1823 /* Free the list of triggers associated with this channel. */
1824 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1825 channel_triggers_ht_node
);
1826 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1827 &trigger_list
->list
, node
) {
1828 cds_list_del(&trigger_list_element
->node
);
1829 free(trigger_list_element
);
1831 cds_lfht_del(state
->channel_triggers_ht
, node
);
1832 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1834 /* Free sampled channel state. */
1835 cds_lfht_lookup(state
->channel_state_ht
,
1836 hash_channel_key(&key
),
1837 match_channel_state_sample
,
1840 node
= cds_lfht_iter_get_node(&iter
);
1842 * This is expected to be NULL if the channel is destroyed before we
1843 * received a sample.
1846 struct channel_state_sample
*sample
= caa_container_of(node
,
1847 struct channel_state_sample
,
1848 channel_state_ht_node
);
1850 cds_lfht_del(state
->channel_state_ht
, node
);
1851 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1854 /* Remove the channel from the channels_ht and free it. */
1855 cds_lfht_lookup(state
->channels_ht
,
1856 hash_channel_key(&key
),
1860 node
= cds_lfht_iter_get_node(&iter
);
1862 channel_info
= caa_container_of(node
, struct channel_info
,
1864 cds_lfht_del(state
->channels_ht
, node
);
1865 channel_info_destroy(channel_info
);
1868 *cmd_result
= LTTNG_OK
;
1873 int handle_notification_thread_command_session_rotation(
1874 struct notification_thread_state
*state
,
1875 enum notification_thread_command_type cmd_type
,
1876 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1877 uint64_t trace_archive_chunk_id
,
1878 struct lttng_trace_archive_location
*location
,
1879 enum lttng_error_code
*_cmd_result
)
1882 enum lttng_error_code cmd_result
= LTTNG_OK
;
1883 struct lttng_session_trigger_list
*trigger_list
;
1884 struct lttng_trigger_list_element
*trigger_list_element
;
1885 struct session_info
*session_info
;
1886 const struct lttng_credentials session_creds
= {
1887 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1888 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1893 session_info
= find_or_create_session_info(state
, session_name
,
1894 session_uid
, session_gid
);
1895 if (!session_info
) {
1896 /* Allocation error or an internal error occurred. */
1898 cmd_result
= LTTNG_ERR_NOMEM
;
1902 session_info
->rotation
.ongoing
=
1903 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1904 session_info
->rotation
.id
= trace_archive_chunk_id
;
1905 trigger_list
= get_session_trigger_list(state
, session_name
);
1906 if (!trigger_list
) {
1907 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1912 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1914 const struct lttng_condition
*condition
;
1915 struct lttng_trigger
*trigger
;
1916 struct notification_client_list
*client_list
;
1917 struct lttng_evaluation
*evaluation
= NULL
;
1918 enum lttng_condition_type condition_type
;
1919 enum action_executor_status executor_status
;
1921 trigger
= trigger_list_element
->trigger
;
1922 condition
= lttng_trigger_get_const_condition(trigger
);
1924 condition_type
= lttng_condition_get_type(condition
);
1926 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1927 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1929 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1930 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1934 client_list
= get_client_list_from_condition(state
, condition
);
1935 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1936 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1937 trace_archive_chunk_id
);
1939 evaluation
= lttng_evaluation_session_rotation_completed_create(
1940 trace_archive_chunk_id
, location
);
1944 /* Internal error */
1946 cmd_result
= LTTNG_ERR_UNK
;
1951 * Ownership of `evaluation` transferred to the action executor
1952 * no matter the result.
1954 executor_status
= action_executor_enqueue(state
->executor
,
1955 trigger
, evaluation
, &session_creds
,
1958 switch (executor_status
) {
1959 case ACTION_EXECUTOR_STATUS_OK
:
1961 case ACTION_EXECUTOR_STATUS_ERROR
:
1962 case ACTION_EXECUTOR_STATUS_INVALID
:
1964 * TODO Add trigger identification (name/id) when
1965 * it is added to the API.
1967 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1970 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1972 * TODO Add trigger identification (name/id) when
1973 * it is added to the API.
1975 * Not a fatal error.
1977 WARN("No space left when enqueuing action associated with session rotation trigger");
1985 notification_client_list_put(client_list
);
1986 if (caa_unlikely(ret
)) {
1991 session_info_put(session_info
);
1992 *_cmd_result
= cmd_result
;
1998 int handle_notification_thread_command_add_tracer_event_source(
1999 struct notification_thread_state
*state
,
2000 int tracer_event_source_fd
,
2001 enum lttng_domain_type domain_type
,
2002 enum lttng_error_code
*_cmd_result
)
2005 enum lttng_error_code cmd_result
= LTTNG_OK
;
2006 struct notification_event_tracer_event_source_element
*element
= NULL
;
2008 element
= zmalloc(sizeof(*element
));
2010 cmd_result
= LTTNG_ERR_NOMEM
;
2015 element
->fd
= tracer_event_source_fd
;
2016 element
->domain
= domain_type
;
2018 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2020 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2021 tracer_event_source_fd
,
2022 lttng_domain_type_str(domain_type
));
2024 /* Adding the read side pipe to the event poll. */
2025 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
2027 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2028 tracer_event_source_fd
,
2029 lttng_domain_type_str(element
->domain
));
2030 cds_list_del(&element
->node
);
2035 element
->is_fd_in_poll_set
= true;
2038 *_cmd_result
= cmd_result
;
2043 int drain_event_notifier_notification_pipe(
2044 struct notification_thread_state
*state
,
2045 int pipe
, enum lttng_domain_type domain
)
2047 struct lttng_poll_event events
= {0};
2050 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2052 ERR("[notification-thread] Error creating lttng_poll_event");
2056 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2058 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2065 * Continue to consume notifications as long as there are new
2066 * ones coming in. The tracer has been asked to stop producing
2069 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2070 * monitored (on Linux, at least) and will be returned when
2071 * the pipe is closed but empty.
2073 ret
= lttng_poll_wait_interruptible(&events
, 0);
2074 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2075 /* No more notification to be read on this pipe. */
2078 } else if (ret
< 0) {
2079 PERROR("Failed on lttng_poll_wait_interruptible() call");
2084 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2086 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2091 lttng_poll_clean(&events
);
2096 int handle_notification_thread_command_remove_tracer_event_source(
2097 struct notification_thread_state
*state
,
2098 int tracer_event_source_fd
,
2099 enum lttng_error_code
*_cmd_result
)
2103 enum lttng_error_code cmd_result
= LTTNG_OK
;
2104 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
2106 cds_list_for_each_entry_safe(source_element
, tmp
,
2107 &state
->tracer_event_sources_list
, node
) {
2108 if (source_element
->fd
!= tracer_event_source_fd
) {
2112 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2113 tracer_event_source_fd
,
2114 lttng_domain_type_str(source_element
->domain
));
2115 cds_list_del(&source_element
->node
);
2122 * This is temporarily allowed since the poll activity set is
2123 * not properly cleaned-up for the moment. This is adressed in
2126 source_element
= NULL
;
2130 if (!source_element
->is_fd_in_poll_set
) {
2131 /* Skip the poll set removal. */
2135 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2136 tracer_event_source_fd
,
2137 lttng_domain_type_str(source_element
->domain
));
2139 /* Removing the fd from the event poll set. */
2140 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2142 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2143 tracer_event_source_fd
,
2144 lttng_domain_type_str(source_element
->domain
));
2145 cmd_result
= LTTNG_ERR_FATAL
;
2149 source_element
->is_fd_in_poll_set
= false;
2151 ret
= drain_event_notifier_notification_pipe(state
, tracer_event_source_fd
,
2152 source_element
->domain
);
2154 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2155 tracer_event_source_fd
,
2156 lttng_domain_type_str(source_element
->domain
));
2157 cmd_result
= LTTNG_ERR_FATAL
;
2162 * The drain_event_notifier_notification_pipe() call might have read
2163 * data from an fd that we received in event in the latest _poll_wait()
2164 * call. Make sure the thread call poll_wait() again to ensure we have
2167 state
->restart_poll
= true;
2170 free(source_element
);
2171 *_cmd_result
= cmd_result
;
2176 int condition_on_event_update_error_count(struct lttng_trigger
*trigger
)
2179 uint64_t error_count
= 0;
2180 struct lttng_condition
*condition
;
2181 enum event_notifier_error_accounting_status status
;
2183 condition
= lttng_trigger_get_condition(trigger
);
2184 assert(lttng_condition_get_type(condition
) ==
2185 LTTNG_CONDITION_TYPE_ON_EVENT
);
2187 status
= event_notifier_error_accounting_get_count(trigger
, &error_count
);
2188 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2189 uid_t trigger_owner_uid
;
2190 const char *trigger_name
;
2191 const enum lttng_trigger_status trigger_status
=
2192 lttng_trigger_get_owner_uid(
2193 trigger
, &trigger_owner_uid
);
2195 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2196 if (lttng_trigger_get_name(trigger
, &trigger_name
) != LTTNG_TRIGGER_STATUS_OK
) {
2197 trigger_name
= "(unnamed)";
2200 ERR("Failed to get event notifier error count of trigger for update: trigger owner = %d, trigger name = '%s'",
2201 trigger_owner_uid
, trigger_name
);
2205 lttng_condition_on_event_set_error_count(condition
, error_count
);
2209 int handle_notification_thread_remove_tracer_event_source_no_result(
2210 struct notification_thread_state
*state
,
2211 int tracer_event_source_fd
)
2214 enum lttng_error_code cmd_result
;
2216 ret
= handle_notification_thread_command_remove_tracer_event_source(
2217 state
, tracer_event_source_fd
, &cmd_result
);
2222 static int handle_notification_thread_command_list_triggers(
2223 struct notification_thread_handle
*handle
,
2224 struct notification_thread_state
*state
,
2226 struct lttng_triggers
**triggers
,
2227 enum lttng_error_code
*_cmd_result
)
2230 enum lttng_error_code cmd_result
= LTTNG_OK
;
2231 struct cds_lfht_iter iter
;
2232 struct lttng_trigger_ht_element
*trigger_ht_element
;
2233 struct lttng_triggers
*local_triggers
= NULL
;
2234 const struct lttng_credentials
*creds
;
2238 local_triggers
= lttng_triggers_create();
2239 if (!local_triggers
) {
2240 /* Not a fatal error. */
2241 cmd_result
= LTTNG_ERR_NOMEM
;
2245 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2246 trigger_ht_element
, node
) {
2248 * Only return the triggers to which the client has access.
2249 * The root user has visibility over all triggers.
2251 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2252 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2256 if (lttng_trigger_needs_tracer_notifier(trigger_ht_element
->trigger
)) {
2257 ret
= condition_on_event_update_error_count(
2258 trigger_ht_element
->trigger
);
2262 ret
= lttng_triggers_add(local_triggers
,
2263 trigger_ht_element
->trigger
);
2265 /* Not a fatal error. */
2267 cmd_result
= LTTNG_ERR_NOMEM
;
2272 /* Transferring ownership to the caller. */
2273 *triggers
= local_triggers
;
2274 local_triggers
= NULL
;
2278 lttng_triggers_destroy(local_triggers
);
2279 *_cmd_result
= cmd_result
;
2284 bool condition_is_supported(struct lttng_condition
*condition
)
2288 switch (lttng_condition_get_type(condition
)) {
2289 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2290 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2293 enum lttng_domain_type domain
;
2295 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2299 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2300 is_supported
= true;
2305 * Older kernel tracers don't expose the API to monitor their
2306 * buffers. Therefore, we reject triggers that require that
2307 * mechanism to be available to be evaluated.
2309 * Assume unsupported on error.
2311 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2314 case LTTNG_CONDITION_TYPE_ON_EVENT
:
2316 const struct lttng_event_rule
*event_rule
;
2317 enum lttng_domain_type domain
;
2318 const enum lttng_condition_status status
=
2319 lttng_condition_on_event_get_rule(
2320 condition
, &event_rule
);
2322 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2324 domain
= lttng_event_rule_get_domain_type(event_rule
);
2325 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2326 is_supported
= true;
2331 * Older kernel tracers can't emit notification. Therefore, we
2332 * reject triggers that require that mechanism to be available
2335 * Assume unsupported on error.
2337 is_supported
= kernel_supports_event_notifiers() == 1;
2341 is_supported
= true;
2344 return is_supported
;
2347 /* Must be called with RCU read lock held. */
2349 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2350 struct notification_thread_state
*state
)
2353 const struct lttng_condition
*condition
;
2354 const char *session_name
;
2355 struct lttng_session_trigger_list
*trigger_list
;
2357 condition
= lttng_trigger_get_const_condition(trigger
);
2358 switch (lttng_condition_get_type(condition
)) {
2359 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2360 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2362 enum lttng_condition_status status
;
2364 status
= lttng_condition_session_rotation_get_session_name(
2365 condition
, &session_name
);
2366 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2367 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2378 trigger_list
= get_session_trigger_list(state
, session_name
);
2379 if (!trigger_list
) {
2380 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2386 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2388 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2393 /* Must be called with RCU read lock held. */
2395 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2396 struct notification_thread_state
*state
)
2399 struct cds_lfht_node
*node
;
2400 struct cds_lfht_iter iter
;
2401 struct channel_info
*channel
;
2403 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2405 struct lttng_trigger_list_element
*trigger_list_element
;
2406 struct lttng_channel_trigger_list
*trigger_list
;
2407 struct cds_lfht_iter lookup_iter
;
2409 if (!trigger_applies_to_channel(trigger
, channel
)) {
2413 cds_lfht_lookup(state
->channel_triggers_ht
,
2414 hash_channel_key(&channel
->key
),
2415 match_channel_trigger_list
,
2418 node
= cds_lfht_iter_get_node(&lookup_iter
);
2420 trigger_list
= caa_container_of(node
,
2421 struct lttng_channel_trigger_list
,
2422 channel_triggers_ht_node
);
2424 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2425 if (!trigger_list_element
) {
2429 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2430 trigger_list_element
->trigger
= trigger
;
2431 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2432 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2440 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2442 bool is_notify
= false;
2443 unsigned int i
, count
;
2444 enum lttng_action_status action_status
;
2445 const struct lttng_action
*action
=
2446 lttng_trigger_get_const_action(trigger
);
2447 enum lttng_action_type action_type
;
2450 action_type
= lttng_action_get_type(action
);
2451 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2454 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2458 action_status
= lttng_action_group_get_count(action
, &count
);
2459 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2461 for (i
= 0; i
< count
; i
++) {
2462 const struct lttng_action
*inner_action
=
2463 lttng_action_group_get_at_index(
2466 action_type
= lttng_action_get_type(inner_action
);
2467 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2477 static bool trigger_name_taken(struct notification_thread_state
*state
,
2478 const struct lttng_trigger
*trigger
)
2480 struct cds_lfht_iter iter
;
2483 * No duplicata is allowed in the triggers_by_name_uid_ht.
2484 * The match is done against the trigger name and uid.
2486 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2487 hash_trigger_by_name_uid(trigger
),
2488 match_trigger_by_name_uid
,
2491 return !!cds_lfht_iter_get_node(&iter
);
2495 enum lttng_error_code
generate_trigger_name(
2496 struct notification_thread_state
*state
,
2497 struct lttng_trigger
*trigger
, const char **name
)
2499 enum lttng_error_code ret_code
= LTTNG_OK
;
2501 enum lttng_trigger_status status
;
2504 const int ret
= lttng_trigger_generate_name(trigger
,
2505 state
->trigger_id
.name_offset
++);
2507 /* The only reason this can fail right now. */
2508 ret_code
= LTTNG_ERR_NOMEM
;
2512 status
= lttng_trigger_get_name(trigger
, name
);
2513 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2515 taken
= trigger_name_taken(state
, trigger
);
2516 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2522 void notif_thread_state_remove_trigger_ht_elem(
2523 struct notification_thread_state
*state
,
2524 struct lttng_trigger_ht_element
*trigger_ht_element
)
2527 assert(trigger_ht_element
);
2529 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2530 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2534 enum lttng_error_code
setup_tracer_notifier(
2535 struct notification_thread_state
*state
,
2536 struct lttng_trigger
*trigger
)
2538 enum lttng_error_code ret
;
2539 enum event_notifier_error_accounting_status error_accounting_status
;
2540 struct cds_lfht_node
*node
;
2541 uint64_t error_counter_index
= 0;
2542 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2543 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2545 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2546 if (!trigger_tokens_ht_element
) {
2547 ret
= LTTNG_ERR_NOMEM
;
2551 /* Add trigger token to the trigger_tokens_ht. */
2552 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2553 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2554 trigger_tokens_ht_element
->trigger
= trigger
;
2556 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2557 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2558 match_trigger_token
,
2559 &trigger_tokens_ht_element
->token
,
2560 &trigger_tokens_ht_element
->node
);
2561 if (node
!= &trigger_tokens_ht_element
->node
) {
2562 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2563 goto error_free_ht_element
;
2566 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2567 trigger
, &error_counter_index
);
2568 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2569 if (error_accounting_status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2570 DBG("Trigger group error accounting counter full.");
2571 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2573 ERR("Error registering trigger for error accounting");
2574 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2577 goto error_remove_ht_element
;
2580 lttng_condition_on_event_set_error_counter_index(
2581 condition
, error_counter_index
);
2586 error_remove_ht_element
:
2587 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2588 error_free_ht_element
:
2589 free(trigger_tokens_ht_element
);
2595 * FIXME A client's credentials are not checked when registering a trigger.
2597 * The effects of this are benign since:
2598 * - The client will succeed in registering the trigger, as it is valid,
2599 * - The trigger will, internally, be bound to the channel/session,
2600 * - The notifications will not be sent since the client's credentials
2601 * are checked against the channel at that moment.
2603 * If this function returns a non-zero value, it means something is
2604 * fundamentally broken and the whole subsystem/thread will be torn down.
2606 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2610 int handle_notification_thread_command_register_trigger(
2611 struct notification_thread_state
*state
,
2612 struct lttng_trigger
*trigger
,
2613 enum lttng_error_code
*cmd_result
)
2616 struct lttng_condition
*condition
;
2617 struct notification_client_list
*client_list
= NULL
;
2618 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2619 struct cds_lfht_node
*node
;
2620 const char* trigger_name
;
2621 bool free_trigger
= true;
2622 struct lttng_evaluation
*evaluation
= NULL
;
2623 struct lttng_credentials object_creds
;
2626 enum action_executor_status executor_status
;
2627 const uint64_t trigger_tracer_token
=
2628 state
->trigger_id
.next_tracer_token
++;
2632 /* Set the trigger's tracer token. */
2633 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2635 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2636 LTTNG_TRIGGER_STATUS_UNSET
) {
2637 const enum lttng_error_code ret_code
= generate_trigger_name(
2638 state
, trigger
, &trigger_name
);
2640 if (ret_code
!= LTTNG_OK
) {
2643 *cmd_result
= ret_code
;
2646 } else if (trigger_name_taken(state
, trigger
)) {
2647 /* Not a fatal error. */
2648 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2653 condition
= lttng_trigger_get_condition(trigger
);
2656 /* Some conditions require tracers to implement a minimal ABI version. */
2657 if (!condition_is_supported(condition
)) {
2658 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2662 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2663 if (!trigger_ht_element
) {
2668 /* Add trigger to the trigger_ht. */
2669 cds_lfht_node_init(&trigger_ht_element
->node
);
2670 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2671 trigger_ht_element
->trigger
= trigger
;
2673 node
= cds_lfht_add_unique(state
->triggers_ht
,
2674 lttng_condition_hash(condition
),
2677 &trigger_ht_element
->node
);
2678 if (node
!= &trigger_ht_element
->node
) {
2679 /* Not a fatal error, simply report it to the client. */
2680 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2681 goto error_free_ht_element
;
2684 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2685 hash_trigger_by_name_uid(trigger
),
2686 match_trigger_by_name_uid
,
2688 &trigger_ht_element
->node_by_name_uid
);
2689 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2690 /* Not a fatal error, simply report it to the client. */
2691 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2692 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2693 goto error_free_ht_element
;
2696 /* From this point consider the trigger registered. */
2697 lttng_trigger_set_as_registered(trigger
);
2700 * Some triggers might need a tracer notifier depending on its
2701 * condition and actions.
2703 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2704 enum lttng_error_code error_code
;
2706 error_code
= setup_tracer_notifier(state
, trigger
);
2707 if (error_code
!= LTTNG_OK
) {
2708 notif_thread_state_remove_trigger_ht_elem(state
,
2709 trigger_ht_element
);
2710 if (error_code
== LTTNG_ERR_NOMEM
) {
2713 *cmd_result
= error_code
;
2717 goto error_free_ht_element
;
2722 * The rest only applies to triggers that have a "notify" action.
2723 * It is not skipped as this is the only action type currently
2726 if (is_trigger_action_notify(trigger
)) {
2728 * Find or create the client list of this condition. It may
2729 * already be present if another trigger is already registered
2730 * with the same condition.
2732 client_list
= get_client_list_from_condition(state
, condition
);
2735 * No client list for this condition yet. We create new
2736 * one and build it up.
2738 client_list
= notification_client_list_create(state
, condition
);
2740 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2741 goto error_free_ht_element
;
2745 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2747 pthread_mutex_lock(&client_list
->lock
);
2748 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2749 pthread_mutex_unlock(&client_list
->lock
);
2753 * Ownership of the trigger and of its wrapper was transfered to
2754 * the triggers_ht. Same for token ht element if necessary.
2756 trigger_ht_element
= NULL
;
2757 free_trigger
= false;
2759 switch (get_condition_binding_object(condition
)) {
2760 case LTTNG_OBJECT_TYPE_SESSION
:
2761 /* Add the trigger to the list if it matches a known session. */
2762 ret
= bind_trigger_to_matching_session(trigger
, state
);
2764 goto error_free_ht_element
;
2767 case LTTNG_OBJECT_TYPE_CHANNEL
:
2769 * Add the trigger to list of triggers bound to the channels
2772 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2774 goto error_free_ht_element
;
2777 case LTTNG_OBJECT_TYPE_NONE
:
2780 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2782 goto error_free_ht_element
;
2786 * The new trigger's condition must be evaluated against the current
2789 * In the case of `notify` action, nothing preventing clients from
2790 * subscribing to a condition before the corresponding trigger is
2791 * registered, we have to evaluate this new condition right away.
2793 * At some point, we were waiting for the next "evaluation" (e.g. on
2794 * reception of a channel sample) to evaluate this new condition, but
2797 * The reason it was broken is that waiting for the next sample
2798 * does not allow us to properly handle transitions for edge-triggered
2801 * Consider this example: when we handle a new channel sample, we
2802 * evaluate each conditions twice: once with the previous state, and
2803 * again with the newest state. We then use those two results to
2804 * determine whether a state change happened: a condition was false and
2805 * became true. If a state change happened, we have to notify clients.
2807 * Now, if a client subscribes to a given notification and registers
2808 * a trigger *after* that subscription, we have to make sure the
2809 * condition is evaluated at this point while considering only the
2810 * current state. Otherwise, the next evaluation cycle may only see
2811 * that the evaluations remain the same (true for samples n-1 and n) and
2812 * the client will never know that the condition has been met.
2814 switch (get_condition_binding_object(condition
)) {
2815 case LTTNG_OBJECT_TYPE_SESSION
:
2816 ret
= evaluate_session_condition_for_client(condition
, state
,
2817 &evaluation
, &object_uid
,
2819 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2820 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2822 case LTTNG_OBJECT_TYPE_CHANNEL
:
2823 ret
= evaluate_channel_condition_for_client(condition
, state
,
2824 &evaluation
, &object_uid
,
2826 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2827 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2829 case LTTNG_OBJECT_TYPE_NONE
:
2832 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2840 goto error_free_ht_element
;
2843 DBG("Newly registered trigger's condition evaluated to %s",
2844 evaluation
? "true" : "false");
2846 /* Evaluation yielded nothing. Normal exit. */
2852 * Ownership of `evaluation` transferred to the action executor
2853 * no matter the result.
2855 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2856 evaluation
, &object_creds
, client_list
);
2858 switch (executor_status
) {
2859 case ACTION_EXECUTOR_STATUS_OK
:
2861 case ACTION_EXECUTOR_STATUS_ERROR
:
2862 case ACTION_EXECUTOR_STATUS_INVALID
:
2864 * TODO Add trigger identification (name/id) when
2865 * it is added to the API.
2867 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2869 goto error_free_ht_element
;
2870 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2872 * TODO Add trigger identification (name/id) when
2873 * it is added to the API.
2875 * Not a fatal error.
2877 WARN("No space left when enqueuing action associated to newly registered trigger");
2885 *cmd_result
= LTTNG_OK
;
2886 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2887 trigger_name
, trigger_tracer_token
);
2890 error_free_ht_element
:
2891 if (trigger_ht_element
) {
2892 /* Delayed removal due to RCU constraint on delete. */
2893 call_rcu(&trigger_ht_element
->rcu_node
,
2894 free_lttng_trigger_ht_element_rcu
);
2899 * Other objects might have a reference to the trigger, mark it
2902 lttng_trigger_set_as_unregistered(trigger
);
2903 lttng_trigger_destroy(trigger
);
2911 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2913 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2918 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2920 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2925 void teardown_tracer_notifier(struct notification_thread_state
*state
,
2926 const struct lttng_trigger
*trigger
)
2928 struct cds_lfht_iter iter
;
2929 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
2931 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
2932 trigger_tokens_ht_element
, node
) {
2934 if (!lttng_trigger_is_equal(trigger
,
2935 trigger_tokens_ht_element
->trigger
)) {
2939 event_notifier_error_accounting_unregister_event_notifier(
2940 trigger_tokens_ht_element
->trigger
);
2942 /* TODO talk to all app and remove it */
2943 DBG("[notification-thread] Removed trigger from tokens_ht");
2944 cds_lfht_del(state
->trigger_tokens_ht
,
2945 &trigger_tokens_ht_element
->node
);
2947 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2948 free_notification_trigger_tokens_ht_element_rcu
);
2955 int handle_notification_thread_command_unregister_trigger(
2956 struct notification_thread_state
*state
,
2957 const struct lttng_trigger
*trigger
,
2958 enum lttng_error_code
*_cmd_reply
)
2960 struct cds_lfht_iter iter
;
2961 struct cds_lfht_node
*triggers_ht_node
;
2962 struct lttng_channel_trigger_list
*trigger_list
;
2963 struct notification_client_list
*client_list
;
2964 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2965 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
2967 enum lttng_error_code cmd_reply
;
2971 cds_lfht_lookup(state
->triggers_ht
,
2972 lttng_condition_hash(condition
),
2976 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2977 if (!triggers_ht_node
) {
2978 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2981 cmd_reply
= LTTNG_OK
;
2984 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2985 struct lttng_trigger_ht_element
, node
);
2987 /* From this point, consider the trigger unregistered no matter what. */
2988 lttng_trigger_set_as_unregistered(trigger_ht_element
->trigger
);
2990 /* Remove trigger from channel_triggers_ht. */
2991 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2992 channel_triggers_ht_node
) {
2993 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2995 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2996 &trigger_list
->list
, node
) {
2997 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3001 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
3002 cds_list_del(&trigger_element
->node
);
3003 /* A trigger can only appear once per channel */
3008 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3009 teardown_tracer_notifier(state
, trigger
);
3012 if (is_trigger_action_notify(trigger
)) {
3014 * Remove and release the client list from
3015 * notification_trigger_clients_ht.
3017 client_list
= get_client_list_from_condition(state
, condition
);
3018 assert(client_list
);
3020 pthread_mutex_lock(&client_list
->lock
);
3021 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3022 pthread_mutex_unlock(&client_list
->lock
);
3024 /* Put new reference and the hashtable's reference. */
3025 notification_client_list_put(client_list
);
3026 notification_client_list_put(client_list
);
3030 /* Remove trigger from triggers_ht. */
3031 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3033 /* Release the ownership of the trigger. */
3034 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3035 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3039 *_cmd_reply
= cmd_reply
;
3044 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3045 int handle_notification_thread_command(
3046 struct notification_thread_handle
*handle
,
3047 struct notification_thread_state
*state
)
3051 struct notification_thread_command
*cmd
;
3053 /* Read the event pipe to put it back into a quiescent state. */
3054 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
3056 if (ret
!= sizeof(counter
)) {