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.hpp"
12 #include <urcu/rculfhash.h>
14 #include <common/defaults.hpp>
15 #include <common/error.hpp>
16 #include <common/futex.hpp>
17 #include <common/unix.hpp>
18 #include <common/dynamic-buffer.hpp>
19 #include <common/hashtable/utils.hpp>
20 #include <common/sessiond-comm/sessiond-comm.hpp>
21 #include <common/macros.hpp>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.hpp>
24 #include <lttng/action/list-internal.hpp>
25 #include <lttng/domain-internal.hpp>
26 #include <lttng/notification/notification-internal.hpp>
27 #include <lttng/condition/condition-internal.hpp>
28 #include <lttng/condition/buffer-usage-internal.hpp>
29 #include <lttng/condition/session-consumed-size-internal.hpp>
30 #include <lttng/condition/session-rotation-internal.hpp>
31 #include <lttng/condition/event-rule-matches-internal.hpp>
32 #include <lttng/domain-internal.hpp>
33 #include <lttng/notification/channel-internal.hpp>
34 #include <lttng/trigger/trigger-internal.hpp>
35 #include <lttng/event-rule/event-rule-internal.hpp>
42 #include "condition-internal.hpp"
43 #include "event-notifier-error-accounting.hpp"
44 #include "notification-thread.hpp"
45 #include "notification-thread-events.hpp"
46 #include "notification-thread-commands.hpp"
47 #include "lttng-sessiond.hpp"
50 #define CLIENT_POLL_EVENTS_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
51 #define CLIENT_POLL_EVENTS_IN_OUT (CLIENT_POLL_EVENTS_IN | LPOLLOUT)
53 /* The tracers currently limit the capture size to PIPE_BUF (4kb on linux). */
54 #define MAX_CAPTURE_SIZE (PIPE_BUF)
56 enum lttng_object_type
{
57 LTTNG_OBJECT_TYPE_UNKNOWN
,
58 LTTNG_OBJECT_TYPE_NONE
,
59 LTTNG_OBJECT_TYPE_CHANNEL
,
60 LTTNG_OBJECT_TYPE_SESSION
,
63 struct lttng_channel_trigger_list
{
64 struct channel_key channel_key
;
65 /* List of struct lttng_trigger_list_element. */
66 struct cds_list_head list
;
67 /* Node in the channel_triggers_ht */
68 struct cds_lfht_node channel_triggers_ht_node
;
69 /* call_rcu delayed reclaim. */
70 struct rcu_head rcu_node
;
74 * List of triggers applying to a given session.
77 * - lttng_session_trigger_list_create()
78 * - lttng_session_trigger_list_build()
79 * - lttng_session_trigger_list_destroy()
80 * - lttng_session_trigger_list_add()
82 struct lttng_session_trigger_list
{
84 * Not owned by this; points to the session_info structure's
87 const char *session_name
;
88 /* List of struct lttng_trigger_list_element. */
89 struct cds_list_head list
;
90 /* Node in the session_triggers_ht */
91 struct cds_lfht_node session_triggers_ht_node
;
93 * Weak reference to the notification system's session triggers
96 * The session trigger list structure structure is owned by
97 * the session's session_info.
99 * The session_info is kept alive the the channel_infos holding a
100 * reference to it (reference counting). When those channels are
101 * destroyed (at runtime or on teardown), the reference they hold
102 * to the session_info are released. On destruction of session_info,
103 * session_info_destroy() will remove the list of triggers applying
104 * to this session from the notification system's state.
106 * This implies that the session_triggers_ht must be destroyed
107 * after the channels.
109 struct cds_lfht
*session_triggers_ht
;
110 /* Used for delayed RCU reclaim. */
111 struct rcu_head rcu_node
;
115 struct lttng_trigger_list_element
{
116 /* No ownership of the trigger object is assumed. */
117 struct lttng_trigger
*trigger
;
118 struct cds_list_head 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
;
146 static unsigned long hash_channel_key(struct channel_key
*key
);
147 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
148 struct lttng_evaluation
**evaluation
,
149 const struct notification_thread_state
*state
,
150 const struct channel_state_sample
*previous_sample
,
151 const struct channel_state_sample
*latest_sample
,
152 uint64_t previous_session_consumed_total
,
153 uint64_t latest_session_consumed_total
,
154 struct channel_info
*channel_info
);
156 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
157 const struct lttng_evaluation
*evaluation
,
158 struct notification_client_list
*client_list
,
159 struct notification_thread_state
*state
,
160 uid_t channel_uid
, gid_t channel_gid
);
163 /* session_info API */
165 void session_info_destroy(void *_data
);
167 void session_info_get(struct session_info
*session_info
);
169 void session_info_put(struct session_info
*session_info
);
171 struct session_info
*session_info_create(const char *name
,
172 uid_t uid
, gid_t gid
,
173 struct lttng_session_trigger_list
*trigger_list
,
174 struct cds_lfht
*sessions_ht
);
176 void session_info_add_channel(struct session_info
*session_info
,
177 struct channel_info
*channel_info
);
179 void session_info_remove_channel(struct session_info
*session_info
,
180 struct channel_info
*channel_info
);
182 /* lttng_session_trigger_list API */
184 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
185 const char *session_name
,
186 struct cds_lfht
*session_triggers_ht
);
188 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
189 const struct notification_thread_state
*state
,
190 const char *session_name
);
192 void lttng_session_trigger_list_destroy(
193 struct lttng_session_trigger_list
*list
);
195 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
196 struct lttng_trigger
*trigger
);
199 int client_handle_transmission_status(
200 struct notification_client
*client
,
201 enum client_transmission_status transmission_status
,
202 struct notification_thread_state
*state
);
205 int handle_one_event_notifier_notification(
206 struct notification_thread_state
*state
,
207 int pipe
, enum lttng_domain_type domain
);
210 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
213 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
215 /* This double-cast is intended to supress pointer-to-cast warning. */
216 const int socket
= (int) (intptr_t) key
;
217 const struct notification_client
*client
= caa_container_of(node
,
218 struct notification_client
, client_socket_ht_node
);
220 return client
->socket
== socket
;
224 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
226 /* This double-cast is intended to supress pointer-to-cast warning. */
227 const notification_client_id id
= *((notification_client_id
*) key
);
228 const struct notification_client
*client
= caa_container_of(
229 node
, struct notification_client
, client_id_ht_node
);
231 return client
->id
== id
;
235 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
237 struct channel_key
*channel_key
= (struct channel_key
*) key
;
238 struct lttng_channel_trigger_list
*trigger_list
;
240 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
241 channel_triggers_ht_node
);
243 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
244 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
248 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
250 const char *session_name
= (const char *) key
;
251 struct lttng_session_trigger_list
*trigger_list
;
253 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
254 session_triggers_ht_node
);
256 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
260 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
262 struct channel_key
*channel_key
= (struct channel_key
*) key
;
263 struct channel_state_sample
*sample
;
265 sample
= caa_container_of(node
, struct channel_state_sample
,
266 channel_state_ht_node
);
268 return !!((channel_key
->key
== sample
->key
.key
) &&
269 (channel_key
->domain
== sample
->key
.domain
));
273 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
275 struct channel_key
*channel_key
= (struct channel_key
*) key
;
276 struct channel_info
*channel_info
;
278 channel_info
= caa_container_of(node
, struct channel_info
,
281 return !!((channel_key
->key
== channel_info
->key
.key
) &&
282 (channel_key
->domain
== channel_info
->key
.domain
));
286 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
288 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
289 struct lttng_trigger_ht_element
*trigger_ht_element
;
291 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
294 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
298 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
300 const uint64_t *_key
= (uint64_t *) key
;
301 struct notification_trigger_tokens_ht_element
*element
;
303 element
= caa_container_of(node
,
304 struct notification_trigger_tokens_ht_element
, node
);
305 return *_key
== element
->token
;
309 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
311 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
312 struct notification_client_list
*client_list
;
313 const struct lttng_condition
*condition
;
315 LTTNG_ASSERT(condition_key
);
317 client_list
= caa_container_of(node
, struct notification_client_list
,
318 notification_trigger_clients_ht_node
);
319 condition
= client_list
->condition
;
321 return !!lttng_condition_is_equal(condition_key
, condition
);
325 int match_session(struct cds_lfht_node
*node
, const void *key
)
327 const char *name
= (const char *) key
;
328 struct session_info
*session_info
= caa_container_of(
329 node
, struct session_info
, sessions_ht_node
);
331 return !strcmp(session_info
->name
, name
);
335 const char *notification_command_type_str(
336 enum notification_thread_command_type type
)
339 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
340 return "REGISTER_TRIGGER";
341 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
342 return "UNREGISTER_TRIGGER";
343 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
344 return "ADD_CHANNEL";
345 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
346 return "REMOVE_CHANNEL";
347 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
348 return "SESSION_ROTATION_ONGOING";
349 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
350 return "SESSION_ROTATION_COMPLETED";
351 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
352 return "ADD_TRACER_EVENT_SOURCE";
353 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
354 return "REMOVE_TRACER_EVENT_SOURCE";
355 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
356 return "LIST_TRIGGERS";
357 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
358 return "GET_TRIGGER";
359 case NOTIFICATION_COMMAND_TYPE_QUIT
:
361 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
362 return "CLIENT_COMMUNICATION_UPDATE";
369 * Match trigger based on name and credentials only.
370 * Name duplication is NOT allowed for the same uid.
373 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
377 const char *element_trigger_name
;
378 const char *key_name
;
379 enum lttng_trigger_status status
;
380 const struct lttng_credentials
*key_creds
;
381 const struct lttng_credentials
*node_creds
;
382 const struct lttng_trigger
*trigger_key
=
383 (const struct lttng_trigger
*) key
;
384 const struct lttng_trigger_ht_element
*trigger_ht_element
=
385 caa_container_of(node
,
386 struct lttng_trigger_ht_element
,
389 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
,
390 &element_trigger_name
);
391 element_trigger_name
= status
== LTTNG_TRIGGER_STATUS_OK
?
392 element_trigger_name
: NULL
;
394 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
395 key_name
= status
== LTTNG_TRIGGER_STATUS_OK
? key_name
: NULL
;
399 * Consider null names as not equal. This is to maintain backwards
400 * compatibility with pre-2.13 anonymous triggers. Multiples anonymous
401 * triggers are allowed for a given user.
403 if (!element_trigger_name
|| !key_name
) {
407 if (strcmp(element_trigger_name
, key_name
) != 0) {
411 /* Compare the owners' UIDs. */
412 key_creds
= lttng_trigger_get_credentials(trigger_key
);
413 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
415 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
422 * Hash trigger based on name and credentials only.
425 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
427 unsigned long hash
= 0;
428 const struct lttng_credentials
*trigger_creds
;
429 const char *trigger_name
;
430 enum lttng_trigger_status status
;
432 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
433 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
434 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
437 trigger_creds
= lttng_trigger_get_credentials(trigger
);
438 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
445 unsigned long hash_channel_key(struct channel_key
*key
)
447 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
448 unsigned long domain_hash
= hash_key_ulong(
449 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
451 return key_hash
^ domain_hash
;
455 unsigned long hash_client_socket(int socket
)
457 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
461 unsigned long hash_client_id(notification_client_id id
)
463 return hash_key_u64(&id
, lttng_ht_seed
);
467 * Get the type of object to which a given condition applies. Bindings let
468 * the notification system evaluate a trigger's condition when a given
469 * object's state is updated.
471 * For instance, a condition bound to a channel will be evaluated everytime
472 * the channel's state is changed by a channel monitoring sample.
475 enum lttng_object_type
get_condition_binding_object(
476 const struct lttng_condition
*condition
)
478 switch (lttng_condition_get_type(condition
)) {
479 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
480 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
481 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
482 return LTTNG_OBJECT_TYPE_CHANNEL
;
483 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
484 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
485 return LTTNG_OBJECT_TYPE_SESSION
;
486 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
487 return LTTNG_OBJECT_TYPE_NONE
;
489 return LTTNG_OBJECT_TYPE_UNKNOWN
;
494 void free_channel_info_rcu(struct rcu_head
*node
)
496 free(caa_container_of(node
, struct channel_info
, rcu_node
));
500 void channel_info_destroy(struct channel_info
*channel_info
)
506 if (channel_info
->session_info
) {
507 session_info_remove_channel(channel_info
->session_info
,
509 session_info_put(channel_info
->session_info
);
511 if (channel_info
->name
) {
512 free(channel_info
->name
);
514 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
518 void free_session_info_rcu(struct rcu_head
*node
)
520 free(caa_container_of(node
, struct session_info
, rcu_node
));
523 /* Don't call directly, use the ref-counting mechanism. */
525 void session_info_destroy(void *_data
)
527 struct session_info
*session_info
= (struct session_info
*) _data
;
530 LTTNG_ASSERT(session_info
);
531 if (session_info
->channel_infos_ht
) {
532 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
534 ERR("Failed to destroy channel information hash table");
537 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
540 cds_lfht_del(session_info
->sessions_ht
,
541 &session_info
->sessions_ht_node
);
543 free(session_info
->name
);
544 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
548 void session_info_get(struct session_info
*session_info
)
553 lttng_ref_get(&session_info
->ref
);
557 void session_info_put(struct session_info
*session_info
)
562 lttng_ref_put(&session_info
->ref
);
566 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
567 struct lttng_session_trigger_list
*trigger_list
,
568 struct cds_lfht
*sessions_ht
)
570 struct session_info
*session_info
;
574 session_info
= zmalloc
<struct session_info
>();
578 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
580 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
581 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
582 if (!session_info
->channel_infos_ht
) {
586 cds_lfht_node_init(&session_info
->sessions_ht_node
);
587 session_info
->name
= strdup(name
);
588 if (!session_info
->name
) {
591 session_info
->uid
= uid
;
592 session_info
->gid
= gid
;
593 session_info
->trigger_list
= trigger_list
;
594 session_info
->sessions_ht
= sessions_ht
;
598 session_info_put(session_info
);
603 void session_info_add_channel(struct session_info
*session_info
,
604 struct channel_info
*channel_info
)
607 cds_lfht_add(session_info
->channel_infos_ht
,
608 hash_channel_key(&channel_info
->key
),
609 &channel_info
->session_info_channels_ht_node
);
614 void session_info_remove_channel(struct session_info
*session_info
,
615 struct channel_info
*channel_info
)
618 cds_lfht_del(session_info
->channel_infos_ht
,
619 &channel_info
->session_info_channels_ht_node
);
624 struct channel_info
*channel_info_create(const char *channel_name
,
625 struct channel_key
*channel_key
, uint64_t channel_capacity
,
626 struct session_info
*session_info
)
628 struct channel_info
*channel_info
= zmalloc
<struct channel_info
>();
634 cds_lfht_node_init(&channel_info
->channels_ht_node
);
635 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
636 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
637 channel_info
->capacity
= channel_capacity
;
639 channel_info
->name
= strdup(channel_name
);
640 if (!channel_info
->name
) {
645 * Set the references between session and channel infos:
646 * - channel_info holds a strong reference to session_info
647 * - session_info holds a weak reference to channel_info
649 session_info_get(session_info
);
650 session_info_add_channel(session_info
, channel_info
);
651 channel_info
->session_info
= session_info
;
655 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 LTTNG_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
<notification_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
<notification_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 LTTNG_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
,
1038 .triggers_list
= {},
1040 .notification_trigger_clients_ht
= NULL
,
1041 .notification_trigger_clients_ht_node
= {},
1044 struct notification_client_list_element client_list_element
= {};
1045 uid_t object_uid
= 0;
1046 gid_t object_gid
= 0;
1048 LTTNG_ASSERT(trigger
);
1049 LTTNG_ASSERT(condition
);
1050 LTTNG_ASSERT(client
);
1051 LTTNG_ASSERT(state
);
1053 switch (get_condition_binding_object(condition
)) {
1054 case LTTNG_OBJECT_TYPE_SESSION
:
1055 ret
= evaluate_session_condition_for_client(condition
, state
,
1056 &evaluation
, &object_uid
, &object_gid
);
1058 case LTTNG_OBJECT_TYPE_CHANNEL
:
1059 ret
= evaluate_channel_condition_for_client(condition
, state
,
1060 &evaluation
, &object_uid
, &object_gid
);
1062 case LTTNG_OBJECT_TYPE_NONE
:
1063 DBG("Newly subscribed-to condition not bound to object, nothing to evaluate");
1066 case LTTNG_OBJECT_TYPE_UNKNOWN
:
1076 /* Evaluation yielded nothing. Normal exit. */
1077 DBG("Newly subscribed-to condition evaluated to false, nothing to report to client");
1083 * Create a temporary client list with the client currently
1086 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1087 CDS_INIT_LIST_HEAD(&client_list
.clients_list
);
1089 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1090 client_list_element
.client
= client
;
1091 cds_list_add(&client_list_element
.node
, &client_list
.clients_list
);
1093 /* Send evaluation result to the newly-subscribed client. */
1094 DBG("Newly subscribed-to condition evaluated to true, notifying client");
1095 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1096 state
, object_uid
, object_gid
);
1103 int notification_thread_client_subscribe(struct notification_client
*client
,
1104 struct lttng_condition
*condition
,
1105 struct notification_thread_state
*state
,
1106 enum lttng_notification_channel_status
*_status
)
1109 struct notification_client_list
*client_list
= NULL
;
1110 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1111 struct notification_client_list_element
*client_list_element
= NULL
;
1112 struct lttng_trigger_ht_element
*trigger_ht_element
;
1113 enum lttng_notification_channel_status status
=
1114 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1117 * Ensure that the client has not already subscribed to this condition
1120 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1121 if (lttng_condition_is_equal(condition_list_element
->condition
,
1123 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1128 condition_list_element
= zmalloc
<lttng_condition_list_element
>();
1129 if (!condition_list_element
) {
1133 client_list_element
= zmalloc
<notification_client_list_element
>();
1134 if (!client_list_element
) {
1140 * Add the newly-subscribed condition to the client's subscription list.
1142 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1143 condition_list_element
->condition
= condition
;
1145 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1147 client_list
= get_client_list_from_condition(
1148 state
, condition_list_element
->condition
);
1151 * No notification-emiting trigger registered with this
1152 * condition. We don't evaluate the condition right away
1153 * since this trigger is not registered yet.
1155 free(client_list_element
);
1160 * The condition to which the client just subscribed is evaluated
1161 * at this point so that conditions that are already TRUE result
1162 * in a notification being sent out.
1164 * Note the iteration on all triggers which share an identical
1165 * `condition` than the one to which the client is registering. This is
1166 * done to ensure that the client receives a distinct notification for
1167 * all triggers that have a `notify` action that have this condition.
1169 pthread_mutex_lock(&client_list
->lock
);
1170 cds_list_for_each_entry(trigger_ht_element
,
1171 &client_list
->triggers_list
, client_list_trigger_node
) {
1172 if (evaluate_condition_for_client(trigger_ht_element
->trigger
, condition_list_element
->condition
,
1174 WARN("Evaluation of a condition on client subscription failed, aborting.");
1176 free(client_list_element
);
1177 pthread_mutex_unlock(&client_list
->lock
);
1181 pthread_mutex_unlock(&client_list
->lock
);
1184 * Add the client to the list of clients interested in a given trigger
1185 * if a "notification" trigger with a corresponding condition was
1188 client_list_element
->client
= client
;
1189 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1191 pthread_mutex_lock(&client_list
->lock
);
1192 cds_list_add(&client_list_element
->node
, &client_list
->clients_list
);
1193 pthread_mutex_unlock(&client_list
->lock
);
1199 notification_client_list_put(client_list
);
1201 lttng_condition_destroy(condition
);
1204 free(condition_list_element
);
1205 free(client_list_element
);
1206 lttng_condition_destroy(condition
);
1211 int notification_thread_client_unsubscribe(
1212 struct notification_client
*client
,
1213 struct lttng_condition
*condition
,
1214 struct notification_thread_state
*state
,
1215 enum lttng_notification_channel_status
*_status
)
1217 struct notification_client_list
*client_list
;
1218 struct lttng_condition_list_element
*condition_list_element
,
1220 struct notification_client_list_element
*client_list_element
,
1222 bool condition_found
= false;
1223 enum lttng_notification_channel_status status
=
1224 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1226 /* Remove the condition from the client's condition list. */
1227 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1228 &client
->condition_list
, node
) {
1229 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1234 cds_list_del(&condition_list_element
->node
);
1236 * The caller may be iterating on the client's conditions to
1237 * tear down a client's connection. In this case, the condition
1238 * will be destroyed at the end.
1240 if (condition
!= condition_list_element
->condition
) {
1241 lttng_condition_destroy(
1242 condition_list_element
->condition
);
1244 free(condition_list_element
);
1245 condition_found
= true;
1249 if (!condition_found
) {
1250 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1255 * Remove the client from the list of clients interested the trigger
1256 * matching the condition.
1258 client_list
= get_client_list_from_condition(state
, condition
);
1263 pthread_mutex_lock(&client_list
->lock
);
1264 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1265 &client_list
->clients_list
, node
) {
1266 if (client_list_element
->client
->id
!= client
->id
) {
1269 cds_list_del(&client_list_element
->node
);
1270 free(client_list_element
);
1273 pthread_mutex_unlock(&client_list
->lock
);
1274 notification_client_list_put(client_list
);
1277 lttng_condition_destroy(condition
);
1285 void free_notification_client_rcu(struct rcu_head
*node
)
1287 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1291 void notification_client_destroy(struct notification_client
*client
)
1298 * The client object is not reachable by other threads, no need to lock
1301 if (client
->socket
>= 0) {
1302 (void) lttcomm_close_unix_sock(client
->socket
);
1303 client
->socket
= -1;
1305 client
->communication
.active
= false;
1306 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1307 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1308 pthread_mutex_destroy(&client
->lock
);
1309 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1313 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1317 struct notification_client
*get_client_from_socket(int socket
,
1318 struct notification_thread_state
*state
)
1320 struct cds_lfht_iter iter
;
1321 struct cds_lfht_node
*node
;
1322 struct notification_client
*client
= NULL
;
1324 ASSERT_RCU_READ_LOCKED();
1326 cds_lfht_lookup(state
->client_socket_ht
,
1327 hash_client_socket(socket
),
1328 match_client_socket
,
1329 (void *) (unsigned long) socket
,
1331 node
= cds_lfht_iter_get_node(&iter
);
1336 client
= caa_container_of(node
, struct notification_client
,
1337 client_socket_ht_node
);
1343 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1347 struct notification_client
*get_client_from_id(notification_client_id id
,
1348 struct notification_thread_state
*state
)
1350 struct cds_lfht_iter iter
;
1351 struct cds_lfht_node
*node
;
1352 struct notification_client
*client
= NULL
;
1354 ASSERT_RCU_READ_LOCKED();
1356 cds_lfht_lookup(state
->client_id_ht
,
1361 node
= cds_lfht_iter_get_node(&iter
);
1366 client
= caa_container_of(node
, struct notification_client
,
1373 bool buffer_usage_condition_applies_to_channel(
1374 const struct lttng_condition
*condition
,
1375 const struct channel_info
*channel_info
)
1377 enum lttng_condition_status status
;
1378 enum lttng_domain_type condition_domain
;
1379 const char *condition_session_name
= NULL
;
1380 const char *condition_channel_name
= NULL
;
1382 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1384 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
1385 if (channel_info
->key
.domain
!= condition_domain
) {
1389 status
= lttng_condition_buffer_usage_get_session_name(
1390 condition
, &condition_session_name
);
1391 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1393 status
= lttng_condition_buffer_usage_get_channel_name(
1394 condition
, &condition_channel_name
);
1395 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1397 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1400 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1410 bool session_consumed_size_condition_applies_to_channel(
1411 const struct lttng_condition
*condition
,
1412 const struct channel_info
*channel_info
)
1414 enum lttng_condition_status status
;
1415 const char *condition_session_name
= NULL
;
1417 status
= lttng_condition_session_consumed_size_get_session_name(
1418 condition
, &condition_session_name
);
1419 LTTNG_ASSERT((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1421 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1431 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1432 const struct channel_info
*channel_info
)
1434 const struct lttng_condition
*condition
;
1435 bool trigger_applies
;
1437 condition
= lttng_trigger_get_const_condition(trigger
);
1442 switch (lttng_condition_get_type(condition
)) {
1443 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1444 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1445 trigger_applies
= buffer_usage_condition_applies_to_channel(
1446 condition
, channel_info
);
1448 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1449 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1450 condition
, channel_info
);
1456 return trigger_applies
;
1461 /* Must be called with RCU read lock held. */
1463 struct lttng_session_trigger_list
*get_session_trigger_list(
1464 struct notification_thread_state
*state
,
1465 const char *session_name
)
1467 struct lttng_session_trigger_list
*list
= NULL
;
1468 struct cds_lfht_node
*node
;
1469 struct cds_lfht_iter iter
;
1471 ASSERT_RCU_READ_LOCKED();
1473 cds_lfht_lookup(state
->session_triggers_ht
,
1474 hash_key_str(session_name
, lttng_ht_seed
),
1475 match_session_trigger_list
,
1478 node
= cds_lfht_iter_get_node(&iter
);
1481 * Not an error, the list of triggers applying to that session
1482 * will be initialized when the session is created.
1484 DBG("No trigger list found for session \"%s\" as it is not yet known to the notification system",
1489 list
= caa_container_of(node
,
1490 struct lttng_session_trigger_list
,
1491 session_triggers_ht_node
);
1497 * Allocate an empty lttng_session_trigger_list for the session named
1500 * No ownership of 'session_name' is assumed by the session trigger list.
1501 * It is the caller's responsability to ensure the session name is alive
1502 * for as long as this list is.
1505 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1506 const char *session_name
,
1507 struct cds_lfht
*session_triggers_ht
)
1509 struct lttng_session_trigger_list
*list
;
1511 list
= zmalloc
<lttng_session_trigger_list
>();
1515 list
->session_name
= session_name
;
1516 CDS_INIT_LIST_HEAD(&list
->list
);
1517 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1518 list
->session_triggers_ht
= session_triggers_ht
;
1521 /* Publish the list through the session_triggers_ht. */
1522 cds_lfht_add(session_triggers_ht
,
1523 hash_key_str(session_name
, lttng_ht_seed
),
1524 &list
->session_triggers_ht_node
);
1531 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1533 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1538 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1540 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1542 /* Empty the list element by element, and then free the list itself. */
1543 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1544 &list
->list
, node
) {
1545 cds_list_del(&trigger_list_element
->node
);
1546 free(trigger_list_element
);
1549 /* Unpublish the list from the session_triggers_ht. */
1550 cds_lfht_del(list
->session_triggers_ht
,
1551 &list
->session_triggers_ht_node
);
1553 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1557 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1558 struct lttng_trigger
*trigger
)
1561 struct lttng_trigger_list_element
*new_element
=
1562 zmalloc
<lttng_trigger_list_element
>();
1568 CDS_INIT_LIST_HEAD(&new_element
->node
);
1569 new_element
->trigger
= trigger
;
1570 cds_list_add(&new_element
->node
, &list
->list
);
1576 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1577 const char *session_name
)
1579 bool applies
= false;
1580 const struct lttng_condition
*condition
;
1582 condition
= lttng_trigger_get_const_condition(trigger
);
1583 switch (lttng_condition_get_type(condition
)) {
1584 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1585 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1587 enum lttng_condition_status condition_status
;
1588 const char *condition_session_name
;
1590 condition_status
= lttng_condition_session_rotation_get_session_name(
1591 condition
, &condition_session_name
);
1592 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1593 ERR("Failed to retrieve session rotation condition's session name");
1597 LTTNG_ASSERT(condition_session_name
);
1598 applies
= !strcmp(condition_session_name
, session_name
);
1609 * Allocate and initialize an lttng_session_trigger_list which contains
1610 * all triggers that apply to the session named 'session_name'.
1612 * No ownership of 'session_name' is assumed by the session trigger list.
1613 * It is the caller's responsability to ensure the session name is alive
1614 * for as long as this list is.
1617 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1618 const struct notification_thread_state
*state
,
1619 const char *session_name
)
1621 int trigger_count
= 0;
1622 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1623 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1624 struct cds_lfht_iter iter
;
1626 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1627 state
->session_triggers_ht
);
1629 /* Add all triggers applying to the session named 'session_name'. */
1630 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1634 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1639 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1640 trigger_ht_element
->trigger
);
1648 DBG("Found %i triggers that apply to newly created session",
1650 return session_trigger_list
;
1652 lttng_session_trigger_list_destroy(session_trigger_list
);
1657 struct session_info
*find_or_create_session_info(
1658 struct notification_thread_state
*state
,
1659 const char *name
, uid_t uid
, gid_t gid
)
1661 struct session_info
*session
= NULL
;
1662 struct cds_lfht_node
*node
;
1663 struct cds_lfht_iter iter
;
1664 struct lttng_session_trigger_list
*trigger_list
;
1667 cds_lfht_lookup(state
->sessions_ht
,
1668 hash_key_str(name
, lttng_ht_seed
),
1672 node
= cds_lfht_iter_get_node(&iter
);
1674 DBG("Found session info of session \"%s\" (uid = %i, gid = %i)",
1676 session
= caa_container_of(node
, struct session_info
,
1678 LTTNG_ASSERT(session
->uid
== uid
);
1679 LTTNG_ASSERT(session
->gid
== gid
);
1680 session_info_get(session
);
1684 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1685 if (!trigger_list
) {
1689 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1690 state
->sessions_ht
);
1692 ERR("Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1694 lttng_session_trigger_list_destroy(trigger_list
);
1697 trigger_list
= NULL
;
1699 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1700 &session
->sessions_ht_node
);
1706 session_info_put(session
);
1711 int handle_notification_thread_command_add_channel(
1712 struct notification_thread_state
*state
,
1713 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1714 const char *channel_name
, enum lttng_domain_type channel_domain
,
1715 uint64_t channel_key_int
, uint64_t channel_capacity
,
1716 enum lttng_error_code
*cmd_result
)
1718 struct cds_list_head trigger_list
;
1719 struct channel_info
*new_channel_info
= NULL
;
1720 struct channel_key channel_key
= {
1721 .key
= channel_key_int
,
1722 .domain
= channel_domain
,
1724 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1725 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1726 int trigger_count
= 0;
1727 struct cds_lfht_iter iter
;
1728 struct session_info
*session_info
= NULL
;
1730 DBG("Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1731 channel_name
, session_name
, channel_key_int
,
1732 lttng_domain_type_str(channel_domain
));
1734 CDS_INIT_LIST_HEAD(&trigger_list
);
1736 session_info
= find_or_create_session_info(state
, session_name
,
1737 session_uid
, session_gid
);
1738 if (!session_info
) {
1739 /* Allocation error or an internal error occurred. */
1743 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1744 channel_capacity
, session_info
);
1745 if (!new_channel_info
) {
1750 /* Build a list of all triggers applying to the new channel. */
1751 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1753 struct lttng_trigger_list_element
*new_element
;
1755 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1756 new_channel_info
)) {
1760 new_element
= zmalloc
<lttng_trigger_list_element
>();
1765 CDS_INIT_LIST_HEAD(&new_element
->node
);
1766 new_element
->trigger
= trigger_ht_element
->trigger
;
1767 cds_list_add(&new_element
->node
, &trigger_list
);
1772 DBG("Found %i triggers that apply to newly added channel",
1774 channel_trigger_list
= zmalloc
<lttng_channel_trigger_list
>();
1775 if (!channel_trigger_list
) {
1778 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1779 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1780 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1781 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1784 /* Add channel to the channel_ht which owns the channel_infos. */
1785 cds_lfht_add(state
->channels_ht
,
1786 hash_channel_key(&new_channel_info
->key
),
1787 &new_channel_info
->channels_ht_node
);
1789 * Add the list of triggers associated with this channel to the
1790 * channel_triggers_ht.
1792 cds_lfht_add(state
->channel_triggers_ht
,
1793 hash_channel_key(&new_channel_info
->key
),
1794 &channel_trigger_list
->channel_triggers_ht_node
);
1796 session_info_put(session_info
);
1797 *cmd_result
= LTTNG_OK
;
1800 channel_info_destroy(new_channel_info
);
1801 session_info_put(session_info
);
1806 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1808 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1813 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1815 free(caa_container_of(node
, struct channel_state_sample
,
1820 int handle_notification_thread_command_remove_channel(
1821 struct notification_thread_state
*state
,
1822 uint64_t channel_key
, enum lttng_domain_type domain
,
1823 enum lttng_error_code
*cmd_result
)
1825 struct cds_lfht_node
*node
;
1826 struct cds_lfht_iter iter
;
1827 struct lttng_channel_trigger_list
*trigger_list
;
1828 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1829 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1830 struct channel_info
*channel_info
;
1832 DBG("Removing channel key = %" PRIu64
" in %s domain",
1833 channel_key
, lttng_domain_type_str(domain
));
1837 cds_lfht_lookup(state
->channel_triggers_ht
,
1838 hash_channel_key(&key
),
1839 match_channel_trigger_list
,
1842 node
= cds_lfht_iter_get_node(&iter
);
1844 * There is a severe internal error if we are being asked to remove a
1845 * channel that doesn't exist.
1848 ERR("Channel being removed is unknown to the notification thread");
1852 /* Free the list of triggers associated with this channel. */
1853 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1854 channel_triggers_ht_node
);
1855 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1856 &trigger_list
->list
, node
) {
1857 cds_list_del(&trigger_list_element
->node
);
1858 free(trigger_list_element
);
1860 cds_lfht_del(state
->channel_triggers_ht
, node
);
1861 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1863 /* Free sampled channel state. */
1864 cds_lfht_lookup(state
->channel_state_ht
,
1865 hash_channel_key(&key
),
1866 match_channel_state_sample
,
1869 node
= cds_lfht_iter_get_node(&iter
);
1871 * This is expected to be NULL if the channel is destroyed before we
1872 * received a sample.
1875 struct channel_state_sample
*sample
= caa_container_of(node
,
1876 struct channel_state_sample
,
1877 channel_state_ht_node
);
1879 cds_lfht_del(state
->channel_state_ht
, node
);
1880 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1883 /* Remove the channel from the channels_ht and free it. */
1884 cds_lfht_lookup(state
->channels_ht
,
1885 hash_channel_key(&key
),
1889 node
= cds_lfht_iter_get_node(&iter
);
1891 channel_info
= caa_container_of(node
, struct channel_info
,
1893 cds_lfht_del(state
->channels_ht
, node
);
1894 channel_info_destroy(channel_info
);
1897 *cmd_result
= LTTNG_OK
;
1902 int handle_notification_thread_command_session_rotation(
1903 struct notification_thread_state
*state
,
1904 enum notification_thread_command_type cmd_type
,
1905 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1906 uint64_t trace_archive_chunk_id
,
1907 struct lttng_trace_archive_location
*location
,
1908 enum lttng_error_code
*_cmd_result
)
1911 enum lttng_error_code cmd_result
= LTTNG_OK
;
1912 struct lttng_session_trigger_list
*trigger_list
;
1913 struct lttng_trigger_list_element
*trigger_list_element
;
1914 struct session_info
*session_info
;
1915 const struct lttng_credentials session_creds
= {
1916 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1917 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1922 session_info
= find_or_create_session_info(state
, session_name
,
1923 session_uid
, session_gid
);
1924 if (!session_info
) {
1925 /* Allocation error or an internal error occurred. */
1927 cmd_result
= LTTNG_ERR_NOMEM
;
1931 session_info
->rotation
.ongoing
=
1932 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1933 session_info
->rotation
.id
= trace_archive_chunk_id
;
1934 trigger_list
= get_session_trigger_list(state
, session_name
);
1935 if (!trigger_list
) {
1936 DBG("No triggers applying to session \"%s\" found",
1941 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1943 const struct lttng_condition
*condition
;
1944 struct lttng_trigger
*trigger
;
1945 struct notification_client_list
*client_list
;
1946 struct lttng_evaluation
*evaluation
= NULL
;
1947 enum lttng_condition_type condition_type
;
1948 enum action_executor_status executor_status
;
1950 trigger
= trigger_list_element
->trigger
;
1951 condition
= lttng_trigger_get_const_condition(trigger
);
1952 LTTNG_ASSERT(condition
);
1953 condition_type
= lttng_condition_get_type(condition
);
1955 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1956 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1958 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1959 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1963 client_list
= get_client_list_from_condition(state
, condition
);
1964 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1965 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1966 trace_archive_chunk_id
);
1968 evaluation
= lttng_evaluation_session_rotation_completed_create(
1969 trace_archive_chunk_id
, location
);
1973 /* Internal error */
1975 cmd_result
= LTTNG_ERR_UNK
;
1980 * Ownership of `evaluation` transferred to the action executor
1981 * no matter the result.
1983 executor_status
= action_executor_enqueue_trigger(
1984 state
->executor
, trigger
, evaluation
,
1985 &session_creds
, client_list
);
1987 switch (executor_status
) {
1988 case ACTION_EXECUTOR_STATUS_OK
:
1990 case ACTION_EXECUTOR_STATUS_ERROR
:
1991 case ACTION_EXECUTOR_STATUS_INVALID
:
1993 * TODO Add trigger identification (name/id) when
1994 * it is added to the API.
1996 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1999 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2001 * TODO Add trigger identification (name/id) when
2002 * it is added to the API.
2004 * Not a fatal error.
2006 WARN("No space left when enqueuing action associated with session rotation trigger");
2014 notification_client_list_put(client_list
);
2015 if (caa_unlikely(ret
)) {
2020 session_info_put(session_info
);
2021 *_cmd_result
= cmd_result
;
2027 int handle_notification_thread_command_add_tracer_event_source(
2028 struct notification_thread_state
*state
,
2029 int tracer_event_source_fd
,
2030 enum lttng_domain_type domain_type
,
2031 enum lttng_error_code
*_cmd_result
)
2034 enum lttng_error_code cmd_result
= LTTNG_OK
;
2035 struct notification_event_tracer_event_source_element
*element
= NULL
;
2037 element
= zmalloc
<notification_event_tracer_event_source_element
>();
2039 cmd_result
= LTTNG_ERR_NOMEM
;
2044 element
->fd
= tracer_event_source_fd
;
2045 element
->domain
= domain_type
;
2047 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
2049 DBG3("Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
2050 tracer_event_source_fd
,
2051 lttng_domain_type_str(domain_type
));
2053 /* Adding the read side pipe to the event poll. */
2054 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLPRI
| LPOLLIN
| LPOLLERR
);
2056 ERR("Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
2057 tracer_event_source_fd
,
2058 lttng_domain_type_str(element
->domain
));
2059 cds_list_del(&element
->node
);
2064 element
->is_fd_in_poll_set
= true;
2067 *_cmd_result
= cmd_result
;
2072 int drain_event_notifier_notification_pipe(
2073 struct notification_thread_state
*state
,
2074 int pipe
, enum lttng_domain_type domain
)
2076 struct lttng_poll_event events
= {};
2079 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
2081 ERR("Error creating lttng_poll_event");
2085 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2087 ERR("Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2094 * Continue to consume notifications as long as there are new
2095 * ones coming in. The tracer has been asked to stop producing
2098 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2099 * monitored (on Linux, at least) and will be returned when
2100 * the pipe is closed but empty.
2102 ret
= lttng_poll_wait_interruptible(&events
, 0);
2103 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 0) & LPOLLIN
) == 0) {
2104 /* No more notification to be read on this pipe. */
2107 } else if (ret
< 0) {
2108 PERROR("Failed on lttng_poll_wait_interruptible() call");
2113 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2115 ERR("Error consuming an event notifier notification from pipe: fd = %d",
2120 lttng_poll_clean(&events
);
2125 struct notification_event_tracer_event_source_element
*
2126 find_tracer_event_source_element(struct notification_thread_state
*state
,
2127 int tracer_event_source_fd
)
2129 struct notification_event_tracer_event_source_element
*source_element
;
2131 cds_list_for_each_entry(source_element
,
2132 &state
->tracer_event_sources_list
, node
) {
2133 if (source_element
->fd
== tracer_event_source_fd
) {
2138 source_element
= NULL
;
2140 return source_element
;
2144 int remove_tracer_event_source_from_pollset(
2145 struct notification_thread_state
*state
,
2146 struct notification_event_tracer_event_source_element
*source_element
)
2150 LTTNG_ASSERT(source_element
->is_fd_in_poll_set
);
2152 DBG3("Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2154 lttng_domain_type_str(source_element
->domain
));
2156 /* Removing the fd from the event poll set. */
2157 ret
= lttng_poll_del(&state
->events
, source_element
->fd
);
2159 ERR("Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2161 lttng_domain_type_str(source_element
->domain
));
2166 source_element
->is_fd_in_poll_set
= false;
2169 * Force the notification thread to restart the poll() loop to ensure
2170 * that any events from the removed fd are removed.
2172 state
->restart_poll
= true;
2174 ret
= drain_event_notifier_notification_pipe(state
, source_element
->fd
,
2175 source_element
->domain
);
2177 ERR("Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2179 lttng_domain_type_str(source_element
->domain
));
2188 int handle_notification_thread_tracer_event_source_died(
2189 struct notification_thread_state
*state
,
2190 int tracer_event_source_fd
)
2193 struct notification_event_tracer_event_source_element
*source_element
;
2195 source_element
= find_tracer_event_source_element(state
,
2196 tracer_event_source_fd
);
2198 LTTNG_ASSERT(source_element
);
2200 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2202 ERR("Failed to remove dead tracer event source from poll set");
2209 int handle_notification_thread_command_remove_tracer_event_source(
2210 struct notification_thread_state
*state
,
2211 int tracer_event_source_fd
,
2212 enum lttng_error_code
*_cmd_result
)
2215 enum lttng_error_code cmd_result
= LTTNG_OK
;
2216 struct notification_event_tracer_event_source_element
*source_element
= NULL
;
2218 source_element
= find_tracer_event_source_element(state
,
2219 tracer_event_source_fd
);
2221 LTTNG_ASSERT(source_element
);
2223 /* Remove the tracer source from the list. */
2224 cds_list_del(&source_element
->node
);
2226 if (!source_element
->is_fd_in_poll_set
) {
2227 /* Skip the poll set removal. */
2231 ret
= remove_tracer_event_source_from_pollset(state
, source_element
);
2233 ERR("Failed to remove tracer event source from poll set");
2234 cmd_result
= LTTNG_ERR_FATAL
;
2238 free(source_element
);
2239 *_cmd_result
= cmd_result
;
2243 static int handle_notification_thread_command_list_triggers(
2244 struct notification_thread_handle
*handle
__attribute__((unused
)),
2245 struct notification_thread_state
*state
,
2247 struct lttng_triggers
**triggers
,
2248 enum lttng_error_code
*_cmd_result
)
2251 enum lttng_error_code cmd_result
= LTTNG_OK
;
2252 struct cds_lfht_iter iter
;
2253 struct lttng_trigger_ht_element
*trigger_ht_element
;
2254 struct lttng_triggers
*local_triggers
= NULL
;
2255 const struct lttng_credentials
*creds
;
2259 local_triggers
= lttng_triggers_create();
2260 if (!local_triggers
) {
2261 /* Not a fatal error. */
2262 cmd_result
= LTTNG_ERR_NOMEM
;
2266 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2267 trigger_ht_element
, node
) {
2269 * Only return the triggers to which the client has access.
2270 * The root user has visibility over all triggers.
2272 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2273 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2277 ret
= lttng_triggers_add(local_triggers
,
2278 trigger_ht_element
->trigger
);
2280 /* Not a fatal error. */
2282 cmd_result
= LTTNG_ERR_NOMEM
;
2287 /* Transferring ownership to the caller. */
2288 *triggers
= local_triggers
;
2289 local_triggers
= NULL
;
2293 lttng_triggers_destroy(local_triggers
);
2294 *_cmd_result
= cmd_result
;
2298 static inline void get_trigger_info_for_log(const struct lttng_trigger
*trigger
,
2299 const char **trigger_name
,
2300 uid_t
*trigger_owner_uid
)
2302 enum lttng_trigger_status trigger_status
;
2304 trigger_status
= lttng_trigger_get_name(trigger
, trigger_name
);
2305 switch (trigger_status
) {
2306 case LTTNG_TRIGGER_STATUS_OK
:
2308 case LTTNG_TRIGGER_STATUS_UNSET
:
2309 *trigger_name
= "(anonymous)";
2315 trigger_status
= lttng_trigger_get_owner_uid(trigger
,
2317 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
2320 static int handle_notification_thread_command_get_trigger(
2321 struct notification_thread_state
*state
,
2322 const struct lttng_trigger
*trigger
,
2323 struct lttng_trigger
**registered_trigger
,
2324 enum lttng_error_code
*_cmd_result
)
2327 struct cds_lfht_iter iter
;
2328 struct lttng_trigger_ht_element
*trigger_ht_element
;
2329 enum lttng_error_code cmd_result
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2330 const char *trigger_name
;
2331 uid_t trigger_owner_uid
;
2335 cds_lfht_for_each_entry(
2336 state
->triggers_ht
, &iter
, trigger_ht_element
, node
) {
2337 if (lttng_trigger_is_equal(
2338 trigger
, trigger_ht_element
->trigger
)) {
2339 /* Take one reference on the return trigger. */
2340 *registered_trigger
= trigger_ht_element
->trigger
;
2341 lttng_trigger_get(*registered_trigger
);
2343 cmd_result
= LTTNG_OK
;
2348 /* Not a fatal error if the trigger is not found. */
2349 get_trigger_info_for_log(trigger
, &trigger_name
, &trigger_owner_uid
);
2350 DBG("Failed to retrieve registered version of trigger: trigger name = '%s', trigger owner uid = %d",
2351 trigger_name
, (int) trigger_owner_uid
);
2357 *_cmd_result
= cmd_result
;
2362 bool condition_is_supported(struct lttng_condition
*condition
)
2366 switch (lttng_condition_get_type(condition
)) {
2367 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2368 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2371 enum lttng_domain_type domain
;
2373 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2375 LTTNG_ASSERT(ret
== 0);
2377 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2378 is_supported
= true;
2383 * Older kernel tracers don't expose the API to monitor their
2384 * buffers. Therefore, we reject triggers that require that
2385 * mechanism to be available to be evaluated.
2387 * Assume unsupported on error.
2389 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2392 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
2394 const struct lttng_event_rule
*event_rule
;
2395 enum lttng_domain_type domain
;
2396 const enum lttng_condition_status status
=
2397 lttng_condition_event_rule_matches_get_rule(
2398 condition
, &event_rule
);
2400 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
2402 domain
= lttng_event_rule_get_domain_type(event_rule
);
2403 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2404 is_supported
= true;
2409 * Older kernel tracers can't emit notification. Therefore, we
2410 * reject triggers that require that mechanism to be available
2413 * Assume unsupported on error.
2415 is_supported
= kernel_supports_event_notifiers() == 1;
2419 is_supported
= true;
2422 return is_supported
;
2425 /* Must be called with RCU read lock held. */
2427 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2428 struct notification_thread_state
*state
)
2431 const struct lttng_condition
*condition
;
2432 const char *session_name
;
2433 struct lttng_session_trigger_list
*trigger_list
;
2435 ASSERT_RCU_READ_LOCKED();
2437 condition
= lttng_trigger_get_const_condition(trigger
);
2438 switch (lttng_condition_get_type(condition
)) {
2439 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2440 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2442 enum lttng_condition_status status
;
2444 status
= lttng_condition_session_rotation_get_session_name(
2445 condition
, &session_name
);
2446 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2447 ERR("Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2458 trigger_list
= get_session_trigger_list(state
, session_name
);
2459 if (!trigger_list
) {
2460 DBG("Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2466 DBG("Newly registered trigger bound to session \"%s\"",
2468 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2473 /* Must be called with RCU read lock held. */
2475 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2476 struct notification_thread_state
*state
)
2479 struct cds_lfht_node
*node
;
2480 struct cds_lfht_iter iter
;
2481 struct channel_info
*channel
;
2483 ASSERT_RCU_READ_LOCKED();
2485 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2487 struct lttng_trigger_list_element
*trigger_list_element
;
2488 struct lttng_channel_trigger_list
*trigger_list
;
2489 struct cds_lfht_iter lookup_iter
;
2491 if (!trigger_applies_to_channel(trigger
, channel
)) {
2495 cds_lfht_lookup(state
->channel_triggers_ht
,
2496 hash_channel_key(&channel
->key
),
2497 match_channel_trigger_list
,
2500 node
= cds_lfht_iter_get_node(&lookup_iter
);
2502 trigger_list
= caa_container_of(node
,
2503 struct lttng_channel_trigger_list
,
2504 channel_triggers_ht_node
);
2506 trigger_list_element
= zmalloc
<lttng_trigger_list_element
>();
2507 if (!trigger_list_element
) {
2511 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2512 trigger_list_element
->trigger
= trigger
;
2513 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2514 DBG("Newly registered trigger bound to channel \"%s\"",
2522 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2524 bool is_notify
= false;
2525 unsigned int i
, count
;
2526 enum lttng_action_status action_status
;
2527 const struct lttng_action
*action
=
2528 lttng_trigger_get_const_action(trigger
);
2529 enum lttng_action_type action_type
;
2531 LTTNG_ASSERT(action
);
2532 action_type
= lttng_action_get_type(action
);
2533 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2536 } else if (action_type
!= LTTNG_ACTION_TYPE_LIST
) {
2540 action_status
= lttng_action_list_get_count(action
, &count
);
2541 LTTNG_ASSERT(action_status
== LTTNG_ACTION_STATUS_OK
);
2543 for (i
= 0; i
< count
; i
++) {
2544 const struct lttng_action
*inner_action
=
2545 lttng_action_list_get_at_index(
2548 action_type
= lttng_action_get_type(inner_action
);
2549 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2559 static bool trigger_name_taken(struct notification_thread_state
*state
,
2560 const struct lttng_trigger
*trigger
)
2562 struct cds_lfht_iter iter
;
2565 * No duplicata is allowed in the triggers_by_name_uid_ht.
2566 * The match is done against the trigger name and uid.
2568 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2569 hash_trigger_by_name_uid(trigger
),
2570 match_trigger_by_name_uid
,
2573 return !!cds_lfht_iter_get_node(&iter
);
2577 enum lttng_error_code
generate_trigger_name(
2578 struct notification_thread_state
*state
,
2579 struct lttng_trigger
*trigger
, const char **name
)
2581 enum lttng_error_code ret_code
= LTTNG_OK
;
2583 enum lttng_trigger_status status
;
2586 const int ret
= lttng_trigger_generate_name(trigger
,
2587 state
->trigger_id
.name_offset
++);
2589 /* The only reason this can fail right now. */
2590 ret_code
= LTTNG_ERR_NOMEM
;
2594 status
= lttng_trigger_get_name(trigger
, name
);
2595 LTTNG_ASSERT(status
== LTTNG_TRIGGER_STATUS_OK
);
2597 taken
= trigger_name_taken(state
, trigger
);
2598 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2604 void notif_thread_state_remove_trigger_ht_elem(
2605 struct notification_thread_state
*state
,
2606 struct lttng_trigger_ht_element
*trigger_ht_element
)
2608 LTTNG_ASSERT(state
);
2609 LTTNG_ASSERT(trigger_ht_element
);
2611 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2612 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2616 enum lttng_error_code
setup_tracer_notifier(
2617 struct notification_thread_state
*state
,
2618 struct lttng_trigger
*trigger
)
2620 enum lttng_error_code ret
;
2621 enum event_notifier_error_accounting_status error_accounting_status
;
2622 struct cds_lfht_node
*node
;
2623 uint64_t error_counter_index
= 0;
2624 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
2625 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2627 trigger_tokens_ht_element
= zmalloc
<notification_trigger_tokens_ht_element
>();
2628 if (!trigger_tokens_ht_element
) {
2629 ret
= LTTNG_ERR_NOMEM
;
2633 /* Add trigger token to the trigger_tokens_ht. */
2634 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2635 trigger_tokens_ht_element
->token
= LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2636 trigger_tokens_ht_element
->trigger
= trigger
;
2638 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2639 hash_key_u64(&trigger_tokens_ht_element
->token
, lttng_ht_seed
),
2640 match_trigger_token
,
2641 &trigger_tokens_ht_element
->token
,
2642 &trigger_tokens_ht_element
->node
);
2643 if (node
!= &trigger_tokens_ht_element
->node
) {
2644 ret
= LTTNG_ERR_TRIGGER_EXISTS
;
2645 goto error_free_ht_element
;
2648 error_accounting_status
= event_notifier_error_accounting_register_event_notifier(
2649 trigger
, &error_counter_index
);
2650 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2651 if (error_accounting_status
== EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_NO_INDEX_AVAILABLE
) {
2652 DBG("Trigger list error accounting counter full.");
2653 ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING_FULL
;
2655 ERR("Error registering trigger for error accounting");
2656 ret
= LTTNG_ERR_EVENT_NOTIFIER_REGISTRATION
;
2659 goto error_remove_ht_element
;
2662 lttng_condition_event_rule_matches_set_error_counter_index(
2663 condition
, error_counter_index
);
2668 error_remove_ht_element
:
2669 cds_lfht_del(state
->trigger_tokens_ht
, &trigger_tokens_ht_element
->node
);
2670 error_free_ht_element
:
2671 free(trigger_tokens_ht_element
);
2677 * FIXME A client's credentials are not checked when registering a trigger.
2679 * The effects of this are benign since:
2680 * - The client will succeed in registering the trigger, as it is valid,
2681 * - The trigger will, internally, be bound to the channel/session,
2682 * - The notifications will not be sent since the client's credentials
2683 * are checked against the channel at that moment.
2685 * If this function returns a non-zero value, it means something is
2686 * fundamentally broken and the whole subsystem/thread will be torn down.
2688 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2692 int handle_notification_thread_command_register_trigger(
2693 struct notification_thread_state
*state
,
2694 struct lttng_trigger
*trigger
,
2695 bool is_trigger_anonymous
,
2696 enum lttng_error_code
*cmd_result
)
2699 struct lttng_condition
*condition
;
2700 struct notification_client_list
*client_list
= NULL
;
2701 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2702 struct cds_lfht_node
*node
;
2703 const char* trigger_name
;
2704 bool free_trigger
= true;
2705 struct lttng_evaluation
*evaluation
= NULL
;
2706 struct lttng_credentials object_creds
;
2709 enum action_executor_status executor_status
;
2710 const uint64_t trigger_tracer_token
=
2711 state
->trigger_id
.next_tracer_token
++;
2715 /* Set the trigger's tracer token. */
2716 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2718 if (!is_trigger_anonymous
) {
2719 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2720 LTTNG_TRIGGER_STATUS_UNSET
) {
2721 const enum lttng_error_code ret_code
=
2722 generate_trigger_name(state
, trigger
,
2725 if (ret_code
!= LTTNG_OK
) {
2728 *cmd_result
= ret_code
;
2731 } else if (trigger_name_taken(state
, trigger
)) {
2732 /* Not a fatal error. */
2733 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2738 trigger_name
= "(anonymous)";
2741 condition
= lttng_trigger_get_condition(trigger
);
2742 LTTNG_ASSERT(condition
);
2744 /* Some conditions require tracers to implement a minimal ABI version. */
2745 if (!condition_is_supported(condition
)) {
2746 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2750 trigger_ht_element
= zmalloc
<lttng_trigger_ht_element
>();
2751 if (!trigger_ht_element
) {
2756 /* Add trigger to the trigger_ht. */
2757 cds_lfht_node_init(&trigger_ht_element
->node
);
2758 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2759 trigger_ht_element
->trigger
= trigger
;
2761 node
= cds_lfht_add_unique(state
->triggers_ht
,
2762 lttng_condition_hash(condition
),
2765 &trigger_ht_element
->node
);
2766 if (node
!= &trigger_ht_element
->node
) {
2767 /* Not a fatal error, simply report it to the client. */
2768 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2769 goto error_free_ht_element
;
2772 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2773 hash_trigger_by_name_uid(trigger
),
2774 match_trigger_by_name_uid
,
2776 &trigger_ht_element
->node_by_name_uid
);
2777 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2778 /* Internal error: add to triggers_ht should have failed. */
2780 goto error_free_ht_element
;
2783 /* From this point consider the trigger registered. */
2784 lttng_trigger_set_as_registered(trigger
);
2787 * Some triggers might need a tracer notifier depending on its
2788 * condition and actions.
2790 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
2791 enum lttng_error_code error_code
;
2793 error_code
= setup_tracer_notifier(state
, trigger
);
2794 if (error_code
!= LTTNG_OK
) {
2795 notif_thread_state_remove_trigger_ht_elem(state
,
2796 trigger_ht_element
);
2797 if (error_code
== LTTNG_ERR_NOMEM
) {
2800 *cmd_result
= error_code
;
2804 goto error_free_ht_element
;
2809 * The rest only applies to triggers that have a "notify" action.
2810 * It is not skipped as this is the only action type currently
2813 if (is_trigger_action_notify(trigger
)) {
2815 * Find or create the client list of this condition. It may
2816 * already be present if another trigger is already registered
2817 * with the same condition.
2819 client_list
= get_client_list_from_condition(state
, condition
);
2822 * No client list for this condition yet. We create new
2823 * one and build it up.
2825 client_list
= notification_client_list_create(state
, condition
);
2827 ERR("Error creating notification client list for trigger %s", trigger
->name
);
2828 goto error_free_ht_element
;
2832 CDS_INIT_LIST_HEAD(&trigger_ht_element
->client_list_trigger_node
);
2834 pthread_mutex_lock(&client_list
->lock
);
2835 cds_list_add(&trigger_ht_element
->client_list_trigger_node
, &client_list
->triggers_list
);
2836 pthread_mutex_unlock(&client_list
->lock
);
2840 * Ownership of the trigger and of its wrapper was transfered to
2841 * the triggers_ht. Same for token ht element if necessary.
2843 trigger_ht_element
= NULL
;
2844 free_trigger
= false;
2846 switch (get_condition_binding_object(condition
)) {
2847 case LTTNG_OBJECT_TYPE_SESSION
:
2848 /* Add the trigger to the list if it matches a known session. */
2849 ret
= bind_trigger_to_matching_session(trigger
, state
);
2851 goto error_free_ht_element
;
2854 case LTTNG_OBJECT_TYPE_CHANNEL
:
2856 * Add the trigger to list of triggers bound to the channels
2859 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2861 goto error_free_ht_element
;
2864 case LTTNG_OBJECT_TYPE_NONE
:
2867 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2869 goto error_free_ht_element
;
2873 * The new trigger's condition must be evaluated against the current
2876 * In the case of `notify` action, nothing preventing clients from
2877 * subscribing to a condition before the corresponding trigger is
2878 * registered, we have to evaluate this new condition right away.
2880 * At some point, we were waiting for the next "evaluation" (e.g. on
2881 * reception of a channel sample) to evaluate this new condition, but
2884 * The reason it was broken is that waiting for the next sample
2885 * does not allow us to properly handle transitions for edge-triggered
2888 * Consider this example: when we handle a new channel sample, we
2889 * evaluate each conditions twice: once with the previous state, and
2890 * again with the newest state. We then use those two results to
2891 * determine whether a state change happened: a condition was false and
2892 * became true. If a state change happened, we have to notify clients.
2894 * Now, if a client subscribes to a given notification and registers
2895 * a trigger *after* that subscription, we have to make sure the
2896 * condition is evaluated at this point while considering only the
2897 * current state. Otherwise, the next evaluation cycle may only see
2898 * that the evaluations remain the same (true for samples n-1 and n) and
2899 * the client will never know that the condition has been met.
2901 switch (get_condition_binding_object(condition
)) {
2902 case LTTNG_OBJECT_TYPE_SESSION
:
2903 ret
= evaluate_session_condition_for_client(condition
, state
,
2904 &evaluation
, &object_uid
,
2906 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2907 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2909 case LTTNG_OBJECT_TYPE_CHANNEL
:
2910 ret
= evaluate_channel_condition_for_client(condition
, state
,
2911 &evaluation
, &object_uid
,
2913 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2914 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2916 case LTTNG_OBJECT_TYPE_NONE
:
2919 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2927 goto error_free_ht_element
;
2930 DBG("Newly registered trigger's condition evaluated to %s",
2931 evaluation
? "true" : "false");
2933 /* Evaluation yielded nothing. Normal exit. */
2939 * Ownership of `evaluation` transferred to the action executor
2940 * no matter the result.
2942 executor_status
= action_executor_enqueue_trigger(state
->executor
,
2943 trigger
, evaluation
, &object_creds
, client_list
);
2945 switch (executor_status
) {
2946 case ACTION_EXECUTOR_STATUS_OK
:
2948 case ACTION_EXECUTOR_STATUS_ERROR
:
2949 case ACTION_EXECUTOR_STATUS_INVALID
:
2951 * TODO Add trigger identification (name/id) when
2952 * it is added to the API.
2954 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2956 goto error_free_ht_element
;
2957 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2959 * TODO Add trigger identification (name/id) when
2960 * it is added to the API.
2962 * Not a fatal error.
2964 WARN("No space left when enqueuing action associated to newly registered trigger");
2972 *cmd_result
= LTTNG_OK
;
2973 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2974 trigger_name
, trigger_tracer_token
);
2977 error_free_ht_element
:
2978 if (trigger_ht_element
) {
2979 /* Delayed removal due to RCU constraint on delete. */
2980 call_rcu(&trigger_ht_element
->rcu_node
,
2981 free_lttng_trigger_ht_element_rcu
);
2986 * Other objects might have a reference to the trigger, mark it
2989 lttng_trigger_set_as_unregistered(trigger
);
2990 lttng_trigger_destroy(trigger
);
2998 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
3000 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
3005 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
3007 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
3012 void teardown_tracer_notifier(struct notification_thread_state
*state
,
3013 const struct lttng_trigger
*trigger
)
3015 struct cds_lfht_iter iter
;
3016 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
;
3018 cds_lfht_for_each_entry(state
->trigger_tokens_ht
, &iter
,
3019 trigger_tokens_ht_element
, node
) {
3021 if (!lttng_trigger_is_equal(trigger
,
3022 trigger_tokens_ht_element
->trigger
)) {
3026 event_notifier_error_accounting_unregister_event_notifier(
3027 trigger_tokens_ht_element
->trigger
);
3029 /* TODO talk to all app and remove it */
3030 DBG("Removed trigger from tokens_ht");
3031 cds_lfht_del(state
->trigger_tokens_ht
,
3032 &trigger_tokens_ht_element
->node
);
3034 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
3035 free_notification_trigger_tokens_ht_element_rcu
);
3042 int handle_notification_thread_command_unregister_trigger(
3043 struct notification_thread_state
*state
,
3044 const struct lttng_trigger
*trigger
,
3045 enum lttng_error_code
*_cmd_reply
)
3047 struct cds_lfht_iter iter
;
3048 struct cds_lfht_node
*triggers_ht_node
;
3049 struct lttng_channel_trigger_list
*trigger_list
;
3050 struct notification_client_list
*client_list
;
3051 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
3052 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
3054 enum lttng_error_code cmd_reply
;
3058 cds_lfht_lookup(state
->triggers_ht
,
3059 lttng_condition_hash(condition
),
3063 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
3064 if (!triggers_ht_node
) {
3065 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
3068 cmd_reply
= LTTNG_OK
;
3071 trigger_ht_element
= caa_container_of(triggers_ht_node
,
3072 struct lttng_trigger_ht_element
, node
);
3074 /* Remove trigger from channel_triggers_ht. */
3075 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
3076 channel_triggers_ht_node
) {
3077 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
3079 cds_list_for_each_entry_safe(trigger_element
, tmp
,
3080 &trigger_list
->list
, node
) {
3081 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
3085 DBG("Removed trigger from channel_triggers_ht");
3086 cds_list_del(&trigger_element
->node
);
3087 /* A trigger can only appear once per channel */
3092 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
3093 teardown_tracer_notifier(state
, trigger
);
3096 if (is_trigger_action_notify(trigger
)) {
3098 * Remove and release the client list from
3099 * notification_trigger_clients_ht.
3101 client_list
= get_client_list_from_condition(state
, condition
);
3102 LTTNG_ASSERT(client_list
);
3104 pthread_mutex_lock(&client_list
->lock
);
3105 cds_list_del(&trigger_ht_element
->client_list_trigger_node
);
3106 pthread_mutex_unlock(&client_list
->lock
);
3108 /* Put new reference and the hashtable's reference. */
3109 notification_client_list_put(client_list
);
3110 notification_client_list_put(client_list
);
3114 /* Remove trigger from triggers_ht. */
3115 notif_thread_state_remove_trigger_ht_elem(state
, trigger_ht_element
);
3117 /* Release the ownership of the trigger. */
3118 lttng_trigger_destroy(trigger_ht_element
->trigger
);
3119 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
3123 *_cmd_reply
= cmd_reply
;
3129 int pop_cmd_queue(struct notification_thread_handle
*handle
,
3130 struct notification_thread_command
**cmd
)
3135 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
3136 ret
= lttng_read(handle
->cmd_queue
.event_fd
, &counter
, sizeof(counter
));
3137 if (ret
!= sizeof(counter
)) {
3142 *cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
3143 struct notification_thread_command
, cmd_list_node
);
3144 cds_list_del(&((*cmd
)->cmd_list_node
));
3148 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
3152 /* Returns 0 on success, 1 on exit requested, negative value on error. */
3153 int handle_notification_thread_command(
3154 struct notification_thread_handle
*handle
,
3155 struct notification_thread_state
*state
)
3158 struct notification_thread_command
*cmd
;
3160 ret
= pop_cmd_queue(handle
, &cmd
);
3165 DBG("Received `%s` command",
3166 notification_command_type_str(cmd
->type
));
3167 switch (cmd
->type
) {
3168 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
3169 ret
= handle_notification_thread_command_register_trigger(state
,
3170 cmd
->parameters
.register_trigger
.trigger
,
3171 cmd
->parameters
.register_trigger
.is_trigger_anonymous
,
3174 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
3175 ret
= handle_notification_thread_command_unregister_trigger(
3177 cmd
->parameters
.unregister_trigger
.trigger
,
3180 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
3181 ret
= handle_notification_thread_command_add_channel(
3183 cmd
->parameters
.add_channel
.session
.name
,
3184 cmd
->parameters
.add_channel
.session
.uid
,
3185 cmd
->parameters
.add_channel
.session
.gid
,
3186 cmd
->parameters
.add_channel
.channel
.name
,
3187 cmd
->parameters
.add_channel
.channel
.domain
,
3188 cmd
->parameters
.add_channel
.channel
.key
,
3189 cmd
->parameters
.add_channel
.channel
.capacity
,
3192 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
3193 ret
= handle_notification_thread_command_remove_channel(
3194 state
, cmd
->parameters
.remove_channel
.key
,
3195 cmd
->parameters
.remove_channel
.domain
,
3198 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
3199 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
3200 ret
= handle_notification_thread_command_session_rotation(
3203 cmd
->parameters
.session_rotation
.session_name
,
3204 cmd
->parameters
.session_rotation
.uid
,
3205 cmd
->parameters
.session_rotation
.gid
,
3206 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
3207 cmd
->parameters
.session_rotation
.location
,
3210 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
3211 ret
= handle_notification_thread_command_add_tracer_event_source(
3213 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3214 cmd
->parameters
.tracer_event_source
.domain
,
3217 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
3218 ret
= handle_notification_thread_command_remove_tracer_event_source(
3220 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
3223 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
3225 struct lttng_triggers
*triggers
= NULL
;
3227 ret
= handle_notification_thread_command_list_triggers(
3230 cmd
->parameters
.list_triggers
.uid
,
3233 cmd
->reply
.list_triggers
.triggers
= triggers
;
3237 case NOTIFICATION_COMMAND_TYPE_QUIT
:
3238 cmd
->reply_code
= LTTNG_OK
;
3241 case NOTIFICATION_COMMAND_TYPE_GET_TRIGGER
:
3243 struct lttng_trigger
*trigger
= NULL
;
3245 ret
= handle_notification_thread_command_get_trigger(state
,
3246 cmd
->parameters
.get_trigger
.trigger
, &trigger
,
3248 cmd
->reply
.get_trigger
.trigger
= trigger
;
3251 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
3253 const enum client_transmission_status client_status
=
3254 cmd
->parameters
.client_communication_update
3256 const notification_client_id client_id
=
3257 cmd
->parameters
.client_communication_update
.id
;
3258 struct notification_client
*client
;
3261 client
= get_client_from_id(client_id
, state
);
3265 * Client error was probably already picked-up by the
3266 * notification thread or it has disconnected
3267 * gracefully while this command was queued.
3269 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
3273 ret
= client_handle_transmission_status(
3274 client
, client_status
, state
);
3280 ERR("Unknown internal command received");
3288 if (cmd
->is_async
) {
3292 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3296 /* Wake-up and return a fatal error to the calling thread. */
3297 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3298 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3300 /* Indicate a fatal error to the caller. */
3305 int socket_set_non_blocking(int socket
)
3309 /* Set the pipe as non-blocking. */
3310 ret
= fcntl(socket
, F_GETFL
, 0);
3312 PERROR("fcntl get socket flags");
3317 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3319 PERROR("fcntl set O_NONBLOCK socket flag");
3322 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3328 int client_reset_inbound_state(struct notification_client
*client
)
3333 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3335 client
->communication
.inbound
.bytes_to_receive
=
3336 sizeof(struct lttng_notification_channel_message
);
3337 client
->communication
.inbound
.msg_type
=
3338 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3339 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3340 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3341 ret
= lttng_dynamic_buffer_set_size(
3342 &client
->communication
.inbound
.payload
.buffer
,
3343 client
->communication
.inbound
.bytes_to_receive
);
3348 int handle_notification_thread_client_connect(
3349 struct notification_thread_state
*state
)
3352 struct notification_client
*client
;
3354 DBG("Handling new notification channel client connection");
3356 client
= zmalloc
<notification_client
>();
3363 pthread_mutex_init(&client
->lock
, NULL
);
3364 client
->id
= state
->next_notification_client_id
++;
3365 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3366 lttng_payload_init(&client
->communication
.inbound
.payload
);
3367 lttng_payload_init(&client
->communication
.outbound
.payload
);
3368 client
->communication
.inbound
.expect_creds
= true;
3370 ret
= client_reset_inbound_state(client
);
3372 ERR("Failed to reset client communication's inbound state");
3377 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3379 ERR("Failed to accept new notification channel client connection");
3384 client
->socket
= ret
;
3386 ret
= socket_set_non_blocking(client
->socket
);
3388 ERR("Failed to set new notification channel client connection socket as non-blocking");
3392 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3394 ERR("Failed to set socket options on new notification channel client socket");
3399 client
->communication
.current_poll_events
= CLIENT_POLL_EVENTS_IN
;
3400 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3401 client
->communication
.current_poll_events
);
3403 ERR("Failed to add notification channel client socket to poll set");
3407 DBG("Added new notification channel client socket (%i) to poll set",
3411 cds_lfht_add(state
->client_socket_ht
,
3412 hash_client_socket(client
->socket
),
3413 &client
->client_socket_ht_node
);
3414 cds_lfht_add(state
->client_id_ht
,
3415 hash_client_id(client
->id
),
3416 &client
->client_id_ht_node
);
3422 notification_client_destroy(client
);
3427 * RCU read-lock must be held by the caller.
3428 * Client lock must _not_ be held by the caller.
3431 int notification_thread_client_disconnect(
3432 struct notification_client
*client
,
3433 struct notification_thread_state
*state
)
3436 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3438 ASSERT_RCU_READ_LOCKED();
3440 /* Acquire the client lock to disable its communication atomically. */
3441 pthread_mutex_lock(&client
->lock
);
3442 client
->communication
.active
= false;
3443 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3444 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3445 pthread_mutex_unlock(&client
->lock
);
3447 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3449 ERR("Failed to remove client socket %d from poll set",
3453 /* Release all conditions to which the client was subscribed. */
3454 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3455 &client
->condition_list
, node
) {
3456 (void) notification_thread_client_unsubscribe(client
,
3457 condition_list_element
->condition
, state
, NULL
);
3461 * Client no longer accessible to other threads (through the
3464 notification_client_destroy(client
);
3468 int handle_notification_thread_client_disconnect(
3469 int client_socket
, struct notification_thread_state
*state
)
3472 struct notification_client
*client
;
3475 DBG("Closing client connection (socket fd = %i)",
3477 client
= get_client_from_socket(client_socket
, state
);
3479 /* Internal state corruption, fatal error. */
3480 ERR("Unable to find client (socket fd = %i)",
3486 ret
= notification_thread_client_disconnect(client
, state
);
3492 int handle_notification_thread_client_disconnect_all(
3493 struct notification_thread_state
*state
)
3495 struct cds_lfht_iter iter
;
3496 struct notification_client
*client
;
3497 bool error_encoutered
= false;
3500 DBG("Closing all client connections");
3501 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3502 client_socket_ht_node
) {
3505 ret
= notification_thread_client_disconnect(
3508 error_encoutered
= true;
3512 return error_encoutered
? 1 : 0;
3515 int handle_notification_thread_trigger_unregister_all(
3516 struct notification_thread_state
*state
)
3518 bool error_occurred
= false;
3519 struct cds_lfht_iter iter
;
3520 struct lttng_trigger_ht_element
*trigger_ht_element
;
3523 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3525 int ret
= handle_notification_thread_command_unregister_trigger(
3526 state
, trigger_ht_element
->trigger
, NULL
);
3528 error_occurred
= true;
3532 return error_occurred
? -1 : 0;
3536 bool client_has_outbound_data_left(
3537 const struct notification_client
*client
)
3539 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3540 &client
->communication
.outbound
.payload
, 0, -1);
3541 const bool has_data
= pv
.buffer
.size
!= 0;
3542 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3544 return has_data
|| has_fds
;
3548 int client_handle_transmission_status(
3549 struct notification_client
*client
,
3550 enum client_transmission_status transmission_status
,
3551 struct notification_thread_state
*state
)
3555 switch (transmission_status
) {
3556 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3557 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3559 int current_poll_events
;
3560 int new_poll_events
;
3562 * We want to be notified whenever there is buffer space
3563 * available to send the rest of the payload if we are
3564 * waiting to send data to the client.
3566 * The state of the outbound queue being sampled here is
3568 * - it is okay to wake-up "for nothing" in case we see
3569 * that data is left, but another thread succeeds in
3570 * flushing it before us when handling the client "out"
3571 * event. We will simply stop monitoring that event the next
3572 * time it wakes us up and we see no data left to be sent,
3573 * - if another thread fails to flush the entire client
3574 * outgoing queue, it will issue a "communication update"
3575 * command and cause the client's (e)poll mask to be
3578 * The situation we seek to avoid would be to disable the
3579 * monitoring of "out" client events indefinitely when there is
3580 * data to be sent, which can't happen because of the
3581 * aforementioned "communication update" mechanism.
3583 pthread_mutex_lock(&client
->lock
);
3584 current_poll_events
= client
->communication
.current_poll_events
;
3585 new_poll_events
= client_has_outbound_data_left(client
) ?
3586 CLIENT_POLL_EVENTS_IN_OUT
:
3587 CLIENT_POLL_EVENTS_IN
;
3588 client
->communication
.current_poll_events
= new_poll_events
;
3589 pthread_mutex_unlock(&client
->lock
);
3591 /* Update the monitored event set only if it changed. */
3592 if (current_poll_events
!= new_poll_events
) {
3593 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3602 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3603 ret
= notification_thread_client_disconnect(client
, state
);
3608 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3618 /* Client lock must be acquired by caller. */
3620 enum client_transmission_status
client_flush_outgoing_queue(
3621 struct notification_client
*client
)
3624 size_t to_send_count
;
3625 enum client_transmission_status status
;
3626 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3627 &client
->communication
.outbound
.payload
, 0, -1);
3628 const int fds_to_send_count
=
3629 lttng_payload_view_get_fd_handle_count(&pv
);
3631 ASSERT_LOCKED(client
->lock
);
3633 if (!client
->communication
.active
) {
3634 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3638 if (pv
.buffer
.size
== 0) {
3640 * If both data and fds are equal to zero, we are in an invalid
3643 LTTNG_ASSERT(fds_to_send_count
!= 0);
3648 to_send_count
= pv
.buffer
.size
;
3649 DBG("Flushing client (socket fd = %i) outgoing queue",
3652 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3655 if ((ret
>= 0 && ret
< to_send_count
)) {
3656 DBG("Client (socket fd = %i) outgoing queue could not be completely flushed",
3658 to_send_count
-= std::max(ret
, (ssize_t
) 0);
3660 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3662 pv
.buffer
.size
- to_send_count
,
3664 ret
= lttng_dynamic_buffer_set_size(
3665 &client
->communication
.outbound
.payload
.buffer
,
3671 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3673 } else if (ret
< 0) {
3674 /* Generic error, disable the client's communication. */
3675 ERR("Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3677 client
->communication
.active
= false;
3678 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3682 * No error and flushed the queue completely.
3684 * The payload buffer size is used later to
3685 * check if there is notifications queued. So albeit that the
3686 * direct caller knows that the transmission is complete, we
3687 * need to set the buffer size to zero.
3689 ret
= lttng_dynamic_buffer_set_size(
3690 &client
->communication
.outbound
.payload
.buffer
, 0);
3697 /* No fds to send, transmission is complete. */
3698 if (fds_to_send_count
== 0) {
3699 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3703 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3704 client
->socket
, &pv
);
3706 /* Generic error, disable the client's communication. */
3707 ERR("Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3709 client
->communication
.active
= false;
3710 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3712 } else if (ret
== 0) {
3713 /* Nothing could be sent. */
3714 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3716 /* Fd passing is an all or nothing kind of thing. */
3717 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3719 * The payload _fd_array count is used later to
3720 * check if there is notifications queued. So although the
3721 * direct caller knows that the transmission is complete, we
3722 * need to clear the _fd_array for the queuing check.
3724 lttng_dynamic_pointer_array_clear(
3725 &client
->communication
.outbound
.payload
3730 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3731 client
->communication
.outbound
.queued_command_reply
= false;
3732 client
->communication
.outbound
.dropped_notification
= false;
3733 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3738 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3741 /* Client lock must _not_ be held by the caller. */
3743 int client_send_command_reply(struct notification_client
*client
,
3744 struct notification_thread_state
*state
,
3745 enum lttng_notification_channel_status status
)
3748 struct lttng_notification_channel_command_reply reply
= {
3749 .status
= (int8_t) status
,
3751 struct lttng_notification_channel_message msg
= {
3752 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3753 .size
= sizeof(reply
),
3756 char buffer
[sizeof(msg
) + sizeof(reply
)];
3757 enum client_transmission_status transmission_status
;
3759 memcpy(buffer
, &msg
, sizeof(msg
));
3760 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3761 DBG("Send command reply (%i)", (int) status
);
3763 pthread_mutex_lock(&client
->lock
);
3764 if (client
->communication
.outbound
.queued_command_reply
) {
3765 /* Protocol error. */
3769 /* Enqueue buffer to outgoing queue and flush it. */
3770 ret
= lttng_dynamic_buffer_append(
3771 &client
->communication
.outbound
.payload
.buffer
,
3772 buffer
, sizeof(buffer
));
3777 transmission_status
= client_flush_outgoing_queue(client
);
3779 if (client_has_outbound_data_left(client
)) {
3780 /* Queue could not be emptied. */
3781 client
->communication
.outbound
.queued_command_reply
= true;
3784 pthread_mutex_unlock(&client
->lock
);
3785 ret
= client_handle_transmission_status(
3786 client
, transmission_status
, state
);
3793 pthread_mutex_unlock(&client
->lock
);
3799 int client_handle_message_unknown(struct notification_client
*client
,
3800 struct notification_thread_state
*state
__attribute__((unused
)))
3804 * Receiving message header. The function will be called again
3805 * once the rest of the message as been received and can be
3808 const struct lttng_notification_channel_message
*msg
;
3810 LTTNG_ASSERT(sizeof(*msg
) == client
->communication
.inbound
.payload
.buffer
.size
);
3811 msg
= (const struct lttng_notification_channel_message
*)
3812 client
->communication
.inbound
.payload
.buffer
.data
;
3814 if (msg
->size
== 0 ||
3815 msg
->size
> DEFAULT_MAX_NOTIFICATION_CLIENT_MESSAGE_PAYLOAD_SIZE
) {
3816 ERR("Invalid notification channel message: length = %u",
3822 switch (msg
->type
) {
3823 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
3824 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
3825 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
3829 ERR("Invalid notification channel message: unexpected message type");
3833 client
->communication
.inbound
.bytes_to_receive
= msg
->size
;
3834 client
->communication
.inbound
.fds_to_receive
= msg
->fds
;
3835 client
->communication
.inbound
.msg_type
=
3836 (enum lttng_notification_channel_message_type
) msg
->type
;
3837 ret
= lttng_dynamic_buffer_set_size(
3838 &client
->communication
.inbound
.payload
.buffer
, msg
->size
);
3840 /* msg is not valid anymore due to lttng_dynamic_buffer_set_size. */
3847 int client_handle_message_handshake(struct notification_client
*client
,
3848 struct notification_thread_state
*state
)
3851 struct lttng_notification_channel_command_handshake
*handshake_client
;
3852 const struct lttng_notification_channel_command_handshake handshake_reply
= {
3853 .major
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
,
3854 .minor
= LTTNG_NOTIFICATION_CHANNEL_VERSION_MINOR
,
3856 const struct lttng_notification_channel_message msg_header
= {
3857 .type
= LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
,
3858 .size
= sizeof(handshake_reply
),
3861 enum lttng_notification_channel_status status
=
3862 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3863 char send_buffer
[sizeof(msg_header
) + sizeof(handshake_reply
)];
3865 memcpy(send_buffer
, &msg_header
, sizeof(msg_header
));
3866 memcpy(send_buffer
+ sizeof(msg_header
), &handshake_reply
,
3867 sizeof(handshake_reply
));
3870 (struct lttng_notification_channel_command_handshake
*)
3871 client
->communication
.inbound
.payload
.buffer
3873 client
->major
= handshake_client
->major
;
3874 client
->minor
= handshake_client
->minor
;
3875 if (!client
->communication
.inbound
.creds_received
) {
3876 ERR("No credentials received from client");
3881 client
->uid
= LTTNG_SOCK_GET_UID_CRED(
3882 &client
->communication
.inbound
.creds
);
3883 client
->gid
= LTTNG_SOCK_GET_GID_CRED(
3884 &client
->communication
.inbound
.creds
);
3885 client
->is_sessiond
= LTTNG_SOCK_GET_PID_CRED(&client
->communication
.inbound
.creds
) == getpid();
3886 DBG("Received handshake from client: uid = %u, gid = %u, protocol version = %i.%i, client is sessiond = %s",
3887 client
->uid
, client
->gid
, (int) client
->major
,
3888 (int) client
->minor
,
3889 client
->is_sessiond
? "true" : "false");
3891 if (handshake_client
->major
!=
3892 LTTNG_NOTIFICATION_CHANNEL_VERSION_MAJOR
) {
3893 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNSUPPORTED_VERSION
;
3896 pthread_mutex_lock(&client
->lock
);
3897 /* Outgoing queue will be flushed when the command reply is sent. */
3898 ret
= lttng_dynamic_buffer_append(
3899 &client
->communication
.outbound
.payload
.buffer
, send_buffer
,
3900 sizeof(send_buffer
));
3902 ERR("Failed to send protocol version to notification channel client");
3906 client
->validated
= true;
3907 client
->communication
.active
= true;
3908 pthread_mutex_unlock(&client
->lock
);
3910 /* Set reception state to receive the next message header. */
3911 ret
= client_reset_inbound_state(client
);
3913 ERR("Failed to reset client communication's inbound state");
3917 /* Flushes the outgoing queue. */
3918 ret
= client_send_command_reply(client
, state
, status
);
3920 ERR("Failed to send reply to notification channel client");
3926 pthread_mutex_unlock(&client
->lock
);
3932 int client_handle_message_subscription(
3933 struct notification_client
*client
,
3934 enum lttng_notification_channel_message_type msg_type
,
3935 struct notification_thread_state
*state
)
3938 struct lttng_condition
*condition
;
3939 enum lttng_notification_channel_status status
=
3940 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
3941 struct lttng_payload_view condition_view
=
3942 lttng_payload_view_from_payload(
3943 &client
->communication
.inbound
.payload
,
3945 size_t expected_condition_size
;
3948 * No need to lock client to sample the inbound state as the only
3949 * other thread accessing clients (action executor) only uses the
3952 expected_condition_size
= client
->communication
.inbound
.payload
.buffer
.size
;
3953 ret
= lttng_condition_create_from_payload(&condition_view
, &condition
);
3954 if (ret
!= expected_condition_size
) {
3955 ERR("Malformed condition received from client");
3959 /* Ownership of condition is always transferred. */
3960 if (msg_type
== LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
) {
3961 ret
= notification_thread_client_subscribe(
3962 client
, condition
, state
, &status
);
3964 ret
= notification_thread_client_unsubscribe(
3965 client
, condition
, state
, &status
);
3972 /* Set reception state to receive the next message header. */
3973 ret
= client_reset_inbound_state(client
);
3975 ERR("Failed to reset client communication's inbound state");
3979 ret
= client_send_command_reply(client
, state
, status
);
3981 ERR("Failed to send reply to notification channel client");
3990 int client_dispatch_message(struct notification_client
*client
,
3991 struct notification_thread_state
*state
)
3995 if (client
->communication
.inbound
.msg_type
!=
3996 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
&&
3997 client
->communication
.inbound
.msg_type
!=
3998 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
&&
3999 !client
->validated
) {
4000 WARN("client attempted a command before handshake");
4005 switch (client
->communication
.inbound
.msg_type
) {
4006 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
:
4008 ret
= client_handle_message_unknown(client
, state
);
4011 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_HANDSHAKE
:
4013 ret
= client_handle_message_handshake(client
, state
);
4016 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_SUBSCRIBE
:
4017 case LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNSUBSCRIBE
:
4019 ret
= client_handle_message_subscription(client
,
4020 client
->communication
.inbound
.msg_type
, state
);
4030 /* Incoming data from client. */
4031 int handle_notification_thread_client_in(
4032 struct notification_thread_state
*state
, int socket
)
4035 struct notification_client
*client
;
4040 client
= get_client_from_socket(socket
, state
);
4042 /* Internal error, abort. */
4047 if (client
->communication
.inbound
.bytes_to_receive
== 0 &&
4048 client
->communication
.inbound
.fds_to_receive
!= 0) {
4049 /* Only FDs left to receive. */
4053 offset
= client
->communication
.inbound
.payload
.buffer
.size
-
4054 client
->communication
.inbound
.bytes_to_receive
;
4055 if (client
->communication
.inbound
.expect_creds
) {
4056 recv_ret
= lttcomm_recv_creds_unix_sock(socket
,
4057 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
4058 client
->communication
.inbound
.bytes_to_receive
,
4059 &client
->communication
.inbound
.creds
);
4061 client
->communication
.inbound
.expect_creds
= false;
4062 client
->communication
.inbound
.creds_received
= true;
4065 recv_ret
= lttcomm_recv_unix_sock_non_block(socket
,
4066 client
->communication
.inbound
.payload
.buffer
.data
+ offset
,
4067 client
->communication
.inbound
.bytes_to_receive
);
4069 if (recv_ret
>= 0) {
4070 client
->communication
.inbound
.bytes_to_receive
-= recv_ret
;
4072 goto error_disconnect_client
;
4075 if (client
->communication
.inbound
.bytes_to_receive
!= 0) {
4076 /* Message incomplete wait for more data. */
4082 LTTNG_ASSERT(client
->communication
.inbound
.bytes_to_receive
== 0);
4085 if (client
->communication
.inbound
.fds_to_receive
!= 0) {
4086 ret
= lttcomm_recv_payload_fds_unix_sock_non_block(
4088 client
->communication
.inbound
.fds_to_receive
,
4089 &client
->communication
.inbound
.payload
);
4092 * Fds received. non blocking fds passing is all
4095 ssize_t expected_size
;
4097 expected_size
= sizeof(int) *
4098 client
->communication
.inbound
4100 LTTNG_ASSERT(ret
== expected_size
);
4101 client
->communication
.inbound
.fds_to_receive
= 0;
4102 } else if (ret
== 0) {
4103 /* Received nothing. */
4107 goto error_disconnect_client
;
4111 /* At this point the message is complete.*/
4112 LTTNG_ASSERT(client
->communication
.inbound
.bytes_to_receive
== 0 &&
4113 client
->communication
.inbound
.fds_to_receive
== 0);
4114 ret
= client_dispatch_message(client
, state
);
4117 * Only returns an error if this client must be
4120 goto error_disconnect_client
;
4127 error_disconnect_client
:
4128 ret
= notification_thread_client_disconnect(client
, state
);
4132 /* Client ready to receive outgoing data. */
4133 int handle_notification_thread_client_out(
4134 struct notification_thread_state
*state
, int socket
)
4137 struct notification_client
*client
;
4138 enum client_transmission_status transmission_status
;
4141 client
= get_client_from_socket(socket
, state
);
4143 /* Internal error, abort. */
4148 pthread_mutex_lock(&client
->lock
);
4149 if (!client_has_outbound_data_left(client
)) {
4151 * A client "out" event can be received when no payload is left
4152 * to send under some circumstances.
4154 * Many threads can flush a client's outgoing queue and, if they
4155 * had to queue their message (socket was full), will use the
4156 * "communication update" command to signal the (e)poll thread
4157 * to monitor for space being made available in the socket.
4159 * Commands are sent over an internal pipe serviced by the same
4160 * thread as the client sockets.
4162 * When space is made available in the socket, there is a race
4163 * between the (e)poll thread and the other threads that may
4164 * wish to use the client's socket to flush its outgoing queue.
4166 * A non-(e)poll thread may attempt (and succeed) in flushing
4167 * the queue before the (e)poll thread gets a chance to service
4168 * the client's "out" event.
4170 * In this situation, the (e)poll thread processing the client
4171 * out event will see an empty payload: there is nothing to do
4172 * except unsubscribing (e)poll "out" events.
4174 * Note that this thread is the (e)poll thread so it can modify
4175 * the (e)poll mask directly without using a communication
4176 * update command. Other threads that flush the outgoing queue
4177 * will use the "communication update" command to wake up this
4178 * thread and force it to monitor "out" events.
4180 * When other threads succeed in emptying the outgoing queue,
4181 * they don't need to update the (e)poll mask: if the "out"
4182 * event is monitored, it will fire once and the (e)poll
4183 * thread will reach this condition, causing the event to
4184 * stop being monitored.
4186 transmission_status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
4188 transmission_status
= client_flush_outgoing_queue(client
);
4190 pthread_mutex_unlock(&client
->lock
);
4192 ret
= client_handle_transmission_status(
4193 client
, transmission_status
, state
);
4203 bool evaluate_buffer_usage_condition(const struct lttng_condition
*condition
,
4204 const struct channel_state_sample
*sample
,
4205 uint64_t buffer_capacity
)
4207 bool result
= false;
4209 enum lttng_condition_type condition_type
;
4210 const struct lttng_condition_buffer_usage
*use_condition
= container_of(
4211 condition
, struct lttng_condition_buffer_usage
,
4214 if (use_condition
->threshold_bytes
.set
) {
4215 threshold
= use_condition
->threshold_bytes
.value
;
4218 * Threshold was expressed as a ratio.
4220 * TODO the threshold (in bytes) of conditions expressed
4221 * as a ratio of total buffer size could be cached to
4222 * forego this double-multiplication or it could be performed
4223 * as fixed-point math.
4225 * Note that caching should accommodates the case where the
4226 * condition applies to multiple channels (i.e. don't assume
4227 * that all channels matching my_chann* have the same size...)
4229 threshold
= (uint64_t) (use_condition
->threshold_ratio
.value
*
4230 (double) buffer_capacity
);
4233 condition_type
= lttng_condition_get_type(condition
);
4234 if (condition_type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
) {
4235 DBG("Low buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4236 threshold
, sample
->highest_usage
);
4239 * The low condition should only be triggered once _all_ of the
4240 * streams in a channel have gone below the "low" threshold.
4242 if (sample
->highest_usage
<= threshold
) {
4246 DBG("High buffer usage condition being evaluated: threshold = %" PRIu64
", highest usage = %" PRIu64
,
4247 threshold
, sample
->highest_usage
);
4250 * For high buffer usage scenarios, we want to trigger whenever
4251 * _any_ of the streams has reached the "high" threshold.
4253 if (sample
->highest_usage
>= threshold
) {
4262 bool evaluate_session_consumed_size_condition(
4263 const struct lttng_condition
*condition
,
4264 uint64_t session_consumed_size
)
4267 const struct lttng_condition_session_consumed_size
*size_condition
=
4268 container_of(condition
,
4269 struct lttng_condition_session_consumed_size
,
4272 threshold
= size_condition
->consumed_threshold_bytes
.value
;
4273 DBG("Session consumed size condition being evaluated: threshold = %" PRIu64
", current size = %" PRIu64
,
4274 threshold
, session_consumed_size
);
4275 return session_consumed_size
>= threshold
;
4279 int evaluate_buffer_condition(const struct lttng_condition
*condition
,
4280 struct lttng_evaluation
**evaluation
,
4281 const struct notification_thread_state
*state
__attribute__((unused
)),
4282 const struct channel_state_sample
*previous_sample
,
4283 const struct channel_state_sample
*latest_sample
,
4284 uint64_t previous_session_consumed_total
,
4285 uint64_t latest_session_consumed_total
,
4286 struct channel_info
*channel_info
)
4289 enum lttng_condition_type condition_type
;
4290 const bool previous_sample_available
= !!previous_sample
;
4291 bool previous_sample_result
= false;
4292 bool latest_sample_result
;
4294 condition_type
= lttng_condition_get_type(condition
);
4296 switch (condition_type
) {
4297 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4298 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4299 if (caa_likely(previous_sample_available
)) {
4300 previous_sample_result
=
4301 evaluate_buffer_usage_condition(condition
,
4302 previous_sample
, channel_info
->capacity
);
4304 latest_sample_result
= evaluate_buffer_usage_condition(
4305 condition
, latest_sample
,
4306 channel_info
->capacity
);
4308 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4309 if (caa_likely(previous_sample_available
)) {
4310 previous_sample_result
=
4311 evaluate_session_consumed_size_condition(
4313 previous_session_consumed_total
);
4315 latest_sample_result
=
4316 evaluate_session_consumed_size_condition(
4318 latest_session_consumed_total
);
4321 /* Unknown condition type; internal error. */
4325 if (!latest_sample_result
||
4326 (previous_sample_result
== latest_sample_result
)) {
4328 * Only trigger on a condition evaluation transition.
4330 * NOTE: This edge-triggered logic may not be appropriate for
4331 * future condition types.
4336 if (!evaluation
|| !latest_sample_result
) {
4340 switch (condition_type
) {
4341 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
4342 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
4343 *evaluation
= lttng_evaluation_buffer_usage_create(
4345 latest_sample
->highest_usage
,
4346 channel_info
->capacity
);
4348 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
4349 *evaluation
= lttng_evaluation_session_consumed_size_create(
4350 latest_session_consumed_total
);
4365 int client_notification_overflow(struct notification_client
*client
)
4368 const struct lttng_notification_channel_message msg
= {
4369 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION_DROPPED
,
4374 ASSERT_LOCKED(client
->lock
);
4376 DBG("Dropping notification addressed to client (socket fd = %i)",
4378 if (client
->communication
.outbound
.dropped_notification
) {
4380 * The client already has a "notification dropped" message
4381 * in its outgoing queue. Nothing to do since all
4382 * of those messages are coalesced.
4387 client
->communication
.outbound
.dropped_notification
= true;
4388 ret
= lttng_dynamic_buffer_append(
4389 &client
->communication
.outbound
.payload
.buffer
, &msg
,
4392 PERROR("Failed to enqueue \"dropped notification\" message in client's (socket fd = %i) outgoing queue",
4399 static int client_handle_transmission_status_wrapper(
4400 struct notification_client
*client
,
4401 enum client_transmission_status status
,
4404 return client_handle_transmission_status(client
, status
,
4405 (struct notification_thread_state
*) user_data
);
4409 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
4410 const struct lttng_evaluation
*evaluation
,
4411 struct notification_client_list
* client_list
,
4412 struct notification_thread_state
*state
,
4413 uid_t object_uid
, gid_t object_gid
)
4415 const struct lttng_credentials creds
= {
4416 .uid
= LTTNG_OPTIONAL_INIT_VALUE(object_uid
),
4417 .gid
= LTTNG_OPTIONAL_INIT_VALUE(object_gid
),
4420 return notification_client_list_send_evaluation(client_list
,
4421 trigger
, evaluation
,
4423 client_handle_transmission_status_wrapper
, state
);
4427 * Permission checks relative to notification channel clients are performed
4428 * here. Notice how object, client, and trigger credentials are involved in
4431 * The `object` credentials are the credentials associated with the "subject"
4432 * of a condition. For instance, a `rotation completed` condition applies
4433 * to a session. When that condition is met, it will produce an evaluation
4434 * against a session. Hence, in this case, the `object` credentials are the
4435 * credentials of the "subject" session.
4437 * The `trigger` credentials are the credentials of the user that registered the
4440 * The `client` credentials are the credentials of the user that created a given
4441 * notification channel.
4443 * In terms of visibility, it is expected that non-privilieged users can only
4444 * register triggers against "their" objects (their own sessions and
4445 * applications they are allowed to interact with). They can then open a
4446 * notification channel and subscribe to notifications associated with those
4449 * As for privilieged users, they can register triggers against the objects of
4450 * other users. They can then subscribe to the notifications associated to their
4451 * triggers. Privilieged users _can't_ subscribe to the notifications of
4452 * triggers owned by other users; they must create their own triggers.
4454 * This is more a concern of usability than security. It would be difficult for
4455 * a root user reliably subscribe to a specific set of conditions without
4456 * interference from external users (those could, for instance, unregister
4459 int notification_client_list_send_evaluation(
4460 struct notification_client_list
*client_list
,
4461 const struct lttng_trigger
*trigger
,
4462 const struct lttng_evaluation
*evaluation
,
4463 const struct lttng_credentials
*source_object_creds
,
4464 report_client_transmission_result_cb client_report
,
4468 struct lttng_payload msg_payload
;
4469 struct notification_client_list_element
*client_list_element
, *tmp
;
4470 const struct lttng_notification notification
= {
4471 .trigger
= (struct lttng_trigger
*) trigger
,
4472 .evaluation
= (struct lttng_evaluation
*) evaluation
,
4474 struct lttng_notification_channel_message msg_header
= {
4475 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_NOTIFICATION
,
4479 const struct lttng_credentials
*trigger_creds
=
4480 lttng_trigger_get_credentials(trigger
);
4482 lttng_payload_init(&msg_payload
);
4484 ret
= lttng_dynamic_buffer_append(&msg_payload
.buffer
, &msg_header
,
4485 sizeof(msg_header
));
4490 ret
= lttng_notification_serialize(¬ification
, &msg_payload
);
4492 ERR("Failed to serialize notification");
4497 /* Update payload size. */
4498 ((struct lttng_notification_channel_message
*) msg_payload
.buffer
.data
)
4499 ->size
= (uint32_t)(
4500 msg_payload
.buffer
.size
- sizeof(msg_header
));
4502 /* Update the payload number of fds. */
4504 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
4505 &msg_payload
, 0, -1);
4507 ((struct lttng_notification_channel_message
*)
4508 msg_payload
.buffer
.data
)->fds
= (uint32_t)
4509 lttng_payload_view_get_fd_handle_count(&pv
);
4512 pthread_mutex_lock(&client_list
->lock
);
4513 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4514 &client_list
->clients_list
, node
) {
4515 enum client_transmission_status transmission_status
;
4516 struct notification_client
*client
=
4517 client_list_element
->client
;
4520 pthread_mutex_lock(&client
->lock
);
4521 if (!client
->communication
.active
) {
4523 * Skip inactive client (protocol error or
4526 DBG("Skipping client at it is marked as inactive");
4530 if (lttng_trigger_is_hidden(trigger
) && !client
->is_sessiond
) {
4532 * Notifications resulting from an hidden trigger are
4533 * only sent to the session daemon.
4535 DBG("Skipping client as the trigger is hidden and the client is not the session daemon");
4539 if (source_object_creds
) {
4540 if (client
->uid
!= lttng_credentials_get_uid(source_object_creds
) &&
4541 client
->gid
!= lttng_credentials_get_gid(source_object_creds
) &&
4544 * Client is not allowed to monitor this
4547 DBG("Skipping client at it does not have the object permission to receive notification for this trigger");
4552 if (client
->uid
!= lttng_credentials_get_uid(trigger_creds
)) {
4553 DBG("Skipping client at it does not have the permission to receive notification for this trigger");
4557 DBG("Sending notification to client (fd = %i, %zu bytes)",
4558 client
->socket
, msg_payload
.buffer
.size
);
4560 if (client_has_outbound_data_left(client
)) {
4562 * Outgoing data is already buffered for this client;
4563 * drop the notification and enqueue a "dropped
4564 * notification" message if this is the first dropped
4565 * notification since the socket spilled-over to the
4568 ret
= client_notification_overflow(client
);
4575 ret
= lttng_payload_copy(&msg_payload
, &client
->communication
.outbound
.payload
);
4581 transmission_status
= client_flush_outgoing_queue(client
);
4582 pthread_mutex_unlock(&client
->lock
);
4583 ret
= client_report(client
, transmission_status
, user_data
);
4586 goto end_unlock_list
;
4592 pthread_mutex_unlock(&client
->lock
);
4595 goto end_unlock_list
;
4601 pthread_mutex_unlock(&client_list
->lock
);
4603 lttng_payload_reset(&msg_payload
);
4608 struct lttng_event_notifier_notification
*recv_one_event_notifier_notification(
4609 int notification_pipe_read_fd
, enum lttng_domain_type domain
)
4613 struct lttng_event_notifier_notification
*notification
= NULL
;
4614 char *capture_buffer
= NULL
;
4615 size_t capture_buffer_size
;
4616 void *reception_buffer
;
4617 size_t reception_size
;
4619 struct lttng_ust_abi_event_notifier_notification ust_notification
;
4620 struct lttng_kernel_abi_event_notifier_notification kernel_notification
;
4622 /* Init lttng_event_notifier_notification */
4624 case LTTNG_DOMAIN_UST
:
4625 reception_buffer
= (void *) &ust_notification
;
4626 reception_size
= sizeof(ust_notification
);
4628 case LTTNG_DOMAIN_KERNEL
:
4629 reception_buffer
= (void *) &kernel_notification
;
4630 reception_size
= sizeof(kernel_notification
);
4637 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4638 * ensuring that read/write of tracer notifications are atomic.
4640 ret
= lttng_read(notification_pipe_read_fd
, reception_buffer
,
4642 if (ret
!= reception_size
) {
4643 PERROR("Failed to read from event source notification pipe: fd = %d, size to read = %zu, ret = %d",
4644 notification_pipe_read_fd
, reception_size
, ret
);
4650 case LTTNG_DOMAIN_UST
:
4651 token
= ust_notification
.token
;
4652 capture_buffer_size
= ust_notification
.capture_buf_size
;
4654 case LTTNG_DOMAIN_KERNEL
:
4655 token
= kernel_notification
.token
;
4656 capture_buffer_size
= kernel_notification
.capture_buf_size
;
4662 if (capture_buffer_size
== 0) {
4663 capture_buffer
= NULL
;
4667 if (capture_buffer_size
> MAX_CAPTURE_SIZE
) {
4668 ERR("Event notifier has a capture payload size which exceeds the maximum allowed size: capture_payload_size = %zu bytes, max allowed size = %d bytes",
4669 capture_buffer_size
, MAX_CAPTURE_SIZE
);
4673 capture_buffer
= calloc
<char>(capture_buffer_size
);
4674 if (!capture_buffer
) {
4675 ERR("Failed to allocate capture buffer");
4679 /* Fetch additional payload (capture). */
4680 ret
= lttng_read(notification_pipe_read_fd
, capture_buffer
, capture_buffer_size
);
4681 if (ret
!= capture_buffer_size
) {
4682 ERR("Failed to read from event source pipe (fd = %i)",
4683 notification_pipe_read_fd
);
4688 notification
= lttng_event_notifier_notification_create(token
, domain
,
4689 capture_buffer
, capture_buffer_size
);
4690 if (notification
== NULL
) {
4695 * Ownership transfered to the lttng_event_notifier_notification object.
4697 capture_buffer
= NULL
;
4700 free(capture_buffer
);
4701 return notification
;
4705 int dispatch_one_event_notifier_notification(struct notification_thread_state
*state
,
4706 struct lttng_event_notifier_notification
*notification
)
4708 struct cds_lfht_node
*node
;
4709 struct cds_lfht_iter iter
;
4710 struct notification_trigger_tokens_ht_element
*element
;
4711 struct lttng_evaluation
*evaluation
= NULL
;
4712 enum action_executor_status executor_status
;
4713 struct notification_client_list
*client_list
= NULL
;
4715 unsigned int capture_count
= 0;
4717 /* Find triggers associated with this token. */
4719 cds_lfht_lookup(state
->trigger_tokens_ht
,
4720 hash_key_u64(¬ification
->tracer_token
, lttng_ht_seed
),
4721 match_trigger_token
, ¬ification
->tracer_token
, &iter
);
4722 node
= cds_lfht_iter_get_node(&iter
);
4723 if (caa_unlikely(!node
)) {
4725 * This is not an error, slow consumption of the tracer
4726 * notifications can lead to situations where a trigger is
4727 * removed but we still get tracer notifications matching a
4728 * trigger that no longer exists.
4734 element
= caa_container_of(node
,
4735 struct notification_trigger_tokens_ht_element
,
4738 if (lttng_condition_event_rule_matches_get_capture_descriptor_count(
4739 lttng_trigger_get_const_condition(element
->trigger
),
4740 &capture_count
) != LTTNG_CONDITION_STATUS_OK
) {
4741 ERR("Failed to get capture count");
4746 if (!notification
->capture_buffer
&& capture_count
!= 0) {
4747 ERR("Expected capture but capture buffer is null");
4752 evaluation
= lttng_evaluation_event_rule_matches_create(
4753 container_of(lttng_trigger_get_const_condition(
4755 struct lttng_condition_event_rule_matches
,
4757 notification
->capture_buffer
,
4758 notification
->capture_buf_size
, false);
4760 if (evaluation
== NULL
) {
4761 ERR("Failed to create event rule matches evaluation while creating and enqueuing action executor job");
4766 client_list
= get_client_list_from_condition(state
,
4767 lttng_trigger_get_const_condition(element
->trigger
));
4768 executor_status
= action_executor_enqueue_trigger(state
->executor
,
4769 element
->trigger
, evaluation
, NULL
, client_list
);
4770 switch (executor_status
) {
4771 case ACTION_EXECUTOR_STATUS_OK
:
4774 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
4776 struct notification_client_list_element
*client_list_element
,
4780 * Not a fatal error; this is expected and simply means the
4781 * executor has too much work queued already.
4785 /* No clients subscribed to notifications for this trigger. */
4790 /* Warn clients that a notification (or more) was dropped. */
4791 pthread_mutex_lock(&client_list
->lock
);
4792 cds_list_for_each_entry_safe(client_list_element
, tmp
,
4793 &client_list
->clients_list
, node
) {
4794 enum client_transmission_status transmission_status
;
4795 struct notification_client
*client
=
4796 client_list_element
->client
;
4798 pthread_mutex_lock(&client
->lock
);
4799 ret
= client_notification_overflow(client
);
4805 transmission_status
=
4806 client_flush_outgoing_queue(client
);
4807 ret
= client_handle_transmission_status(
4808 client
, transmission_status
, state
);
4814 pthread_mutex_unlock(&client
->lock
);
4820 pthread_mutex_unlock(&client_list
->lock
);
4823 case ACTION_EXECUTOR_STATUS_INVALID
:
4824 case ACTION_EXECUTOR_STATUS_ERROR
:
4825 /* Fatal error, shut down everything. */
4826 ERR("Fatal error encoutered while enqueuing action to the action executor");
4830 /* Unhandled error. */
4835 notification_client_list_put(client_list
);
4842 int handle_one_event_notifier_notification(
4843 struct notification_thread_state
*state
,
4844 int pipe
, enum lttng_domain_type domain
)
4847 struct lttng_event_notifier_notification
*notification
= NULL
;
4849 notification
= recv_one_event_notifier_notification(pipe
, domain
);
4850 if (notification
== NULL
) {
4851 /* Reception failed, don't consider it fatal. */
4852 ERR("Error receiving an event notifier notification from tracer: fd = %i, domain = %s",
4853 pipe
, lttng_domain_type_str(domain
));
4857 ret
= dispatch_one_event_notifier_notification(state
, notification
);
4859 ERR("Error dispatching an event notifier notification from tracer: fd = %i, domain = %s",
4860 pipe
, lttng_domain_type_str(domain
));
4865 lttng_event_notifier_notification_destroy(notification
);
4869 int handle_notification_thread_event_notification(struct notification_thread_state
*state
,
4870 int pipe
, enum lttng_domain_type domain
)
4872 return handle_one_event_notifier_notification(state
, pipe
, domain
);
4875 int handle_notification_thread_channel_sample(
4876 struct notification_thread_state
*state
, int pipe
,
4877 enum lttng_domain_type domain
)
4880 struct lttcomm_consumer_channel_monitor_msg sample_msg
;
4881 struct channel_info
*channel_info
;
4882 struct cds_lfht_node
*node
;
4883 struct cds_lfht_iter iter
;
4884 struct lttng_channel_trigger_list
*trigger_list
;
4885 struct lttng_trigger_list_element
*trigger_list_element
;
4886 bool previous_sample_available
= false;
4887 struct channel_state_sample previous_sample
, latest_sample
;
4888 uint64_t previous_session_consumed_total
, latest_session_consumed_total
;
4889 struct lttng_credentials channel_creds
;
4892 * The monitoring pipe only holds messages smaller than PIPE_BUF,
4893 * ensuring that read/write of sampling messages are atomic.
4895 ret
= lttng_read(pipe
, &sample_msg
, sizeof(sample_msg
));
4896 if (ret
!= sizeof(sample_msg
)) {
4897 ERR("Failed to read from monitoring pipe (fd = %i)",
4904 latest_sample
.key
.key
= sample_msg
.key
;
4905 latest_sample
.key
.domain
= domain
;
4906 latest_sample
.highest_usage
= sample_msg
.highest
;
4907 latest_sample
.lowest_usage
= sample_msg
.lowest
;
4908 latest_sample
.channel_total_consumed
= sample_msg
.total_consumed
;
4912 /* Retrieve the channel's informations */
4913 cds_lfht_lookup(state
->channels_ht
,
4914 hash_channel_key(&latest_sample
.key
),
4918 node
= cds_lfht_iter_get_node(&iter
);
4919 if (caa_unlikely(!node
)) {
4921 * Not an error since the consumer can push a sample to the pipe
4922 * and the rest of the session daemon could notify us of the
4923 * channel's destruction before we get a chance to process that
4926 DBG("Received a sample for an unknown channel from consumerd, key = %" PRIu64
" in %s domain",
4927 latest_sample
.key
.key
,
4928 lttng_domain_type_str(domain
));
4931 channel_info
= caa_container_of(node
, struct channel_info
,
4933 DBG("Handling channel sample for channel %s (key = %" PRIu64
") in session %s (highest usage = %" PRIu64
", lowest usage = %" PRIu64
", total consumed = %" PRIu64
")",
4935 latest_sample
.key
.key
,
4936 channel_info
->session_info
->name
,
4937 latest_sample
.highest_usage
,
4938 latest_sample
.lowest_usage
,
4939 latest_sample
.channel_total_consumed
);
4941 previous_session_consumed_total
=
4942 channel_info
->session_info
->consumed_data_size
;
4944 /* Retrieve the channel's last sample, if it exists, and update it. */
4945 cds_lfht_lookup(state
->channel_state_ht
,
4946 hash_channel_key(&latest_sample
.key
),
4947 match_channel_state_sample
,
4950 node
= cds_lfht_iter_get_node(&iter
);
4951 if (caa_likely(node
)) {
4952 struct channel_state_sample
*stored_sample
;
4954 /* Update the sample stored. */
4955 stored_sample
= caa_container_of(node
,
4956 struct channel_state_sample
,
4957 channel_state_ht_node
);
4959 memcpy(&previous_sample
, stored_sample
,
4960 sizeof(previous_sample
));
4961 stored_sample
->highest_usage
= latest_sample
.highest_usage
;
4962 stored_sample
->lowest_usage
= latest_sample
.lowest_usage
;
4963 stored_sample
->channel_total_consumed
= latest_sample
.channel_total_consumed
;
4964 previous_sample_available
= true;
4966 latest_session_consumed_total
=
4967 previous_session_consumed_total
+
4968 (latest_sample
.channel_total_consumed
- previous_sample
.channel_total_consumed
);
4971 * This is the channel's first sample, allocate space for and
4972 * store the new sample.
4974 struct channel_state_sample
*stored_sample
;
4976 stored_sample
= zmalloc
<channel_state_sample
>();
4977 if (!stored_sample
) {
4982 memcpy(stored_sample
, &latest_sample
, sizeof(*stored_sample
));
4983 cds_lfht_node_init(&stored_sample
->channel_state_ht_node
);
4984 cds_lfht_add(state
->channel_state_ht
,
4985 hash_channel_key(&stored_sample
->key
),
4986 &stored_sample
->channel_state_ht_node
);
4988 latest_session_consumed_total
=
4989 previous_session_consumed_total
+
4990 latest_sample
.channel_total_consumed
;
4993 channel_info
->session_info
->consumed_data_size
=
4994 latest_session_consumed_total
;
4996 /* Find triggers associated with this channel. */
4997 cds_lfht_lookup(state
->channel_triggers_ht
,
4998 hash_channel_key(&latest_sample
.key
),
4999 match_channel_trigger_list
,
5002 node
= cds_lfht_iter_get_node(&iter
);
5003 if (caa_likely(!node
)) {
5007 channel_creds
= (typeof(channel_creds
)) {
5008 .uid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->uid
),
5009 .gid
= LTTNG_OPTIONAL_INIT_VALUE(channel_info
->session_info
->gid
),
5012 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
5013 channel_triggers_ht_node
);
5014 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
5016 const struct lttng_condition
*condition
;
5017 struct lttng_trigger
*trigger
;
5018 struct notification_client_list
*client_list
= NULL
;
5019 struct lttng_evaluation
*evaluation
= NULL
;
5020 enum action_executor_status executor_status
;
5023 trigger
= trigger_list_element
->trigger
;
5024 condition
= lttng_trigger_get_const_condition(trigger
);
5025 LTTNG_ASSERT(condition
);
5028 * Check if any client is subscribed to the result of this
5031 client_list
= get_client_list_from_condition(state
, condition
);
5033 ret
= evaluate_buffer_condition(condition
, &evaluation
, state
,
5034 previous_sample_available
? &previous_sample
: NULL
,
5036 previous_session_consumed_total
,
5037 latest_session_consumed_total
,
5039 if (caa_unlikely(ret
)) {
5043 if (caa_likely(!evaluation
)) {
5048 * Ownership of `evaluation` transferred to the action executor
5049 * no matter the result.
5051 executor_status
= action_executor_enqueue_trigger(
5052 state
->executor
, trigger
, evaluation
,
5053 &channel_creds
, client_list
);
5055 switch (executor_status
) {
5056 case ACTION_EXECUTOR_STATUS_OK
:
5058 case ACTION_EXECUTOR_STATUS_ERROR
:
5059 case ACTION_EXECUTOR_STATUS_INVALID
:
5061 * TODO Add trigger identification (name/id) when
5062 * it is added to the API.
5064 ERR("Fatal error occurred while enqueuing action associated with buffer-condition trigger");
5067 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
5069 * TODO Add trigger identification (name/id) when
5070 * it is added to the API.
5072 * Not a fatal error.
5074 WARN("No space left when enqueuing action associated with buffer-condition trigger");
5082 notification_client_list_put(client_list
);
5083 if (caa_unlikely(ret
)) {