2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "lttng/action/action.h"
9 #include "lttng/trigger/trigger-internal.h"
12 #include <urcu/rculfhash.h>
14 #include <common/defaults.h>
15 #include <common/error.h>
16 #include <common/futex.h>
17 #include <common/unix.h>
18 #include <common/dynamic-buffer.h>
19 #include <common/hashtable/utils.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/macros.h>
22 #include <lttng/condition/condition.h>
23 #include <lttng/action/action-internal.h>
24 #include <lttng/notification/notification-internal.h>
25 #include <lttng/condition/condition-internal.h>
26 #include <lttng/condition/buffer-usage-internal.h>
27 #include <lttng/condition/session-consumed-size-internal.h>
28 #include <lttng/condition/session-rotation-internal.h>
29 #include <lttng/condition/event-rule-internal.h>
30 #include <lttng/domain-internal.h>
31 #include <lttng/notification/channel-internal.h>
32 #include <lttng/trigger/trigger-internal.h>
33 #include <lttng/event-rule/event-rule-internal.h>
41 #include "condition-internal.h"
42 #include "notification-thread.h"
43 #include "notification-thread-events.h"
44 #include "notification-thread-commands.h"
45 #include "lttng-sessiond.h"
48 #define CLIENT_POLL_MASK_IN (LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP)
49 #define CLIENT_POLL_MASK_IN_OUT (CLIENT_POLL_MASK_IN | LPOLLOUT)
51 enum lttng_object_type
{
52 LTTNG_OBJECT_TYPE_UNKNOWN
,
53 LTTNG_OBJECT_TYPE_NONE
,
54 LTTNG_OBJECT_TYPE_CHANNEL
,
55 LTTNG_OBJECT_TYPE_SESSION
,
58 struct lttng_trigger_list_element
{
59 /* No ownership of the trigger object is assumed. */
60 struct lttng_trigger
*trigger
;
61 struct cds_list_head node
;
64 struct lttng_channel_trigger_list
{
65 struct channel_key channel_key
;
66 /* List of struct lttng_trigger_list_element. */
67 struct cds_list_head list
;
68 /* Node in the channel_triggers_ht */
69 struct cds_lfht_node channel_triggers_ht_node
;
70 /* call_rcu delayed reclaim. */
71 struct rcu_head rcu_node
;
75 * List of triggers applying to a given session.
78 * - lttng_session_trigger_list_create()
79 * - lttng_session_trigger_list_build()
80 * - lttng_session_trigger_list_destroy()
81 * - lttng_session_trigger_list_add()
83 struct lttng_session_trigger_list
{
85 * Not owned by this; points to the session_info structure's
88 const char *session_name
;
89 /* List of struct lttng_trigger_list_element. */
90 struct cds_list_head list
;
91 /* Node in the session_triggers_ht */
92 struct cds_lfht_node session_triggers_ht_node
;
94 * Weak reference to the notification system's session triggers
97 * The session trigger list structure structure is owned by
98 * the session's session_info.
100 * The session_info is kept alive the the channel_infos holding a
101 * reference to it (reference counting). When those channels are
102 * destroyed (at runtime or on teardown), the reference they hold
103 * to the session_info are released. On destruction of session_info,
104 * session_info_destroy() will remove the list of triggers applying
105 * to this session from the notification system's state.
107 * This implies that the session_triggers_ht must be destroyed
108 * after the channels.
110 struct cds_lfht
*session_triggers_ht
;
111 /* Used for delayed RCU reclaim. */
112 struct rcu_head rcu_node
;
115 struct lttng_trigger_ht_element
{
116 struct lttng_trigger
*trigger
;
117 struct cds_lfht_node node
;
118 struct cds_lfht_node node_by_name_uid
;
119 /* call_rcu delayed reclaim. */
120 struct rcu_head rcu_node
;
123 struct lttng_condition_list_element
{
124 struct lttng_condition
*condition
;
125 struct cds_list_head node
;
128 struct channel_state_sample
{
129 struct channel_key key
;
130 struct cds_lfht_node channel_state_ht_node
;
131 uint64_t highest_usage
;
132 uint64_t lowest_usage
;
133 uint64_t channel_total_consumed
;
134 /* call_rcu delayed reclaim. */
135 struct rcu_head rcu_node
;
138 static unsigned long hash_channel_key(struct channel_key
*key
);
139 static int evaluate_buffer_condition(const struct lttng_condition
*condition
,
140 struct lttng_evaluation
**evaluation
,
141 const struct notification_thread_state
*state
,
142 const struct channel_state_sample
*previous_sample
,
143 const struct channel_state_sample
*latest_sample
,
144 uint64_t previous_session_consumed_total
,
145 uint64_t latest_session_consumed_total
,
146 struct channel_info
*channel_info
);
148 int send_evaluation_to_clients(const struct lttng_trigger
*trigger
,
149 const struct lttng_evaluation
*evaluation
,
150 struct notification_client_list
*client_list
,
151 struct notification_thread_state
*state
,
152 uid_t channel_uid
, gid_t channel_gid
);
155 /* session_info API */
157 void session_info_destroy(void *_data
);
159 void session_info_get(struct session_info
*session_info
);
161 void session_info_put(struct session_info
*session_info
);
163 struct session_info
*session_info_create(const char *name
,
164 uid_t uid
, gid_t gid
,
165 struct lttng_session_trigger_list
*trigger_list
,
166 struct cds_lfht
*sessions_ht
);
168 void session_info_add_channel(struct session_info
*session_info
,
169 struct channel_info
*channel_info
);
171 void session_info_remove_channel(struct session_info
*session_info
,
172 struct channel_info
*channel_info
);
174 /* lttng_session_trigger_list API */
176 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
177 const char *session_name
,
178 struct cds_lfht
*session_triggers_ht
);
180 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
181 const struct notification_thread_state
*state
,
182 const char *session_name
);
184 void lttng_session_trigger_list_destroy(
185 struct lttng_session_trigger_list
*list
);
187 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
188 struct lttng_trigger
*trigger
);
191 int client_handle_transmission_status(
192 struct notification_client
*client
,
193 enum client_transmission_status transmission_status
,
194 struct notification_thread_state
*state
);
197 int handle_one_event_notifier_notification(
198 struct notification_thread_state
*state
,
199 int pipe
, enum lttng_domain_type domain
);
202 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
);
205 int match_client_socket(struct cds_lfht_node
*node
, const void *key
)
207 /* This double-cast is intended to supress pointer-to-cast warning. */
208 const int socket
= (int) (intptr_t) key
;
209 const struct notification_client
*client
= caa_container_of(node
,
210 struct notification_client
, client_socket_ht_node
);
212 return client
->socket
== socket
;
216 int match_client_id(struct cds_lfht_node
*node
, const void *key
)
218 /* This double-cast is intended to supress pointer-to-cast warning. */
219 const notification_client_id id
= *((notification_client_id
*) key
);
220 const struct notification_client
*client
= caa_container_of(
221 node
, struct notification_client
, client_id_ht_node
);
223 return client
->id
== id
;
227 int match_channel_trigger_list(struct cds_lfht_node
*node
, const void *key
)
229 struct channel_key
*channel_key
= (struct channel_key
*) key
;
230 struct lttng_channel_trigger_list
*trigger_list
;
232 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
233 channel_triggers_ht_node
);
235 return !!((channel_key
->key
== trigger_list
->channel_key
.key
) &&
236 (channel_key
->domain
== trigger_list
->channel_key
.domain
));
240 int match_session_trigger_list(struct cds_lfht_node
*node
, const void *key
)
242 const char *session_name
= (const char *) key
;
243 struct lttng_session_trigger_list
*trigger_list
;
245 trigger_list
= caa_container_of(node
, struct lttng_session_trigger_list
,
246 session_triggers_ht_node
);
248 return !!(strcmp(trigger_list
->session_name
, session_name
) == 0);
252 int match_channel_state_sample(struct cds_lfht_node
*node
, const void *key
)
254 struct channel_key
*channel_key
= (struct channel_key
*) key
;
255 struct channel_state_sample
*sample
;
257 sample
= caa_container_of(node
, struct channel_state_sample
,
258 channel_state_ht_node
);
260 return !!((channel_key
->key
== sample
->key
.key
) &&
261 (channel_key
->domain
== sample
->key
.domain
));
265 int match_channel_info(struct cds_lfht_node
*node
, const void *key
)
267 struct channel_key
*channel_key
= (struct channel_key
*) key
;
268 struct channel_info
*channel_info
;
270 channel_info
= caa_container_of(node
, struct channel_info
,
273 return !!((channel_key
->key
== channel_info
->key
.key
) &&
274 (channel_key
->domain
== channel_info
->key
.domain
));
278 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
280 struct lttng_trigger
*trigger_key
= (struct lttng_trigger
*) key
;
281 struct lttng_trigger_ht_element
*trigger_ht_element
;
283 trigger_ht_element
= caa_container_of(node
, struct lttng_trigger_ht_element
,
286 return !!lttng_trigger_is_equal(trigger_key
, trigger_ht_element
->trigger
);
290 int match_trigger_token(struct cds_lfht_node
*node
, const void *key
)
292 const uint64_t *_key
= key
;
293 struct notification_trigger_tokens_ht_element
*element
;
295 element
= caa_container_of(node
,
296 struct notification_trigger_tokens_ht_element
, node
);
297 return *_key
== element
->token
;
301 int match_client_list_condition(struct cds_lfht_node
*node
, const void *key
)
303 struct lttng_condition
*condition_key
= (struct lttng_condition
*) key
;
304 struct notification_client_list
*client_list
;
305 const struct lttng_condition
*condition
;
307 assert(condition_key
);
309 client_list
= caa_container_of(node
, struct notification_client_list
,
310 notification_trigger_clients_ht_node
);
311 condition
= lttng_trigger_get_const_condition(client_list
->trigger
);
313 return !!lttng_condition_is_equal(condition_key
, condition
);
317 int match_session(struct cds_lfht_node
*node
, const void *key
)
319 const char *name
= key
;
320 struct session_info
*session_info
= caa_container_of(
321 node
, struct session_info
, sessions_ht_node
);
323 return !strcmp(session_info
->name
, name
);
327 const char *notification_command_type_str(
328 enum notification_thread_command_type type
)
331 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
332 return "REGISTER_TRIGGER";
333 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
334 return "UNREGISTER_TRIGGER";
335 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
336 return "ADD_CHANNEL";
337 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
338 return "REMOVE_CHANNEL";
339 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
340 return "SESSION_ROTATION_ONGOING";
341 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
342 return "SESSION_ROTATION_COMPLETED";
343 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
344 return "ADD_TRACER_EVENT_SOURCE";
345 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
346 return "REMOVE_TRACER_EVENT_SOURCE";
347 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
348 return "LIST_TRIGGERS";
349 case NOTIFICATION_COMMAND_TYPE_QUIT
:
351 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
352 return "CLIENT_COMMUNICATION_UPDATE";
359 * Match trigger based on name and credentials only.
360 * Name duplication is NOT allowed for the same uid.
363 int match_trigger_by_name_uid(struct cds_lfht_node
*node
,
368 const char *key_name
;
369 enum lttng_trigger_status status
;
370 const struct lttng_credentials
*key_creds
;
371 const struct lttng_credentials
*node_creds
;
372 const struct lttng_trigger
*trigger_key
=
373 (const struct lttng_trigger
*) key
;
374 const struct lttng_trigger_ht_element
*trigger_ht_element
=
375 caa_container_of(node
,
376 struct lttng_trigger_ht_element
,
379 status
= lttng_trigger_get_name(trigger_ht_element
->trigger
, &name
);
380 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
382 status
= lttng_trigger_get_name(trigger_key
, &key_name
);
383 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
385 /* Compare the names. */
386 if (strcmp(name
, key_name
) != 0) {
390 /* Compare the owners' UIDs. */
391 key_creds
= lttng_trigger_get_credentials(trigger_key
);
392 node_creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
394 match
= lttng_credentials_is_equal_uid(key_creds
, node_creds
);
401 * Hash trigger based on name and credentials only.
404 unsigned long hash_trigger_by_name_uid(const struct lttng_trigger
*trigger
)
406 unsigned long hash
= 0;
407 const struct lttng_credentials
*trigger_creds
;
408 const char *trigger_name
;
409 enum lttng_trigger_status status
;
411 status
= lttng_trigger_get_name(trigger
, &trigger_name
);
412 if (status
== LTTNG_TRIGGER_STATUS_OK
) {
413 hash
= hash_key_str(trigger_name
, lttng_ht_seed
);
416 trigger_creds
= lttng_trigger_get_credentials(trigger
);
417 hash
^= hash_key_ulong((void *) (unsigned long) LTTNG_OPTIONAL_GET(trigger_creds
->uid
),
424 unsigned long hash_channel_key(struct channel_key
*key
)
426 unsigned long key_hash
= hash_key_u64(&key
->key
, lttng_ht_seed
);
427 unsigned long domain_hash
= hash_key_ulong(
428 (void *) (unsigned long) key
->domain
, lttng_ht_seed
);
430 return key_hash
^ domain_hash
;
434 unsigned long hash_client_socket(int socket
)
436 return hash_key_ulong((void *) (unsigned long) socket
, lttng_ht_seed
);
440 unsigned long hash_client_id(notification_client_id id
)
442 return hash_key_u64(&id
, lttng_ht_seed
);
446 * Get the type of object to which a given condition applies. Bindings let
447 * the notification system evaluate a trigger's condition when a given
448 * object's state is updated.
450 * For instance, a condition bound to a channel will be evaluated everytime
451 * the channel's state is changed by a channel monitoring sample.
454 enum lttng_object_type
get_condition_binding_object(
455 const struct lttng_condition
*condition
)
457 switch (lttng_condition_get_type(condition
)) {
458 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
459 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
460 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
461 return LTTNG_OBJECT_TYPE_CHANNEL
;
462 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
463 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
464 return LTTNG_OBJECT_TYPE_SESSION
;
465 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
466 return LTTNG_OBJECT_TYPE_NONE
;
468 return LTTNG_OBJECT_TYPE_UNKNOWN
;
473 void free_channel_info_rcu(struct rcu_head
*node
)
475 free(caa_container_of(node
, struct channel_info
, rcu_node
));
479 void channel_info_destroy(struct channel_info
*channel_info
)
485 if (channel_info
->session_info
) {
486 session_info_remove_channel(channel_info
->session_info
,
488 session_info_put(channel_info
->session_info
);
490 if (channel_info
->name
) {
491 free(channel_info
->name
);
493 call_rcu(&channel_info
->rcu_node
, free_channel_info_rcu
);
497 void free_session_info_rcu(struct rcu_head
*node
)
499 free(caa_container_of(node
, struct session_info
, rcu_node
));
502 /* Don't call directly, use the ref-counting mechanism. */
504 void session_info_destroy(void *_data
)
506 struct session_info
*session_info
= _data
;
509 assert(session_info
);
510 if (session_info
->channel_infos_ht
) {
511 ret
= cds_lfht_destroy(session_info
->channel_infos_ht
, NULL
);
513 ERR("[notification-thread] Failed to destroy channel information hash table");
516 lttng_session_trigger_list_destroy(session_info
->trigger_list
);
519 cds_lfht_del(session_info
->sessions_ht
,
520 &session_info
->sessions_ht_node
);
522 free(session_info
->name
);
523 call_rcu(&session_info
->rcu_node
, free_session_info_rcu
);
527 void session_info_get(struct session_info
*session_info
)
532 lttng_ref_get(&session_info
->ref
);
536 void session_info_put(struct session_info
*session_info
)
541 lttng_ref_put(&session_info
->ref
);
545 struct session_info
*session_info_create(const char *name
, uid_t uid
, gid_t gid
,
546 struct lttng_session_trigger_list
*trigger_list
,
547 struct cds_lfht
*sessions_ht
)
549 struct session_info
*session_info
;
553 session_info
= zmalloc(sizeof(*session_info
));
557 lttng_ref_init(&session_info
->ref
, session_info_destroy
);
559 session_info
->channel_infos_ht
= cds_lfht_new(DEFAULT_HT_SIZE
,
560 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
561 if (!session_info
->channel_infos_ht
) {
565 cds_lfht_node_init(&session_info
->sessions_ht_node
);
566 session_info
->name
= strdup(name
);
567 if (!session_info
->name
) {
570 session_info
->uid
= uid
;
571 session_info
->gid
= gid
;
572 session_info
->trigger_list
= trigger_list
;
573 session_info
->sessions_ht
= sessions_ht
;
577 session_info_put(session_info
);
582 void session_info_add_channel(struct session_info
*session_info
,
583 struct channel_info
*channel_info
)
586 cds_lfht_add(session_info
->channel_infos_ht
,
587 hash_channel_key(&channel_info
->key
),
588 &channel_info
->session_info_channels_ht_node
);
593 void session_info_remove_channel(struct session_info
*session_info
,
594 struct channel_info
*channel_info
)
597 cds_lfht_del(session_info
->channel_infos_ht
,
598 &channel_info
->session_info_channels_ht_node
);
603 struct channel_info
*channel_info_create(const char *channel_name
,
604 struct channel_key
*channel_key
, uint64_t channel_capacity
,
605 struct session_info
*session_info
)
607 struct channel_info
*channel_info
= zmalloc(sizeof(*channel_info
));
613 cds_lfht_node_init(&channel_info
->channels_ht_node
);
614 cds_lfht_node_init(&channel_info
->session_info_channels_ht_node
);
615 memcpy(&channel_info
->key
, channel_key
, sizeof(*channel_key
));
616 channel_info
->capacity
= channel_capacity
;
618 channel_info
->name
= strdup(channel_name
);
619 if (!channel_info
->name
) {
624 * Set the references between session and channel infos:
625 * - channel_info holds a strong reference to session_info
626 * - session_info holds a weak reference to channel_info
628 session_info_get(session_info
);
629 session_info_add_channel(session_info
, channel_info
);
630 channel_info
->session_info
= session_info
;
634 channel_info_destroy(channel_info
);
639 bool notification_client_list_get(struct notification_client_list
*list
)
641 return urcu_ref_get_unless_zero(&list
->ref
);
645 void free_notification_client_list_rcu(struct rcu_head
*node
)
647 free(caa_container_of(node
, struct notification_client_list
,
652 void notification_client_list_release(struct urcu_ref
*list_ref
)
654 struct notification_client_list
*list
=
655 container_of(list_ref
, typeof(*list
), ref
);
656 struct notification_client_list_element
*client_list_element
, *tmp
;
658 if (list
->notification_trigger_clients_ht
) {
660 cds_lfht_del(list
->notification_trigger_clients_ht
,
661 &list
->notification_trigger_clients_ht_node
);
663 list
->notification_trigger_clients_ht
= NULL
;
665 cds_list_for_each_entry_safe(client_list_element
, tmp
,
667 free(client_list_element
);
669 pthread_mutex_destroy(&list
->lock
);
670 call_rcu(&list
->rcu_node
, free_notification_client_list_rcu
);
674 struct notification_client_list
*notification_client_list_create(
675 const struct lttng_trigger
*trigger
)
677 struct notification_client_list
*client_list
=
678 zmalloc(sizeof(*client_list
));
683 pthread_mutex_init(&client_list
->lock
, NULL
);
684 urcu_ref_init(&client_list
->ref
);
685 cds_lfht_node_init(&client_list
->notification_trigger_clients_ht_node
);
686 CDS_INIT_LIST_HEAD(&client_list
->list
);
687 client_list
->trigger
= trigger
;
693 void publish_notification_client_list(
694 struct notification_thread_state
*state
,
695 struct notification_client_list
*list
)
697 const struct lttng_condition
*condition
=
698 lttng_trigger_get_const_condition(list
->trigger
);
700 assert(!list
->notification_trigger_clients_ht
);
701 notification_client_list_get(list
);
703 list
->notification_trigger_clients_ht
=
704 state
->notification_trigger_clients_ht
;
707 cds_lfht_add(state
->notification_trigger_clients_ht
,
708 lttng_condition_hash(condition
),
709 &list
->notification_trigger_clients_ht_node
);
714 void notification_client_list_put(struct notification_client_list
*list
)
719 return urcu_ref_put(&list
->ref
, notification_client_list_release
);
722 /* Provides a reference to the returned list. */
724 struct notification_client_list
*get_client_list_from_condition(
725 struct notification_thread_state
*state
,
726 const struct lttng_condition
*condition
)
728 struct cds_lfht_node
*node
;
729 struct cds_lfht_iter iter
;
730 struct notification_client_list
*list
= NULL
;
733 cds_lfht_lookup(state
->notification_trigger_clients_ht
,
734 lttng_condition_hash(condition
),
735 match_client_list_condition
,
738 node
= cds_lfht_iter_get_node(&iter
);
740 list
= container_of(node
, struct notification_client_list
,
741 notification_trigger_clients_ht_node
);
742 list
= notification_client_list_get(list
) ? list
: NULL
;
750 int evaluate_channel_condition_for_client(
751 const struct lttng_condition
*condition
,
752 struct notification_thread_state
*state
,
753 struct lttng_evaluation
**evaluation
,
754 uid_t
*session_uid
, gid_t
*session_gid
)
757 struct cds_lfht_iter iter
;
758 struct cds_lfht_node
*node
;
759 struct channel_info
*channel_info
= NULL
;
760 struct channel_key
*channel_key
= NULL
;
761 struct channel_state_sample
*last_sample
= NULL
;
762 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
766 /* Find the channel associated with the condition. */
767 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
,
768 channel_trigger_list
, channel_triggers_ht_node
) {
769 struct lttng_trigger_list_element
*element
;
771 cds_list_for_each_entry(element
, &channel_trigger_list
->list
, node
) {
772 const struct lttng_condition
*current_condition
=
773 lttng_trigger_get_const_condition(
776 assert(current_condition
);
777 if (!lttng_condition_is_equal(condition
,
778 current_condition
)) {
782 /* Found the trigger, save the channel key. */
783 channel_key
= &channel_trigger_list
->channel_key
;
787 /* The channel key was found stop iteration. */
793 /* No channel found; normal exit. */
794 DBG("[notification-thread] No known channel associated with newly subscribed-to condition");
799 /* Fetch channel info for the matching channel. */
800 cds_lfht_lookup(state
->channels_ht
,
801 hash_channel_key(channel_key
),
805 node
= cds_lfht_iter_get_node(&iter
);
807 channel_info
= caa_container_of(node
, struct channel_info
,
810 /* Retrieve the channel's last sample, if it exists. */
811 cds_lfht_lookup(state
->channel_state_ht
,
812 hash_channel_key(channel_key
),
813 match_channel_state_sample
,
816 node
= cds_lfht_iter_get_node(&iter
);
818 last_sample
= caa_container_of(node
,
819 struct channel_state_sample
,
820 channel_state_ht_node
);
822 /* Nothing to evaluate, no sample was ever taken. Normal exit */
823 DBG("[notification-thread] No channel sample associated with newly subscribed-to condition");
828 ret
= evaluate_buffer_condition(condition
, evaluation
, state
,
830 0, channel_info
->session_info
->consumed_data_size
,
833 WARN("[notification-thread] Fatal error occurred while evaluating a newly subscribed-to condition");
837 *session_uid
= channel_info
->session_info
->uid
;
838 *session_gid
= channel_info
->session_info
->gid
;
845 const char *get_condition_session_name(const struct lttng_condition
*condition
)
847 const char *session_name
= NULL
;
848 enum lttng_condition_status status
;
850 switch (lttng_condition_get_type(condition
)) {
851 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
852 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
853 status
= lttng_condition_buffer_usage_get_session_name(
854 condition
, &session_name
);
856 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
857 status
= lttng_condition_session_consumed_size_get_session_name(
858 condition
, &session_name
);
860 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
861 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
862 status
= lttng_condition_session_rotation_get_session_name(
863 condition
, &session_name
);
868 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
869 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
877 int evaluate_session_condition_for_client(
878 const struct lttng_condition
*condition
,
879 struct notification_thread_state
*state
,
880 struct lttng_evaluation
**evaluation
,
881 uid_t
*session_uid
, gid_t
*session_gid
)
884 struct cds_lfht_iter iter
;
885 struct cds_lfht_node
*node
;
886 const char *session_name
;
887 struct session_info
*session_info
= NULL
;
890 session_name
= get_condition_session_name(condition
);
892 /* Find the session associated with the trigger. */
893 cds_lfht_lookup(state
->sessions_ht
,
894 hash_key_str(session_name
, lttng_ht_seed
),
898 node
= cds_lfht_iter_get_node(&iter
);
900 DBG("[notification-thread] No known session matching name \"%s\"",
906 session_info
= caa_container_of(node
, struct session_info
,
908 session_info_get(session_info
);
911 * Evaluation is performed in-line here since only one type of
912 * session-bound condition is handled for the moment.
914 switch (lttng_condition_get_type(condition
)) {
915 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
916 if (!session_info
->rotation
.ongoing
) {
918 goto end_session_put
;
921 *evaluation
= lttng_evaluation_session_rotation_ongoing_create(
922 session_info
->rotation
.id
);
925 ERR("[notification-thread] Failed to create session rotation ongoing evaluation for session \"%s\"",
928 goto end_session_put
;
934 goto end_session_put
;
937 *session_uid
= session_info
->uid
;
938 *session_gid
= session_info
->gid
;
941 session_info_put(session_info
);
948 int evaluate_condition_for_client(const struct lttng_trigger
*trigger
,
949 const struct lttng_condition
*condition
,
950 struct notification_client
*client
,
951 struct notification_thread_state
*state
)
954 struct lttng_evaluation
*evaluation
= NULL
;
955 struct notification_client_list client_list
= {
956 .lock
= PTHREAD_MUTEX_INITIALIZER
,
958 struct notification_client_list_element client_list_element
= { 0 };
959 uid_t object_uid
= 0;
960 gid_t object_gid
= 0;
967 switch (get_condition_binding_object(condition
)) {
968 case LTTNG_OBJECT_TYPE_SESSION
:
969 ret
= evaluate_session_condition_for_client(condition
, state
,
970 &evaluation
, &object_uid
, &object_gid
);
972 case LTTNG_OBJECT_TYPE_CHANNEL
:
973 ret
= evaluate_channel_condition_for_client(condition
, state
,
974 &evaluation
, &object_uid
, &object_gid
);
976 case LTTNG_OBJECT_TYPE_NONE
:
977 DBG("[notification-thread] Newly subscribed-to condition not bound to object, nothing to evaluate");
980 case LTTNG_OBJECT_TYPE_UNKNOWN
:
990 /* Evaluation yielded nothing. Normal exit. */
991 DBG("[notification-thread] Newly subscribed-to condition evaluated to false, nothing to report to client");
997 * Create a temporary client list with the client currently
1000 cds_lfht_node_init(&client_list
.notification_trigger_clients_ht_node
);
1001 CDS_INIT_LIST_HEAD(&client_list
.list
);
1002 client_list
.trigger
= trigger
;
1004 CDS_INIT_LIST_HEAD(&client_list_element
.node
);
1005 client_list_element
.client
= client
;
1006 cds_list_add(&client_list_element
.node
, &client_list
.list
);
1008 /* Send evaluation result to the newly-subscribed client. */
1009 DBG("[notification-thread] Newly subscribed-to condition evaluated to true, notifying client");
1010 ret
= send_evaluation_to_clients(trigger
, evaluation
, &client_list
,
1011 state
, object_uid
, object_gid
);
1018 int notification_thread_client_subscribe(struct notification_client
*client
,
1019 struct lttng_condition
*condition
,
1020 struct notification_thread_state
*state
,
1021 enum lttng_notification_channel_status
*_status
)
1024 struct notification_client_list
*client_list
= NULL
;
1025 struct lttng_condition_list_element
*condition_list_element
= NULL
;
1026 struct notification_client_list_element
*client_list_element
= NULL
;
1027 enum lttng_notification_channel_status status
=
1028 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1031 * Ensure that the client has not already subscribed to this condition
1034 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
, node
) {
1035 if (lttng_condition_is_equal(condition_list_element
->condition
,
1037 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_ALREADY_SUBSCRIBED
;
1042 condition_list_element
= zmalloc(sizeof(*condition_list_element
));
1043 if (!condition_list_element
) {
1047 client_list_element
= zmalloc(sizeof(*client_list_element
));
1048 if (!client_list_element
) {
1054 * Add the newly-subscribed condition to the client's subscription list.
1056 CDS_INIT_LIST_HEAD(&condition_list_element
->node
);
1057 condition_list_element
->condition
= condition
;
1058 cds_list_add(&condition_list_element
->node
, &client
->condition_list
);
1060 client_list
= get_client_list_from_condition(state
, condition
);
1063 * No notification-emiting trigger registered with this
1064 * condition. We don't evaluate the condition right away
1065 * since this trigger is not registered yet.
1067 free(client_list_element
);
1072 * The condition to which the client just subscribed is evaluated
1073 * at this point so that conditions that are already TRUE result
1074 * in a notification being sent out.
1076 * The client_list's trigger is used without locking the list itself.
1077 * This is correct since the list doesn't own the trigger and the
1078 * object is immutable.
1080 if (evaluate_condition_for_client(client_list
->trigger
, condition
,
1082 WARN("[notification-thread] Evaluation of a condition on client subscription failed, aborting.");
1084 free(client_list_element
);
1089 * Add the client to the list of clients interested in a given trigger
1090 * if a "notification" trigger with a corresponding condition was
1093 client_list_element
->client
= client
;
1094 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
1096 pthread_mutex_lock(&client_list
->lock
);
1097 cds_list_add(&client_list_element
->node
, &client_list
->list
);
1098 pthread_mutex_unlock(&client_list
->lock
);
1104 notification_client_list_put(client_list
);
1108 free(condition_list_element
);
1109 free(client_list_element
);
1114 int notification_thread_client_unsubscribe(
1115 struct notification_client
*client
,
1116 struct lttng_condition
*condition
,
1117 struct notification_thread_state
*state
,
1118 enum lttng_notification_channel_status
*_status
)
1120 struct notification_client_list
*client_list
;
1121 struct lttng_condition_list_element
*condition_list_element
,
1123 struct notification_client_list_element
*client_list_element
,
1125 bool condition_found
= false;
1126 enum lttng_notification_channel_status status
=
1127 LTTNG_NOTIFICATION_CHANNEL_STATUS_OK
;
1129 /* Remove the condition from the client's condition list. */
1130 cds_list_for_each_entry_safe(condition_list_element
, condition_tmp
,
1131 &client
->condition_list
, node
) {
1132 if (!lttng_condition_is_equal(condition_list_element
->condition
,
1137 cds_list_del(&condition_list_element
->node
);
1139 * The caller may be iterating on the client's conditions to
1140 * tear down a client's connection. In this case, the condition
1141 * will be destroyed at the end.
1143 if (condition
!= condition_list_element
->condition
) {
1144 lttng_condition_destroy(
1145 condition_list_element
->condition
);
1147 free(condition_list_element
);
1148 condition_found
= true;
1152 if (!condition_found
) {
1153 status
= LTTNG_NOTIFICATION_CHANNEL_STATUS_UNKNOWN_CONDITION
;
1158 * Remove the client from the list of clients interested the trigger
1159 * matching the condition.
1161 client_list
= get_client_list_from_condition(state
, condition
);
1166 pthread_mutex_lock(&client_list
->lock
);
1167 cds_list_for_each_entry_safe(client_list_element
, client_tmp
,
1168 &client_list
->list
, node
) {
1169 if (client_list_element
->client
->id
!= client
->id
) {
1172 cds_list_del(&client_list_element
->node
);
1173 free(client_list_element
);
1176 pthread_mutex_unlock(&client_list
->lock
);
1177 notification_client_list_put(client_list
);
1180 lttng_condition_destroy(condition
);
1188 void free_notification_client_rcu(struct rcu_head
*node
)
1190 free(caa_container_of(node
, struct notification_client
, rcu_node
));
1194 void notification_client_destroy(struct notification_client
*client
,
1195 struct notification_thread_state
*state
)
1202 * The client object is not reachable by other threads, no need to lock
1205 if (client
->socket
>= 0) {
1206 (void) lttcomm_close_unix_sock(client
->socket
);
1207 client
->socket
= -1;
1209 client
->communication
.active
= false;
1210 lttng_payload_reset(&client
->communication
.inbound
.payload
);
1211 lttng_payload_reset(&client
->communication
.outbound
.payload
);
1212 pthread_mutex_destroy(&client
->lock
);
1213 call_rcu(&client
->rcu_node
, free_notification_client_rcu
);
1217 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1221 struct notification_client
*get_client_from_socket(int socket
,
1222 struct notification_thread_state
*state
)
1224 struct cds_lfht_iter iter
;
1225 struct cds_lfht_node
*node
;
1226 struct notification_client
*client
= NULL
;
1228 cds_lfht_lookup(state
->client_socket_ht
,
1229 hash_client_socket(socket
),
1230 match_client_socket
,
1231 (void *) (unsigned long) socket
,
1233 node
= cds_lfht_iter_get_node(&iter
);
1238 client
= caa_container_of(node
, struct notification_client
,
1239 client_socket_ht_node
);
1245 * Call with rcu_read_lock held (and hold for the lifetime of the returned
1249 struct notification_client
*get_client_from_id(notification_client_id id
,
1250 struct notification_thread_state
*state
)
1252 struct cds_lfht_iter iter
;
1253 struct cds_lfht_node
*node
;
1254 struct notification_client
*client
= NULL
;
1256 cds_lfht_lookup(state
->client_id_ht
,
1261 node
= cds_lfht_iter_get_node(&iter
);
1266 client
= caa_container_of(node
, struct notification_client
,
1273 bool buffer_usage_condition_applies_to_channel(
1274 const struct lttng_condition
*condition
,
1275 const struct channel_info
*channel_info
)
1277 enum lttng_condition_status status
;
1278 enum lttng_domain_type condition_domain
;
1279 const char *condition_session_name
= NULL
;
1280 const char *condition_channel_name
= NULL
;
1282 status
= lttng_condition_buffer_usage_get_domain_type(condition
,
1284 assert(status
== LTTNG_CONDITION_STATUS_OK
);
1285 if (channel_info
->key
.domain
!= condition_domain
) {
1289 status
= lttng_condition_buffer_usage_get_session_name(
1290 condition
, &condition_session_name
);
1291 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1293 status
= lttng_condition_buffer_usage_get_channel_name(
1294 condition
, &condition_channel_name
);
1295 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_channel_name
);
1297 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1300 if (strcmp(channel_info
->name
, condition_channel_name
)) {
1310 bool session_consumed_size_condition_applies_to_channel(
1311 const struct lttng_condition
*condition
,
1312 const struct channel_info
*channel_info
)
1314 enum lttng_condition_status status
;
1315 const char *condition_session_name
= NULL
;
1317 status
= lttng_condition_session_consumed_size_get_session_name(
1318 condition
, &condition_session_name
);
1319 assert((status
== LTTNG_CONDITION_STATUS_OK
) && condition_session_name
);
1321 if (strcmp(channel_info
->session_info
->name
, condition_session_name
)) {
1331 bool trigger_applies_to_channel(const struct lttng_trigger
*trigger
,
1332 const struct channel_info
*channel_info
)
1334 const struct lttng_condition
*condition
;
1335 bool trigger_applies
;
1337 condition
= lttng_trigger_get_const_condition(trigger
);
1342 switch (lttng_condition_get_type(condition
)) {
1343 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
1344 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
1345 trigger_applies
= buffer_usage_condition_applies_to_channel(
1346 condition
, channel_info
);
1348 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
1349 trigger_applies
= session_consumed_size_condition_applies_to_channel(
1350 condition
, channel_info
);
1356 return trigger_applies
;
1362 bool trigger_applies_to_client(struct lttng_trigger
*trigger
,
1363 struct notification_client
*client
)
1365 bool applies
= false;
1366 struct lttng_condition_list_element
*condition_list_element
;
1368 cds_list_for_each_entry(condition_list_element
, &client
->condition_list
,
1370 applies
= lttng_condition_is_equal(
1371 condition_list_element
->condition
,
1372 lttng_trigger_get_condition(trigger
));
1380 /* Must be called with RCU read lock held. */
1382 struct lttng_session_trigger_list
*get_session_trigger_list(
1383 struct notification_thread_state
*state
,
1384 const char *session_name
)
1386 struct lttng_session_trigger_list
*list
= NULL
;
1387 struct cds_lfht_node
*node
;
1388 struct cds_lfht_iter iter
;
1390 cds_lfht_lookup(state
->session_triggers_ht
,
1391 hash_key_str(session_name
, lttng_ht_seed
),
1392 match_session_trigger_list
,
1395 node
= cds_lfht_iter_get_node(&iter
);
1398 * Not an error, the list of triggers applying to that session
1399 * will be initialized when the session is created.
1401 DBG("[notification-thread] No trigger list found for session \"%s\" as it is not yet known to the notification system",
1406 list
= caa_container_of(node
,
1407 struct lttng_session_trigger_list
,
1408 session_triggers_ht_node
);
1414 * Allocate an empty lttng_session_trigger_list for the session named
1417 * No ownership of 'session_name' is assumed by the session trigger list.
1418 * It is the caller's responsability to ensure the session name is alive
1419 * for as long as this list is.
1422 struct lttng_session_trigger_list
*lttng_session_trigger_list_create(
1423 const char *session_name
,
1424 struct cds_lfht
*session_triggers_ht
)
1426 struct lttng_session_trigger_list
*list
;
1428 list
= zmalloc(sizeof(*list
));
1432 list
->session_name
= session_name
;
1433 CDS_INIT_LIST_HEAD(&list
->list
);
1434 cds_lfht_node_init(&list
->session_triggers_ht_node
);
1435 list
->session_triggers_ht
= session_triggers_ht
;
1438 /* Publish the list through the session_triggers_ht. */
1439 cds_lfht_add(session_triggers_ht
,
1440 hash_key_str(session_name
, lttng_ht_seed
),
1441 &list
->session_triggers_ht_node
);
1448 void free_session_trigger_list_rcu(struct rcu_head
*node
)
1450 free(caa_container_of(node
, struct lttng_session_trigger_list
,
1455 void lttng_session_trigger_list_destroy(struct lttng_session_trigger_list
*list
)
1457 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1459 /* Empty the list element by element, and then free the list itself. */
1460 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1461 &list
->list
, node
) {
1462 cds_list_del(&trigger_list_element
->node
);
1463 free(trigger_list_element
);
1466 /* Unpublish the list from the session_triggers_ht. */
1467 cds_lfht_del(list
->session_triggers_ht
,
1468 &list
->session_triggers_ht_node
);
1470 call_rcu(&list
->rcu_node
, free_session_trigger_list_rcu
);
1474 int lttng_session_trigger_list_add(struct lttng_session_trigger_list
*list
,
1475 struct lttng_trigger
*trigger
)
1478 struct lttng_trigger_list_element
*new_element
=
1479 zmalloc(sizeof(*new_element
));
1485 CDS_INIT_LIST_HEAD(&new_element
->node
);
1486 new_element
->trigger
= trigger
;
1487 cds_list_add(&new_element
->node
, &list
->list
);
1493 bool trigger_applies_to_session(const struct lttng_trigger
*trigger
,
1494 const char *session_name
)
1496 bool applies
= false;
1497 const struct lttng_condition
*condition
;
1499 condition
= lttng_trigger_get_const_condition(trigger
);
1500 switch (lttng_condition_get_type(condition
)) {
1501 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
1502 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
1504 enum lttng_condition_status condition_status
;
1505 const char *condition_session_name
;
1507 condition_status
= lttng_condition_session_rotation_get_session_name(
1508 condition
, &condition_session_name
);
1509 if (condition_status
!= LTTNG_CONDITION_STATUS_OK
) {
1510 ERR("[notification-thread] Failed to retrieve session rotation condition's session name");
1514 assert(condition_session_name
);
1515 applies
= !strcmp(condition_session_name
, session_name
);
1526 * Allocate and initialize an lttng_session_trigger_list which contains
1527 * all triggers that apply to the session named 'session_name'.
1529 * No ownership of 'session_name' is assumed by the session trigger list.
1530 * It is the caller's responsability to ensure the session name is alive
1531 * for as long as this list is.
1534 struct lttng_session_trigger_list
*lttng_session_trigger_list_build(
1535 const struct notification_thread_state
*state
,
1536 const char *session_name
)
1538 int trigger_count
= 0;
1539 struct lttng_session_trigger_list
*session_trigger_list
= NULL
;
1540 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1541 struct cds_lfht_iter iter
;
1543 session_trigger_list
= lttng_session_trigger_list_create(session_name
,
1544 state
->session_triggers_ht
);
1546 /* Add all triggers applying to the session named 'session_name'. */
1547 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1551 if (!trigger_applies_to_session(trigger_ht_element
->trigger
,
1556 ret
= lttng_session_trigger_list_add(session_trigger_list
,
1557 trigger_ht_element
->trigger
);
1565 DBG("[notification-thread] Found %i triggers that apply to newly created session",
1567 return session_trigger_list
;
1569 lttng_session_trigger_list_destroy(session_trigger_list
);
1574 struct session_info
*find_or_create_session_info(
1575 struct notification_thread_state
*state
,
1576 const char *name
, uid_t uid
, gid_t gid
)
1578 struct session_info
*session
= NULL
;
1579 struct cds_lfht_node
*node
;
1580 struct cds_lfht_iter iter
;
1581 struct lttng_session_trigger_list
*trigger_list
;
1584 cds_lfht_lookup(state
->sessions_ht
,
1585 hash_key_str(name
, lttng_ht_seed
),
1589 node
= cds_lfht_iter_get_node(&iter
);
1591 DBG("[notification-thread] Found session info of session \"%s\" (uid = %i, gid = %i)",
1593 session
= caa_container_of(node
, struct session_info
,
1595 assert(session
->uid
== uid
);
1596 assert(session
->gid
== gid
);
1597 session_info_get(session
);
1601 trigger_list
= lttng_session_trigger_list_build(state
, name
);
1602 if (!trigger_list
) {
1606 session
= session_info_create(name
, uid
, gid
, trigger_list
,
1607 state
->sessions_ht
);
1609 ERR("[notification-thread] Failed to allocation session info for session \"%s\" (uid = %i, gid = %i)",
1611 lttng_session_trigger_list_destroy(trigger_list
);
1614 trigger_list
= NULL
;
1616 cds_lfht_add(state
->sessions_ht
, hash_key_str(name
, lttng_ht_seed
),
1617 &session
->sessions_ht_node
);
1623 session_info_put(session
);
1628 int handle_notification_thread_command_add_channel(
1629 struct notification_thread_state
*state
,
1630 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1631 const char *channel_name
, enum lttng_domain_type channel_domain
,
1632 uint64_t channel_key_int
, uint64_t channel_capacity
,
1633 enum lttng_error_code
*cmd_result
)
1635 struct cds_list_head trigger_list
;
1636 struct channel_info
*new_channel_info
= NULL
;
1637 struct channel_key channel_key
= {
1638 .key
= channel_key_int
,
1639 .domain
= channel_domain
,
1641 struct lttng_channel_trigger_list
*channel_trigger_list
= NULL
;
1642 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
1643 int trigger_count
= 0;
1644 struct cds_lfht_iter iter
;
1645 struct session_info
*session_info
= NULL
;
1647 DBG("[notification-thread] Adding channel %s from session %s, channel key = %" PRIu64
" in %s domain",
1648 channel_name
, session_name
, channel_key_int
,
1649 lttng_domain_type_str(channel_domain
));
1651 CDS_INIT_LIST_HEAD(&trigger_list
);
1653 session_info
= find_or_create_session_info(state
, session_name
,
1654 session_uid
, session_gid
);
1655 if (!session_info
) {
1656 /* Allocation error or an internal error occurred. */
1660 new_channel_info
= channel_info_create(channel_name
, &channel_key
,
1661 channel_capacity
, session_info
);
1662 if (!new_channel_info
) {
1667 /* Build a list of all triggers applying to the new channel. */
1668 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
1670 struct lttng_trigger_list_element
*new_element
;
1672 if (!trigger_applies_to_channel(trigger_ht_element
->trigger
,
1673 new_channel_info
)) {
1677 new_element
= zmalloc(sizeof(*new_element
));
1682 CDS_INIT_LIST_HEAD(&new_element
->node
);
1683 new_element
->trigger
= trigger_ht_element
->trigger
;
1684 cds_list_add(&new_element
->node
, &trigger_list
);
1689 DBG("[notification-thread] Found %i triggers that apply to newly added channel",
1691 channel_trigger_list
= zmalloc(sizeof(*channel_trigger_list
));
1692 if (!channel_trigger_list
) {
1695 channel_trigger_list
->channel_key
= new_channel_info
->key
;
1696 CDS_INIT_LIST_HEAD(&channel_trigger_list
->list
);
1697 cds_lfht_node_init(&channel_trigger_list
->channel_triggers_ht_node
);
1698 cds_list_splice(&trigger_list
, &channel_trigger_list
->list
);
1701 /* Add channel to the channel_ht which owns the channel_infos. */
1702 cds_lfht_add(state
->channels_ht
,
1703 hash_channel_key(&new_channel_info
->key
),
1704 &new_channel_info
->channels_ht_node
);
1706 * Add the list of triggers associated with this channel to the
1707 * channel_triggers_ht.
1709 cds_lfht_add(state
->channel_triggers_ht
,
1710 hash_channel_key(&new_channel_info
->key
),
1711 &channel_trigger_list
->channel_triggers_ht_node
);
1713 session_info_put(session_info
);
1714 *cmd_result
= LTTNG_OK
;
1717 channel_info_destroy(new_channel_info
);
1718 session_info_put(session_info
);
1723 void free_channel_trigger_list_rcu(struct rcu_head
*node
)
1725 free(caa_container_of(node
, struct lttng_channel_trigger_list
,
1730 void free_channel_state_sample_rcu(struct rcu_head
*node
)
1732 free(caa_container_of(node
, struct channel_state_sample
,
1737 int handle_notification_thread_command_remove_channel(
1738 struct notification_thread_state
*state
,
1739 uint64_t channel_key
, enum lttng_domain_type domain
,
1740 enum lttng_error_code
*cmd_result
)
1742 struct cds_lfht_node
*node
;
1743 struct cds_lfht_iter iter
;
1744 struct lttng_channel_trigger_list
*trigger_list
;
1745 struct lttng_trigger_list_element
*trigger_list_element
, *tmp
;
1746 struct channel_key key
= { .key
= channel_key
, .domain
= domain
};
1747 struct channel_info
*channel_info
;
1749 DBG("[notification-thread] Removing channel key = %" PRIu64
" in %s domain",
1750 channel_key
, lttng_domain_type_str(domain
));
1754 cds_lfht_lookup(state
->channel_triggers_ht
,
1755 hash_channel_key(&key
),
1756 match_channel_trigger_list
,
1759 node
= cds_lfht_iter_get_node(&iter
);
1761 * There is a severe internal error if we are being asked to remove a
1762 * channel that doesn't exist.
1765 ERR("[notification-thread] Channel being removed is unknown to the notification thread");
1769 /* Free the list of triggers associated with this channel. */
1770 trigger_list
= caa_container_of(node
, struct lttng_channel_trigger_list
,
1771 channel_triggers_ht_node
);
1772 cds_list_for_each_entry_safe(trigger_list_element
, tmp
,
1773 &trigger_list
->list
, node
) {
1774 cds_list_del(&trigger_list_element
->node
);
1775 free(trigger_list_element
);
1777 cds_lfht_del(state
->channel_triggers_ht
, node
);
1778 call_rcu(&trigger_list
->rcu_node
, free_channel_trigger_list_rcu
);
1780 /* Free sampled channel state. */
1781 cds_lfht_lookup(state
->channel_state_ht
,
1782 hash_channel_key(&key
),
1783 match_channel_state_sample
,
1786 node
= cds_lfht_iter_get_node(&iter
);
1788 * This is expected to be NULL if the channel is destroyed before we
1789 * received a sample.
1792 struct channel_state_sample
*sample
= caa_container_of(node
,
1793 struct channel_state_sample
,
1794 channel_state_ht_node
);
1796 cds_lfht_del(state
->channel_state_ht
, node
);
1797 call_rcu(&sample
->rcu_node
, free_channel_state_sample_rcu
);
1800 /* Remove the channel from the channels_ht and free it. */
1801 cds_lfht_lookup(state
->channels_ht
,
1802 hash_channel_key(&key
),
1806 node
= cds_lfht_iter_get_node(&iter
);
1808 channel_info
= caa_container_of(node
, struct channel_info
,
1810 cds_lfht_del(state
->channels_ht
, node
);
1811 channel_info_destroy(channel_info
);
1814 *cmd_result
= LTTNG_OK
;
1819 int handle_notification_thread_command_session_rotation(
1820 struct notification_thread_state
*state
,
1821 enum notification_thread_command_type cmd_type
,
1822 const char *session_name
, uid_t session_uid
, gid_t session_gid
,
1823 uint64_t trace_archive_chunk_id
,
1824 struct lttng_trace_archive_location
*location
,
1825 enum lttng_error_code
*_cmd_result
)
1828 enum lttng_error_code cmd_result
= LTTNG_OK
;
1829 struct lttng_session_trigger_list
*trigger_list
;
1830 struct lttng_trigger_list_element
*trigger_list_element
;
1831 struct session_info
*session_info
;
1832 const struct lttng_credentials session_creds
= {
1833 .uid
= LTTNG_OPTIONAL_INIT_VALUE(session_uid
),
1834 .gid
= LTTNG_OPTIONAL_INIT_VALUE(session_gid
),
1839 session_info
= find_or_create_session_info(state
, session_name
,
1840 session_uid
, session_gid
);
1841 if (!session_info
) {
1842 /* Allocation error or an internal error occurred. */
1844 cmd_result
= LTTNG_ERR_NOMEM
;
1848 session_info
->rotation
.ongoing
=
1849 cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
;
1850 session_info
->rotation
.id
= trace_archive_chunk_id
;
1851 trigger_list
= get_session_trigger_list(state
, session_name
);
1852 if (!trigger_list
) {
1853 DBG("[notification-thread] No triggers applying to session \"%s\" found",
1858 cds_list_for_each_entry(trigger_list_element
, &trigger_list
->list
,
1860 const struct lttng_condition
*condition
;
1861 struct lttng_trigger
*trigger
;
1862 struct notification_client_list
*client_list
;
1863 struct lttng_evaluation
*evaluation
= NULL
;
1864 enum lttng_condition_type condition_type
;
1865 enum action_executor_status executor_status
;
1867 trigger
= trigger_list_element
->trigger
;
1868 condition
= lttng_trigger_get_const_condition(trigger
);
1870 condition_type
= lttng_condition_get_type(condition
);
1872 if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
&&
1873 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1875 } else if (condition_type
== LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
&&
1876 cmd_type
!= NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
) {
1880 client_list
= get_client_list_from_condition(state
, condition
);
1881 if (cmd_type
== NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
) {
1882 evaluation
= lttng_evaluation_session_rotation_ongoing_create(
1883 trace_archive_chunk_id
);
1885 evaluation
= lttng_evaluation_session_rotation_completed_create(
1886 trace_archive_chunk_id
, location
);
1890 /* Internal error */
1892 cmd_result
= LTTNG_ERR_UNK
;
1897 * Ownership of `evaluation` transferred to the action executor
1898 * no matter the result.
1900 executor_status
= action_executor_enqueue(state
->executor
,
1901 trigger
, evaluation
, &session_creds
,
1904 switch (executor_status
) {
1905 case ACTION_EXECUTOR_STATUS_OK
:
1907 case ACTION_EXECUTOR_STATUS_ERROR
:
1908 case ACTION_EXECUTOR_STATUS_INVALID
:
1910 * TODO Add trigger identification (name/id) when
1911 * it is added to the API.
1913 ERR("Fatal error occurred while enqueuing action associated with session rotation trigger");
1916 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
1918 * TODO Add trigger identification (name/id) when
1919 * it is added to the API.
1921 * Not a fatal error.
1923 WARN("No space left when enqueuing action associated with session rotation trigger");
1931 notification_client_list_put(client_list
);
1932 if (caa_unlikely(ret
)) {
1937 session_info_put(session_info
);
1938 *_cmd_result
= cmd_result
;
1944 int handle_notification_thread_command_add_tracer_event_source(
1945 struct notification_thread_state
*state
,
1946 int tracer_event_source_fd
,
1947 enum lttng_domain_type domain_type
,
1948 enum lttng_error_code
*_cmd_result
)
1951 enum lttng_error_code cmd_result
= LTTNG_OK
;
1952 struct notification_event_tracer_event_source_element
*element
= NULL
;
1954 element
= zmalloc(sizeof(*element
));
1956 cmd_result
= LTTNG_ERR_NOMEM
;
1961 element
->fd
= tracer_event_source_fd
;
1962 element
->domain
= domain_type
;
1964 cds_list_add(&element
->node
, &state
->tracer_event_sources_list
);
1966 DBG3("[notification-thread] Adding tracer event source fd to poll set: tracer_event_source_fd = %d, domain = '%s'",
1967 tracer_event_source_fd
,
1968 lttng_domain_type_str(domain_type
));
1970 /* Adding the read side pipe to the event poll. */
1971 ret
= lttng_poll_add(&state
->events
, tracer_event_source_fd
, LPOLLIN
| LPOLLERR
);
1973 ERR("[notification-thread] Failed to add tracer event source to poll set: tracer_event_source_fd = %d, domain = '%s'",
1974 tracer_event_source_fd
,
1975 lttng_domain_type_str(element
->domain
));
1976 cds_list_del(&element
->node
);
1981 element
->is_fd_in_poll_set
= true;
1984 *_cmd_result
= cmd_result
;
1989 int drain_event_notifier_notification_pipe(
1990 struct notification_thread_state
*state
,
1991 int pipe
, enum lttng_domain_type domain
)
1993 struct lttng_poll_event events
= {0};
1996 ret
= lttng_poll_create(&events
, 1, LTTNG_CLOEXEC
);
1998 ERR("[notification-thread] Error creating lttng_poll_event");
2002 ret
= lttng_poll_add(&events
, pipe
, LPOLLIN
);
2004 ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
2011 * Continue to consume notifications as long as there are new
2012 * ones coming in. The tracer has been asked to stop producing
2015 * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
2016 * monitored (on Linux, at least) and will be returned when
2017 * the pipe is closed but empty.
2019 ret
= lttng_poll_wait_interruptible(&events
, 0);
2020 if (ret
== 0 || (LTTNG_POLL_GETEV(&events
, 1) & LPOLLIN
) == 0) {
2021 /* No more notification to be read on this pipe. */
2024 } else if (ret
< 0) {
2025 PERROR("Failed on lttng_poll_wait_interruptible() call");
2030 ret
= handle_one_event_notifier_notification(state
, pipe
, domain
);
2032 ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
2037 lttng_poll_clean(&events
);
2042 int handle_notification_thread_command_remove_tracer_event_source(
2043 struct notification_thread_state
*state
,
2044 int tracer_event_source_fd
,
2045 enum lttng_error_code
*_cmd_result
)
2049 enum lttng_error_code cmd_result
= LTTNG_OK
;
2050 struct notification_event_tracer_event_source_element
*source_element
= NULL
, *tmp
;
2052 cds_list_for_each_entry_safe(source_element
, tmp
,
2053 &state
->tracer_event_sources_list
, node
) {
2054 if (source_element
->fd
!= tracer_event_source_fd
) {
2058 DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2059 tracer_event_source_fd
,
2060 lttng_domain_type_str(source_element
->domain
));
2061 cds_list_del(&source_element
->node
);
2068 * This is temporarily allowed since the poll activity set is
2069 * not properly cleaned-up for the moment. This is adressed in
2072 source_element
= NULL
;
2076 if (!source_element
->is_fd_in_poll_set
) {
2077 /* Skip the poll set removal. */
2081 DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2082 tracer_event_source_fd
,
2083 lttng_domain_type_str(source_element
->domain
));
2085 /* Removing the fd from the event poll set. */
2086 ret
= lttng_poll_del(&state
->events
, tracer_event_source_fd
);
2088 ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
2089 tracer_event_source_fd
,
2090 lttng_domain_type_str(source_element
->domain
));
2091 cmd_result
= LTTNG_ERR_FATAL
;
2095 source_element
->is_fd_in_poll_set
= false;
2097 ret
= drain_event_notifier_notification_pipe(state
, tracer_event_source_fd
,
2098 source_element
->domain
);
2100 ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
2101 tracer_event_source_fd
,
2102 lttng_domain_type_str(source_element
->domain
));
2103 cmd_result
= LTTNG_ERR_FATAL
;
2108 * The drain_event_notifier_notification_pipe() call might have read
2109 * data from an fd that we received in event in the latest _poll_wait()
2110 * call. Make sure the thread call poll_wait() again to ensure we have
2113 state
->restart_poll
= true;
2116 free(source_element
);
2117 *_cmd_result
= cmd_result
;
2121 int handle_notification_thread_remove_tracer_event_source_no_result(
2122 struct notification_thread_state
*state
,
2123 int tracer_event_source_fd
)
2126 enum lttng_error_code cmd_result
;
2128 ret
= handle_notification_thread_command_remove_tracer_event_source(
2129 state
, tracer_event_source_fd
, &cmd_result
);
2134 static int handle_notification_thread_command_list_triggers(
2135 struct notification_thread_handle
*handle
,
2136 struct notification_thread_state
*state
,
2138 struct lttng_triggers
**triggers
,
2139 enum lttng_error_code
*_cmd_result
)
2142 enum lttng_error_code cmd_result
= LTTNG_OK
;
2143 struct cds_lfht_iter iter
;
2144 struct lttng_trigger_ht_element
*trigger_ht_element
;
2145 struct lttng_triggers
*local_triggers
= NULL
;
2146 const struct lttng_credentials
*creds
;
2150 local_triggers
= lttng_triggers_create();
2151 if (!local_triggers
) {
2152 /* Not a fatal error. */
2153 cmd_result
= LTTNG_ERR_NOMEM
;
2157 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
,
2158 trigger_ht_element
, node
) {
2160 * Only return the triggers to which the client has access.
2161 * The root user has visibility over all triggers.
2163 creds
= lttng_trigger_get_credentials(trigger_ht_element
->trigger
);
2164 if (client_uid
!= lttng_credentials_get_uid(creds
) && client_uid
!= 0) {
2168 ret
= lttng_triggers_add(local_triggers
,
2169 trigger_ht_element
->trigger
);
2171 /* Not a fatal error. */
2173 cmd_result
= LTTNG_ERR_NOMEM
;
2178 /* Transferring ownership to the caller. */
2179 *triggers
= local_triggers
;
2180 local_triggers
= NULL
;
2184 lttng_triggers_destroy(local_triggers
);
2185 *_cmd_result
= cmd_result
;
2190 bool condition_is_supported(struct lttng_condition
*condition
)
2194 switch (lttng_condition_get_type(condition
)) {
2195 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
2196 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
2199 enum lttng_domain_type domain
;
2201 ret
= lttng_condition_buffer_usage_get_domain_type(condition
,
2205 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2206 is_supported
= true;
2211 * Older kernel tracers don't expose the API to monitor their
2212 * buffers. Therefore, we reject triggers that require that
2213 * mechanism to be available to be evaluated.
2215 * Assume unsupported on error.
2217 is_supported
= kernel_supports_ring_buffer_snapshot_sample_positions() == 1;
2220 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
2222 const struct lttng_event_rule
*event_rule
;
2223 enum lttng_domain_type domain
;
2224 const enum lttng_condition_status status
=
2225 lttng_condition_event_rule_get_rule(
2226 condition
, &event_rule
);
2228 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2230 domain
= lttng_event_rule_get_domain_type(event_rule
);
2231 if (domain
!= LTTNG_DOMAIN_KERNEL
) {
2232 is_supported
= true;
2237 * Older kernel tracers can't emit notification. Therefore, we
2238 * reject triggers that require that mechanism to be available
2241 * Assume unsupported on error.
2243 is_supported
= kernel_supports_event_notifiers() == 1;
2247 is_supported
= true;
2250 return is_supported
;
2253 /* Must be called with RCU read lock held. */
2255 int bind_trigger_to_matching_session(struct lttng_trigger
*trigger
,
2256 struct notification_thread_state
*state
)
2259 const struct lttng_condition
*condition
;
2260 const char *session_name
;
2261 struct lttng_session_trigger_list
*trigger_list
;
2263 condition
= lttng_trigger_get_const_condition(trigger
);
2264 switch (lttng_condition_get_type(condition
)) {
2265 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
2266 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
2268 enum lttng_condition_status status
;
2270 status
= lttng_condition_session_rotation_get_session_name(
2271 condition
, &session_name
);
2272 if (status
!= LTTNG_CONDITION_STATUS_OK
) {
2273 ERR("[notification-thread] Failed to bind trigger to session: unable to get 'session_rotation' condition's session name");
2284 trigger_list
= get_session_trigger_list(state
, session_name
);
2285 if (!trigger_list
) {
2286 DBG("[notification-thread] Unable to bind trigger applying to session \"%s\" as it is not yet known to the notification system",
2292 DBG("[notification-thread] Newly registered trigger bound to session \"%s\"",
2294 ret
= lttng_session_trigger_list_add(trigger_list
, trigger
);
2299 /* Must be called with RCU read lock held. */
2301 int bind_trigger_to_matching_channels(struct lttng_trigger
*trigger
,
2302 struct notification_thread_state
*state
)
2305 struct cds_lfht_node
*node
;
2306 struct cds_lfht_iter iter
;
2307 struct channel_info
*channel
;
2309 cds_lfht_for_each_entry(state
->channels_ht
, &iter
, channel
,
2311 struct lttng_trigger_list_element
*trigger_list_element
;
2312 struct lttng_channel_trigger_list
*trigger_list
;
2313 struct cds_lfht_iter lookup_iter
;
2315 if (!trigger_applies_to_channel(trigger
, channel
)) {
2319 cds_lfht_lookup(state
->channel_triggers_ht
,
2320 hash_channel_key(&channel
->key
),
2321 match_channel_trigger_list
,
2324 node
= cds_lfht_iter_get_node(&lookup_iter
);
2326 trigger_list
= caa_container_of(node
,
2327 struct lttng_channel_trigger_list
,
2328 channel_triggers_ht_node
);
2330 trigger_list_element
= zmalloc(sizeof(*trigger_list_element
));
2331 if (!trigger_list_element
) {
2335 CDS_INIT_LIST_HEAD(&trigger_list_element
->node
);
2336 trigger_list_element
->trigger
= trigger
;
2337 cds_list_add(&trigger_list_element
->node
, &trigger_list
->list
);
2338 DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
2346 bool is_trigger_action_notify(const struct lttng_trigger
*trigger
)
2348 bool is_notify
= false;
2349 unsigned int i
, count
;
2350 enum lttng_action_status action_status
;
2351 const struct lttng_action
*action
=
2352 lttng_trigger_get_const_action(trigger
);
2353 enum lttng_action_type action_type
;
2356 action_type
= lttng_action_get_type(action
);
2357 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2360 } else if (action_type
!= LTTNG_ACTION_TYPE_GROUP
) {
2364 action_status
= lttng_action_group_get_count(action
, &count
);
2365 assert(action_status
== LTTNG_ACTION_STATUS_OK
);
2367 for (i
= 0; i
< count
; i
++) {
2368 const struct lttng_action
*inner_action
=
2369 lttng_action_group_get_at_index(
2372 action_type
= lttng_action_get_type(inner_action
);
2373 if (action_type
== LTTNG_ACTION_TYPE_NOTIFY
) {
2383 static bool trigger_name_taken(struct notification_thread_state
*state
,
2384 const struct lttng_trigger
*trigger
)
2386 struct cds_lfht_iter iter
;
2389 * No duplicata is allowed in the triggers_by_name_uid_ht.
2390 * The match is done against the trigger name and uid.
2392 cds_lfht_lookup(state
->triggers_by_name_uid_ht
,
2393 hash_trigger_by_name_uid(trigger
),
2394 match_trigger_by_name_uid
,
2397 return !!cds_lfht_iter_get_node(&iter
);
2401 enum lttng_error_code
generate_trigger_name(
2402 struct notification_thread_state
*state
,
2403 struct lttng_trigger
*trigger
, const char **name
)
2405 enum lttng_error_code ret_code
= LTTNG_OK
;
2407 enum lttng_trigger_status status
;
2410 const int ret
= lttng_trigger_generate_name(trigger
,
2411 state
->trigger_id
.name_offset
++);
2413 /* The only reason this can fail right now. */
2414 ret_code
= LTTNG_ERR_NOMEM
;
2418 status
= lttng_trigger_get_name(trigger
, name
);
2419 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
2421 taken
= trigger_name_taken(state
, trigger
);
2422 } while (taken
|| state
->trigger_id
.name_offset
== UINT64_MAX
);
2428 * FIXME A client's credentials are not checked when registering a trigger.
2430 * The effects of this are benign since:
2431 * - The client will succeed in registering the trigger, as it is valid,
2432 * - The trigger will, internally, be bound to the channel/session,
2433 * - The notifications will not be sent since the client's credentials
2434 * are checked against the channel at that moment.
2436 * If this function returns a non-zero value, it means something is
2437 * fundamentally broken and the whole subsystem/thread will be torn down.
2439 * If a non-fatal error occurs, just set the cmd_result to the appropriate
2443 int handle_notification_thread_command_register_trigger(
2444 struct notification_thread_state
*state
,
2445 struct lttng_trigger
*trigger
,
2446 enum lttng_error_code
*cmd_result
)
2449 struct lttng_condition
*condition
;
2450 struct notification_client
*client
;
2451 struct notification_client_list
*client_list
= NULL
;
2452 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2453 struct notification_client_list_element
*client_list_element
;
2454 struct notification_trigger_tokens_ht_element
*trigger_tokens_ht_element
= NULL
;
2455 struct cds_lfht_node
*node
;
2456 struct cds_lfht_iter iter
;
2457 const char* trigger_name
;
2458 bool free_trigger
= true;
2459 struct lttng_evaluation
*evaluation
= NULL
;
2460 struct lttng_credentials object_creds
;
2463 enum action_executor_status executor_status
;
2464 const uint64_t trigger_tracer_token
=
2465 state
->trigger_id
.next_tracer_token
++;
2469 /* Set the trigger's tracer token. */
2470 lttng_trigger_set_tracer_token(trigger
, trigger_tracer_token
);
2472 if (lttng_trigger_get_name(trigger
, &trigger_name
) ==
2473 LTTNG_TRIGGER_STATUS_UNSET
) {
2474 const enum lttng_error_code ret_code
= generate_trigger_name(
2475 state
, trigger
, &trigger_name
);
2477 if (ret_code
!= LTTNG_OK
) {
2480 *cmd_result
= ret_code
;
2483 } else if (trigger_name_taken(state
, trigger
)) {
2484 /* Not a fatal error. */
2485 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2490 condition
= lttng_trigger_get_condition(trigger
);
2493 /* Some conditions require tracers to implement a minimal ABI version. */
2494 if (!condition_is_supported(condition
)) {
2495 *cmd_result
= LTTNG_ERR_NOT_SUPPORTED
;
2499 trigger_ht_element
= zmalloc(sizeof(*trigger_ht_element
));
2500 if (!trigger_ht_element
) {
2505 /* Add trigger to the trigger_ht. */
2506 cds_lfht_node_init(&trigger_ht_element
->node
);
2507 cds_lfht_node_init(&trigger_ht_element
->node_by_name_uid
);
2508 trigger_ht_element
->trigger
= trigger
;
2510 node
= cds_lfht_add_unique(state
->triggers_ht
,
2511 lttng_condition_hash(condition
),
2514 &trigger_ht_element
->node
);
2515 if (node
!= &trigger_ht_element
->node
) {
2516 /* Not a fatal error, simply report it to the client. */
2517 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2518 goto error_free_ht_element
;
2521 node
= cds_lfht_add_unique(state
->triggers_by_name_uid_ht
,
2522 hash_trigger_by_name_uid(trigger
),
2523 match_trigger_by_name_uid
,
2525 &trigger_ht_element
->node_by_name_uid
);
2526 if (node
!= &trigger_ht_element
->node_by_name_uid
) {
2527 /* Not a fatal error, simply report it to the client. */
2528 cds_lfht_del(state
->triggers_ht
, &trigger_ht_element
->node
);
2529 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2530 goto error_free_ht_element
;
2533 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2534 trigger_tokens_ht_element
= zmalloc(sizeof(*trigger_tokens_ht_element
));
2535 if (!trigger_tokens_ht_element
) {
2538 cds_lfht_del(state
->triggers_ht
,
2539 &trigger_ht_element
->node
);
2540 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2541 &trigger_ht_element
->node_by_name_uid
);
2542 goto error_free_ht_element
;
2545 /* Add trigger token to the trigger_tokens_ht. */
2546 cds_lfht_node_init(&trigger_tokens_ht_element
->node
);
2547 trigger_tokens_ht_element
->token
=
2548 LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
2549 trigger_tokens_ht_element
->trigger
= trigger
;
2551 node
= cds_lfht_add_unique(state
->trigger_tokens_ht
,
2552 hash_key_u64(&trigger_tokens_ht_element
->token
,
2554 match_trigger_token
,
2555 &trigger_tokens_ht_element
->token
,
2556 &trigger_tokens_ht_element
->node
);
2557 if (node
!= &trigger_tokens_ht_element
->node
) {
2558 /* Internal corruption, fatal error. */
2560 *cmd_result
= LTTNG_ERR_TRIGGER_EXISTS
;
2561 cds_lfht_del(state
->triggers_ht
,
2562 &trigger_ht_element
->node
);
2563 cds_lfht_del(state
->triggers_by_name_uid_ht
,
2564 &trigger_ht_element
->node_by_name_uid
);
2565 goto error_free_ht_element
;
2570 * Ownership of the trigger and of its wrapper was transfered to
2571 * the triggers_ht. Same for token ht element if necessary.
2573 trigger_tokens_ht_element
= NULL
;
2574 trigger_ht_element
= NULL
;
2575 free_trigger
= false;
2578 * The rest only applies to triggers that have a "notify" action.
2579 * It is not skipped as this is the only action type currently
2582 if (is_trigger_action_notify(trigger
)) {
2583 client_list
= notification_client_list_create(trigger
);
2586 goto error_free_ht_element
;
2589 /* Build a list of clients to which this new trigger applies. */
2590 cds_lfht_for_each_entry (state
->client_socket_ht
, &iter
, client
,
2591 client_socket_ht_node
) {
2592 if (!trigger_applies_to_client(trigger
, client
)) {
2596 client_list_element
=
2597 zmalloc(sizeof(*client_list_element
));
2598 if (!client_list_element
) {
2600 goto error_put_client_list
;
2603 CDS_INIT_LIST_HEAD(&client_list_element
->node
);
2604 client_list_element
->client
= client
;
2605 cds_list_add(&client_list_element
->node
,
2606 &client_list
->list
);
2610 * Client list ownership transferred to the
2611 * notification_trigger_clients_ht.
2613 publish_notification_client_list(state
, client_list
);
2616 switch (get_condition_binding_object(condition
)) {
2617 case LTTNG_OBJECT_TYPE_SESSION
:
2618 /* Add the trigger to the list if it matches a known session. */
2619 ret
= bind_trigger_to_matching_session(trigger
, state
);
2621 goto error_put_client_list
;
2624 case LTTNG_OBJECT_TYPE_CHANNEL
:
2626 * Add the trigger to list of triggers bound to the channels
2629 ret
= bind_trigger_to_matching_channels(trigger
, state
);
2631 goto error_put_client_list
;
2634 case LTTNG_OBJECT_TYPE_NONE
:
2637 ERR("Unknown object type on which to bind a newly registered trigger was encountered");
2639 goto error_put_client_list
;
2643 * The new trigger's condition must be evaluated against the current
2646 * In the case of `notify` action, nothing preventing clients from
2647 * subscribing to a condition before the corresponding trigger is
2648 * registered, we have to evaluate this new condition right away.
2650 * At some point, we were waiting for the next "evaluation" (e.g. on
2651 * reception of a channel sample) to evaluate this new condition, but
2654 * The reason it was broken is that waiting for the next sample
2655 * does not allow us to properly handle transitions for edge-triggered
2658 * Consider this example: when we handle a new channel sample, we
2659 * evaluate each conditions twice: once with the previous state, and
2660 * again with the newest state. We then use those two results to
2661 * determine whether a state change happened: a condition was false and
2662 * became true. If a state change happened, we have to notify clients.
2664 * Now, if a client subscribes to a given notification and registers
2665 * a trigger *after* that subscription, we have to make sure the
2666 * condition is evaluated at this point while considering only the
2667 * current state. Otherwise, the next evaluation cycle may only see
2668 * that the evaluations remain the same (true for samples n-1 and n) and
2669 * the client will never know that the condition has been met.
2671 switch (get_condition_binding_object(condition
)) {
2672 case LTTNG_OBJECT_TYPE_SESSION
:
2673 ret
= evaluate_session_condition_for_client(condition
, state
,
2674 &evaluation
, &object_uid
,
2676 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2677 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2679 case LTTNG_OBJECT_TYPE_CHANNEL
:
2680 ret
= evaluate_channel_condition_for_client(condition
, state
,
2681 &evaluation
, &object_uid
,
2683 LTTNG_OPTIONAL_SET(&object_creds
.uid
, object_uid
);
2684 LTTNG_OPTIONAL_SET(&object_creds
.gid
, object_gid
);
2686 case LTTNG_OBJECT_TYPE_NONE
:
2689 case LTTNG_OBJECT_TYPE_UNKNOWN
:
2697 goto error_put_client_list
;
2700 DBG("Newly registered trigger's condition evaluated to %s",
2701 evaluation
? "true" : "false");
2703 /* Evaluation yielded nothing. Normal exit. */
2709 * Ownership of `evaluation` transferred to the action executor
2710 * no matter the result.
2712 executor_status
= action_executor_enqueue(state
->executor
, trigger
,
2713 evaluation
, &object_creds
, client_list
);
2715 switch (executor_status
) {
2716 case ACTION_EXECUTOR_STATUS_OK
:
2718 case ACTION_EXECUTOR_STATUS_ERROR
:
2719 case ACTION_EXECUTOR_STATUS_INVALID
:
2721 * TODO Add trigger identification (name/id) when
2722 * it is added to the API.
2724 ERR("Fatal error occurred while enqueuing action associated to newly registered trigger");
2726 goto error_put_client_list
;
2727 case ACTION_EXECUTOR_STATUS_OVERFLOW
:
2729 * TODO Add trigger identification (name/id) when
2730 * it is added to the API.
2732 * Not a fatal error.
2734 WARN("No space left when enqueuing action associated to newly registered trigger");
2742 *cmd_result
= LTTNG_OK
;
2743 DBG("Registered trigger: name = `%s`, tracer token = %" PRIu64
,
2744 trigger_name
, trigger_tracer_token
);
2746 error_put_client_list
:
2747 notification_client_list_put(client_list
);
2749 error_free_ht_element
:
2750 if (trigger_ht_element
) {
2751 /* Delayed removal due to RCU constraint on delete. */
2752 call_rcu(&trigger_ht_element
->rcu_node
,
2753 free_lttng_trigger_ht_element_rcu
);
2756 free(trigger_tokens_ht_element
);
2759 lttng_trigger_destroy(trigger
);
2766 void free_lttng_trigger_ht_element_rcu(struct rcu_head
*node
)
2768 free(caa_container_of(node
, struct lttng_trigger_ht_element
,
2773 void free_notification_trigger_tokens_ht_element_rcu(struct rcu_head
*node
)
2775 free(caa_container_of(node
, struct notification_trigger_tokens_ht_element
,
2780 int handle_notification_thread_command_unregister_trigger(
2781 struct notification_thread_state
*state
,
2782 const struct lttng_trigger
*trigger
,
2783 enum lttng_error_code
*_cmd_reply
)
2785 struct cds_lfht_iter iter
;
2786 struct cds_lfht_node
*triggers_ht_node
;
2787 struct lttng_channel_trigger_list
*trigger_list
;
2788 struct notification_client_list
*client_list
;
2789 struct lttng_trigger_ht_element
*trigger_ht_element
= NULL
;
2790 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(
2792 enum lttng_error_code cmd_reply
;
2796 cds_lfht_lookup(state
->triggers_ht
,
2797 lttng_condition_hash(condition
),
2801 triggers_ht_node
= cds_lfht_iter_get_node(&iter
);
2802 if (!triggers_ht_node
) {
2803 cmd_reply
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2806 cmd_reply
= LTTNG_OK
;
2809 /* Remove trigger from channel_triggers_ht. */
2810 cds_lfht_for_each_entry(state
->channel_triggers_ht
, &iter
, trigger_list
,
2811 channel_triggers_ht_node
) {
2812 struct lttng_trigger_list_element
*trigger_element
, *tmp
;
2814 cds_list_for_each_entry_safe(trigger_element
, tmp
,
2815 &trigger_list
->list
, node
) {
2816 if (!lttng_trigger_is_equal(trigger
, trigger_element
->trigger
)) {
2820 DBG("[notification-thread] Removed trigger from channel_triggers_ht");
2821 cds_list_del(&trigger_element
->node
);
2822 /* A trigger can only appear once per channel */
2827 if (lttng_condition_get_type(condition
) ==
2828 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
2829 struct notification_trigger_tokens_ht_element
2830 *trigger_tokens_ht_element
;
2832 cds_lfht_for_each_entry (state
->trigger_tokens_ht
, &iter
,
2833 trigger_tokens_ht_element
, node
) {
2834 if (!lttng_trigger_is_equal(trigger
,
2835 trigger_tokens_ht_element
->trigger
)) {
2839 DBG("[notification-thread] Removed trigger from tokens_ht");
2840 cds_lfht_del(state
->trigger_tokens_ht
,
2841 &trigger_tokens_ht_element
->node
);
2842 call_rcu(&trigger_tokens_ht_element
->rcu_node
,
2843 free_notification_trigger_tokens_ht_element_rcu
);
2849 if (is_trigger_action_notify(trigger
)) {
2851 * Remove and release the client list from
2852 * notification_trigger_clients_ht.
2854 client_list
= get_client_list_from_condition(state
, condition
);
2855 assert(client_list
);
2857 /* Put new reference and the hashtable's reference. */
2858 notification_client_list_put(client_list
);
2859 notification_client_list_put(client_list
);
2863 /* Remove trigger from triggers_ht. */
2864 trigger_ht_element
= caa_container_of(triggers_ht_node
,
2865 struct lttng_trigger_ht_element
, node
);
2866 cds_lfht_del(state
->triggers_by_name_uid_ht
, &trigger_ht_element
->node_by_name_uid
);
2867 cds_lfht_del(state
->triggers_ht
, triggers_ht_node
);
2869 /* Release the ownership of the trigger. */
2870 lttng_trigger_destroy(trigger_ht_element
->trigger
);
2871 call_rcu(&trigger_ht_element
->rcu_node
, free_lttng_trigger_ht_element_rcu
);
2875 *_cmd_reply
= cmd_reply
;
2880 /* Returns 0 on success, 1 on exit requested, negative value on error. */
2881 int handle_notification_thread_command(
2882 struct notification_thread_handle
*handle
,
2883 struct notification_thread_state
*state
)
2887 struct notification_thread_command
*cmd
;
2889 /* Read the event pipe to put it back into a quiescent state. */
2890 ret
= lttng_read(lttng_pipe_get_readfd(handle
->cmd_queue
.event_pipe
), &counter
,
2892 if (ret
!= sizeof(counter
)) {
2896 pthread_mutex_lock(&handle
->cmd_queue
.lock
);
2897 cmd
= cds_list_first_entry(&handle
->cmd_queue
.list
,
2898 struct notification_thread_command
, cmd_list_node
);
2899 cds_list_del(&cmd
->cmd_list_node
);
2900 pthread_mutex_unlock(&handle
->cmd_queue
.lock
);
2902 DBG("[notification-thread] Received `%s` command",
2903 notification_command_type_str(cmd
->type
));
2904 switch (cmd
->type
) {
2905 case NOTIFICATION_COMMAND_TYPE_REGISTER_TRIGGER
:
2906 ret
= handle_notification_thread_command_register_trigger(state
,
2907 cmd
->parameters
.register_trigger
.trigger
,
2910 case NOTIFICATION_COMMAND_TYPE_UNREGISTER_TRIGGER
:
2911 ret
= handle_notification_thread_command_unregister_trigger(
2913 cmd
->parameters
.unregister_trigger
.trigger
,
2916 case NOTIFICATION_COMMAND_TYPE_ADD_CHANNEL
:
2917 ret
= handle_notification_thread_command_add_channel(
2919 cmd
->parameters
.add_channel
.session
.name
,
2920 cmd
->parameters
.add_channel
.session
.uid
,
2921 cmd
->parameters
.add_channel
.session
.gid
,
2922 cmd
->parameters
.add_channel
.channel
.name
,
2923 cmd
->parameters
.add_channel
.channel
.domain
,
2924 cmd
->parameters
.add_channel
.channel
.key
,
2925 cmd
->parameters
.add_channel
.channel
.capacity
,
2928 case NOTIFICATION_COMMAND_TYPE_REMOVE_CHANNEL
:
2929 ret
= handle_notification_thread_command_remove_channel(
2930 state
, cmd
->parameters
.remove_channel
.key
,
2931 cmd
->parameters
.remove_channel
.domain
,
2934 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING
:
2935 case NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED
:
2936 ret
= handle_notification_thread_command_session_rotation(
2939 cmd
->parameters
.session_rotation
.session_name
,
2940 cmd
->parameters
.session_rotation
.uid
,
2941 cmd
->parameters
.session_rotation
.gid
,
2942 cmd
->parameters
.session_rotation
.trace_archive_chunk_id
,
2943 cmd
->parameters
.session_rotation
.location
,
2946 case NOTIFICATION_COMMAND_TYPE_ADD_TRACER_EVENT_SOURCE
:
2947 ret
= handle_notification_thread_command_add_tracer_event_source(
2949 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2950 cmd
->parameters
.tracer_event_source
.domain
,
2953 case NOTIFICATION_COMMAND_TYPE_REMOVE_TRACER_EVENT_SOURCE
:
2954 ret
= handle_notification_thread_command_remove_tracer_event_source(
2956 cmd
->parameters
.tracer_event_source
.tracer_event_source_fd
,
2959 case NOTIFICATION_COMMAND_TYPE_LIST_TRIGGERS
:
2961 struct lttng_triggers
*triggers
= NULL
;
2963 ret
= handle_notification_thread_command_list_triggers(
2966 cmd
->parameters
.list_triggers
.uid
,
2969 cmd
->reply
.list_triggers
.triggers
= triggers
;
2973 case NOTIFICATION_COMMAND_TYPE_QUIT
:
2974 cmd
->reply_code
= LTTNG_OK
;
2977 case NOTIFICATION_COMMAND_TYPE_CLIENT_COMMUNICATION_UPDATE
:
2979 const enum client_transmission_status client_status
=
2980 cmd
->parameters
.client_communication_update
2982 const notification_client_id client_id
=
2983 cmd
->parameters
.client_communication_update
.id
;
2984 struct notification_client
*client
;
2987 client
= get_client_from_id(client_id
, state
);
2991 * Client error was probably already picked-up by the
2992 * notification thread or it has disconnected
2993 * gracefully while this command was queued.
2995 DBG("Failed to find notification client to update communication status, client id = %" PRIu64
,
2999 ret
= client_handle_transmission_status(
3000 client
, client_status
, state
);
3006 ERR("[notification-thread] Unknown internal command received");
3014 if (cmd
->is_async
) {
3018 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3022 /* Wake-up and return a fatal error to the calling thread. */
3023 lttng_waiter_wake_up(&cmd
->reply_waiter
);
3024 cmd
->reply_code
= LTTNG_ERR_FATAL
;
3026 /* Indicate a fatal error to the caller. */
3031 int socket_set_non_blocking(int socket
)
3035 /* Set the pipe as non-blocking. */
3036 ret
= fcntl(socket
, F_GETFL
, 0);
3038 PERROR("fcntl get socket flags");
3043 ret
= fcntl(socket
, F_SETFL
, flags
| O_NONBLOCK
);
3045 PERROR("fcntl set O_NONBLOCK socket flag");
3048 DBG("Client socket (fd = %i) set as non-blocking", socket
);
3054 int client_reset_inbound_state(struct notification_client
*client
)
3059 lttng_payload_clear(&client
->communication
.inbound
.payload
);
3061 client
->communication
.inbound
.bytes_to_receive
=
3062 sizeof(struct lttng_notification_channel_message
);
3063 client
->communication
.inbound
.msg_type
=
3064 LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_UNKNOWN
;
3065 LTTNG_SOCK_SET_UID_CRED(&client
->communication
.inbound
.creds
, -1);
3066 LTTNG_SOCK_SET_GID_CRED(&client
->communication
.inbound
.creds
, -1);
3067 ret
= lttng_dynamic_buffer_set_size(
3068 &client
->communication
.inbound
.payload
.buffer
,
3069 client
->communication
.inbound
.bytes_to_receive
);
3074 int handle_notification_thread_client_connect(
3075 struct notification_thread_state
*state
)
3078 struct notification_client
*client
;
3080 DBG("[notification-thread] Handling new notification channel client connection");
3082 client
= zmalloc(sizeof(*client
));
3089 pthread_mutex_init(&client
->lock
, NULL
);
3090 client
->id
= state
->next_notification_client_id
++;
3091 CDS_INIT_LIST_HEAD(&client
->condition_list
);
3092 lttng_payload_init(&client
->communication
.inbound
.payload
);
3093 lttng_payload_init(&client
->communication
.outbound
.payload
);
3094 client
->communication
.inbound
.expect_creds
= true;
3096 ret
= client_reset_inbound_state(client
);
3098 ERR("[notification-thread] Failed to reset client communication's inbound state");
3103 ret
= lttcomm_accept_unix_sock(state
->notification_channel_socket
);
3105 ERR("[notification-thread] Failed to accept new notification channel client connection");
3110 client
->socket
= ret
;
3112 ret
= socket_set_non_blocking(client
->socket
);
3114 ERR("[notification-thread] Failed to set new notification channel client connection socket as non-blocking");
3118 ret
= lttcomm_setsockopt_creds_unix_sock(client
->socket
);
3120 ERR("[notification-thread] Failed to set socket options on new notification channel client socket");
3125 ret
= lttng_poll_add(&state
->events
, client
->socket
,
3126 LPOLLIN
| LPOLLERR
|
3127 LPOLLHUP
| LPOLLRDHUP
);
3129 ERR("[notification-thread] Failed to add notification channel client socket to poll set");
3133 DBG("[notification-thread] Added new notification channel client socket (%i) to poll set",
3137 cds_lfht_add(state
->client_socket_ht
,
3138 hash_client_socket(client
->socket
),
3139 &client
->client_socket_ht_node
);
3140 cds_lfht_add(state
->client_id_ht
,
3141 hash_client_id(client
->id
),
3142 &client
->client_id_ht_node
);
3148 notification_client_destroy(client
, state
);
3153 * RCU read-lock must be held by the caller.
3154 * Client lock must _not_ be held by the caller.
3157 int notification_thread_client_disconnect(
3158 struct notification_client
*client
,
3159 struct notification_thread_state
*state
)
3162 struct lttng_condition_list_element
*condition_list_element
, *tmp
;
3164 /* Acquire the client lock to disable its communication atomically. */
3165 pthread_mutex_lock(&client
->lock
);
3166 client
->communication
.active
= false;
3167 cds_lfht_del(state
->client_socket_ht
, &client
->client_socket_ht_node
);
3168 cds_lfht_del(state
->client_id_ht
, &client
->client_id_ht_node
);
3169 pthread_mutex_unlock(&client
->lock
);
3171 ret
= lttng_poll_del(&state
->events
, client
->socket
);
3173 ERR("[notification-thread] Failed to remove client socket %d from poll set",
3177 /* Release all conditions to which the client was subscribed. */
3178 cds_list_for_each_entry_safe(condition_list_element
, tmp
,
3179 &client
->condition_list
, node
) {
3180 (void) notification_thread_client_unsubscribe(client
,
3181 condition_list_element
->condition
, state
, NULL
);
3185 * Client no longer accessible to other threads (through the
3188 notification_client_destroy(client
, state
);
3192 int handle_notification_thread_client_disconnect(
3193 int client_socket
, struct notification_thread_state
*state
)
3196 struct notification_client
*client
;
3199 DBG("[notification-thread] Closing client connection (socket fd = %i)",
3201 client
= get_client_from_socket(client_socket
, state
);
3203 /* Internal state corruption, fatal error. */
3204 ERR("[notification-thread] Unable to find client (socket fd = %i)",
3210 ret
= notification_thread_client_disconnect(client
, state
);
3216 int handle_notification_thread_client_disconnect_all(
3217 struct notification_thread_state
*state
)
3219 struct cds_lfht_iter iter
;
3220 struct notification_client
*client
;
3221 bool error_encoutered
= false;
3224 DBG("[notification-thread] Closing all client connections");
3225 cds_lfht_for_each_entry(state
->client_socket_ht
, &iter
, client
,
3226 client_socket_ht_node
) {
3229 ret
= notification_thread_client_disconnect(
3232 error_encoutered
= true;
3236 return error_encoutered
? 1 : 0;
3239 int handle_notification_thread_trigger_unregister_all(
3240 struct notification_thread_state
*state
)
3242 bool error_occurred
= false;
3243 struct cds_lfht_iter iter
;
3244 struct lttng_trigger_ht_element
*trigger_ht_element
;
3247 cds_lfht_for_each_entry(state
->triggers_ht
, &iter
, trigger_ht_element
,
3249 int ret
= handle_notification_thread_command_unregister_trigger(
3250 state
, trigger_ht_element
->trigger
, NULL
);
3252 error_occurred
= true;
3256 return error_occurred
? -1 : 0;
3260 int client_handle_transmission_status(
3261 struct notification_client
*client
,
3262 enum client_transmission_status transmission_status
,
3263 struct notification_thread_state
*state
)
3267 switch (transmission_status
) {
3268 case CLIENT_TRANSMISSION_STATUS_COMPLETE
:
3269 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3270 CLIENT_POLL_MASK_IN
);
3276 case CLIENT_TRANSMISSION_STATUS_QUEUED
:
3278 * We want to be notified whenever there is buffer space
3279 * available to send the rest of the payload.
3281 ret
= lttng_poll_mod(&state
->events
, client
->socket
,
3282 CLIENT_POLL_MASK_IN_OUT
);
3287 case CLIENT_TRANSMISSION_STATUS_FAIL
:
3288 ret
= notification_thread_client_disconnect(client
, state
);
3293 case CLIENT_TRANSMISSION_STATUS_ERROR
:
3303 /* Client lock must be acquired by caller. */
3305 enum client_transmission_status
client_flush_outgoing_queue(
3306 struct notification_client
*client
)
3309 size_t to_send_count
;
3310 enum client_transmission_status status
;
3311 struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3312 &client
->communication
.outbound
.payload
, 0, -1);
3313 const int fds_to_send_count
=
3314 lttng_payload_view_get_fd_handle_count(&pv
);
3316 ASSERT_LOCKED(client
->lock
);
3318 if (!client
->communication
.active
) {
3319 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3323 if (pv
.buffer
.size
== 0) {
3325 * If both data and fds are equal to zero, we are in an invalid
3328 assert(fds_to_send_count
!= 0);
3333 to_send_count
= pv
.buffer
.size
;
3334 DBG("[notification-thread] Flushing client (socket fd = %i) outgoing queue",
3337 ret
= lttcomm_send_unix_sock_non_block(client
->socket
,
3340 if ((ret
>= 0 && ret
< to_send_count
)) {
3341 DBG("[notification-thread] Client (socket fd = %i) outgoing queue could not be completely flushed",
3343 to_send_count
-= max(ret
, 0);
3345 memmove(client
->communication
.outbound
.payload
.buffer
.data
,
3347 pv
.buffer
.size
- to_send_count
,
3349 ret
= lttng_dynamic_buffer_set_size(
3350 &client
->communication
.outbound
.payload
.buffer
,
3356 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3358 } else if (ret
< 0) {
3359 /* Generic error, disable the client's communication. */
3360 ERR("[notification-thread] Failed to flush outgoing queue, disconnecting client (socket fd = %i)",
3362 client
->communication
.active
= false;
3363 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3367 * No error and flushed the queue completely.
3369 * The payload buffer size is used later to
3370 * check if there is notifications queued. So albeit that the
3371 * direct caller knows that the transmission is complete, we
3372 * need to set the buffer size to zero.
3374 ret
= lttng_dynamic_buffer_set_size(
3375 &client
->communication
.outbound
.payload
.buffer
, 0);
3382 /* No fds to send, transmission is complete. */
3383 if (fds_to_send_count
== 0) {
3384 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3388 ret
= lttcomm_send_payload_view_fds_unix_sock_non_block(
3389 client
->socket
, &pv
);
3391 /* Generic error, disable the client's communication. */
3392 ERR("[notification-thread] Failed to flush outgoing fds queue, disconnecting client (socket fd = %i)",
3394 client
->communication
.active
= false;
3395 status
= CLIENT_TRANSMISSION_STATUS_FAIL
;
3397 } else if (ret
== 0) {
3398 /* Nothing could be sent. */
3399 status
= CLIENT_TRANSMISSION_STATUS_QUEUED
;
3401 /* Fd passing is an all or nothing kind of thing. */
3402 status
= CLIENT_TRANSMISSION_STATUS_COMPLETE
;
3404 * The payload _fd_array count is used later to
3405 * check if there is notifications queued. So although the
3406 * direct caller knows that the transmission is complete, we
3407 * need to clear the _fd_array for the queuing check.
3409 lttng_dynamic_pointer_array_clear(
3410 &client
->communication
.outbound
.payload
3415 if (status
== CLIENT_TRANSMISSION_STATUS_COMPLETE
) {
3416 client
->communication
.outbound
.queued_command_reply
= false;
3417 client
->communication
.outbound
.dropped_notification
= false;
3418 lttng_payload_clear(&client
->communication
.outbound
.payload
);
3423 return CLIENT_TRANSMISSION_STATUS_ERROR
;
3427 bool client_has_outbound_data_left(
3428 const struct notification_client
*client
)
3430 const struct lttng_payload_view pv
= lttng_payload_view_from_payload(
3431 &client
->communication
.outbound
.payload
, 0, -1);
3432 const bool has_data
= pv
.buffer
.size
!= 0;
3433 const bool has_fds
= lttng_payload_view_get_fd_handle_count(&pv
);
3435 return has_data
|| has_fds
;
3438 /* Client lock must _not_ be held by the caller. */
3440 int client_send_command_reply(struct notification_client
*client
,
3441 struct notification_thread_state
*state
,
3442 enum lttng_notification_channel_status status
)
3445 struct lttng_notification_channel_command_reply reply
= {
3446 .status
= (int8_t) status
,
3448 struct lttng_notification_channel_message msg
= {
3449 .type
= (int8_t) LTTNG_NOTIFICATION_CHANNEL_MESSAGE_TYPE_COMMAND_REPLY
,
3450 .size
= sizeof(reply
),
3452 char buffer
[sizeof(msg
) + sizeof(reply
)];
3453 enum client_transmission_status transmission_status
;
3455 memcpy(buffer
, &msg
, sizeof(msg
));
3456 memcpy(buffer
+ sizeof(msg
), &reply
, sizeof(reply
));
3457 DBG("[notification-thread] Send command reply (%i)", (int) status
);
3459 pthread_mutex_lock(&client
->lock
);
3460 if (client
->communication
.outbound
.queued_command_reply
) {
3461 /* Protocol error. */
3465 /* Enqueue buffer to outgoing queue and flush it. */
3466 ret
= lttng_dynamic_buffer_append(
3467 &client
->communication
.outbound
.payload
.buffer
,
3468 buffer
, sizeof(buffer
));
3473 transmission_status
= client_flush_outgoing_queue(client
);
3475 if (client_has_outbound_data_left(client
)) {
3476 /* Queue could not be emptied. */
3477 client
->communication
.outbound
.queued_command_reply
= true;
3480 pthread_mutex_unlock(&client
->lock
);
3481 ret
= client_handle_transmission_status(
3482 client
, transmission_status
, state
);
3489 pthread_mutex_unlock(&client
->lock
);