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/event-rule-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 "notification-thread.h"
45 #include "notification-thread-events.h"
46 #include "notification-thread-commands.h"
47 #include "lttng-sessiond.h"
50 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
51 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
53 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
54 #define MAX_CAPTURE_SIZE (PIPE_BUF)
56 enum lttng_object_type
{
57 LTTNG_OBJECT_TYPE_UNKNOWN
,
58 LTTNG_OBJECT_TYPE_NONE
,
59 LTTNG_OBJECT_TYPE_CHANNEL
,
60 LTTNG_OBJECT_TYPE_SESSION
,
63 struct lttng_trigger_list_element
{
64 /* No ownership of the trigger object is assumed. */
65 struct lttng_trigger
*trigger
;
66 struct cds_list_head node
;
69 struct lttng_channel_trigger_list
{
70 struct channel_key channel_key
;
71 /* List of struct lttng_trigger_list_element. */
72 struct cds_list_head list
;
73 /* Node in the channel_triggers_ht */
74 struct cds_lfht_node channel_triggers_ht_node
;
75 /* call_rcu delayed reclaim. */
76 struct rcu_head rcu_node
;
80 * List of triggers applying to a given session.
83 * - lttng_session_trigger_list_create()
84 * - lttng_session_trigger_list_build()
85 * - lttng_session_trigger_list_destroy()
86 * - lttng_session_trigger_list_add()
88 struct lttng_session_trigger_list
{
90 * Not owned by this; points to the session_info structure's
93 const char *session_name
;
94 /* List of struct lttng_trigger_list_element. */
95 struct cds_list_head list
;
96 /* Node in the session_triggers_ht */
97 struct cds_lfht_node session_triggers_ht_node
;
99 * Weak reference to the notification system's session triggers
102 * The session trigger list structure structure is owned by
103 * the session's session_info.
105 * The session_info is kept alive the the channel_infos holding a
106 * reference to it (reference counting). When those channels are
107 * destroyed (at runtime or on teardown), the reference they hold
108 * to the session_info are released. On destruction of session_info,
109 * session_info_destroy() will remove the list of triggers applying
110 * to this session from the notification system's state.
112 * This implies that the session_triggers_ht must be destroyed
113 * after the channels.
115 struct cds_lfht
*session_triggers_ht
;
116 /* Used for delayed RCU reclaim. */
117 struct rcu_head rcu_node
;
120 struct lttng_trigger_ht_element
{
121 struct lttng_trigger
*trigger
;
122 struct cds_lfht_node node
;
123 struct cds_lfht_node node_by_name_uid
;
124 /* call_rcu delayed reclaim. */
125 struct rcu_head rcu_node
;
128 struct lttng_condition_list_element
{
129 struct lttng_condition
*condition
;
130 struct cds_list_head node
;
133 struct channel_state_sample
{
134 struct channel_key key
;
135 struct cds_lfht_node channel_state_ht_node
;
136 uint64_t highest_usage
;
137 uint64_t lowest_usage
;
138 uint64_t channel_total_consumed
;
139 /* call_rcu delayed reclaim. */
140 struct rcu_head rcu_node
;
143 static unsigned long hash_channel_key(struct channel_key
*key
);
144 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
145 struct lttng_evaluation
**evaluation
,
146 const struct notification_thread_state
*state
,
147 const struct channel_state_sample
*previous_sample
,
148 const struct channel_state_sample
*latest_sample
,
149 uint64_t previous_session_consumed_total
,
150 uint64_t latest_session_consumed_total
,
151 struct channel_info
*channel_info
);
153 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
154 const struct lttng_evaluation
*evaluation
,
155 struct notification_client_list
*client_list
,
156 struct notification_thread_state
*state
,
157 uid_t channel_uid
, gid_t channel_gid
);
160 /* session_info API */
162 void session_info_destroy(void *_data
);
164 void session_info_get(struct session_info
*session_info
);
166 void session_info_put(struct session_info
*session_info
);
168 struct session_info
*session_info_create(const char *name
,
169 uid_t uid
, gid_t gid
,
170 struct lttng_session_trigger_list
*trigger_list
,
171 struct cds_lfht
*sessions_ht
);
173 void session_info_add_channel(struct session_info
*session_info
,
174 struct channel_info
*channel_info
);
176 void session_info_remove_channel(struct session_info
*session_info
,
177 struct channel_info
*channel_info
);
179 /* lttng_session_trigger_list API */
181 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
182 const char *session_name
,
183 struct cds_lfht
*session_triggers_ht
);
185 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
186 const struct notification_thread_state
*state
,
187 const char *session_name
);
189 void lttng_session_trigger_list_destroy(
190 struct lttng_session_trigger_list
*list
);
192 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
193 struct lttng_trigger
*trigger
);
196 int client_handle_transmission_status(
197 struct notification_client
*client
,
198 enum client_transmission_status transmission_status
,
199 struct notification_thread_state
*state
);
202 int handle_one_event_notifier_notification(
203 struct notification_thread_state
*state
,
204 int pipe
, enum lttng_domain_type domain
);
207 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
210 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
212 /* This double-cast is intended to supress pointer-to-cast warning. */
213 const int socket
= (int) (intptr_t) key
;
214 const struct notification_client
*client
= caa_container_of(node
,
215 struct notification_client
, client_socket_ht_node
);
217 return client
->socket
== socket
;
221 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
223 /* This double-cast is intended to supress pointer-to-cast warning. */
224 const notification_client_id id
= *((notification_client_id
*) key
);
225 const struct notification_client
*client
= caa_container_of(
226 node
, struct notification_client
, client_id_ht_node
);
228 return client
->id
== id
;
232 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
234 struct channel_key
*channel_key
= (struct channel_key
*) key
;
235 struct lttng_channel_trigger_list
*trigger_list
;
237 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
238 channel_triggers_ht_node
);
240 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
241 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
245 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
247 const char *session_name
= (const char *) key
;
248 struct lttng_session_trigger_list
*trigger_list
;
250 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
251 session_triggers_ht_node
);
253 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
257 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
259 struct channel_key
*channel_key
= (struct channel_key
*) key
;
260 struct channel_state_sample
*sample
;
262 sample
= caa_container_of(node
, struct channel_state_sample
,
263 channel_state_ht_node
);
265 return !!((channel_key
->key
== sample
->key
.key
) &&
266 (channel_key
->domain
== sample
->key
.domain
));
270 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
272 struct channel_key
*channel_key
= (struct channel_key
*) key
;
273 struct channel_info
*channel_info
;
275 channel_info
= caa_container_of(node
, struct channel_info
,
278 return !!((channel_key
->key
== channel_info
->key
.key
) &&
279 (channel_key
->domain
== channel_info
->key
.domain
));
283 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
285 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
286 struct lttng_trigger_ht_element
*trigger_ht_element
;
288 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
291 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
295 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
297 const uint64_t *_key
= key
;
298 struct notification_trigger_tokens_ht_element
*element
;
300 element
= caa_container_of(node
,
301 struct notification_trigger_tokens_ht_element
, node
);
302 return *_key
== element
->token
;
306 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
308 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
309 struct notification_client_list
*client_list
;
310 const struct lttng_condition
*condition
;
312 assert(condition_key
);
314 client_list
= caa_container_of(node
, struct notification_client_list
,
315 notification_trigger_clients_ht_node
);
316 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
318 return !!lttng_condition_is_equal(condition_key
, condition
);
322 int match_session(struct cds_lfht_node
*node
, const void *key
)
324 const char *name
= key
;
325 struct session_info
*session_info
= caa_container_of(
326 node
, struct session_info
, sessions_ht_node
);
328 return !strcmp(session_info
->name
, name
);
332 const char *notification_command_type_str(
333 enum notification_thread_command_type type
)
336 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
337 return "REGISTER_TRIGGER";
338 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
339 return "UNREGISTER_TRIGGER";
340 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
341 return "ADD_CHANNEL";
342 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
343 return "REMOVE_CHANNEL";
344 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
345 return "SESSION_ROTATION_ONGOING";
346 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
347 return "SESSION_ROTATION_COMPLETED";
348 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
349 return "ADD_TRACER_EVENT_SOURCE";
350 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
351 return "REMOVE_TRACER_EVENT_SOURCE";
352 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
353 return "LIST_TRIGGERS";
354 case NOTIFICATION_COMMAND_TYPE_QUIT
:
356 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
357 return "CLIENT_COMMUNICATION_UPDATE";
364 * Match trigger based on name and credentials only.
365 * Name duplication is NOT allowed for the same uid.
368 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
373 const char *key_name
;
374 enum lttng_trigger_status status
;
375 const struct lttng_credentials
*key_creds
;
376 const struct lttng_credentials
*node_creds
;
377 const struct lttng_trigger
*trigger_key
=
378 (const struct lttng_trigger
*) key
;
379 const struct lttng_trigger_ht_element
*trigger_ht_element
=
380 caa_container_of(node
,
381 struct lttng_trigger_ht_element
,
384 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
385 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
387 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
388 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
390 /* Compare the names. */
391 if (strcmp(name
, key_name
) != 0) {
395 /* Compare the owners' UIDs. */
396 key_creds
= lttng_trigger_get_credentials(trigger_key
);
397 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
399 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
406 * Hash trigger based on name and credentials only.
409 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
411 unsigned long hash
= 0;
412 const struct lttng_credentials
*trigger_creds
;
413 const char *trigger_name
;
414 enum lttng_trigger_status status
;
416 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
417 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
418 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
421 trigger_creds
= lttng_trigger_get_credentials(trigger
);
422 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
429 unsigned long hash_channel_key(struct channel_key
*key
)
431 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
432 unsigned long domain_hash
= hash_key_ulong(
433 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
435 return key_hash
^ domain_hash
;
439 unsigned long hash_client_socket(int socket
)
441 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
445 unsigned long hash_client_id(notification_client_id id
)
447 return hash_key_u64(&id
, lttng_ht_seed
);
451 * Get the type of object to which a given condition applies. Bindings let
452 * the notification system evaluate a trigger's condition when a given
453 * object's state is updated.
455 * For instance, a condition bound to a channel will be evaluated everytime
456 * the channel's state is changed by a channel monitoring sample.
459 enum lttng_object_type
get_condition_binding_object(
460 const struct lttng_condition
*condition
)
462 switch (lttng_condition_get_type(condition
)) {
463 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
464 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
465 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
466 return LTTNG_OBJECT_TYPE_CHANNEL
;
467 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
468 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
469 return LTTNG_OBJECT_TYPE_SESSION
;
470 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
471 return LTTNG_OBJECT_TYPE_NONE
;
473 return LTTNG_OBJECT_TYPE_UNKNOWN
;
478 void free_channel_info_rcu(struct rcu_head
*node
)
480 free(caa_container_of(node
, struct channel_info
, rcu_node
));
484 void channel_info_destroy(struct channel_info
*channel_info
)
490 if (channel_info
->session_info
) {
491 session_info_remove_channel(channel_info
->session_info
,
493 session_info_put(channel_info
->session_info
);
495 if (channel_info
->name
) {
496 free(channel_info
->name
);
498 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
502 void free_session_info_rcu(struct rcu_head
*node
)
504 free(caa_container_of(node
, struct session_info
, rcu_node
));
507 /* Don't call directly, use the ref-counting mechanism. */
509 void session_info_destroy(void *_data
)
511 struct session_info
*session_info
= _data
;
514 assert(session_info
);
515 if (session_info
->channel_infos_ht
) {
516 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
518 ERR("[notification-thread] Failed to destroy channel information hash table");
521 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
524 cds_lfht_del(session_info
->sessions_ht
,
525 &session_info
->sessions_ht_node
);
527 free(session_info
->name
);
528 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
532 void session_info_get(struct session_info
*session_info
)
537 lttng_ref_get(&session_info
->ref
);
541 void session_info_put(struct session_info
*session_info
)
546 lttng_ref_put(&session_info
->ref
);
550 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
551 struct lttng_session_trigger_list
*trigger_list
,
552 struct cds_lfht
*sessions_ht
)
554 struct session_info
*session_info
;
558 session_info
= zmalloc(sizeof(*session_info
));
562 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
564 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
565 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
566 if (!session_info
->channel_infos_ht
) {
570 cds_lfht_node_init(&session_info
->sessions_ht_node
);
571 session_info
->name
= strdup(name
);
572 if (!session_info
->name
) {
575 session_info
->uid
= uid
;
576 session_info
->gid
= gid
;
577 session_info
->trigger_list
= trigger_list
;
578 session_info
->sessions_ht
= sessions_ht
;
582 session_info_put(session_info
);
587 void session_info_add_channel(struct session_info
*session_info
,
588 struct channel_info
*channel_info
)
591 cds_lfht_add(session_info
->channel_infos_ht
,
592 hash_channel_key(&channel_info
->key
),
593 &channel_info
->session_info_channels_ht_node
);
598 void session_info_remove_channel(struct session_info
*session_info
,
599 struct channel_info
*channel_info
)
602 cds_lfht_del(session_info
->channel_infos_ht
,
603 &channel_info
->session_info_channels_ht_node
);
608 struct channel_info
*channel_info_create(const char *channel_name
,
609 struct channel_key
*channel_key
, uint64_t channel_capacity
,
610 struct session_info
*session_info
)
612 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
618 cds_lfht_node_init(&channel_info
->channels_ht_node
);
619 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
620 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
621 channel_info
->capacity
= channel_capacity
;
623 channel_info
->name
= strdup(channel_name
);
624 if (!channel_info
->name
) {
629 * Set the references between session and channel infos:
630 * - channel_info holds a strong reference to session_info
631 * - session_info holds a weak reference to channel_info
633 session_info_get(session_info
);
634 session_info_add_channel(session_info
, channel_info
);
635 channel_info
->session_info
= session_info
;
639 channel_info_destroy(channel_info
);
644 bool notification_client_list_get(struct notification_client_list
*list
)
646 return urcu_ref_get_unless_zero(&list
->ref
);
650 void free_notification_client_list_rcu(struct rcu_head
*node
)
652 free(caa_container_of(node
, struct notification_client_list
,
657 void notification_client_list_release(struct urcu_ref
*list_ref
)
659 struct notification_client_list
*list
=
660 container_of(list_ref
, typeof(*list
), ref
);
661 struct notification_client_list_element
*client_list_element
, *tmp
;
663 if (list
->notification_trigger_clients_ht
) {
665 cds_lfht_del(list
->notification_trigger_clients_ht
,
666 &list
->notification_trigger_clients_ht_node
);
668 list
->notification_trigger_clients_ht
= NULL
;
670 cds_list_for_each_entry_safe(client_list_element
, tmp
,
672 free(client_list_element
);
674 pthread_mutex_destroy(&list
->lock
);
675 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
679 struct notification_client_list
*notification_client_list_create(
680 const struct lttng_trigger
*trigger
)
682 struct notification_client_list
*client_list
=
683 zmalloc(sizeof(*client_list
));
688 pthread_mutex_init(&client_list
->lock
, NULL
);
689 urcu_ref_init(&client_list
->ref
);
690 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
691 CDS_INIT_LIST_HEAD(&client_list
->list
);
692 client_list
->trigger
= trigger
;
698 void publish_notification_client_list(
699 struct notification_thread_state
*state
,
700 struct notification_client_list
*list
)
702 const struct lttng_condition
*condition
=
703 lttng_trigger_get_const_condition(list
->trigger
);
705 assert(!list
->notification_trigger_clients_ht
);
706 notification_client_list_get(list
);
708 list
->notification_trigger_clients_ht
=
709 state
->notification_trigger_clients_ht
;
712 cds_lfht_add(state
->notification_trigger_clients_ht
,
713 lttng_condition_hash(condition
),
714 &list
->notification_trigger_clients_ht_node
);
719 void notification_client_list_put(struct notification_client_list
*list
)
724 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
727 /* Provides a reference to the returned list. */
729 struct notification_client_list
*get_client_list_from_condition(
730 struct notification_thread_state
*state
,
731 const struct lttng_condition
*condition
)
733 struct cds_lfht_node
*node
;
734 struct cds_lfht_iter iter
;
735 struct notification_client_list
*list
= NULL
;
738 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
739 lttng_condition_hash(condition
),
740 match_client_list_condition
,
743 node
= cds_lfht_iter_get_node(&iter
);
745 list
= container_of(node
, struct notification_client_list
,
746 notification_trigger_clients_ht_node
);
747 list
= notification_client_list_get(list
) ? list
: NULL
;
755 int evaluate_channel_condition_for_client(
756 const struct lttng_condition
*condition
,
757 struct notification_thread_state
*state
,
758 struct lttng_evaluation
**evaluation
,
759 uid_t
*session_uid
, gid_t
*session_gid
)
762 struct cds_lfht_iter iter
;
763 struct cds_lfht_node
*node
;
764 struct channel_info
*channel_info
= NULL
;
765 struct channel_key
*channel_key
= NULL
;
766 struct channel_state_sample
*last_sample
= NULL
;
767 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
771 /* Find the channel associated with the condition. */
772 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
773 channel_trigger_list
, channel_triggers_ht_node
) {
774 struct lttng_trigger_list_element
*element
;
776 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
777 const struct lttng_condition
*current_condition
=
778 lttng_trigger_get_const_condition(
781 assert(current_condition
);
782 if (!lttng_condition_is_equal(condition
,
783 current_condition
)) {
787 /* Found the trigger, save the channel key. */
788 channel_key
= &channel_trigger_list
->channel_key
;
792 /* The channel key was found stop iteration. */
798 /* No channel found; normal exit. */
799 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
804 /* Fetch channel info for the matching channel. */
805 cds_lfht_lookup(state
->channels_ht
,
806 hash_channel_key(channel_key
),
810 node
= cds_lfht_iter_get_node(&iter
);
812 channel_info
= caa_container_of(node
, struct channel_info
,
815 /* Retrieve the channel's last sample, if it exists. */
816 cds_lfht_lookup(state
->channel_state_ht
,
817 hash_channel_key(channel_key
),
818 match_channel_state_sample
,
821 node
= cds_lfht_iter_get_node(&iter
);
823 last_sample
= caa_container_of(node
,
824 struct channel_state_sample
,
825 channel_state_ht_node
);
827 /* Nothing to evaluate, no sample was ever taken. Normal exit */
828 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
833 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
835 0, channel_info
->session_info
->consumed_data_size
,
838 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
842 *session_uid
= channel_info
->session_info
->uid
;
843 *session_gid
= channel_info
->session_info
->gid
;
850 const char *get_condition_session_name(const struct lttng_condition
*condition
)
852 const char *session_name
= NULL
;
853 enum lttng_condition_status status
;
855 switch (lttng_condition_get_type(condition
)) {
856 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
857 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
858 status
= lttng_condition_buffer_usage_get_session_name(
859 condition
, &session_name
);
861 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
862 status
= lttng_condition_session_consumed_size_get_session_name(
863 condition
, &session_name
);
865 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
866 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
867 status
= lttng_condition_session_rotation_get_session_name(
868 condition
, &session_name
);
873 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
874 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
882 int evaluate_session_condition_for_client(
883 const struct lttng_condition
*condition
,
884 struct notification_thread_state
*state
,
885 struct lttng_evaluation
**evaluation
,
886 uid_t
*session_uid
, gid_t
*session_gid
)
889 struct cds_lfht_iter iter
;
890 struct cds_lfht_node
*node
;
891 const char *session_name
;
892 struct session_info
*session_info
= NULL
;
895 session_name
= get_condition_session_name(condition
);
897 /* Find the session associated with the trigger. */
898 cds_lfht_lookup(state
->sessions_ht
,
899 hash_key_str(session_name
, lttng_ht_seed
),
903 node
= cds_lfht_iter_get_node(&iter
);
905 DBG("[notification-thread] No known session matching name \"%s\"",
911 session_info
= caa_container_of(node
, struct session_info
,
913 session_info_get(session_info
);
916 * Evaluation is performed in-line here since only one type of
917 * session-bound condition is handled for the moment.
919 switch (lttng_condition_get_type(condition
)) {
920 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
921 if (!session_info
->rotation
.ongoing
) {
923 goto end_session_put
;
926 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
927 session_info
->rotation
.id
);
930 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
933 goto end_session_put
;
939 goto end_session_put
;
942 *session_uid
= session_info
->uid
;
943 *session_gid
= session_info
->gid
;
946 session_info_put(session_info
);
953 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
954 const struct lttng_condition
*condition
,
955 struct notification_client
*client
,
956 struct notification_thread_state
*state
)
959 struct lttng_evaluation
*evaluation
= NULL
;
960 struct notification_client_list client_list
= {
961 .lock
= PTHREAD_MUTEX_INITIALIZER
,
963 struct notification_client_list_element client_list_element
= { 0 };
964 uid_t object_uid
= 0;
965 gid_t object_gid
= 0;
972 switch (get_condition_binding_object(condition
)) {
973 case LTTNG_OBJECT_TYPE_SESSION
:
974 ret
= evaluate_session_condition_for_client(condition
, state
,
975 &evaluation
, &object_uid
, &object_gid
);
977 case LTTNG_OBJECT_TYPE_CHANNEL
:
978 ret
= evaluate_channel_condition_for_client(condition
, state
,
979 &evaluation
, &object_uid
, &object_gid
);
981 case LTTNG_OBJECT_TYPE_NONE
:
982 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
985 case LTTNG_OBJECT_TYPE_UNKNOWN
:
995 /* Evaluation yielded nothing. Normal exit. */
996 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
1002 * Create a temporary client list with the client currently
1005 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1006 CDS_INIT_LIST_HEAD(&client_list
.list
);
1007 client_list
.trigger
= trigger
;
1009 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1010 client_list_element
.client
= client
;
1011 cds_list_add(&client_list_element
.node
, &client_list
.list
);
1013 /* Send evaluation result to the newly-subscribed client. */
1014 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1015 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1016 state
, object_uid
, object_gid
);
1023 int notification_thread_client_subscribe(struct notification_client
*client
,
1024 struct lttng_condition
*condition
,
1025 struct notification_thread_state
*state
,
1026 enum lttng_notification_channel_status
*_status
)
1029 struct notification_client_list
*client_list
= NULL
;
1030 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1031 struct notification_client_list_element
*client_list_element
= NULL
;
1032 enum lttng_notification_channel_status status
=
1033 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1036 * Ensure that the client has not already subscribed to this condition
1039 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1040 if (lttng_condition_is_equal(condition_list_element
->condition
,
1042 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1047 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1048 if (!condition_list_element
) {
1052 client_list_element
= zmalloc(sizeof(*client_list_element
));
1053 if (!client_list_element
) {
1059 * Add the newly-subscribed condition to the client's subscription list.
1061 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1062 condition_list_element
->condition
= condition
;
1063 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1065 client_list
= get_client_list_from_condition(state
, condition
);
1068 * No notification-emiting trigger registered with this
1069 * condition. We don't evaluate the condition right away
1070 * since this trigger is not registered yet.
1072 free(client_list_element
);
1077 * The condition to which the client just subscribed is evaluated
1078 * at this point so that conditions that are already TRUE result
1079 * in a notification being sent out.
1081 * The client_list's trigger is used without locking the list itself.
1082 * This is correct since the list doesn't own the trigger and the
1083 * object is immutable.
1085 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1087 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1089 free(client_list_element
);
1094 * Add the client to the list of clients interested in a given trigger
1095 * if a "notification" trigger with a corresponding condition was
1098 client_list_element
->client
= client
;
1099 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1101 pthread_mutex_lock(&client_list
->lock
);
1102 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1103 pthread_mutex_unlock(&client_list
->lock
);
1109 notification_client_list_put(client_list
);
1113 free(condition_list_element
);
1114 free(client_list_element
);
1119 int notification_thread_client_unsubscribe(
1120 struct notification_client
*client
,
1121 struct lttng_condition
*condition
,
1122 struct notification_thread_state
*state
,
1123 enum lttng_notification_channel_status
*_status
)
1125 struct notification_client_list
*client_list
;
1126 struct lttng_condition_list_element
*condition_list_element
,
1128 struct notification_client_list_element
*client_list_element
,
1130 bool condition_found
= false;
1131 enum lttng_notification_channel_status status
=
1132 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1134 /* Remove the condition from the client's condition list. */
1135 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1136 &client
->condition_list
, node
) {
1137 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1142 cds_list_del(&condition_list_element
->node
);
1144 * The caller may be iterating on the client's conditions to
1145 * tear down a client's connection. In this case, the condition
1146 * will be destroyed at the end.
1148 if (condition
!= condition_list_element
->condition
) {
1149 lttng_condition_destroy(
1150 condition_list_element
->condition
);
1152 free(condition_list_element
);
1153 condition_found
= true;
1157 if (!condition_found
) {
1158 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1163 * Remove the client from the list of clients interested the trigger
1164 * matching the condition.
1166 client_list
= get_client_list_from_condition(state
, condition
);
1171 pthread_mutex_lock(&client_list
->lock
);
1172 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1173 &client_list
->list
, node
) {
1174 if (client_list_element
->client
->id
!= client
->id
) {
1177 cds_list_del(&client_list_element
->node
);
1178 free(client_list_element
);
1181 pthread_mutex_unlock(&client_list
->lock
);
1182 notification_client_list_put(client_list
);
1185 lttng_condition_destroy(condition
);
1193 void free_notification_client_rcu(struct rcu_head
*node
)
1195 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1199 void notification_client_destroy(struct notification_client
*client
,
1200 struct notification_thread_state
*state
)
1207 * The client object is not reachable by other threads, no need to lock
1210 if (client
->socket
>= 0) {
1211 (void) lttcomm_close_unix_sock(client
->socket
);
1212 client
->socket
= -1;
1214 client
->communication
.active
= false;
1215 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1216 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1217 pthread_mutex_destroy(&client
->lock
);
1218 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1222 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1226 struct notification_client
*get_client_from_socket(int socket
,
1227 struct notification_thread_state
*state
)
1229 struct cds_lfht_iter iter
;
1230 struct cds_lfht_node
*node
;
1231 struct notification_client
*client
= NULL
;
1233 cds_lfht_lookup(state
->client_socket_ht
,
1234 hash_client_socket(socket
),
1235 match_client_socket
,
1236 (void *) (unsigned long) socket
,
1238 node
= cds_lfht_iter_get_node(&iter
);
1243 client
= caa_container_of(node
, struct notification_client
,
1244 client_socket_ht_node
);
1250 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1254 struct notification_client
*get_client_from_id(notification_client_id id
,
1255 struct notification_thread_state
*state
)
1257 struct cds_lfht_iter iter
;
1258 struct cds_lfht_node
*node
;
1259 struct notification_client
*client
= NULL
;
1261 cds_lfht_lookup(state
->client_id_ht
,
1266 node
= cds_lfht_iter_get_node(&iter
);
1271 client
= caa_container_of(node
, struct notification_client
,
1278 bool buffer_usage_condition_applies_to_channel(
1279 const struct lttng_condition
*condition
,
1280 const struct channel_info
*channel_info
)
1282 enum lttng_condition_status status
;
1283 enum lttng_domain_type condition_domain
;
1284 const char *condition_session_name
= NULL
;
1285 const char *condition_channel_name
= NULL
;
1287 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1289 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1290 if (channel_info
->key
.domain
!= condition_domain
) {
1294 status
= lttng_condition_buffer_usage_get_session_name(
1295 condition
, &condition_session_name
);
1296 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1298 status
= lttng_condition_buffer_usage_get_channel_name(
1299 condition
, &condition_channel_name
);
1300 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1302 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1305 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1315 bool session_consumed_size_condition_applies_to_channel(
1316 const struct lttng_condition
*condition
,
1317 const struct channel_info
*channel_info
)
1319 enum lttng_condition_status status
;
1320 const char *condition_session_name
= NULL
;
1322 status
= lttng_condition_session_consumed_size_get_session_name(
1323 condition
, &condition_session_name
);
1324 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1326 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1336 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1337 const struct channel_info
*channel_info
)
1339 const struct lttng_condition
*condition
;
1340 bool trigger_applies
;
1342 condition
= lttng_trigger_get_const_condition(trigger
);
1347 switch (lttng_condition_get_type(condition
)) {
1348 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1349 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1350 trigger_applies
= buffer_usage_condition_applies_to_channel(
1351 condition
, channel_info
);
1353 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1354 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1355 condition
, channel_info
);
1361 return trigger_applies
;
1367 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1368 struct notification_client
*client
)
1370 bool applies
= false;
1371 struct lttng_condition_list_element
*condition_list_element
;
1373 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1375 applies
= lttng_condition_is_equal(
1376 condition_list_element
->condition
,
1377 lttng_trigger_get_condition(trigger
));
1385 /* Must be called with RCU read lock held. */
1387 struct lttng_session_trigger_list
*get_session_trigger_list(
1388 struct notification_thread_state
*state
,
1389 const char *session_name
)
1391 struct lttng_session_trigger_list
*list
= NULL
;
1392 struct cds_lfht_node
*node
;
1393 struct cds_lfht_iter iter
;
1395 cds_lfht_lookup(state
->session_triggers_ht
,
1396 hash_key_str(session_name
, lttng_ht_seed
),
1397 match_session_trigger_list
,
1400 node
= cds_lfht_iter_get_node(&iter
);
1403 * Not an error, the list of triggers applying to that session
1404 * will be initialized when the session is created.
1406 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1411 list
= caa_container_of(node
,
1412 struct lttng_session_trigger_list
,
1413 session_triggers_ht_node
);
1419 * Allocate an empty lttng_session_trigger_list for the session named
1422 * No ownership of 'session_name' is assumed by the session trigger list.
1423 * It is the caller's responsability to ensure the session name is alive
1424 * for as long as this list is.
1427 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1428 const char *session_name
,
1429 struct cds_lfht
*session_triggers_ht
)
1431 struct lttng_session_trigger_list
*list
;
1433 list
= zmalloc(sizeof(*list
));
1437 list
->session_name
= session_name
;
1438 CDS_INIT_LIST_HEAD(&list
->list
);
1439 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1440 list
->session_triggers_ht
= session_triggers_ht
;
1443 /* Publish the list through the session_triggers_ht. */
1444 cds_lfht_add(session_triggers_ht
,
1445 hash_key_str(session_name
, lttng_ht_seed
),
1446 &list
->session_triggers_ht_node
);
1453 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1455 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1460 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1462 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1464 /* Empty the list element by element, and then free the list itself. */
1465 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1466 &list
->list
, node
) {
1467 cds_list_del(&trigger_list_element
->node
);
1468 free(trigger_list_element
);
1471 /* Unpublish the list from the session_triggers_ht. */
1472 cds_lfht_del(list
->session_triggers_ht
,
1473 &list
->session_triggers_ht_node
);
1475 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1479 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1480 struct lttng_trigger
*trigger
)
1483 struct lttng_trigger_list_element
*new_element
=
1484 zmalloc(sizeof(*new_element
));
1490 CDS_INIT_LIST_HEAD(&new_element
->node
);
1491 new_element
->trigger
= trigger
;
1492 cds_list_add(&new_element
->node
, &list
->list
);
1498 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1499 const char *session_name
)
1501 bool applies
= false;
1502 const struct lttng_condition
*condition
;
1504 condition
= lttng_trigger_get_const_condition(trigger
);
1505 switch (lttng_condition_get_type(condition
)) {
1506 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1507 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1509 enum lttng_condition_status condition_status
;
1510 const char *condition_session_name
;
1512 condition_status
= lttng_condition_session_rotation_get_session_name(
1513 condition
, &condition_session_name
);
1514 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1515 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1519 assert(condition_session_name
);
1520 applies
= !strcmp(condition_session_name
, session_name
);
1531 * Allocate and initialize an lttng_session_trigger_list which contains
1532 * all triggers that apply to the session named 'session_name'.
1534 * No ownership of 'session_name' is assumed by the session trigger list.
1535 * It is the caller's responsability to ensure the session name is alive
1536 * for as long as this list is.
1539 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1540 const struct notification_thread_state
*state
,
1541 const char *session_name
)
1543 int trigger_count
= 0;
1544 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1545 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1546 struct cds_lfht_iter iter
;
1548 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1549 state
->session_triggers_ht
);
1551 /* Add all triggers applying to the session named 'session_name'. */
1552 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1556 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1561 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1562 trigger_ht_element
->trigger
);
1570 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1572 return session_trigger_list
;
1574 lttng_session_trigger_list_destroy(session_trigger_list
);
1579 struct session_info
*find_or_create_session_info(
1580 struct notification_thread_state
*state
,
1581 const char *name
, uid_t uid
, gid_t gid
)
1583 struct session_info
*session
= NULL
;
1584 struct cds_lfht_node
*node
;
1585 struct cds_lfht_iter iter
;
1586 struct lttng_session_trigger_list
*trigger_list
;
1589 cds_lfht_lookup(state
->sessions_ht
,
1590 hash_key_str(name
, lttng_ht_seed
),
1594 node
= cds_lfht_iter_get_node(&iter
);
1596 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1598 session
= caa_container_of(node
, struct session_info
,
1600 assert(session
->uid
== uid
);
1601 assert(session
->gid
== gid
);
1602 session_info_get(session
);
1606 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1607 if (!trigger_list
) {
1611 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1612 state
->sessions_ht
);
1614 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1616 lttng_session_trigger_list_destroy(trigger_list
);
1619 trigger_list
= NULL
;
1621 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1622 &session
->sessions_ht_node
);
1628 session_info_put(session
);
1633 int handle_notification_thread_command_add_channel(
1634 struct notification_thread_state
*state
,
1635 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1636 const char *channel_name
, enum lttng_domain_type channel_domain
,
1637 uint64_t channel_key_int
, uint64_t channel_capacity
,
1638 enum lttng_error_code
*cmd_result
)
1640 struct cds_list_head trigger_list
;
1641 struct channel_info
*new_channel_info
= NULL
;
1642 struct channel_key channel_key
= {
1643 .key
= channel_key_int
,
1644 .domain
= channel_domain
,
1646 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1647 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1648 int trigger_count
= 0;
1649 struct cds_lfht_iter iter
;
1650 struct session_info
*session_info
= NULL
;
1652 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1653 channel_name
, session_name
, channel_key_int
,
1654 lttng_domain_type_str(channel_domain
));
1656 CDS_INIT_LIST_HEAD(&trigger_list
);
1658 session_info
= find_or_create_session_info(state
, session_name
,
1659 session_uid
, session_gid
);
1660 if (!session_info
) {
1661 /* Allocation error or an internal error occurred. */
1665 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1666 channel_capacity
, session_info
);
1667 if (!new_channel_info
) {
1672 /* Build a list of all triggers applying to the new channel. */
1673 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1675 struct lttng_trigger_list_element
*new_element
;
1677 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1678 new_channel_info
)) {
1682 new_element
= zmalloc(sizeof(*new_element
));
1687 CDS_INIT_LIST_HEAD(&new_element
->node
);
1688 new_element
->trigger
= trigger_ht_element
->trigger
;
1689 cds_list_add(&new_element
->node
, &trigger_list
);
1694 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1696 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1697 if (!channel_trigger_list
) {
1700 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1701 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1702 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1703 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1706 /* Add channel to the channel_ht which owns the channel_infos. */
1707 cds_lfht_add(state
->channels_ht
,
1708 hash_channel_key(&new_channel_info
->key
),
1709 &new_channel_info
->channels_ht_node
);
1711 * Add the list of triggers associated with this channel to the
1712 * channel_triggers_ht.
1714 cds_lfht_add(state
->channel_triggers_ht
,
1715 hash_channel_key(&new_channel_info
->key
),
1716 &channel_trigger_list
->channel_triggers_ht_node
);
1718 session_info_put(session_info
);
1719 *cmd_result
= LTTNG_OK
;
1722 channel_info_destroy(new_channel_info
);
1723 session_info_put(session_info
);
1728 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1730 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1735 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1737 free(caa_container_of(node
, struct channel_state_sample
,
1742 int handle_notification_thread_command_remove_channel(
1743 struct notification_thread_state
*state
,
1744 uint64_t channel_key
, enum lttng_domain_type domain
,
1745 enum lttng_error_code
*cmd_result
)
1747 struct cds_lfht_node
*node
;
1748 struct cds_lfht_iter iter
;
1749 struct lttng_channel_trigger_list
*trigger_list
;
1750 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1751 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1752 struct channel_info
*channel_info
;
1754 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1755 channel_key
, lttng_domain_type_str(domain
));
1759 cds_lfht_lookup(state
->channel_triggers_ht
,
1760 hash_channel_key(&key
),
1761 match_channel_trigger_list
,
1764 node
= cds_lfht_iter_get_node(&iter
);
1766 * There is a severe internal error if we are being asked to remove a
1767 * channel that doesn't exist.
1770 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1774 /* Free the list of triggers associated with this channel. */
1775 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1776 channel_triggers_ht_node
);
1777 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1778 &trigger_list
->list
, node
) {
1779 cds_list_del(&trigger_list_element
->node
);
1780 free(trigger_list_element
);
1782 cds_lfht_del(state
->channel_triggers_ht
, node
);
1783 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1785 /* Free sampled channel state. */
1786 cds_lfht_lookup(state
->channel_state_ht
,
1787 hash_channel_key(&key
),
1788 match_channel_state_sample
,
1791 node
= cds_lfht_iter_get_node(&iter
);
1793 * This is expected to be NULL if the channel is destroyed before we
1794 * received a sample.
1797 struct channel_state_sample
*sample
= caa_container_of(node
,
1798 struct channel_state_sample
,
1799 channel_state_ht_node
);
1801 cds_lfht_del(state
->channel_state_ht
, node
);
1802 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1805 /* Remove the channel from the channels_ht and free it. */
1806 cds_lfht_lookup(state
->channels_ht
,
1807 hash_channel_key(&key
),
1811 node
= cds_lfht_iter_get_node(&iter
);
1813 channel_info
= caa_container_of(node
, struct channel_info
,
1815 cds_lfht_del(state
->channels_ht
, node
);
1816 channel_info_destroy(channel_info
);
1819 *cmd_result
= LTTNG_OK
;
1824 int handle_notification_thread_command_session_rotation(
1825 struct notification_thread_state
*state
,
1826 enum notification_thread_command_type cmd_type
,
1827 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1828 uint64_t trace_archive_chunk_id
,
1829 struct lttng_trace_archive_location
*location
,
1830 enum lttng_error_code
*_cmd_result
)
1833 enum lttng_error_code cmd_result
= LTTNG_OK
;
1834 struct lttng_session_trigger_list
*trigger_list
;
1835 struct lttng_trigger_list_element
*trigger_list_element
;
1836 struct session_info
*session_info
;
1837 const struct lttng_credentials session_creds
= {
1838 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1839 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1844 session_info
= find_or_create_session_info(state
, session_name
,
1845 session_uid
, session_gid
);
1846 if (!session_info
) {
1847 /* Allocation error or an internal error occurred. */
1849 cmd_result
= LTTNG_ERR_NOMEM
;
1853 session_info
->rotation
.ongoing
=
1854 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1855 session_info
->rotation
.id
= trace_archive_chunk_id
;
1856 trigger_list
= get_session_trigger_list(state
, session_name
);
1857 if (!trigger_list
) {
1858 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1863 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1865 const struct lttng_condition
*condition
;
1866 struct lttng_trigger
*trigger
;
1867 struct notification_client_list
*client_list
;
1868 struct lttng_evaluation
*evaluation
= NULL
;
1869 enum lttng_condition_type condition_type
;
1870 enum action_executor_status executor_status
;
1872 trigger
= trigger_list_element
->trigger
;
1873 condition
= lttng_trigger_get_const_condition(trigger
);
1875 condition_type
= lttng_condition_get_type(condition
);
1877 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1878 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1880 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1881 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1885 client_list
= get_client_list_from_condition(state
, condition
);
1886 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1887 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1888 trace_archive_chunk_id
);
1890 evaluation
= lttng_evaluation_session_rotation_completed_create(
1891 trace_archive_chunk_id
, location
);
1895 /* Internal error */
1897 cmd_result
= LTTNG_ERR_UNK
;
1902 * Ownership of `evaluation` transferred to the action executor
1903 * no matter the result.
1905 executor_status
= action_executor_enqueue(state
->executor
,
1906 trigger
, evaluation
, &session_creds
,
1909 switch (executor_status
) {
1910 case ACTION_EXECUTOR_STATUS_OK
:
1912 case ACTION_EXECUTOR_STATUS_ERROR
:
1913 case ACTION_EXECUTOR_STATUS_INVALID
:
1915 * TODO Add trigger identification (name/id) when
1916 * it is added to the API.
1918 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1921 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1923 * TODO Add trigger identification (name/id) when
1924 * it is added to the API.
1926 * Not a fatal error.
1928 WARN("No space left when enqueuing action associated with session rotation trigger");
1936 notification_client_list_put(client_list
);
1937 if (caa_unlikely(ret
)) {
1942 session_info_put(session_info
);
1943 *_cmd_result
= cmd_result
;
1949 int handle_notification_thread_command_add_tracer_event_source(
1950 struct notification_thread_state
*state
,
1951 int tracer_event_source_fd
,
1952 enum lttng_domain_type domain_type
,
1953 enum lttng_error_code
*_cmd_result
)
1956 enum lttng_error_code cmd_result
= LTTNG_OK
;
1957 struct notification_event_tracer_event_source_element
*element
= NULL
;
1959 element
= zmalloc(sizeof(*element
));
1961 cmd_result
= LTTNG_ERR_NOMEM
;
1966 element
->fd
= tracer_event_source_fd
;
1967 element
->domain
= domain_type
;
1969 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
1971 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
1972 tracer_event_source_fd
,
1973 lttng_domain_type_str(domain_type
));
1975 /* Adding the read side pipe to the event poll. */
1976 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
1978 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
1979 tracer_event_source_fd
,
1980 lttng_domain_type_str(element
->domain
));
1981 cds_list_del(&element
->node
);
1986 element
->is_fd_in_poll_set
= true;
1989 *_cmd_result
= cmd_result
;
1994 int drain_event_notifier_notification_pipe(
1995 struct notification_thread_state
*state
,
1996 int pipe
, enum lttng_domain_type domain
)
1998 struct lttng_poll_event events
= {0};
2001 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2003 ERR("[notification-thread] Error creating lttng_poll_event");
2007 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2009 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2016 * Continue to consume notifications as long as there are new
2017 * ones coming in. The tracer has been asked to stop producing
2020 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2021 * monitored (on Linux, at least) and will be returned when
2022 * the pipe is closed but empty.
2024 ret
= lttng_poll_wait_interruptible(&events
, 0);
2025 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2026 /* No more notification to be read on this pipe. */
2029 } else if (ret
< 0) {
2030 PERROR("Failed on lttng_poll_wait_interruptible() call");
2035 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2037 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2042 lttng_poll_clean(&events
);
2047 int handle_notification_thread_command_remove_tracer_event_source(
2048 struct notification_thread_state
*state
,
2049 int tracer_event_source_fd
,
2050 enum lttng_error_code
*_cmd_result
)
2054 enum lttng_error_code cmd_result
= LTTNG_OK
;
2055 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
2057 cds_list_for_each_entry_safe(source_element
, tmp
,
2058 &state
->tracer_event_sources_list
, node
) {
2059 if (source_element
->fd
!= tracer_event_source_fd
) {
2063 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2064 tracer_event_source_fd
,
2065 lttng_domain_type_str(source_element
->domain
));
2066 cds_list_del(&source_element
->node
);
2073 * This is temporarily allowed since the poll activity set is
2074 * not properly cleaned-up for the moment. This is adressed in
2077 source_element
= NULL
;
2081 if (!source_element
->is_fd_in_poll_set
) {
2082 /* Skip the poll set removal. */
2086 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2087 tracer_event_source_fd
,
2088 lttng_domain_type_str(source_element
->domain
));
2090 /* Removing the fd from the event poll set. */
2091 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2093 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2094 tracer_event_source_fd
,
2095 lttng_domain_type_str(source_element
->domain
));
2096 cmd_result
= LTTNG_ERR_FATAL
;
2100 source_element
->is_fd_in_poll_set
= false;
2102 ret
= drain_event_notifier_notification_pipe(state
, tracer_event_source_fd
,
2103 source_element
->domain
);
2105 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2106 tracer_event_source_fd
,
2107 lttng_domain_type_str(source_element
->domain
));
2108 cmd_result
= LTTNG_ERR_FATAL
;
2113 * The drain_event_notifier_notification_pipe() call might have read
2114 * data from an fd that we received in event in the latest _poll_wait()
2115 * call. Make sure the thread call poll_wait() again to ensure we have
2118 state
->restart_poll
= true;
2121 free(source_element
);
2122 *_cmd_result
= cmd_result
;
2126 int handle_notification_thread_remove_tracer_event_source_no_result(
2127 struct notification_thread_state
*state
,
2128 int tracer_event_source_fd
)
2131 enum lttng_error_code cmd_result
;
2133 ret
= handle_notification_thread_command_remove_tracer_event_source(
2134 state
, tracer_event_source_fd
, &cmd_result
);
2139 static int handle_notification_thread_command_list_triggers(
2140 struct notification_thread_handle
*handle
,
2141 struct notification_thread_state
*state
,
2143 struct lttng_triggers
**triggers
,
2144 enum lttng_error_code
*_cmd_result
)
2147 enum lttng_error_code cmd_result
= LTTNG_OK
;
2148 struct cds_lfht_iter iter
;
2149 struct lttng_trigger_ht_element
*trigger_ht_element
;
2150 struct lttng_triggers
*local_triggers
= NULL
;
2151 const struct lttng_credentials
*creds
;
2155 local_triggers
= lttng_triggers_create();
2156 if (!local_triggers
) {
2157 /* Not a fatal error. */
2158 cmd_result
= LTTNG_ERR_NOMEM
;
2162 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2163 trigger_ht_element
, node
) {
2165 * Only return the triggers to which the client has access.
2166 * The root user has visibility over all triggers.
2168 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2169 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2173 ret
= lttng_triggers_add(local_triggers
,
2174 trigger_ht_element
->trigger
);
2176 /* Not a fatal error. */
2178 cmd_result
= LTTNG_ERR_NOMEM
;
2183 /* Transferring ownership to the caller. */
2184 *triggers
= local_triggers
;
2185 local_triggers
= NULL
;
2189 lttng_triggers_destroy(local_triggers
);
2190 *_cmd_result
= cmd_result
;
2195 bool condition_is_supported(struct lttng_condition
*condition
)
2199 switch (lttng_condition_get_type(condition
)) {
2200 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2201 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2204 enum lttng_domain_type domain
;
2206 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2210 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2211 is_supported
= true;
2216 * Older kernel tracers don't expose the API to monitor their
2217 * buffers. Therefore, we reject triggers that require that
2218 * mechanism to be available to be evaluated.
2220 * Assume unsupported on error.
2222 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2225 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
2227 const struct lttng_event_rule
*event_rule
;
2228 enum lttng_domain_type domain
;
2229 const enum lttng_condition_status status
=
2230 lttng_condition_event_rule_get_rule(
2231 condition
, &event_rule
);
2233 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2235 domain
= lttng_event_rule_get_domain_type(event_rule
);
2236 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2237 is_supported
= true;
2242 * Older kernel tracers can't emit notification. Therefore, we
2243 * reject triggers that require that mechanism to be available
2246 * Assume unsupported on error.
2248 is_supported
= kernel_supports_event_notifiers() == 1;
2252 is_supported
= true;
2255 return is_supported
;
2258 /* Must be called with RCU read lock held. */
2260 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2261 struct notification_thread_state
*state
)
2264 const struct lttng_condition
*condition
;
2265 const char *session_name
;
2266 struct lttng_session_trigger_list
*trigger_list
;
2268 condition
= lttng_trigger_get_const_condition(trigger
);
2269 switch (lttng_condition_get_type(condition
)) {
2270 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2271 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2273 enum lttng_condition_status status
;
2275 status
= lttng_condition_session_rotation_get_session_name(
2276 condition
, &session_name
);
2277 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2278 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2289 trigger_list
= get_session_trigger_list(state
, session_name
);
2290 if (!trigger_list
) {
2291 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2297 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2299 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2304 /* Must be called with RCU read lock held. */
2306 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2307 struct notification_thread_state
*state
)
2310 struct cds_lfht_node
*node
;
2311 struct cds_lfht_iter iter
;
2312 struct channel_info
*channel
;
2314 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2316 struct lttng_trigger_list_element
*trigger_list_element
;
2317 struct lttng_channel_trigger_list
*trigger_list
;
2318 struct cds_lfht_iter lookup_iter
;
2320 if (!trigger_applies_to_channel(trigger
, channel
)) {
2324 cds_lfht_lookup(state
->channel_triggers_ht
,
2325 hash_channel_key(&channel
->key
),
2326 match_channel_trigger_list
,
2329 node
= cds_lfht_iter_get_node(&lookup_iter
);
2331 trigger_list
= caa_container_of(node
,
2332 struct lttng_channel_trigger_list
,
2333 channel_triggers_ht_node
);
2335 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2336 if (!trigger_list_element
) {
2340 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2341 trigger_list_element
->trigger
= trigger
;
2342 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2343 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2351 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2353 bool is_notify
= false;
2354 unsigned int i
, count
;
2355 enum lttng_action_status action_status
;
2356 const struct lttng_action
*action
=
2357 lttng_trigger_get_const_action(trigger
);
2358 enum lttng_action_type action_type
;
2361 action_type
= lttng_action_get_type(action
);
2362 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2365 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2369 action_status
= lttng_action_group_get_count(action
, &count
);
2370 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2372 for (i
= 0; i
< count
; i
++) {
2373 const struct lttng_action
*inner_action
=
2374 lttng_action_group_get_at_index(
2377 action_type
= lttng_action_get_type(inner_action
);
2378 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2388 static bool trigger_name_taken(struct notification_thread_state
*state
,
2389 const struct lttng_trigger
*trigger
)
2391 struct cds_lfht_iter iter
;
2394 * No duplicata is allowed in the triggers_by_name_uid_ht.
2395 * The match is done against the trigger name and uid.
2397 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2398 hash_trigger_by_name_uid(trigger
),
2399 match_trigger_by_name_uid
,
2402 return !!cds_lfht_iter_get_node(&iter
);
2406 enum lttng_error_code
generate_trigger_name(
2407 struct notification_thread_state
*state
,
2408 struct lttng_trigger
*trigger
, const char **name
)
2410 enum lttng_error_code ret_code
= LTTNG_OK
;
2412 enum lttng_trigger_status status
;
2415 const int ret
= lttng_trigger_generate_name(trigger
,
2416 state
->trigger_id
.name_offset
++);
2418 /* The only reason this can fail right now. */
2419 ret_code
= LTTNG_ERR_NOMEM
;
2423 status
= lttng_trigger_get_name(trigger
, name
);
2424 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2426 taken
= trigger_name_taken(state
, trigger
);
2427 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2433 * FIXME A client's credentials are not checked when registering a trigger.
2435 * The effects of this are benign since:
2436 * - The client will succeed in registering the trigger, as it is valid,
2437 * - The trigger will, internally, be bound to the channel/session,
2438 * - The notifications will not be sent since the client's credentials
2439 * are checked against the channel at that moment.
2441 * If this function returns a non-zero value, it means something is
2442 * fundamentally broken and the whole subsystem/thread will be torn down.
2444 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2448 int handle_notification_thread_command_register_trigger(
2449 struct notification_thread_state
*state
,
2450 struct lttng_trigger
*trigger
,
2451 enum lttng_error_code
*cmd_result
)
2454 struct lttng_condition
*condition
;
2455 struct notification_client
*client
;
2456 struct notification_client_list
*client_list
= NULL
;
2457 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2458 struct notification_client_list_element
*client_list_element
;
2459 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2460 struct cds_lfht_node
*node
;
2461 struct cds_lfht_iter iter
;
2462 const char* trigger_name
;
2463 bool free_trigger
= true;
2464 struct lttng_evaluation
*evaluation
= NULL
;
2465 struct lttng_credentials object_creds
;
2468 enum action_executor_status executor_status
;
2469 const uint64_t trigger_tracer_token
=
2470 state
->trigger_id
.next_tracer_token
++;
2474 /* Set the trigger's tracer token. */
2475 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2477 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2478 LTTNG_TRIGGER_STATUS_UNSET
) {
2479 const enum lttng_error_code ret_code
= generate_trigger_name(
2480 state
, trigger
, &trigger_name
);
2482 if (ret_code
!= LTTNG_OK
) {
2485 *cmd_result
= ret_code
;
2488 } else if (trigger_name_taken(state
, trigger
)) {
2489 /* Not a fatal error. */
2490 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2495 condition
= lttng_trigger_get_condition(trigger
);
2498 /* Some conditions require tracers to implement a minimal ABI version. */
2499 if (!condition_is_supported(condition
)) {
2500 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2504 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2505 if (!trigger_ht_element
) {
2510 /* Add trigger to the trigger_ht. */
2511 cds_lfht_node_init(&trigger_ht_element
->node
);
2512 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2513 trigger_ht_element
->trigger
= trigger
;
2515 node
= cds_lfht_add_unique(state
->triggers_ht
,
2516 lttng_condition_hash(condition
),
2519 &trigger_ht_element
->node
);
2520 if (node
!= &trigger_ht_element
->node
) {
2521 /* Not a fatal error, simply report it to the client. */
2522 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2523 goto error_free_ht_element
;
2526 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2527 hash_trigger_by_name_uid(trigger
),
2528 match_trigger_by_name_uid
,
2530 &trigger_ht_element
->node_by_name_uid
);
2531 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2532 /* Not a fatal error, simply report it to the client. */
2533 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2534 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2535 goto error_free_ht_element
;
2538 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2539 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2540 if (!trigger_tokens_ht_element
) {
2543 cds_lfht_del(state
->triggers_ht
,
2544 &trigger_ht_element
->node
);
2545 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2546 &trigger_ht_element
->node_by_name_uid
);
2547 goto error_free_ht_element
;
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
=
2553 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
,
2559 match_trigger_token
,
2560 &trigger_tokens_ht_element
->token
,
2561 &trigger_tokens_ht_element
->node
);
2562 if (node
!= &trigger_tokens_ht_element
->node
) {
2563 /* Internal corruption, fatal error. */
2565 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2566 cds_lfht_del(state
->triggers_ht
,
2567 &trigger_ht_element
->node
);
2568 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2569 &trigger_ht_element
->node_by_name_uid
);
2570 goto error_free_ht_element
;
2575 * Ownership of the trigger and of its wrapper was transfered to
2576 * the triggers_ht. Same for token ht element if necessary.
2578 trigger_tokens_ht_element
= NULL
;
2579 trigger_ht_element
= NULL
;
2580 free_trigger
= false;
2583 * The rest only applies to triggers that have a "notify" action.
2584 * It is not skipped as this is the only action type currently
2587 if (is_trigger_action_notify(trigger
)) {
2588 client_list
= notification_client_list_create(trigger
);
2591 goto error_free_ht_element
;
2594 /* Build a list of clients to which this new trigger applies. */
2595 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2596 client_socket_ht_node
) {
2597 if (!trigger_applies_to_client(trigger
, client
)) {
2601 client_list_element
=
2602 zmalloc(sizeof(*client_list_element
));
2603 if (!client_list_element
) {
2605 goto error_put_client_list
;
2608 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2609 client_list_element
->client
= client
;
2610 cds_list_add(&client_list_element
->node
,
2611 &client_list
->list
);
2615 * Client list ownership transferred to the
2616 * notification_trigger_clients_ht.
2618 publish_notification_client_list(state
, client_list
);
2621 switch (get_condition_binding_object(condition
)) {
2622 case LTTNG_OBJECT_TYPE_SESSION
:
2623 /* Add the trigger to the list if it matches a known session. */
2624 ret
= bind_trigger_to_matching_session(trigger
, state
);
2626 goto error_put_client_list
;
2629 case LTTNG_OBJECT_TYPE_CHANNEL
:
2631 * Add the trigger to list of triggers bound to the channels
2634 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2636 goto error_put_client_list
;
2639 case LTTNG_OBJECT_TYPE_NONE
:
2642 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2644 goto error_put_client_list
;
2648 * The new trigger's condition must be evaluated against the current
2651 * In the case of `notify` action, nothing preventing clients from
2652 * subscribing to a condition before the corresponding trigger is
2653 * registered, we have to evaluate this new condition right away.
2655 * At some point, we were waiting for the next "evaluation" (e.g. on
2656 * reception of a channel sample) to evaluate this new condition, but
2659 * The reason it was broken is that waiting for the next sample
2660 * does not allow us to properly handle transitions for edge-triggered
2663 * Consider this example: when we handle a new channel sample, we
2664 * evaluate each conditions twice: once with the previous state, and
2665 * again with the newest state. We then use those two results to
2666 * determine whether a state change happened: a condition was false and
2667 * became true. If a state change happened, we have to notify clients.
2669 * Now, if a client subscribes to a given notification and registers
2670 * a trigger *after* that subscription, we have to make sure the
2671 * condition is evaluated at this point while considering only the
2672 * current state. Otherwise, the next evaluation cycle may only see
2673 * that the evaluations remain the same (true for samples n-1 and n) and
2674 * the client will never know that the condition has been met.
2676 switch (get_condition_binding_object(condition
)) {
2677 case LTTNG_OBJECT_TYPE_SESSION
:
2678 ret
= evaluate_session_condition_for_client(condition
, state
,
2679 &evaluation
, &object_uid
,
2681 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2682 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2684 case LTTNG_OBJECT_TYPE_CHANNEL
:
2685 ret
= evaluate_channel_condition_for_client(condition
, state
,
2686 &evaluation
, &object_uid
,
2688 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2689 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2691 case LTTNG_OBJECT_TYPE_NONE
:
2694 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2702 goto error_put_client_list
;
2705 DBG("Newly registered trigger's condition evaluated to %s",
2706 evaluation
? "true" : "false");
2708 /* Evaluation yielded nothing. Normal exit. */
2714 * Ownership of `evaluation` transferred to the action executor
2715 * no matter the result.
2717 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2718 evaluation
, &object_creds
, client_list
);
2720 switch (executor_status
) {
2721 case ACTION_EXECUTOR_STATUS_OK
:
2723 case ACTION_EXECUTOR_STATUS_ERROR
:
2724 case ACTION_EXECUTOR_STATUS_INVALID
:
2726 * TODO Add trigger identification (name/id) when
2727 * it is added to the API.
2729 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2731 goto error_put_client_list
;
2732 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2734 * TODO Add trigger identification (name/id) when
2735 * it is added to the API.
2737 * Not a fatal error.
2739 WARN("No space left when enqueuing action associated to newly registered trigger");
2747 *cmd_result
= LTTNG_OK
;
2748 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2749 trigger_name
, trigger_tracer_token
);
2751 error_put_client_list
:
2752 notification_client_list_put(client_list
);
2754 error_free_ht_element
:
2755 if (trigger_ht_element
) {
2756 /* Delayed removal due to RCU constraint on delete. */
2757 call_rcu(&trigger_ht_element
->rcu_node
,
2758 free_lttng_trigger_ht_element_rcu
);
2761 free(trigger_tokens_ht_element
);
2764 lttng_trigger_destroy(trigger
);
2771 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2773 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2778 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2780 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2785 int handle_notification_thread_command_unregister_trigger(
2786 struct notification_thread_state
*state
,
2787 const struct lttng_trigger
*trigger
,
2788 enum lttng_error_code
*_cmd_reply
)
2790 struct cds_lfht_iter iter
;
2791 struct cds_lfht_node
*triggers_ht_node
;
2792 struct lttng_channel_trigger_list
*trigger_list
;
2793 struct notification_client_list
*client_list
;
2794 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2795 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
2797 enum lttng_error_code cmd_reply
;
2801 cds_lfht_lookup(state
->triggers_ht
,
2802 lttng_condition_hash(condition
),
2806 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2807 if (!triggers_ht_node
) {
2808 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2811 cmd_reply
= LTTNG_OK
;
2814 /* Remove trigger from channel_triggers_ht. */
2815 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2816 channel_triggers_ht_node
) {
2817 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2819 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2820 &trigger_list
->list
, node
) {
2821 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2825 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2826 cds_list_del(&trigger_element
->node
);
2827 /* A trigger can only appear once per channel */
2832 if (lttng_condition_get_type(condition
) ==
2833 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2834 struct notification_trigger_tokens_ht_element
2835 *trigger_tokens_ht_element
;
2837 cds_lfht_for_each_entry (state
->trigger_tokens_ht
, &iter
,
2838 trigger_tokens_ht_element
, node
) {
2839 if (!lttng_trigger_is_equal(trigger
,
2840 trigger_tokens_ht_element
->trigger
)) {
2844 DBG("[notification-thread] Removed trigger from tokens_ht");
2845 cds_lfht_del(state
->trigger_tokens_ht
,
2846 &trigger_tokens_ht_element
->node
);
2847 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2848 free_notification_trigger_tokens_ht_element_rcu
);
2854 if (is_trigger_action_notify(trigger
)) {
2856 * Remove and release the client list from
2857 * notification_trigger_clients_ht.
2859 client_list
= get_client_list_from_condition(state
, condition
);
2860 assert(client_list
);
2862 /* Put new reference and the hashtable's reference. */
2863 notification_client_list_put(client_list
);
2864 notification_client_list_put(client_list
);
2868 /* Remove trigger from triggers_ht. */
2869 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2870 struct lttng_trigger_ht_element
, node
);
2871 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2872 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2874 /* Release the ownership of the trigger. */
2875 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2876 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2880 *_cmd_reply
= cmd_reply
;
2885 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2886 int handle_notification_thread_command(
2887 struct notification_thread_handle
*handle
,
2888 struct notification_thread_state
*state
)
2892 struct notification_thread_command
*cmd
;
2894 /* Read the event pipe to put it back into a quiescent state. */
2895 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2897 if (ret
!= sizeof(counter
)) {
2901 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2902 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2903 struct notification_thread_command
, cmd_list_node
);
2904 cds_list_del(&cmd
->cmd_list_node
);
2905 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2907 DBG("[notification-thread] Received `%s` command",
2908 notification_command_type_str(cmd
->type
));
2909 switch (cmd
->type
) {
2910 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2911 ret
= handle_notification_thread_command_register_trigger(state
,
2912 cmd
->parameters
.register_trigger
.trigger
,
2915 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2916 ret
= handle_notification_thread_command_unregister_trigger(
2918 cmd
->parameters
.unregister_trigger
.trigger
,
2921 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2922 ret
= handle_notification_thread_command_add_channel(
2924 cmd
->parameters
.add_channel
.session
.name
,
2925 cmd
->parameters
.add_channel
.session
.uid
,
2926 cmd
->parameters
.add_channel
.session
.gid
,
2927 cmd
->parameters
.add_channel
.channel
.name
,
2928 cmd
->parameters
.add_channel
.channel
.domain
,
2929 cmd
->parameters
.add_channel
.channel
.key
,
2930 cmd
->parameters
.add_channel
.channel
.capacity
,
2933 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2934 ret
= handle_notification_thread_command_remove_channel(
2935 state
, cmd
->parameters
.remove_channel
.key
,
2936 cmd
->parameters
.remove_channel
.domain
,
2939 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2940 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2941 ret
= handle_notification_thread_command_session_rotation(
2944 cmd
->parameters
.session_rotation
.session_name
,
2945 cmd
->parameters
.session_rotation
.uid
,
2946 cmd
->parameters
.session_rotation
.gid
,
2947 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2948 cmd
->parameters
.session_rotation
.location
,
2951 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
2952 ret
= handle_notification_thread_command_add_tracer_event_source(
2954 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2955 cmd
->parameters
.tracer_event_source
.domain
,
2958 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
2959 ret
= handle_notification_thread_command_remove_tracer_event_source(
2961 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2964 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2966 struct lttng_triggers
*triggers
= NULL
;
2968 ret
= handle_notification_thread_command_list_triggers(
2971 cmd
->parameters
.list_triggers
.uid
,
2974 cmd
->reply
.list_triggers
.triggers
= triggers
;
2978 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2979 cmd
->reply_code
= LTTNG_OK
;
2982 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2984 const enum client_transmission_status client_status
=
2985 cmd
->parameters
.client_communication_update
2987 const notification_client_id client_id
=
2988 cmd
->parameters
.client_communication_update
.id
;
2989 struct notification_client
*client
;
2992 client
= get_client_from_id(client_id
, state
);
2996 * Client error was probably already picked-up by the
2997 * notification thread or it has disconnected
2998 * gracefully while this command was queued.
3000 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3004 ret
= client_handle_transmission_status(
3005 client
, client_status
, state
);
3011 ERR("[notification-thread] Unknown internal command received");
3019 if (cmd
->is_async
) {
3023 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3027 /* Wake-up and return a fatal error to the calling thread. */
3028 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3029 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3031 /* Indicate a fatal error to the caller. */
3036 int socket_set_non_blocking(int socket
)
3040 /* Set the pipe as non-blocking. */
3041 ret
= fcntl(socket
, F_GETFL
, 0);
3043 PERROR("fcntl get socket flags");
3048 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3050 PERROR("fcntl set O_NONBLOCK socket flag");
3053 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3059 int client_reset_inbound_state(struct notification_client
*client
)
3064 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3066 client
->communication
.inbound
.bytes_to_receive
=
3067 sizeof(struct lttng_notification_channel_message
);
3068 client
->communication
.inbound
.msg_type
=
3069 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3070 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3071 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3072 ret
= lttng_dynamic_buffer_set_size(
3073 &client
->communication
.inbound
.payload
.buffer
,
3074 client
->communication
.inbound
.bytes_to_receive
);
3079 int handle_notification_thread_client_connect(
3080 struct notification_thread_state
*state
)
3083 struct notification_client
*client
;
3085 DBG("[notification-thread] Handling new notification channel client connection");
3087 client
= zmalloc(sizeof(*client
));
3094 pthread_mutex_init(&client
->lock
, NULL
);
3095 client
->id
= state
->next_notification_client_id
++;
3096 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3097 lttng_payload_init(&client
->communication
.inbound
.payload
);
3098 lttng_payload_init(&client
->communication
.outbound
.payload
);
3099 client
->communication
.inbound
.expect_creds
= true;
3101 ret
= client_reset_inbound_state(client
);
3103 ERR("[notification-thread] Failed to reset client communication's inbound state");
3108 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3110 ERR("[notification-thread] Failed to accept new notification channel client connection");
3115 client
->socket
= ret
;
3117 ret
= socket_set_non_blocking(client
->socket
);
3119 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3123 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3125 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3130 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3131 LPOLLIN
| LPOLLERR
|
3132 LPOLLHUP
| LPOLLRDHUP
);
3134 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3138 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3142 cds_lfht_add(state
->client_socket_ht
,
3143 hash_client_socket(client
->socket
),
3144 &client
->client_socket_ht_node
);
3145 cds_lfht_add(state
->client_id_ht
,
3146 hash_client_id(client
->id
),
3147 &client
->client_id_ht_node
);
3153 notification_client_destroy(client
, state
);
3158 * RCU read-lock must be held by the caller.
3159 * Client lock must _not_ be held by the caller.
3162 int notification_thread_client_disconnect(
3163 struct notification_client
*client
,
3164 struct notification_thread_state
*state
)
3167 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3169 /* Acquire the client lock to disable its communication atomically. */
3170 pthread_mutex_lock(&client
->lock
);
3171 client
->communication
.active
= false;
3172 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3173 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3174 pthread_mutex_unlock(&client
->lock
);
3176 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3178 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3182 /* Release all conditions to which the client was subscribed. */
3183 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3184 &client
->condition_list
, node
) {
3185 (void) notification_thread_client_unsubscribe(client
,
3186 condition_list_element
->condition
, state
, NULL
);
3190 * Client no longer accessible to other threads (through the
3193 notification_client_destroy(client
, state
);
3197 int handle_notification_thread_client_disconnect(
3198 int client_socket
, struct notification_thread_state
*state
)
3201 struct notification_client
*client
;
3204 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3206 client
= get_client_from_socket(client_socket
, state
);
3208 /* Internal state corruption, fatal error. */
3209 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3215 ret
= notification_thread_client_disconnect(client
, state
);
3221 int handle_notification_thread_client_disconnect_all(
3222 struct notification_thread_state
*state
)
3224 struct cds_lfht_iter iter
;
3225 struct notification_client
*client
;
3226 bool error_encoutered
= false;
3229 DBG("[notification-thread] Closing all client connections");
3230 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3231 client_socket_ht_node
) {
3234 ret
= notification_thread_client_disconnect(
3237 error_encoutered
= true;
3241 return error_encoutered
? 1 : 0;
3244 int handle_notification_thread_trigger_unregister_all(
3245 struct notification_thread_state
*state
)
3247 bool error_occurred
= false;
3248 struct cds_lfht_iter iter
;
3249 struct lttng_trigger_ht_element
*trigger_ht_element
;
3252 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3254 int ret
= handle_notification_thread_command_unregister_trigger(
3255 state
, trigger_ht_element
->trigger
, NULL
);
3257 error_occurred
= true;
3261 return error_occurred
? -1 : 0;
3265 int client_handle_transmission_status(
3266 struct notification_client
*client
,
3267 enum client_transmission_status transmission_status
,
3268 struct notification_thread_state
*state
)
3272 switch (transmission_status
) {
3273 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3274 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3275 CLIENT_POLL_MASK_IN
);
3281 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3283 * We want to be notified whenever there is buffer space
3284 * available to send the rest of the payload.
3286 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3287 CLIENT_POLL_MASK_IN_OUT
);
3292 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3293 ret
= notification_thread_client_disconnect(client
, state
);
3298 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3308 /* Client lock must be acquired by caller. */
3310 enum client_transmission_status
client_flush_outgoing_queue(
3311 struct notification_client
*client
)
3314 size_t to_send_count
;
3315 enum client_transmission_status status
;
3316 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3317 &client
->communication
.outbound
.payload
, 0, -1);
3318 const int fds_to_send_count
=
3319 lttng_payload_view_get_fd_handle_count(&pv
);
3321 ASSERT_LOCKED(client
->lock
);
3323 if (!client
->communication
.active
) {
3324 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3328 if (pv
.buffer
.size
== 0) {
3330 * If both data and fds are equal to zero, we are in an invalid
3333 assert(fds_to_send_count
!= 0);
3338 to_send_count
= pv
.buffer
.size
;
3339 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3342 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3345 if ((ret
>= 0 && ret
< to_send_count
)) {
3346 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3348 to_send_count
-= max(ret
, 0);
3350 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3352 pv
.buffer
.size
- to_send_count
,
3354 ret
= lttng_dynamic_buffer_set_size(
3355 &client
->communication
.outbound
.payload
.buffer
,
3361 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3363 } else if (ret
< 0) {
3364 /* Generic error, disable the client's communication. */
3365 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3367 client
->communication
.active
= false;
3368 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3372 * No error and flushed the queue completely.
3374 * The payload buffer size is used later to
3375 * check if there is notifications queued. So albeit that the
3376 * direct caller knows that the transmission is complete, we
3377 * need to set the buffer size to zero.
3379 ret
= lttng_dynamic_buffer_set_size(
3380 &client
->communication
.outbound
.payload
.buffer
, 0);
3387 /* No fds to send, transmission is complete. */
3388 if (fds_to_send_count
== 0) {
3389 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3393 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3394 client
->socket
, &pv
);
3396 /* Generic error, disable the client's communication. */
3397 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3399 client
->communication
.active
= false;
3400 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3402 } else if (ret
== 0) {
3403 /* Nothing could be sent. */
3404 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3406 /* Fd passing is an all or nothing kind of thing. */
3407 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3409 * The payload _fd_array count is used later to
3410 * check if there is notifications queued. So although the
3411 * direct caller knows that the transmission is complete, we
3412 * need to clear the _fd_array for the queuing check.
3414 lttng_dynamic_pointer_array_clear(
3415 &client
->communication
.outbound
.payload
3420 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3421 client
->communication
.outbound
.queued_command_reply
= false;
3422 client
->communication
.outbound
.dropped_notification
= false;
3423 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3428 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3432 bool client_has_outbound_data_left(
3433 const struct notification_client
*client
)
3435 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3436 &client
->communication
.outbound
.payload
, 0, -1);
3437 const bool has_data
= pv
.buffer
.size
!= 0;
3438 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3440 return has_data
|| has_fds
;
3443 /* Client lock must _not_ be held by the caller. */
3445 int client_send_command_reply(struct notification_client
*client
,
3446 struct notification_thread_state
*state
,
3447 enum lttng_notification_channel_status status
)
3450 struct lttng_notification_channel_command_reply reply
= {
3451 .status
= (int8_t) status
,
3453 struct lttng_notification_channel_message msg
= {
3454 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3455 .size
= sizeof(reply
),
3457 char buffer
[sizeof(msg
) + sizeof(reply
)];
3458 enum client_transmission_status transmission_status
;
3460 memcpy(buffer
, &msg
, sizeof(msg
));
3461 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3462 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3464 pthread_mutex_lock(&client
->lock
);
3465 if (client
->communication
.outbound
.queued_command_reply
) {
3466 /* Protocol error. */
3470 /* Enqueue buffer to outgoing queue and flush it. */
3471 ret
= lttng_dynamic_buffer_append(
3472 &client
->communication
.outbound
.payload
.buffer
,
3473 buffer
, sizeof(buffer
));
3478 transmission_status
= client_flush_outgoing_queue(client
);
3480 if (client_has_outbound_data_left(client
)) {
3481 /* Queue could not be emptied. */
3482 client
->communication
.outbound
.queued_command_reply
= true;
3485 pthread_mutex_unlock(&client
->lock
);
3486 ret
= client_handle_transmission_status(
3487 client
, transmission_status
, state
);
3494 pthread_mutex_unlock(&client
->lock
);
3500 int client_handle_message_unknown(struct notification_client
*client
,
3501 struct notification_thread_state
*state
)
3505 * Receiving message header. The function will be called again
3506 * once the rest of the message as been received and can be
3509 const struct lttng_notification_channel_message
*msg
;
3511 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3512 msg
= (const struct lttng_notification_channel_message
*)
3513 client
->communication
.inbound
.payload
.buffer
.data
;
3515 if (msg
->size
== 0 ||
3516 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3517 ERR("[notification-thread] Invalid notification channel message: length = %u",
3523 switch (msg
->type
) {
3524 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3525 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3526 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3530 ERR("[notification-thread] Invalid notification channel message: unexpected message type");
3534 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3535 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3536 client
->communication
.inbound
.msg_type
=
3537 (enum lttng_notification_channel_message_type
) msg
->type
;
3538 ret
= lttng_dynamic_buffer_set_size(
3539 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3541 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3548 int client_handle_message_handshake(struct notification_client
*client
,
3549 struct notification_thread_state
*state
)
3552 struct lttng_notification_channel_command_handshake
*handshake_client
;
3553 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3554 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3555 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3557 const struct lttng_notification_channel_message msg_header
= {
3558 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3559 .size
= sizeof(handshake_reply
),
3561 enum lttng_notification_channel_status status
=
3562 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3563 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3565 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3566 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3567 sizeof(handshake_reply
));
3570 (struct lttng_notification_channel_command_handshake
*)
3571 client
->communication
.inbound
.payload
.buffer
3573 client
->major
= handshake_client
->major
;
3574 client
->minor
= handshake_client
->minor
;
3575 if (!client
->communication
.inbound
.creds_received
) {
3576 ERR("[notification-thread] No credentials received from client");
3581 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3582 &client
->communication
.inbound
.creds
);
3583 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3584 &client
->communication
.inbound
.creds
);
3585 DBG("[notification-thread] Received handshake from client (uid = %u, gid = %u) with version %i.%i",
3586 client
->uid
, client
->gid
, (int) client
->major
,
3587 (int) client
->minor
);
3589 if (handshake_client
->major
!=
3590 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3591 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3594 pthread_mutex_lock(&client
->lock
);
3595 /* Outgoing queue will be flushed when the command reply is sent. */
3596 ret
= lttng_dynamic_buffer_append(
3597 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3598 sizeof(send_buffer
));
3600 ERR("[notification-thread] Failed to send protocol version to notification channel client");
3604 client
->validated
= true;
3605 client
->communication
.active
= true;
3606 pthread_mutex_unlock(&client
->lock
);
3608 /* Set reception state to receive the next message header. */
3609 ret
= client_reset_inbound_state(client
);
3611 ERR("[notification-thread] Failed to reset client communication's inbound state");
3615 /* Flushes the outgoing queue. */
3616 ret
= client_send_command_reply(client
, state
, status
);
3618 ERR("[notification-thread] Failed to send reply to notification channel client");
3624 pthread_mutex_unlock(&client
->lock
);
3630 int client_handle_message_subscription(
3631 struct notification_client
*client
,
3632 enum lttng_notification_channel_message_type msg_type
,
3633 struct notification_thread_state
*state
)
3636 struct lttng_condition
*condition
;
3637 enum lttng_notification_channel_status status
=
3638 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3639 struct lttng_payload_view condition_view
=
3640 lttng_payload_view_from_payload(
3641 &client
->communication
.inbound
.payload
,
3643 size_t expected_condition_size
;
3646 * No need to lock client to sample the inbound state as the only
3647 * other thread accessing clients (action executor) only uses the
3650 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3651 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3652 if (ret
!= expected_condition_size
) {
3653 ERR("[notification-thread] Malformed condition received from client");
3657 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3658 ret
= notification_thread_client_subscribe(
3659 client
, condition
, state
, &status
);
3661 ret
= notification_thread_client_unsubscribe(
3662 client
, condition
, state
, &status
);
3669 /* Set reception state to receive the next message header. */
3670 ret
= client_reset_inbound_state(client
);
3672 ERR("[notification-thread] Failed to reset client communication's inbound state");
3676 ret
= client_send_command_reply(client
, state
, status
);
3678 ERR("[notification-thread] Failed to send reply to notification channel client");
3687 int client_dispatch_message(struct notification_client
*client
,
3688 struct notification_thread_state
*state
)
3692 if (client
->communication
.inbound
.msg_type
!=
3693 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3694 client
->communication
.inbound
.msg_type
!=
3695 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3696 !client
->validated
) {
3697 WARN("[notification-thread] client attempted a command before handshake");
3702 switch (client
->communication
.inbound
.msg_type
) {
3703 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
3705 ret
= client_handle_message_unknown(client
, state
);
3708 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3710 ret
= client_handle_message_handshake(client
, state
);
3713 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3714 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3716 ret
= client_handle_message_subscription(client
,
3717 client
->communication
.inbound
.msg_type
, state
);
3727 /* Incoming data from client. */
3728 int handle_notification_thread_client_in(
3729 struct notification_thread_state
*state
, int socket
)
3732 struct notification_client
*client
;
3737 client
= get_client_from_socket(socket
, state
);
3739 /* Internal error, abort. */
3744 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
3745 client
->communication
.inbound
.bytes_to_receive
;
3746 if (client
->communication
.inbound
.expect_creds
) {
3747 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
3748 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3749 client
->communication
.inbound
.bytes_to_receive
,
3750 &client
->communication
.inbound
.creds
);
3752 client
->communication
.inbound
.expect_creds
= false;
3753 client
->communication
.inbound
.creds_received
= true;
3756 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
3757 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
3758 client
->communication
.inbound
.bytes_to_receive
);
3760 if (recv_ret
>= 0) {
3761 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
3763 goto error_disconnect_client
;
3766 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
3767 /* Message incomplete wait for more data. */
3772 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
3775 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
3776 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
3778 client
->communication
.inbound
.fds_to_receive
,
3779 &client
->communication
.inbound
.payload
);
3782 * Fds received. non blocking fds passing is all
3785 ssize_t expected_size
;
3787 expected_size
= sizeof(int) *
3788 client
->communication
.inbound
3790 assert(ret
== expected_size
);
3791 client
->communication
.inbound
.fds_to_receive
= 0;
3792 } else if (ret
== 0) {
3793 /* Received nothing. */
3797 goto error_disconnect_client
;
3801 /* At this point the message is complete.*/
3802 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
3803 client
->communication
.inbound
.fds_to_receive
== 0);
3804 ret
= client_dispatch_message(client
, state
);
3807 * Only returns an error if this client must be
3810 goto error_disconnect_client
;
3817 error_disconnect_client
:
3818 ret
= notification_thread_client_disconnect(client
, state
);
3822 /* Client ready to receive outgoing data. */
3823 int handle_notification_thread_client_out(
3824 struct notification_thread_state
*state
, int socket
)
3827 struct notification_client
*client
;
3828 enum client_transmission_status transmission_status
;
3831 client
= get_client_from_socket(socket
, state
);
3833 /* Internal error, abort. */
3838 pthread_mutex_lock(&client
->lock
);
3839 transmission_status
= client_flush_outgoing_queue(client
);
3840 pthread_mutex_unlock(&client
->lock
);
3842 ret
= client_handle_transmission_status(
3843 client
, transmission_status
, state
);
3853 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
3854 const struct channel_state_sample
*sample
,
3855 uint64_t buffer_capacity
)
3857 bool result
= false;
3859 enum lttng_condition_type condition_type
;
3860 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
3861 condition
, struct lttng_condition_buffer_usage
,
3864 if (use_condition
->threshold_bytes
.set
) {
3865 threshold
= use_condition
->threshold_bytes
.value
;
3868 * Threshold was expressed as a ratio.
3870 * TODO the threshold (in bytes) of conditions expressed
3871 * as a ratio of total buffer size could be cached to
3872 * forego this double-multiplication or it could be performed
3873 * as fixed-point math.
3875 * Note that caching should accommodates the case where the
3876 * condition applies to multiple channels (i.e. don't assume
3877 * that all channels matching my_chann* have the same size...)
3879 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
3880 (double) buffer_capacity
);
3883 condition_type
= lttng_condition_get_type(condition
);
3884 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
3885 DBG("[notification-thread] Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3886 threshold
, sample
->highest_usage
);
3889 * The low condition should only be triggered once _all_ of the
3890 * streams in a channel have gone below the "low" threshold.
3892 if (sample
->highest_usage
<= threshold
) {
3896 DBG("[notification-thread] High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
3897 threshold
, sample
->highest_usage
);
3900 * For high buffer usage scenarios, we want to trigger whenever
3901 * _any_ of the streams has reached the "high" threshold.
3903 if (sample
->highest_usage
>= threshold
) {
3912 bool evaluate_session_consumed_size_condition(
3913 const struct lttng_condition
*condition
,
3914 uint64_t session_consumed_size
)
3917 const struct lttng_condition_session_consumed_size
*size_condition
=
3918 container_of(condition
,
3919 struct lttng_condition_session_consumed_size
,
3922 threshold
= size_condition
->consumed_threshold_bytes
.value
;
3923 DBG("[notification-thread] Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
3924 threshold
, session_consumed_size
);
3925 return session_consumed_size
>= threshold
;
3929 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
3930 struct lttng_evaluation
**evaluation
,
3931 const struct notification_thread_state
*state
,
3932 const struct channel_state_sample
*previous_sample
,
3933 const struct channel_state_sample
*latest_sample
,
3934 uint64_t previous_session_consumed_total
,
3935 uint64_t latest_session_consumed_total
,
3936 struct channel_info
*channel_info
)
3939 enum lttng_condition_type condition_type
;
3940 const bool previous_sample_available
= !!previous_sample
;
3941 bool previous_sample_result
= false;
3942 bool latest_sample_result
;
3944 condition_type
= lttng_condition_get_type(condition
);
3946 switch (condition_type
) {
3947 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3948 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3949 if (caa_likely(previous_sample_available
)) {
3950 previous_sample_result
=
3951 evaluate_buffer_usage_condition(condition
,
3952 previous_sample
, channel_info
->capacity
);
3954 latest_sample_result
= evaluate_buffer_usage_condition(
3955 condition
, latest_sample
,
3956 channel_info
->capacity
);
3958 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3959 if (caa_likely(previous_sample_available
)) {
3960 previous_sample_result
=
3961 evaluate_session_consumed_size_condition(
3963 previous_session_consumed_total
);
3965 latest_sample_result
=
3966 evaluate_session_consumed_size_condition(
3968 latest_session_consumed_total
);
3971 /* Unknown condition type; internal error. */
3975 if (!latest_sample_result
||
3976 (previous_sample_result
== latest_sample_result
)) {
3978 * Only trigger on a condition evaluation transition.
3980 * NOTE: This edge-triggered logic may not be appropriate for
3981 * future condition types.
3986 if (!evaluation
|| !latest_sample_result
) {
3990 switch (condition_type
) {
3991 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
3992 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
3993 *evaluation
= lttng_evaluation_buffer_usage_create(
3995 latest_sample
->highest_usage
,
3996 channel_info
->capacity
);
3998 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
3999 *evaluation
= lttng_evaluation_session_consumed_size_create(
4000 latest_session_consumed_total
);
4015 int client_notification_overflow(struct notification_client
*client
)
4018 const struct lttng_notification_channel_message msg
= {
4019 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4022 ASSERT_LOCKED(client
->lock
);
4024 DBG("Dropping notification addressed to client (socket fd = %i)",
4026 if (client
->communication
.outbound
.dropped_notification
) {
4028 * The client already has a "notification dropped" message
4029 * in its outgoing queue. Nothing to do since all
4030 * of those messages are coalesced.
4035 client
->communication
.outbound
.dropped_notification
= true;
4036 ret
= lttng_dynamic_buffer_append(
4037 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4040 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4047 static int client_handle_transmission_status_wrapper(
4048 struct notification_client
*client
,
4049 enum client_transmission_status status
,
4052 return client_handle_transmission_status(client
, status
,
4053 (struct notification_thread_state
*) user_data
);
4057 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4058 const struct lttng_evaluation
*evaluation
,
4059 struct notification_client_list
* client_list
,
4060 struct notification_thread_state
*state
,
4061 uid_t object_uid
, gid_t object_gid
)
4063 const struct lttng_credentials creds
= {
4064 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4065 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4068 return notification_client_list_send_evaluation(client_list
,
4069 lttng_trigger_get_const_condition(trigger
), evaluation
,
4070 lttng_trigger_get_credentials(trigger
),
4072 client_handle_transmission_status_wrapper
, state
);
4076 * Permission checks relative to notification channel clients are performed
4077 * here. Notice how object, client, and trigger credentials are involved in
4080 * The `object` credentials are the credentials associated with the "subject"
4081 * of a condition. For instance, a `rotation completed` condition applies
4082 * to a session. When that condition is met, it will produce an evaluation
4083 * against a session. Hence, in this case, the `object` credentials are the
4084 * credentials of the "subject" session.
4086 * The `trigger` credentials are the credentials of the user that registered the
4089 * The `client` credentials are the credentials of the user that created a given
4090 * notification channel.
4092 * In terms of visibility, it is expected that non-privilieged users can only
4093 * register triggers against "their" objects (their own sessions and
4094 * applications they are allowed to interact with). They can then open a
4095 * notification channel and subscribe to notifications associated with those
4098 * As for privilieged users, they can register triggers against the objects of
4099 * other users. They can then subscribe to the notifications associated to their
4100 * triggers. Privilieged users _can't_ subscribe to the notifications of
4101 * triggers owned by other users; they must create their own triggers.
4103 * This is more a concern of usability than security. It would be difficult for
4104 * a root user reliably subscribe to a specific set of conditions without
4105 * interference from external users (those could, for instance, unregister
4109 int notification_client_list_send_evaluation(
4110 struct notification_client_list
*client_list
,
4111 const struct lttng_condition
*condition
,
4112 const struct lttng_evaluation
*evaluation
,
4113 const struct lttng_credentials
*trigger_creds
,
4114 const struct lttng_credentials
*source_object_creds
,
4115 report_client_transmission_result_cb client_report
,
4119 struct lttng_payload msg_payload
;
4120 struct notification_client_list_element
*client_list_element
, *tmp
;
4121 const struct lttng_notification notification
= {
4122 .condition
= (struct lttng_condition
*) condition
,
4123 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4125 struct lttng_notification_channel_message msg_header
= {
4126 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4129 lttng_payload_init(&msg_payload
);
4131 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4132 sizeof(msg_header
));
4137 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4139 ERR("[notification-thread] Failed to serialize notification");
4144 /* Update payload size. */
4145 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4146 ->size
= (uint32_t)(
4147 msg_payload
.buffer
.size
- sizeof(msg_header
));
4149 /* Update the payload number of fds. */
4151 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4152 &msg_payload
, 0, -1);
4154 ((struct lttng_notification_channel_message
*)
4155 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4156 lttng_payload_view_get_fd_handle_count(&pv
);
4159 pthread_mutex_lock(&client_list
->lock
);
4160 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4161 &client_list
->list
, node
) {
4162 enum client_transmission_status transmission_status
;
4163 struct notification_client
*client
=
4164 client_list_element
->client
;
4167 pthread_mutex_lock(&client
->lock
);
4168 if (!client
->communication
.active
) {
4170 * Skip inactive client (protocol error or
4173 DBG("Skipping client at it is marked as inactive");
4177 if (source_object_creds
) {
4178 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4179 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4182 * Client is not allowed to monitor this
4185 DBG("[notification-thread] Skipping client at it does not have the object permission to receive notification for this trigger");
4190 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
) && client
->gid
!= lttng_credentials_get_gid(trigger_creds
)) {
4191 DBG("[notification-thread] Skipping client at it does not have the permission to receive notification for this trigger");
4195 DBG("[notification-thread] Sending notification to client (fd = %i, %zu bytes)",
4196 client
->socket
, msg_payload
.buffer
.size
);
4198 if (client_has_outbound_data_left(client
)) {
4200 * Outgoing data is already buffered for this client;
4201 * drop the notification and enqueue a "dropped
4202 * notification" message if this is the first dropped
4203 * notification since the socket spilled-over to the
4206 ret
= client_notification_overflow(client
);
4213 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4219 transmission_status
= client_flush_outgoing_queue(client
);
4220 pthread_mutex_unlock(&client
->lock
);
4221 ret
= client_report(client
, transmission_status
, user_data
);
4224 goto end_unlock_list
;
4230 pthread_mutex_unlock(&client
->lock
);
4233 goto end_unlock_list
;
4239 pthread_mutex_unlock(&client_list
->lock
);
4241 lttng_payload_reset(&msg_payload
);
4246 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4247 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4251 struct lttng_event_notifier_notification
*notification
= NULL
;
4252 char *capture_buffer
= NULL
;
4253 size_t capture_buffer_size
;
4254 void *reception_buffer
;
4255 size_t reception_size
;
4257 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4258 struct lttng_kernel_event_notifier_notification kernel_notification
;
4260 /* Init lttng_event_notifier_notification */
4262 case LTTNG_DOMAIN_UST
:
4263 reception_buffer
= (void *) &ust_notification
;
4264 reception_size
= sizeof(ust_notification
);
4266 case LTTNG_DOMAIN_KERNEL
:
4267 reception_buffer
= (void *) &kernel_notification
;
4268 reception_size
= sizeof(kernel_notification
);
4275 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4276 * ensuring that read/write of tracer notifications are atomic.
4278 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4280 if (ret
!= reception_size
) {
4281 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4282 notification_pipe_read_fd
, reception_size
, ret
);
4288 case LTTNG_DOMAIN_UST
:
4289 token
= ust_notification
.token
;
4290 capture_buffer_size
= ust_notification
.capture_buf_size
;
4292 case LTTNG_DOMAIN_KERNEL
:
4293 token
= kernel_notification
.token
;
4294 capture_buffer_size
= 0;
4300 if (capture_buffer_size
== 0) {
4301 capture_buffer
= NULL
;
4305 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4306 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",
4307 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4311 capture_buffer
= zmalloc(capture_buffer_size
);
4312 if (!capture_buffer
) {
4313 ERR("[notification-thread] Failed to allocate capture buffer");
4317 /* Fetch additional payload (capture). */
4318 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4319 if (ret
!= capture_buffer_size
) {
4320 ERR("[notification-thread] Failed to read from event source pipe (fd = %i)",
4321 notification_pipe_read_fd
);
4326 notification
= lttng_event_notifier_notification_create(token
, domain
,
4327 capture_buffer
, capture_buffer_size
);
4328 if (notification
== NULL
) {
4333 * Ownership transfered to the lttng_event_notifier_notification object.
4335 capture_buffer
= NULL
;
4338 free(capture_buffer
);
4339 return notification
;
4343 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4344 struct lttng_event_notifier_notification
*notification
)
4346 struct cds_lfht_node
*node
;
4347 struct cds_lfht_iter iter
;
4348 struct notification_trigger_tokens_ht_element
*element
;
4349 enum lttng_trigger_status trigger_status
;
4350 struct lttng_evaluation
*evaluation
= NULL
;
4351 enum action_executor_status executor_status
;
4352 struct notification_client_list
*client_list
= NULL
;
4353 const char *trigger_name
;
4355 unsigned int capture_count
= 0;
4357 /* Find triggers associated with this token. */
4359 cds_lfht_lookup(state
->trigger_tokens_ht
,
4360 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4361 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4362 node
= cds_lfht_iter_get_node(&iter
);
4363 if (caa_unlikely(!node
)) {
4365 * This is not an error, slow consumption of the tracer
4366 * notifications can lead to situations where a trigger is
4367 * removed but we still get tracer notifications matching a
4368 * trigger that no longer exists.
4374 element
= caa_container_of(node
,
4375 struct notification_trigger_tokens_ht_element
,
4378 if (!lttng_trigger_should_fire(element
->trigger
)) {
4383 lttng_trigger_fire(element
->trigger
);
4385 trigger_status
= lttng_trigger_get_name(element
->trigger
, &trigger_name
);
4386 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4388 if (lttng_condition_event_rule_get_capture_descriptor_count(
4389 lttng_trigger_get_const_condition(element
->trigger
),
4390 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4391 ERR("Failed to get capture count");
4396 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4397 ERR("Expected capture but capture buffer is null");
4402 evaluation
= lttng_evaluation_event_rule_create(
4403 container_of(lttng_trigger_get_const_condition(
4405 struct lttng_condition_event_rule
,
4408 notification
->capture_buffer
,
4409 notification
->capture_buf_size
, false);
4411 if (evaluation
== NULL
) {
4412 ERR("[notification-thread] Failed to create event rule hit evaluation while creating and enqueuing action executor job");
4416 client_list
= get_client_list_from_condition(state
,
4417 lttng_trigger_get_const_condition(element
->trigger
));
4418 executor_status
= action_executor_enqueue(state
->executor
,
4419 element
->trigger
, evaluation
, NULL
, client_list
);
4420 switch (executor_status
) {
4421 case ACTION_EXECUTOR_STATUS_OK
:
4424 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4426 struct notification_client_list_element
*client_list_element
,
4430 * Not a fatal error; this is expected and simply means the
4431 * executor has too much work queued already.
4435 /* No clients subscribed to notifications for this trigger. */
4440 /* Warn clients that a notification (or more) was dropped. */
4441 pthread_mutex_lock(&client_list
->lock
);
4442 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4443 &client_list
->list
, node
) {
4444 enum client_transmission_status transmission_status
;
4445 struct notification_client
*client
=
4446 client_list_element
->client
;
4448 pthread_mutex_lock(&client
->lock
);
4449 ret
= client_notification_overflow(client
);
4455 transmission_status
=
4456 client_flush_outgoing_queue(client
);
4457 ret
= client_handle_transmission_status(
4458 client
, transmission_status
, state
);
4464 pthread_mutex_unlock(&client
->lock
);
4470 pthread_mutex_unlock(&client_list
->lock
);
4473 case ACTION_EXECUTOR_STATUS_ERROR
:
4474 /* Fatal error, shut down everything. */
4475 ERR("Fatal error encoutered while enqueuing action to the action executor");
4479 /* Unhandled error. */
4484 notification_client_list_put(client_list
);
4491 int handle_one_event_notifier_notification(
4492 struct notification_thread_state
*state
,
4493 int pipe
, enum lttng_domain_type domain
)
4496 struct lttng_event_notifier_notification
*notification
= NULL
;
4498 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4499 if (notification
== NULL
) {
4500 /* Reception failed, don't consider it fatal. */
4501 ERR("[notification-thread] Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4502 pipe
, lttng_domain_type_str(domain
));
4506 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4508 ERR("[notification-thread] Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4509 pipe
, lttng_domain_type_str(domain
));
4514 lttng_event_notifier_notification_destroy(notification
);
4518 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4519 int pipe
, enum lttng_domain_type domain
)
4521 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4524 int handle_notification_thread_channel_sample(
4525 struct notification_thread_state
*state
, int pipe
,
4526 enum lttng_domain_type domain
)
4529 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4530 struct channel_info
*channel_info
;
4531 struct cds_lfht_node
*node
;
4532 struct cds_lfht_iter iter
;
4533 struct lttng_channel_trigger_list
*trigger_list
;
4534 struct lttng_trigger_list_element
*trigger_list_element
;
4535 bool previous_sample_available
= false;
4536 struct channel_state_sample previous_sample
, latest_sample
;
4537 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4538 struct lttng_credentials channel_creds
;
4541 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4542 * ensuring that read/write of sampling messages are atomic.
4544 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4545 if (ret
!= sizeof(sample_msg
)) {
4546 ERR("[notification-thread] Failed to read from monitoring pipe (fd = %i)",
4553 latest_sample
.key
.key
= sample_msg
.key
;
4554 latest_sample
.key
.domain
= domain
;
4555 latest_sample
.highest_usage
= sample_msg
.highest
;
4556 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4557 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4561 /* Retrieve the channel's informations */
4562 cds_lfht_lookup(state
->channels_ht
,
4563 hash_channel_key(&latest_sample
.key
),
4567 node
= cds_lfht_iter_get_node(&iter
);
4568 if (caa_unlikely(!node
)) {
4570 * Not an error since the consumer can push a sample to the pipe
4571 * and the rest of the session daemon could notify us of the
4572 * channel's destruction before we get a chance to process that
4575 DBG("[notification-thread] Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4576 latest_sample
.key
.key
,
4577 lttng_domain_type_str(domain
));
4580 channel_info
= caa_container_of(node
, struct channel_info
,
4582 DBG("[notification-thread] Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4584 latest_sample
.key
.key
,
4585 channel_info
->session_info
->name
,
4586 latest_sample
.highest_usage
,
4587 latest_sample
.lowest_usage
,
4588 latest_sample
.channel_total_consumed
);
4590 previous_session_consumed_total
=
4591 channel_info
->session_info
->consumed_data_size
;
4593 /* Retrieve the channel's last sample, if it exists, and update it. */
4594 cds_lfht_lookup(state
->channel_state_ht
,
4595 hash_channel_key(&latest_sample
.key
),
4596 match_channel_state_sample
,
4599 node
= cds_lfht_iter_get_node(&iter
);
4600 if (caa_likely(node
)) {
4601 struct channel_state_sample
*stored_sample
;
4603 /* Update the sample stored. */
4604 stored_sample
= caa_container_of(node
,
4605 struct channel_state_sample
,
4606 channel_state_ht_node
);
4608 memcpy(&previous_sample
, stored_sample
,
4609 sizeof(previous_sample
));
4610 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4611 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4612 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4613 previous_sample_available
= true;
4615 latest_session_consumed_total
=
4616 previous_session_consumed_total
+
4617 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4620 * This is the channel's first sample, allocate space for and
4621 * store the new sample.
4623 struct channel_state_sample
*stored_sample
;
4625 stored_sample
= zmalloc(sizeof(*stored_sample
));
4626 if (!stored_sample
) {
4631 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4632 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4633 cds_lfht_add(state
->channel_state_ht
,
4634 hash_channel_key(&stored_sample
->key
),
4635 &stored_sample
->channel_state_ht_node
);
4637 latest_session_consumed_total
=
4638 previous_session_consumed_total
+
4639 latest_sample
.channel_total_consumed
;
4642 channel_info
->session_info
->consumed_data_size
=
4643 latest_session_consumed_total
;
4645 /* Find triggers associated with this channel. */
4646 cds_lfht_lookup(state
->channel_triggers_ht
,
4647 hash_channel_key(&latest_sample
.key
),
4648 match_channel_trigger_list
,
4651 node
= cds_lfht_iter_get_node(&iter
);
4652 if (caa_likely(!node
)) {
4656 channel_creds
= (typeof(channel_creds
)) {
4657 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4658 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
4661 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
4662 channel_triggers_ht_node
);
4663 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
4665 const struct lttng_condition
*condition
;
4666 struct lttng_trigger
*trigger
;
4667 struct notification_client_list
*client_list
= NULL
;
4668 struct lttng_evaluation
*evaluation
= NULL
;
4669 enum action_executor_status executor_status
;
4672 trigger
= trigger_list_element
->trigger
;
4673 condition
= lttng_trigger_get_const_condition(trigger
);
4677 * Check if any client is subscribed to the result of this
4680 client_list
= get_client_list_from_condition(state
, condition
);
4682 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
4683 previous_sample_available
? &previous_sample
: NULL
,
4685 previous_session_consumed_total
,
4686 latest_session_consumed_total
,
4688 if (caa_unlikely(ret
)) {
4692 if (caa_likely(!evaluation
)) {
4696 if (!lttng_trigger_should_fire(trigger
)) {
4700 lttng_trigger_fire(trigger
);
4703 * Ownership of `evaluation` transferred to the action executor
4704 * no matter the result.
4706 executor_status
= action_executor_enqueue(state
->executor
,
4707 trigger
, evaluation
, &channel_creds
,
4710 switch (executor_status
) {
4711 case ACTION_EXECUTOR_STATUS_OK
:
4713 case ACTION_EXECUTOR_STATUS_ERROR
:
4714 case ACTION_EXECUTOR_STATUS_INVALID
:
4716 * TODO Add trigger identification (name/id) when
4717 * it is added to the API.
4719 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
4722 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4724 * TODO Add trigger identification (name/id) when
4725 * it is added to the API.
4727 * Not a fatal error.
4729 WARN("No space left when enqueuing action associated with buffer-condition trigger");
4737 notification_client_list_put(client_list
);
4738 if (caa_unlikely(ret
)) {