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/list-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-matches-internal.h>
32 #include <lttng/domain-internal.h>
33 #include <lttng/notification/channel-internal.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <lttng/event-rule/event-rule-internal.h>
43 #include "condition-internal.h"
44 #include "event-notifier-error-accounting.h"
45 #include "notification-thread.h"
46 #include "notification-thread-events.h"
47 #include "notification-thread-commands.h"
48 #include "lttng-sessiond.h"
51 #define CLIENT_POLL_EVENTS_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
52 #define CLIENT_POLL_EVENTS_IN_OUT (CLIENT_POLL_EVENTS_IN | LPOLLOUT)
54 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
55 #define MAX_CAPTURE_SIZE (PIPE_BUF)
57 enum lttng_object_type
{
58 LTTNG_OBJECT_TYPE_UNKNOWN
,
59 LTTNG_OBJECT_TYPE_NONE
,
60 LTTNG_OBJECT_TYPE_CHANNEL
,
61 LTTNG_OBJECT_TYPE_SESSION
,
64 struct lttng_trigger_list_element
{
65 /* No ownership of the trigger object is assumed. */
66 struct lttng_trigger
*trigger
;
67 struct cds_list_head node
;
70 struct lttng_channel_trigger_list
{
71 struct channel_key channel_key
;
72 /* List of struct lttng_trigger_list_element. */
73 struct cds_list_head list
;
74 /* Node in the channel_triggers_ht */
75 struct cds_lfht_node channel_triggers_ht_node
;
76 /* call_rcu delayed reclaim. */
77 struct rcu_head rcu_node
;
81 * List of triggers applying to a given session.
84 * - lttng_session_trigger_list_create()
85 * - lttng_session_trigger_list_build()
86 * - lttng_session_trigger_list_destroy()
87 * - lttng_session_trigger_list_add()
89 struct lttng_session_trigger_list
{
91 * Not owned by this; points to the session_info structure's
94 const char *session_name
;
95 /* List of struct lttng_trigger_list_element. */
96 struct cds_list_head list
;
97 /* Node in the session_triggers_ht */
98 struct cds_lfht_node session_triggers_ht_node
;
100 * Weak reference to the notification system's session triggers
103 * The session trigger list structure structure is owned by
104 * the session's session_info.
106 * The session_info is kept alive the the channel_infos holding a
107 * reference to it (reference counting). When those channels are
108 * destroyed (at runtime or on teardown), the reference they hold
109 * to the session_info are released. On destruction of session_info,
110 * session_info_destroy() will remove the list of triggers applying
111 * to this session from the notification system's state.
113 * This implies that the session_triggers_ht must be destroyed
114 * after the channels.
116 struct cds_lfht
*session_triggers_ht
;
117 /* Used for delayed RCU reclaim. */
118 struct rcu_head rcu_node
;
121 struct lttng_trigger_ht_element
{
122 struct lttng_trigger
*trigger
;
123 struct cds_lfht_node node
;
124 struct cds_lfht_node node_by_name_uid
;
125 struct cds_list_head client_list_trigger_node
;
126 /* call_rcu delayed reclaim. */
127 struct rcu_head rcu_node
;
130 struct lttng_condition_list_element
{
131 struct lttng_condition
*condition
;
132 struct cds_list_head node
;
135 struct channel_state_sample
{
136 struct channel_key key
;
137 struct cds_lfht_node channel_state_ht_node
;
138 uint64_t highest_usage
;
139 uint64_t lowest_usage
;
140 uint64_t channel_total_consumed
;
141 /* call_rcu delayed reclaim. */
142 struct rcu_head rcu_node
;
145 static unsigned long hash_channel_key(struct channel_key
*key
);
146 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
147 struct lttng_evaluation
**evaluation
,
148 const struct notification_thread_state
*state
,
149 const struct channel_state_sample
*previous_sample
,
150 const struct channel_state_sample
*latest_sample
,
151 uint64_t previous_session_consumed_total
,
152 uint64_t latest_session_consumed_total
,
153 struct channel_info
*channel_info
);
155 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
156 const struct lttng_evaluation
*evaluation
,
157 struct notification_client_list
*client_list
,
158 struct notification_thread_state
*state
,
159 uid_t channel_uid
, gid_t channel_gid
);
162 /* session_info API */
164 void session_info_destroy(void *_data
);
166 void session_info_get(struct session_info
*session_info
);
168 void session_info_put(struct session_info
*session_info
);
170 struct session_info
*session_info_create(const char *name
,
171 uid_t uid
, gid_t gid
,
172 struct lttng_session_trigger_list
*trigger_list
,
173 struct cds_lfht
*sessions_ht
);
175 void session_info_add_channel(struct session_info
*session_info
,
176 struct channel_info
*channel_info
);
178 void session_info_remove_channel(struct session_info
*session_info
,
179 struct channel_info
*channel_info
);
181 /* lttng_session_trigger_list API */
183 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
184 const char *session_name
,
185 struct cds_lfht
*session_triggers_ht
);
187 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
188 const struct notification_thread_state
*state
,
189 const char *session_name
);
191 void lttng_session_trigger_list_destroy(
192 struct lttng_session_trigger_list
*list
);
194 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
195 struct lttng_trigger
*trigger
);
198 int client_handle_transmission_status(
199 struct notification_client
*client
,
200 enum client_transmission_status transmission_status
,
201 struct notification_thread_state
*state
);
204 int handle_one_event_notifier_notification(
205 struct notification_thread_state
*state
,
206 int pipe
, enum lttng_domain_type domain
);
209 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
212 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
214 /* This double-cast is intended to supress pointer-to-cast warning. */
215 const int socket
= (int) (intptr_t) key
;
216 const struct notification_client
*client
= caa_container_of(node
,
217 struct notification_client
, client_socket_ht_node
);
219 return client
->socket
== socket
;
223 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
225 /* This double-cast is intended to supress pointer-to-cast warning. */
226 const notification_client_id id
= *((notification_client_id
*) key
);
227 const struct notification_client
*client
= caa_container_of(
228 node
, struct notification_client
, client_id_ht_node
);
230 return client
->id
== id
;
234 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
236 struct channel_key
*channel_key
= (struct channel_key
*) key
;
237 struct lttng_channel_trigger_list
*trigger_list
;
239 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
240 channel_triggers_ht_node
);
242 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
243 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
247 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
249 const char *session_name
= (const char *) key
;
250 struct lttng_session_trigger_list
*trigger_list
;
252 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
253 session_triggers_ht_node
);
255 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
259 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
261 struct channel_key
*channel_key
= (struct channel_key
*) key
;
262 struct channel_state_sample
*sample
;
264 sample
= caa_container_of(node
, struct channel_state_sample
,
265 channel_state_ht_node
);
267 return !!((channel_key
->key
== sample
->key
.key
) &&
268 (channel_key
->domain
== sample
->key
.domain
));
272 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
274 struct channel_key
*channel_key
= (struct channel_key
*) key
;
275 struct channel_info
*channel_info
;
277 channel_info
= caa_container_of(node
, struct channel_info
,
280 return !!((channel_key
->key
== channel_info
->key
.key
) &&
281 (channel_key
->domain
== channel_info
->key
.domain
));
285 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
287 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
288 struct lttng_trigger_ht_element
*trigger_ht_element
;
290 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
293 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
297 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
299 const uint64_t *_key
= key
;
300 struct notification_trigger_tokens_ht_element
*element
;
302 element
= caa_container_of(node
,
303 struct notification_trigger_tokens_ht_element
, node
);
304 return *_key
== element
->token
;
308 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
310 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
311 struct notification_client_list
*client_list
;
312 const struct lttng_condition
*condition
;
314 assert(condition_key
);
316 client_list
= caa_container_of(node
, struct notification_client_list
,
317 notification_trigger_clients_ht_node
);
318 condition
= client_list
->condition
;
320 return !!lttng_condition_is_equal(condition_key
, condition
);
324 int match_session(struct cds_lfht_node
*node
, const void *key
)
326 const char *name
= key
;
327 struct session_info
*session_info
= caa_container_of(
328 node
, struct session_info
, sessions_ht_node
);
330 return !strcmp(session_info
->name
, name
);
334 const char *notification_command_type_str(
335 enum notification_thread_command_type type
)
338 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
339 return "REGISTER_TRIGGER";
340 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
341 return "UNREGISTER_TRIGGER";
342 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
343 return "ADD_CHANNEL";
344 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
345 return "REMOVE_CHANNEL";
346 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
347 return "SESSION_ROTATION_ONGOING";
348 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
349 return "SESSION_ROTATION_COMPLETED";
350 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
351 return "ADD_TRACER_EVENT_SOURCE";
352 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
353 return "REMOVE_TRACER_EVENT_SOURCE";
354 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
355 return "LIST_TRIGGERS";
356 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
357 return "GET_TRIGGER";
358 case NOTIFICATION_COMMAND_TYPE_QUIT
:
360 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
361 return "CLIENT_COMMUNICATION_UPDATE";
368 * Match trigger based on name and credentials only.
369 * Name duplication is NOT allowed for the same uid.
372 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
376 const char *element_trigger_name
;
377 const char *key_name
;
378 enum lttng_trigger_status status
;
379 const struct lttng_credentials
*key_creds
;
380 const struct lttng_credentials
*node_creds
;
381 const struct lttng_trigger
*trigger_key
=
382 (const struct lttng_trigger
*) key
;
383 const struct lttng_trigger_ht_element
*trigger_ht_element
=
384 caa_container_of(node
,
385 struct lttng_trigger_ht_element
,
388 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
,
389 &element_trigger_name
);
390 element_trigger_name
= status
== LTTNG_TRIGGER_STATUS_OK
?
391 element_trigger_name
: NULL
;
393 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
394 key_name
= status
== LTTNG_TRIGGER_STATUS_OK
? key_name
: NULL
;
398 * Consider null names as not equal. This is to maintain backwards
399 * compatibility with pre-2.13 anonymous triggers. Multiples anonymous
400 * triggers are allowed for a given user.
402 if (!element_trigger_name
|| !key_name
) {
406 if (strcmp(element_trigger_name
, key_name
) != 0) {
410 /* Compare the owners' UIDs. */
411 key_creds
= lttng_trigger_get_credentials(trigger_key
);
412 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
414 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
421 * Hash trigger based on name and credentials only.
424 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
426 unsigned long hash
= 0;
427 const struct lttng_credentials
*trigger_creds
;
428 const char *trigger_name
;
429 enum lttng_trigger_status status
;
431 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
432 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
433 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
436 trigger_creds
= lttng_trigger_get_credentials(trigger
);
437 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
444 unsigned long hash_channel_key(struct channel_key
*key
)
446 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
447 unsigned long domain_hash
= hash_key_ulong(
448 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
450 return key_hash
^ domain_hash
;
454 unsigned long hash_client_socket(int socket
)
456 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
460 unsigned long hash_client_id(notification_client_id id
)
462 return hash_key_u64(&id
, lttng_ht_seed
);
466 * Get the type of object to which a given condition applies. Bindings let
467 * the notification system evaluate a trigger's condition when a given
468 * object's state is updated.
470 * For instance, a condition bound to a channel will be evaluated everytime
471 * the channel's state is changed by a channel monitoring sample.
474 enum lttng_object_type
get_condition_binding_object(
475 const struct lttng_condition
*condition
)
477 switch (lttng_condition_get_type(condition
)) {
478 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
479 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
480 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
481 return LTTNG_OBJECT_TYPE_CHANNEL
;
482 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
483 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
484 return LTTNG_OBJECT_TYPE_SESSION
;
485 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
486 return LTTNG_OBJECT_TYPE_NONE
;
488 return LTTNG_OBJECT_TYPE_UNKNOWN
;
493 void free_channel_info_rcu(struct rcu_head
*node
)
495 free(caa_container_of(node
, struct channel_info
, rcu_node
));
499 void channel_info_destroy(struct channel_info
*channel_info
)
505 if (channel_info
->session_info
) {
506 session_info_remove_channel(channel_info
->session_info
,
508 session_info_put(channel_info
->session_info
);
510 if (channel_info
->name
) {
511 free(channel_info
->name
);
513 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
517 void free_session_info_rcu(struct rcu_head
*node
)
519 free(caa_container_of(node
, struct session_info
, rcu_node
));
522 /* Don't call directly, use the ref-counting mechanism. */
524 void session_info_destroy(void *_data
)
526 struct session_info
*session_info
= _data
;
529 assert(session_info
);
530 if (session_info
->channel_infos_ht
) {
531 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
533 ERR("Failed to destroy channel information hash table");
536 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
539 cds_lfht_del(session_info
->sessions_ht
,
540 &session_info
->sessions_ht_node
);
542 free(session_info
->name
);
543 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
547 void session_info_get(struct session_info
*session_info
)
552 lttng_ref_get(&session_info
->ref
);
556 void session_info_put(struct session_info
*session_info
)
561 lttng_ref_put(&session_info
->ref
);
565 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
566 struct lttng_session_trigger_list
*trigger_list
,
567 struct cds_lfht
*sessions_ht
)
569 struct session_info
*session_info
;
573 session_info
= zmalloc(sizeof(*session_info
));
577 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
579 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
580 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
581 if (!session_info
->channel_infos_ht
) {
585 cds_lfht_node_init(&session_info
->sessions_ht_node
);
586 session_info
->name
= strdup(name
);
587 if (!session_info
->name
) {
590 session_info
->uid
= uid
;
591 session_info
->gid
= gid
;
592 session_info
->trigger_list
= trigger_list
;
593 session_info
->sessions_ht
= sessions_ht
;
597 session_info_put(session_info
);
602 void session_info_add_channel(struct session_info
*session_info
,
603 struct channel_info
*channel_info
)
606 cds_lfht_add(session_info
->channel_infos_ht
,
607 hash_channel_key(&channel_info
->key
),
608 &channel_info
->session_info_channels_ht_node
);
613 void session_info_remove_channel(struct session_info
*session_info
,
614 struct channel_info
*channel_info
)
617 cds_lfht_del(session_info
->channel_infos_ht
,
618 &channel_info
->session_info_channels_ht_node
);
623 struct channel_info
*channel_info_create(const char *channel_name
,
624 struct channel_key
*channel_key
, uint64_t channel_capacity
,
625 struct session_info
*session_info
)
627 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
633 cds_lfht_node_init(&channel_info
->channels_ht_node
);
634 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
635 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
636 channel_info
->capacity
= channel_capacity
;
638 channel_info
->name
= strdup(channel_name
);
639 if (!channel_info
->name
) {
644 * Set the references between session and channel infos:
645 * - channel_info holds a strong reference to session_info
646 * - session_info holds a weak reference to channel_info
648 session_info_get(session_info
);
649 session_info_add_channel(session_info
, channel_info
);
650 channel_info
->session_info
= session_info
;
654 channel_info_destroy(channel_info
);
659 bool notification_client_list_get(struct notification_client_list
*list
)
661 return urcu_ref_get_unless_zero(&list
->ref
);
665 void free_notification_client_list_rcu(struct rcu_head
*node
)
667 free(caa_container_of(node
, struct notification_client_list
,
672 void notification_client_list_release(struct urcu_ref
*list_ref
)
674 struct notification_client_list
*list
=
675 container_of(list_ref
, typeof(*list
), ref
);
676 struct notification_client_list_element
*client_list_element
, *tmp
;
678 lttng_condition_put(list
->condition
);
680 if (list
->notification_trigger_clients_ht
) {
683 cds_lfht_del(list
->notification_trigger_clients_ht
,
684 &list
->notification_trigger_clients_ht_node
);
686 list
->notification_trigger_clients_ht
= NULL
;
688 cds_list_for_each_entry_safe(client_list_element
, tmp
,
689 &list
->clients_list
, node
) {
690 free(client_list_element
);
693 assert(cds_list_empty(&list
->triggers_list
));
695 pthread_mutex_destroy(&list
->lock
);
696 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
700 bool condition_applies_to_client(const struct lttng_condition
*condition
,
701 struct notification_client
*client
)
703 bool applies
= false;
704 struct lttng_condition_list_element
*condition_list_element
;
706 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
708 applies
= lttng_condition_is_equal(
709 condition_list_element
->condition
,
720 struct notification_client_list
*notification_client_list_create(
721 struct notification_thread_state
*state
,
722 const struct lttng_condition
*condition
)
724 struct notification_client
*client
;
725 struct cds_lfht_iter iter
;
726 struct notification_client_list
*client_list
;
728 client_list
= zmalloc(sizeof(*client_list
));
730 PERROR("Failed to allocate notification client list");
734 pthread_mutex_init(&client_list
->lock
, NULL
);
736 * The trigger that owns the condition has the first reference to this
739 urcu_ref_init(&client_list
->ref
);
740 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
741 CDS_INIT_LIST_HEAD(&client_list
->clients_list
);
742 CDS_INIT_LIST_HEAD(&client_list
->triggers_list
);
745 * Create a copy of the condition so that it's independent of any
746 * trigger. The client list may outlive the trigger object (which owns
747 * the condition) that is used to create it.
749 client_list
->condition
= lttng_condition_copy(condition
);
751 /* Build a list of clients to which this new condition applies. */
752 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
753 client_socket_ht_node
) {
754 struct notification_client_list_element
*client_list_element
;
756 if (!condition_applies_to_client(condition
, client
)) {
760 client_list_element
= zmalloc(sizeof(*client_list_element
));
761 if (!client_list_element
) {
762 goto error_put_client_list
;
765 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
766 client_list_element
->client
= client
;
767 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
770 client_list
->notification_trigger_clients_ht
=
771 state
->notification_trigger_clients_ht
;
775 * Add the client list to the global list of client list.
777 cds_lfht_add_unique(state
->notification_trigger_clients_ht
,
778 lttng_condition_hash(client_list
->condition
),
779 match_client_list_condition
,
780 client_list
->condition
,
781 &client_list
->notification_trigger_clients_ht_node
);
785 error_put_client_list
:
786 notification_client_list_put(client_list
);
793 void notification_client_list_put(struct notification_client_list
*list
)
798 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
801 /* Provides a reference to the returned list. */
803 struct notification_client_list
*get_client_list_from_condition(
804 struct notification_thread_state
*state
,
805 const struct lttng_condition
*condition
)
807 struct cds_lfht_node
*node
;
808 struct cds_lfht_iter iter
;
809 struct notification_client_list
*list
= NULL
;
812 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
813 lttng_condition_hash(condition
),
814 match_client_list_condition
,
817 node
= cds_lfht_iter_get_node(&iter
);
819 list
= container_of(node
, struct notification_client_list
,
820 notification_trigger_clients_ht_node
);
821 list
= notification_client_list_get(list
) ? list
: NULL
;
829 int evaluate_channel_condition_for_client(
830 const struct lttng_condition
*condition
,
831 struct notification_thread_state
*state
,
832 struct lttng_evaluation
**evaluation
,
833 uid_t
*session_uid
, gid_t
*session_gid
)
836 struct cds_lfht_iter iter
;
837 struct cds_lfht_node
*node
;
838 struct channel_info
*channel_info
= NULL
;
839 struct channel_key
*channel_key
= NULL
;
840 struct channel_state_sample
*last_sample
= NULL
;
841 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
845 /* Find the channel associated with the condition. */
846 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
847 channel_trigger_list
, channel_triggers_ht_node
) {
848 struct lttng_trigger_list_element
*element
;
850 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
851 const struct lttng_condition
*current_condition
=
852 lttng_trigger_get_const_condition(
855 assert(current_condition
);
856 if (!lttng_condition_is_equal(condition
,
857 current_condition
)) {
861 /* Found the trigger, save the channel key. */
862 channel_key
= &channel_trigger_list
->channel_key
;
866 /* The channel key was found stop iteration. */
872 /* No channel found; normal exit. */
873 DBG("No known channel associated with newly subscribed-to condition");
878 /* Fetch channel info for the matching channel. */
879 cds_lfht_lookup(state
->channels_ht
,
880 hash_channel_key(channel_key
),
884 node
= cds_lfht_iter_get_node(&iter
);
886 channel_info
= caa_container_of(node
, struct channel_info
,
889 /* Retrieve the channel's last sample, if it exists. */
890 cds_lfht_lookup(state
->channel_state_ht
,
891 hash_channel_key(channel_key
),
892 match_channel_state_sample
,
895 node
= cds_lfht_iter_get_node(&iter
);
897 last_sample
= caa_container_of(node
,
898 struct channel_state_sample
,
899 channel_state_ht_node
);
901 /* Nothing to evaluate, no sample was ever taken. Normal exit */
902 DBG("No channel sample associated with newly subscribed-to condition");
907 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
909 0, channel_info
->session_info
->consumed_data_size
,
912 WARN("Fatal error occurred while evaluating a newly subscribed-to condition");
916 *session_uid
= channel_info
->session_info
->uid
;
917 *session_gid
= channel_info
->session_info
->gid
;
924 const char *get_condition_session_name(const struct lttng_condition
*condition
)
926 const char *session_name
= NULL
;
927 enum lttng_condition_status status
;
929 switch (lttng_condition_get_type(condition
)) {
930 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
931 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
932 status
= lttng_condition_buffer_usage_get_session_name(
933 condition
, &session_name
);
935 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
936 status
= lttng_condition_session_consumed_size_get_session_name(
937 condition
, &session_name
);
939 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
940 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
941 status
= lttng_condition_session_rotation_get_session_name(
942 condition
, &session_name
);
947 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
948 ERR("Failed to retrieve session rotation condition's session name");
956 int evaluate_session_condition_for_client(
957 const struct lttng_condition
*condition
,
958 struct notification_thread_state
*state
,
959 struct lttng_evaluation
**evaluation
,
960 uid_t
*session_uid
, gid_t
*session_gid
)
963 struct cds_lfht_iter iter
;
964 struct cds_lfht_node
*node
;
965 const char *session_name
;
966 struct session_info
*session_info
= NULL
;
969 session_name
= get_condition_session_name(condition
);
971 /* Find the session associated with the trigger. */
972 cds_lfht_lookup(state
->sessions_ht
,
973 hash_key_str(session_name
, lttng_ht_seed
),
977 node
= cds_lfht_iter_get_node(&iter
);
979 DBG("No known session matching name \"%s\"",
985 session_info
= caa_container_of(node
, struct session_info
,
987 session_info_get(session_info
);
990 * Evaluation is performed in-line here since only one type of
991 * session-bound condition is handled for the moment.
993 switch (lttng_condition_get_type(condition
)) {
994 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
995 if (!session_info
->rotation
.ongoing
) {
997 goto end_session_put
;
1000 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1001 session_info
->rotation
.id
);
1004 ERR("Failed to create session rotation ongoing evaluation for session \"%s\"",
1005 session_info
->name
);
1007 goto end_session_put
;
1013 goto end_session_put
;
1016 *session_uid
= session_info
->uid
;
1017 *session_gid
= session_info
->gid
;
1020 session_info_put(session_info
);
1027 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
1028 const struct lttng_condition
*condition
,
1029 struct notification_client
*client
,
1030 struct notification_thread_state
*state
)
1033 struct lttng_evaluation
*evaluation
= NULL
;
1034 struct notification_client_list client_list
= {
1035 .lock
= PTHREAD_MUTEX_INITIALIZER
,
1037 struct notification_client_list_element client_list_element
= { 0 };
1038 uid_t object_uid
= 0;
1039 gid_t object_gid
= 0;
1046 switch (get_condition_binding_object(condition
)) {
1047 case LTTNG_OBJECT_TYPE_SESSION
:
1048 ret
= evaluate_session_condition_for_client(condition
, state
,
1049 &evaluation
, &object_uid
, &object_gid
);
1051 case LTTNG_OBJECT_TYPE_CHANNEL
:
1052 ret
= evaluate_channel_condition_for_client(condition
, state
,
1053 &evaluation
, &object_uid
, &object_gid
);
1055 case LTTNG_OBJECT_TYPE_NONE
:
1056 DBG("Newly subscribed-to condition not bound to object, nothing to evaluate");
1059 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1069 /* Evaluation yielded nothing. Normal exit. */
1070 DBG("Newly subscribed-to condition evaluated to false, nothing to report to client");
1076 * Create a temporary client list with the client currently
1079 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1080 CDS_INIT_LIST_HEAD(&client_list
.clients_list
);
1082 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1083 client_list_element
.client
= client
;
1084 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1086 /* Send evaluation result to the newly-subscribed client. */
1087 DBG("Newly subscribed-to condition evaluated to true, notifying client");
1088 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1089 state
, object_uid
, object_gid
);
1096 int notification_thread_client_subscribe(struct notification_client
*client
,
1097 struct lttng_condition
*condition
,
1098 struct notification_thread_state
*state
,
1099 enum lttng_notification_channel_status
*_status
)
1102 struct notification_client_list
*client_list
= NULL
;
1103 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1104 struct notification_client_list_element
*client_list_element
= NULL
;
1105 struct lttng_trigger_ht_element
*trigger_ht_element
;
1106 enum lttng_notification_channel_status status
=
1107 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1110 * Ensure that the client has not already subscribed to this condition
1113 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1114 if (lttng_condition_is_equal(condition_list_element
->condition
,
1116 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1121 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1122 if (!condition_list_element
) {
1126 client_list_element
= zmalloc(sizeof(*client_list_element
));
1127 if (!client_list_element
) {
1133 * Add the newly-subscribed condition to the client's subscription list.
1135 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1136 condition_list_element
->condition
= condition
;
1138 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1140 client_list
= get_client_list_from_condition(
1141 state
, condition_list_element
->condition
);
1144 * No notification-emiting trigger registered with this
1145 * condition. We don't evaluate the condition right away
1146 * since this trigger is not registered yet.
1148 free(client_list_element
);
1153 * The condition to which the client just subscribed is evaluated
1154 * at this point so that conditions that are already TRUE result
1155 * in a notification being sent out.
1157 * Note the iteration on all triggers which share an identical
1158 * `condition` than the one to which the client is registering. This is
1159 * done to ensure that the client receives a distinct notification for
1160 * all triggers that have a `notify` action that have this condition.
1162 pthread_mutex_lock(&client_list
->lock
);
1163 cds_list_for_each_entry(trigger_ht_element
,
1164 &client_list
->triggers_list
, client_list_trigger_node
) {
1165 if (evaluate_condition_for_client(trigger_ht_element
->trigger
, condition_list_element
->condition
,
1167 WARN("Evaluation of a condition on client subscription failed, aborting.");
1169 free(client_list_element
);
1170 pthread_mutex_unlock(&client_list
->lock
);
1174 pthread_mutex_unlock(&client_list
->lock
);
1177 * Add the client to the list of clients interested in a given trigger
1178 * if a "notification" trigger with a corresponding condition was
1181 client_list_element
->client
= client
;
1182 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1184 pthread_mutex_lock(&client_list
->lock
);
1185 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1186 pthread_mutex_unlock(&client_list
->lock
);
1192 notification_client_list_put(client_list
);
1194 lttng_condition_destroy(condition
);
1197 free(condition_list_element
);
1198 free(client_list_element
);
1199 lttng_condition_destroy(condition
);
1204 int notification_thread_client_unsubscribe(
1205 struct notification_client
*client
,
1206 struct lttng_condition
*condition
,
1207 struct notification_thread_state
*state
,
1208 enum lttng_notification_channel_status
*_status
)
1210 struct notification_client_list
*client_list
;
1211 struct lttng_condition_list_element
*condition_list_element
,
1213 struct notification_client_list_element
*client_list_element
,
1215 bool condition_found
= false;
1216 enum lttng_notification_channel_status status
=
1217 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1219 /* Remove the condition from the client's condition list. */
1220 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1221 &client
->condition_list
, node
) {
1222 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1227 cds_list_del(&condition_list_element
->node
);
1229 * The caller may be iterating on the client's conditions to
1230 * tear down a client's connection. In this case, the condition
1231 * will be destroyed at the end.
1233 if (condition
!= condition_list_element
->condition
) {
1234 lttng_condition_destroy(
1235 condition_list_element
->condition
);
1237 free(condition_list_element
);
1238 condition_found
= true;
1242 if (!condition_found
) {
1243 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1248 * Remove the client from the list of clients interested the trigger
1249 * matching the condition.
1251 client_list
= get_client_list_from_condition(state
, condition
);
1256 pthread_mutex_lock(&client_list
->lock
);
1257 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1258 &client_list
->clients_list
, node
) {
1259 if (client_list_element
->client
->id
!= client
->id
) {
1262 cds_list_del(&client_list_element
->node
);
1263 free(client_list_element
);
1266 pthread_mutex_unlock(&client_list
->lock
);
1267 notification_client_list_put(client_list
);
1270 lttng_condition_destroy(condition
);
1278 void free_notification_client_rcu(struct rcu_head
*node
)
1280 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1284 void notification_client_destroy(struct notification_client
*client
,
1285 struct notification_thread_state
*state
)
1292 * The client object is not reachable by other threads, no need to lock
1295 if (client
->socket
>= 0) {
1296 (void) lttcomm_close_unix_sock(client
->socket
);
1297 client
->socket
= -1;
1299 client
->communication
.active
= false;
1300 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1301 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1302 pthread_mutex_destroy(&client
->lock
);
1303 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1307 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1311 struct notification_client
*get_client_from_socket(int socket
,
1312 struct notification_thread_state
*state
)
1314 struct cds_lfht_iter iter
;
1315 struct cds_lfht_node
*node
;
1316 struct notification_client
*client
= NULL
;
1318 cds_lfht_lookup(state
->client_socket_ht
,
1319 hash_client_socket(socket
),
1320 match_client_socket
,
1321 (void *) (unsigned long) socket
,
1323 node
= cds_lfht_iter_get_node(&iter
);
1328 client
= caa_container_of(node
, struct notification_client
,
1329 client_socket_ht_node
);
1335 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1339 struct notification_client
*get_client_from_id(notification_client_id id
,
1340 struct notification_thread_state
*state
)
1342 struct cds_lfht_iter iter
;
1343 struct cds_lfht_node
*node
;
1344 struct notification_client
*client
= NULL
;
1346 cds_lfht_lookup(state
->client_id_ht
,
1351 node
= cds_lfht_iter_get_node(&iter
);
1356 client
= caa_container_of(node
, struct notification_client
,
1363 bool buffer_usage_condition_applies_to_channel(
1364 const struct lttng_condition
*condition
,
1365 const struct channel_info
*channel_info
)
1367 enum lttng_condition_status status
;
1368 enum lttng_domain_type condition_domain
;
1369 const char *condition_session_name
= NULL
;
1370 const char *condition_channel_name
= NULL
;
1372 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1374 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1375 if (channel_info
->key
.domain
!= condition_domain
) {
1379 status
= lttng_condition_buffer_usage_get_session_name(
1380 condition
, &condition_session_name
);
1381 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1383 status
= lttng_condition_buffer_usage_get_channel_name(
1384 condition
, &condition_channel_name
);
1385 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1387 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1390 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1400 bool session_consumed_size_condition_applies_to_channel(
1401 const struct lttng_condition
*condition
,
1402 const struct channel_info
*channel_info
)
1404 enum lttng_condition_status status
;
1405 const char *condition_session_name
= NULL
;
1407 status
= lttng_condition_session_consumed_size_get_session_name(
1408 condition
, &condition_session_name
);
1409 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1411 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1421 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1422 const struct channel_info
*channel_info
)
1424 const struct lttng_condition
*condition
;
1425 bool trigger_applies
;
1427 condition
= lttng_trigger_get_const_condition(trigger
);
1432 switch (lttng_condition_get_type(condition
)) {
1433 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1434 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1435 trigger_applies
= buffer_usage_condition_applies_to_channel(
1436 condition
, channel_info
);
1438 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1439 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1440 condition
, channel_info
);
1446 return trigger_applies
;
1451 /* Must be called with RCU read lock held. */
1453 struct lttng_session_trigger_list
*get_session_trigger_list(
1454 struct notification_thread_state
*state
,
1455 const char *session_name
)
1457 struct lttng_session_trigger_list
*list
= NULL
;
1458 struct cds_lfht_node
*node
;
1459 struct cds_lfht_iter iter
;
1461 cds_lfht_lookup(state
->session_triggers_ht
,
1462 hash_key_str(session_name
, lttng_ht_seed
),
1463 match_session_trigger_list
,
1466 node
= cds_lfht_iter_get_node(&iter
);
1469 * Not an error, the list of triggers applying to that session
1470 * will be initialized when the session is created.
1472 DBG("No trigger list found for session \"%s\" as it is not yet known to the notification system",
1477 list
= caa_container_of(node
,
1478 struct lttng_session_trigger_list
,
1479 session_triggers_ht_node
);
1485 * Allocate an empty lttng_session_trigger_list for the session named
1488 * No ownership of 'session_name' is assumed by the session trigger list.
1489 * It is the caller's responsability to ensure the session name is alive
1490 * for as long as this list is.
1493 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1494 const char *session_name
,
1495 struct cds_lfht
*session_triggers_ht
)
1497 struct lttng_session_trigger_list
*list
;
1499 list
= zmalloc(sizeof(*list
));
1503 list
->session_name
= session_name
;
1504 CDS_INIT_LIST_HEAD(&list
->list
);
1505 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1506 list
->session_triggers_ht
= session_triggers_ht
;
1509 /* Publish the list through the session_triggers_ht. */
1510 cds_lfht_add(session_triggers_ht
,
1511 hash_key_str(session_name
, lttng_ht_seed
),
1512 &list
->session_triggers_ht_node
);
1519 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1521 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1526 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1528 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1530 /* Empty the list element by element, and then free the list itself. */
1531 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1532 &list
->list
, node
) {
1533 cds_list_del(&trigger_list_element
->node
);
1534 free(trigger_list_element
);
1537 /* Unpublish the list from the session_triggers_ht. */
1538 cds_lfht_del(list
->session_triggers_ht
,
1539 &list
->session_triggers_ht_node
);
1541 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1545 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1546 struct lttng_trigger
*trigger
)
1549 struct lttng_trigger_list_element
*new_element
=
1550 zmalloc(sizeof(*new_element
));
1556 CDS_INIT_LIST_HEAD(&new_element
->node
);
1557 new_element
->trigger
= trigger
;
1558 cds_list_add(&new_element
->node
, &list
->list
);
1564 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1565 const char *session_name
)
1567 bool applies
= false;
1568 const struct lttng_condition
*condition
;
1570 condition
= lttng_trigger_get_const_condition(trigger
);
1571 switch (lttng_condition_get_type(condition
)) {
1572 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1573 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1575 enum lttng_condition_status condition_status
;
1576 const char *condition_session_name
;
1578 condition_status
= lttng_condition_session_rotation_get_session_name(
1579 condition
, &condition_session_name
);
1580 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1581 ERR("Failed to retrieve session rotation condition's session name");
1585 assert(condition_session_name
);
1586 applies
= !strcmp(condition_session_name
, session_name
);
1597 * Allocate and initialize an lttng_session_trigger_list which contains
1598 * all triggers that apply to the session named 'session_name'.
1600 * No ownership of 'session_name' is assumed by the session trigger list.
1601 * It is the caller's responsability to ensure the session name is alive
1602 * for as long as this list is.
1605 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1606 const struct notification_thread_state
*state
,
1607 const char *session_name
)
1609 int trigger_count
= 0;
1610 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1611 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1612 struct cds_lfht_iter iter
;
1614 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1615 state
->session_triggers_ht
);
1617 /* Add all triggers applying to the session named 'session_name'. */
1618 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1622 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1627 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1628 trigger_ht_element
->trigger
);
1636 DBG("Found %i triggers that apply to newly created session",
1638 return session_trigger_list
;
1640 lttng_session_trigger_list_destroy(session_trigger_list
);
1645 struct session_info
*find_or_create_session_info(
1646 struct notification_thread_state
*state
,
1647 const char *name
, uid_t uid
, gid_t gid
)
1649 struct session_info
*session
= NULL
;
1650 struct cds_lfht_node
*node
;
1651 struct cds_lfht_iter iter
;
1652 struct lttng_session_trigger_list
*trigger_list
;
1655 cds_lfht_lookup(state
->sessions_ht
,
1656 hash_key_str(name
, lttng_ht_seed
),
1660 node
= cds_lfht_iter_get_node(&iter
);
1662 DBG("Found session info of session \"%s\" (uid = %i, gid = %i)",
1664 session
= caa_container_of(node
, struct session_info
,
1666 assert(session
->uid
== uid
);
1667 assert(session
->gid
== gid
);
1668 session_info_get(session
);
1672 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1673 if (!trigger_list
) {
1677 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1678 state
->sessions_ht
);
1680 ERR("Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1682 lttng_session_trigger_list_destroy(trigger_list
);
1685 trigger_list
= NULL
;
1687 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1688 &session
->sessions_ht_node
);
1694 session_info_put(session
);
1699 int handle_notification_thread_command_add_channel(
1700 struct notification_thread_state
*state
,
1701 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1702 const char *channel_name
, enum lttng_domain_type channel_domain
,
1703 uint64_t channel_key_int
, uint64_t channel_capacity
,
1704 enum lttng_error_code
*cmd_result
)
1706 struct cds_list_head trigger_list
;
1707 struct channel_info
*new_channel_info
= NULL
;
1708 struct channel_key channel_key
= {
1709 .key
= channel_key_int
,
1710 .domain
= channel_domain
,
1712 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1713 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1714 int trigger_count
= 0;
1715 struct cds_lfht_iter iter
;
1716 struct session_info
*session_info
= NULL
;
1718 DBG("Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1719 channel_name
, session_name
, channel_key_int
,
1720 lttng_domain_type_str(channel_domain
));
1722 CDS_INIT_LIST_HEAD(&trigger_list
);
1724 session_info
= find_or_create_session_info(state
, session_name
,
1725 session_uid
, session_gid
);
1726 if (!session_info
) {
1727 /* Allocation error or an internal error occurred. */
1731 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1732 channel_capacity
, session_info
);
1733 if (!new_channel_info
) {
1738 /* Build a list of all triggers applying to the new channel. */
1739 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1741 struct lttng_trigger_list_element
*new_element
;
1743 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1744 new_channel_info
)) {
1748 new_element
= zmalloc(sizeof(*new_element
));
1753 CDS_INIT_LIST_HEAD(&new_element
->node
);
1754 new_element
->trigger
= trigger_ht_element
->trigger
;
1755 cds_list_add(&new_element
->node
, &trigger_list
);
1760 DBG("Found %i triggers that apply to newly added channel",
1762 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1763 if (!channel_trigger_list
) {
1766 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1767 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1768 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1769 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1772 /* Add channel to the channel_ht which owns the channel_infos. */
1773 cds_lfht_add(state
->channels_ht
,
1774 hash_channel_key(&new_channel_info
->key
),
1775 &new_channel_info
->channels_ht_node
);
1777 * Add the list of triggers associated with this channel to the
1778 * channel_triggers_ht.
1780 cds_lfht_add(state
->channel_triggers_ht
,
1781 hash_channel_key(&new_channel_info
->key
),
1782 &channel_trigger_list
->channel_triggers_ht_node
);
1784 session_info_put(session_info
);
1785 *cmd_result
= LTTNG_OK
;
1788 channel_info_destroy(new_channel_info
);
1789 session_info_put(session_info
);
1794 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1796 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1801 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1803 free(caa_container_of(node
, struct channel_state_sample
,
1808 int handle_notification_thread_command_remove_channel(
1809 struct notification_thread_state
*state
,
1810 uint64_t channel_key
, enum lttng_domain_type domain
,
1811 enum lttng_error_code
*cmd_result
)
1813 struct cds_lfht_node
*node
;
1814 struct cds_lfht_iter iter
;
1815 struct lttng_channel_trigger_list
*trigger_list
;
1816 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1817 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1818 struct channel_info
*channel_info
;
1820 DBG("Removing channel key = %" PRIu64
" in %s domain",
1821 channel_key
, lttng_domain_type_str(domain
));
1825 cds_lfht_lookup(state
->channel_triggers_ht
,
1826 hash_channel_key(&key
),
1827 match_channel_trigger_list
,
1830 node
= cds_lfht_iter_get_node(&iter
);
1832 * There is a severe internal error if we are being asked to remove a
1833 * channel that doesn't exist.
1836 ERR("Channel being removed is unknown to the notification thread");
1840 /* Free the list of triggers associated with this channel. */
1841 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1842 channel_triggers_ht_node
);
1843 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1844 &trigger_list
->list
, node
) {
1845 cds_list_del(&trigger_list_element
->node
);
1846 free(trigger_list_element
);
1848 cds_lfht_del(state
->channel_triggers_ht
, node
);
1849 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1851 /* Free sampled channel state. */
1852 cds_lfht_lookup(state
->channel_state_ht
,
1853 hash_channel_key(&key
),
1854 match_channel_state_sample
,
1857 node
= cds_lfht_iter_get_node(&iter
);
1859 * This is expected to be NULL if the channel is destroyed before we
1860 * received a sample.
1863 struct channel_state_sample
*sample
= caa_container_of(node
,
1864 struct channel_state_sample
,
1865 channel_state_ht_node
);
1867 cds_lfht_del(state
->channel_state_ht
, node
);
1868 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1871 /* Remove the channel from the channels_ht and free it. */
1872 cds_lfht_lookup(state
->channels_ht
,
1873 hash_channel_key(&key
),
1877 node
= cds_lfht_iter_get_node(&iter
);
1879 channel_info
= caa_container_of(node
, struct channel_info
,
1881 cds_lfht_del(state
->channels_ht
, node
);
1882 channel_info_destroy(channel_info
);
1885 *cmd_result
= LTTNG_OK
;
1890 int handle_notification_thread_command_session_rotation(
1891 struct notification_thread_state
*state
,
1892 enum notification_thread_command_type cmd_type
,
1893 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1894 uint64_t trace_archive_chunk_id
,
1895 struct lttng_trace_archive_location
*location
,
1896 enum lttng_error_code
*_cmd_result
)
1899 enum lttng_error_code cmd_result
= LTTNG_OK
;
1900 struct lttng_session_trigger_list
*trigger_list
;
1901 struct lttng_trigger_list_element
*trigger_list_element
;
1902 struct session_info
*session_info
;
1903 const struct lttng_credentials session_creds
= {
1904 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1905 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1910 session_info
= find_or_create_session_info(state
, session_name
,
1911 session_uid
, session_gid
);
1912 if (!session_info
) {
1913 /* Allocation error or an internal error occurred. */
1915 cmd_result
= LTTNG_ERR_NOMEM
;
1919 session_info
->rotation
.ongoing
=
1920 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1921 session_info
->rotation
.id
= trace_archive_chunk_id
;
1922 trigger_list
= get_session_trigger_list(state
, session_name
);
1923 if (!trigger_list
) {
1924 DBG("No triggers applying to session \"%s\" found",
1929 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1931 const struct lttng_condition
*condition
;
1932 struct lttng_trigger
*trigger
;
1933 struct notification_client_list
*client_list
;
1934 struct lttng_evaluation
*evaluation
= NULL
;
1935 enum lttng_condition_type condition_type
;
1936 enum action_executor_status executor_status
;
1938 trigger
= trigger_list_element
->trigger
;
1939 condition
= lttng_trigger_get_const_condition(trigger
);
1941 condition_type
= lttng_condition_get_type(condition
);
1943 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1944 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1946 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1947 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1951 client_list
= get_client_list_from_condition(state
, condition
);
1952 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1953 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1954 trace_archive_chunk_id
);
1956 evaluation
= lttng_evaluation_session_rotation_completed_create(
1957 trace_archive_chunk_id
, location
);
1961 /* Internal error */
1963 cmd_result
= LTTNG_ERR_UNK
;
1968 * Ownership of `evaluation` transferred to the action executor
1969 * no matter the result.
1971 executor_status
= action_executor_enqueue_trigger(
1972 state
->executor
, trigger
, evaluation
,
1973 &session_creds
, client_list
);
1975 switch (executor_status
) {
1976 case ACTION_EXECUTOR_STATUS_OK
:
1978 case ACTION_EXECUTOR_STATUS_ERROR
:
1979 case ACTION_EXECUTOR_STATUS_INVALID
:
1981 * TODO Add trigger identification (name/id) when
1982 * it is added to the API.
1984 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1987 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1989 * TODO Add trigger identification (name/id) when
1990 * it is added to the API.
1992 * Not a fatal error.
1994 WARN("No space left when enqueuing action associated with session rotation trigger");
2002 notification_client_list_put(client_list
);
2003 if (caa_unlikely(ret
)) {
2008 session_info_put(session_info
);
2009 *_cmd_result
= cmd_result
;
2015 int handle_notification_thread_command_add_tracer_event_source(
2016 struct notification_thread_state
*state
,
2017 int tracer_event_source_fd
,
2018 enum lttng_domain_type domain_type
,
2019 enum lttng_error_code
*_cmd_result
)
2022 enum lttng_error_code cmd_result
= LTTNG_OK
;
2023 struct notification_event_tracer_event_source_element
*element
= NULL
;
2025 element
= zmalloc(sizeof(*element
));
2027 cmd_result
= LTTNG_ERR_NOMEM
;
2032 element
->fd
= tracer_event_source_fd
;
2033 element
->domain
= domain_type
;
2035 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2037 DBG3("Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2038 tracer_event_source_fd
,
2039 lttng_domain_type_str(domain_type
));
2041 /* Adding the read side pipe to the event poll. */
2042 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLPRI
| LPOLLIN
| LPOLLERR
);
2044 ERR("Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2045 tracer_event_source_fd
,
2046 lttng_domain_type_str(element
->domain
));
2047 cds_list_del(&element
->node
);
2052 element
->is_fd_in_poll_set
= true;
2055 *_cmd_result
= cmd_result
;
2060 int drain_event_notifier_notification_pipe(
2061 struct notification_thread_state
*state
,
2062 int pipe
, enum lttng_domain_type domain
)
2064 struct lttng_poll_event events
= {0};
2067 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2069 ERR("Error creating lttng_poll_event");
2073 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2075 ERR("Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2082 * Continue to consume notifications as long as there are new
2083 * ones coming in. The tracer has been asked to stop producing
2086 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2087 * monitored (on Linux, at least) and will be returned when
2088 * the pipe is closed but empty.
2090 ret
= lttng_poll_wait_interruptible(&events
, 0);
2091 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2092 /* No more notification to be read on this pipe. */
2095 } else if (ret
< 0) {
2096 PERROR("Failed on lttng_poll_wait_interruptible() call");
2101 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2103 ERR("Error consuming an event notifier notification from pipe: fd = %d",
2108 lttng_poll_clean(&events
);
2113 struct notification_event_tracer_event_source_element
*
2114 find_tracer_event_source_element(struct notification_thread_state
*state
,
2115 int tracer_event_source_fd
)
2117 struct notification_event_tracer_event_source_element
*source_element
;
2119 cds_list_for_each_entry(source_element
,
2120 &state
->tracer_event_sources_list
, node
) {
2121 if (source_element
->fd
== tracer_event_source_fd
) {
2126 source_element
= NULL
;
2128 return source_element
;
2132 int remove_tracer_event_source_from_pollset(
2133 struct notification_thread_state
*state
,
2134 struct notification_event_tracer_event_source_element
*source_element
)
2138 assert(source_element
->is_fd_in_poll_set
);
2140 DBG3("Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2142 lttng_domain_type_str(source_element
->domain
));
2144 /* Removing the fd from the event poll set. */
2145 ret
= lttng_poll_del(&state
->events
, source_element
->fd
);
2147 ERR("Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2149 lttng_domain_type_str(source_element
->domain
));
2154 source_element
->is_fd_in_poll_set
= false;
2157 * Force the notification thread to restart the poll() loop to ensure
2158 * that any events from the removed fd are removed.
2160 state
->restart_poll
= true;
2162 ret
= drain_event_notifier_notification_pipe(state
, source_element
->fd
,
2163 source_element
->domain
);
2165 ERR("Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2167 lttng_domain_type_str(source_element
->domain
));
2176 int handle_notification_thread_tracer_event_source_died(
2177 struct notification_thread_state
*state
,
2178 int tracer_event_source_fd
)
2181 struct notification_event_tracer_event_source_element
*source_element
;
2183 source_element
= find_tracer_event_source_element(state
,
2184 tracer_event_source_fd
);
2186 assert(source_element
);
2188 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2190 ERR("Failed to remove dead tracer event source from poll set");
2197 int handle_notification_thread_command_remove_tracer_event_source(
2198 struct notification_thread_state
*state
,
2199 int tracer_event_source_fd
,
2200 enum lttng_error_code
*_cmd_result
)
2203 enum lttng_error_code cmd_result
= LTTNG_OK
;
2204 struct notification_event_tracer_event_source_element
*source_element
= NULL
;
2206 source_element
= find_tracer_event_source_element(state
,
2207 tracer_event_source_fd
);
2209 assert(source_element
);
2211 /* Remove the tracer source from the list. */
2212 cds_list_del(&source_element
->node
);
2214 if (!source_element
->is_fd_in_poll_set
) {
2215 /* Skip the poll set removal. */
2219 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2221 ERR("Failed to remove tracer event source from poll set");
2222 cmd_result
= LTTNG_ERR_FATAL
;
2226 free(source_element
);
2227 *_cmd_result
= cmd_result
;
2231 static int handle_notification_thread_command_list_triggers(
2232 struct notification_thread_handle
*handle
,
2233 struct notification_thread_state
*state
,
2235 struct lttng_triggers
**triggers
,
2236 enum lttng_error_code
*_cmd_result
)
2239 enum lttng_error_code cmd_result
= LTTNG_OK
;
2240 struct cds_lfht_iter iter
;
2241 struct lttng_trigger_ht_element
*trigger_ht_element
;
2242 struct lttng_triggers
*local_triggers
= NULL
;
2243 const struct lttng_credentials
*creds
;
2247 local_triggers
= lttng_triggers_create();
2248 if (!local_triggers
) {
2249 /* Not a fatal error. */
2250 cmd_result
= LTTNG_ERR_NOMEM
;
2254 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2255 trigger_ht_element
, node
) {
2257 * Only return the triggers to which the client has access.
2258 * The root user has visibility over all triggers.
2260 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2261 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2265 ret
= lttng_triggers_add(local_triggers
,
2266 trigger_ht_element
->trigger
);
2268 /* Not a fatal error. */
2270 cmd_result
= LTTNG_ERR_NOMEM
;
2275 /* Transferring ownership to the caller. */
2276 *triggers
= local_triggers
;
2277 local_triggers
= NULL
;
2281 lttng_triggers_destroy(local_triggers
);
2282 *_cmd_result
= cmd_result
;
2286 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
2287 const char **trigger_name
,
2288 uid_t
*trigger_owner_uid
)
2290 enum lttng_trigger_status trigger_status
;
2292 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
2293 switch (trigger_status
) {
2294 case LTTNG_TRIGGER_STATUS_OK
:
2296 case LTTNG_TRIGGER_STATUS_UNSET
:
2297 *trigger_name
= "(anonymous)";
2303 trigger_status
= lttng_trigger_get_owner_uid(trigger
,
2305 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2308 static int handle_notification_thread_command_get_trigger(
2309 struct notification_thread_state
*state
,
2310 const struct lttng_trigger
*trigger
,
2311 struct lttng_trigger
**registered_trigger
,
2312 enum lttng_error_code
*_cmd_result
)
2315 struct cds_lfht_iter iter
;
2316 struct lttng_trigger_ht_element
*trigger_ht_element
;
2317 enum lttng_error_code cmd_result
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2318 const char *trigger_name
;
2319 uid_t trigger_owner_uid
;
2323 cds_lfht_for_each_entry(
2324 state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2325 if (lttng_trigger_is_equal(
2326 trigger
, trigger_ht_element
->trigger
)) {
2327 /* Take one reference on the return trigger. */
2328 *registered_trigger
= trigger_ht_element
->trigger
;
2329 lttng_trigger_get(*registered_trigger
);
2331 cmd_result
= LTTNG_OK
;
2336 /* Not a fatal error if the trigger is not found. */
2337 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
2338 DBG("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d",
2339 trigger_name
, (int) trigger_owner_uid
);
2345 *_cmd_result
= cmd_result
;
2350 bool condition_is_supported(struct lttng_condition
*condition
)
2354 switch (lttng_condition_get_type(condition
)) {
2355 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2356 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2359 enum lttng_domain_type domain
;
2361 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2365 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2366 is_supported
= true;
2371 * Older kernel tracers don't expose the API to monitor their
2372 * buffers. Therefore, we reject triggers that require that
2373 * mechanism to be available to be evaluated.
2375 * Assume unsupported on error.
2377 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2380 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
2382 const struct lttng_event_rule
*event_rule
;
2383 enum lttng_domain_type domain
;
2384 const enum lttng_condition_status status
=
2385 lttng_condition_event_rule_matches_get_rule(
2386 condition
, &event_rule
);
2388 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2390 domain
= lttng_event_rule_get_domain_type(event_rule
);
2391 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2392 is_supported
= true;
2397 * Older kernel tracers can't emit notification. Therefore, we
2398 * reject triggers that require that mechanism to be available
2401 * Assume unsupported on error.
2403 is_supported
= kernel_supports_event_notifiers() == 1;
2407 is_supported
= true;
2410 return is_supported
;
2413 /* Must be called with RCU read lock held. */
2415 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2416 struct notification_thread_state
*state
)
2419 const struct lttng_condition
*condition
;
2420 const char *session_name
;
2421 struct lttng_session_trigger_list
*trigger_list
;
2423 condition
= lttng_trigger_get_const_condition(trigger
);
2424 switch (lttng_condition_get_type(condition
)) {
2425 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2426 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2428 enum lttng_condition_status status
;
2430 status
= lttng_condition_session_rotation_get_session_name(
2431 condition
, &session_name
);
2432 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2433 ERR("Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2444 trigger_list
= get_session_trigger_list(state
, session_name
);
2445 if (!trigger_list
) {
2446 DBG("Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2452 DBG("Newly registered trigger bound to session \"%s\"",
2454 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2459 /* Must be called with RCU read lock held. */
2461 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2462 struct notification_thread_state
*state
)
2465 struct cds_lfht_node
*node
;
2466 struct cds_lfht_iter iter
;
2467 struct channel_info
*channel
;
2469 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2471 struct lttng_trigger_list_element
*trigger_list_element
;
2472 struct lttng_channel_trigger_list
*trigger_list
;
2473 struct cds_lfht_iter lookup_iter
;
2475 if (!trigger_applies_to_channel(trigger
, channel
)) {
2479 cds_lfht_lookup(state
->channel_triggers_ht
,
2480 hash_channel_key(&channel
->key
),
2481 match_channel_trigger_list
,
2484 node
= cds_lfht_iter_get_node(&lookup_iter
);
2486 trigger_list
= caa_container_of(node
,
2487 struct lttng_channel_trigger_list
,
2488 channel_triggers_ht_node
);
2490 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2491 if (!trigger_list_element
) {
2495 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2496 trigger_list_element
->trigger
= trigger
;
2497 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2498 DBG("Newly registered trigger bound to channel \"%s\"",
2506 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2508 bool is_notify
= false;
2509 unsigned int i
, count
;
2510 enum lttng_action_status action_status
;
2511 const struct lttng_action
*action
=
2512 lttng_trigger_get_const_action(trigger
);
2513 enum lttng_action_type action_type
;
2516 action_type
= lttng_action_get_type(action
);
2517 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2520 } else if (action_type
!= LTTNG_ACTION_TYPE_LIST
) {
2524 action_status
= lttng_action_list_get_count(action
, &count
);
2525 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2527 for (i
= 0; i
< count
; i
++) {
2528 const struct lttng_action
*inner_action
=
2529 lttng_action_list_get_at_index(
2532 action_type
= lttng_action_get_type(inner_action
);
2533 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2543 static bool trigger_name_taken(struct notification_thread_state
*state
,
2544 const struct lttng_trigger
*trigger
)
2546 struct cds_lfht_iter iter
;
2549 * No duplicata is allowed in the triggers_by_name_uid_ht.
2550 * The match is done against the trigger name and uid.
2552 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2553 hash_trigger_by_name_uid(trigger
),
2554 match_trigger_by_name_uid
,
2557 return !!cds_lfht_iter_get_node(&iter
);
2561 enum lttng_error_code
generate_trigger_name(
2562 struct notification_thread_state
*state
,
2563 struct lttng_trigger
*trigger
, const char **name
)
2565 enum lttng_error_code ret_code
= LTTNG_OK
;
2567 enum lttng_trigger_status status
;
2570 const int ret
= lttng_trigger_generate_name(trigger
,
2571 state
->trigger_id
.name_offset
++);
2573 /* The only reason this can fail right now. */
2574 ret_code
= LTTNG_ERR_NOMEM
;
2578 status
= lttng_trigger_get_name(trigger
, name
);
2579 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2581 taken
= trigger_name_taken(state
, trigger
);
2582 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2588 void notif_thread_state_remove_trigger_ht_elem(
2589 struct notification_thread_state
*state
,
2590 struct lttng_trigger_ht_element
*trigger_ht_element
)
2593 assert(trigger_ht_element
);
2595 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2596 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2600 enum lttng_error_code
setup_tracer_notifier(
2601 struct notification_thread_state
*state
,
2602 struct lttng_trigger
*trigger
)
2604 enum lttng_error_code ret
;
2605 enum event_notifier_error_accounting_status error_accounting_status
;
2606 struct cds_lfht_node
*node
;
2607 uint64_t error_counter_index
= 0;
2608 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2609 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2611 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2612 if (!trigger_tokens_ht_element
) {
2613 ret
= LTTNG_ERR_NOMEM
;
2617 /* Add trigger token to the trigger_tokens_ht. */
2618 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2619 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2620 trigger_tokens_ht_element
->trigger
= trigger
;
2622 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2623 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2624 match_trigger_token
,
2625 &trigger_tokens_ht_element
->token
,
2626 &trigger_tokens_ht_element
->node
);
2627 if (node
!= &trigger_tokens_ht_element
->node
) {
2628 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2629 goto error_free_ht_element
;
2632 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2633 trigger
, &error_counter_index
);
2634 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2635 if (error_accounting_status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2636 DBG("Trigger list error accounting counter full.");
2637 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2639 ERR("Error registering trigger for error accounting");
2640 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2643 goto error_remove_ht_element
;
2646 lttng_condition_event_rule_matches_set_error_counter_index(
2647 condition
, error_counter_index
);
2652 error_remove_ht_element
:
2653 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2654 error_free_ht_element
:
2655 free(trigger_tokens_ht_element
);
2661 * FIXME A client's credentials are not checked when registering a trigger.
2663 * The effects of this are benign since:
2664 * - The client will succeed in registering the trigger, as it is valid,
2665 * - The trigger will, internally, be bound to the channel/session,
2666 * - The notifications will not be sent since the client's credentials
2667 * are checked against the channel at that moment.
2669 * If this function returns a non-zero value, it means something is
2670 * fundamentally broken and the whole subsystem/thread will be torn down.
2672 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2676 int handle_notification_thread_command_register_trigger(
2677 struct notification_thread_state
*state
,
2678 struct lttng_trigger
*trigger
,
2679 bool is_trigger_anonymous
,
2680 enum lttng_error_code
*cmd_result
)
2683 struct lttng_condition
*condition
;
2684 struct notification_client_list
*client_list
= NULL
;
2685 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2686 struct cds_lfht_node
*node
;
2687 const char* trigger_name
;
2688 bool free_trigger
= true;
2689 struct lttng_evaluation
*evaluation
= NULL
;
2690 struct lttng_credentials object_creds
;
2693 enum action_executor_status executor_status
;
2694 const uint64_t trigger_tracer_token
=
2695 state
->trigger_id
.next_tracer_token
++;
2699 /* Set the trigger's tracer token. */
2700 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2702 if (!is_trigger_anonymous
) {
2703 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2704 LTTNG_TRIGGER_STATUS_UNSET
) {
2705 const enum lttng_error_code ret_code
=
2706 generate_trigger_name(state
, trigger
,
2709 if (ret_code
!= LTTNG_OK
) {
2712 *cmd_result
= ret_code
;
2715 } else if (trigger_name_taken(state
, trigger
)) {
2716 /* Not a fatal error. */
2717 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2722 trigger_name
= "(anonymous)";
2725 condition
= lttng_trigger_get_condition(trigger
);
2728 /* Some conditions require tracers to implement a minimal ABI version. */
2729 if (!condition_is_supported(condition
)) {
2730 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2734 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2735 if (!trigger_ht_element
) {
2740 /* Add trigger to the trigger_ht. */
2741 cds_lfht_node_init(&trigger_ht_element
->node
);
2742 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2743 trigger_ht_element
->trigger
= trigger
;
2745 node
= cds_lfht_add_unique(state
->triggers_ht
,
2746 lttng_condition_hash(condition
),
2749 &trigger_ht_element
->node
);
2750 if (node
!= &trigger_ht_element
->node
) {
2751 /* Not a fatal error, simply report it to the client. */
2752 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2753 goto error_free_ht_element
;
2756 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2757 hash_trigger_by_name_uid(trigger
),
2758 match_trigger_by_name_uid
,
2760 &trigger_ht_element
->node_by_name_uid
);
2761 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2762 /* Internal error: add to triggers_ht should have failed. */
2764 goto error_free_ht_element
;
2767 /* From this point consider the trigger registered. */
2768 lttng_trigger_set_as_registered(trigger
);
2771 * Some triggers might need a tracer notifier depending on its
2772 * condition and actions.
2774 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2775 enum lttng_error_code error_code
;
2777 error_code
= setup_tracer_notifier(state
, trigger
);
2778 if (error_code
!= LTTNG_OK
) {
2779 notif_thread_state_remove_trigger_ht_elem(state
,
2780 trigger_ht_element
);
2781 if (error_code
== LTTNG_ERR_NOMEM
) {
2784 *cmd_result
= error_code
;
2788 goto error_free_ht_element
;
2793 * The rest only applies to triggers that have a "notify" action.
2794 * It is not skipped as this is the only action type currently
2797 if (is_trigger_action_notify(trigger
)) {
2799 * Find or create the client list of this condition. It may
2800 * already be present if another trigger is already registered
2801 * with the same condition.
2803 client_list
= get_client_list_from_condition(state
, condition
);
2806 * No client list for this condition yet. We create new
2807 * one and build it up.
2809 client_list
= notification_client_list_create(state
, condition
);
2811 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2812 goto error_free_ht_element
;
2816 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2818 pthread_mutex_lock(&client_list
->lock
);
2819 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2820 pthread_mutex_unlock(&client_list
->lock
);
2824 * Ownership of the trigger and of its wrapper was transfered to
2825 * the triggers_ht. Same for token ht element if necessary.
2827 trigger_ht_element
= NULL
;
2828 free_trigger
= false;
2830 switch (get_condition_binding_object(condition
)) {
2831 case LTTNG_OBJECT_TYPE_SESSION
:
2832 /* Add the trigger to the list if it matches a known session. */
2833 ret
= bind_trigger_to_matching_session(trigger
, state
);
2835 goto error_free_ht_element
;
2838 case LTTNG_OBJECT_TYPE_CHANNEL
:
2840 * Add the trigger to list of triggers bound to the channels
2843 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2845 goto error_free_ht_element
;
2848 case LTTNG_OBJECT_TYPE_NONE
:
2851 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2853 goto error_free_ht_element
;
2857 * The new trigger's condition must be evaluated against the current
2860 * In the case of `notify` action, nothing preventing clients from
2861 * subscribing to a condition before the corresponding trigger is
2862 * registered, we have to evaluate this new condition right away.
2864 * At some point, we were waiting for the next "evaluation" (e.g. on
2865 * reception of a channel sample) to evaluate this new condition, but
2868 * The reason it was broken is that waiting for the next sample
2869 * does not allow us to properly handle transitions for edge-triggered
2872 * Consider this example: when we handle a new channel sample, we
2873 * evaluate each conditions twice: once with the previous state, and
2874 * again with the newest state. We then use those two results to
2875 * determine whether a state change happened: a condition was false and
2876 * became true. If a state change happened, we have to notify clients.
2878 * Now, if a client subscribes to a given notification and registers
2879 * a trigger *after* that subscription, we have to make sure the
2880 * condition is evaluated at this point while considering only the
2881 * current state. Otherwise, the next evaluation cycle may only see
2882 * that the evaluations remain the same (true for samples n-1 and n) and
2883 * the client will never know that the condition has been met.
2885 switch (get_condition_binding_object(condition
)) {
2886 case LTTNG_OBJECT_TYPE_SESSION
:
2887 ret
= evaluate_session_condition_for_client(condition
, state
,
2888 &evaluation
, &object_uid
,
2890 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2891 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2893 case LTTNG_OBJECT_TYPE_CHANNEL
:
2894 ret
= evaluate_channel_condition_for_client(condition
, state
,
2895 &evaluation
, &object_uid
,
2897 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2898 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2900 case LTTNG_OBJECT_TYPE_NONE
:
2903 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2911 goto error_free_ht_element
;
2914 DBG("Newly registered trigger's condition evaluated to %s",
2915 evaluation
? "true" : "false");
2917 /* Evaluation yielded nothing. Normal exit. */
2923 * Ownership of `evaluation` transferred to the action executor
2924 * no matter the result.
2926 executor_status
= action_executor_enqueue_trigger(state
->executor
,
2927 trigger
, evaluation
, &object_creds
, client_list
);
2929 switch (executor_status
) {
2930 case ACTION_EXECUTOR_STATUS_OK
:
2932 case ACTION_EXECUTOR_STATUS_ERROR
:
2933 case ACTION_EXECUTOR_STATUS_INVALID
:
2935 * TODO Add trigger identification (name/id) when
2936 * it is added to the API.
2938 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2940 goto error_free_ht_element
;
2941 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2943 * TODO Add trigger identification (name/id) when
2944 * it is added to the API.
2946 * Not a fatal error.
2948 WARN("No space left when enqueuing action associated to newly registered trigger");
2956 *cmd_result
= LTTNG_OK
;
2957 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2958 trigger_name
, trigger_tracer_token
);
2961 error_free_ht_element
:
2962 if (trigger_ht_element
) {
2963 /* Delayed removal due to RCU constraint on delete. */
2964 call_rcu(&trigger_ht_element
->rcu_node
,
2965 free_lttng_trigger_ht_element_rcu
);
2970 * Other objects might have a reference to the trigger, mark it
2973 lttng_trigger_set_as_unregistered(trigger
);
2974 lttng_trigger_destroy(trigger
);
2982 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2984 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2989 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2991 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2996 void teardown_tracer_notifier(struct notification_thread_state
*state
,
2997 const struct lttng_trigger
*trigger
)
2999 struct cds_lfht_iter iter
;
3000 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
3002 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
3003 trigger_tokens_ht_element
, node
) {
3005 if (!lttng_trigger_is_equal(trigger
,
3006 trigger_tokens_ht_element
->trigger
)) {
3010 event_notifier_error_accounting_unregister_event_notifier(
3011 trigger_tokens_ht_element
->trigger
);
3013 /* TODO talk to all app and remove it */
3014 DBG("Removed trigger from tokens_ht");
3015 cds_lfht_del(state
->trigger_tokens_ht
,
3016 &trigger_tokens_ht_element
->node
);
3018 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
3019 free_notification_trigger_tokens_ht_element_rcu
);
3026 int handle_notification_thread_command_unregister_trigger(
3027 struct notification_thread_state
*state
,
3028 const struct lttng_trigger
*trigger
,
3029 enum lttng_error_code
*_cmd_reply
)
3031 struct cds_lfht_iter iter
;
3032 struct cds_lfht_node
*triggers_ht_node
;
3033 struct lttng_channel_trigger_list
*trigger_list
;
3034 struct notification_client_list
*client_list
;
3035 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
3036 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
3038 enum lttng_error_code cmd_reply
;
3042 cds_lfht_lookup(state
->triggers_ht
,
3043 lttng_condition_hash(condition
),
3047 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3048 if (!triggers_ht_node
) {
3049 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3052 cmd_reply
= LTTNG_OK
;
3055 trigger_ht_element
= caa_container_of(triggers_ht_node
,
3056 struct lttng_trigger_ht_element
, node
);
3058 /* Remove trigger from channel_triggers_ht. */
3059 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
3060 channel_triggers_ht_node
) {
3061 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3063 cds_list_for_each_entry_safe(trigger_element
, tmp
,
3064 &trigger_list
->list
, node
) {
3065 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3069 DBG("Removed trigger from channel_triggers_ht");
3070 cds_list_del(&trigger_element
->node
);
3071 /* A trigger can only appear once per channel */
3076 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3077 teardown_tracer_notifier(state
, trigger
);
3080 if (is_trigger_action_notify(trigger
)) {
3082 * Remove and release the client list from
3083 * notification_trigger_clients_ht.
3085 client_list
= get_client_list_from_condition(state
, condition
);
3086 assert(client_list
);
3088 pthread_mutex_lock(&client_list
->lock
);
3089 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3090 pthread_mutex_unlock(&client_list
->lock
);
3092 /* Put new reference and the hashtable's reference. */
3093 notification_client_list_put(client_list
);
3094 notification_client_list_put(client_list
);
3098 /* Remove trigger from triggers_ht. */
3099 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3101 /* Release the ownership of the trigger. */
3102 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3103 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3107 *_cmd_reply
= cmd_reply
;
3113 int pop_cmd_queue(struct notification_thread_handle
*handle
,
3114 struct notification_thread_command
**cmd
)
3119 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3120 ret
= lttng_read(handle
->cmd_queue
.event_fd
, &counter
, sizeof(counter
));
3121 if (ret
!= sizeof(counter
)) {
3126 /* Simulate behaviour of EFD_SEMAPHORE for older kernels. */
3129 ret
= lttng_write(handle
->cmd_queue
.event_fd
, &counter
,
3131 if (ret
!= sizeof(counter
)) {
3132 PERROR("Failed to write back to event_fd for EFD_SEMAPHORE emulation");
3138 *cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3139 struct notification_thread_command
, cmd_list_node
);
3140 cds_list_del(&((*cmd
)->cmd_list_node
));
3144 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3148 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3149 int handle_notification_thread_command(
3150 struct notification_thread_handle
*handle
,
3151 struct notification_thread_state
*state
)
3154 struct notification_thread_command
*cmd
;
3156 ret
= pop_cmd_queue(handle
, &cmd
);
3161 DBG("Received `%s` command",
3162 notification_command_type_str(cmd
->type
));
3163 switch (cmd
->type
) {
3164 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3165 ret
= handle_notification_thread_command_register_trigger(state
,
3166 cmd
->parameters
.register_trigger
.trigger
,
3167 cmd
->parameters
.register_trigger
.is_trigger_anonymous
,
3170 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3171 ret
= handle_notification_thread_command_unregister_trigger(
3173 cmd
->parameters
.unregister_trigger
.trigger
,
3176 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3177 ret
= handle_notification_thread_command_add_channel(
3179 cmd
->parameters
.add_channel
.session
.name
,
3180 cmd
->parameters
.add_channel
.session
.uid
,
3181 cmd
->parameters
.add_channel
.session
.gid
,
3182 cmd
->parameters
.add_channel
.channel
.name
,
3183 cmd
->parameters
.add_channel
.channel
.domain
,
3184 cmd
->parameters
.add_channel
.channel
.key
,
3185 cmd
->parameters
.add_channel
.channel
.capacity
,
3188 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3189 ret
= handle_notification_thread_command_remove_channel(
3190 state
, cmd
->parameters
.remove_channel
.key
,
3191 cmd
->parameters
.remove_channel
.domain
,
3194 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3195 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3196 ret
= handle_notification_thread_command_session_rotation(
3199 cmd
->parameters
.session_rotation
.session_name
,
3200 cmd
->parameters
.session_rotation
.uid
,
3201 cmd
->parameters
.session_rotation
.gid
,
3202 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3203 cmd
->parameters
.session_rotation
.location
,
3206 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3207 ret
= handle_notification_thread_command_add_tracer_event_source(
3209 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3210 cmd
->parameters
.tracer_event_source
.domain
,
3213 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3214 ret
= handle_notification_thread_command_remove_tracer_event_source(
3216 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3219 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3221 struct lttng_triggers
*triggers
= NULL
;
3223 ret
= handle_notification_thread_command_list_triggers(
3226 cmd
->parameters
.list_triggers
.uid
,
3229 cmd
->reply
.list_triggers
.triggers
= triggers
;
3233 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3234 cmd
->reply_code
= LTTNG_OK
;
3237 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3239 struct lttng_trigger
*trigger
= NULL
;
3241 ret
= handle_notification_thread_command_get_trigger(state
,
3242 cmd
->parameters
.get_trigger
.trigger
, &trigger
,
3244 cmd
->reply
.get_trigger
.trigger
= trigger
;
3247 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3249 const enum client_transmission_status client_status
=
3250 cmd
->parameters
.client_communication_update
3252 const notification_client_id client_id
=
3253 cmd
->parameters
.client_communication_update
.id
;
3254 struct notification_client
*client
;
3257 client
= get_client_from_id(client_id
, state
);
3261 * Client error was probably already picked-up by the
3262 * notification thread or it has disconnected
3263 * gracefully while this command was queued.
3265 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3269 ret
= client_handle_transmission_status(
3270 client
, client_status
, state
);
3276 ERR("Unknown internal command received");
3284 if (cmd
->is_async
) {
3288 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3292 /* Wake-up and return a fatal error to the calling thread. */
3293 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3294 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3296 /* Indicate a fatal error to the caller. */
3301 int socket_set_non_blocking(int socket
)
3305 /* Set the pipe as non-blocking. */
3306 ret
= fcntl(socket
, F_GETFL
, 0);
3308 PERROR("fcntl get socket flags");
3313 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3315 PERROR("fcntl set O_NONBLOCK socket flag");
3318 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3324 int client_reset_inbound_state(struct notification_client
*client
)
3329 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3331 client
->communication
.inbound
.bytes_to_receive
=
3332 sizeof(struct lttng_notification_channel_message
);
3333 client
->communication
.inbound
.msg_type
=
3334 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3335 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3336 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3337 ret
= lttng_dynamic_buffer_set_size(
3338 &client
->communication
.inbound
.payload
.buffer
,
3339 client
->communication
.inbound
.bytes_to_receive
);
3344 int handle_notification_thread_client_connect(
3345 struct notification_thread_state
*state
)
3348 struct notification_client
*client
;
3350 DBG("Handling new notification channel client connection");
3352 client
= zmalloc(sizeof(*client
));
3359 pthread_mutex_init(&client
->lock
, NULL
);
3360 client
->id
= state
->next_notification_client_id
++;
3361 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3362 lttng_payload_init(&client
->communication
.inbound
.payload
);
3363 lttng_payload_init(&client
->communication
.outbound
.payload
);
3364 client
->communication
.inbound
.expect_creds
= true;
3366 ret
= client_reset_inbound_state(client
);
3368 ERR("Failed to reset client communication's inbound state");
3373 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3375 ERR("Failed to accept new notification channel client connection");
3380 client
->socket
= ret
;
3382 ret
= socket_set_non_blocking(client
->socket
);
3384 ERR("Failed to set new notification channel client connection socket as non-blocking");
3388 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3390 ERR("Failed to set socket options on new notification channel client socket");
3395 client
->communication
.current_poll_events
= CLIENT_POLL_EVENTS_IN
;
3396 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3397 client
->communication
.current_poll_events
);
3399 ERR("Failed to add notification channel client socket to poll set");
3403 DBG("Added new notification channel client socket (%i) to poll set",
3407 cds_lfht_add(state
->client_socket_ht
,
3408 hash_client_socket(client
->socket
),
3409 &client
->client_socket_ht_node
);
3410 cds_lfht_add(state
->client_id_ht
,
3411 hash_client_id(client
->id
),
3412 &client
->client_id_ht_node
);
3418 notification_client_destroy(client
, state
);
3423 * RCU read-lock must be held by the caller.
3424 * Client lock must _not_ be held by the caller.
3427 int notification_thread_client_disconnect(
3428 struct notification_client
*client
,
3429 struct notification_thread_state
*state
)
3432 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3434 /* Acquire the client lock to disable its communication atomically. */
3435 pthread_mutex_lock(&client
->lock
);
3436 client
->communication
.active
= false;
3437 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3438 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3439 pthread_mutex_unlock(&client
->lock
);
3441 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3443 ERR("Failed to remove client socket %d from poll set",
3447 /* Release all conditions to which the client was subscribed. */
3448 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3449 &client
->condition_list
, node
) {
3450 (void) notification_thread_client_unsubscribe(client
,
3451 condition_list_element
->condition
, state
, NULL
);
3455 * Client no longer accessible to other threads (through the
3458 notification_client_destroy(client
, state
);
3462 int handle_notification_thread_client_disconnect(
3463 int client_socket
, struct notification_thread_state
*state
)
3466 struct notification_client
*client
;
3469 DBG("Closing client connection (socket fd = %i)",
3471 client
= get_client_from_socket(client_socket
, state
);
3473 /* Internal state corruption, fatal error. */
3474 ERR("Unable to find client (socket fd = %i)",
3480 ret
= notification_thread_client_disconnect(client
, state
);
3486 int handle_notification_thread_client_disconnect_all(
3487 struct notification_thread_state
*state
)
3489 struct cds_lfht_iter iter
;
3490 struct notification_client
*client
;
3491 bool error_encoutered
= false;
3494 DBG("Closing all client connections");
3495 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3496 client_socket_ht_node
) {
3499 ret
= notification_thread_client_disconnect(
3502 error_encoutered
= true;
3506 return error_encoutered
? 1 : 0;
3509 int handle_notification_thread_trigger_unregister_all(
3510 struct notification_thread_state
*state
)
3512 bool error_occurred
= false;
3513 struct cds_lfht_iter iter
;
3514 struct lttng_trigger_ht_element
*trigger_ht_element
;
3517 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3519 int ret
= handle_notification_thread_command_unregister_trigger(
3520 state
, trigger_ht_element
->trigger
, NULL
);
3522 error_occurred
= true;
3526 return error_occurred
? -1 : 0;
3530 bool client_has_outbound_data_left(
3531 const struct notification_client
*client
)
3533 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3534 &client
->communication
.outbound
.payload
, 0, -1);
3535 const bool has_data
= pv
.buffer
.size
!= 0;
3536 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3538 return has_data
|| has_fds
;
3542 int client_handle_transmission_status(
3543 struct notification_client
*client
,
3544 enum client_transmission_status transmission_status
,
3545 struct notification_thread_state
*state
)
3549 switch (transmission_status
) {
3550 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3551 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3553 int current_poll_events
;
3554 int new_poll_events
;
3556 * We want to be notified whenever there is buffer space
3557 * available to send the rest of the payload if we are
3558 * waiting to send data to the client.
3560 * The state of the outbound queue being sampled here is
3562 * - it is okay to wake-up "for nothing" in case we see
3563 * that data is left, but another thread succeeds in
3564 * flushing it before us when handling the client "out"
3565 * event. We will simply stop monitoring that event the next
3566 * time it wakes us up and we see no data left to be sent,
3567 * - if another thread fails to flush the entire client
3568 * outgoing queue, it will issue a "communication update"
3569 * command and cause the client's (e)poll mask to be
3572 * The situation we seek to avoid would be to disable the
3573 * monitoring of "out" client events indefinitely when there is
3574 * data to be sent, which can't happen because of the
3575 * aforementioned "communication update" mechanism.
3577 pthread_mutex_lock(&client
->lock
);
3578 current_poll_events
= client
->communication
.current_poll_events
;
3579 new_poll_events
= client_has_outbound_data_left(client
) ?
3580 CLIENT_POLL_EVENTS_IN_OUT
:
3581 CLIENT_POLL_EVENTS_IN
;
3582 client
->communication
.current_poll_events
= new_poll_events
;
3583 pthread_mutex_unlock(&client
->lock
);
3585 /* Update the monitored event set only if it changed. */
3586 if (current_poll_events
!= new_poll_events
) {
3587 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3596 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3597 ret
= notification_thread_client_disconnect(client
, state
);
3602 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3612 /* Client lock must be acquired by caller. */
3614 enum client_transmission_status
client_flush_outgoing_queue(
3615 struct notification_client
*client
)
3618 size_t to_send_count
;
3619 enum client_transmission_status status
;
3620 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3621 &client
->communication
.outbound
.payload
, 0, -1);
3622 const int fds_to_send_count
=
3623 lttng_payload_view_get_fd_handle_count(&pv
);
3625 ASSERT_LOCKED(client
->lock
);
3627 if (!client
->communication
.active
) {
3628 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3632 if (pv
.buffer
.size
== 0) {
3634 * If both data and fds are equal to zero, we are in an invalid
3637 assert(fds_to_send_count
!= 0);
3642 to_send_count
= pv
.buffer
.size
;
3643 DBG("Flushing client (socket fd = %i) outgoing queue",
3646 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3649 if ((ret
>= 0 && ret
< to_send_count
)) {
3650 DBG("Client (socket fd = %i) outgoing queue could not be completely flushed",
3652 to_send_count
-= max(ret
, 0);
3654 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3656 pv
.buffer
.size
- to_send_count
,
3658 ret
= lttng_dynamic_buffer_set_size(
3659 &client
->communication
.outbound
.payload
.buffer
,
3665 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3667 } else if (ret
< 0) {
3668 /* Generic error, disable the client's communication. */
3669 ERR("Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3671 client
->communication
.active
= false;
3672 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3676 * No error and flushed the queue completely.
3678 * The payload buffer size is used later to
3679 * check if there is notifications queued. So albeit that the
3680 * direct caller knows that the transmission is complete, we
3681 * need to set the buffer size to zero.
3683 ret
= lttng_dynamic_buffer_set_size(
3684 &client
->communication
.outbound
.payload
.buffer
, 0);
3691 /* No fds to send, transmission is complete. */
3692 if (fds_to_send_count
== 0) {
3693 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3697 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3698 client
->socket
, &pv
);
3700 /* Generic error, disable the client's communication. */
3701 ERR("Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3703 client
->communication
.active
= false;
3704 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3706 } else if (ret
== 0) {
3707 /* Nothing could be sent. */
3708 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3710 /* Fd passing is an all or nothing kind of thing. */
3711 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3713 * The payload _fd_array count is used later to
3714 * check if there is notifications queued. So although the
3715 * direct caller knows that the transmission is complete, we
3716 * need to clear the _fd_array for the queuing check.
3718 lttng_dynamic_pointer_array_clear(
3719 &client
->communication
.outbound
.payload
3724 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3725 client
->communication
.outbound
.queued_command_reply
= false;
3726 client
->communication
.outbound
.dropped_notification
= false;
3727 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3732 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3735 /* Client lock must _not_ be held by the caller. */
3737 int client_send_command_reply(struct notification_client
*client
,
3738 struct notification_thread_state
*state
,
3739 enum lttng_notification_channel_status status
)
3742 struct lttng_notification_channel_command_reply reply
= {
3743 .status
= (int8_t) status
,
3745 struct lttng_notification_channel_message msg
= {
3746 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3747 .size
= sizeof(reply
),
3749 char buffer
[sizeof(msg
) + sizeof(reply
)];
3750 enum client_transmission_status transmission_status
;
3752 memcpy(buffer
, &msg
, sizeof(msg
));
3753 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3754 DBG("Send command reply (%i)", (int) status
);
3756 pthread_mutex_lock(&client
->lock
);
3757 if (client
->communication
.outbound
.queued_command_reply
) {
3758 /* Protocol error. */
3762 /* Enqueue buffer to outgoing queue and flush it. */
3763 ret
= lttng_dynamic_buffer_append(
3764 &client
->communication
.outbound
.payload
.buffer
,
3765 buffer
, sizeof(buffer
));
3770 transmission_status
= client_flush_outgoing_queue(client
);
3772 if (client_has_outbound_data_left(client
)) {
3773 /* Queue could not be emptied. */
3774 client
->communication
.outbound
.queued_command_reply
= true;
3777 pthread_mutex_unlock(&client
->lock
);
3778 ret
= client_handle_transmission_status(
3779 client
, transmission_status
, state
);
3786 pthread_mutex_unlock(&client
->lock
);
3792 int client_handle_message_unknown(struct notification_client
*client
,
3793 struct notification_thread_state
*state
)
3797 * Receiving message header. The function will be called again
3798 * once the rest of the message as been received and can be
3801 const struct lttng_notification_channel_message
*msg
;
3803 assert(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3804 msg
= (const struct lttng_notification_channel_message
*)
3805 client
->communication
.inbound
.payload
.buffer
.data
;
3807 if (msg
->size
== 0 ||
3808 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3809 ERR("Invalid notification channel message: length = %u",
3815 switch (msg
->type
) {
3816 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3817 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3818 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3822 ERR("Invalid notification channel message: unexpected message type");
3826 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3827 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3828 client
->communication
.inbound
.msg_type
=
3829 (enum lttng_notification_channel_message_type
) msg
->type
;
3830 ret
= lttng_dynamic_buffer_set_size(
3831 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3833 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3840 int client_handle_message_handshake(struct notification_client
*client
,
3841 struct notification_thread_state
*state
)
3844 struct lttng_notification_channel_command_handshake
*handshake_client
;
3845 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3846 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3847 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3849 const struct lttng_notification_channel_message msg_header
= {
3850 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3851 .size
= sizeof(handshake_reply
),
3853 enum lttng_notification_channel_status status
=
3854 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3855 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3857 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3858 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3859 sizeof(handshake_reply
));
3862 (struct lttng_notification_channel_command_handshake
*)
3863 client
->communication
.inbound
.payload
.buffer
3865 client
->major
= handshake_client
->major
;
3866 client
->minor
= handshake_client
->minor
;
3867 if (!client
->communication
.inbound
.creds_received
) {
3868 ERR("No credentials received from client");
3873 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3874 &client
->communication
.inbound
.creds
);
3875 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3876 &client
->communication
.inbound
.creds
);
3877 client
->is_sessiond
= LTTNG_SOCK_GET_PID_CRED(&client
->communication
.inbound
.creds
) == getpid();
3878 DBG("Received handshake from client: uid = %u, gid = %u, protocol version = %i.%i, client is sessiond = %s",
3879 client
->uid
, client
->gid
, (int) client
->major
,
3880 (int) client
->minor
,
3881 client
->is_sessiond
? "true" : "false");
3883 if (handshake_client
->major
!=
3884 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3885 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3888 pthread_mutex_lock(&client
->lock
);
3889 /* Outgoing queue will be flushed when the command reply is sent. */
3890 ret
= lttng_dynamic_buffer_append(
3891 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3892 sizeof(send_buffer
));
3894 ERR("Failed to send protocol version to notification channel client");
3898 client
->validated
= true;
3899 client
->communication
.active
= true;
3900 pthread_mutex_unlock(&client
->lock
);
3902 /* Set reception state to receive the next message header. */
3903 ret
= client_reset_inbound_state(client
);
3905 ERR("Failed to reset client communication's inbound state");
3909 /* Flushes the outgoing queue. */
3910 ret
= client_send_command_reply(client
, state
, status
);
3912 ERR("Failed to send reply to notification channel client");
3918 pthread_mutex_unlock(&client
->lock
);
3924 int client_handle_message_subscription(
3925 struct notification_client
*client
,
3926 enum lttng_notification_channel_message_type msg_type
,
3927 struct notification_thread_state
*state
)
3930 struct lttng_condition
*condition
;
3931 enum lttng_notification_channel_status status
=
3932 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3933 struct lttng_payload_view condition_view
=
3934 lttng_payload_view_from_payload(
3935 &client
->communication
.inbound
.payload
,
3937 size_t expected_condition_size
;
3940 * No need to lock client to sample the inbound state as the only
3941 * other thread accessing clients (action executor) only uses the
3944 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3945 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3946 if (ret
!= expected_condition_size
) {
3947 ERR("Malformed condition received from client");
3951 /* Ownership of condition is always transferred. */
3952 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3953 ret
= notification_thread_client_subscribe(
3954 client
, condition
, state
, &status
);
3956 ret
= notification_thread_client_unsubscribe(
3957 client
, condition
, state
, &status
);
3964 /* Set reception state to receive the next message header. */
3965 ret
= client_reset_inbound_state(client
);
3967 ERR("Failed to reset client communication's inbound state");
3971 ret
= client_send_command_reply(client
, state
, status
);
3973 ERR("Failed to send reply to notification channel client");
3982 int client_dispatch_message(struct notification_client
*client
,
3983 struct notification_thread_state
*state
)
3987 if (client
->communication
.inbound
.msg_type
!=
3988 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3989 client
->communication
.inbound
.msg_type
!=
3990 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3991 !client
->validated
) {
3992 WARN("client attempted a command before handshake");
3997 switch (client
->communication
.inbound
.msg_type
) {
3998 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
4000 ret
= client_handle_message_unknown(client
, state
);
4003 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
4005 ret
= client_handle_message_handshake(client
, state
);
4008 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
4009 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
4011 ret
= client_handle_message_subscription(client
,
4012 client
->communication
.inbound
.msg_type
, state
);
4022 /* Incoming data from client. */
4023 int handle_notification_thread_client_in(
4024 struct notification_thread_state
*state
, int socket
)
4027 struct notification_client
*client
;
4032 client
= get_client_from_socket(socket
, state
);
4034 /* Internal error, abort. */
4039 if (client
->communication
.inbound
.bytes_to_receive
== 0 &&
4040 client
->communication
.inbound
.fds_to_receive
!= 0) {
4041 /* Only FDs left to receive. */
4045 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
4046 client
->communication
.inbound
.bytes_to_receive
;
4047 if (client
->communication
.inbound
.expect_creds
) {
4048 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
4049 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
4050 client
->communication
.inbound
.bytes_to_receive
,
4051 &client
->communication
.inbound
.creds
);
4053 client
->communication
.inbound
.expect_creds
= false;
4054 client
->communication
.inbound
.creds_received
= true;
4057 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
4058 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
4059 client
->communication
.inbound
.bytes_to_receive
);
4061 if (recv_ret
>= 0) {
4062 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
4064 goto error_disconnect_client
;
4067 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
4068 /* Message incomplete wait for more data. */
4074 assert(client
->communication
.inbound
.bytes_to_receive
== 0);
4077 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
4078 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
4080 client
->communication
.inbound
.fds_to_receive
,
4081 &client
->communication
.inbound
.payload
);
4084 * Fds received. non blocking fds passing is all
4087 ssize_t expected_size
;
4089 expected_size
= sizeof(int) *
4090 client
->communication
.inbound
4092 assert(ret
== expected_size
);
4093 client
->communication
.inbound
.fds_to_receive
= 0;
4094 } else if (ret
== 0) {
4095 /* Received nothing. */
4099 goto error_disconnect_client
;
4103 /* At this point the message is complete.*/
4104 assert(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4105 client
->communication
.inbound
.fds_to_receive
== 0);
4106 ret
= client_dispatch_message(client
, state
);
4109 * Only returns an error if this client must be
4112 goto error_disconnect_client
;
4119 error_disconnect_client
:
4120 ret
= notification_thread_client_disconnect(client
, state
);
4124 /* Client ready to receive outgoing data. */
4125 int handle_notification_thread_client_out(
4126 struct notification_thread_state
*state
, int socket
)
4129 struct notification_client
*client
;
4130 enum client_transmission_status transmission_status
;
4133 client
= get_client_from_socket(socket
, state
);
4135 /* Internal error, abort. */
4140 pthread_mutex_lock(&client
->lock
);
4141 if (!client_has_outbound_data_left(client
)) {
4143 * A client "out" event can be received when no payload is left
4144 * to send under some circumstances.
4146 * Many threads can flush a client's outgoing queue and, if they
4147 * had to queue their message (socket was full), will use the
4148 * "communication update" command to signal the (e)poll thread
4149 * to monitor for space being made available in the socket.
4151 * Commands are sent over an internal pipe serviced by the same
4152 * thread as the client sockets.
4154 * When space is made available in the socket, there is a race
4155 * between the (e)poll thread and the other threads that may
4156 * wish to use the client's socket to flush its outgoing queue.
4158 * A non-(e)poll thread may attempt (and succeed) in flushing
4159 * the queue before the (e)poll thread gets a chance to service
4160 * the client's "out" event.
4162 * In this situation, the (e)poll thread processing the client
4163 * out event will see an empty payload: there is nothing to do
4164 * except unsubscribing (e)poll "out" events.
4166 * Note that this thread is the (e)poll thread so it can modify
4167 * the (e)poll mask directly without using a communication
4168 * update command. Other threads that flush the outgoing queue
4169 * will use the "communication update" command to wake up this
4170 * thread and force it to monitor "out" events.
4172 * When other threads succeed in emptying the outgoing queue,
4173 * they don't need to update the (e)poll mask: if the "out"
4174 * event is monitored, it will fire once and the (e)poll
4175 * thread will reach this condition, causing the event to
4176 * stop being monitored.
4178 transmission_status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
4180 transmission_status
= client_flush_outgoing_queue(client
);
4182 pthread_mutex_unlock(&client
->lock
);
4184 ret
= client_handle_transmission_status(
4185 client
, transmission_status
, state
);
4195 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4196 const struct channel_state_sample
*sample
,
4197 uint64_t buffer_capacity
)
4199 bool result
= false;
4201 enum lttng_condition_type condition_type
;
4202 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4203 condition
, struct lttng_condition_buffer_usage
,
4206 if (use_condition
->threshold_bytes
.set
) {
4207 threshold
= use_condition
->threshold_bytes
.value
;
4210 * Threshold was expressed as a ratio.
4212 * TODO the threshold (in bytes) of conditions expressed
4213 * as a ratio of total buffer size could be cached to
4214 * forego this double-multiplication or it could be performed
4215 * as fixed-point math.
4217 * Note that caching should accommodates the case where the
4218 * condition applies to multiple channels (i.e. don't assume
4219 * that all channels matching my_chann* have the same size...)
4221 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4222 (double) buffer_capacity
);
4225 condition_type
= lttng_condition_get_type(condition
);
4226 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4227 DBG("Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4228 threshold
, sample
->highest_usage
);
4231 * The low condition should only be triggered once _all_ of the
4232 * streams in a channel have gone below the "low" threshold.
4234 if (sample
->highest_usage
<= threshold
) {
4238 DBG("High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4239 threshold
, sample
->highest_usage
);
4242 * For high buffer usage scenarios, we want to trigger whenever
4243 * _any_ of the streams has reached the "high" threshold.
4245 if (sample
->highest_usage
>= threshold
) {
4254 bool evaluate_session_consumed_size_condition(
4255 const struct lttng_condition
*condition
,
4256 uint64_t session_consumed_size
)
4259 const struct lttng_condition_session_consumed_size
*size_condition
=
4260 container_of(condition
,
4261 struct lttng_condition_session_consumed_size
,
4264 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4265 DBG("Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4266 threshold
, session_consumed_size
);
4267 return session_consumed_size
>= threshold
;
4271 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4272 struct lttng_evaluation
**evaluation
,
4273 const struct notification_thread_state
*state
,
4274 const struct channel_state_sample
*previous_sample
,
4275 const struct channel_state_sample
*latest_sample
,
4276 uint64_t previous_session_consumed_total
,
4277 uint64_t latest_session_consumed_total
,
4278 struct channel_info
*channel_info
)
4281 enum lttng_condition_type condition_type
;
4282 const bool previous_sample_available
= !!previous_sample
;
4283 bool previous_sample_result
= false;
4284 bool latest_sample_result
;
4286 condition_type
= lttng_condition_get_type(condition
);
4288 switch (condition_type
) {
4289 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4290 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4291 if (caa_likely(previous_sample_available
)) {
4292 previous_sample_result
=
4293 evaluate_buffer_usage_condition(condition
,
4294 previous_sample
, channel_info
->capacity
);
4296 latest_sample_result
= evaluate_buffer_usage_condition(
4297 condition
, latest_sample
,
4298 channel_info
->capacity
);
4300 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4301 if (caa_likely(previous_sample_available
)) {
4302 previous_sample_result
=
4303 evaluate_session_consumed_size_condition(
4305 previous_session_consumed_total
);
4307 latest_sample_result
=
4308 evaluate_session_consumed_size_condition(
4310 latest_session_consumed_total
);
4313 /* Unknown condition type; internal error. */
4317 if (!latest_sample_result
||
4318 (previous_sample_result
== latest_sample_result
)) {
4320 * Only trigger on a condition evaluation transition.
4322 * NOTE: This edge-triggered logic may not be appropriate for
4323 * future condition types.
4328 if (!evaluation
|| !latest_sample_result
) {
4332 switch (condition_type
) {
4333 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4334 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4335 *evaluation
= lttng_evaluation_buffer_usage_create(
4337 latest_sample
->highest_usage
,
4338 channel_info
->capacity
);
4340 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4341 *evaluation
= lttng_evaluation_session_consumed_size_create(
4342 latest_session_consumed_total
);
4357 int client_notification_overflow(struct notification_client
*client
)
4360 const struct lttng_notification_channel_message msg
= {
4361 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4364 ASSERT_LOCKED(client
->lock
);
4366 DBG("Dropping notification addressed to client (socket fd = %i)",
4368 if (client
->communication
.outbound
.dropped_notification
) {
4370 * The client already has a "notification dropped" message
4371 * in its outgoing queue. Nothing to do since all
4372 * of those messages are coalesced.
4377 client
->communication
.outbound
.dropped_notification
= true;
4378 ret
= lttng_dynamic_buffer_append(
4379 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4382 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4389 static int client_handle_transmission_status_wrapper(
4390 struct notification_client
*client
,
4391 enum client_transmission_status status
,
4394 return client_handle_transmission_status(client
, status
,
4395 (struct notification_thread_state
*) user_data
);
4399 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4400 const struct lttng_evaluation
*evaluation
,
4401 struct notification_client_list
* client_list
,
4402 struct notification_thread_state
*state
,
4403 uid_t object_uid
, gid_t object_gid
)
4405 const struct lttng_credentials creds
= {
4406 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4407 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4410 return notification_client_list_send_evaluation(client_list
,
4411 trigger
, evaluation
,
4413 client_handle_transmission_status_wrapper
, state
);
4417 * Permission checks relative to notification channel clients are performed
4418 * here. Notice how object, client, and trigger credentials are involved in
4421 * The `object` credentials are the credentials associated with the "subject"
4422 * of a condition. For instance, a `rotation completed` condition applies
4423 * to a session. When that condition is met, it will produce an evaluation
4424 * against a session. Hence, in this case, the `object` credentials are the
4425 * credentials of the "subject" session.
4427 * The `trigger` credentials are the credentials of the user that registered the
4430 * The `client` credentials are the credentials of the user that created a given
4431 * notification channel.
4433 * In terms of visibility, it is expected that non-privilieged users can only
4434 * register triggers against "their" objects (their own sessions and
4435 * applications they are allowed to interact with). They can then open a
4436 * notification channel and subscribe to notifications associated with those
4439 * As for privilieged users, they can register triggers against the objects of
4440 * other users. They can then subscribe to the notifications associated to their
4441 * triggers. Privilieged users _can't_ subscribe to the notifications of
4442 * triggers owned by other users; they must create their own triggers.
4444 * This is more a concern of usability than security. It would be difficult for
4445 * a root user reliably subscribe to a specific set of conditions without
4446 * interference from external users (those could, for instance, unregister
4450 int notification_client_list_send_evaluation(
4451 struct notification_client_list
*client_list
,
4452 const struct lttng_trigger
*trigger
,
4453 const struct lttng_evaluation
*evaluation
,
4454 const struct lttng_credentials
*source_object_creds
,
4455 report_client_transmission_result_cb client_report
,
4459 struct lttng_payload msg_payload
;
4460 struct notification_client_list_element
*client_list_element
, *tmp
;
4461 const struct lttng_notification notification
= {
4462 .trigger
= (struct lttng_trigger
*) trigger
,
4463 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4465 struct lttng_notification_channel_message msg_header
= {
4466 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4468 const struct lttng_credentials
*trigger_creds
=
4469 lttng_trigger_get_credentials(trigger
);
4471 lttng_payload_init(&msg_payload
);
4473 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4474 sizeof(msg_header
));
4479 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4481 ERR("Failed to serialize notification");
4486 /* Update payload size. */
4487 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4488 ->size
= (uint32_t)(
4489 msg_payload
.buffer
.size
- sizeof(msg_header
));
4491 /* Update the payload number of fds. */
4493 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4494 &msg_payload
, 0, -1);
4496 ((struct lttng_notification_channel_message
*)
4497 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4498 lttng_payload_view_get_fd_handle_count(&pv
);
4501 pthread_mutex_lock(&client_list
->lock
);
4502 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4503 &client_list
->clients_list
, node
) {
4504 enum client_transmission_status transmission_status
;
4505 struct notification_client
*client
=
4506 client_list_element
->client
;
4509 pthread_mutex_lock(&client
->lock
);
4510 if (!client
->communication
.active
) {
4512 * Skip inactive client (protocol error or
4515 DBG("Skipping client at it is marked as inactive");
4519 if (lttng_trigger_is_hidden(trigger
) && !client
->is_sessiond
) {
4521 * Notifications resulting from an hidden trigger are
4522 * only sent to the session daemon.
4524 DBG("Skipping client as the trigger is hidden and the client is not the session daemon");
4528 if (source_object_creds
) {
4529 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4530 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4533 * Client is not allowed to monitor this
4536 DBG("Skipping client at it does not have the object permission to receive notification for this trigger");
4541 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
)) {
4542 DBG("Skipping client at it does not have the permission to receive notification for this trigger");
4546 DBG("Sending notification to client (fd = %i, %zu bytes)",
4547 client
->socket
, msg_payload
.buffer
.size
);
4549 if (client_has_outbound_data_left(client
)) {
4551 * Outgoing data is already buffered for this client;
4552 * drop the notification and enqueue a "dropped
4553 * notification" message if this is the first dropped
4554 * notification since the socket spilled-over to the
4557 ret
= client_notification_overflow(client
);
4564 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4570 transmission_status
= client_flush_outgoing_queue(client
);
4571 pthread_mutex_unlock(&client
->lock
);
4572 ret
= client_report(client
, transmission_status
, user_data
);
4575 goto end_unlock_list
;
4581 pthread_mutex_unlock(&client
->lock
);
4584 goto end_unlock_list
;
4590 pthread_mutex_unlock(&client_list
->lock
);
4592 lttng_payload_reset(&msg_payload
);
4597 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4598 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4602 struct lttng_event_notifier_notification
*notification
= NULL
;
4603 char *capture_buffer
= NULL
;
4604 size_t capture_buffer_size
;
4605 void *reception_buffer
;
4606 size_t reception_size
;
4608 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4609 struct lttng_kernel_abi_event_notifier_notification kernel_notification
;
4611 /* Init lttng_event_notifier_notification */
4613 case LTTNG_DOMAIN_UST
:
4614 reception_buffer
= (void *) &ust_notification
;
4615 reception_size
= sizeof(ust_notification
);
4617 case LTTNG_DOMAIN_KERNEL
:
4618 reception_buffer
= (void *) &kernel_notification
;
4619 reception_size
= sizeof(kernel_notification
);
4626 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4627 * ensuring that read/write of tracer notifications are atomic.
4629 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4631 if (ret
!= reception_size
) {
4632 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4633 notification_pipe_read_fd
, reception_size
, ret
);
4639 case LTTNG_DOMAIN_UST
:
4640 token
= ust_notification
.token
;
4641 capture_buffer_size
= ust_notification
.capture_buf_size
;
4643 case LTTNG_DOMAIN_KERNEL
:
4644 token
= kernel_notification
.token
;
4645 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4651 if (capture_buffer_size
== 0) {
4652 capture_buffer
= NULL
;
4656 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4657 ERR("Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4658 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4662 capture_buffer
= zmalloc(capture_buffer_size
);
4663 if (!capture_buffer
) {
4664 ERR("Failed to allocate capture buffer");
4668 /* Fetch additional payload (capture). */
4669 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4670 if (ret
!= capture_buffer_size
) {
4671 ERR("Failed to read from event source pipe (fd = %i)",
4672 notification_pipe_read_fd
);
4677 notification
= lttng_event_notifier_notification_create(token
, domain
,
4678 capture_buffer
, capture_buffer_size
);
4679 if (notification
== NULL
) {
4684 * Ownership transfered to the lttng_event_notifier_notification object.
4686 capture_buffer
= NULL
;
4689 free(capture_buffer
);
4690 return notification
;
4694 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4695 struct lttng_event_notifier_notification
*notification
)
4697 struct cds_lfht_node
*node
;
4698 struct cds_lfht_iter iter
;
4699 struct notification_trigger_tokens_ht_element
*element
;
4700 struct lttng_evaluation
*evaluation
= NULL
;
4701 enum action_executor_status executor_status
;
4702 struct notification_client_list
*client_list
= NULL
;
4704 unsigned int capture_count
= 0;
4706 /* Find triggers associated with this token. */
4708 cds_lfht_lookup(state
->trigger_tokens_ht
,
4709 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4710 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4711 node
= cds_lfht_iter_get_node(&iter
);
4712 if (caa_unlikely(!node
)) {
4714 * This is not an error, slow consumption of the tracer
4715 * notifications can lead to situations where a trigger is
4716 * removed but we still get tracer notifications matching a
4717 * trigger that no longer exists.
4723 element
= caa_container_of(node
,
4724 struct notification_trigger_tokens_ht_element
,
4727 if (lttng_condition_event_rule_matches_get_capture_descriptor_count(
4728 lttng_trigger_get_const_condition(element
->trigger
),
4729 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4730 ERR("Failed to get capture count");
4735 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4736 ERR("Expected capture but capture buffer is null");
4741 evaluation
= lttng_evaluation_event_rule_matches_create(
4742 container_of(lttng_trigger_get_const_condition(
4744 struct lttng_condition_event_rule_matches
,
4746 notification
->capture_buffer
,
4747 notification
->capture_buf_size
, false);
4749 if (evaluation
== NULL
) {
4750 ERR("Failed to create event rule matches evaluation while creating and enqueuing action executor job");
4755 client_list
= get_client_list_from_condition(state
,
4756 lttng_trigger_get_const_condition(element
->trigger
));
4757 executor_status
= action_executor_enqueue_trigger(state
->executor
,
4758 element
->trigger
, evaluation
, NULL
, client_list
);
4759 switch (executor_status
) {
4760 case ACTION_EXECUTOR_STATUS_OK
:
4763 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4765 struct notification_client_list_element
*client_list_element
,
4769 * Not a fatal error; this is expected and simply means the
4770 * executor has too much work queued already.
4774 /* No clients subscribed to notifications for this trigger. */
4779 /* Warn clients that a notification (or more) was dropped. */
4780 pthread_mutex_lock(&client_list
->lock
);
4781 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4782 &client_list
->clients_list
, node
) {
4783 enum client_transmission_status transmission_status
;
4784 struct notification_client
*client
=
4785 client_list_element
->client
;
4787 pthread_mutex_lock(&client
->lock
);
4788 ret
= client_notification_overflow(client
);
4794 transmission_status
=
4795 client_flush_outgoing_queue(client
);
4796 ret
= client_handle_transmission_status(
4797 client
, transmission_status
, state
);
4803 pthread_mutex_unlock(&client
->lock
);
4809 pthread_mutex_unlock(&client_list
->lock
);
4812 case ACTION_EXECUTOR_STATUS_INVALID
:
4813 case ACTION_EXECUTOR_STATUS_ERROR
:
4814 /* Fatal error, shut down everything. */
4815 ERR("Fatal error encoutered while enqueuing action to the action executor");
4819 /* Unhandled error. */
4824 notification_client_list_put(client_list
);
4831 int handle_one_event_notifier_notification(
4832 struct notification_thread_state
*state
,
4833 int pipe
, enum lttng_domain_type domain
)
4836 struct lttng_event_notifier_notification
*notification
= NULL
;
4838 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4839 if (notification
== NULL
) {
4840 /* Reception failed, don't consider it fatal. */
4841 ERR("Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4842 pipe
, lttng_domain_type_str(domain
));
4846 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4848 ERR("Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4849 pipe
, lttng_domain_type_str(domain
));
4854 lttng_event_notifier_notification_destroy(notification
);
4858 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4859 int pipe
, enum lttng_domain_type domain
)
4861 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4864 int handle_notification_thread_channel_sample(
4865 struct notification_thread_state
*state
, int pipe
,
4866 enum lttng_domain_type domain
)
4869 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4870 struct channel_info
*channel_info
;
4871 struct cds_lfht_node
*node
;
4872 struct cds_lfht_iter iter
;
4873 struct lttng_channel_trigger_list
*trigger_list
;
4874 struct lttng_trigger_list_element
*trigger_list_element
;
4875 bool previous_sample_available
= false;
4876 struct channel_state_sample previous_sample
, latest_sample
;
4877 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4878 struct lttng_credentials channel_creds
;
4881 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4882 * ensuring that read/write of sampling messages are atomic.
4884 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4885 if (ret
!= sizeof(sample_msg
)) {
4886 ERR("Failed to read from monitoring pipe (fd = %i)",
4893 latest_sample
.key
.key
= sample_msg
.key
;
4894 latest_sample
.key
.domain
= domain
;
4895 latest_sample
.highest_usage
= sample_msg
.highest
;
4896 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4897 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4901 /* Retrieve the channel's informations */
4902 cds_lfht_lookup(state
->channels_ht
,
4903 hash_channel_key(&latest_sample
.key
),
4907 node
= cds_lfht_iter_get_node(&iter
);
4908 if (caa_unlikely(!node
)) {
4910 * Not an error since the consumer can push a sample to the pipe
4911 * and the rest of the session daemon could notify us of the
4912 * channel's destruction before we get a chance to process that
4915 DBG("Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4916 latest_sample
.key
.key
,
4917 lttng_domain_type_str(domain
));
4920 channel_info
= caa_container_of(node
, struct channel_info
,
4922 DBG("Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4924 latest_sample
.key
.key
,
4925 channel_info
->session_info
->name
,
4926 latest_sample
.highest_usage
,
4927 latest_sample
.lowest_usage
,
4928 latest_sample
.channel_total_consumed
);
4930 previous_session_consumed_total
=
4931 channel_info
->session_info
->consumed_data_size
;
4933 /* Retrieve the channel's last sample, if it exists, and update it. */
4934 cds_lfht_lookup(state
->channel_state_ht
,
4935 hash_channel_key(&latest_sample
.key
),
4936 match_channel_state_sample
,
4939 node
= cds_lfht_iter_get_node(&iter
);
4940 if (caa_likely(node
)) {
4941 struct channel_state_sample
*stored_sample
;
4943 /* Update the sample stored. */
4944 stored_sample
= caa_container_of(node
,
4945 struct channel_state_sample
,
4946 channel_state_ht_node
);
4948 memcpy(&previous_sample
, stored_sample
,
4949 sizeof(previous_sample
));
4950 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4951 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4952 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4953 previous_sample_available
= true;
4955 latest_session_consumed_total
=
4956 previous_session_consumed_total
+
4957 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4960 * This is the channel's first sample, allocate space for and
4961 * store the new sample.
4963 struct channel_state_sample
*stored_sample
;
4965 stored_sample
= zmalloc(sizeof(*stored_sample
));
4966 if (!stored_sample
) {
4971 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4972 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4973 cds_lfht_add(state
->channel_state_ht
,
4974 hash_channel_key(&stored_sample
->key
),
4975 &stored_sample
->channel_state_ht_node
);
4977 latest_session_consumed_total
=
4978 previous_session_consumed_total
+
4979 latest_sample
.channel_total_consumed
;
4982 channel_info
->session_info
->consumed_data_size
=
4983 latest_session_consumed_total
;
4985 /* Find triggers associated with this channel. */
4986 cds_lfht_lookup(state
->channel_triggers_ht
,
4987 hash_channel_key(&latest_sample
.key
),
4988 match_channel_trigger_list
,
4991 node
= cds_lfht_iter_get_node(&iter
);
4992 if (caa_likely(!node
)) {
4996 channel_creds
= (typeof(channel_creds
)) {
4997 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
4998 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
5001 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
5002 channel_triggers_ht_node
);
5003 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
5005 const struct lttng_condition
*condition
;
5006 struct lttng_trigger
*trigger
;
5007 struct notification_client_list
*client_list
= NULL
;
5008 struct lttng_evaluation
*evaluation
= NULL
;
5009 enum action_executor_status executor_status
;
5012 trigger
= trigger_list_element
->trigger
;
5013 condition
= lttng_trigger_get_const_condition(trigger
);
5017 * Check if any client is subscribed to the result of this
5020 client_list
= get_client_list_from_condition(state
, condition
);
5022 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
5023 previous_sample_available
? &previous_sample
: NULL
,
5025 previous_session_consumed_total
,
5026 latest_session_consumed_total
,
5028 if (caa_unlikely(ret
)) {
5032 if (caa_likely(!evaluation
)) {
5037 * Ownership of `evaluation` transferred to the action executor
5038 * no matter the result.
5040 executor_status
= action_executor_enqueue_trigger(
5041 state
->executor
, trigger
, evaluation
,
5042 &channel_creds
, client_list
);
5044 switch (executor_status
) {
5045 case ACTION_EXECUTOR_STATUS_OK
:
5047 case ACTION_EXECUTOR_STATUS_ERROR
:
5048 case ACTION_EXECUTOR_STATUS_INVALID
:
5050 * TODO Add trigger identification (name/id) when
5051 * it is added to the API.
5053 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
5056 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
5058 * TODO Add trigger identification (name/id) when
5059 * it is added to the API.
5061 * Not a fatal error.
5063 WARN("No space left when enqueuing action associated with buffer-condition trigger");
5071 notification_client_list_put(client_list
);
5072 if (caa_unlikely(ret
)) {