2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/credentials.h>
10 #include <common/dynamic-array.h>
11 #include <common/error.h>
12 #include <common/mi-lttng.h>
13 #include <common/optional.h>
14 #include <common/payload-view.h>
15 #include <common/payload.h>
17 #include <lttng/action/action-internal.h>
18 #include <lttng/condition/buffer-usage.h>
19 #include <lttng/condition/condition-internal.h>
20 #include <lttng/condition/event-rule-matches-internal.h>
21 #include <lttng/condition/event-rule-matches.h>
22 #include <lttng/domain.h>
23 #include <lttng/error-query-internal.h>
24 #include <lttng/event-expr-internal.h>
25 #include <lttng/event-rule/event-rule-internal.h>
26 #include <lttng/trigger/trigger-internal.h>
30 bool lttng_trigger_validate(const struct lttng_trigger
*trigger
)
39 if (!trigger
->creds
.uid
.is_set
) {
44 valid
= lttng_condition_validate(trigger
->condition
) &&
45 lttng_action_validate(trigger
->action
);
50 struct lttng_trigger
*lttng_trigger_create(
51 struct lttng_condition
*condition
,
52 struct lttng_action
*action
)
54 struct lttng_trigger
*trigger
= NULL
;
56 if (!condition
|| !action
) {
60 trigger
= zmalloc(sizeof(struct lttng_trigger
));
65 urcu_ref_init(&trigger
->ref
);
67 lttng_condition_get(condition
);
68 trigger
->condition
= condition
;
70 lttng_action_get(action
);
71 trigger
->action
= action
;
73 pthread_mutex_init(&trigger
->lock
, NULL
);
74 trigger
->registered
= false;
81 * Note: the lack of reference counting 'get' on the condition object is normal.
82 * This API was exposed as such in 2.11. The client is not expected to call
83 * lttng_condition_destroy on the returned object.
85 struct lttng_condition
*lttng_trigger_get_condition(
86 struct lttng_trigger
*trigger
)
88 return trigger
? trigger
->condition
: NULL
;
91 const struct lttng_condition
*lttng_trigger_get_const_condition(
92 const struct lttng_trigger
*trigger
)
94 return trigger
? trigger
->condition
: NULL
;
98 * Note: the lack of reference counting 'get' on the action object is normal.
99 * This API was exposed as such in 2.11. The client is not expected to call
100 * lttng_action_destroy on the returned object.
102 struct lttng_action
*lttng_trigger_get_action(
103 struct lttng_trigger
*trigger
)
105 return trigger
? trigger
->action
: NULL
;
108 const struct lttng_action
*lttng_trigger_get_const_action(
109 const struct lttng_trigger
*trigger
)
111 return trigger
? trigger
->action
: NULL
;
114 static void trigger_destroy_ref(struct urcu_ref
*ref
)
116 struct lttng_trigger
*trigger
=
117 container_of(ref
, struct lttng_trigger
, ref
);
118 struct lttng_action
*action
= lttng_trigger_get_action(trigger
);
119 struct lttng_condition
*condition
=
120 lttng_trigger_get_condition(trigger
);
125 /* Release ownership. */
126 lttng_action_put(action
);
127 lttng_condition_put(condition
);
129 pthread_mutex_destroy(&trigger
->lock
);
135 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
137 lttng_trigger_put(trigger
);
141 ssize_t
lttng_trigger_create_from_payload(
142 struct lttng_payload_view
*src_view
,
143 struct lttng_trigger
**_trigger
)
145 ssize_t ret
, offset
= 0, condition_size
, action_size
, name_size
= 0;
146 struct lttng_condition
*condition
= NULL
;
147 struct lttng_action
*action
= NULL
;
148 const struct lttng_trigger_comm
*trigger_comm
;
149 const char *name
= NULL
;
150 struct lttng_credentials creds
= {
151 .uid
= LTTNG_OPTIONAL_INIT_UNSET
,
152 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
154 struct lttng_trigger
*trigger
= NULL
;
155 const struct lttng_payload_view trigger_comm_view
=
156 lttng_payload_view_from_view(
157 src_view
, 0, sizeof(*trigger_comm
));
159 if (!src_view
|| !_trigger
) {
164 if (!lttng_payload_view_is_valid(&trigger_comm_view
)) {
165 /* Payload not large enough to contain the header. */
170 /* lttng_trigger_comm header */
171 trigger_comm
= (typeof(trigger_comm
)) trigger_comm_view
.buffer
.data
;
173 /* Set the trigger's creds. */
174 if (trigger_comm
->uid
> (uint64_t) ((uid_t
) -1)) {
175 /* UID out of range for this platform. */
180 LTTNG_OPTIONAL_SET(&creds
.uid
, trigger_comm
->uid
);
182 offset
+= sizeof(*trigger_comm
);
184 if (trigger_comm
->name_length
!= 0) {
186 const struct lttng_payload_view name_view
=
187 lttng_payload_view_from_view(
189 trigger_comm
->name_length
);
191 if (!lttng_payload_view_is_valid(&name_view
)) {
196 name
= name_view
.buffer
.data
;
197 if (!lttng_buffer_view_contains_string(&name_view
.buffer
, name
,
198 trigger_comm
->name_length
)) {
203 offset
+= trigger_comm
->name_length
;
204 name_size
= trigger_comm
->name_length
;
208 /* struct lttng_condition */
209 struct lttng_payload_view condition_view
=
210 lttng_payload_view_from_view(
211 src_view
, offset
, -1);
213 condition_size
= lttng_condition_create_from_payload(&condition_view
,
217 if (condition_size
< 0) {
218 ret
= condition_size
;
222 offset
+= condition_size
;
224 /* struct lttng_action */
225 struct lttng_payload_view action_view
=
226 lttng_payload_view_from_view(
227 src_view
, offset
, -1);
229 action_size
= lttng_action_create_from_payload(&action_view
, &action
);
232 if (action_size
< 0) {
236 offset
+= action_size
;
238 /* Unexpected size of inner-elements; the buffer is corrupted. */
239 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
+ name_size
) {
244 trigger
= lttng_trigger_create(condition
, action
);
250 lttng_trigger_set_credentials(trigger
, &creds
);
253 * The trigger object owns references to the action and condition
256 lttng_condition_put(condition
);
259 lttng_action_put(action
);
263 const enum lttng_trigger_status status
=
264 lttng_trigger_set_name(trigger
, name
);
266 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
275 lttng_condition_put(condition
);
276 lttng_action_put(action
);
281 lttng_trigger_put(trigger
);
288 * Both elements are stored contiguously, see their "*_comm" structure
289 * for the detailed format.
292 int lttng_trigger_serialize(const struct lttng_trigger
*trigger
,
293 struct lttng_payload
*payload
)
296 size_t header_offset
, size_before_payload
, size_name
;
297 struct lttng_trigger_comm trigger_comm
= {};
298 struct lttng_trigger_comm
*header
;
299 const struct lttng_credentials
*creds
= NULL
;
301 creds
= lttng_trigger_get_credentials(trigger
);
304 trigger_comm
.uid
= LTTNG_OPTIONAL_GET(creds
->uid
);
306 if (trigger
->name
!= NULL
) {
307 size_name
= strlen(trigger
->name
) + 1;
312 trigger_comm
.name_length
= size_name
;
314 header_offset
= payload
->buffer
.size
;
315 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &trigger_comm
,
316 sizeof(trigger_comm
));
321 size_before_payload
= payload
->buffer
.size
;
324 ret
= lttng_dynamic_buffer_append(
325 &payload
->buffer
, trigger
->name
, size_name
);
330 ret
= lttng_condition_serialize(trigger
->condition
, payload
);
335 ret
= lttng_action_serialize(trigger
->action
, payload
);
340 /* Update payload size. */
341 header
= (typeof(header
)) (payload
->buffer
.data
+ header_offset
);
342 header
->length
= payload
->buffer
.size
- size_before_payload
;
348 bool lttng_trigger_is_equal(
349 const struct lttng_trigger
*a
, const struct lttng_trigger
*b
)
351 if (!!a
->name
!= !!b
->name
) {
352 /* Both must be either anonymous or named. */
356 if (a
->name
&& strcmp(a
->name
, b
->name
) != 0) {
360 if (!lttng_condition_is_equal(a
->condition
, b
->condition
)) {
364 if (!lttng_action_is_equal(a
->action
, b
->action
)) {
368 if (!lttng_credentials_is_equal(lttng_trigger_get_credentials(a
),
369 lttng_trigger_get_credentials(b
))) {
377 enum lttng_trigger_status
lttng_trigger_set_name(struct lttng_trigger
*trigger
,
380 char *name_copy
= NULL
;
381 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
384 status
= LTTNG_TRIGGER_STATUS_INVALID
;
389 name_copy
= strdup(name
);
391 status
= LTTNG_TRIGGER_STATUS_ERROR
;
398 trigger
->name
= name_copy
;
404 enum lttng_trigger_status
lttng_trigger_get_name(
405 const struct lttng_trigger
*trigger
, const char **name
)
407 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
409 if (!trigger
|| !name
) {
410 status
= LTTNG_TRIGGER_STATUS_INVALID
;
414 if (!trigger
->name
) {
415 status
= LTTNG_TRIGGER_STATUS_UNSET
;
418 *name
= trigger
->name
;
424 int lttng_trigger_assign_name(struct lttng_trigger
*dst
,
425 const struct lttng_trigger
*src
)
428 enum lttng_trigger_status status
;
430 status
= lttng_trigger_set_name(dst
, src
->name
);
431 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
433 ERR("Failed to set name for trigger");
441 void lttng_trigger_set_tracer_token(struct lttng_trigger
*trigger
,
445 LTTNG_OPTIONAL_SET(&trigger
->tracer_token
, token
);
449 uint64_t lttng_trigger_get_tracer_token(const struct lttng_trigger
*trigger
)
453 return LTTNG_OPTIONAL_GET(trigger
->tracer_token
);
457 int lttng_trigger_generate_name(struct lttng_trigger
*trigger
,
461 char *generated_name
= NULL
;
463 ret
= asprintf(&generated_name
, "trigger%" PRIu64
"", unique_id
);
465 ERR("Failed to generate trigger name");
472 trigger
->name
= generated_name
;
478 void lttng_trigger_get(struct lttng_trigger
*trigger
)
480 urcu_ref_get(&trigger
->ref
);
484 void lttng_trigger_put(struct lttng_trigger
*trigger
)
490 urcu_ref_put(&trigger
->ref
, trigger_destroy_ref
);
493 static void delete_trigger_array_element(void *ptr
)
495 struct lttng_trigger
*trigger
= ptr
;
497 lttng_trigger_put(trigger
);
501 struct lttng_triggers
*lttng_triggers_create(void)
503 struct lttng_triggers
*triggers
= NULL
;
505 triggers
= zmalloc(sizeof(*triggers
));
510 lttng_dynamic_pointer_array_init(&triggers
->array
, delete_trigger_array_element
);
517 struct lttng_trigger
*lttng_triggers_borrow_mutable_at_index(
518 const struct lttng_triggers
*triggers
, unsigned int index
)
520 struct lttng_trigger
*trigger
= NULL
;
523 if (index
>= lttng_dynamic_pointer_array_get_count(&triggers
->array
)) {
527 trigger
= (struct lttng_trigger
*)
528 lttng_dynamic_pointer_array_get_pointer(
529 &triggers
->array
, index
);
535 int lttng_triggers_add(
536 struct lttng_triggers
*triggers
, struct lttng_trigger
*trigger
)
543 lttng_trigger_get(trigger
);
545 ret
= lttng_dynamic_pointer_array_add_pointer(&triggers
->array
, trigger
);
547 lttng_trigger_put(trigger
);
553 const struct lttng_trigger
*lttng_triggers_get_at_index(
554 const struct lttng_triggers
*triggers
, unsigned int index
)
556 return lttng_triggers_borrow_mutable_at_index(triggers
, index
);
559 enum lttng_trigger_status
lttng_triggers_get_count(const struct lttng_triggers
*triggers
, unsigned int *count
)
561 enum lttng_trigger_status status
= LTTNG_TRIGGER_STATUS_OK
;
563 if (!triggers
|| !count
) {
564 status
= LTTNG_TRIGGER_STATUS_INVALID
;
568 *count
= lttng_dynamic_pointer_array_get_count(&triggers
->array
);
573 void lttng_triggers_destroy(struct lttng_triggers
*triggers
)
579 lttng_dynamic_pointer_array_reset(&triggers
->array
);
583 int lttng_triggers_serialize(const struct lttng_triggers
*triggers
,
584 struct lttng_payload
*payload
)
587 unsigned int i
, count
;
588 size_t size_before_payload
;
589 struct lttng_triggers_comm triggers_comm
= {};
590 struct lttng_triggers_comm
*header
;
591 enum lttng_trigger_status status
;
592 const size_t header_offset
= payload
->buffer
.size
;
594 status
= lttng_triggers_get_count(triggers
, &count
);
595 if (status
!= LTTNG_TRIGGER_STATUS_OK
) {
596 ret
= LTTNG_ERR_INVALID
;
600 triggers_comm
.count
= count
;
602 /* Placeholder header; updated at the end. */
603 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &triggers_comm
,
604 sizeof(triggers_comm
));
609 size_before_payload
= payload
->buffer
.size
;
611 for (i
= 0; i
< count
; i
++) {
612 const struct lttng_trigger
*trigger
=
613 lttng_triggers_get_at_index(triggers
, i
);
617 ret
= lttng_trigger_serialize(trigger
, payload
);
623 /* Update payload size. */
624 header
= (struct lttng_triggers_comm
*) ((char *) payload
->buffer
.data
+ header_offset
);
625 header
->length
= payload
->buffer
.size
- size_before_payload
;
631 ssize_t
lttng_triggers_create_from_payload(
632 struct lttng_payload_view
*src_view
,
633 struct lttng_triggers
**triggers
)
635 ssize_t ret
, offset
= 0, triggers_size
= 0;
637 const struct lttng_triggers_comm
*triggers_comm
;
638 struct lttng_triggers
*local_triggers
= NULL
;
640 if (!src_view
|| !triggers
) {
645 /* lttng_trigger_comms header */
646 triggers_comm
= (const struct lttng_triggers_comm
*) src_view
->buffer
.data
;
647 offset
+= sizeof(*triggers_comm
);
649 local_triggers
= lttng_triggers_create();
650 if (!local_triggers
) {
655 for (i
= 0; i
< triggers_comm
->count
; i
++) {
656 struct lttng_trigger
*trigger
= NULL
;
657 struct lttng_payload_view trigger_view
=
658 lttng_payload_view_from_view(src_view
, offset
, -1);
659 ssize_t trigger_size
;
661 trigger_size
= lttng_trigger_create_from_payload(
662 &trigger_view
, &trigger
);
663 if (trigger_size
< 0) {
668 /* Transfer ownership of the trigger to the collection. */
669 ret
= lttng_triggers_add(local_triggers
, trigger
);
670 lttng_trigger_put(trigger
);
676 offset
+= trigger_size
;
677 triggers_size
+= trigger_size
;
680 /* Unexpected size of inner-elements; the buffer is corrupted. */
681 if ((ssize_t
) triggers_comm
->length
!= triggers_size
) {
686 /* Pass ownership to caller. */
687 *triggers
= local_triggers
;
688 local_triggers
= NULL
;
693 lttng_triggers_destroy(local_triggers
);
698 const struct lttng_credentials
*lttng_trigger_get_credentials(
699 const struct lttng_trigger
*trigger
)
701 return &trigger
->creds
;
705 void lttng_trigger_set_credentials(struct lttng_trigger
*trigger
,
706 const struct lttng_credentials
*creds
)
709 trigger
->creds
= *creds
;
712 enum lttng_trigger_status
lttng_trigger_set_owner_uid(
713 struct lttng_trigger
*trigger
, uid_t uid
)
715 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
716 const uid_t euid
= geteuid();
717 const struct lttng_credentials creds
= {
718 .uid
= LTTNG_OPTIONAL_INIT_VALUE(uid
),
719 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
723 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
727 /* Client-side validation only to report a clearer error. */
728 if (euid
!= 0 && euid
!= uid
) {
729 ret
= LTTNG_TRIGGER_STATUS_PERMISSION_DENIED
;
733 lttng_trigger_set_credentials(trigger
, &creds
);
739 enum lttng_trigger_status
lttng_trigger_get_owner_uid(
740 const struct lttng_trigger
*trigger
, uid_t
*uid
)
742 enum lttng_trigger_status ret
= LTTNG_TRIGGER_STATUS_OK
;
743 const struct lttng_credentials
*creds
= NULL
;
745 if (!trigger
|| !uid
) {
746 ret
= LTTNG_TRIGGER_STATUS_INVALID
;
750 if (!trigger
->creds
.uid
.is_set
) {
751 ret
= LTTNG_TRIGGER_STATUS_UNSET
;
755 creds
= lttng_trigger_get_credentials(trigger
);
756 *uid
= lttng_credentials_get_uid(creds
);
763 enum lttng_domain_type
lttng_trigger_get_underlying_domain_type_restriction(
764 const struct lttng_trigger
*trigger
)
766 enum lttng_domain_type type
= LTTNG_DOMAIN_NONE
;
767 const struct lttng_event_rule
*event_rule
;
768 enum lttng_condition_status c_status
;
769 enum lttng_condition_type c_type
;
772 assert(trigger
->condition
);
774 c_type
= lttng_condition_get_type(trigger
->condition
);
775 assert (c_type
!= LTTNG_CONDITION_TYPE_UNKNOWN
);
778 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
779 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
780 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
781 /* Apply to any domain. */
782 type
= LTTNG_DOMAIN_NONE
;
784 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
785 /* Return the domain of the event rule. */
786 c_status
= lttng_condition_event_rule_matches_get_rule(
787 trigger
->condition
, &event_rule
);
788 assert(c_status
== LTTNG_CONDITION_STATUS_OK
);
789 type
= lttng_event_rule_get_domain_type(event_rule
);
791 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
792 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
793 /* Return the domain of the channel being monitored. */
794 c_status
= lttng_condition_buffer_usage_get_domain_type(
795 trigger
->condition
, &type
);
796 assert(c_status
== LTTNG_CONDITION_STATUS_OK
);
806 * Generate bytecode related to the trigger.
807 * On success LTTNG_OK. On error, returns lttng_error code.
810 enum lttng_error_code
lttng_trigger_generate_bytecode(
811 struct lttng_trigger
*trigger
,
812 const struct lttng_credentials
*creds
)
814 enum lttng_error_code ret
;
815 struct lttng_condition
*condition
= NULL
;
817 condition
= lttng_trigger_get_condition(trigger
);
819 ret
= LTTNG_ERR_INVALID_TRIGGER
;
823 switch (lttng_condition_get_type(condition
)) {
824 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
826 struct lttng_event_rule
*event_rule
;
827 const enum lttng_condition_status condition_status
=
828 lttng_condition_event_rule_matches_borrow_rule_mutable(
829 condition
, &event_rule
);
831 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
833 /* Generate the filter bytecode. */
834 ret
= lttng_event_rule_generate_filter_bytecode(
836 if (ret
!= LTTNG_OK
) {
840 /* Generate the capture bytecode. */
841 ret
= lttng_condition_event_rule_matches_generate_capture_descriptor_bytecode(
843 if (ret
!= LTTNG_OK
) {
859 struct lttng_trigger
*lttng_trigger_copy(const struct lttng_trigger
*trigger
)
862 struct lttng_payload copy_buffer
;
863 struct lttng_condition
*condition_copy
= NULL
;
864 struct lttng_action
*action_copy
= NULL
;
865 struct lttng_trigger
*copy
= NULL
;
866 enum lttng_trigger_status trigger_status
;
867 const char *trigger_name
;
868 uid_t trigger_owner_uid
;
870 lttng_payload_init(©_buffer
);
872 ret
= lttng_condition_serialize(trigger
->condition
, ©_buffer
);
878 struct lttng_payload_view view
=
879 lttng_payload_view_from_payload(
880 ©_buffer
, 0, -1);
882 ret
= lttng_condition_create_from_payload(
883 &view
, &condition_copy
);
889 lttng_payload_clear(©_buffer
);
891 ret
= lttng_action_serialize(trigger
->action
, ©_buffer
);
897 struct lttng_payload_view view
=
898 lttng_payload_view_from_payload(
899 ©_buffer
, 0, -1);
901 ret
= lttng_action_create_from_payload(
902 &view
, &action_copy
);
908 copy
= lttng_trigger_create(condition_copy
, action_copy
);
910 ERR("Failed to allocate trigger during trigger copy");
914 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
915 switch (trigger_status
) {
916 case LTTNG_TRIGGER_STATUS_OK
:
917 trigger_status
= lttng_trigger_set_name(copy
, trigger_name
);
918 if (trigger_status
!= LTTNG_TRIGGER_STATUS_OK
) {
919 ERR("Failed to set name of new trigger during copy");
920 goto error_cleanup_trigger
;
923 case LTTNG_TRIGGER_STATUS_UNSET
:
926 ERR("Failed to get name of original trigger during copy");
927 goto error_cleanup_trigger
;
930 trigger_status
= lttng_trigger_get_owner_uid(
931 trigger
, &trigger_owner_uid
);
932 switch (trigger_status
) {
933 case LTTNG_TRIGGER_STATUS_OK
:
934 LTTNG_OPTIONAL_SET(©
->creds
.uid
, trigger_owner_uid
);
936 case LTTNG_TRIGGER_STATUS_UNSET
:
939 ERR("Failed to get owner uid of original trigger during copy");
940 goto error_cleanup_trigger
;
943 copy
->tracer_token
= trigger
->tracer_token
;
944 copy
->registered
= trigger
->registered
;
947 error_cleanup_trigger
:
948 lttng_trigger_destroy(copy
);
951 lttng_condition_put(condition_copy
);
952 lttng_action_put(action_copy
);
953 lttng_payload_reset(©_buffer
);
958 bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger
*trigger
)
960 bool needs_tracer_notifier
= false;
961 const struct lttng_condition
*condition
=
962 lttng_trigger_get_const_condition(trigger
);
964 switch (lttng_condition_get_type(condition
)) {
965 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
:
966 needs_tracer_notifier
= true;
968 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
969 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
970 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
971 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
972 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
974 case LTTNG_CONDITION_TYPE_UNKNOWN
:
979 return needs_tracer_notifier
;
983 void lttng_trigger_set_as_registered(struct lttng_trigger
*trigger
)
985 pthread_mutex_lock(&trigger
->lock
);
986 trigger
->registered
= true;
987 pthread_mutex_unlock(&trigger
->lock
);
991 void lttng_trigger_set_as_unregistered(struct lttng_trigger
*trigger
)
993 pthread_mutex_lock(&trigger
->lock
);
994 trigger
->registered
= false;
995 pthread_mutex_unlock(&trigger
->lock
);
999 * The trigger must be locked before calling lttng_trigger_registered.
1000 * The lock is necessary since a trigger can be unregistered at anytime.
1001 * Manipulations requiring that the trigger be registered must always acquire
1002 * the trigger lock for the duration of the manipulation using
1003 * `lttng_trigger_lock` and `lttng_trigger_unlock`.
1006 bool lttng_trigger_is_registered(struct lttng_trigger
*trigger
)
1008 ASSERT_LOCKED(trigger
->lock
);
1009 return trigger
->registered
;
1013 void lttng_trigger_lock(struct lttng_trigger
*trigger
)
1015 pthread_mutex_lock(&trigger
->lock
);
1019 void lttng_trigger_unlock(struct lttng_trigger
*trigger
)
1021 pthread_mutex_unlock(&trigger
->lock
);
1025 enum lttng_error_code
lttng_trigger_mi_serialize(const struct lttng_trigger
*trigger
,
1026 struct mi_writer
*writer
,
1027 const struct mi_lttng_error_query_callbacks
1028 *error_query_callbacks
)
1031 enum lttng_error_code ret_code
;
1032 enum lttng_trigger_status trigger_status
;
1033 const struct lttng_condition
*condition
= NULL
;
1034 const struct lttng_action
*action
= NULL
;
1035 struct lttng_dynamic_array action_path_indexes
;
1041 lttng_dynamic_array_init(&action_path_indexes
, sizeof(uint64_t), NULL
);
1043 /* Open trigger element. */
1044 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_trigger
);
1049 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &owner_uid
);
1050 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1053 ret
= mi_lttng_writer_write_element_string(
1054 writer
, config_element_name
, trigger
->name
);
1060 ret
= mi_lttng_writer_write_element_signed_int(writer
,
1061 mi_lttng_element_trigger_owner_uid
,
1062 (int64_t) owner_uid
);
1068 condition
= lttng_trigger_get_const_condition(trigger
);
1070 ret_code
= lttng_condition_mi_serialize(
1071 trigger
, condition
, writer
, error_query_callbacks
);
1072 if (ret_code
!= LTTNG_OK
) {
1077 action
= lttng_trigger_get_const_action(trigger
);
1079 ret_code
= lttng_action_mi_serialize(trigger
, action
, writer
,
1080 error_query_callbacks
, &action_path_indexes
);
1081 if (ret_code
!= LTTNG_OK
) {
1085 if (error_query_callbacks
&& error_query_callbacks
->trigger_cb
) {
1086 struct lttng_error_query_results
*results
= NULL
;
1088 ret_code
= error_query_callbacks
->trigger_cb(trigger
, &results
);
1089 if (ret_code
!= LTTNG_OK
) {
1093 ret_code
= lttng_error_query_results_mi_serialize(
1095 lttng_error_query_results_destroy(results
);
1096 if (ret_code
!= LTTNG_OK
) {
1101 /* Close trigger element. */
1102 ret
= mi_lttng_writer_close_element(writer
);
1107 ret_code
= LTTNG_OK
;
1111 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
1113 lttng_dynamic_array_reset(&action_path_indexes
);
1117 /* Used by qsort, which expects the semantics of strcmp(). */
1118 static int compare_triggers_by_name(const void *a
, const void *b
)
1120 const struct lttng_trigger
*trigger_a
=
1121 *((const struct lttng_trigger
**) a
);
1122 const struct lttng_trigger
*trigger_b
=
1123 *((const struct lttng_trigger
**) b
);
1124 const char *name_a
, *name_b
;
1125 enum lttng_trigger_status trigger_status
;
1127 /* Anonymous triggers are not reachable here. */
1128 trigger_status
= lttng_trigger_get_name(trigger_a
, &name_a
);
1129 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1131 trigger_status
= lttng_trigger_get_name(trigger_b
, &name_b
);
1132 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1134 return strcmp(name_a
, name_b
);
1138 enum lttng_error_code
lttng_triggers_mi_serialize(const struct lttng_triggers
*triggers
,
1139 struct mi_writer
*writer
,
1140 const struct mi_lttng_error_query_callbacks
1141 *error_query_callbacks
)
1144 enum lttng_error_code ret_code
;
1145 enum lttng_trigger_status status
;
1146 unsigned int count
, i
;
1147 struct lttng_dynamic_pointer_array sorted_triggers
;
1153 * Sort trigger by name to ensure an order at the MI level and ignore
1154 * any anonymous trigger present.
1156 lttng_dynamic_pointer_array_init(&sorted_triggers
, NULL
);
1158 status
= lttng_triggers_get_count(triggers
, &count
);
1159 assert(status
== LTTNG_TRIGGER_STATUS_OK
);
1161 for (i
= 0; i
< count
; i
++) {
1163 const char *unused_name
;
1164 const struct lttng_trigger
*trigger
=
1165 lttng_triggers_get_at_index(triggers
, i
);
1167 status
= lttng_trigger_get_name(trigger
, &unused_name
);
1169 case LTTNG_TRIGGER_STATUS_OK
:
1171 case LTTNG_TRIGGER_STATUS_UNSET
:
1172 /* Don't list anonymous triggers. */
1178 add_ret
= lttng_dynamic_pointer_array_add_pointer(
1179 &sorted_triggers
, (void *) trigger
);
1182 ERR("Failed to lttng_trigger to sorting array.");
1183 ret_code
= LTTNG_ERR_NOMEM
;
1188 qsort(sorted_triggers
.array
.buffer
.data
, count
,
1189 sizeof(struct lttng_trigger
*),
1190 compare_triggers_by_name
);
1192 /* Open triggers element. */
1193 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_triggers
);
1195 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
1199 for (i
= 0; i
< lttng_dynamic_pointer_array_get_count(&sorted_triggers
); i
++) {
1200 const struct lttng_trigger
*trigger
=
1201 (const struct lttng_trigger
*)
1202 lttng_dynamic_pointer_array_get_pointer(
1203 &sorted_triggers
, i
);
1205 lttng_trigger_mi_serialize(trigger
, writer
, error_query_callbacks
);
1208 /* Close triggers element. */
1209 ret
= mi_lttng_writer_close_element(writer
);
1211 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
1215 ret_code
= LTTNG_OK
;
1218 lttng_dynamic_pointer_array_reset(&sorted_triggers
);